CheckerContext.cpp 4.5 KB

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