CheckerHelpers.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //===---- CheckerHelpers.cpp - Helper functions for checkers ----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines several static functions for use in checkers.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
  14. #include "clang/AST/Expr.h"
  15. // Recursively find any substatements containing macros
  16. bool clang::ento::containsMacro(const Stmt *S) {
  17. if (S->getLocStart().isMacroID())
  18. return true;
  19. if (S->getLocEnd().isMacroID())
  20. return true;
  21. for (const Stmt *Child : S->children())
  22. if (Child && containsMacro(Child))
  23. return true;
  24. return false;
  25. }
  26. // Recursively find any substatements containing enum constants
  27. bool clang::ento::containsEnum(const Stmt *S) {
  28. const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
  29. if (DR && isa<EnumConstantDecl>(DR->getDecl()))
  30. return true;
  31. for (const Stmt *Child : S->children())
  32. if (Child && containsEnum(Child))
  33. return true;
  34. return false;
  35. }
  36. // Recursively find any substatements containing static vars
  37. bool clang::ento::containsStaticLocal(const Stmt *S) {
  38. const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
  39. if (DR)
  40. if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
  41. if (VD->isStaticLocal())
  42. return true;
  43. for (const Stmt *Child : S->children())
  44. if (Child && containsStaticLocal(Child))
  45. return true;
  46. return false;
  47. }
  48. // Recursively find any substatements containing __builtin_offsetof
  49. bool clang::ento::containsBuiltinOffsetOf(const Stmt *S) {
  50. if (isa<OffsetOfExpr>(S))
  51. return true;
  52. for (const Stmt *Child : S->children())
  53. if (Child && containsBuiltinOffsetOf(Child))
  54. return true;
  55. return false;
  56. }