10#include "clang/AST/ParentMapContext.h"
11#include "llvm/ADT/SmallVector.h"
29 ASTContext *Context) {
30 SmallVector<const Stmt *, 1> Result;
32 const 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 const 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 const SmallVector<const Stmt *, 1> BeforeParents =
107 for (
const Stmt *Parent : BeforeParents) {
123 if (
const auto *Call = dyn_cast<CXXMemberCallExpr>(Parent)) {
124 if (is_contained(Call->arguments(), Before) &&
126 Call->getImplicitObjectArgument()->IgnoreParenImpCasts()) &&
133 if (
const auto *Member = dyn_cast<MemberExpr>(Before);
134 Member && Call->getCallee() == Member &&
135 isa<DeclRefExpr>(Member->getBase()->IgnoreParenImpCasts()) &&
140 if (!Context->getLangOpts().CPlusPlus17)
143 if (
const auto *Call = dyn_cast<CallExpr>(Parent);
144 Call && Call->getCallee() == Before &&
151 for (
const Stmt *Parent : BeforeParents) {
152 if (Parent == After ||
inSequence(Parent, After))
160 const Stmt *Before)
const {
164const Stmt *ExprSequence::getSequenceSuccessor(
const Stmt *S)
const {
171 if (
const auto *BO = dyn_cast<BinaryOperator>(Parent)) {
173 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
175 }
else if (
const auto *InitList = dyn_cast<InitListExpr>(Parent)) {
179 for (
unsigned I = 1; I < Form->getNumInits(); ++I) {
180 if (Form->getInit(I - 1) == S) {
181 return Form->getInit(I);
185 }
else if (
const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(Parent)) {
188 if (ConstructExpr->isListInitialization()) {
189 for (
unsigned I = 1; I < ConstructExpr->getNumArgs(); ++I) {
190 if (ConstructExpr->getArg(I - 1) == S) {
191 return ConstructExpr->getArg(I);
195 }
else if (
const auto *Compound = dyn_cast<CompoundStmt>(Parent)) {
198 const Stmt *Previous =
nullptr;
199 for (
const auto *Child : Compound->body()) {
204 }
else if (
const auto *TheDeclStmt = dyn_cast<DeclStmt>(Parent)) {
207 const Expr *PreviousInit =
nullptr;
208 for (
const Decl *TheDecl : TheDeclStmt->decls()) {
209 if (
const auto *TheVarDecl = dyn_cast<VarDecl>(TheDecl)) {
210 if (
const Expr *Init = TheVarDecl->getInit()) {
211 if (PreviousInit == S)
217 }
else if (
const auto *ForRange = dyn_cast<CXXForRangeStmt>(Parent)) {
221 if (S == ForRange->getLoopVarStmt())
222 return ForRange->getBody();
223 }
else if (
const auto *TheIfStmt = dyn_cast<IfStmt>(Parent)) {
229 if (S == TheIfStmt->getInit()) {
230 if (TheIfStmt->getConditionVariableDeclStmt() !=
nullptr)
231 return TheIfStmt->getConditionVariableDeclStmt();
232 return TheIfStmt->getCond();
234 if (S == TheIfStmt->getConditionVariableDeclStmt())
235 return TheIfStmt->getCond();
236 }
else if (
const auto *TheSwitchStmt = dyn_cast<SwitchStmt>(Parent)) {
238 if (S == TheSwitchStmt->getInit()) {
239 if (TheSwitchStmt->getConditionVariableDeclStmt() !=
nullptr)
240 return TheSwitchStmt->getConditionVariableDeclStmt();
241 return TheSwitchStmt->getCond();
243 if (S == TheSwitchStmt->getConditionVariableDeclStmt())
244 return TheSwitchStmt->getCond();
245 }
else if (
const auto *TheWhileStmt = dyn_cast<WhileStmt>(Parent)) {
249 if (S == TheWhileStmt->getConditionVariableDeclStmt())
250 return TheWhileStmt->getCond();
257const Stmt *ExprSequence::resolveSyntheticStmt(
const Stmt *S)
const {
258 if (SyntheticStmtSourceMap.contains(S))
259 return SyntheticStmtSourceMap.lookup(S);
264 : Context(TheContext) {
265 for (
const auto *B : *TheCFG) {
266 for (
const auto &Elem : *B) {
267 if (std::optional<CFGStmt> S = Elem.getAs<CFGStmt>())
268 Map[S->getStmt()] = B;
274 while (!Map.contains(S)) {
275 SmallVector<const Stmt *, 1> Parents =
getParentStmts(S, Context);
281 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)