[clang-tidy] Sequence init statements, declarations, and conditions correctly in if, switch, and while
Summary: Fixes https://bugs.llvm.org/show_bug.cgi?id=36516.
Reviewers: ilya-biryukov, alexfh, aaron.ballman, hokein
Reviewed By: alexfh
Subscribers: xazax.hun, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D49918
llvm-svn: 338932
diff --git a/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp b/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp
index 02d4a0b..48c3de5 100644
--- a/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp
+++ b/clang-tools-extra/clang-tidy/utils/ExprSequence.cpp
@@ -139,11 +139,26 @@
if (S == ForRange->getLoopVarStmt())
return ForRange->getBody();
} else if (const auto *TheIfStmt = dyn_cast<IfStmt>(Parent)) {
- // If statement: If a variable is declared inside the condition, the
- // expression used to initialize the variable is sequenced before the
- // evaluation of the condition.
+ // If statement:
+ // - Sequence init statement before variable declaration.
+ // - Sequence variable declaration (along with the expression used to
+ // initialize it) before the evaluation of the condition.
+ if (S == TheIfStmt->getInit())
+ return TheIfStmt->getConditionVariableDeclStmt();
if (S == TheIfStmt->getConditionVariableDeclStmt())
return TheIfStmt->getCond();
+ } else if (const auto *TheSwitchStmt = dyn_cast<SwitchStmt>(Parent)) {
+ // Ditto for switch statements.
+ if (S == TheSwitchStmt->getInit())
+ return TheSwitchStmt->getConditionVariableDeclStmt();
+ if (S == TheSwitchStmt->getConditionVariableDeclStmt())
+ return TheSwitchStmt->getCond();
+ } else if (const auto *TheWhileStmt = dyn_cast<WhileStmt>(Parent)) {
+ // While statement: Sequence variable declaration (along with the
+ // expression used to initialize it) before the evaluation of the
+ // condition.
+ if (S == TheWhileStmt->getConditionVariableDeclStmt())
+ return TheWhileStmt->getCond();
}
}