CheckerContext.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. const Expr *Callee = CE->getCallee();
  21. SVal L = Pred->getSVal(Callee);
  22. return L.getAsFunctionDecl();
  23. }
  24. StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
  25. if (!FunDecl)
  26. return StringRef();
  27. IdentifierInfo *funI = FunDecl->getIdentifier();
  28. if (!funI)
  29. return StringRef();
  30. return funI->getName();
  31. }
  32. StringRef CheckerContext::getDeclDescription(const Decl *D) {
  33. if (isa<ObjCMethodDecl>(D) || isa<CXXMethodDecl>(D))
  34. return "method";
  35. if (isa<BlockDecl>(D))
  36. return "anonymous block";
  37. return "function";
  38. }
  39. bool 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. 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. const IdentifierInfo *II = FD->getIdentifier();
  53. // If this is a special C++ name without IdentifierInfo, it can't be a
  54. // C library function.
  55. if (!II)
  56. return false;
  57. // Look through 'extern "C"' and anything similar invented in the future.
  58. // If this function is not in TU directly, it is not a C library function.
  59. if (!FD->getDeclContext()->getRedeclContext()->isTranslationUnit())
  60. return false;
  61. // If this function is not externally visible, it is not a C library function.
  62. // Note that we make an exception for inline functions, which may be
  63. // declared in header files without external linkage.
  64. if (!FD->isInlined() && !FD->isExternallyVisible())
  65. return false;
  66. if (Name.empty())
  67. return true;
  68. StringRef FName = II->getName();
  69. if (FName.equals(Name))
  70. return true;
  71. if (FName.startswith("__inline") && (FName.find(Name) != StringRef::npos))
  72. return true;
  73. if (FName.startswith("__") && FName.endswith("_chk") &&
  74. FName.find(Name) != StringRef::npos)
  75. return true;
  76. return false;
  77. }
  78. StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) {
  79. if (Loc.isMacroID())
  80. return Lexer::getImmediateMacroName(Loc, getSourceManager(),
  81. getLangOpts());
  82. SmallVector<char, 16> buf;
  83. return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
  84. }
  85. /// Evaluate comparison and return true if it's known that condition is true
  86. static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
  87. SVal RHSVal, ProgramStateRef State) {
  88. if (LHSVal.isUnknownOrUndef())
  89. return false;
  90. ProgramStateManager &Mgr = State->getStateManager();
  91. if (!LHSVal.getAs<NonLoc>()) {
  92. LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
  93. LHSVal.castAs<Loc>());
  94. if (LHSVal.isUnknownOrUndef() || !LHSVal.getAs<NonLoc>())
  95. return false;
  96. }
  97. SValBuilder &Bldr = Mgr.getSValBuilder();
  98. SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
  99. Bldr.getConditionType());
  100. if (Eval.isUnknownOrUndef())
  101. return false;
  102. ProgramStateRef StTrue, StFalse;
  103. std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
  104. return StTrue && !StFalse;
  105. }
  106. bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
  107. DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
  108. return evalComparison(getSVal(E), BO_GE, V, getState());
  109. }
  110. bool CheckerContext::isNegative(const Expr *E) {
  111. DefinedSVal V = getSValBuilder().makeIntVal(0, false);
  112. return evalComparison(getSVal(E), BO_LT, V, getState());
  113. }