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 return llvm::any_of(BeforeParents, [&](
const Stmt *Parent) {
152 return Parent == After ||
inSequence(Parent, After);
157 const Stmt *Before)
const {
161const Stmt *ExprSequence::getSequenceSuccessor(
const Stmt *S)
const {
168 if (
const auto *BO = dyn_cast<BinaryOperator>(Parent)) {
170 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
172 }
else if (
const auto *InitList = dyn_cast<InitListExpr>(Parent)) {
176 for (
unsigned I = 1; I < Form->getNumInits(); ++I) {
177 if (Form->getInit(I - 1) == S) {
178 return Form->getInit(I);
182 }
else if (
const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(Parent)) {
185 if (ConstructExpr->isListInitialization()) {
186 for (
unsigned I = 1; I < ConstructExpr->getNumArgs(); ++I) {
187 if (ConstructExpr->getArg(I - 1) == S) {
188 return ConstructExpr->getArg(I);
192 }
else if (
const auto *Compound = dyn_cast<CompoundStmt>(Parent)) {
195 const Stmt *Previous =
nullptr;
196 for (
const auto *Child : Compound->body()) {
201 }
else if (
const auto *TheDeclStmt = dyn_cast<DeclStmt>(Parent)) {
204 const Expr *PreviousInit =
nullptr;
205 for (
const Decl *TheDecl : TheDeclStmt->decls()) {
206 if (
const auto *TheVarDecl = dyn_cast<VarDecl>(TheDecl)) {
207 if (
const Expr *Init = TheVarDecl->getInit()) {
208 if (PreviousInit == S)
214 }
else if (
const auto *ForRange = dyn_cast<CXXForRangeStmt>(Parent)) {
218 if (S == ForRange->getLoopVarStmt())
219 return ForRange->getBody();
220 }
else if (
const auto *TheIfStmt = dyn_cast<IfStmt>(Parent)) {
226 if (S == TheIfStmt->getInit()) {
227 if (TheIfStmt->getConditionVariableDeclStmt() !=
nullptr)
228 return TheIfStmt->getConditionVariableDeclStmt();
229 return TheIfStmt->getCond();
231 if (S == TheIfStmt->getConditionVariableDeclStmt())
232 return TheIfStmt->getCond();
233 }
else if (
const auto *TheSwitchStmt = dyn_cast<SwitchStmt>(Parent)) {
235 if (S == TheSwitchStmt->getInit()) {
236 if (TheSwitchStmt->getConditionVariableDeclStmt() !=
nullptr)
237 return TheSwitchStmt->getConditionVariableDeclStmt();
238 return TheSwitchStmt->getCond();
240 if (S == TheSwitchStmt->getConditionVariableDeclStmt())
241 return TheSwitchStmt->getCond();
242 }
else if (
const auto *TheWhileStmt = dyn_cast<WhileStmt>(Parent)) {
246 if (S == TheWhileStmt->getConditionVariableDeclStmt())
247 return TheWhileStmt->getCond();
254const Stmt *ExprSequence::resolveSyntheticStmt(
const Stmt *S)
const {
255 if (SyntheticStmtSourceMap.contains(S))
256 return SyntheticStmtSourceMap.lookup(S);
261 : Context(TheContext) {
262 for (
const auto *B : *TheCFG) {
263 for (
const auto &Elem : *B) {
264 if (std::optional<CFGStmt> S = Elem.getAs<CFGStmt>())
265 Map[S->getStmt()] = B;
271 while (!Map.contains(S)) {
272 SmallVector<const Stmt *, 1> Parents =
getParentStmts(S, Context);
278 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)