clang 18.0.0git
Go to the documentation of this file.
1//== CheckerContext.cpp - Context info for path-sensitive checkers-----------=//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9// This file defines CheckerContext that provides contextual info for
10// path-sensitive checkers.
16#include "clang/Lex/Lexer.h"
17#include "llvm/ADT/StringExtras.h"
19using namespace clang;
20using namespace ento;
23 const FunctionDecl *D = CE->getDirectCallee();
24 if (D)
25 return D;
27 const Expr *Callee = CE->getCallee();
28 SVal L = Pred->getSVal(Callee);
29 return L.getAsFunctionDecl();
32StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
33 if (!FunDecl)
34 return StringRef();
35 IdentifierInfo *funI = FunDecl->getIdentifier();
36 if (!funI)
37 return StringRef();
38 return funI->getName();
42 if (isa<ObjCMethodDecl, CXXMethodDecl>(D))
43 return "method";
44 if (isa<BlockDecl>(D))
45 return "anonymous block";
46 return "function";
50 StringRef Name) {
51 // To avoid false positives (Ex: finding user defined functions with
52 // similar names), only perform fuzzy name matching when it's a builtin.
53 // Using a string compare is slow, we might want to switch on BuiltinID here.
54 unsigned BId = FD->getBuiltinID();
55 if (BId != 0) {
56 if (Name.empty())
57 return true;
58 StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
59 size_t start = BName.find(Name);
60 if (start != StringRef::npos) {
61 // Accept exact match.
62 if (BName.size() == Name.size())
63 return true;
65 // v-- match starts here
66 // ...xxxxx...
67 // _xxxxx_
68 // ^ ^ lookbehind and lookahead characters
70 const auto MatchPredecessor = [=]() -> bool {
71 return start <= 0 || !llvm::isAlpha(BName[start - 1]);
72 };
73 const auto MatchSuccessor = [=]() -> bool {
74 std::size_t LookbehindPlace = start + Name.size();
75 return LookbehindPlace >= BName.size() ||
76 !llvm::isAlpha(BName[LookbehindPlace]);
77 };
79 if (MatchPredecessor() && MatchSuccessor())
80 return true;
81 }
82 }
84 const IdentifierInfo *II = FD->getIdentifier();
85 // If this is a special C++ name without IdentifierInfo, it can't be a
86 // C library function.
87 if (!II)
88 return false;
90 // Look through 'extern "C"' and anything similar invented in the future.
91 // If this function is not in TU directly, it is not a C library function.
93 return false;
95 // If this function is not externally visible, it is not a C library function.
96 // Note that we make an exception for inline functions, which may be
97 // declared in header files without external linkage.
98 if (!FD->isInlined() && !FD->isExternallyVisible())
99 return false;
101 if (Name.empty())
102 return true;
104 StringRef FName = II->getName();
105 if (FName.equals(Name))
106 return true;
108 if (FName.startswith("__inline") && FName.contains(Name))
109 return true;
111 if (FName.startswith("__") && FName.endswith("_chk") && FName.contains(Name))
112 return true;
114 return false;
118 if (Loc.isMacroID())
120 getLangOpts());
121 SmallString<16> buf;
125/// Evaluate comparison and return true if it's known that condition is true
126static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
127 SVal RHSVal, ProgramStateRef State) {
128 if (LHSVal.isUnknownOrUndef())
129 return false;
130 ProgramStateManager &Mgr = State->getStateManager();
131 if (!isa<NonLoc>(LHSVal)) {
132 LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
133 LHSVal.castAs<Loc>());
134 if (LHSVal.isUnknownOrUndef() || !isa<NonLoc>(LHSVal))
135 return false;
136 }
138 SValBuilder &Bldr = Mgr.getSValBuilder();
139 SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
140 Bldr.getConditionType());
141 if (Eval.isUnknownOrUndef())
142 return false;
143 ProgramStateRef StTrue, StFalse;
144 std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
145 return StTrue && !StFalse;
148bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
149 DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
150 return evalComparison(getSVal(E), BO_GE, V, getState());
155 return evalComparison(getSVal(E), BO_LT, V, getState());
#define V(N, I)
Definition: ASTContext.h:3241
Defines enum values for all the target-independent builtin functions.
static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp, SVal RHSVal, ProgramStateRef State)
Evaluate comparison and return true if it's known that condition is true.
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:638
llvm::StringRef getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Definition: Builtins.h:103
CallExpr - Represents a function call (C99, C++ []).
Definition: Expr.h:2847
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition: Expr.h:3017
Expr * getCallee()
Definition: Expr.h:2997
bool isTranslationUnit() const
Definition: DeclBase.h:2140
DeclContext * getRedeclContext()
getRedeclContext - Retrieve the context in which an entity conflicts with other entities of the same ...
Definition: DeclBase.cpp:1933
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:501
DeclContext * getDeclContext()
Definition: DeclBase.h:453
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1957
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3569
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition: Decl.h:2788
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition: Lexer.cpp:1015
static unsigned getSpelling(const Token &Tok, const char *&Buffer, const SourceManager &SourceMgr, const LangOptions &LangOpts, bool *Invalid=nullptr)
getSpelling - This method is used to get the spelling of a token into a preallocated buffer,...
Definition: Lexer.cpp:407
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:269
bool isExternallyVisible() const
Definition: Decl.h:406
Encodes a location in the source.
StringRef getDeclDescription(const Decl *D)
Returns the word that should be used to refer to the declaration in the report.
const SourceManager & getSourceManager()
SValBuilder & getSValBuilder()
StringRef getCalleeName(const FunctionDecl *FunDecl) const
Get the name of the called function (path-sensitive).
const ProgramStateRef & getState() const
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
bool isNegative(const Expr *E)
Returns true if the value of E is negative.
bool isGreaterOrEqual(const Expr *E, unsigned long long Val)
Returns true if the value of E is greater than or equal to Val under unsigned comparison.
static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name=StringRef())
Returns true if the callee is an externally-visible function in the top-level namespace,...
const FunctionDecl * getCalleeDecl(const CallExpr *CE) const
Get the declaration of the called function (path-sensitive).
StringRef getMacroNameOrSpelling(SourceLocation &Loc)
Depending on wither the location corresponds to a macro, return either the macro name or the token sp...
const LangOptions & getLangOpts() const
SVal getSVal(const Stmt *S) const
Get the value of an arbitrary expression at this node.
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:278
QualType getConditionType() const
Definition: SValBuilder.h:141
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, SVal lhs, SVal rhs, QualType type)
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:55
bool isUnknownOrUndef() const
Definition: SVals.h:106
const FunctionDecl * getAsFunctionDecl() const
getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a CodeTextRegion wrapping a FunctionDecl...
Definition: SVals.cpp:46
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition: SVals.h:82
virtual SVal getBinding(Store store, Loc loc, QualType T=QualType())=0
Return the value bound to specified location in a given state.