10#include "clang/AST/ParentMapContext.h"
11#include "llvm/ADT/SmallVector.h"
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());
54bool isDescendantOrEqual(
const Stmt *Descendant,
const Stmt *Ancestor,
55 ASTContext *Context) {
56 if (Descendant == Ancestor)
59 [Ancestor, Context](
const Stmt *Parent) {
60 return isDescendantOrEqual(Parent, Ancestor, Context);
64bool 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);
72llvm::SmallVector<const InitListExpr *>
73getAllInitListForms(
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);
85 ASTContext *TheContext)
86 : Context(TheContext), Root(Root) {
87 SyntheticStmtSourceMap.insert_range(TheCFG->synthetic_stmts());
91 Before = resolveSyntheticStmt(Before);
92 After = resolveSyntheticStmt(After);
96 for (
const Stmt *Successor = getSequenceSuccessor(Before); Successor;
97 Successor = getSequenceSuccessor(Successor)) {
98 if (isDescendantOrEqual(After, Successor, Context))
102 SmallVector<const Stmt *, 1> BeforeParents =
getParentStmts(Before, Context);
110 for (
const Stmt *Parent : BeforeParents) {
126 if (
const auto *Call = dyn_cast<CXXMemberCallExpr>(Parent)) {
127 if (is_contained(Call->arguments(), Before) &&
129 Call->getImplicitObjectArgument()->IgnoreParenImpCasts()) &&
130 isDescendantOrEqual(After, Call->getImplicitObjectArgument(),
136 if (
const auto *Member = dyn_cast<MemberExpr>(Before);
137 Member && Call->getCallee() == Member &&
138 isa<DeclRefExpr>(Member->getBase()->IgnoreParenImpCasts()) &&
139 isDescendantOfArgs(After, Call, Context))
143 if (!Context->getLangOpts().CPlusPlus17)
146 if (
const auto *Call = dyn_cast<CallExpr>(Parent);
147 Call && Call->getCallee() == Before &&
148 isDescendantOfArgs(After, Call, Context))
154 for (
const Stmt *Parent : BeforeParents) {
155 if (Parent == After ||
inSequence(Parent, After))
163 const Stmt *Before)
const {
167const Stmt *ExprSequence::getSequenceSuccessor(
const Stmt *S)
const {
171 if (!isDescendantOrEqual(Parent, Root, Context))
174 if (
const auto *BO = dyn_cast<BinaryOperator>(Parent)) {
176 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
178 }
else if (
const auto *InitList = dyn_cast<InitListExpr>(Parent)) {
181 for (
const InitListExpr *Form : getAllInitListForms(InitList)) {
182 for (
unsigned I = 1; I < Form->getNumInits(); ++I) {
183 if (Form->getInit(I - 1) == S) {
184 return Form->getInit(I);
188 }
else if (
const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(Parent)) {
191 if (ConstructExpr->isListInitialization()) {
192 for (
unsigned I = 1; I < ConstructExpr->getNumArgs(); ++I) {
193 if (ConstructExpr->getArg(I - 1) == S) {
194 return ConstructExpr->getArg(I);
198 }
else if (
const auto *Compound = dyn_cast<CompoundStmt>(Parent)) {
201 const Stmt *Previous =
nullptr;
202 for (
const auto *Child : Compound->body()) {
207 }
else if (
const auto *TheDeclStmt = dyn_cast<DeclStmt>(Parent)) {
210 const Expr *PreviousInit =
nullptr;
211 for (
const Decl *TheDecl : TheDeclStmt->decls()) {
212 if (
const auto *TheVarDecl = dyn_cast<VarDecl>(TheDecl)) {
213 if (
const Expr *Init = TheVarDecl->getInit()) {
214 if (PreviousInit == S)
220 }
else if (
const auto *ForRange = dyn_cast<CXXForRangeStmt>(Parent)) {
224 if (S == ForRange->getLoopVarStmt())
225 return ForRange->getBody();
226 }
else if (
const auto *TheIfStmt = dyn_cast<IfStmt>(Parent)) {
232 if (S == TheIfStmt->getInit()) {
233 if (TheIfStmt->getConditionVariableDeclStmt() !=
nullptr)
234 return TheIfStmt->getConditionVariableDeclStmt();
235 return TheIfStmt->getCond();
237 if (S == TheIfStmt->getConditionVariableDeclStmt())
238 return TheIfStmt->getCond();
239 }
else if (
const auto *TheSwitchStmt = dyn_cast<SwitchStmt>(Parent)) {
241 if (S == TheSwitchStmt->getInit()) {
242 if (TheSwitchStmt->getConditionVariableDeclStmt() !=
nullptr)
243 return TheSwitchStmt->getConditionVariableDeclStmt();
244 return TheSwitchStmt->getCond();
246 if (S == TheSwitchStmt->getConditionVariableDeclStmt())
247 return TheSwitchStmt->getCond();
248 }
else if (
const auto *TheWhileStmt = dyn_cast<WhileStmt>(Parent)) {
252 if (S == TheWhileStmt->getConditionVariableDeclStmt())
253 return TheWhileStmt->getCond();
260const Stmt *ExprSequence::resolveSyntheticStmt(
const Stmt *S)
const {
261 if (SyntheticStmtSourceMap.contains(S))
262 return SyntheticStmtSourceMap.lookup(S);
267 : Context(TheContext) {
268 for (
const auto *B : *TheCFG) {
269 for (
const auto &Elem : *B) {
270 if (std::optional<CFGStmt> S = Elem.getAs<CFGStmt>())
271 Map[S->getStmt()] = B;
277 while (!Map.contains(S)) {
278 SmallVector<const Stmt *, 1> Parents =
getParentStmts(S, Context);
284 return Map.lookup(S);
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.
bool inSequence(const Stmt *Before, const Stmt *After) const
Returns whether Before is sequenced before After.
StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext)
Initializes the map for the given CFG.
const CFGBlock * blockContainingStmt(const Stmt *S) const
Returns the block that S is contained in.
static SmallVector< const Stmt *, 1 > getParentStmts(const Stmt *S, ASTContext *Context)