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. namespace clang {
  17. namespace ento {
  18. // Recursively find any substatements containing macros
  19. bool containsMacro(const Stmt *S) {
  20. if (S->getBeginLoc().isMacroID())
  21. return true;
  22. if (S->getEndLoc().isMacroID())
  23. return true;
  24. for (const Stmt *Child : S->children())
  25. if (Child && containsMacro(Child))
  26. return true;
  27. return false;
  28. }
  29. // Recursively find any substatements containing enum constants
  30. bool containsEnum(const Stmt *S) {
  31. const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
  32. if (DR && isa<EnumConstantDecl>(DR->getDecl()))
  33. return true;
  34. for (const Stmt *Child : S->children())
  35. if (Child && containsEnum(Child))
  36. return true;
  37. return false;
  38. }
  39. // Recursively find any substatements containing static vars
  40. bool containsStaticLocal(const Stmt *S) {
  41. const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
  42. if (DR)
  43. if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
  44. if (VD->isStaticLocal())
  45. return true;
  46. for (const Stmt *Child : S->children())
  47. if (Child && containsStaticLocal(Child))
  48. return true;
  49. return false;
  50. }
  51. // Recursively find any substatements containing __builtin_offsetof
  52. bool containsBuiltinOffsetOf(const Stmt *S) {
  53. if (isa<OffsetOfExpr>(S))
  54. return true;
  55. for (const Stmt *Child : S->children())
  56. if (Child && containsBuiltinOffsetOf(Child))
  57. return true;
  58. return false;
  59. }
  60. // Extract lhs and rhs from assignment statement
  61. std::pair<const clang::VarDecl *, const clang::Expr *>
  62. parseAssignment(const Stmt *S) {
  63. const VarDecl *VD = nullptr;
  64. const Expr *RHS = nullptr;
  65. if (auto Assign = dyn_cast_or_null<BinaryOperator>(S)) {
  66. if (Assign->isAssignmentOp()) {
  67. // Ordinary assignment
  68. RHS = Assign->getRHS();
  69. if (auto DE = dyn_cast_or_null<DeclRefExpr>(Assign->getLHS()))
  70. VD = dyn_cast_or_null<VarDecl>(DE->getDecl());
  71. }
  72. } else if (auto PD = dyn_cast_or_null<DeclStmt>(S)) {
  73. // Initialization
  74. assert(PD->isSingleDecl() && "We process decls one by one");
  75. VD = cast<VarDecl>(PD->getSingleDecl());
  76. RHS = VD->getAnyInitializer();
  77. }
  78. return std::make_pair(VD, RHS);
  79. }
  80. Nullability getNullabilityAnnotation(QualType Type) {
  81. const auto *AttrType = Type->getAs<AttributedType>();
  82. if (!AttrType)
  83. return Nullability::Unspecified;
  84. if (AttrType->getAttrKind() == attr::TypeNullable)
  85. return Nullability::Nullable;
  86. else if (AttrType->getAttrKind() == attr::TypeNonNull)
  87. return Nullability::Nonnull;
  88. return Nullability::Unspecified;
  89. }
  90. llvm::Optional<int> tryExpandAsInteger(StringRef Macro,
  91. const Preprocessor &PP) {
  92. const auto *MacroII = PP.getIdentifierInfo(Macro);
  93. if (!MacroII)
  94. return llvm::None;
  95. const MacroInfo *MI = PP.getMacroInfo(MacroII);
  96. if (!MI)
  97. return llvm::None;
  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 llvm::None;
  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 llvm::None;
  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