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
|
|
|
|
|
|
|
|
|
|
|
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;
|
2012-11-03 07:49:24 +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.
|
|
|
|
const DeclContext *DC = FD->getDeclContext();
|
|
|
|
while (DC->isTransparentContext())
|
|
|
|
DC = DC->getParent();
|
|
|
|
|
|
|
|
// If this function is in a namespace, it is not a C library function.
|
|
|
|
if (!DC->isTranslationUnit())
|
|
|
|
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
|
|
|
}
|
|
|
|
|