clang 17.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"
18using namespace clang;
19using namespace ento;
22 const FunctionDecl *D = CE->getDirectCallee();
23 if (D)
24 return D;
26 const Expr *Callee = CE->getCallee();
27 SVal L = Pred->getSVal(Callee);
28 return L.getAsFunctionDecl();
31StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
32 if (!FunDecl)
33 return StringRef();
34 IdentifierInfo *funI = FunDecl->getIdentifier();
35 if (!funI)
36 return StringRef();
37 return funI->getName();
41 if (isa<ObjCMethodDecl, CXXMethodDecl>(D))
42 return "method";
43 if (isa<BlockDecl>(D))
44 return "anonymous block";
45 return "function";
49 StringRef Name) {
50 // To avoid false positives (Ex: finding user defined functions with
51 // similar names), only perform fuzzy name matching when it's a builtin.
52 // Using a string compare is slow, we might want to switch on BuiltinID here.
53 unsigned BId = FD->getBuiltinID();
54 if (BId != 0) {
55 if (Name.empty())
56 return true;
57 StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
58 size_t start = BName.find(Name);
59 if (start != StringRef::npos) {
60 // Accept exact match.
61 if (BName.size() == Name.size())
62 return true;
64 // v-- match starts here
65 // ...xxxxx...
66 // _xxxxx_
67 // ^ ^ lookbehind and lookahead characters
69 const auto MatchPredecessor = [=]() -> bool {
70 return start <= 0 || !llvm::isAlpha(BName[start - 1]);
71 };
72 const auto MatchSuccessor = [=]() -> bool {
73 std::size_t LookbehindPlace = start + Name.size();
74 return LookbehindPlace >= BName.size() ||
75 !llvm::isAlpha(BName[LookbehindPlace]);
76 };
78 if (MatchPredecessor() && MatchSuccessor())
79 return true;
80 }
81 }
83 const IdentifierInfo *II = FD->getIdentifier();
84 // If this is a special C++ name without IdentifierInfo, it can't be a
85 // C library function.
86 if (!II)
87 return false;
89 // Look through 'extern "C"' and anything similar invented in the future.
90 // If this function is not in TU directly, it is not a C library function.
92 return false;
94 // If this function is not externally visible, it is not a C library function.
95 // Note that we make an exception for inline functions, which may be
96 // declared in header files without external linkage.
97 if (!FD->isInlined() && !FD->isExternallyVisible())
98 return false;
100 if (Name.empty())
101 return true;
103 StringRef FName = II->getName();
104 if (FName.equals(Name))
105 return true;
107 if (FName.startswith("__inline") && FName.contains(Name))
108 return true;
110 if (FName.startswith("__") && FName.endswith("_chk") && FName.contains(Name))
111 return true;
113 return false;
117 if (Loc.isMacroID())
119 getLangOpts());
120 SmallString<16> buf;
124/// Evaluate comparison and return true if it's known that condition is true
125static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
126 SVal RHSVal, ProgramStateRef State) {
127 if (LHSVal.isUnknownOrUndef())
128 return false;
129 ProgramStateManager &Mgr = State->getStateManager();
130 if (!isa<NonLoc>(LHSVal)) {
131 LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
132 LHSVal.castAs<Loc>());
133 if (LHSVal.isUnknownOrUndef() || !isa<NonLoc>(LHSVal))
134 return false;
135 }
137 SValBuilder &Bldr = Mgr.getSValBuilder();
138 SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
139 Bldr.getConditionType());
140 if (Eval.isUnknownOrUndef())
141 return false;
142 ProgramStateRef StTrue, StFalse;
143 std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
144 return StTrue && !StFalse;
147bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
148 DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
149 return evalComparison(getSVal(E), BO_GE, V, getState());
154 return evalComparison(getSVal(E), BO_LT, V, getState());
#define V(N, I)
Definition: ASTContext.h:3230
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:632
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:2817
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition: Expr.h:2987
Expr * getCallee()
Definition: Expr.h:2967
bool isTranslationUnit() const
Definition: DeclBase.h:2017
DeclContext * getRedeclContext()
getRedeclContext - Retrieve the context in which an entity conflicts with other entities of the same ...
Definition: DeclBase.cpp:1864
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:429
DeclContext * getDeclContext()
Definition: DeclBase.h:441
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1917
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3482
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition: Decl.h:2716
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:996
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:403
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:268
bool isExternallyVisible() const
Definition: Decl.h:405
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:269
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:72
bool isUnknownOrUndef() const
Definition: SVals.h:132
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:99
virtual SVal getBinding(Store store, Loc loc, QualType T=QualType())=0
Return the value bound to specified location in a given state.