clang  3.7.0
CheckerContext.cpp
Go to the documentation of this file.
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 
16 #include "clang/Basic/Builtins.h"
17 #include "clang/Lex/Lexer.h"
18 
19 using namespace clang;
20 using namespace ento;
21 
24  const Expr *Callee = CE->getCallee();
25  SVal L = State->getSVal(Callee, Pred->getLocationContext());
26  return L.getAsFunctionDecl();
27 }
28 
29 StringRef 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 
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  if (Name.empty())
47  return true;
48  StringRef BName = FD->getASTContext().BuiltinInfo.GetName(BId);
49  if (BName.find(Name) != StringRef::npos)
50  return true;
51  }
52 
53  const IdentifierInfo *II = FD->getIdentifier();
54  // If this is a special C++ name without IdentifierInfo, it can't be a
55  // C library function.
56  if (!II)
57  return false;
58 
59  // Look through 'extern "C"' and anything similar invented in the future.
60  const DeclContext *DC = FD->getDeclContext();
61  while (DC->isTransparentContext())
62  DC = DC->getParent();
63 
64  // If this function is in a namespace, it is not a C library function.
65  if (!DC->isTranslationUnit())
66  return false;
67 
68  // If this function is not externally visible, it is not a C library function.
69  // Note that we make an exception for inline functions, which may be
70  // declared in header files without external linkage.
71  if (!FD->isInlined() && !FD->isExternallyVisible())
72  return false;
73 
74  if (Name.empty())
75  return true;
76 
77  StringRef FName = II->getName();
78  if (FName.equals(Name))
79  return true;
80 
81  if (FName.startswith("__inline") && (FName.find(Name) != StringRef::npos))
82  return true;
83 
84  if (FName.startswith("__") && FName.endswith("_chk") &&
85  FName.find(Name) != StringRef::npos)
86  return true;
87 
88  return false;
89 }
90 
92  if (Loc.isMacroID())
94  getLangOpts());
96  return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
97 }
98 
static unsigned getSpelling(const Token &Tok, const char *&Buffer, const SourceManager &SourceMgr, const LangOptions &LangOpts, bool *Invalid=nullptr)
Definition: Lexer.cpp:358
bool isTransparentContext() const
Definition: DeclBase.cpp:883
StringRef getCalleeName(const FunctionDecl *FunDecl) const
Get the name of the called function (path-sensitive).
bool isMacroID() const
IdentifierInfo * getIdentifier() const
Definition: Decl.h:163
const Expr * getCallee() const
Definition: Expr.h:2188
const FunctionDecl * getCalleeDecl(const CallExpr *CE) const
Get the declaration of the called function (path-sensitive).
LineState State
bool isTranslationUnit() const
Definition: DeclBase.h:1243
const FunctionDecl * getAsFunctionDecl() const
Definition: SVals.cpp:51
const LocationContext * getLocationContext() const
StringRef getName() const
Return the actual identifier string.
const ProgramStateRef & getState() const
static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name=StringRef())
Returns true if the callee is an externally-visible function in the top-level namespace, such as malloc.
DeclContext * getDeclContext()
Definition: DeclBase.h:381
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition: Decl.h:2040
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1174
bool isExternallyVisible() const
Definition: Decl.h:279
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:284
const char * GetName(unsigned ID) const
Return the identifier name for the specified builtin, e.g. "__builtin_abs".
Definition: Builtins.h:85
unsigned getBuiltinID() const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:2601
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition: Lexer.cpp:956
StringRef getMacroNameOrSpelling(SourceLocation &Loc)
Depending on wither the location corresponds to a macro, return either the macro name or the token sp...
const LangOptions & getLangOpts() const
SourceManager & getSourceManager()
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:441
Defines enum values for all the target-independent builtin functions.