[PATCH] D79423: [analyzer][NFC] StdLibraryFunctionsChecker: Add empty Signatures

Gabor Marton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 5 08:36:04 PDT 2020


martong created this revision.
martong added reviewers: Szelethus, NoQ, baloghadamsoftware, balazske, steakhal.
Herald added subscribers: cfe-commits, ASDenysPetrov, Charusso, gamesh411, dkrupp, donat.nagy, mikhail.ramalho, a.sidorin, rnkovacs, szepet, xazax.hun, whisperity.
Herald added a project: clang.
martong added a parent revision: D77658: [analyzer] StdLibraryFunctionsChecker: Add sanity checks for constraints.

In the C language, functions must not be overloaded. In this patch I am adding
empty `Signatures`. During adding a summary for a function with a given name,
if we provide an empty `Signature` then we assume that all visible function
declarations in the given TU share the same signature (i.e. their
`FunctionProtoType` is the same). This makes the administration of adding
summaries for the standard C library (and other C libraries) way easier.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79423

Files:
  clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp


Index: clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
@@ -251,6 +251,7 @@
   struct Signature {
     const ArgTypes ArgTys;
     const QualType RetTy;
+    Signature(){};
     Signature(ArgTypes ArgTys, QualType RetTy) : ArgTys(ArgTys), RetTy(RetTy) {
       assertRetTypeSuitableForSignature(RetTy);
       for (size_t I = 0, E = ArgTys.size(); I != E; ++I) {
@@ -259,6 +260,7 @@
       }
     }
     bool matches(const FunctionDecl *FD) const;
+    bool empty() const { return RetTy.isNull() && ArgTys.size() == 0; }
 
   private:
     static void assertArgTypeSuitableForSignature(QualType T) {
@@ -316,6 +318,9 @@
     Summary(ArgTypes ArgTys, QualType RetTy, InvalidationKind InvalidationKd)
         : Sign(ArgTys, RetTy), InvalidationKd(InvalidationKd) {}
 
+    // Create with empty signature.
+    Summary(InvalidationKind InvalidationKd) : InvalidationKd(InvalidationKd) {}
+
     Summary &Case(ConstraintSet&& CS) {
       CaseConstraints.push_back(std::move(CS));
       return *this;
@@ -338,6 +343,7 @@
     bool matchesSignature(const FunctionDecl *FD) const {
       return Sign.matches(FD) && validate(FD);
     }
+    bool hasEmptySignature() const { return Sign.empty(); }
 
   private:
     // Once we know the exact type of the function then do sanity check on all
@@ -596,6 +602,10 @@
 
 bool StdLibraryFunctionsChecker::Signature::matches(
     const FunctionDecl *FD) const {
+  // Empty matches everything.
+  if (empty())
+    return true;
+
   // Check number of arguments:
   if (FD->param_size() != ArgTys.size())
     return false;
@@ -721,15 +731,21 @@
       auto LookupRes = ACtx.getTranslationUnitDecl()->lookup(&II);
       if (LookupRes.size() == 0)
         return;
-      for (Decl *D : LookupRes) {
-        if (auto *FD = dyn_cast<FunctionDecl>(D)) {
-          if (S.matchesSignature(FD)) {
-            S.setFunctionDecl(FD);
-            auto Res = Map.insert({FD->getCanonicalDecl(), S});
-            assert(Res.second && "Function already has a summary set!");
-            (void)Res;
-            return;
-          }
+      // Get all FunctionDecls.
+      SmallVector<FunctionDecl *, 2> FDs;
+      for (Decl *D : LookupRes)
+        if (auto *FD = dyn_cast<FunctionDecl>(D))
+          FDs.push_back(FD);
+      // Empty signatures should be used only without name overloading.
+      if (FDs.size() > 1 && S.hasEmptySignature())
+        return;
+      for (FunctionDecl *FD : FDs) {
+        if (S.matchesSignature(FD)) {
+          S.setFunctionDecl(FD);
+          auto Res = Map.insert({FD->getCanonicalDecl(), S});
+          assert(Res.second && "Function already has a summary set!");
+          (void)Res;
+          return;
         }
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79423.262119.patch
Type: text/x-patch
Size: 2935 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200505/e3327a46/attachment.bin>


More information about the cfe-commits mailing list