1 //== CheckerContext.cpp - Context info for path-sensitive checkers-----------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines CheckerContext that provides contextual info for
11 // path-sensitive checkers.
13 //===----------------------------------------------------------------------===//
15 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
16 #include "clang/Basic/Builtins.h"
17 #include "clang/Lex/Lexer.h"
19 using namespace clang;
22 const FunctionDecl *CheckerContext::getCalleeDecl(const CallExpr *CE) const {
23 const Expr *Callee = CE->getCallee();
24 SVal L = Pred->getSVal(Callee);
25 return L.getAsFunctionDecl();
28 StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
31 IdentifierInfo *funI = FunDecl->getIdentifier();
34 return funI->getName();
37 StringRef CheckerContext::getDeclDescription(const Decl *D) {
38 if (isa<ObjCMethodDecl>(D) || isa<CXXMethodDecl>(D))
40 if (isa<BlockDecl>(D))
41 return "anonymous block";
45 bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
47 // To avoid false positives (Ex: finding user defined functions with
48 // similar names), only perform fuzzy name matching when it's a builtin.
49 // Using a string compare is slow, we might want to switch on BuiltinID here.
50 unsigned BId = FD->getBuiltinID();
54 StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
55 if (BName.find(Name) != StringRef::npos)
59 const IdentifierInfo *II = FD->getIdentifier();
60 // If this is a special C++ name without IdentifierInfo, it can't be a
61 // C library function.
65 // Look through 'extern "C"' and anything similar invented in the future.
66 // If this function is not in TU directly, it is not a C library function.
67 if (!FD->getDeclContext()->getRedeclContext()->isTranslationUnit())
70 // If this function is not externally visible, it is not a C library function.
71 // Note that we make an exception for inline functions, which may be
72 // declared in header files without external linkage.
73 if (!FD->isInlined() && !FD->isExternallyVisible())
79 StringRef FName = II->getName();
80 if (FName.equals(Name))
83 if (FName.startswith("__inline") && (FName.find(Name) != StringRef::npos))
86 if (FName.startswith("__") && FName.endswith("_chk") &&
87 FName.find(Name) != StringRef::npos)
93 StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) {
95 return Lexer::getImmediateMacroName(Loc, getSourceManager(),
97 SmallVector<char, 16> buf;
98 return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
101 /// Evaluate comparison and return true if it's known that condition is true
102 static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
103 SVal RHSVal, ProgramStateRef State) {
104 if (LHSVal.isUnknownOrUndef())
106 ProgramStateManager &Mgr = State->getStateManager();
107 if (!LHSVal.getAs<NonLoc>()) {
108 LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
109 LHSVal.castAs<Loc>());
110 if (LHSVal.isUnknownOrUndef() || !LHSVal.getAs<NonLoc>())
114 SValBuilder &Bldr = Mgr.getSValBuilder();
115 SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
116 Bldr.getConditionType());
117 if (Eval.isUnknownOrUndef())
119 ProgramStateRef StTrue, StFalse;
120 std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
121 return StTrue && !StFalse;
124 bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
125 DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
126 return evalComparison(getSVal(E), BO_GE, V, getState());
129 bool CheckerContext::isNegative(const Expr *E) {
130 DefinedSVal V = getSValBuilder().makeIntVal(0, false);
131 return evalComparison(getSVal(E), BO_LT, V, getState());