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)
87 for (
const auto &SyntheticStmt : TheCFG->synthetic_stmts()) {
88 SyntheticStmtSourceMap[SyntheticStmt.first] = SyntheticStmt.second;
93 Before = resolveSyntheticStmt(Before);
94 After = resolveSyntheticStmt(After);
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);
112 for (
const Stmt *
Parent : BeforeParents) {
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(),
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))
156 for (
const Stmt *
Parent : BeforeParents) {
165 const Stmt *Before)
const {
169const Stmt *ExprSequence::getSequenceSuccessor(
const Stmt *S)
const {
173 if (!isDescendantOrEqual(
Parent, Root, Context))
176 if (
const auto *BO = dyn_cast<BinaryOperator>(
Parent)) {
178 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
180 }
else if (
const auto *InitList = dyn_cast<InitListExpr>(
Parent)) {
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)) {
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)) {
203 const Stmt *Previous =
nullptr;
204 for (
const auto *Child : Compound->body()) {
209 }
else if (
const auto *TheDeclStmt = dyn_cast<DeclStmt>(
Parent)) {
212 const Expr *PreviousInit =
nullptr;
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)) {
226 if (S == ForRange->getLoopVarStmt())
227 return ForRange->getBody();
228 }
else if (
const auto *TheIfStmt = dyn_cast<IfStmt>(
Parent)) {
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)) {
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)) {
254 if (S == TheWhileStmt->getConditionVariableDeclStmt())
255 return TheWhileStmt->getCond();
262const Stmt *ExprSequence::resolveSyntheticStmt(
const Stmt *S)
const {
263 if (SyntheticStmtSourceMap.count(S))
264 return SyntheticStmtSourceMap.lookup(S);
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;
279 while (!Map.count(S)) {
280 SmallVector<const Stmt *, 1> Parents =
getParentStmts(S, Context);
286 return Map.lookup(S);
const FunctionDecl * Decl
::clang::DynTypedNode Node
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)