clang  6.0.0svn
CheckerContext.cpp
Go to the documentation of this file.
1 //== CheckerContext.cpp - Context info for path-sensitive checkers-----------=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines CheckerContext that provides contextual info for
11 // path-sensitive checkers.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "clang/Basic/Builtins.h"
17 #include "clang/Lex/Lexer.h"
18 
19 using namespace clang;
20 using namespace ento;
21 
24  const Expr *Callee = CE->getCallee();
25  SVal L = State->getSVal(Callee, Pred->getLocationContext());
26  return L.getAsFunctionDecl();
27 }
28 
29 StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
30  if (!FunDecl)
31  return StringRef();
32  IdentifierInfo *funI = FunDecl->getIdentifier();
33  if (!funI)
34  return StringRef();
35  return funI->getName();
36 }
37 
39  if (isa<ObjCMethodDecl>(D) || isa<CXXMethodDecl>(D))
40  return "method";
41  if (isa<BlockDecl>(D))
42  return "anonymous block";
43  return "function";
44 }
45 
47  StringRef Name) {
48  // To avoid false positives (Ex: finding user defined functions with
49  // similar names), only perform fuzzy name matching when it's a builtin.
50  // Using a string compare is slow, we might want to switch on BuiltinID here.
51  unsigned BId = FD->getBuiltinID();
52  if (BId != 0) {
53  if (Name.empty())
54  return true;
55  StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
56  if (BName.find(Name) != StringRef::npos)
57  return true;
58  }
59 
60  const IdentifierInfo *II = FD->getIdentifier();
61  // If this is a special C++ name without IdentifierInfo, it can't be a
62  // C library function.
63  if (!II)
64  return false;
65 
66  // Look through 'extern "C"' and anything similar invented in the future.
67  // If this function is not in TU directly, it is not a C library function.
69  return false;
70 
71  // If this function is not externally visible, it is not a C library function.
72  // Note that we make an exception for inline functions, which may be
73  // declared in header files without external linkage.
74  if (!FD->isInlined() && !FD->isExternallyVisible())
75  return false;
76 
77  if (Name.empty())
78  return true;
79 
80  StringRef FName = II->getName();
81  if (FName.equals(Name))
82  return true;
83 
84  if (FName.startswith("__inline") && (FName.find(Name) != StringRef::npos))
85  return true;
86 
87  if (FName.startswith("__") && FName.endswith("_chk") &&
88  FName.find(Name) != StringRef::npos)
89  return true;
90 
91  return false;
92 }
93 
95  if (Loc.isMacroID())
97  getLangOpts());
99  return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
100 }
101 
102 /// Evaluate comparison and return true if it's known that condition is true
103 static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
104  SVal RHSVal, ProgramStateRef State) {
105  if (LHSVal.isUnknownOrUndef())
106  return false;
107  ProgramStateManager &Mgr = State->getStateManager();
108  if (!LHSVal.getAs<NonLoc>()) {
109  LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
110  LHSVal.castAs<Loc>());
111  if (LHSVal.isUnknownOrUndef() || !LHSVal.getAs<NonLoc>())
112  return false;
113  }
114 
115  SValBuilder &Bldr = Mgr.getSValBuilder();
116  SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
117  Bldr.getConditionType());
118  if (Eval.isUnknownOrUndef())
119  return false;
120  ProgramStateRef StTrue, StFalse;
121  std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
122  return StTrue && !StFalse;
123 }
124 
125 bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
126  DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
127  return evalComparison(getSVal(E), BO_GE, V, getState());
128 }
129 
131  DefinedSVal V = getSValBuilder().makeIntVal(0, false);
132  return evalComparison(getSVal(E), BO_LT, V, getState());
133 }
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:389
An instance of this class is created to represent a function declaration or definition.
Definition: Decl.h:1697
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:254
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
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).
virtual SVal getBinding(Store store, Loc loc, QualType T=QualType())=0
Return the value bound to specified location in a given state.
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Definition: Decl.h:265
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
BinaryOperatorKind
const LocationContext * getLocationContext() const
const char * getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Definition: Builtins.h:86
Expr - This represents one expression.
Definition: Expr.h:106
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.
const Expr * getCallee() const
Definition: Expr.h:2249
DeclContext * getDeclContext()
Definition: DeclBase.h:425
QualType getConditionType() const
Definition: SValBuilder.h:136
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, SVal lhs, SVal rhs, QualType type)
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
Definition: SVals.h:100
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:365
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:63
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition: Lexer.cpp:971
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...
bool isMacroID() const
const FunctionDecl * getAsFunctionDecl() const
getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a CodeTextRegion wrapping a FunctionDecl...
Definition: SVals.cpp:52
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition: SVals.h:92
const ProgramStateRef & getState() const
DeclContext * getRedeclContext()
getRedeclContext - Retrieve the context in which an entity conflicts with other entities of the same ...
Definition: DeclBase.cpp:1648
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition: Decl.h:2251
bool isNegative(const Expr *E)
Returns true if the value of E is negative.
unsigned getBuiltinID() const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:2852
SourceManager & getSourceManager()
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:539
SValBuilder & getSValBuilder()
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2209
bool isTranslationUnit() const
Definition: DeclBase.h:1401
Defines enum values for all the target-independent builtin functions.
const LangOptions & getLangOpts() const
bool isUnknownOrUndef() const
Definition: SVals.h:133
bool isExternallyVisible() const
Definition: Decl.h:370