30 SmallVector<const Stmt *, 1>
Result;
32 ASTContext::DynTypedNodeList Parents = Context->getParents(*S);
34 SmallVector<ast_type_traits::DynTypedNode, 1> NodesToProcess(Parents.begin(),
37 while (!NodesToProcess.empty()) {
38 ast_type_traits::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 bool isDescendantOrEqual(
const Stmt *Descendant,
const Stmt *Ancestor,
55 if (Descendant == Ancestor)
58 if (isDescendantOrEqual(Parent, Ancestor, Context))
67 : Context(TheContext) {
68 for (
const auto &SyntheticStmt : TheCFG->synthetic_stmts()) {
69 SyntheticStmtSourceMap[SyntheticStmt.first] = SyntheticStmt.second;
74 Before = resolveSyntheticStmt(Before);
75 After = resolveSyntheticStmt(After);
79 for (
const Stmt *Successor = getSequenceSuccessor(Before); Successor;
80 Successor = getSequenceSuccessor(Successor)) {
81 if (isDescendantOrEqual(After, Successor, Context))
88 if (Parent == After ||
inSequence(Parent, After))
96 const Stmt *Before)
const {
100 const Stmt *ExprSequence::getSequenceSuccessor(
const Stmt *S)
const {
102 if (
const auto *BO = dyn_cast<BinaryOperator>(Parent)) {
104 if (BO->getLHS() == S && BO->getOpcode() == BO_Comma)
106 }
else if (
const auto *InitList = dyn_cast<InitListExpr>(Parent)) {
109 for (
unsigned I = 1; I < InitList->getNumInits(); ++I) {
110 if (InitList->getInit(I - 1) == S)
111 return InitList->getInit(I);
113 }
else if (
const auto *Compound = dyn_cast<CompoundStmt>(Parent)) {
116 const Stmt *Previous =
nullptr;
117 for (
const auto *Child : Compound->body()) {
122 }
else if (
const auto *TheDeclStmt = dyn_cast<DeclStmt>(Parent)) {
125 const Expr *PreviousInit =
nullptr;
126 for (
const Decl *TheDecl : TheDeclStmt->decls()) {
127 if (
const auto *TheVarDecl = dyn_cast<VarDecl>(TheDecl)) {
128 if (
const Expr *Init = TheVarDecl->getInit()) {
129 if (PreviousInit == S)
135 }
else if (
const auto *ForRange = dyn_cast<CXXForRangeStmt>(Parent)) {
139 if (S == ForRange->getLoopVarStmt())
140 return ForRange->getBody();
141 }
else if (
const auto *TheIfStmt = dyn_cast<IfStmt>(Parent)) {
145 if (S == TheIfStmt->getConditionVariableDeclStmt())
146 return TheIfStmt->getCond();
153 const Stmt *ExprSequence::resolveSyntheticStmt(
const Stmt *S)
const {
154 if (SyntheticStmtSourceMap.count(S))
155 return SyntheticStmtSourceMap.lookup(S);
160 : Context(TheContext) {
161 for (
const auto *B : *TheCFG) {
162 for (
const auto &Elem : *B) {
163 if (Optional<CFGStmt> S = Elem.getAs<CFGStmt>())
164 Map[S->getStmt()] = B;
170 while (!Map.count(S)) {
171 SmallVector<const Stmt *, 1> Parents =
getParentStmts(S, Context);
177 return Map.lookup(S);
const CFGBlock * blockContainingStmt(const Stmt *S) const
Returns the block that S is contained in.
StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext)
Initializes the map for the given CFG.
ExprSequence(const CFG *TheCFG, ASTContext *TheContext)
Initializes this ExprSequence with sequence information for the given CFG.
ClangTidyContext & Context
bool potentiallyAfter(const Stmt *After, const Stmt *Before) const
Returns whether After can potentially be evaluated after Before.
bool inSequence(const Stmt *Before, const Stmt *After) const
Returns whether Before is sequenced before After.
static SmallVector< const Stmt *, 1 > getParentStmts(const Stmt *S, ASTContext *Context)