diff options
| author | James King <jamescking@google.com> | 2021-10-08 17:42:06 +0000 |
|---|---|---|
| committer | Yitzhak Mandelbaum <yitzhakm@google.com> | 2021-10-08 17:42:18 +0000 |
| commit | ac742965628631059af7fdc77e8661fa660ac180 (patch) | |
| tree | 7ade88f1611524b66a068cb345ef31c56faaf2d6 /clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp | |
| parent | 68e49aea9ac4dca550df70706cc845de04939c03 (diff) | |
| download | llvm-ac742965628631059af7fdc77e8661fa660ac180.zip llvm-ac742965628631059af7fdc77e8661fa660ac180.tar.gz llvm-ac742965628631059af7fdc77e8661fa660ac180.tar.bz2 | |
Add `TypeLoc`-related matchers.
Contributes several matchers that involve `TypeLoc`s. These matchers are (in alphabetical order):
- elaboratedTypeLoc
- hasAnyTemplateArgumentLoc
- hasNamedTypeLoc
- hasPointeeLoc
- hasReferentLoc
- hasReturnTypeLoc
- hasTemplateArgumentLoc
- hasUnqualifiedLoc
- pointerTypeLoc
- qualifiedTypeLoc
- referenceTypeLoc
- templateSpecializationTypeLoc
Reviewed By: ymandel, aaron.ballman
Differential Revision: https://reviews.llvm.org/D111242
Diffstat (limited to 'clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp')
| -rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp index ed5f908..92fd530 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp @@ -2078,6 +2078,165 @@ TEST_P(ASTMatchersTest, TypeAliasTemplateDecl) { notMatches(Code, typeAliasTemplateDecl(hasName("typeAliasDecl")))); } +TEST_P(ASTMatchersTest, QualifiedTypeLocTest_BindsToConstIntVarDecl) { + EXPECT_TRUE(matches("const int x = 0;", + qualifiedTypeLoc(loc(asString("const int"))))); +} + +TEST_P(ASTMatchersTest, QualifiedTypeLocTest_BindsToConstIntFunctionDecl) { + EXPECT_TRUE(matches("const int f() { return 5; }", + qualifiedTypeLoc(loc(asString("const int"))))); +} + +TEST_P(ASTMatchersTest, QualifiedTypeLocTest_DoesNotBindToUnqualifiedVarDecl) { + EXPECT_TRUE(notMatches("int x = 0;", qualifiedTypeLoc(loc(asString("int"))))); +} + +TEST_P(ASTMatchersTest, QualifiedTypeLocTest_IntDoesNotBindToConstIntDecl) { + EXPECT_TRUE( + notMatches("const int x = 0;", qualifiedTypeLoc(loc(asString("int"))))); +} + +TEST_P(ASTMatchersTest, QualifiedTypeLocTest_IntDoesNotBindToConstFloatDecl) { + EXPECT_TRUE( + notMatches("const float x = 0;", qualifiedTypeLoc(loc(asString("int"))))); +} + +TEST_P(ASTMatchersTest, PointerTypeLocTest_BindsToAnyPointerTypeLoc) { + auto matcher = varDecl(hasName("x"), hasTypeLoc(pointerTypeLoc())); + EXPECT_TRUE(matches("int* x;", matcher)); + EXPECT_TRUE(matches("float* x;", matcher)); + EXPECT_TRUE(matches("char* x;", matcher)); + EXPECT_TRUE(matches("void* x;", matcher)); +} + +TEST_P(ASTMatchersTest, PointerTypeLocTest_DoesNotBindToNonPointerTypeLoc) { + auto matcher = varDecl(hasName("x"), hasTypeLoc(pointerTypeLoc())); + EXPECT_TRUE(notMatches("int x;", matcher)); + EXPECT_TRUE(notMatches("float x;", matcher)); + EXPECT_TRUE(notMatches("char x;", matcher)); +} + +TEST_P(ASTMatchersTest, ReferenceTypeLocTest_BindsToAnyReferenceTypeLoc) { + if (!GetParam().isCXX()) { + return; + } + auto matcher = varDecl(hasName("r"), hasTypeLoc(referenceTypeLoc())); + EXPECT_TRUE(matches("int rr = 3; int& r = rr;", matcher)); + EXPECT_TRUE(matches("int rr = 3; auto& r = rr;", matcher)); + EXPECT_TRUE(matches("int rr = 3; const int& r = rr;", matcher)); + EXPECT_TRUE(matches("float rr = 3.0; float& r = rr;", matcher)); + EXPECT_TRUE(matches("char rr = 'a'; char& r = rr;", matcher)); +} + +TEST_P(ASTMatchersTest, ReferenceTypeLocTest_DoesNotBindToNonReferenceTypeLoc) { + auto matcher = varDecl(hasName("r"), hasTypeLoc(referenceTypeLoc())); + EXPECT_TRUE(notMatches("int r;", matcher)); + EXPECT_TRUE(notMatches("int r = 3;", matcher)); + EXPECT_TRUE(notMatches("const int r = 3;", matcher)); + EXPECT_TRUE(notMatches("int* r;", matcher)); + EXPECT_TRUE(notMatches("float r;", matcher)); + EXPECT_TRUE(notMatches("char r;", matcher)); +} + +TEST_P(ASTMatchersTest, ReferenceTypeLocTest_BindsToAnyRvalueReferenceTypeLoc) { + if (!GetParam().isCXX()) { + return; + } + auto matcher = varDecl(hasName("r"), hasTypeLoc(referenceTypeLoc())); + EXPECT_TRUE(matches("int&& r = 3;", matcher)); + EXPECT_TRUE(matches("auto&& r = 3;", matcher)); + EXPECT_TRUE(matches("float&& r = 3.0;", matcher)); +} + +TEST_P( + ASTMatchersTest, + TemplateSpecializationTypeLocTest_BindsToTemplateSpecializationExplicitInstantiation) { + if (!GetParam().isCXX()) { + return; + } + EXPECT_TRUE( + matches("template <typename T> class C {}; template class C<int>;", + classTemplateSpecializationDecl( + hasName("C"), hasTypeLoc(templateSpecializationTypeLoc())))); +} + +TEST_P(ASTMatchersTest, + TemplateSpecializationTypeLocTest_BindsToVarDeclTemplateSpecialization) { + if (!GetParam().isCXX()) { + return; + } + EXPECT_TRUE(matches( + "template <typename T> class C {}; C<char> var;", + varDecl(hasName("var"), hasTypeLoc(templateSpecializationTypeLoc())))); +} + +TEST_P( + ASTMatchersTest, + TemplateSpecializationTypeLocTest_DoesNotBindToNonTemplateSpecialization) { + if (!GetParam().isCXX()) { + return; + } + EXPECT_TRUE(notMatches( + "class C {}; C var;", + varDecl(hasName("var"), hasTypeLoc(templateSpecializationTypeLoc())))); +} + +TEST_P(ASTMatchersTest, + ElaboratedTypeLocTest_BindsToElaboratedObjectDeclaration) { + if (!GetParam().isCXX()) { + return; + } + EXPECT_TRUE(matches("class C {}; class C c;", + varDecl(hasName("c"), hasTypeLoc(elaboratedTypeLoc())))); +} + +TEST_P(ASTMatchersTest, + ElaboratedTypeLocTest_BindsToNamespaceElaboratedObjectDeclaration) { + if (!GetParam().isCXX()) { + return; + } + EXPECT_TRUE(matches("namespace N { class D {}; } N::D d;", + varDecl(hasName("d"), hasTypeLoc(elaboratedTypeLoc())))); +} + +TEST_P(ASTMatchersTest, + ElaboratedTypeLocTest_BindsToElaboratedStructDeclaration) { + EXPECT_TRUE(matches("struct s {}; struct s ss;", + varDecl(hasName("ss"), hasTypeLoc(elaboratedTypeLoc())))); +} + +TEST_P(ASTMatchersTest, + ElaboratedTypeLocTest_DoesNotBindToNonElaboratedObjectDeclaration) { + if (!GetParam().isCXX()) { + return; + } + EXPECT_TRUE( + notMatches("class C {}; C c;", + varDecl(hasName("c"), hasTypeLoc(elaboratedTypeLoc())))); +} + +TEST_P( + ASTMatchersTest, + ElaboratedTypeLocTest_DoesNotBindToNamespaceNonElaboratedObjectDeclaration) { + if (!GetParam().isCXX()) { + return; + } + EXPECT_TRUE( + notMatches("namespace N { class D {}; } using N::D; D d;", + varDecl(hasName("d"), hasTypeLoc(elaboratedTypeLoc())))); +} + +TEST_P(ASTMatchersTest, + ElaboratedTypeLocTest_DoesNotBindToNonElaboratedStructDeclaration) { + if (!GetParam().isCXX()) { + return; + } + EXPECT_TRUE( + notMatches("struct s {}; s ss;", + varDecl(hasName("ss"), hasTypeLoc(elaboratedTypeLoc())))); +} + TEST(ASTMatchersTestObjC, ObjCMessageExpr) { // Don't find ObjCMessageExpr where none are present. EXPECT_TRUE(notMatchesObjC("", objcMessageExpr(anything()))); |
