clang  14.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/Basic/Builtins.h"
16 #include "clang/Lex/Lexer.h"
17 
18 using namespace clang;
19 using 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 
31 StringRef 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 
41  if (isa<ObjCMethodDecl, CXXMethodDecl>(D))
42  return "method";
43  if (isa<BlockDecl>(D))
44  return "anonymous block";
45  return "function";
46 }
47 
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  if (BName.contains(Name))
59  return true;
60  }
61 
62  const IdentifierInfo *II = FD->getIdentifier();
63  // If this is a special C++ name without IdentifierInfo, it can't be a
64  // C library function.
65  if (!II)
66  return false;
67 
68  // Look through 'extern "C"' and anything similar invented in the future.
69  // If this function is not in TU directly, it is not a C library function.
71  return false;
72 
73  // If this function is not externally visible, it is not a C library function.
74  // Note that we make an exception for inline functions, which may be
75  // declared in header files without external linkage.
76  if (!FD->isInlined() && !FD->isExternallyVisible())
77  return false;
78 
79  if (Name.empty())
80  return true;
81 
82  StringRef FName = II->getName();
83  if (FName.equals(Name))
84  return true;
85 
86  if (FName.startswith("__inline") && FName.contains(Name))
87  return true;
88 
89  if (FName.startswith("__") && FName.endswith("_chk") && FName.contains(Name))
90  return true;
91 
92  return false;
93 }
94 
96  if (Loc.isMacroID())
98  getLangOpts());
99  SmallString<16> buf;
101 }
102 
103 /// Evaluate comparison and return true if it's known that condition is true
104 static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
105  SVal RHSVal, ProgramStateRef State) {
106  if (LHSVal.isUnknownOrUndef())
107  return false;
108  ProgramStateManager &Mgr = State->getStateManager();
109  if (!LHSVal.getAs<NonLoc>()) {
110  LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
111  LHSVal.castAs<Loc>());
112  if (LHSVal.isUnknownOrUndef() || !LHSVal.getAs<NonLoc>())
113  return false;
114  }
115 
116  SValBuilder &Bldr = Mgr.getSValBuilder();
117  SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
118  Bldr.getConditionType());
119  if (Eval.isUnknownOrUndef())
120  return false;
121  ProgramStateRef StTrue, StFalse;
122  std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
123  return StTrue && !StFalse;
124 }
125 
126 bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
127  DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
128  return evalComparison(getSVal(E), BO_GE, V, getState());
129 }
130 
132  DefinedSVal V = getSValBuilder().makeIntVal(0, false);
133  return evalComparison(getSVal(E), BO_LT, V, getState());
134 }
Builtins.h
clang::Decl::getASTContext
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:414
clang::ento::SVal::getAsFunctionDecl
const FunctionDecl * getAsFunctionDecl() const
getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a CodeTextRegion wrapping a FunctionDecl...
Definition: SVals.cpp:65
clang::ento::CheckerContext::getDeclDescription
StringRef getDeclDescription(const Decl *D)
Returns the word that should be used to refer to the declaration in the report.
Definition: CheckerContext.cpp:40
clang::NamedDecl::isExternallyVisible
bool isExternallyVisible() const
Definition: Decl.h:407
clang::Lexer::getSpelling
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
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::ASTContext::BuiltinInfo
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:650
clang::ento::ProgramStateRef
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
Definition: ProgramState_Fwd.h:37
clang::ento::CheckerContext::getCalleeDecl
const FunctionDecl * getCalleeDecl(const CallExpr *CE) const
Get the declaration of the called function (path-sensitive).
Definition: CheckerContext.cpp:21
clang::ento::CheckerContext::isGreaterOrEqual
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.
Definition: CheckerContext.cpp:126
clang::ento::CheckerContext::getState
const ProgramStateRef & getState() const
Definition: CheckerContext.h:71
clang::FunctionDecl::isInlined
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition: Decl.h:2613
clang::CallExpr::getCallee
Expr * getCallee()
Definition: Expr.h:2945
V
#define V(N, I)
Definition: ASTContext.h:3121
clang::ento::DefinedSVal
Definition: SVals.h:269
clang::CallExpr::getDirectCallee
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition: Expr.h:2965
evalComparison
static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp, SVal RHSVal, ProgramStateRef State)
Evaluate comparison and return true if it's known that condition is true.
Definition: CheckerContext.cpp:104
clang::ento::CheckerContext::getCalleeName
StringRef getCalleeName(const FunctionDecl *FunDecl) const
Get the name of the called function (path-sensitive).
Definition: CheckerContext.cpp:31
clang::DeclContext::isTranslationUnit
bool isTranslationUnit() const
Definition: DeclBase.h:1935
llvm::SmallString< 16 >
clang::ento::CheckerContext::getMacroNameOrSpelling
StringRef getMacroNameOrSpelling(SourceLocation &Loc)
Depending on wither the location corresponds to a macro, return either the macro name or the token sp...
Definition: CheckerContext.cpp:95
clang::Lexer::getImmediateMacroName
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition: Lexer.cpp:993
clang::ento::CheckerContext::getSourceManager
const SourceManager & getSourceManager()
Definition: CheckerContext.h:106
clang::ento::CheckerContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: CheckerContext.h:87
clang::ento::Loc
Definition: SVals.h:327
clang::ento::CheckerContext::getSValBuilder
SValBuilder & getSValBuilder()
Definition: CheckerContext.h:112
clang::DeclContext::getRedeclContext
DeclContext * getRedeclContext()
getRedeclContext - Retrieve the context in which an entity conflicts with other entities of the same ...
Definition: DeclBase.cpp:1780
clang::ento::CheckerContext::getASTContext
ASTContext & getASTContext()
Definition: CheckerContext.h:83
clang::NamedDecl::getIdentifier
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:270
clang::Builtin::Context::getName
const char * getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Definition: Builtins.h:85
Lexer.h
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::ento::SValBuilder::makeIntVal
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:301
State
LineState State
Definition: UnwrappedLineFormatter.cpp:986
clang::BinaryOperatorKind
BinaryOperatorKind
Definition: OperationKinds.h:25
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:84
clang::ento::CheckerContext::isCLibraryFunction
static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name=StringRef())
Returns true if the callee is an externally-visible function in the top-level namespace,...
Definition: CheckerContext.cpp:48
CheckerContext.h
clang::IdentifierInfo::getName
StringRef getName() const
Return the actual identifier string.
Definition: IdentifierTable.h:195
clang::ento::CheckerContext::isNegative
bool isNegative(const Expr *E)
Returns true if the value of E is negative.
Definition: CheckerContext.cpp:131
clang
Definition: CalledOnceCheck.h:17
clang::ento::ExplodedNode::getSVal
SVal getSVal(const Stmt *S) const
Get the value of an arbitrary expression at this node.
Definition: ExplodedGraph.h:177
clang::ento::SVal
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:75
clang::FunctionDecl::getBuiltinID
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3320
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::ento::CheckerContext::getSVal
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
Definition: CheckerContext.h:146
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1856
clang::CallExpr
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2795
clang::Decl::getDeclContext
DeclContext * getDeclContext()
Definition: DeclBase.h:439