clang-tools 6.0.0

ExprSequence.cpp
Go to the documentation of this file.
1 //===---------- ExprSequence.cpp - 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 #include "ExprSequence.h"
11
12 namespace clang {
13 namespace tidy {
14 namespace utils {
15
16 // Returns the Stmt nodes that are parents of 'S', skipping any potential
17 // intermediate non-Stmt nodes.
18 //
19 // In almost all cases, this function returns a single parent or no parents at
20 // all.
21 //
22 // The case that a Stmt has multiple parents is rare but does actually occur in
23 // the parts of the AST that we're interested in. Specifically, InitListExpr
24 // nodes cause ASTContext::getParent() to return multiple parents for certain
25 // nodes in their subtree because RecursiveASTVisitor visits both the syntactic
26 // and semantic forms of InitListExpr, and the parent-child relationships are
27 // different between the two forms.
28 static SmallVector<const Stmt *, 1> getParentStmts(const Stmt *S,
29 ASTContext *Context) {
30 SmallVector<const Stmt *, 1> Result;
31
32 ASTContext::DynTypedNodeList Parents = Context->getParents(*S);
33
34 SmallVector<ast_type_traits::DynTypedNode, 1> NodesToProcess(Parents.begin(),
35 Parents.end());
36
37 while (!NodesToProcess.empty()) {
38 ast_type_traits::DynTypedNode Node = NodesToProcess.back();
39 NodesToProcess.pop_back();
40
41 if (const auto *S = Node.get<Stmt>()) {
42 Result.push_back(S);
43 } else {
44 Parents = Context->getParents(Node);
45 NodesToProcess.append(Parents.begin(), Parents.end());
46 }
47 }
48
49 return Result;
50 }
51
52 namespace {
53 bool isDescendantOrEqual(const Stmt *Descendant, const Stmt *Ancestor,
54 ASTContext *Context) {
55 if (Descendant == Ancestor)
56 return true;
57 for (const Stmt *Parent : getParentStmts(Descendant, Context)) {
58 if (isDescendantOrEqual(Parent, Ancestor, Context))
59 return true;
60 }
61
62 return false;
63 }
64 }
65
66 ExprSequence::ExprSequence(const CFG *TheCFG, ASTContext *TheContext)
67 : Context(TheContext) {
68 for (const auto &SyntheticStmt : TheCFG->synthetic_stmts()) {
69 SyntheticStmtSourceMap[SyntheticStmt.first] = SyntheticStmt.second;
70 }
71 }
72
73 bool ExprSequence::inSequence(const Stmt *Before, const Stmt *After) const {
74 Before = resolveSyntheticStmt(Before);
75 After = resolveSyntheticStmt(After);
76
77 // If 'After' is in the subtree of the siblings that follow 'Before' in the
78 // chain of successors, we know that 'After' is sequenced after 'Before'.
79 for (const Stmt *Successor = getSequenceSuccessor(Before); Successor;
80 Successor = getSequenceSuccessor(Successor)) {
81 if (isDescendantOrEqual(After, Successor, Context))
82 return true;
83 }
84
85 // If 'After' is a parent of 'Before' or is sequenced after one of these
86 // parents, we know that it is sequenced after 'Before'.
87 for (const Stmt *Parent : getParentStmts(Before, Context)) {
88 if (Parent == After || inSequence(Parent, After))
89 return true;
90 }
91
92 return false;
93 }
94
95 bool ExprSequence::potentiallyAfter(const Stmt *After,
96 const Stmt *Before) const {
97 return !inSequence(After, Before);
98 }
99
100 const Stmt *ExprSequence::getSequenceSuccessor(const Stmt *S) const {
101 for (const Stmt *Parent : getParentStmts(S, Context)) {
102 if (const auto *BO = dyn_cast<BinaryOperator>(Parent)) {
103 // Comma operator: Right-hand side is sequenced after the left-hand side.
104 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
105 return BO->getRHS();
106 } else if (const auto *InitList = dyn_cast<InitListExpr>(Parent)) {
107 // Initializer list: Each initializer clause is sequenced after the
108 // clauses that precede it.
109 for (unsigned I = 1; I < InitList->getNumInits(); ++I) {
110 if (InitList->getInit(I - 1) == S)
111 return InitList->getInit(I);
112 }
113 } else if (const auto *Compound = dyn_cast<CompoundStmt>(Parent)) {
114 // Compound statement: Each sub-statement is sequenced after the
115 // statements that precede it.
116 const Stmt *Previous = nullptr;
117 for (const auto *Child : Compound->body()) {
118 if (Previous == S)
119 return Child;
120 Previous = Child;
121 }
122 } else if (const auto *TheDeclStmt = dyn_cast<DeclStmt>(Parent)) {
123 // Declaration: Every initializer expression is sequenced after the
124 // initializer expressions that precede it.
125 const Expr *PreviousInit = nullptr;
126 for (const Decl *TheDecl : TheDeclStmt->decls()) {
127 if (const auto *TheVarDecl = dyn_cast<VarDecl>(TheDecl)) {
128 if (const Expr *Init = TheVarDecl->getInit()) {
129 if (PreviousInit == S)
130 return Init;
131 PreviousInit = Init;
132 }
133 }
134 }
135 } else if (const auto *ForRange = dyn_cast<CXXForRangeStmt>(Parent)) {
136 // Range-based for: Loop variable declaration is sequenced before the
137 // body. (We need this rule because these get placed in the same
138 // CFGBlock.)
139 if (S == ForRange->getLoopVarStmt())
140 return ForRange->getBody();
141 } else if (const auto *TheIfStmt = dyn_cast<IfStmt>(Parent)) {
142 // If statement: If a variable is declared inside the condition, the
143 // expression used to initialize the variable is sequenced before the
144 // evaluation of the condition.
145 if (S == TheIfStmt->getConditionVariableDeclStmt())
146 return TheIfStmt->getCond();
147 }
148 }
149
150 return nullptr;
151 }
152
153 const Stmt *ExprSequence::resolveSyntheticStmt(const Stmt *S) const {
154 if (SyntheticStmtSourceMap.count(S))
155 return SyntheticStmtSourceMap.lookup(S);
156 return S;
157 }
158
159 StmtToBlockMap::StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext)
160 : Context(TheContext) {
161 for (const auto *B : *TheCFG) {
162 for (const auto &Elem : *B) {
163 if (Optional<CFGStmt> S = Elem.getAs<CFGStmt>())
164 Map[S->getStmt()] = B;
165 }
166 }
167 }
168
169 const CFGBlock *StmtToBlockMap::blockContainingStmt(const Stmt *S) const {
170 while (!Map.count(S)) {
171 SmallVector<const Stmt *, 1> Parents = getParentStmts(S, Context);
172 if (Parents.empty())
173 return nullptr;
174 S = Parents[0];
175 }
176
177 return Map.lookup(S);
178 }
179
180 } // namespace utils
181 } // namespace tidy
182 } // namespace clang
clang::tidy::utils::ExprSequence::inSequence
bool inSequence(const Stmt *Before, const Stmt *After) const
Returns whether Before is sequenced before After.
Definition: ExprSequence.cpp:73
clang::tidy::utils::StmtToBlockMap::blockContainingStmt
const CFGBlock * blockContainingStmt(const Stmt *S) const
Returns the block that S is contained in.
Definition: ExprSequence.cpp:169
clang::tidy::utils::ExprSequence::potentiallyAfter
bool potentiallyAfter(const Stmt *After, const Stmt *Before) const
Returns whether After can potentially be evaluated after Before.
Definition: ExprSequence.cpp:95
clang::tidy::utils::StmtToBlockMap::StmtToBlockMap
StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext)
Initializes the map for the given CFG.
Definition: ExprSequence.cpp:159
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::utils::ExprSequence::ExprSequence
ExprSequence(const CFG *TheCFG, ASTContext *TheContext)
Initializes this ExprSequence with sequence information for the given CFG.
Definition: ExprSequence.cpp:66
clang::tidy::utils::getParentStmts
static SmallVector< const Stmt *, 1 > getParentStmts(const Stmt *S, ASTContext *Context)
Definition: ExprSequence.cpp:28
ExprSequence.h