r246860 - Fixing a bug where hasType(decl()) would fail to match on C code involving structs or unions.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 4 11:34:49 PDT 2015


Author: aaronballman
Date: Fri Sep  4 13:34:48 2015
New Revision: 246860

URL: http://llvm.org/viewvc/llvm-project?rev=246860&view=rev
Log:
Fixing a bug where hasType(decl()) would fail to match on C code involving structs or unions.

Modified:
    cfe/trunk/include/clang/ASTMatchers/ASTMatchersInternal.h
    cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp
    cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h

Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchersInternal.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ASTMatchers/ASTMatchersInternal.h?rev=246860&r1=246859&r2=246860&view=diff
==============================================================================
--- cfe/trunk/include/clang/ASTMatchers/ASTMatchersInternal.h (original)
+++ cfe/trunk/include/clang/ASTMatchers/ASTMatchersInternal.h Fri Sep  4 13:34:48 2015
@@ -668,16 +668,14 @@ private:
     return matchesDecl(Node.getDecl(), Finder, Builder);
   }
 
-  /// \brief Extracts the CXXRecordDecl or EnumDecl of a QualType and returns
-  /// whether the inner matcher matches on it.
+  /// \brief Extracts the TagDecl of a QualType and returns whether the inner
+  /// matcher matches on it.
   bool matchesSpecialized(const QualType &Node, ASTMatchFinder *Finder,
                           BoundNodesTreeBuilder *Builder) const {
     /// FIXME: Add other ways to convert...
     if (Node.isNull())
       return false;
-    if (const EnumType *AsEnum = dyn_cast<EnumType>(Node.getTypePtr()))
-      return matchesDecl(AsEnum->getDecl(), Finder, Builder);
-    return matchesDecl(Node->getAsCXXRecordDecl(), Finder, Builder);
+    return matchesDecl(Node->getAsTagDecl(), Finder, Builder);
   }
 
   /// \brief Gets the TemplateDecl from a TemplateSpecializationType

Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp?rev=246860&r1=246859&r2=246860&view=diff
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp (original)
+++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp Fri Sep  4 13:34:48 2015
@@ -919,6 +919,9 @@ TEST(TypeMatcher, MatchesClassType) {
 
   EXPECT_TRUE(
       matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
+
+  EXPECT_TRUE(matchesC("struct S {}; void f(void) { struct S s; }",
+                       varDecl(hasType(namedDecl(hasName("S"))))));
 }
 
 TEST(Matcher, BindMatchedNodes) {

Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h?rev=246860&r1=246859&r2=246860&view=diff
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h (original)
+++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h Fri Sep  4 13:34:48 2015
@@ -120,6 +120,12 @@ testing::AssertionResult matchesObjC(con
 }
 
 template <typename T>
+testing::AssertionResult matchesC(const std::string &Code, const T &AMatcher) {
+  return matchesConditionally(Code, AMatcher, true, "", FileContentMappings(),
+                              "input.c");
+}
+
+template <typename T>
 testing::AssertionResult notMatchesObjC(const std::string &Code,
                                      const T &AMatcher) {
   return matchesConditionally(




More information about the cfe-commits mailing list