clang  12.0.0git
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 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 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/Builtins.h"
16 #include "clang/Lex/Lexer.h"
18 using namespace clang;
19 using namespace ento;
22  const Expr *Callee = CE->getCallee();
23  SVal L = Pred->getSVal(Callee);
24  return L.getAsFunctionDecl();
25 }
27 StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
28  if (!FunDecl)
29  return StringRef();
30  IdentifierInfo *funI = FunDecl->getIdentifier();
31  if (!funI)
32  return StringRef();
33  return funI->getName();
34 }
37  if (isa<ObjCMethodDecl>(D) || isa<CXXMethodDecl>(D))
38  return "method";
39  if (isa<BlockDecl>(D))
40  return "anonymous block";
41  return "function";
42 }
45  StringRef Name) {
46  // To avoid false positives (Ex: finding user defined functions with
47  // similar names), only perform fuzzy name matching when it's a builtin.
48  // Using a string compare is slow, we might want to switch on BuiltinID here.
49  unsigned BId = FD->getBuiltinID();
50  if (BId != 0) {
51  if (Name.empty())
52  return true;
53  StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
54  if (BName.find(Name) != StringRef::npos)
55  return true;
56  }
58  const IdentifierInfo *II = FD->getIdentifier();
59  // If this is a special C++ name without IdentifierInfo, it can't be a
60  // C library function.
61  if (!II)
62  return false;
64  // Look through 'extern "C"' and anything similar invented in the future.
65  // If this function is not in TU directly, it is not a C library function.
67  return false;
69  // If this function is not externally visible, it is not a C library function.
70  // Note that we make an exception for inline functions, which may be
71  // declared in header files without external linkage.
72  if (!FD->isInlined() && !FD->isExternallyVisible())
73  return false;
75  if (Name.empty())
76  return true;
78  StringRef FName = II->getName();
79  if (FName.equals(Name))
80  return true;
82  if (FName.startswith("__inline") && (FName.find(Name) != StringRef::npos))
83  return true;
85  if (FName.startswith("__") && FName.endswith("_chk") &&
86  FName.find(Name) != StringRef::npos)
87  return true;
89  return false;
90 }
93  if (Loc.isMacroID())
95  getLangOpts());
96  SmallString<16> buf;
97  return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
98 }
100 /// Evaluate comparison and return true if it's known that condition is true
101 static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
102  SVal RHSVal, ProgramStateRef State) {
103  if (LHSVal.isUnknownOrUndef())
104  return false;
105  ProgramStateManager &Mgr = State->getStateManager();
106  if (!LHSVal.getAs<NonLoc>()) {
107  LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
108  LHSVal.castAs<Loc>());
109  if (LHSVal.isUnknownOrUndef() || !LHSVal.getAs<NonLoc>())
110  return false;
111  }
113  SValBuilder &Bldr = Mgr.getSValBuilder();
114  SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
115  Bldr.getConditionType());
116  if (Eval.isUnknownOrUndef())
117  return false;
118  ProgramStateRef StTrue, StFalse;
119  std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
120  return StTrue && !StFalse;
121 }
123 bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
124  DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
125  return evalComparison(getSVal(E), BO_GE, V, getState());
126 }
129  DefinedSVal V = getSValBuilder().makeIntVal(0, false);
130  return evalComparison(getSVal(E), BO_LT, V, getState());
131 }
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:400
Represents a function declaration or definition.
Definition: Decl.h:1805
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:278
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
StringRef getDeclDescription(const Decl *D)
Returns the word that should be used to refer to the declaration in the report.
const FunctionDecl * getCalleeDecl(const CallExpr *CE) const
Get the declaration of the called function (path-sensitive).
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:244
StringRef getCalleeName(const FunctionDecl *FunDecl) const
Get the name of the called function (path-sensitive).
One of these records is kept for each identifier that is lexed.
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.
LineState State
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3253
const SourceManager & getSourceManager()
const char * getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Definition: Builtins.h:83
This represents one expression.
Definition: Expr.h:109
static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name=StringRef())
Returns true if the callee is an externally-visible function in the top-level namespace, such as malloc.
#define V(N, I)
Definition: ASTContext.h:2984
Expr * getCallee()
Definition: Expr.h:2880
DeclContext * getDeclContext()
Definition: DeclBase.h:439
Encodes a location in the source.
static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp, SVal RHSVal, ProgramStateRef State)
Evaluate comparison and return true if it&#39;s known that condition is true.
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:414
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:75
SVal getSVal(const Stmt *S) const
Get the value of an arbitrary expression at this node.
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition: Lexer.cpp:978
StringRef getName() const
Return the actual identifier string.
Dataflow Directional Tag Classes.
StringRef getMacroNameOrSpelling(SourceLocation &Loc)
Depending on wither the location corresponds to a macro, return either the macro name or the token sp...
const FunctionDecl * getAsFunctionDecl() const
getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a CodeTextRegion wrapping a FunctionDecl...
Definition: SVals.cpp:63
const ProgramStateRef & getState() const
DeclContext * getRedeclContext()
getRedeclContext - Retrieve the context in which an entity conflicts with other entities of the same ...
Definition: DeclBase.cpp:1804
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition: Decl.h:2554
bool isNegative(const Expr *E)
Returns true if the value of E is negative.
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:598
SValBuilder & getSValBuilder()
CallExpr - Represents a function call (C99, C++ []).
Definition: Expr.h:2730
bool isTranslationUnit() const
Definition: DeclBase.h:1890
Defines enum values for all the target-independent builtin functions.
const LangOptions & getLangOpts() const
bool isExternallyVisible() const
Definition: Decl.h:377