clang-tools 8.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, const Stmt *Root,
67 ASTContext *TheContext)
68 : Context(TheContext), Root(Root) {
69 for (const auto &SyntheticStmt : TheCFG->synthetic_stmts()) {
70 SyntheticStmtSourceMap[SyntheticStmt.first] = SyntheticStmt.second;
71 }
72 }
73
74 bool ExprSequence::inSequence(const Stmt *Before, const Stmt *After) const {
75 Before = resolveSyntheticStmt(Before);
76 After = resolveSyntheticStmt(After);
77
78 // If 'After' is in the subtree of the siblings that follow 'Before' in the
79 // chain of successors, we know that 'After' is sequenced after 'Before'.
80 for (const Stmt *Successor = getSequenceSuccessor(Before); Successor;
81 Successor = getSequenceSuccessor(Successor)) {
82 if (isDescendantOrEqual(After, Successor, Context))
83 return true;
84 }
85
86 // If 'After' is a parent of 'Before' or is sequenced after one of these
87 // parents, we know that it is sequenced after 'Before'.
88 for (const Stmt *Parent : getParentStmts(Before, Context)) {
89 if (Parent == After || inSequence(Parent, After))
90 return true;
91 }
92
93 return false;
94 }
95
96 bool ExprSequence::potentiallyAfter(const Stmt *After,
97 const Stmt *Before) const {
98 return !inSequence(After, Before);
99 }
100
101 const Stmt *ExprSequence::getSequenceSuccessor(const Stmt *S) const {
102 for (const Stmt *Parent : getParentStmts(S, Context)) {
103 // If a statement has multiple parents, make sure we're using the parent
104 // that lies within the sub-tree under Root.
105 if (!isDescendantOrEqual(Parent, Root, Context))
106 continue;
107
108 if (const auto *BO = dyn_cast<BinaryOperator>(Parent)) {
109 // Comma operator: Right-hand side is sequenced after the left-hand side.
110 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
111 return BO->getRHS();
112 } else if (const auto *InitList = dyn_cast<InitListExpr>(Parent)) {
113 // Initializer list: Each initializer clause is sequenced after the
114 // clauses that precede it.
115 for (unsigned I = 1; I < InitList->getNumInits(); ++I) {
116 if (InitList->getInit(I - 1) == S)
117 return InitList->getInit(I);
118 }
119 } else if (const auto *Compound = dyn_cast<CompoundStmt>(Parent)) {
120 // Compound statement: Each sub-statement is sequenced after the
121 // statements that precede it.
122 const Stmt *Previous = nullptr;
123 for (const auto *Child : Compound->body()) {
124 if (Previous == S)
125 return Child;
126 Previous = Child;
127 }
128 } else if (const auto *TheDeclStmt = dyn_cast<DeclStmt>(Parent)) {
129 // Declaration: Every initializer expression is sequenced after the
130 // initializer expressions that precede it.
131 const Expr *PreviousInit = nullptr;
132 for (const Decl *TheDecl : TheDeclStmt->decls()) {
133 if (const auto *TheVarDecl = dyn_cast<VarDecl>(TheDecl)) {
134 if (const Expr *Init = TheVarDecl->getInit()) {
135 if (PreviousInit == S)
136 return Init;
137 PreviousInit = Init;
138 }
139 }
140 }
141 } else if (const auto *ForRange = dyn_cast<CXXForRangeStmt>(Parent)) {
142 // Range-based for: Loop variable declaration is sequenced before the
143 // body. (We need this rule because these get placed in the same
144 // CFGBlock.)
145 if (S == ForRange->getLoopVarStmt())
146 return ForRange->getBody();
147 } else if (const auto *TheIfStmt = dyn_cast<IfStmt>(Parent)) {
148 // If statement:
149 // - Sequence init statement before variable declaration.
150 // - Sequence variable declaration (along with the expression used to
151 // initialize it) before the evaluation of the condition.
152 if (S == TheIfStmt->getInit())
153 return TheIfStmt->getConditionVariableDeclStmt();
154 if (S == TheIfStmt->getConditionVariableDeclStmt())
155 return TheIfStmt->getCond();
156 } else if (const auto *TheSwitchStmt = dyn_cast<SwitchStmt>(Parent)) {
157 // Ditto for switch statements.
158 if (S == TheSwitchStmt->getInit())
159 return TheSwitchStmt->getConditionVariableDeclStmt();
160 if (S == TheSwitchStmt->getConditionVariableDeclStmt())
161 return TheSwitchStmt->getCond();
162 } else if (const auto *TheWhileStmt = dyn_cast<WhileStmt>(Parent)) {
163 // While statement: Sequence variable declaration (along with the
164 // expression used to initialize it) before the evaluation of the
165 // condition.
166 if (S == TheWhileStmt->getConditionVariableDeclStmt())
167 return TheWhileStmt->getCond();
168 }
169 }
170
171 return nullptr;
172 }
173
174 const Stmt *ExprSequence::resolveSyntheticStmt(const Stmt *S) const {
175 if (SyntheticStmtSourceMap.count(S))
176 return SyntheticStmtSourceMap.lookup(S);
177 return S;
178 }
179
180 StmtToBlockMap::StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext)
181 : Context(TheContext) {
182 for (const auto *B : *TheCFG) {
183 for (const auto &Elem : *B) {
184 if (Optional<CFGStmt> S = Elem.getAs<CFGStmt>())
185 Map[S->getStmt()] = B;
186 }
187 }
188 }
189
190 const CFGBlock *StmtToBlockMap::blockContainingStmt(const Stmt *S) const {
191 while (!Map.count(S)) {
192 SmallVector<const Stmt *, 1> Parents = getParentStmts(S, Context);
193 if (Parents.empty())
194 return nullptr;
195 S = Parents[0];
196 }
197
198 return Map.lookup(S);
199 }
200
201 } // namespace utils
202 } // namespace tidy
203 } // 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:74
clang::tidy::utils::StmtToBlockMap::blockContainingStmt
const CFGBlock * blockContainingStmt(const Stmt *S) const
Returns the block that S is contained in.
Definition: ExprSequence.cpp:190
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:96
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang::tidy::utils::StmtToBlockMap::StmtToBlockMap
StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext)
Initializes the map for the given CFG.
Definition: ExprSequence.cpp:180
clang::tidy::utils::ExprSequence::ExprSequence
ExprSequence(const CFG *TheCFG, const Stmt *Root, ASTContext *TheContext)
Initializes this ExprSequence with sequence information for the given CFG.
Definition: ExprSequence.cpp:66
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::tidy::utils::getParentStmts
static SmallVector< const Stmt *, 1 > getParentStmts(const Stmt *S, ASTContext *Context)
Definition: ExprSequence.cpp:28
ExprSequence.h