ExprSequence.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //===------------- ExprSequence.h - clang-tidy ----------------------------===//
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_EXPRSEQUENCE_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_EXPRSEQUENCE_H
  10. #include "clang/Analysis/CFG.h"
  11. #include "clang/Lex/Lexer.h"
  12. #include "llvm/ADT/DenseMap.h"
  13. #include "llvm/ADT/SmallPtrSet.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "../ClangTidy.h"
  16. namespace clang::tidy::utils {
  17. /// Provides information about the evaluation order of (sub-)expressions within
  18. /// a `CFGBlock`.
  19. ///
  20. /// While a `CFGBlock` does contain individual `CFGElement`s for some
  21. /// sub-expressions, the order in which those `CFGElement`s appear reflects
  22. /// only one possible order in which the sub-expressions may be evaluated.
  23. /// However, we want to warn if any of the potential evaluation orders can lead
  24. /// to a use-after-move, not just the one contained in the `CFGBlock`.
  25. ///
  26. /// This class implements only a simplified version of the C++ sequencing
  27. /// rules. The main limitation is that we do not distinguish between value
  28. /// computation and side effect -- see the "Implementation" section for more
  29. /// details.
  30. ///
  31. /// Note: `SequenceChecker` from SemaChecking.cpp does a similar job (and much
  32. /// more thoroughly), but using it would require
  33. /// - Pulling `SequenceChecker` out into a header file (i.e. making it part of
  34. /// the API),
  35. /// - Removing the dependency of `SequenceChecker` on `Sema`, and
  36. /// - (Probably) modifying `SequenceChecker` to make it suitable to be used in
  37. /// this context.
  38. /// For the moment, it seems preferable to re-implement our own version of
  39. /// sequence checking that is special-cased to what we need here.
  40. ///
  41. /// Implementation
  42. /// --------------
  43. ///
  44. /// `ExprSequence` uses two types of sequencing edges between nodes in the AST:
  45. ///
  46. /// - Every `Stmt` is assumed to be sequenced after its children. This is
  47. /// overly optimistic because the standard only states that value computations
  48. /// of operands are sequenced before the value computation of the operator,
  49. /// making no guarantees about side effects (in general).
  50. ///
  51. /// For our purposes, this rule is sufficient, however, because this check is
  52. /// interested in operations on objects, which are generally performed through
  53. /// function calls (whether explicit and implicit). Function calls guarantee
  54. /// that the value computations and side effects for all function arguments
  55. /// are sequenced before the execution of the function.
  56. ///
  57. /// - In addition, some `Stmt`s are known to be sequenced before or after
  58. /// their siblings. For example, the `Stmt`s that make up a `CompoundStmt`are
  59. /// all sequenced relative to each other. The function
  60. /// `getSequenceSuccessor()` implements these sequencing rules.
  61. class ExprSequence {
  62. public:
  63. /// Initializes this `ExprSequence` with sequence information for the given
  64. /// `CFG`. `Root` is the root statement the CFG was built from.
  65. ExprSequence(const CFG *TheCFG, const Stmt *Root, ASTContext *TheContext);
  66. /// Returns whether \p Before is sequenced before \p After.
  67. bool inSequence(const Stmt *Before, const Stmt *After) const;
  68. /// Returns whether \p After can potentially be evaluated after \p Before.
  69. /// This is exactly equivalent to `!inSequence(After, Before)` but makes some
  70. /// conditions read more naturally.
  71. bool potentiallyAfter(const Stmt *After, const Stmt *Before) const;
  72. private:
  73. // Returns the sibling of \p S (if any) that is directly sequenced after \p S,
  74. // or nullptr if no such sibling exists. For example, if \p S is the child of
  75. // a `CompoundStmt`, this would return the Stmt that directly follows \p S in
  76. // the `CompoundStmt`.
  77. //
  78. // As the sequencing of many constructs that change control flow is already
  79. // encoded in the `CFG`, this function only implements the sequencing rules
  80. // for those constructs where sequencing cannot be inferred from the `CFG`.
  81. const Stmt *getSequenceSuccessor(const Stmt *S) const;
  82. const Stmt *resolveSyntheticStmt(const Stmt *S) const;
  83. ASTContext *Context;
  84. const Stmt *Root;
  85. llvm::DenseMap<const Stmt *, const Stmt *> SyntheticStmtSourceMap;
  86. };
  87. /// Maps `Stmt`s to the `CFGBlock` that contains them. Some `Stmt`s may be
  88. /// contained in more than one `CFGBlock`; in this case, they are mapped to the
  89. /// innermost block (i.e. the one that is furthest from the root of the tree).
  90. class StmtToBlockMap {
  91. public:
  92. /// Initializes the map for the given `CFG`.
  93. StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext);
  94. /// Returns the block that \p S is contained in. Some `Stmt`s may be contained
  95. /// in more than one `CFGBlock`; in this case, this function returns the
  96. /// innermost block (i.e. the one that is furthest from the root of the tree).
  97. const CFGBlock *blockContainingStmt(const Stmt *S) const;
  98. private:
  99. ASTContext *Context;
  100. llvm::DenseMap<const Stmt *, const CFGBlock *> Map;
  101. };
  102. } // namespace clang::tidy::utils
  103. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_EXPRSEQUENCE_H