2011-11-17 09:09:15 +08:00
|
|
|
//== CheckerContext.cpp - Context info for path-sensitive checkers-----------=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines CheckerContext that provides contextual info for
|
|
|
|
// path-sensitive checkers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2012-01-18 10:45:07 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2012-01-20 08:11:12 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2012-01-18 10:45:07 +08:00
|
|
|
|
2011-11-17 09:09:15 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
2011-12-01 13:57:37 +08:00
|
|
|
const FunctionDecl *CheckerContext::getCalleeDecl(const CallExpr *CE) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef State = getState();
|
2011-11-17 09:09:15 +08:00
|
|
|
const Expr *Callee = CE->getCallee();
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal L = State->getSVal(Callee, Pred->getLocationContext());
|
2011-12-01 13:57:37 +08:00
|
|
|
return L.getAsFunctionDecl();
|
|
|
|
}
|
2011-11-17 09:09:15 +08:00
|
|
|
|
2012-01-18 10:45:07 +08:00
|
|
|
StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
|
|
|
|
if (!FunDecl)
|
2011-11-17 09:09:15 +08:00
|
|
|
return StringRef();
|
2012-01-18 10:45:07 +08:00
|
|
|
IdentifierInfo *funI = FunDecl->getIdentifier();
|
2011-11-17 09:09:15 +08:00
|
|
|
if (!funI)
|
|
|
|
return StringRef();
|
|
|
|
return funI->getName();
|
|
|
|
}
|
2012-01-18 10:45:07 +08:00
|
|
|
|
2016-01-30 02:43:15 +08:00
|
|
|
StringRef CheckerContext::getDeclDescription(const Decl *D) {
|
|
|
|
if (isa<ObjCMethodDecl>(D) || isa<CXXMethodDecl>(D))
|
|
|
|
return "method";
|
|
|
|
if (isa<BlockDecl>(D))
|
|
|
|
return "anonymous block";
|
|
|
|
return "function";
|
|
|
|
}
|
2012-01-18 10:45:07 +08:00
|
|
|
|
|
|
|
bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
|
2012-02-01 03:33:39 +08:00
|
|
|
StringRef Name) {
|
2012-01-18 10:45:07 +08:00
|
|
|
// To avoid false positives (Ex: finding user defined functions with
|
|
|
|
// similar names), only perform fuzzy name matching when it's a builtin.
|
|
|
|
// Using a string compare is slow, we might want to switch on BuiltinID here.
|
|
|
|
unsigned BId = FD->getBuiltinID();
|
|
|
|
if (BId != 0) {
|
2012-11-03 07:49:29 +08:00
|
|
|
if (Name.empty())
|
|
|
|
return true;
|
2015-08-06 09:01:12 +08:00
|
|
|
StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
|
2012-02-01 03:33:39 +08:00
|
|
|
if (BName.find(Name) != StringRef::npos)
|
2012-01-18 10:45:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-02 03:16:20 +08:00
|
|
|
const IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
// If this is a special C++ name without IdentifierInfo, it can't be a
|
|
|
|
// C library function.
|
|
|
|
if (!II)
|
|
|
|
return false;
|
|
|
|
|
2012-11-03 07:49:24 +08:00
|
|
|
// Look through 'extern "C"' and anything similar invented in the future.
|
2015-12-29 05:47:51 +08:00
|
|
|
// If this function is not in TU directly, it is not a C library function.
|
|
|
|
if (!FD->getDeclContext()->getRedeclContext()->isTranslationUnit())
|
2012-11-03 07:49:24 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// If this function is not externally visible, it is not a C library function.
|
2012-11-03 07:49:29 +08:00
|
|
|
// Note that we make an exception for inline functions, which may be
|
|
|
|
// declared in header files without external linkage.
|
2013-05-13 08:12:11 +08:00
|
|
|
if (!FD->isInlined() && !FD->isExternallyVisible())
|
2012-11-03 07:49:24 +08:00
|
|
|
return false;
|
|
|
|
|
2012-11-03 07:49:29 +08:00
|
|
|
if (Name.empty())
|
|
|
|
return true;
|
|
|
|
|
2012-02-02 03:16:20 +08:00
|
|
|
StringRef FName = II->getName();
|
2012-02-18 06:35:26 +08:00
|
|
|
if (FName.equals(Name))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (FName.startswith("__inline") && (FName.find(Name) != StringRef::npos))
|
|
|
|
return true;
|
2012-02-01 03:33:39 +08:00
|
|
|
|
2012-02-18 06:35:26 +08:00
|
|
|
if (FName.startswith("__") && FName.endswith("_chk") &&
|
|
|
|
FName.find(Name) != StringRef::npos)
|
2012-01-18 10:45:07 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-20 08:11:12 +08:00
|
|
|
|
|
|
|
StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) {
|
2012-01-21 05:50:17 +08:00
|
|
|
if (Loc.isMacroID())
|
2012-01-20 08:11:12 +08:00
|
|
|
return Lexer::getImmediateMacroName(Loc, getSourceManager(),
|
2012-03-11 15:00:24 +08:00
|
|
|
getLangOpts());
|
2012-01-21 05:50:17 +08:00
|
|
|
SmallVector<char, 16> buf;
|
2012-03-11 15:00:24 +08:00
|
|
|
return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
|
2012-01-20 08:11:12 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 22:49:35 +08:00
|
|
|
/// Evaluate comparison and return true if it's known that condition is true
|
|
|
|
static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
|
|
|
|
SVal RHSVal, ProgramStateRef State) {
|
|
|
|
if (LHSVal.isUnknownOrUndef())
|
|
|
|
return false;
|
|
|
|
ProgramStateManager &Mgr = State->getStateManager();
|
|
|
|
if (!LHSVal.getAs<NonLoc>()) {
|
|
|
|
LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
|
|
|
|
LHSVal.castAs<Loc>());
|
|
|
|
if (LHSVal.isUnknownOrUndef() || !LHSVal.getAs<NonLoc>())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SValBuilder &Bldr = Mgr.getSValBuilder();
|
|
|
|
SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
|
|
|
|
Bldr.getConditionType());
|
|
|
|
if (Eval.isUnknownOrUndef())
|
|
|
|
return false;
|
|
|
|
ProgramStateRef StTrue, StFalse;
|
|
|
|
std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
|
|
|
|
return StTrue && !StFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
|
|
|
|
DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
|
|
|
|
return evalComparison(getSVal(E), BO_GE, V, getState());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckerContext::isNegative(const Expr *E) {
|
|
|
|
DefinedSVal V = getSValBuilder().makeIntVal(0, false);
|
|
|
|
return evalComparison(getSVal(E), BO_LT, V, getState());
|
|
|
|
}
|