CheckerContext.cpp revision 3026348bd4c13a0f83b59839f64065e0fcbea253
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 15#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 16#include "clang/Basic/Builtins.h" 17#include "clang/Lex/Lexer.h" 18 19using namespace clang; 20using namespace ento; 21 22const FunctionDecl *CheckerContext::getCalleeDecl(const CallExpr *CE) const { 23 const ProgramState *State = getState(); 24 const Expr *Callee = CE->getCallee(); 25 SVal L = State->getSVal(Callee, Pred->getLocationContext()); 26 return L.getAsFunctionDecl(); 27} 28 29StringRef 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 38 39bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD, 40 StringRef Name){ 41 // To avoid false positives (Ex: finding user defined functions with 42 // similar names), only perform fuzzy name matching when it's a builtin. 43 // Using a string compare is slow, we might want to switch on BuiltinID here. 44 unsigned BId = FD->getBuiltinID(); 45 if (BId != 0) { 46 ASTContext &Context = getASTContext(); 47 StringRef BName = Context.BuiltinInfo.GetName(BId); 48 if (StringRef(BName).find(Name) != StringRef::npos) 49 return true; 50 } 51 52 if (FD->isExternC() && FD->getIdentifier()->getName().equals(Name)) 53 return true; 54 55 return false; 56} 57 58StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) { 59 if (Loc.isMacroID()) 60 return Lexer::getImmediateMacroName(Loc, getSourceManager(), 61 getLangOptions()); 62 SmallVector<char, 16> buf; 63 return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOptions()); 64} 65 66