| 1 |
//===- unittest/ASTMatchers/Dynamic/VariantValueTest.cpp - VariantValue unit tests -===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===-----------------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "../ASTMatchersTest.h" |
| 10 |
#include "clang/ASTMatchers/Dynamic/VariantValue.h" |
| 11 |
#include "gtest/gtest.h" |
| 12 |
|
| 13 |
namespace clang { |
| 14 |
namespace ast_matchers { |
| 15 |
namespace dynamic { |
| 16 |
namespace { |
| 17 |
|
| 18 |
TEST(VariantValueTest, Unsigned) { |
| 19 |
const unsigned kUnsigned = 17; |
| 20 |
VariantValue Value = kUnsigned; |
| 21 |
|
| 22 |
EXPECT_TRUE(Value.isUnsigned()); |
| 23 |
EXPECT_EQ(kUnsigned, Value.getUnsigned()); |
| 24 |
|
| 25 |
EXPECT_TRUE(Value.hasValue()); |
| 26 |
EXPECT_FALSE(Value.isString()); |
| 27 |
EXPECT_FALSE(Value.isMatcher()); |
| 28 |
} |
| 29 |
|
| 30 |
TEST(VariantValueTest, String) { |
| 31 |
const StringRef kString = "string"; |
| 32 |
VariantValue Value = kString; |
| 33 |
|
| 34 |
EXPECT_TRUE(Value.isString()); |
| 35 |
EXPECT_EQ(kString, Value.getString()); |
| 36 |
EXPECT_EQ("String", Value.getTypeAsString()); |
| 37 |
|
| 38 |
EXPECT_TRUE(Value.hasValue()); |
| 39 |
EXPECT_FALSE(Value.isUnsigned()); |
| 40 |
EXPECT_FALSE(Value.isMatcher()); |
| 41 |
} |
| 42 |
|
| 43 |
TEST(VariantValueTest, DynTypedMatcher) { |
| 44 |
VariantValue Value = VariantMatcher::SingleMatcher(stmt()); |
| 45 |
|
| 46 |
EXPECT_TRUE(Value.hasValue()); |
| 47 |
EXPECT_FALSE(Value.isUnsigned()); |
| 48 |
EXPECT_FALSE(Value.isString()); |
| 49 |
|
| 50 |
EXPECT_TRUE(Value.isMatcher()); |
| 51 |
EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>()); |
| 52 |
EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<UnaryOperator>()); |
| 53 |
EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString()); |
| 54 |
|
| 55 |
// Can only convert to compatible matchers. |
| 56 |
Value = VariantMatcher::SingleMatcher(recordDecl()); |
| 57 |
EXPECT_TRUE(Value.isMatcher()); |
| 58 |
EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>()); |
| 59 |
EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>()); |
| 60 |
EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); |
| 61 |
|
| 62 |
Value = VariantMatcher::SingleMatcher(ignoringImpCasts(expr())); |
| 63 |
EXPECT_TRUE(Value.isMatcher()); |
| 64 |
EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>()); |
| 65 |
EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Stmt>()); |
| 66 |
EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Expr>()); |
| 67 |
EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<IntegerLiteral>()); |
| 68 |
EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<GotoStmt>()); |
| 69 |
EXPECT_EQ("Matcher<Expr>", Value.getTypeAsString()); |
| 70 |
} |
| 71 |
|
| 72 |
TEST(VariantValueTest, Assignment) { |
| 73 |
VariantValue Value = StringRef("A"); |
| 74 |
EXPECT_TRUE(Value.isString()); |
| 75 |
EXPECT_EQ("A", Value.getString()); |
| 76 |
EXPECT_TRUE(Value.hasValue()); |
| 77 |
EXPECT_FALSE(Value.isBoolean()); |
| 78 |
EXPECT_FALSE(Value.isDouble()); |
| 79 |
EXPECT_FALSE(Value.isUnsigned()); |
| 80 |
EXPECT_FALSE(Value.isMatcher()); |
| 81 |
EXPECT_EQ("String", Value.getTypeAsString()); |
| 82 |
|
| 83 |
Value = VariantMatcher::SingleMatcher(recordDecl()); |
| 84 |
EXPECT_TRUE(Value.hasValue()); |
| 85 |
EXPECT_FALSE(Value.isBoolean()); |
| 86 |
EXPECT_FALSE(Value.isDouble()); |
| 87 |
EXPECT_FALSE(Value.isUnsigned()); |
| 88 |
EXPECT_FALSE(Value.isString()); |
| 89 |
EXPECT_TRUE(Value.isMatcher()); |
| 90 |
EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>()); |
| 91 |
EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>()); |
| 92 |
EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); |
| 93 |
|
| 94 |
Value = true; |
| 95 |
EXPECT_TRUE(Value.isBoolean()); |
| 96 |
EXPECT_EQ(true, Value.getBoolean()); |
| 97 |
EXPECT_TRUE(Value.hasValue()); |
| 98 |
EXPECT_FALSE(Value.isUnsigned()); |
| 99 |
EXPECT_FALSE(Value.isMatcher()); |
| 100 |
EXPECT_FALSE(Value.isString()); |
| 101 |
|
| 102 |
Value = 3.14; |
| 103 |
EXPECT_TRUE(Value.isDouble()); |
| 104 |
EXPECT_EQ(3.14, Value.getDouble()); |
| 105 |
EXPECT_TRUE(Value.hasValue()); |
| 106 |
EXPECT_FALSE(Value.isBoolean()); |
| 107 |
EXPECT_FALSE(Value.isUnsigned()); |
| 108 |
EXPECT_FALSE(Value.isMatcher()); |
| 109 |
EXPECT_FALSE(Value.isString()); |
| 110 |
|
| 111 |
Value = 17; |
| 112 |
EXPECT_TRUE(Value.isUnsigned()); |
| 113 |
EXPECT_EQ(17U, Value.getUnsigned()); |
| 114 |
EXPECT_FALSE(Value.isBoolean()); |
| 115 |
EXPECT_FALSE(Value.isDouble()); |
| 116 |
EXPECT_TRUE(Value.hasValue()); |
| 117 |
EXPECT_FALSE(Value.isMatcher()); |
| 118 |
EXPECT_FALSE(Value.isString()); |
| 119 |
|
| 120 |
Value = VariantValue(); |
| 121 |
EXPECT_FALSE(Value.hasValue()); |
| 122 |
EXPECT_FALSE(Value.isBoolean()); |
| 123 |
EXPECT_FALSE(Value.isDouble()); |
| 124 |
EXPECT_FALSE(Value.isUnsigned()); |
| 125 |
EXPECT_FALSE(Value.isString()); |
| 126 |
EXPECT_FALSE(Value.isMatcher()); |
| 127 |
EXPECT_EQ("Nothing", Value.getTypeAsString()); |
| 128 |
} |
| 129 |
|
| 130 |
TEST(VariantValueTest, ImplicitBool) { |
| 131 |
VariantValue Value; |
| 132 |
bool IfTrue = false; |
| 133 |
if (Value) { |
| 134 |
IfTrue = true; |
| 135 |
} |
| 136 |
EXPECT_FALSE(IfTrue); |
| 137 |
EXPECT_TRUE(!Value); |
| 138 |
|
| 139 |
Value = StringRef(); |
| 140 |
IfTrue = false; |
| 141 |
if (Value) { |
| 142 |
IfTrue = true; |
| 143 |
} |
| 144 |
EXPECT_TRUE(IfTrue); |
| 145 |
EXPECT_FALSE(!Value); |
| 146 |
} |
| 147 |
|
| 148 |
TEST(VariantValueTest, Matcher) { |
| 149 |
EXPECT_TRUE(matches("class X {};", VariantValue(VariantMatcher::SingleMatcher( |
| 150 |
recordDecl(hasName("X")))) |
| 151 |
.getMatcher() |
| 152 |
.getTypedMatcher<Decl>())); |
| 153 |
EXPECT_TRUE( |
| 154 |
matches("int x;", VariantValue(VariantMatcher::SingleMatcher(varDecl())) |
| 155 |
.getMatcher() |
| 156 |
.getTypedMatcher<Decl>())); |
| 157 |
EXPECT_TRUE( |
| 158 |
matches("int foo() { return 1 + 1; }", |
| 159 |
VariantValue(VariantMatcher::SingleMatcher(functionDecl())) |
| 160 |
.getMatcher() |
| 161 |
.getTypedMatcher<Decl>())); |
| 162 |
// Can't get the wrong matcher. |
| 163 |
EXPECT_FALSE(VariantValue(VariantMatcher::SingleMatcher(varDecl())) |
| 164 |
.getMatcher() |
| 165 |
.hasTypedMatcher<Stmt>()); |
| 166 |
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST |
| 167 |
// Trying to get the wrong matcher fails an assertion in Matcher<T>. We don't |
| 168 |
// do this test when building with MSVC because its debug C runtime prints the |
| 169 |
// assertion failure message as a wide string, which gtest doesn't understand. |
| 170 |
EXPECT_DEATH(VariantValue(VariantMatcher::SingleMatcher(varDecl())) |
| 171 |
.getMatcher() |
| 172 |
.getTypedMatcher<Stmt>(), |
| 173 |
"hasTypedMatcher"); |
| 174 |
#endif |
| 175 |
|
| 176 |
EXPECT_FALSE(matches( |
| 177 |
"int x;", VariantValue(VariantMatcher::SingleMatcher(functionDecl())) |
| 178 |
.getMatcher() |
| 179 |
.getTypedMatcher<Decl>())); |
| 180 |
EXPECT_FALSE( |
| 181 |
matches("int foo() { return 1 + 1; }", |
| 182 |
VariantValue(VariantMatcher::SingleMatcher(declRefExpr())) |
| 183 |
.getMatcher() |
| 184 |
.getTypedMatcher<Stmt>())); |
| 185 |
} |
| 186 |
|
| 187 |
TEST(VariantValueTest, NodeKind) { |
| 188 |
VariantValue Value = ASTNodeKind::getFromNodeKind<Stmt>(); |
| 189 |
EXPECT_TRUE(Value.isNodeKind()); |
| 190 |
EXPECT_TRUE(Value.getNodeKind().isSame(ASTNodeKind::getFromNodeKind<Stmt>())); |
| 191 |
|
| 192 |
Value = ASTNodeKind::getFromNodeKind<CXXMethodDecl>(); |
| 193 |
EXPECT_TRUE(Value.isNodeKind()); |
| 194 |
EXPECT_TRUE(Value.getNodeKind().isSame( |
| 195 |
ASTNodeKind::getFromNodeKind<CXXMethodDecl>())); |
| 196 |
|
| 197 |
Value.setNodeKind(ASTNodeKind::getFromNodeKind<PointerType>()); |
| 198 |
EXPECT_TRUE(Value.isNodeKind()); |
| 199 |
EXPECT_TRUE( |
| 200 |
Value.getNodeKind().isSame(ASTNodeKind::getFromNodeKind<PointerType>())); |
| 201 |
|
| 202 |
Value = 42; |
| 203 |
EXPECT_TRUE(!Value.isNodeKind()); |
| 204 |
} |
| 205 |
|
| 206 |
} // end anonymous namespace |
| 207 |
} // end namespace dynamic |
| 208 |
} // end namespace ast_matchers |
| 209 |
} // end namespace clang |