[clang-tools-extra] 32d565b - [clang-tidy] Fix crash in readability-function-cognitive-complexity on weak refs

Zinovy Nis via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 11 08:53:12 PDT 2020


Author: Zinovy Nis
Date: 2020-10-11T18:52:38+03:00
New Revision: 32d565b4618d31511e79dbe77aae8d456f2bf466

URL: https://github.com/llvm/llvm-project/commit/32d565b4618d31511e79dbe77aae8d456f2bf466
DIFF: https://github.com/llvm/llvm-project/commit/32d565b4618d31511e79dbe77aae8d456f2bf466.diff

LOG: [clang-tidy] Fix crash in readability-function-cognitive-complexity on weak refs

Fix for https://bugs.llvm.org/show_bug.cgi?id=47779

Differential Revision: https://reviews.llvm.org/D89194

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp
    clang/docs/LibASTMatchersReference.html
    clang/include/clang/ASTMatchers/ASTMatchers.h
    clang/lib/ASTMatchers/Dynamic/Registry.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
index 96fe9a2e29a4..96854bf2a7ab 100644
--- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
@@ -501,9 +501,9 @@ void FunctionCognitiveComplexityCheck::storeOptions(
 
 void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
-      functionDecl(
-          allOf(isDefinition(), unless(anyOf(isDefaulted(), isDeleted(),
-                                             isImplicit(), isInstantiated()))))
+      functionDecl(isDefinition(),
+                   unless(anyOf(isDefaulted(), isDeleted(), isImplicit(),
+                                isInstantiated(), isWeak())))
           .bind("func"),
       this);
 }

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp
index 431540c6ee96..0916acd8e675 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp
@@ -1013,3 +1013,5 @@ void functionThatCallsTemplatedFunctions() {
 
   templatedFunction<void*>();
 }
+
+static void pr47779_dont_crash_on_weak() __attribute__((__weakref__("__pr47779_dont_crash_on_weak")));

diff  --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index fd8b217b7bc8..e281c5d3b850 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -3569,6 +3569,17 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
 </pre></td></tr>
 
 
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('isWeak0')"><a name="isWeak0Anchor">isWeak</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="isWeak0"><pre>Matches weak function declarations.
+
+Given:
+  void foo() __attribute__((__weakref__("__foo")));
+  void bar();
+functionDecl(isWeak())
+  matches the weak declaration "foo", but not "bar".
+</pre></td></tr>
+
+
 <tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('parameterCountIs0')"><a name="parameterCountIs0Anchor">parameterCountIs</a></td><td>unsigned N</td></tr>
 <tr><td colspan="4" class="doc" id="parameterCountIs0"><pre>Matches FunctionDecls and FunctionProtoTypes that have a
 specific parameter count.

diff  --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index fd79b176ab4b..51d51d707de4 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4647,6 +4647,17 @@ AST_MATCHER(FunctionDecl, isDefaulted) {
   return Node.isDefaulted();
 }
 
+/// Matches weak function declarations.
+///
+/// Given:
+/// \code
+///   void foo() __attribute__((__weakref__("__foo")));
+///   void bar();
+/// \endcode
+/// functionDecl(isWeak())
+///   matches the weak declaration "foo", but not "bar".
+AST_MATCHER(FunctionDecl, isWeak) { return Node.isWeak(); }
+
 /// Matches functions that have a dynamic exception specification.
 ///
 /// Given:

diff  --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 8e62dce4fab5..00e6abbb477b 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -433,6 +433,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(isVirtual);
   REGISTER_MATCHER(isVirtualAsWritten);
   REGISTER_MATCHER(isVolatileQualified);
+  REGISTER_MATCHER(isWeak);
   REGISTER_MATCHER(isWritten);
   REGISTER_MATCHER(lValueReferenceType);
   REGISTER_MATCHER(labelDecl);


        


More information about the cfe-commits mailing list