VarBypassDetector.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===--- VarBypassDetector.h - Bypass jumps detector --------------*- 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 contains VarBypassDetector class, which is used to detect
  10. // local variable declarations which can be bypassed by jumps.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_LIB_CODEGEN_VARBYPASSDETECTOR_H
  14. #define LLVM_CLANG_LIB_CODEGEN_VARBYPASSDETECTOR_H
  15. #include "clang/AST/Decl.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/DenseSet.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. namespace clang {
  20. class Decl;
  21. class Stmt;
  22. class VarDecl;
  23. namespace CodeGen {
  24. /// The class detects jumps which bypass local variables declaration:
  25. /// goto L;
  26. /// int a;
  27. /// L:
  28. ///
  29. /// This is simplified version of JumpScopeChecker. Primary differences:
  30. /// * Detects only jumps into the scope local variables.
  31. /// * Does not detect jumps out of the scope of local variables.
  32. /// * Not limited to variables with initializers, JumpScopeChecker is limited.
  33. class VarBypassDetector {
  34. // Scope information. Contains a parent scope and related variable
  35. // declaration.
  36. llvm::SmallVector<std::pair<unsigned, const VarDecl *>, 48> Scopes;
  37. // List of jumps with scopes.
  38. llvm::SmallVector<std::pair<const Stmt *, unsigned>, 16> FromScopes;
  39. // Lookup map to find scope for destinations.
  40. llvm::DenseMap<const Stmt *, unsigned> ToScopes;
  41. // Set of variables which were bypassed by some jump.
  42. llvm::DenseSet<const VarDecl *> Bypasses;
  43. // If true assume that all variables are being bypassed.
  44. bool AlwaysBypassed = false;
  45. public:
  46. void Init(const Stmt *Body);
  47. /// Returns true if the variable declaration was by bypassed by any goto or
  48. /// switch statement.
  49. bool IsBypassed(const VarDecl *D) const {
  50. return AlwaysBypassed || Bypasses.contains(D);
  51. }
  52. private:
  53. bool BuildScopeInformation(const Decl *D, unsigned &ParentScope);
  54. bool BuildScopeInformation(const Stmt *S, unsigned &origParentScope);
  55. void Detect();
  56. void Detect(unsigned From, unsigned To);
  57. };
  58. }
  59. }
  60. #endif