1 //===---------- ExprSequence.cpp - clang-tidy -----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "ExprSequence.h"
10 #include "clang/AST/ParentMapContext.h"
11 #include "llvm/ADT/SmallVector.h"
14 namespace clang::tidy::utils
{
16 // Returns the Stmt nodes that are parents of 'S', skipping any potential
17 // intermediate non-Stmt nodes.
19 // In almost all cases, this function returns a single parent or no parents at
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
;
32 TraversalKindScope
RAII(*Context
, TK_AsIs
);
33 DynTypedNodeList Parents
= Context
->getParents(*S
);
35 SmallVector
<DynTypedNode
, 1> NodesToProcess(Parents
.begin(), Parents
.end());
37 while (!NodesToProcess
.empty()) {
38 DynTypedNode Node
= NodesToProcess
.back();
39 NodesToProcess
.pop_back();
41 if (const auto *S
= Node
.get
<Stmt
>()) {
44 Parents
= Context
->getParents(Node
);
45 NodesToProcess
.append(Parents
.begin(), Parents
.end());
54 bool isDescendantOrEqual(const Stmt
*Descendant
, const Stmt
*Ancestor
,
55 ASTContext
*Context
) {
56 if (Descendant
== Ancestor
)
58 return llvm::any_of(getParentStmts(Descendant
, Context
),
59 [Ancestor
, Context
](const Stmt
*Parent
) {
60 return isDescendantOrEqual(Parent
, Ancestor
, Context
);
64 bool isDescendantOfArgs(const Stmt
*Descendant
, const CallExpr
*Call
,
65 ASTContext
*Context
) {
66 return llvm::any_of(Call
->arguments(),
67 [Descendant
, Context
](const Expr
*Arg
) {
68 return isDescendantOrEqual(Descendant
, Arg
, Context
);
72 llvm::SmallVector
<const InitListExpr
*>
73 getAllInitListForms(const InitListExpr
*InitList
) {
74 llvm::SmallVector
<const InitListExpr
*> result
= {InitList
};
75 if (const InitListExpr
*AltForm
= InitList
->getSyntacticForm())
76 result
.push_back(AltForm
);
77 if (const InitListExpr
*AltForm
= InitList
->getSemanticForm())
78 result
.push_back(AltForm
);
84 ExprSequence::ExprSequence(const CFG
*TheCFG
, const Stmt
*Root
,
85 ASTContext
*TheContext
)
86 : Context(TheContext
), Root(Root
) {
87 for (const auto &SyntheticStmt
: TheCFG
->synthetic_stmts()) {
88 SyntheticStmtSourceMap
[SyntheticStmt
.first
] = SyntheticStmt
.second
;
92 bool ExprSequence::inSequence(const Stmt
*Before
, const Stmt
*After
) const {
93 Before
= resolveSyntheticStmt(Before
);
94 After
= resolveSyntheticStmt(After
);
96 // If 'After' is in the subtree of the siblings that follow 'Before' in the
97 // chain of successors, we know that 'After' is sequenced after 'Before'.
98 for (const Stmt
*Successor
= getSequenceSuccessor(Before
); Successor
;
99 Successor
= getSequenceSuccessor(Successor
)) {
100 if (isDescendantOrEqual(After
, Successor
, Context
))
104 SmallVector
<const Stmt
*, 1> BeforeParents
= getParentStmts(Before
, Context
);
106 // Since C++17, the callee of a call expression is guaranteed to be sequenced
107 // before all of the arguments.
108 // We handle this as a special case rather than using the general
109 // `getSequenceSuccessor` logic above because the callee expression doesn't
110 // have an unambiguous successor; the order in which arguments are evaluated
112 for (const Stmt
*Parent
: BeforeParents
) {
113 // Special case: If the callee is a `MemberExpr` with a `DeclRefExpr` as its
114 // base, we consider it to be sequenced _after_ the arguments. This is
115 // because the variable referenced in the base will only actually be
116 // accessed when the call happens, i.e. once all of the arguments have been
117 // evaluated. This has no basis in the C++ standard, but it reflects actual
118 // behavior that is relevant to a use-after-move scenario:
121 // a.bar(consumeA(std::move(a));
124 // In this example, we end up accessing `a` after it has been moved from,
125 // even though nominally the callee `a.bar` is evaluated before the argument
126 // `consumeA(std::move(a))`. Note that this is not specific to C++17, so
127 // we implement this logic unconditionally.
128 if (const auto *Call
= dyn_cast
<CXXMemberCallExpr
>(Parent
)) {
129 if (is_contained(Call
->arguments(), Before
) &&
131 Call
->getImplicitObjectArgument()->IgnoreParenImpCasts()) &&
132 isDescendantOrEqual(After
, Call
->getImplicitObjectArgument(),
136 // We need this additional early exit so that we don't fall through to the
137 // more general logic below.
138 if (const auto *Member
= dyn_cast
<MemberExpr
>(Before
);
139 Member
&& Call
->getCallee() == Member
&&
140 isa
<DeclRefExpr
>(Member
->getBase()->IgnoreParenImpCasts()) &&
141 isDescendantOfArgs(After
, Call
, Context
))
145 if (!Context
->getLangOpts().CPlusPlus17
)
148 if (const auto *Call
= dyn_cast
<CallExpr
>(Parent
);
149 Call
&& Call
->getCallee() == Before
&&
150 isDescendantOfArgs(After
, Call
, Context
))
154 // If 'After' is a parent of 'Before' or is sequenced after one of these
155 // parents, we know that it is sequenced after 'Before'.
156 for (const Stmt
*Parent
: BeforeParents
) {
157 if (Parent
== After
|| inSequence(Parent
, After
))
164 bool ExprSequence::potentiallyAfter(const Stmt
*After
,
165 const Stmt
*Before
) const {
166 return !inSequence(After
, Before
);
169 const Stmt
*ExprSequence::getSequenceSuccessor(const Stmt
*S
) const {
170 for (const Stmt
*Parent
: getParentStmts(S
, Context
)) {
171 // If a statement has multiple parents, make sure we're using the parent
172 // that lies within the sub-tree under Root.
173 if (!isDescendantOrEqual(Parent
, Root
, Context
))
176 if (const auto *BO
= dyn_cast
<BinaryOperator
>(Parent
)) {
177 // Comma operator: Right-hand side is sequenced after the left-hand side.
178 if (BO
->getLHS() == S
&& BO
->getOpcode() == BO_Comma
)
180 } else if (const auto *InitList
= dyn_cast
<InitListExpr
>(Parent
)) {
181 // Initializer list: Each initializer clause is sequenced after the
182 // clauses that precede it.
183 for (const InitListExpr
*Form
: getAllInitListForms(InitList
)) {
184 for (unsigned I
= 1; I
< Form
->getNumInits(); ++I
) {
185 if (Form
->getInit(I
- 1) == S
) {
186 return Form
->getInit(I
);
190 } else if (const auto *ConstructExpr
= dyn_cast
<CXXConstructExpr
>(Parent
)) {
191 // Constructor arguments are sequenced if the constructor call is written
192 // as list-initialization.
193 if (ConstructExpr
->isListInitialization()) {
194 for (unsigned I
= 1; I
< ConstructExpr
->getNumArgs(); ++I
) {
195 if (ConstructExpr
->getArg(I
- 1) == S
) {
196 return ConstructExpr
->getArg(I
);
200 } else if (const auto *Compound
= dyn_cast
<CompoundStmt
>(Parent
)) {
201 // Compound statement: Each sub-statement is sequenced after the
202 // statements that precede it.
203 const Stmt
*Previous
= nullptr;
204 for (const auto *Child
: Compound
->body()) {
209 } else if (const auto *TheDeclStmt
= dyn_cast
<DeclStmt
>(Parent
)) {
210 // Declaration: Every initializer expression is sequenced after the
211 // initializer expressions that precede it.
212 const Expr
*PreviousInit
= nullptr;
213 for (const Decl
*TheDecl
: TheDeclStmt
->decls()) {
214 if (const auto *TheVarDecl
= dyn_cast
<VarDecl
>(TheDecl
)) {
215 if (const Expr
*Init
= TheVarDecl
->getInit()) {
216 if (PreviousInit
== S
)
222 } else if (const auto *ForRange
= dyn_cast
<CXXForRangeStmt
>(Parent
)) {
223 // Range-based for: Loop variable declaration is sequenced before the
224 // body. (We need this rule because these get placed in the same
226 if (S
== ForRange
->getLoopVarStmt())
227 return ForRange
->getBody();
228 } else if (const auto *TheIfStmt
= dyn_cast
<IfStmt
>(Parent
)) {
230 // - Sequence init statement before variable declaration, if present;
231 // before condition evaluation, otherwise.
232 // - Sequence variable declaration (along with the expression used to
233 // initialize it) before the evaluation of the condition.
234 if (S
== TheIfStmt
->getInit()) {
235 if (TheIfStmt
->getConditionVariableDeclStmt() != nullptr)
236 return TheIfStmt
->getConditionVariableDeclStmt();
237 return TheIfStmt
->getCond();
239 if (S
== TheIfStmt
->getConditionVariableDeclStmt())
240 return TheIfStmt
->getCond();
241 } else if (const auto *TheSwitchStmt
= dyn_cast
<SwitchStmt
>(Parent
)) {
242 // Ditto for switch statements.
243 if (S
== TheSwitchStmt
->getInit()) {
244 if (TheSwitchStmt
->getConditionVariableDeclStmt() != nullptr)
245 return TheSwitchStmt
->getConditionVariableDeclStmt();
246 return TheSwitchStmt
->getCond();
248 if (S
== TheSwitchStmt
->getConditionVariableDeclStmt())
249 return TheSwitchStmt
->getCond();
250 } else if (const auto *TheWhileStmt
= dyn_cast
<WhileStmt
>(Parent
)) {
251 // While statement: Sequence variable declaration (along with the
252 // expression used to initialize it) before the evaluation of the
254 if (S
== TheWhileStmt
->getConditionVariableDeclStmt())
255 return TheWhileStmt
->getCond();
262 const Stmt
*ExprSequence::resolveSyntheticStmt(const Stmt
*S
) const {
263 if (SyntheticStmtSourceMap
.count(S
))
264 return SyntheticStmtSourceMap
.lookup(S
);
268 StmtToBlockMap::StmtToBlockMap(const CFG
*TheCFG
, ASTContext
*TheContext
)
269 : Context(TheContext
) {
270 for (const auto *B
: *TheCFG
) {
271 for (const auto &Elem
: *B
) {
272 if (std::optional
<CFGStmt
> S
= Elem
.getAs
<CFGStmt
>())
273 Map
[S
->getStmt()] = B
;
278 const CFGBlock
*StmtToBlockMap::blockContainingStmt(const Stmt
*S
) const {
279 while (!Map
.count(S
)) {
280 SmallVector
<const Stmt
*, 1> Parents
= getParentStmts(S
, Context
);
286 return Map
.lookup(S
);
289 } // namespace clang::tidy::utils