[clang] [analyzer][NFC] Compute callee properties once per CallDescriptionMap lookup (PR #245630)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 04:46:52 PST 2026
https://github.com/Xazax-hun updated https://github.com/llvm/llvm-project/pull/245630
>From de20a8f90e69f3010e18dec59c0fef717f2fc2b8 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Sun, 22 Nov 2026 19:38:29 +0000
Subject: [PATCH 1/4] [analyzer] Compute callee properties once per
CallDescriptionMap lookup
CallDescriptionMap::lookup matched the call against every entry with
CallDescription::matches, which looked up the callee declaration and the
parameters through virtual CallEvent methods for each entry, and for C
library entries recomputed the builtin ID, the builtin name string, whether
the builtin is a predefined library function and the declaration context
checks of isCLibraryFunction.
Compute the callee, the argument and parameter counts and whether it is a
method once per lookup, and the C library function data at most once. Split
CheckerContext::isCLibraryFunction into private helpers for the
callee-dependent parts and the name matching, which CallDescription can
reuse; the public API and the matching are the same as before, including
the exact-only matching of predefined library functions. Also check the
cheap isHardenedVariantOf before isCLibraryFunction.
On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces
the instructions of the static analyzer by 5.1-7.8% and the cycles by
3.5-5.8%; peak memory is unchanged within noise. Most of the win comes from
looking up the callee once per lookup instead of once per entry: that part
alone gives 4.9-7.2%. The analyzer statistics and the diagnostics are
unchanged.
Assisted-by: Claude Opus 5.5
---
.../Core/PathSensitive/CallDescription.h | 50 +++++++++++---
.../Core/PathSensitive/CheckerContext.h | 27 ++++++++
.../StaticAnalyzer/Core/CallDescription.cpp | 68 ++++++++++++++-----
.../StaticAnalyzer/Core/CheckerContext.cpp | 63 +++++++++++------
4 files changed, 161 insertions(+), 47 deletions(-)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
index 3c242898ef6cdf..319de16e0b9926 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
@@ -80,6 +80,35 @@ class CallDescription {
MaybeCount RequiredParams;
Mode MatchAs;
+ template <typename T> friend class CallDescriptionMap;
+
+ /// The properties of a call that are needed for matching it. They are
+ /// computed once when a call is matched against many CallDescriptions.
+ class Callee {
+ /// The callee-dependent data of CheckerContext::isCLibraryFunction.
+ struct CLibraryInfo {
+ std::string BuiltinName; // Empty if not a builtin.
+ bool IsPredefinedLibFunction;
+ bool IsCLibraryDecl;
+ };
+ /// Computed on first use, as only C library entries need it.
+ mutable std::optional<CLibraryInfo> CLib;
+
+ const CLibraryInfo &getCLibraryInfo() const;
+
+ public:
+ const FunctionDecl *FD = nullptr;
+ size_t ArgCount = 0;
+ size_t ParamCount = 0;
+ bool IsMethod = false;
+
+ explicit Callee(const CallEvent &Call);
+ explicit Callee(const CallExpr &CE);
+
+ /// Equivalent to CheckerContext::isCLibraryFunction(FD, Name).
+ bool isCLibraryFunction(StringRef Name) const;
+ };
+
public:
/// Constructs a CallDescription object.
///
@@ -182,8 +211,7 @@ class CallDescription {
/// @}
private:
- bool matchesImpl(const FunctionDecl *Callee, size_t ArgCount,
- size_t ParamCount) const;
+ bool matchesImpl(const Callee &C) const;
bool matchNameOnly(const NamedDecl *ND) const;
bool matchQualifiedNameParts(const Decl *D) const;
@@ -220,13 +248,7 @@ template <typename T> class CallDescriptionMap {
CallDescriptionMap &operator=(CallDescriptionMap &&) = default;
[[nodiscard]] const T *lookup(const CallEvent &Call) const {
- // Slow path: linear lookup.
- // TODO: Implement some sort of fast path.
- for (const std::pair<CallDescription, T> &I : LinearMap)
- if (I.first.matches(Call))
- return &I.second;
-
- return nullptr;
+ return lookupImpl(CallDescription::Callee(Call));
}
/// When available, always prefer lookup with a CallEvent! This function
@@ -242,10 +264,18 @@ template <typename T> class CallDescriptionMap {
/// CallEvent::getNumArgs), the called function if it was called through a
/// function pointer, and other information not available syntactically.
[[nodiscard]] const T *lookupAsWritten(const CallExpr &Call) const {
+ return lookupImpl(CallDescription::Callee(Call));
+ }
+
+private:
+ const T *lookupImpl(const CallDescription::Callee &C) const {
+ if (!C.FD)
+ return nullptr;
+
// Slow path: linear lookup.
// TODO: Implement some sort of fast path.
for (const std::pair<CallDescription, T> &I : LinearMap)
- if (I.first.matchesAsWritten(Call))
+ if (I.first.matchesImpl(C))
return &I.second;
return nullptr;
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
index 02b59ced4321fe..3c406c45293ed5 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
@@ -435,6 +435,33 @@ class CheckerContext {
std::string getMacroNameOrSpelling(SourceLocation &Loc);
private:
+ // CallDescription matches a function against many names, so it computes the
+ // callee-dependent parts of isCLibraryFunction only once.
+ friend class CallDescription;
+
+ /// Equivalent to isCLibraryFunction(FD, Name), for callers that match the
+ /// same function against many names: with BId = FD->getBuiltinID(),
+ /// \p BuiltinName must be getBuiltinName(FD, BId), \p IsPredefinedLibFunction
+ /// must be isPredefinedLibFunction(FD, BId) and \p IsCLibraryDecl must be
+ /// isCLibraryDecl(FD).
+ static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
+ StringRef BuiltinName,
+ bool IsPredefinedLibFunction,
+ bool IsCLibraryDecl);
+
+ /// Returns the name of the builtin \p BId of \p FD, or an empty string if
+ /// \p BId is 0.
+ static std::string getBuiltinName(const FunctionDecl *FD, unsigned BId);
+
+ /// Returns true if \p BId is a builtin that is a predefined library function
+ /// (e.g. getlogin_r), for which no fuzzy name matching is done.
+ static bool isPredefinedLibFunction(const FunctionDecl *FD, unsigned BId);
+
+ /// Returns true if \p FD is declared like a C library function: it has an
+ /// identifier, it is in the top-level or the \c std namespace, and it is
+ /// externally visible or inline.
+ static bool isCLibraryDecl(const FunctionDecl *FD);
+
ExplodedNode *addTransitionImpl(const ProgramStateRef &State, bool MarkAsSink,
ExplodedNode *P, const ProgramPointTag *Tag) {
Changed = true;
diff --git a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
index 9199e764bb2f36..234ceaaea05dfc 100644
--- a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
@@ -47,24 +47,55 @@ ento::CallDescription::CallDescription(Mode MatchAs,
[](StringRef From) { return From.str(); });
}
-bool ento::CallDescription::matches(const CallEvent &Call) const {
+ento::CallDescription::Callee::Callee(const CallEvent &Call) {
// FIXME: Add ObjC Message support.
if (Call.getKind() == CE_ObjCMessage)
- return false;
+ return;
- const auto *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
+ FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
if (!FD)
- return false;
+ return;
- return matchesImpl(FD, Call.getNumArgs(), Call.parameters().size());
+ ArgCount = Call.getNumArgs();
+ // Same as Call.parameters().size() for calls of FunctionDecls.
+ ParamCount = FD->param_size();
+ IsMethod = isa<CXXMethodDecl>(FD);
}
-bool ento::CallDescription::matchesAsWritten(const CallExpr &CE) const {
- const auto *FD = dyn_cast_or_null<FunctionDecl>(CE.getCalleeDecl());
+ento::CallDescription::Callee::Callee(const CallExpr &CE) {
+ FD = dyn_cast_or_null<FunctionDecl>(CE.getCalleeDecl());
if (!FD)
- return false;
+ return;
+
+ ArgCount = CE.getNumArgs();
+ ParamCount = FD->param_size();
+ IsMethod = isa<CXXMethodDecl>(FD);
+}
+
+const ento::CallDescription::Callee::CLibraryInfo &
+ento::CallDescription::Callee::getCLibraryInfo() const {
+ if (!CLib) {
+ unsigned BId = FD->getBuiltinID();
+ CLib = CLibraryInfo{CheckerContext::getBuiltinName(FD, BId),
+ CheckerContext::isPredefinedLibFunction(FD, BId),
+ CheckerContext::isCLibraryDecl(FD)};
+ }
+ return *CLib;
+}
- return matchesImpl(FD, CE.getNumArgs(), FD->param_size());
+bool ento::CallDescription::Callee::isCLibraryFunction(StringRef Name) const {
+ const CLibraryInfo &Info = getCLibraryInfo();
+ return CheckerContext::isCLibraryFunction(FD, Name, Info.BuiltinName,
+ Info.IsPredefinedLibFunction,
+ Info.IsCLibraryDecl);
+}
+
+bool ento::CallDescription::matches(const CallEvent &Call) const {
+ return matchesImpl(Callee(Call));
+}
+
+bool ento::CallDescription::matchesAsWritten(const CallExpr &CE) const {
+ return matchesImpl(Callee(CE));
}
bool ento::CallDescription::matchNameOnly(const NamedDecl *ND) const {
@@ -139,24 +170,25 @@ bool ento::CallDescription::matchQualifiedNameParts(const Decl *D) const {
return QualifierPartsIt == QualifierPartsEndIt;
}
-bool ento::CallDescription::matchesImpl(const FunctionDecl *FD, size_t ArgCount,
- size_t ParamCount) const {
+bool ento::CallDescription::matchesImpl(const Callee &C) const {
+ const FunctionDecl *FD = C.FD;
if (!FD)
return false;
- const bool isMethod = isa<CXXMethodDecl>(FD);
-
- if (MatchAs == Mode::SimpleFunc && isMethod)
+ if (MatchAs == Mode::SimpleFunc && C.IsMethod)
return false;
- if (MatchAs == Mode::CXXMethod && !isMethod)
+ if (MatchAs == Mode::CXXMethod && !C.IsMethod)
return false;
+ const size_t ArgCount = C.ArgCount;
+ const size_t ParamCount = C.ParamCount;
+
if (MatchAs == Mode::CLibraryMaybeHardened) {
// In addition to accepting FOO() with CLibrary rules, we also want to
// accept calls to __FOO_chk() and __builtin___FOO_chk().
- if (CheckerContext::isCLibraryFunction(FD) &&
- CheckerContext::isHardenedVariantOf(FD, getFunctionName())) {
+ if (CheckerContext::isHardenedVariantOf(FD, getFunctionName()) &&
+ C.isCLibraryFunction(StringRef())) {
// Check that the actual argument/parameter counts are greater or equal
// to the required counts. (Setting a requirement to std::nullopt matches
// anything, so in that case value_or ensures that the value is compared
@@ -171,7 +203,7 @@ bool ento::CallDescription::matchesImpl(const FunctionDecl *FD, size_t ArgCount,
return false;
if (MatchAs == Mode::CLibrary || MatchAs == Mode::CLibraryMaybeHardened)
- return CheckerContext::isCLibraryFunction(FD, getFunctionName());
+ return C.isCLibraryFunction(getFunctionName());
if (!matchNameOnly(FD))
return false;
diff --git a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
index 2c99d45f10304e..3cc15910c92fb9 100644
--- a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
@@ -48,22 +48,43 @@ StringRef CheckerContext::getDeclDescription(const Decl *D) {
bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
StringRef Name) {
+ unsigned BId = FD->getBuiltinID();
+ return isCLibraryFunction(FD, Name, getBuiltinName(FD, BId),
+ isPredefinedLibFunction(FD, BId),
+ isCLibraryDecl(FD));
+}
+
+std::string CheckerContext::getBuiltinName(const FunctionDecl *FD,
+ unsigned BId) {
+ if (BId == 0)
+ return std::string();
+ return FD->getASTContext().BuiltinInfo.getName(BId);
+}
+
+bool CheckerContext::isPredefinedLibFunction(const FunctionDecl *FD,
+ unsigned BId) {
+ return BId != 0 &&
+ FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(BId);
+}
+
+bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
+ StringRef BuiltinName,
+ bool IsPredefinedLibFunction,
+ bool IsCLibraryDecl) {
// To avoid false positives (Ex: finding user defined functions with
// similar names), only perform fuzzy name matching when it's a builtin.
// Using a string compare is slow, we might want to switch on BuiltinID here.
- unsigned BId = FD->getBuiltinID();
- if (BId != 0) {
+ if (!BuiltinName.empty()) {
if (Name.empty())
return true;
- const Builtin::Context &BuiltinInfo = FD->getASTContext().BuiltinInfo;
- std::string BName = BuiltinInfo.getName(BId);
+ StringRef BName = BuiltinName;
// Accept exact match.
if (BName == Name)
return true;
// Only perform fuzzy name matching for non-library builtins (e.g.
// __builtin_memcpy, __builtin_memcpy_inline), not for distinct predefined
// library functions (e.g. objc_read_weak vs read, getlogin_r vs getlogin).
- if (!BuiltinInfo.isPredefinedLibFunction(BId)) {
+ if (!IsPredefinedLibFunction) {
size_t start = BName.find(Name);
if (start != StringRef::npos) {
// v-- match starts here
@@ -86,6 +107,23 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
}
}
+ if (!IsCLibraryDecl)
+ return false;
+
+ if (Name.empty())
+ return true;
+
+ StringRef FName = FD->getName();
+ if (FName == Name)
+ return true;
+
+ if (FName.starts_with("__inline") && FName.contains(Name))
+ return true;
+
+ return false;
+}
+
+bool CheckerContext::isCLibraryDecl(const FunctionDecl *FD) {
const IdentifierInfo *II = FD->getIdentifier();
// If this is a special C++ name without IdentifierInfo, it can't be a
// C library function.
@@ -102,20 +140,7 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
// If this function is not externally visible, it is not a C library function.
// Note that we make an exception for inline functions, which may be
// declared in header files without external linkage.
- if (!FD->isInlined() && !FD->isExternallyVisible())
- return false;
-
- if (Name.empty())
- return true;
-
- StringRef FName = II->getName();
- if (FName == Name)
- return true;
-
- if (FName.starts_with("__inline") && FName.contains(Name))
- return true;
-
- return false;
+ return FD->isInlined() || FD->isExternallyVisible();
}
bool CheckerContext::isHardenedVariantOf(const FunctionDecl *FD,
>From 522ce624c40c077784745ac5663aa0fb6abf3e4a Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Tue, 24 Nov 2026 17:44:15 +0000
Subject: [PATCH 2/4] [analyzer][NFC] Compute C library properties of the
callee lazily
Address review comments: replace the private isCLibraryFunction overload
and its eagerly computed helpers with a private CLibraryCallee class in
CheckerContext that computes the builtin name, whether the builtin is a
predefined library function and the declaration checks on first use. The
public isCLibraryFunction(FD, Name) no longer computes properties it does
not need, and the matching logic stays in a single function.
Assisted-by: Claude Opus 5.5
---
.../Core/PathSensitive/CallDescription.h | 14 +--
.../Core/PathSensitive/CheckerContext.h | 48 +++++-----
.../StaticAnalyzer/Core/CallDescription.cpp | 19 +---
.../StaticAnalyzer/Core/CheckerContext.cpp | 92 +++++++++----------
4 files changed, 78 insertions(+), 95 deletions(-)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
index 319de16e0b9926..f4f6c5a6cf8282 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
@@ -16,6 +16,7 @@
#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CALLDESCRIPTION_H
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Compiler.h"
#include <optional>
@@ -85,16 +86,8 @@ class CallDescription {
/// The properties of a call that are needed for matching it. They are
/// computed once when a call is matched against many CallDescriptions.
class Callee {
- /// The callee-dependent data of CheckerContext::isCLibraryFunction.
- struct CLibraryInfo {
- std::string BuiltinName; // Empty if not a builtin.
- bool IsPredefinedLibFunction;
- bool IsCLibraryDecl;
- };
- /// Computed on first use, as only C library entries need it.
- mutable std::optional<CLibraryInfo> CLib;
-
- const CLibraryInfo &getCLibraryInfo() const;
+ /// Constructed on first use, as only C library entries need it.
+ mutable std::optional<CheckerContext::CLibraryCallee> CLib;
public:
const FunctionDecl *FD = nullptr;
@@ -269,6 +262,7 @@ template <typename T> class CallDescriptionMap {
private:
const T *lookupImpl(const CallDescription::Callee &C) const {
+ // Null for ObjC messages, blocks and calls of unknown function pointers.
if (!C.FD)
return nullptr;
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
index 3c406c45293ed5..6725cebfb27ce3 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
@@ -439,28 +439,32 @@ class CheckerContext {
// callee-dependent parts of isCLibraryFunction only once.
friend class CallDescription;
- /// Equivalent to isCLibraryFunction(FD, Name), for callers that match the
- /// same function against many names: with BId = FD->getBuiltinID(),
- /// \p BuiltinName must be getBuiltinName(FD, BId), \p IsPredefinedLibFunction
- /// must be isPredefinedLibFunction(FD, BId) and \p IsCLibraryDecl must be
- /// isCLibraryDecl(FD).
- static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
- StringRef BuiltinName,
- bool IsPredefinedLibFunction,
- bool IsCLibraryDecl);
-
- /// Returns the name of the builtin \p BId of \p FD, or an empty string if
- /// \p BId is 0.
- static std::string getBuiltinName(const FunctionDecl *FD, unsigned BId);
-
- /// Returns true if \p BId is a builtin that is a predefined library function
- /// (e.g. getlogin_r), for which no fuzzy name matching is done.
- static bool isPredefinedLibFunction(const FunctionDecl *FD, unsigned BId);
-
- /// Returns true if \p FD is declared like a C library function: it has an
- /// identifier, it is in the top-level or the \c std namespace, and it is
- /// externally visible or inline.
- static bool isCLibraryDecl(const FunctionDecl *FD);
+ /// The properties of a function that isCLibraryFunction depends on, computed
+ /// on first use.
+ class CLibraryCallee {
+ const FunctionDecl *FD;
+ unsigned BId;
+ std::optional<std::string> BuiltinName;
+ std::optional<bool> IsPredefinedLibFunction;
+ std::optional<bool> IsCLibraryDecl;
+
+ public:
+ explicit CLibraryCallee(const FunctionDecl *FD)
+ : FD(FD), BId(FD->getBuiltinID()) {}
+
+ const FunctionDecl *getDecl() const { return FD; }
+ unsigned getBuiltinID() const { return BId; }
+ /// Must only be called if getBuiltinID() is not 0.
+ StringRef getBuiltinName();
+ /// Returns true if the builtin is a predefined library function (e.g.
+ /// getlogin_r). Must only be called if getBuiltinID() is not 0.
+ bool isPredefinedLibFunction();
+ /// Returns true if the function has an identifier, it is in the top-level
+ /// or the \c std namespace, and it is externally visible or inline.
+ bool isCLibraryDecl();
+ };
+
+ static bool isCLibraryFunction(CLibraryCallee &C, StringRef Name);
ExplodedNode *addTransitionImpl(const ProgramStateRef &State, bool MarkAsSink,
ExplodedNode *P, const ProgramPointTag *Tag) {
diff --git a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
index 234ceaaea05dfc..9a29b4edf34971 100644
--- a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
@@ -57,7 +57,6 @@ ento::CallDescription::Callee::Callee(const CallEvent &Call) {
return;
ArgCount = Call.getNumArgs();
- // Same as Call.parameters().size() for calls of FunctionDecls.
ParamCount = FD->param_size();
IsMethod = isa<CXXMethodDecl>(FD);
}
@@ -72,22 +71,10 @@ ento::CallDescription::Callee::Callee(const CallExpr &CE) {
IsMethod = isa<CXXMethodDecl>(FD);
}
-const ento::CallDescription::Callee::CLibraryInfo &
-ento::CallDescription::Callee::getCLibraryInfo() const {
- if (!CLib) {
- unsigned BId = FD->getBuiltinID();
- CLib = CLibraryInfo{CheckerContext::getBuiltinName(FD, BId),
- CheckerContext::isPredefinedLibFunction(FD, BId),
- CheckerContext::isCLibraryDecl(FD)};
- }
- return *CLib;
-}
-
bool ento::CallDescription::Callee::isCLibraryFunction(StringRef Name) const {
- const CLibraryInfo &Info = getCLibraryInfo();
- return CheckerContext::isCLibraryFunction(FD, Name, Info.BuiltinName,
- Info.IsPredefinedLibFunction,
- Info.IsCLibraryDecl);
+ if (!CLib)
+ CLib.emplace(FD);
+ return CheckerContext::isCLibraryFunction(*CLib, Name);
}
bool ento::CallDescription::matches(const CallEvent &Call) const {
diff --git a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
index 3cc15910c92fb9..107592f0513149 100644
--- a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
@@ -48,44 +48,62 @@ StringRef CheckerContext::getDeclDescription(const Decl *D) {
bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
StringRef Name) {
- unsigned BId = FD->getBuiltinID();
- return isCLibraryFunction(FD, Name, getBuiltinName(FD, BId),
- isPredefinedLibFunction(FD, BId),
- isCLibraryDecl(FD));
+ CLibraryCallee C(FD);
+ return isCLibraryFunction(C, Name);
}
-std::string CheckerContext::getBuiltinName(const FunctionDecl *FD,
- unsigned BId) {
- if (BId == 0)
- return std::string();
- return FD->getASTContext().BuiltinInfo.getName(BId);
+StringRef CheckerContext::CLibraryCallee::getBuiltinName() {
+ if (!BuiltinName)
+ BuiltinName = FD->getASTContext().BuiltinInfo.getName(BId);
+ return *BuiltinName;
}
-bool CheckerContext::isPredefinedLibFunction(const FunctionDecl *FD,
- unsigned BId) {
- return BId != 0 &&
- FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(BId);
+bool CheckerContext::CLibraryCallee::isPredefinedLibFunction() {
+ if (!IsPredefinedLibFunction)
+ IsPredefinedLibFunction =
+ FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(BId);
+ return *IsPredefinedLibFunction;
}
-bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
- StringRef BuiltinName,
- bool IsPredefinedLibFunction,
- bool IsCLibraryDecl) {
+bool CheckerContext::CLibraryCallee::isCLibraryDecl() {
+ if (IsCLibraryDecl)
+ return *IsCLibraryDecl;
+
+ IsCLibraryDecl = false;
+ // If this is a special C++ name without IdentifierInfo, it can't be a
+ // C library function.
+ if (!FD->getIdentifier())
+ return false;
+
+ // C library functions are either declared directly within a TU (the common
+ // case) or they are accessed through the namespace `std` (when they are used
+ // in C++ via headers like <cstdlib>).
+ const DeclContext *DC = FD->getDeclContext()->getRedeclContext();
+ if (!(DC->isTranslationUnit() || DC->isStdNamespace()))
+ return false;
+
+ // If this function is not externally visible, it is not a C library function.
+ // Note that we make an exception for inline functions, which may be
+ // declared in header files without external linkage.
+ IsCLibraryDecl = FD->isInlined() || FD->isExternallyVisible();
+ return *IsCLibraryDecl;
+}
+
+bool CheckerContext::isCLibraryFunction(CLibraryCallee &C, StringRef Name) {
// To avoid false positives (Ex: finding user defined functions with
// similar names), only perform fuzzy name matching when it's a builtin.
// Using a string compare is slow, we might want to switch on BuiltinID here.
- if (!BuiltinName.empty()) {
+ if (C.getBuiltinID() != 0) {
if (Name.empty())
return true;
- StringRef BName = BuiltinName;
// Accept exact match.
- if (BName == Name)
+ if (C.getBuiltinName() == Name)
return true;
// Only perform fuzzy name matching for non-library builtins (e.g.
// __builtin_memcpy, __builtin_memcpy_inline), not for distinct predefined
// library functions (e.g. objc_read_weak vs read, getlogin_r vs getlogin).
- if (!IsPredefinedLibFunction) {
- size_t start = BName.find(Name);
+ if (!C.isPredefinedLibFunction()) {
+ size_t start = C.getBuiltinName().find(Name);
if (start != StringRef::npos) {
// v-- match starts here
// ...xxxxx...
@@ -93,12 +111,12 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
// ^ ^ lookbehind and lookahead characters
const auto MatchPredecessor = [&]() -> bool {
- return start <= 0 || !llvm::isAlpha(BName[start - 1]);
+ return start <= 0 || !llvm::isAlpha(C.getBuiltinName()[start - 1]);
};
const auto MatchSuccessor = [&]() -> bool {
std::size_t LookbehindPlace = start + Name.size();
- return LookbehindPlace >= BName.size() ||
- !llvm::isAlpha(BName[LookbehindPlace]);
+ return LookbehindPlace >= C.getBuiltinName().size() ||
+ !llvm::isAlpha(C.getBuiltinName()[LookbehindPlace]);
};
if (MatchPredecessor() && MatchSuccessor())
@@ -107,13 +125,13 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
}
}
- if (!IsCLibraryDecl)
+ if (!C.isCLibraryDecl())
return false;
if (Name.empty())
return true;
- StringRef FName = FD->getName();
+ StringRef FName = C.getDecl()->getName();
if (FName == Name)
return true;
@@ -123,26 +141,6 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
return false;
}
-bool CheckerContext::isCLibraryDecl(const FunctionDecl *FD) {
- const IdentifierInfo *II = FD->getIdentifier();
- // If this is a special C++ name without IdentifierInfo, it can't be a
- // C library function.
- if (!II)
- return false;
-
- // C library functions are either declared directly within a TU (the common
- // case) or they are accessed through the namespace `std` (when they are used
- // in C++ via headers like <cstdlib>).
- const DeclContext *DC = FD->getDeclContext()->getRedeclContext();
- if (!(DC->isTranslationUnit() || DC->isStdNamespace()))
- return false;
-
- // If this function is not externally visible, it is not a C library function.
- // Note that we make an exception for inline functions, which may be
- // declared in header files without external linkage.
- return FD->isInlined() || FD->isExternallyVisible();
-}
-
bool CheckerContext::isHardenedVariantOf(const FunctionDecl *FD,
StringRef Name) {
const IdentifierInfo *II = FD->getIdentifier();
>From 4eaea917c658823f30565d6858e2684a7b5c96fd Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Tue, 24 Nov 2026 18:26:05 +0000
Subject: [PATCH 3/4] [analyzer][NFC] Keep the C library properties separate
from the callee
Address review comments: CallDescription::Callee now only holds the facts
about the call (the callee declaration, the argument and parameter counts
and whether it is a method), while the lazily computed properties of the
declaration that the C library matching depends on (builtin ID and name,
whether the builtin is a predefined library function, the declaration
context and visibility checks) live in a separate CLibraryFunctionInfo.
CallDescriptionMap creates both once per lookup and only C library entries
use the latter.
The C library matching now lives with CallDescription, its main user, and
CheckerContext::isCLibraryFunction is a thin wrapper around it, so
CheckerContext is unchanged by this PR. The matching itself is unchanged.
Assisted-by: Claude Opus 5.5
---
.../Core/PathSensitive/CallDescription.h | 42 +++++--
.../Core/PathSensitive/CheckerContext.h | 31 -----
.../StaticAnalyzer/Core/CallDescription.cpp | 116 ++++++++++++++++--
.../StaticAnalyzer/Core/CheckerContext.cpp | 95 +-------------
4 files changed, 140 insertions(+), 144 deletions(-)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
index f4f6c5a6cf8282..aa520c79a73e7a 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
@@ -16,7 +16,6 @@
#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CALLDESCRIPTION_H
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
-#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Compiler.h"
#include <optional>
@@ -28,6 +27,33 @@ class IdentifierInfo;
namespace clang {
namespace ento {
+/// The properties of a function declaration that determine whether it is a C
+/// library function with a given name. They are computed on first use, so
+/// matching a function against many names computes each at most once.
+class CLibraryFunctionInfo {
+ const FunctionDecl *FD;
+ std::optional<unsigned> BuiltinID;
+ std::optional<std::string> BuiltinName;
+ std::optional<bool> IsPredefinedLibFunction;
+ std::optional<bool> IsCLibraryDecl;
+
+ unsigned getBuiltinID();
+ /// Must only be called if getBuiltinID() is not 0.
+ StringRef getBuiltinName();
+ /// Returns true if the builtin is a predefined library function (e.g.
+ /// getlogin_r). Must only be called if getBuiltinID() is not 0.
+ bool isPredefinedLibFunction();
+ /// Returns true if the function has an identifier, it is in the top-level
+ /// or the \c std namespace, and it is externally visible or inline.
+ bool isCLibraryDecl();
+
+public:
+ explicit CLibraryFunctionInfo(const FunctionDecl *FD) : FD(FD) {}
+
+ /// Implements CheckerContext::isCLibraryFunction(FD, Name).
+ bool isCLibraryFunction(StringRef Name);
+};
+
/// A `CallDescription` is a pattern that can be used to _match_ calls
/// based on the qualified name and the argument/parameter counts.
class CallDescription {
@@ -85,11 +111,7 @@ class CallDescription {
/// The properties of a call that are needed for matching it. They are
/// computed once when a call is matched against many CallDescriptions.
- class Callee {
- /// Constructed on first use, as only C library entries need it.
- mutable std::optional<CheckerContext::CLibraryCallee> CLib;
-
- public:
+ struct Callee {
const FunctionDecl *FD = nullptr;
size_t ArgCount = 0;
size_t ParamCount = 0;
@@ -97,9 +119,6 @@ class CallDescription {
explicit Callee(const CallEvent &Call);
explicit Callee(const CallExpr &CE);
-
- /// Equivalent to CheckerContext::isCLibraryFunction(FD, Name).
- bool isCLibraryFunction(StringRef Name) const;
};
public:
@@ -204,7 +223,7 @@ class CallDescription {
/// @}
private:
- bool matchesImpl(const Callee &C) const;
+ bool matchesImpl(const Callee &C, CLibraryFunctionInfo &CLib) const;
bool matchNameOnly(const NamedDecl *ND) const;
bool matchQualifiedNameParts(const Decl *D) const;
@@ -266,10 +285,11 @@ template <typename T> class CallDescriptionMap {
if (!C.FD)
return nullptr;
+ CLibraryFunctionInfo CLib(C.FD);
// Slow path: linear lookup.
// TODO: Implement some sort of fast path.
for (const std::pair<CallDescription, T> &I : LinearMap)
- if (I.first.matchesImpl(C))
+ if (I.first.matchesImpl(C, CLib))
return &I.second;
return nullptr;
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
index 6725cebfb27ce3..02b59ced4321fe 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
@@ -435,37 +435,6 @@ class CheckerContext {
std::string getMacroNameOrSpelling(SourceLocation &Loc);
private:
- // CallDescription matches a function against many names, so it computes the
- // callee-dependent parts of isCLibraryFunction only once.
- friend class CallDescription;
-
- /// The properties of a function that isCLibraryFunction depends on, computed
- /// on first use.
- class CLibraryCallee {
- const FunctionDecl *FD;
- unsigned BId;
- std::optional<std::string> BuiltinName;
- std::optional<bool> IsPredefinedLibFunction;
- std::optional<bool> IsCLibraryDecl;
-
- public:
- explicit CLibraryCallee(const FunctionDecl *FD)
- : FD(FD), BId(FD->getBuiltinID()) {}
-
- const FunctionDecl *getDecl() const { return FD; }
- unsigned getBuiltinID() const { return BId; }
- /// Must only be called if getBuiltinID() is not 0.
- StringRef getBuiltinName();
- /// Returns true if the builtin is a predefined library function (e.g.
- /// getlogin_r). Must only be called if getBuiltinID() is not 0.
- bool isPredefinedLibFunction();
- /// Returns true if the function has an identifier, it is in the top-level
- /// or the \c std namespace, and it is externally visible or inline.
- bool isCLibraryDecl();
- };
-
- static bool isCLibraryFunction(CLibraryCallee &C, StringRef Name);
-
ExplodedNode *addTransitionImpl(const ProgramStateRef &State, bool MarkAsSink,
ExplodedNode *P, const ProgramPointTag *Tag) {
Changed = true;
diff --git a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
index 9a29b4edf34971..40098f1960d7e2 100644
--- a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
@@ -13,9 +13,12 @@
//===----------------------------------------------------------------------===//
#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
+#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
+#include "clang/Basic/Builtins.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
+#include "llvm/ADT/StringExtras.h"
#include <iterator>
#include <optional>
@@ -71,18 +74,112 @@ ento::CallDescription::Callee::Callee(const CallExpr &CE) {
IsMethod = isa<CXXMethodDecl>(FD);
}
-bool ento::CallDescription::Callee::isCLibraryFunction(StringRef Name) const {
- if (!CLib)
- CLib.emplace(FD);
- return CheckerContext::isCLibraryFunction(*CLib, Name);
+unsigned ento::CLibraryFunctionInfo::getBuiltinID() {
+ if (!BuiltinID)
+ BuiltinID = FD->getBuiltinID();
+ return *BuiltinID;
+}
+
+StringRef ento::CLibraryFunctionInfo::getBuiltinName() {
+ if (!BuiltinName)
+ BuiltinName = FD->getASTContext().BuiltinInfo.getName(getBuiltinID());
+ return *BuiltinName;
+}
+
+bool ento::CLibraryFunctionInfo::isPredefinedLibFunction() {
+ if (!IsPredefinedLibFunction)
+ IsPredefinedLibFunction =
+ FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(getBuiltinID());
+ return *IsPredefinedLibFunction;
+}
+
+bool ento::CLibraryFunctionInfo::isCLibraryDecl() {
+ if (IsCLibraryDecl)
+ return *IsCLibraryDecl;
+
+ IsCLibraryDecl = false;
+ // If this is a special C++ name without IdentifierInfo, it can't be a
+ // C library function.
+ if (!FD->getIdentifier())
+ return false;
+
+ // C library functions are either declared directly within a TU (the common
+ // case) or they are accessed through the namespace `std` (when they are used
+ // in C++ via headers like <cstdlib>).
+ const DeclContext *DC = FD->getDeclContext()->getRedeclContext();
+ if (!(DC->isTranslationUnit() || DC->isStdNamespace()))
+ return false;
+
+ // If this function is not externally visible, it is not a C library function.
+ // Note that we make an exception for inline functions, which may be
+ // declared in header files without external linkage.
+ IsCLibraryDecl = FD->isInlined() || FD->isExternallyVisible();
+ return *IsCLibraryDecl;
+}
+
+bool ento::CLibraryFunctionInfo::isCLibraryFunction(StringRef Name) {
+ // To avoid false positives (Ex: finding user defined functions with
+ // similar names), only perform fuzzy name matching when it's a builtin.
+ // Using a string compare is slow, we might want to switch on BuiltinID here.
+ if (getBuiltinID() != 0) {
+ if (Name.empty())
+ return true;
+ StringRef BName = getBuiltinName();
+ // Accept exact match.
+ if (BName == Name)
+ return true;
+ // Only perform fuzzy name matching for non-library builtins (e.g.
+ // __builtin_memcpy, __builtin_memcpy_inline), not for distinct predefined
+ // library functions (e.g. objc_read_weak vs read, getlogin_r vs getlogin).
+ if (!isPredefinedLibFunction()) {
+ size_t start = BName.find(Name);
+ if (start != StringRef::npos) {
+ // v-- match starts here
+ // ...xxxxx...
+ // _xxxxx_
+ // ^ ^ lookbehind and lookahead characters
+
+ const auto MatchPredecessor = [&]() -> bool {
+ return start <= 0 || !llvm::isAlpha(BName[start - 1]);
+ };
+ const auto MatchSuccessor = [&]() -> bool {
+ std::size_t LookbehindPlace = start + Name.size();
+ return LookbehindPlace >= BName.size() ||
+ !llvm::isAlpha(BName[LookbehindPlace]);
+ };
+
+ if (MatchPredecessor() && MatchSuccessor())
+ return true;
+ }
+ }
+ }
+
+ if (!isCLibraryDecl())
+ return false;
+
+ if (Name.empty())
+ return true;
+
+ StringRef FName = FD->getName();
+ if (FName == Name)
+ return true;
+
+ if (FName.starts_with("__inline") && FName.contains(Name))
+ return true;
+
+ return false;
}
bool ento::CallDescription::matches(const CallEvent &Call) const {
- return matchesImpl(Callee(Call));
+ Callee C(Call);
+ CLibraryFunctionInfo CLib(C.FD);
+ return matchesImpl(C, CLib);
}
bool ento::CallDescription::matchesAsWritten(const CallExpr &CE) const {
- return matchesImpl(Callee(CE));
+ Callee C(CE);
+ CLibraryFunctionInfo CLib(C.FD);
+ return matchesImpl(C, CLib);
}
bool ento::CallDescription::matchNameOnly(const NamedDecl *ND) const {
@@ -157,7 +254,8 @@ bool ento::CallDescription::matchQualifiedNameParts(const Decl *D) const {
return QualifierPartsIt == QualifierPartsEndIt;
}
-bool ento::CallDescription::matchesImpl(const Callee &C) const {
+bool ento::CallDescription::matchesImpl(const Callee &C,
+ CLibraryFunctionInfo &CLib) const {
const FunctionDecl *FD = C.FD;
if (!FD)
return false;
@@ -175,7 +273,7 @@ bool ento::CallDescription::matchesImpl(const Callee &C) const {
// In addition to accepting FOO() with CLibrary rules, we also want to
// accept calls to __FOO_chk() and __builtin___FOO_chk().
if (CheckerContext::isHardenedVariantOf(FD, getFunctionName()) &&
- C.isCLibraryFunction(StringRef())) {
+ CLib.isCLibraryFunction(StringRef())) {
// Check that the actual argument/parameter counts are greater or equal
// to the required counts. (Setting a requirement to std::nullopt matches
// anything, so in that case value_or ensures that the value is compared
@@ -190,7 +288,7 @@ bool ento::CallDescription::matchesImpl(const Callee &C) const {
return false;
if (MatchAs == Mode::CLibrary || MatchAs == Mode::CLibraryMaybeHardened)
- return C.isCLibraryFunction(getFunctionName());
+ return CLib.isCLibraryFunction(getFunctionName());
if (!matchNameOnly(FD))
return false;
diff --git a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
index 107592f0513149..7bf52873be94e4 100644
--- a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
@@ -12,9 +12,8 @@
//===----------------------------------------------------------------------===//
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
-#include "clang/Basic/Builtins.h"
#include "clang/Lex/Lexer.h"
-#include "llvm/ADT/StringExtras.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
using namespace clang;
using namespace ento;
@@ -48,97 +47,7 @@ StringRef CheckerContext::getDeclDescription(const Decl *D) {
bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
StringRef Name) {
- CLibraryCallee C(FD);
- return isCLibraryFunction(C, Name);
-}
-
-StringRef CheckerContext::CLibraryCallee::getBuiltinName() {
- if (!BuiltinName)
- BuiltinName = FD->getASTContext().BuiltinInfo.getName(BId);
- return *BuiltinName;
-}
-
-bool CheckerContext::CLibraryCallee::isPredefinedLibFunction() {
- if (!IsPredefinedLibFunction)
- IsPredefinedLibFunction =
- FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(BId);
- return *IsPredefinedLibFunction;
-}
-
-bool CheckerContext::CLibraryCallee::isCLibraryDecl() {
- if (IsCLibraryDecl)
- return *IsCLibraryDecl;
-
- IsCLibraryDecl = false;
- // If this is a special C++ name without IdentifierInfo, it can't be a
- // C library function.
- if (!FD->getIdentifier())
- return false;
-
- // C library functions are either declared directly within a TU (the common
- // case) or they are accessed through the namespace `std` (when they are used
- // in C++ via headers like <cstdlib>).
- const DeclContext *DC = FD->getDeclContext()->getRedeclContext();
- if (!(DC->isTranslationUnit() || DC->isStdNamespace()))
- return false;
-
- // If this function is not externally visible, it is not a C library function.
- // Note that we make an exception for inline functions, which may be
- // declared in header files without external linkage.
- IsCLibraryDecl = FD->isInlined() || FD->isExternallyVisible();
- return *IsCLibraryDecl;
-}
-
-bool CheckerContext::isCLibraryFunction(CLibraryCallee &C, StringRef Name) {
- // To avoid false positives (Ex: finding user defined functions with
- // similar names), only perform fuzzy name matching when it's a builtin.
- // Using a string compare is slow, we might want to switch on BuiltinID here.
- if (C.getBuiltinID() != 0) {
- if (Name.empty())
- return true;
- // Accept exact match.
- if (C.getBuiltinName() == Name)
- return true;
- // Only perform fuzzy name matching for non-library builtins (e.g.
- // __builtin_memcpy, __builtin_memcpy_inline), not for distinct predefined
- // library functions (e.g. objc_read_weak vs read, getlogin_r vs getlogin).
- if (!C.isPredefinedLibFunction()) {
- size_t start = C.getBuiltinName().find(Name);
- if (start != StringRef::npos) {
- // v-- match starts here
- // ...xxxxx...
- // _xxxxx_
- // ^ ^ lookbehind and lookahead characters
-
- const auto MatchPredecessor = [&]() -> bool {
- return start <= 0 || !llvm::isAlpha(C.getBuiltinName()[start - 1]);
- };
- const auto MatchSuccessor = [&]() -> bool {
- std::size_t LookbehindPlace = start + Name.size();
- return LookbehindPlace >= C.getBuiltinName().size() ||
- !llvm::isAlpha(C.getBuiltinName()[LookbehindPlace]);
- };
-
- if (MatchPredecessor() && MatchSuccessor())
- return true;
- }
- }
- }
-
- if (!C.isCLibraryDecl())
- return false;
-
- if (Name.empty())
- return true;
-
- StringRef FName = C.getDecl()->getName();
- if (FName == Name)
- return true;
-
- if (FName.starts_with("__inline") && FName.contains(Name))
- return true;
-
- return false;
+ return CLibraryFunctionInfo(FD).isCLibraryFunction(Name);
}
bool CheckerContext::isHardenedVariantOf(const FunctionDecl *FD,
>From 6be02d3296407261c2c9b9bfe01e0aabbf67df9b Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Thu, 26 Nov 2026 12:12:24 +0000
Subject: [PATCH 4/4] [analyzer][NFC] Address review comments
Assisted-by: Claude Opus 5.5
---
.../StaticAnalyzer/Core/PathSensitive/CallDescription.h | 5 +++--
clang/lib/StaticAnalyzer/Core/CallDescription.cpp | 2 ++
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
index aa520c79a73e7a..4283565bb7c84c 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
@@ -31,7 +31,7 @@ namespace ento {
/// library function with a given name. They are computed on first use, so
/// matching a function against many names computes each at most once.
class CLibraryFunctionInfo {
- const FunctionDecl *FD;
+ const FunctionDecl *const FD;
std::optional<unsigned> BuiltinID;
std::optional<std::string> BuiltinName;
std::optional<bool> IsPredefinedLibFunction;
@@ -110,7 +110,8 @@ class CallDescription {
template <typename T> friend class CallDescriptionMap;
/// The properties of a call that are needed for matching it. They are
- /// computed once when a call is matched against many CallDescriptions.
+ /// computed once when a call is matched against many CallDescriptions, to
+ /// avoid repeated virtual calls like CallEvent::getDecl().
struct Callee {
const FunctionDecl *FD = nullptr;
size_t ArgCount = 0;
diff --git a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
index 40098f1960d7e2..1ea11391ddfbff 100644
--- a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
@@ -81,12 +81,14 @@ unsigned ento::CLibraryFunctionInfo::getBuiltinID() {
}
StringRef ento::CLibraryFunctionInfo::getBuiltinName() {
+ assert(getBuiltinID() != 0 && "Not a builtin");
if (!BuiltinName)
BuiltinName = FD->getASTContext().BuiltinInfo.getName(getBuiltinID());
return *BuiltinName;
}
bool ento::CLibraryFunctionInfo::isPredefinedLibFunction() {
+ assert(getBuiltinID() != 0 && "Not a builtin");
if (!IsPredefinedLibFunction)
IsPredefinedLibFunction =
FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(getBuiltinID());
More information about the cfe-commits
mailing list