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());
53 ASTContext *Context) {
54 if (Descendant == Ancestor)
57 [Ancestor, Context](
const Stmt *Parent) {
63 ASTContext *Context) {
64 return llvm::any_of(Call->arguments(),
65 [Descendant, Context](
const Expr *Arg) {
66 return isDescendantOrEqual(Descendant, Arg, Context);
70static llvm::SmallVector<const InitListExpr *>
72 llvm::SmallVector<const InitListExpr *> Result = {InitList};
73 if (
const InitListExpr *AltForm = InitList->getSyntacticForm())
74 Result.push_back(AltForm);
75 if (
const InitListExpr *AltForm = InitList->getSemanticForm())
76 Result.push_back(AltForm);
81 ASTContext *TheContext)
82 : Context(TheContext), Root(Root) {
83 SyntheticStmtSourceMap.insert_range(TheCFG->synthetic_stmts());
87 Before = resolveSyntheticStmt(Before);
88 After = resolveSyntheticStmt(After);
92 for (
const Stmt *Successor = getSequenceSuccessor(Before); Successor;
93 Successor = getSequenceSuccessor(Successor)) {
98 SmallVector<const Stmt *, 1> BeforeParents =
getParentStmts(Before, Context);
106 for (
const Stmt *Parent : BeforeParents) {
122 if (
const auto *Call = dyn_cast<CXXMemberCallExpr>(Parent)) {
123 if (is_contained(Call->arguments(), Before) &&
125 Call->getImplicitObjectArgument()->IgnoreParenImpCasts()) &&
132 if (
const auto *Member = dyn_cast<MemberExpr>(Before);
133 Member && Call->getCallee() == Member &&
134 isa<DeclRefExpr>(Member->getBase()->IgnoreParenImpCasts()) &&
139 if (!Context->getLangOpts().CPlusPlus17)
142 if (
const auto *Call = dyn_cast<CallExpr>(Parent);
143 Call && Call->getCallee() == Before &&
150 for (
const Stmt *Parent : BeforeParents) {
151 if (Parent == After ||
inSequence(Parent, After))
159 const Stmt *Before)
const {
163const Stmt *ExprSequence::getSequenceSuccessor(
const Stmt *S)
const {
170 if (
const auto *BO = dyn_cast<BinaryOperator>(Parent)) {
172 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
174 }
else if (
const auto *InitList = dyn_cast<InitListExpr>(Parent)) {
178 for (
unsigned I = 1; I < Form->getNumInits(); ++I) {
179 if (Form->getInit(I - 1) == S) {
180 return Form->getInit(I);
184 }
else if (
const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(Parent)) {
187 if (ConstructExpr->isListInitialization()) {
188 for (
unsigned I = 1; I < ConstructExpr->getNumArgs(); ++I) {
189 if (ConstructExpr->getArg(I - 1) == S) {
190 return ConstructExpr->getArg(I);
194 }
else if (
const auto *Compound = dyn_cast<CompoundStmt>(Parent)) {
197 const Stmt *Previous =
nullptr;
198 for (
const auto *Child : Compound->body()) {
203 }
else if (
const auto *TheDeclStmt = dyn_cast<DeclStmt>(Parent)) {
206 const Expr *PreviousInit =
nullptr;
207 for (
const Decl *TheDecl : TheDeclStmt->decls()) {
208 if (
const auto *TheVarDecl = dyn_cast<VarDecl>(TheDecl)) {
209 if (
const Expr *Init = TheVarDecl->getInit()) {
210 if (PreviousInit == S)
216 }
else if (
const auto *ForRange = dyn_cast<CXXForRangeStmt>(Parent)) {
220 if (S == ForRange->getLoopVarStmt())
221 return ForRange->getBody();
222 }
else if (
const auto *TheIfStmt = dyn_cast<IfStmt>(Parent)) {
228 if (S == TheIfStmt->getInit()) {
229 if (TheIfStmt->getConditionVariableDeclStmt() !=
nullptr)
230 return TheIfStmt->getConditionVariableDeclStmt();
231 return TheIfStmt->getCond();
233 if (S == TheIfStmt->getConditionVariableDeclStmt())
234 return TheIfStmt->getCond();
235 }
else if (
const auto *TheSwitchStmt = dyn_cast<SwitchStmt>(Parent)) {
237 if (S == TheSwitchStmt->getInit()) {
238 if (TheSwitchStmt->getConditionVariableDeclStmt() !=
nullptr)
239 return TheSwitchStmt->getConditionVariableDeclStmt();
240 return TheSwitchStmt->getCond();
242 if (S == TheSwitchStmt->getConditionVariableDeclStmt())
243 return TheSwitchStmt->getCond();
244 }
else if (
const auto *TheWhileStmt = dyn_cast<WhileStmt>(Parent)) {
248 if (S == TheWhileStmt->getConditionVariableDeclStmt())
249 return TheWhileStmt->getCond();
256const Stmt *ExprSequence::resolveSyntheticStmt(
const Stmt *S)
const {
257 if (SyntheticStmtSourceMap.contains(S))
258 return SyntheticStmtSourceMap.lookup(S);
263 : Context(TheContext) {
264 for (
const auto *B : *TheCFG) {
265 for (
const auto &Elem : *B) {
266 if (std::optional<CFGStmt> S = Elem.getAs<CFGStmt>())
267 Map[S->getStmt()] = B;
273 while (!Map.contains(S)) {
274 SmallVector<const Stmt *, 1> Parents =
getParentStmts(S, Context);
280 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 bool isDescendantOrEqual(const Stmt *Descendant, const Stmt *Ancestor, ASTContext *Context)
static bool isDescendantOfArgs(const Stmt *Descendant, const CallExpr *Call, ASTContext *Context)
static SmallVector< const Stmt *, 1 > getParentStmts(const Stmt *S, ASTContext *Context)
static llvm::SmallVector< const InitListExpr * > getAllInitListForms(const InitListExpr *InitList)