clang 24.0.0git
CheckerContext.cpp
Go to the documentation of this file.
1//== CheckerContext.cpp - Context info for path-sensitive checkers-----------=//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines CheckerContext that provides contextual info for
10// path-sensitive checkers.
11//
12//===----------------------------------------------------------------------===//
13
15#include "clang/Lex/Lexer.h"
17
18using namespace clang;
19using namespace ento;
20
22 const FunctionDecl *D = CE->getDirectCallee();
23 if (D)
24 return D;
25
26 const Expr *Callee = CE->getCallee();
27 SVal L = Pred->getSVal(Callee);
28 return L.getAsFunctionDecl();
29}
30
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();
38}
39
42 return "method";
43 if (isa<BlockDecl>(D))
44 return "anonymous block";
45 return "function";
46}
47
49 StringRef Name) {
51}
52
54 StringRef Name) {
55 const IdentifierInfo *II = FD->getIdentifier();
56 if (!II)
57 return false;
58
59 auto CompletelyMatchesParts = [II](auto... Parts) -> bool {
60 StringRef FName = II->getName();
61 return (FName.consume_front(Parts) && ...) && FName.empty();
62 };
63
64 return CompletelyMatchesParts("__", Name, "_chk") ||
65 CompletelyMatchesParts("__builtin_", "__", Name, "_chk");
66}
67
69 const auto &SM = getSourceManager();
70 const auto &LO = getLangOpts();
71 if (Loc.isMacroID())
72 return Lexer::getImmediateMacroName(Loc, SM, LO).str();
74 return Lexer::getSpelling(Loc, Buf, SM, LO).str();
75}
76
77/// Evaluate comparison and return true if it's known that condition is true
78static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
79 SVal RHSVal, ProgramStateRef State) {
80 if (LHSVal.isUnknownOrUndef())
81 return false;
82 ProgramStateManager &Mgr = State->getStateManager();
83 if (!isa<NonLoc>(LHSVal)) {
84 LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
85 LHSVal.castAs<Loc>());
86 if (LHSVal.isUnknownOrUndef() || !isa<NonLoc>(LHSVal))
87 return false;
88 }
89
90 SValBuilder &Bldr = Mgr.getSValBuilder();
91 SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
92 Bldr.getConditionType());
93 if (Eval.isUnknownOrUndef())
94 return false;
95 ProgramStateRef StTrue, StFalse;
96 std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
97 return StTrue && !StFalse;
98}
99
100bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
101 DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
102 return evalComparison(getSVal(E), BO_GE, V, getState());
103}
104
107 return evalComparison(getSVal(E), BO_LT, V, getState());
108}
#define V(N, I)
static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp, SVal RHSVal, ProgramStateRef State)
Evaluate comparison and return true if it's known that condition is true.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:3012
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3195
Expr * getCallee()
Definition Expr.h:3159
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
This represents one expression.
Definition Expr.h:114
Represents a function declaration or definition.
Definition Decl.h:2082
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:1111
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:462
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:297
Encodes a location in the source.
The properties of a function declaration that determine whether it is a C library function with a giv...
bool isCLibraryFunction(StringRef Name)
Implements CheckerContext::isCLibraryFunction(FD, Name).
StringRef getDeclDescription(const Decl *D)
Returns the word that should be used to refer to the declaration in the report.
SVal getSVal(const Expr *E) const
Get the value of arbitrary expressions at this point in the path.
const SourceManager & getSourceManager()
StringRef getCalleeName(const FunctionDecl *FunDecl) const
Get the name of the called function (path-sensitive).
std::string getMacroNameOrSpelling(SourceLocation &Loc)
Depending on whether the location corresponds to a macro, return either the macro name or the token s...
const ProgramStateRef & getState() const
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 given function 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).
const LangOptions & getLangOpts() const
static bool isHardenedVariantOf(const FunctionDecl *FD, StringRef Name)
In builds that use source hardening (-D_FORTIFY_SOURCE), many standard functions are implemented as m...
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
QualType getConditionType() const
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:57
bool isUnknownOrUndef() const
Definition SVals.h:115
const FunctionDecl * getAsFunctionDecl() const
getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a CodeTextRegion wrapping a FunctionDecl...
Definition SVals.cpp:45
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition SVals.h:84
virtual SVal getBinding(Store store, Loc loc, QualType T=QualType())=0
Return the value bound to specified location in a given state.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330