#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/Basic/Builtins.h"
#include "clang/Lex/Lexer.h"
using namespace clang;
using namespace ento;
const FunctionDecl *CheckerContext::getCalleeDecl(const CallExpr *CE) const {
const FunctionDecl *D = CE->getDirectCallee();
if (D)
return D;
const Expr *Callee = CE->getCallee();
SVal L = Pred->getSVal(Callee);
return L.getAsFunctionDecl();
}
StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
if (!FunDecl)
return StringRef();
IdentifierInfo *funI = FunDecl->getIdentifier();
if (!funI)
return StringRef();
return funI->getName();
}
StringRef CheckerContext::getDeclDescription(const Decl *D) {
if (isa<ObjCMethodDecl, CXXMethodDecl>(D))
return "method";
if (isa<BlockDecl>(D))
return "anonymous block";
return "function";
}
bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
StringRef Name) {
unsigned BId = FD->getBuiltinID();
if (BId != 0) {
if (Name.empty())
return true;
StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
size_t start = BName.find(Name);
if (start != StringRef::npos) {
if (BName.size() == Name.size())
return true;
const auto MatchPredecessor = [=]() -> bool {
return start <= 0 || !llvm::isAlpha(BName[start - 1]);
};
const auto MatchSuccessor = [=]() -> bool {
std::size_t LookbehindPlace = start + Name.size();
return LookbehindPlace >= BName.size() ||
!llvm::isAlpha(BName[LookbehindPlace]);
};
if (MatchPredecessor() && MatchSuccessor())
return true;
}
}
const IdentifierInfo *II = FD->getIdentifier();
if (!II)
return false;
if (!FD->getDeclContext()->getRedeclContext()->isTranslationUnit())
return false;
if (!FD->isInlined() && !FD->isExternallyVisible())
return false;
if (Name.empty())
return true;
StringRef FName = II->getName();
if (FName.equals(Name))
return true;
if (FName.startswith("__inline") && FName.contains(Name))
return true;
if (FName.startswith("__") && FName.endswith("_chk") && FName.contains(Name))
return true;
return false;
}
StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) {
if (Loc.isMacroID())
return Lexer::getImmediateMacroName(Loc, getSourceManager(),
getLangOpts());
SmallString<16> buf;
return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
}
static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
SVal RHSVal, ProgramStateRef State) {
if (LHSVal.isUnknownOrUndef())
return false;
ProgramStateManager &Mgr = State->getStateManager();
if (!isa<NonLoc>(LHSVal)) {
LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
LHSVal.castAs<Loc>());
if (LHSVal.isUnknownOrUndef() || !isa<NonLoc>(LHSVal))
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());
}