CheckerContext.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  15. #include "clang/Basic/Builtins.h"
  16. #include "clang/Lex/Lexer.h"
  17. using namespace clang;
  18. using namespace ento;
  19. const FunctionDecl *CheckerContext::getCalleeDecl(const CallExpr *CE) const {
  20. ProgramStateRef State = getState();
  21. const Expr *Callee = CE->getCallee();
  22. SVal L = State->getSVal(Callee, Pred->getLocationContext());
  23. return L.getAsFunctionDecl();
  24. }
  25. StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
  26. if (!FunDecl)
  27. return StringRef();
  28. IdentifierInfo *funI = FunDecl->getIdentifier();
  29. if (!funI)
  30. return StringRef();
  31. return funI->getName();
  32. }
  33. bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
  34. StringRef Name) {
  35. // To avoid false positives (Ex: finding user defined functions with
  36. // similar names), only perform fuzzy name matching when it's a builtin.
  37. // Using a string compare is slow, we might want to switch on BuiltinID here.
  38. unsigned BId = FD->getBuiltinID();
  39. if (BId != 0) {
  40. if (Name.empty())
  41. return true;
  42. StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
  43. if (BName.find(Name) != StringRef::npos)
  44. return true;
  45. }
  46. const IdentifierInfo *II = FD->getIdentifier();
  47. // If this is a special C++ name without IdentifierInfo, it can't be a
  48. // C library function.
  49. if (!II)
  50. return false;
  51. // Look through 'extern "C"' and anything similar invented in the future.
  52. const DeclContext *DC = FD->getDeclContext();
  53. while (DC->isTransparentContext())
  54. DC = DC->getParent();
  55. // If this function is in a namespace, it is not a C library function.
  56. if (!DC->isTranslationUnit())
  57. return false;
  58. // If this function is not externally visible, it is not a C library function.
  59. // Note that we make an exception for inline functions, which may be
  60. // declared in header files without external linkage.
  61. if (!FD->isInlined() && !FD->isExternallyVisible())
  62. return false;
  63. if (Name.empty())
  64. return true;
  65. StringRef FName = II->getName();
  66. if (FName.equals(Name))
  67. return true;
  68. if (FName.startswith("__inline") && (FName.find(Name) != StringRef::npos))
  69. return true;
  70. if (FName.startswith("__") && FName.endswith("_chk") &&
  71. FName.find(Name) != StringRef::npos)
  72. return true;
  73. return false;
  74. }
  75. StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) {
  76. if (Loc.isMacroID())
  77. return Lexer::getImmediateMacroName(Loc, getSourceManager(),
  78. getLangOpts());
  79. SmallVector<char, 16> buf;
  80. return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
  81. }