summaryrefslogtreecommitdiffstats
path: root/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/unittests/ASTMatchers/ASTMatchersTest.cpp')
-rw-r--r--clang/unittests/ASTMatchers/ASTMatchersTest.cpp105
1 files changed, 104 insertions, 1 deletions
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
index a777e7f497c..63d080563e4 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -1209,7 +1209,12 @@ TEST(Matcher, FlowControl) {
EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
continueStmt()));
EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
- EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
+ EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}",
+ labelStmt(
+ hasDeclaration(
+ labelDecl(hasName("FOO"))))));
+ EXPECT_TRUE(matches("void f() { FOO: ; void *ptr = &&FOO; goto *ptr; }",
+ addrLabelExpr()));
EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
}
@@ -2527,6 +2532,82 @@ TEST(Matcher, GNUNullExpr) {
EXPECT_TRUE(matches("int* i = __null;", gnuNullExpr()));
}
+TEST(Matcher, AtomicExpr) {
+ EXPECT_TRUE(matches("void foo() { int *ptr; __atomic_load_n(ptr, 1); }",
+ atomicExpr()));
+}
+
+TEST(Matcher, Initializers) {
+ const char *ToMatch = "void foo() { struct point { double x; double y; };"
+ " struct point ptarray[10] = "
+ " { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }";
+ EXPECT_TRUE(matchesConditionally(
+ ToMatch,
+ initListExpr(
+ has(
+ cxxConstructExpr(
+ requiresZeroInitialization())),
+ has(
+ initListExpr(
+ hasType(asString("struct point")),
+ has(floatLiteral(equals(1.0))),
+ has(implicitValueInitExpr(
+ hasType(asString("double")))))),
+ has(
+ initListExpr(
+ hasType(asString("struct point")),
+ has(floatLiteral(equals(2.0))),
+ has(floatLiteral(equals(1.0)))))
+ ), true, "-std=gnu++98"));
+
+ EXPECT_TRUE(matchesC99(ToMatch,
+ initListExpr(
+ hasSyntacticForm(
+ initListExpr(
+ has(
+ designatedInitExpr(
+ designatorCountIs(2),
+ has(floatLiteral(
+ equals(1.0))),
+ has(integerLiteral(
+ equals(2))))),
+ has(
+ designatedInitExpr(
+ designatorCountIs(2),
+ has(floatLiteral(
+ equals(2.0))),
+ has(integerLiteral(
+ equals(2))))),
+ has(
+ designatedInitExpr(
+ designatorCountIs(2),
+ has(floatLiteral(
+ equals(1.0))),
+ has(integerLiteral(
+ equals(0)))))
+ )))));
+}
+
+TEST(Matcher, ParenListExpr) {
+ EXPECT_TRUE(
+ matches(
+ " template<typename T> class foo { void bar() { foo X(*this); } }; ",
+ varDecl(hasInitializer(parenListExpr(has(unaryOperator()))))));
+}
+
+TEST(Matcher, StmtExpr) {
+ EXPECT_TRUE(matches("void declToImport() { int C = ({int X=4; X;}); }",
+ varDecl(hasInitializer(stmtExpr()))));
+}
+
+TEST(Matcher, ImportPredefinedExpr) {
+ // __func__ expands as StringLiteral("foo")
+ EXPECT_TRUE(matches("void foo() { __func__; }",
+ predefinedExpr(
+ hasType(asString("const char [4]")),
+ has(stringLiteral()))));
+}
+
TEST(Matcher, AsmStatement) {
EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
}
@@ -2750,6 +2831,28 @@ TEST(Matcher, ConditionalOperator) {
EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
EXPECT_TRUE(
notMatches("void x() { true ? false : true; }", ConditionalFalse));
+
+ EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
+ EXPECT_TRUE(
+ notMatches("void x() { true ? false : true; }", ConditionalFalse));
+}
+
+TEST(Matcher, BinaryConditionalOperator) {
+ StatementMatcher AlwaysOne = binaryConditionalOperator(
+ hasCondition(implicitCastExpr(
+ has(
+ opaqueValueExpr(
+ hasSourceExpression((integerLiteral(equals(1)))))))),
+ hasFalseExpression(integerLiteral(equals(0))));
+
+ EXPECT_TRUE(matches("void x() { 1 ?: 0; }", AlwaysOne));
+
+ StatementMatcher FourNotFive = binaryConditionalOperator(
+ hasTrueExpression(opaqueValueExpr(
+ hasSourceExpression((integerLiteral(equals(4)))))),
+ hasFalseExpression(integerLiteral(equals(5))));
+
+ EXPECT_TRUE(matches("void x() { 4 ?: 5; }", FourNotFive));
}
TEST(ArraySubscriptMatchers, ArraySubscripts) {
OpenPOWER on IntegriCloud