CheckerHelpers.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //===---- CheckerHelpers.cpp - Helper functions for checkers ----*- C++ -*-===//
  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 several static functions for use in checkers.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
  13. #include "clang/AST/Decl.h"
  14. #include "clang/AST/Expr.h"
  15. #include "clang/Lex/Preprocessor.h"
  16. #include <optional>
  17. namespace clang {
  18. namespace ento {
  19. // Recursively find any substatements containing macros
  20. bool containsMacro(const Stmt *S) {
  21. if (S->getBeginLoc().isMacroID())
  22. return true;
  23. if (S->getEndLoc().isMacroID())
  24. return true;
  25. for (const Stmt *Child : S->children())
  26. if (Child && containsMacro(Child))
  27. return true;
  28. return false;
  29. }
  30. // Recursively find any substatements containing enum constants
  31. bool containsEnum(const Stmt *S) {
  32. const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
  33. if (DR && isa<EnumConstantDecl>(DR->getDecl()))
  34. return true;
  35. for (const Stmt *Child : S->children())
  36. if (Child && containsEnum(Child))
  37. return true;
  38. return false;
  39. }
  40. // Recursively find any substatements containing static vars
  41. bool containsStaticLocal(const Stmt *S) {
  42. const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
  43. if (DR)
  44. if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
  45. if (VD->isStaticLocal())
  46. return true;
  47. for (const Stmt *Child : S->children())
  48. if (Child && containsStaticLocal(Child))
  49. return true;
  50. return false;
  51. }
  52. // Recursively find any substatements containing __builtin_offsetof
  53. bool containsBuiltinOffsetOf(const Stmt *S) {
  54. if (isa<OffsetOfExpr>(S))
  55. return true;
  56. for (const Stmt *Child : S->children())
  57. if (Child && containsBuiltinOffsetOf(Child))
  58. return true;
  59. return false;
  60. }
  61. // Extract lhs and rhs from assignment statement
  62. std::pair<const clang::VarDecl *, const clang::Expr *>
  63. parseAssignment(const Stmt *S) {
  64. const VarDecl *VD = nullptr;
  65. const Expr *RHS = nullptr;
  66. if (auto Assign = dyn_cast_or_null<BinaryOperator>(S)) {
  67. if (Assign->isAssignmentOp()) {
  68. // Ordinary assignment
  69. RHS = Assign->getRHS();
  70. if (auto DE = dyn_cast_or_null<DeclRefExpr>(Assign->getLHS()))
  71. VD = dyn_cast_or_null<VarDecl>(DE->getDecl());
  72. }
  73. } else if (auto PD = dyn_cast_or_null<DeclStmt>(S)) {
  74. // Initialization
  75. assert(PD->isSingleDecl() && "We process decls one by one");
  76. VD = cast<VarDecl>(PD->getSingleDecl());
  77. RHS = VD->getAnyInitializer();
  78. }
  79. return std::make_pair(VD, RHS);
  80. }
  81. Nullability getNullabilityAnnotation(QualType Type) {
  82. const auto *AttrType = Type->getAs<AttributedType>();
  83. if (!AttrType)
  84. return Nullability::Unspecified;
  85. if (AttrType->getAttrKind() == attr::TypeNullable)
  86. return Nullability::Nullable;
  87. else if (AttrType->getAttrKind() == attr::TypeNonNull)
  88. return Nullability::Nonnull;
  89. return Nullability::Unspecified;
  90. }
  91. std::optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP) {
  92. const auto *MacroII = PP.getIdentifierInfo(Macro);
  93. if (!MacroII)
  94. return std::nullopt;
  95. const MacroInfo *MI = PP.getMacroInfo(MacroII);
  96. if (!MI)
  97. return std::nullopt;
  98. // Filter out parens.
  99. std::vector<Token> FilteredTokens;
  100. FilteredTokens.reserve(MI->tokens().size());
  101. for (auto &T : MI->tokens())
  102. if (!T.isOneOf(tok::l_paren, tok::r_paren))
  103. FilteredTokens.push_back(T);
  104. // Parse an integer at the end of the macro definition.
  105. const Token &T = FilteredTokens.back();
  106. // FIXME: EOF macro token coming from a PCH file on macOS while marked as
  107. // literal, doesn't contain any literal data
  108. if (!T.isLiteral() || !T.getLiteralData())
  109. return std::nullopt;
  110. StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
  111. llvm::APInt IntValue;
  112. constexpr unsigned AutoSenseRadix = 0;
  113. if (ValueStr.getAsInteger(AutoSenseRadix, IntValue))
  114. return std::nullopt;
  115. // Parse an optional minus sign.
  116. size_t Size = FilteredTokens.size();
  117. if (Size >= 2) {
  118. if (FilteredTokens[Size - 2].is(tok::minus))
  119. IntValue = -IntValue;
  120. }
  121. return IntValue.getSExtValue();
  122. }
  123. OperatorKind operationKindFromOverloadedOperator(OverloadedOperatorKind OOK,
  124. bool IsBinary) {
  125. llvm::StringMap<BinaryOperatorKind> BinOps{
  126. #define BINARY_OPERATION(Name, Spelling) {Spelling, BO_##Name},
  127. #include "clang/AST/OperationKinds.def"
  128. };
  129. llvm::StringMap<UnaryOperatorKind> UnOps{
  130. #define UNARY_OPERATION(Name, Spelling) {Spelling, UO_##Name},
  131. #include "clang/AST/OperationKinds.def"
  132. };
  133. switch (OOK) {
  134. #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
  135. case OO_##Name: \
  136. if (IsBinary) { \
  137. auto BinOpIt = BinOps.find(Spelling); \
  138. if (BinOpIt != BinOps.end()) \
  139. return OperatorKind(BinOpIt->second); \
  140. else \
  141. llvm_unreachable("operator was expected to be binary but is not"); \
  142. } else { \
  143. auto UnOpIt = UnOps.find(Spelling); \
  144. if (UnOpIt != UnOps.end()) \
  145. return OperatorKind(UnOpIt->second); \
  146. else \
  147. llvm_unreachable("operator was expected to be unary but is not"); \
  148. } \
  149. break;
  150. #include "clang/Basic/OperatorKinds.def"
  151. default:
  152. llvm_unreachable("unexpected operator kind");
  153. }
  154. }
  155. } // namespace ento
  156. } // namespace clang