[clang] 72d04d7 - [analyzer] Allow matching non-CallExprs using CallDescriptions

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 18 05:58:13 PDT 2021


Author: Balazs Benics
Date: 2021-10-18T14:57:24+02:00
New Revision: 72d04d7b2b53eea977e160551077cf1a3f51ba9a

URL: https://github.com/llvm/llvm-project/commit/72d04d7b2b53eea977e160551077cf1a3f51ba9a
DIFF: https://github.com/llvm/llvm-project/commit/72d04d7b2b53eea977e160551077cf1a3f51ba9a.diff

LOG: [analyzer] Allow matching non-CallExprs using CallDescriptions

Fallback to stringification and string comparison if we cannot compare
the `IdentifierInfo`s, which is the case for C++ overloaded operators,
constructors, destructors, etc.

Examples:
  { "std", "basic_string", "basic_string", 2} // match the 2 param std::string constructor
  { "std", "basic_string", "~basic_string" }  // match the std::string destructor
  { "aaa", "bbb", "operator int" } // matches the struct bbb conversion operator to int

Reviewed By: martong

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

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Core/CallEvent.cpp
    clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Core/CallEvent.cpp b/clang/lib/StaticAnalyzer/Core/CallEvent.cpp
index ec44bcf5b27ca..5e504ad30b101 100644
--- a/clang/lib/StaticAnalyzer/Core/CallEvent.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CallEvent.cpp
@@ -328,9 +328,11 @@ bool CallEvent::isCalled(const CallDescription &CD) const {
     if (const auto *II = Name.getAsIdentifierInfo())
       return II == CD.II.getValue(); // Fast case.
 
-    // Simply report mismatch for:
+    // Fallback to the slow stringification and comparison for:
     // C++ overloaded operators, constructors, destructors, etc.
-    return false;
+    // FIXME This comparison is way SLOWER than comparing pointers.
+    // At some point in the future, we should compare FunctionDecl pointers.
+    return Name.getAsString() == CD.getFunctionName();
   };
 
   const auto ExactMatchArgAndParamCounts =

diff  --git a/clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp b/clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp
index e232d0ff065bb..d719240236558 100644
--- a/clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp
+++ b/clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp
@@ -187,12 +187,11 @@ TEST(CallDescription, MatchConstructor) {
       using namespace std;
       basic_string<char> s("hello");
     })code";
-  // FIXME: We should match.
   const std::string Code = (Twine{MockStdStringHeader} + AdditionalCode).str();
   EXPECT_TRUE(tooling::runToolOnCode(
       std::unique_ptr<FrontendAction>(
           new CallDescriptionAction<CXXConstructExpr>({
-              {{{"std", "basic_string", "basic_string"}, 2, 2}, false},
+              {{{"std", "basic_string", "basic_string"}, 2, 2}, true},
           })),
       Code));
 }
@@ -216,10 +215,9 @@ TEST(CallDescription, MatchConversionOperator) {
       aaa::bbb::Bar x;
       int tmp = x;
     })code";
-  // FIXME: We should match.
   EXPECT_TRUE(tooling::runToolOnCode(
       std::unique_ptr<FrontendAction>(new CallDescriptionAction<>({
-          {{{"aaa", "bbb", "Bar", "operator int"}}, false},
+          {{{"aaa", "bbb", "Bar", "operator int"}}, true},
       })),
       Code));
 }


        


More information about the cfe-commits mailing list