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