[clang] [analyzer] Use explicit call description mode (easy cases) (PR #88879)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 16 04:33:25 PDT 2024


https://github.com/NagyDonat created https://github.com/llvm/llvm-project/pull/88879

This commit explicitly specifies the matching mode (C library function, any non-method function, or C++ method) for the `CallDescription`s constructed in various checkers where this transition was easy and straightforward.

This change won't cause major functional changes, but isn't NFC because it ensures that e.g. call descriptions for a non-method function won't accidentally match a method that has the same name.

Separate commits will perform (or have already performed) this change in other checkers. My goal is to ensure that the call description mode is always explicitly specified and eliminate (or strongly restrict) the vague "may be either a method or a simple function" mode that's the current default.

>From 7305bb3aa8b05fc4bbe1bc3b44bb185dc0a647cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Tue, 16 Apr 2024 13:19:56 +0200
Subject: [PATCH] [analyzer] Use explicit call description mode (easy cases)

This commit explicitly specifies the matching mode (C library function,
any non-method function, or C++ method) for the `CallDescription`s
constructed in various checkers where this transition was easy and
straightforward.

This change won't cause major functional changes, but isn't NFC because
it ensures that e.g. call descriptions for a non-method function won't
accidentally match a method that has the same name.

Separate commits will perform (or have already performed) this change in
other checkers. My goal is to ensure that the call description mode is
always explicitly specified and eliminate (or strongly restrict) the
vague "may be either a method or a simple function" mode that's the
current default.
---
 .../Checkers/CastValueChecker.cpp              | 18 +++++++++---------
 .../StaticAnalyzer/Checkers/ChrootChecker.cpp  |  3 ++-
 .../Checkers/ErrnoTesterChecker.cpp            | 12 +++++++-----
 .../Checkers/MmapWriteExecChecker.cpp          |  5 ++---
 .../Checkers/StdVariantChecker.cpp             | 10 ++++++----
 .../StaticAnalyzer/Checkers/StringChecker.cpp  |  2 +-
 .../Checkers/cert/PutenvWithAutoChecker.cpp    |  2 +-
 7 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/CastValueChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/CastValueChecker.cpp
index f02d20d45678b3..c7479d74eafc33 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CastValueChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CastValueChecker.cpp
@@ -56,23 +56,23 @@ class CastValueChecker : public Checker<check::DeadSymbols, eval::Call> {
 
 private:
   // These are known in the LLVM project. The pairs are in the following form:
-  // {{{namespace, call}, argument-count}, {callback, kind}}
+  // {{match-mode, {namespace, call}, argument-count}, {callback, kind}}
   const CallDescriptionMap<std::pair<CastCheck, CallKind>> CDM = {
-      {{{"llvm", "cast"}, 1},
+      {{CDM::SimpleFunc, {"llvm", "cast"}, 1},
        {&CastValueChecker::evalCast, CallKind::Function}},
-      {{{"llvm", "dyn_cast"}, 1},
+      {{CDM::SimpleFunc, {"llvm", "dyn_cast"}, 1},
        {&CastValueChecker::evalDynCast, CallKind::Function}},
-      {{{"llvm", "cast_or_null"}, 1},
+      {{CDM::SimpleFunc, {"llvm", "cast_or_null"}, 1},
        {&CastValueChecker::evalCastOrNull, CallKind::Function}},
-      {{{"llvm", "dyn_cast_or_null"}, 1},
+      {{CDM::SimpleFunc, {"llvm", "dyn_cast_or_null"}, 1},
        {&CastValueChecker::evalDynCastOrNull, CallKind::Function}},
-      {{{"clang", "castAs"}, 0},
+      {{CDM::CXXMethod, {"clang", "castAs"}, 0},
        {&CastValueChecker::evalCastAs, CallKind::Method}},
-      {{{"clang", "getAs"}, 0},
+      {{CDM::CXXMethod, {"clang", "getAs"}, 0},
        {&CastValueChecker::evalGetAs, CallKind::Method}},
-      {{{"llvm", "isa"}, 1},
+      {{CDM::SimpleFunc, {"llvm", "isa"}, 1},
        {&CastValueChecker::evalIsa, CallKind::InstanceOf}},
-      {{{"llvm", "isa_and_nonnull"}, 1},
+      {{CDM::SimpleFunc, {"llvm", "isa_and_nonnull"}, 1},
        {&CastValueChecker::evalIsaAndNonNull, CallKind::InstanceOf}}};
 
   void evalCast(const CallEvent &Call, DefinedOrUnknownSVal DV,
diff --git a/clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp
index be7be15022d360..3a0a01c23de03e 100644
--- a/clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp
@@ -43,7 +43,8 @@ class ChrootChecker : public Checker<eval::Call, check::PreCall> {
   // This bug refers to possibly break out of a chroot() jail.
   const BugType BT_BreakJail{this, "Break out of jail"};
 
-  const CallDescription Chroot{{"chroot"}, 1}, Chdir{{"chdir"}, 1};
+  const CallDescription Chroot{CDM::CLibrary, {"chroot"}, 1},
+      Chdir{CDM::CLibrary, {"chdir"}, 1};
 
 public:
   ChrootChecker() {}
diff --git a/clang/lib/StaticAnalyzer/Checkers/ErrnoTesterChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/ErrnoTesterChecker.cpp
index c46ebee0c94ff4..6076a6bc789737 100644
--- a/clang/lib/StaticAnalyzer/Checkers/ErrnoTesterChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/ErrnoTesterChecker.cpp
@@ -70,13 +70,15 @@ class ErrnoTesterChecker : public Checker<eval::Call> {
 
   using EvalFn = std::function<void(CheckerContext &, const CallEvent &)>;
   const CallDescriptionMap<EvalFn> TestCalls{
-      {{{"ErrnoTesterChecker_setErrno"}, 1}, &ErrnoTesterChecker::evalSetErrno},
-      {{{"ErrnoTesterChecker_getErrno"}, 0}, &ErrnoTesterChecker::evalGetErrno},
-      {{{"ErrnoTesterChecker_setErrnoIfError"}, 0},
+      {{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrno"}, 1},
+       &ErrnoTesterChecker::evalSetErrno},
+      {{CDM::SimpleFunc, {"ErrnoTesterChecker_getErrno"}, 0},
+       &ErrnoTesterChecker::evalGetErrno},
+      {{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrnoIfError"}, 0},
        &ErrnoTesterChecker::evalSetErrnoIfError},
-      {{{"ErrnoTesterChecker_setErrnoIfErrorRange"}, 0},
+      {{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrnoIfErrorRange"}, 0},
        &ErrnoTesterChecker::evalSetErrnoIfErrorRange},
-      {{{"ErrnoTesterChecker_setErrnoCheckState"}, 0},
+      {{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrnoCheckState"}, 0},
        &ErrnoTesterChecker::evalSetErrnoCheckState}};
 };
 
diff --git a/clang/lib/StaticAnalyzer/Checkers/MmapWriteExecChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MmapWriteExecChecker.cpp
index 2e31c16e457c2e..cd1dd1b2fc511f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MmapWriteExecChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MmapWriteExecChecker.cpp
@@ -27,8 +27,8 @@ using namespace ento;
 
 namespace {
 class MmapWriteExecChecker : public Checker<check::PreCall> {
-  CallDescription MmapFn;
-  CallDescription MprotectFn;
+  CallDescription MmapFn{CDM::CLibrary, {"mmap"}, 6};
+  CallDescription MprotectFn{CDM::CLibrary, {"mprotect"}, 3};
   static int ProtWrite;
   static int ProtExec;
   static int ProtRead;
@@ -36,7 +36,6 @@ class MmapWriteExecChecker : public Checker<check::PreCall> {
                    "Security"};
 
 public:
-  MmapWriteExecChecker() : MmapFn({"mmap"}, 6), MprotectFn({"mprotect"}, 3) {}
   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
   int ProtExecOv;
   int ProtReadOv;
diff --git a/clang/lib/StaticAnalyzer/Checkers/StdVariantChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdVariantChecker.cpp
index f7b7befe28ee7d..19877964bd900a 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StdVariantChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StdVariantChecker.cpp
@@ -129,9 +129,11 @@ static llvm::StringRef indefiniteArticleBasedOnVowel(char a) {
 
 class StdVariantChecker : public Checker<eval::Call, check::RegionChanges> {
   // Call descriptors to find relevant calls
-  CallDescription VariantConstructor{{"std", "variant", "variant"}};
-  CallDescription VariantAssignmentOperator{{"std", "variant", "operator="}};
-  CallDescription StdGet{{"std", "get"}, 1, 1};
+  CallDescription VariantConstructor{CDM::CXXMethod,
+                                     {"std", "variant", "variant"}};
+  CallDescription VariantAssignmentOperator{CDM::CXXMethod,
+                                            {"std", "variant", "operator="}};
+  CallDescription StdGet{CDM::SimpleFunc, {"std", "get"}, 1, 1};
 
   BugType BadVariantType{this, "BadVariantType", "BadVariantType"};
 
@@ -295,4 +297,4 @@ bool clang::ento::shouldRegisterStdVariantChecker(
 
 void clang::ento::registerStdVariantChecker(clang::ento::CheckerManager &mgr) {
   mgr.registerChecker<StdVariantChecker>();
-}
\ No newline at end of file
+}
diff --git a/clang/lib/StaticAnalyzer/Checkers/StringChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StringChecker.cpp
index 2dc9e29ca90688..8f1c31763e212c 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StringChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StringChecker.cpp
@@ -27,7 +27,7 @@ class StringChecker : public Checker<check::PreCall> {
   mutable const FunctionDecl *StringConstCharPtrCtor = nullptr;
   mutable CanQualType SizeTypeTy;
   const CallDescription TwoParamStdStringCtor = {
-      {"std", "basic_string", "basic_string"}, 2, 2};
+      CDM::CXXMethod, {"std", "basic_string", "basic_string"}, 2, 2};
 
   bool isCharToStringCtor(const CallEvent &Call, const ASTContext &ACtx) const;
 
diff --git a/clang/lib/StaticAnalyzer/Checkers/cert/PutenvWithAutoChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/cert/PutenvWithAutoChecker.cpp
index eae162cda69310..a82f7caf16b291 100644
--- a/clang/lib/StaticAnalyzer/Checkers/cert/PutenvWithAutoChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/cert/PutenvWithAutoChecker.cpp
@@ -30,7 +30,7 @@ class PutenvWithAutoChecker : public Checker<check::PostCall> {
 private:
   BugType BT{this, "'putenv' function should not be called with auto variables",
              categories::SecurityError};
-  const CallDescription Putenv{{"putenv"}, 1};
+  const CallDescription Putenv{CDM::CLibrary, {"putenv"}, 1};
 
 public:
   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;



More information about the cfe-commits mailing list