10#include "clang/AST/ParentMapContext.h"
28 ASTContext *Context) {
29 SmallVector<const Stmt *, 1> Result;
31 TraversalKindScope RAII(*Context, TK_AsIs);
32 DynTypedNodeList Parents = Context->getParents(*S);
34 SmallVector<DynTypedNode, 1> NodesToProcess(Parents.begin(), Parents.end());
36 while (!NodesToProcess.empty()) {
37 DynTypedNode Node = NodesToProcess.back();
38 NodesToProcess.pop_back();
40 if (
const auto *S = Node.get<Stmt>()) {
43 Parents = Context->getParents(Node);
44 NodesToProcess.append(Parents.begin(), Parents.end());
52bool isDescendantOrEqual(
const Stmt *Descendant,
const Stmt *Ancestor,
53 ASTContext *Context) {
54 if (Descendant == Ancestor)
57 if (isDescendantOrEqual(
Parent, Ancestor, Context))
66 ASTContext *TheContext)
68 for (
const auto &SyntheticStmt : TheCFG->synthetic_stmts()) {
69 SyntheticStmtSourceMap[SyntheticStmt.first] = SyntheticStmt.second;
74 Before = resolveSyntheticStmt(Before);
75 After = resolveSyntheticStmt(After);
79 for (
const Stmt *Successor = getSequenceSuccessor(Before); Successor;
80 Successor = getSequenceSuccessor(Successor)) {
81 if (isDescendantOrEqual(After, Successor, Context))
96 const Stmt *Before)
const {
100const Stmt *ExprSequence::getSequenceSuccessor(
const Stmt *S)
const {
104 if (!isDescendantOrEqual(
Parent, Root, Context))
107 if (
const auto *BO = dyn_cast<BinaryOperator>(
Parent)) {
109 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
111 }
else if (
const auto *InitList = dyn_cast<InitListExpr>(
Parent)) {
114 for (
unsigned I = 1; I < InitList->getNumInits(); ++I) {
115 if (InitList->getInit(I - 1) == S)
116 return InitList->getInit(I);
118 }
else if (
const auto *Compound = dyn_cast<CompoundStmt>(
Parent)) {
121 const Stmt *Previous =
nullptr;
122 for (
const auto *Child : Compound->body()) {
127 }
else if (
const auto *TheDeclStmt = dyn_cast<DeclStmt>(
Parent)) {
130 const Expr *PreviousInit =
nullptr;
132 if (
const auto *TheVarDecl = dyn_cast<VarDecl>(
TheDecl)) {
133 if (
const Expr *Init = TheVarDecl->getInit()) {
134 if (PreviousInit == S)
140 }
else if (
const auto *ForRange = dyn_cast<CXXForRangeStmt>(
Parent)) {
144 if (S == ForRange->getLoopVarStmt())
145 return ForRange->getBody();
146 }
else if (
const auto *TheIfStmt = dyn_cast<IfStmt>(
Parent)) {
152 if (S == TheIfStmt->getInit()) {
153 if (TheIfStmt->getConditionVariableDeclStmt() !=
nullptr)
154 return TheIfStmt->getConditionVariableDeclStmt();
155 return TheIfStmt->getCond();
157 if (S == TheIfStmt->getConditionVariableDeclStmt())
158 return TheIfStmt->getCond();
159 }
else if (
const auto *TheSwitchStmt = dyn_cast<SwitchStmt>(
Parent)) {
161 if (S == TheSwitchStmt->getInit()) {
162 if (TheSwitchStmt->getConditionVariableDeclStmt() !=
nullptr)
163 return TheSwitchStmt->getConditionVariableDeclStmt();
164 return TheSwitchStmt->getCond();
166 if (S == TheSwitchStmt->getConditionVariableDeclStmt())
167 return TheSwitchStmt->getCond();
168 }
else if (
const auto *TheWhileStmt = dyn_cast<WhileStmt>(
Parent)) {
172 if (S == TheWhileStmt->getConditionVariableDeclStmt())
173 return TheWhileStmt->getCond();
180const Stmt *ExprSequence::resolveSyntheticStmt(
const Stmt *S)
const {
181 if (SyntheticStmtSourceMap.count(S))
182 return SyntheticStmtSourceMap.lookup(S);
187 : Context(TheContext) {
188 for (
const auto *B : *TheCFG) {
189 for (
const auto &Elem : *B) {
190 if (std::optional<CFGStmt> S = Elem.getAs<CFGStmt>())
191 Map[S->getStmt()] = B;
197 while (!Map.count(S)) {
198 SmallVector<const Stmt *, 1> Parents =
getParentStmts(S, Context);
204 return Map.lookup(S);
const FunctionDecl * Decl
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)