CheckerHelpers.cpp 3.1 KB

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