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 if (isDescendantOrEqual(
Parent, Ancestor, Context))
66llvm::SmallVector<const InitListExpr *>
67getAllInitListForms(
const InitListExpr *InitList) {
68 llvm::SmallVector<const InitListExpr *> result = {InitList};
69 if (
const InitListExpr *AltForm = InitList->getSyntacticForm())
70 result.push_back(AltForm);
71 if (
const InitListExpr *AltForm = InitList->getSemanticForm())
72 result.push_back(AltForm);
79 ASTContext *TheContext)
81 for (
const auto &SyntheticStmt : TheCFG->synthetic_stmts()) {
82 SyntheticStmtSourceMap[SyntheticStmt.first] = SyntheticStmt.second;
87 Before = resolveSyntheticStmt(Before);
88 After = resolveSyntheticStmt(After);
92 for (
const Stmt *Successor = getSequenceSuccessor(Before); Successor;
93 Successor = getSequenceSuccessor(Successor)) {
94 if (isDescendantOrEqual(After, Successor, Context))
109 const Stmt *Before)
const {
113const Stmt *ExprSequence::getSequenceSuccessor(
const Stmt *S)
const {
117 if (!isDescendantOrEqual(
Parent, Root, Context))
120 if (
const auto *BO = dyn_cast<BinaryOperator>(
Parent)) {
122 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
124 }
else if (
const auto *InitList = dyn_cast<InitListExpr>(
Parent)) {
127 for (
const InitListExpr *Form : getAllInitListForms(InitList)) {
128 for (
unsigned I = 1; I < Form->getNumInits(); ++I) {
129 if (Form->getInit(I - 1) == S) {
130 return Form->getInit(I);
134 }
else if (
const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(
Parent)) {
137 if (ConstructExpr->isListInitialization()) {
138 for (
unsigned I = 1; I < ConstructExpr->getNumArgs(); ++I) {
139 if (ConstructExpr->getArg(I - 1) == S) {
140 return ConstructExpr->getArg(I);
144 }
else if (
const auto *Compound = dyn_cast<CompoundStmt>(
Parent)) {
147 const Stmt *Previous =
nullptr;
148 for (
const auto *Child : Compound->body()) {
153 }
else if (
const auto *TheDeclStmt = dyn_cast<DeclStmt>(
Parent)) {
156 const Expr *PreviousInit =
nullptr;
158 if (
const auto *TheVarDecl = dyn_cast<VarDecl>(
TheDecl)) {
159 if (
const Expr *Init = TheVarDecl->getInit()) {
160 if (PreviousInit == S)
166 }
else if (
const auto *ForRange = dyn_cast<CXXForRangeStmt>(
Parent)) {
170 if (S == ForRange->getLoopVarStmt())
171 return ForRange->getBody();
172 }
else if (
const auto *TheIfStmt = dyn_cast<IfStmt>(
Parent)) {
178 if (S == TheIfStmt->getInit()) {
179 if (TheIfStmt->getConditionVariableDeclStmt() !=
nullptr)
180 return TheIfStmt->getConditionVariableDeclStmt();
181 return TheIfStmt->getCond();
183 if (S == TheIfStmt->getConditionVariableDeclStmt())
184 return TheIfStmt->getCond();
185 }
else if (
const auto *TheSwitchStmt = dyn_cast<SwitchStmt>(
Parent)) {
187 if (S == TheSwitchStmt->getInit()) {
188 if (TheSwitchStmt->getConditionVariableDeclStmt() !=
nullptr)
189 return TheSwitchStmt->getConditionVariableDeclStmt();
190 return TheSwitchStmt->getCond();
192 if (S == TheSwitchStmt->getConditionVariableDeclStmt())
193 return TheSwitchStmt->getCond();
194 }
else if (
const auto *TheWhileStmt = dyn_cast<WhileStmt>(
Parent)) {
198 if (S == TheWhileStmt->getConditionVariableDeclStmt())
199 return TheWhileStmt->getCond();
206const Stmt *ExprSequence::resolveSyntheticStmt(
const Stmt *S)
const {
207 if (SyntheticStmtSourceMap.count(S))
208 return SyntheticStmtSourceMap.lookup(S);
213 : Context(TheContext) {
214 for (
const auto *B : *TheCFG) {
215 for (
const auto &Elem : *B) {
216 if (std::optional<CFGStmt> S = Elem.getAs<CFGStmt>())
217 Map[S->getStmt()] = B;
223 while (!Map.count(S)) {
224 SmallVector<const Stmt *, 1> Parents =
getParentStmts(S, Context);
230 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)