CheckerContext.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //== CheckerContext.cpp - Context info for path-sensitive checkers-----------=//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines CheckerContext that provides contextual info for
  10. // path-sensitive checkers.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  14. #include "clang/Basic/Builtins.h"
  15. #include "clang/Lex/Lexer.h"
  16. using namespace clang;
  17. using namespace ento;
  18. const FunctionDecl *CheckerContext::getCalleeDecl(const CallExpr *CE) const {
  19. const FunctionDecl *D = CE->getDirectCallee();
  20. if (D)
  21. return D;
  22. const Expr *Callee = CE->getCallee();
  23. SVal L = Pred->getSVal(Callee);
  24. return L.getAsFunctionDecl();
  25. }
  26. StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
  27. if (!FunDecl)
  28. return StringRef();
  29. IdentifierInfo *funI = FunDecl->getIdentifier();
  30. if (!funI)
  31. return StringRef();
  32. return funI->getName();
  33. }
  34. StringRef CheckerContext::getDeclDescription(const Decl *D) {
  35. if (isa<ObjCMethodDecl, CXXMethodDecl>(D))
  36. return "method";
  37. if (isa<BlockDecl>(D))
  38. return "anonymous block";
  39. return "function";
  40. }
  41. bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
  42. StringRef Name) {
  43. // To avoid false positives (Ex: finding user defined functions with
  44. // similar names), only perform fuzzy name matching when it's a builtin.
  45. // Using a string compare is slow, we might want to switch on BuiltinID here.
  46. unsigned BId = FD->getBuiltinID();
  47. if (BId != 0) {
  48. if (Name.empty())
  49. return true;
  50. StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
  51. size_t start = BName.find(Name);
  52. if (start != StringRef::npos) {
  53. // Accept exact match.
  54. if (BName.size() == Name.size())
  55. return true;
  56. // v-- match starts here
  57. // ...xxxxx...
  58. // _xxxxx_
  59. // ^ ^ lookbehind and lookahead characters
  60. const auto MatchPredecessor = [=]() -> bool {
  61. return start <= 0 || !llvm::isAlpha(BName[start - 1]);
  62. };
  63. const auto MatchSuccessor = [=]() -> bool {
  64. std::size_t LookbehindPlace = start + Name.size();
  65. return LookbehindPlace >= BName.size() ||
  66. !llvm::isAlpha(BName[LookbehindPlace]);
  67. };
  68. if (MatchPredecessor() && MatchSuccessor())
  69. return true;
  70. }
  71. }
  72. const IdentifierInfo *II = FD->getIdentifier();
  73. // If this is a special C++ name without IdentifierInfo, it can't be a
  74. // C library function.
  75. if (!II)
  76. return false;
  77. // Look through 'extern "C"' and anything similar invented in the future.
  78. // If this function is not in TU directly, it is not a C library function.
  79. if (!FD->getDeclContext()->getRedeclContext()->isTranslationUnit())
  80. return false;
  81. // If this function is not externally visible, it is not a C library function.
  82. // Note that we make an exception for inline functions, which may be
  83. // declared in header files without external linkage.
  84. if (!FD->isInlined() && !FD->isExternallyVisible())
  85. return false;
  86. if (Name.empty())
  87. return true;
  88. StringRef FName = II->getName();
  89. if (FName.equals(Name))
  90. return true;
  91. if (FName.startswith("__inline") && FName.contains(Name))
  92. return true;
  93. if (FName.startswith("__") && FName.endswith("_chk") && FName.contains(Name))
  94. return true;
  95. return false;
  96. }
  97. StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) {
  98. if (Loc.isMacroID())
  99. return Lexer::getImmediateMacroName(Loc, getSourceManager(),
  100. getLangOpts());
  101. SmallString<16> buf;
  102. return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
  103. }
  104. /// Evaluate comparison and return true if it's known that condition is true
  105. static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
  106. SVal RHSVal, ProgramStateRef State) {
  107. if (LHSVal.isUnknownOrUndef())
  108. return false;
  109. ProgramStateManager &Mgr = State->getStateManager();
  110. if (!isa<NonLoc>(LHSVal)) {
  111. LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
  112. LHSVal.castAs<Loc>());
  113. if (LHSVal.isUnknownOrUndef() || !isa<NonLoc>(LHSVal))
  114. return false;
  115. }
  116. SValBuilder &Bldr = Mgr.getSValBuilder();
  117. SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
  118. Bldr.getConditionType());
  119. if (Eval.isUnknownOrUndef())
  120. return false;
  121. ProgramStateRef StTrue, StFalse;
  122. std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
  123. return StTrue && !StFalse;
  124. }
  125. bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
  126. DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
  127. return evalComparison(getSVal(E), BO_GE, V, getState());
  128. }
  129. bool CheckerContext::isNegative(const Expr *E) {
  130. DefinedSVal V = getSValBuilder().makeIntVal(0, false);
  131. return evalComparison(getSVal(E), BO_LT, V, getState());
  132. }