[PATCH] D49918: [clang-tidy] Sequence init statements, declarations, and conditions correctly in if, switch, and while
This revision was automatically updated to reflect the committed changes. Closed by commit rCTE338932: [clang-tidy] Sequence init statements, declarations, and conditions correctly… (authored by mboehme, committed by ). Changed prior to commit: https://reviews.llvm.org/D49918?vs=159093=159105#toc Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D49918 Files: clang-tidy/utils/ExprSequence.cpp test/clang-tidy/bugprone-use-after-move.cpp Index: test/clang-tidy/bugprone-use-after-move.cpp === --- test/clang-tidy/bugprone-use-after-move.cpp +++ test/clang-tidy/bugprone-use-after-move.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s bugprone-use-after-move %t -- -- -std=c++11 -fno-delayed-template-parsing +// RUN: %check_clang_tidy %s bugprone-use-after-move %t -- -- -std=c++17 -fno-delayed-template-parsing typedef decltype(nullptr) nullptr_t; @@ -1132,13 +1132,30 @@ } } -// If a variable is declared in an if statement, the declaration of the variable -// (which is treated like a reinitialization by the check) is sequenced before -// the evaluation of the condition (which constitutes a use). -void ifStmtSequencesDeclAndCondition() { +// If a variable is declared in an if, while or switch statement, the init +// statement (for if and switch) is sequenced before the variable declaration, +// which in turn is sequenced before the evaluation of the condition. +void ifWhileAndSwitchSequenceInitDeclAndCondition() { for (int i = 0; i < 10; ++i) { -if (A a = A()) { - std::move(a); +A a1; +if (A a2 = std::move(a1)) { + std::move(a2); +} + } + for (int i = 0; i < 10; ++i) { +A a1; +if (A a2 = std::move(a1); A a3 = std::move(a2)) { + std::move(a3); +} + } + while (A a = A()) { +std::move(a); + } + for (int i = 0; i < 10; ++i) { +A a1; +switch (A a2 = a1; A a3 = std::move(a2)) { + case true: +std::move(a3); } } } Index: clang-tidy/utils/ExprSequence.cpp === --- clang-tidy/utils/ExprSequence.cpp +++ clang-tidy/utils/ExprSequence.cpp @@ -139,11 +139,26 @@ if (S == ForRange->getLoopVarStmt()) return ForRange->getBody(); } else if (const auto *TheIfStmt = dyn_cast(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(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(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(); } } Index: test/clang-tidy/bugprone-use-after-move.cpp === --- test/clang-tidy/bugprone-use-after-move.cpp +++ test/clang-tidy/bugprone-use-after-move.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s bugprone-use-after-move %t -- -- -std=c++11 -fno-delayed-template-parsing +// RUN: %check_clang_tidy %s bugprone-use-after-move %t -- -- -std=c++17 -fno-delayed-template-parsing typedef decltype(nullptr) nullptr_t; @@ -1132,13 +1132,30 @@ } } -// If a variable is declared in an if statement, the declaration of the variable -// (which is treated like a reinitialization by the check) is sequenced before -// the evaluation of the condition (which constitutes a use). -void ifStmtSequencesDeclAndCondition() { +// If a variable is declared in an if, while or switch statement, the init +// statement (for if and switch) is sequenced before the variable declaration, +// which in turn is sequenced before the evaluation of the condition. +void ifWhileAndSwitchSequenceInitDeclAndCondition() { for (int i = 0; i < 10; ++i) { -if (A a = A()) { - std::move(a); +A a1; +if (A a2 = std::move(a1)) { + std::move(a2); +} + } + for (int i = 0; i < 10; ++i) { +A a1; +if (A a2 = std::move(a1); A a3 = std::move(a2)) { + std::move(a3); +} +
[PATCH] D49918: [clang-tidy] Sequence init statements, declarations, and conditions correctly in if, switch, and while
mboehme updated this revision to Diff 159093. mboehme added a comment. Apply clang-format. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D49918 Files: clang-tidy/utils/ExprSequence.cpp test/clang-tidy/bugprone-use-after-move.cpp Index: test/clang-tidy/bugprone-use-after-move.cpp === --- test/clang-tidy/bugprone-use-after-move.cpp +++ test/clang-tidy/bugprone-use-after-move.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s bugprone-use-after-move %t -- -- -std=c++11 -fno-delayed-template-parsing +// RUN: %check_clang_tidy %s bugprone-use-after-move %t -- -- -std=c++17 -fno-delayed-template-parsing typedef decltype(nullptr) nullptr_t; @@ -1132,13 +1132,30 @@ } } -// If a variable is declared in an if statement, the declaration of the variable -// (which is treated like a reinitialization by the check) is sequenced before -// the evaluation of the condition (which constitutes a use). -void ifStmtSequencesDeclAndCondition() { +// If a variable is declared in an if, while or switch statement, the init +// statement (for if and switch) is sequenced before the variable declaration, +// which in turn is sequenced before the evaluation of the condition. +void ifWhileAndSwitchSequenceInitDeclAndCondition() { for (int i = 0; i < 10; ++i) { -if (A a = A()) { - std::move(a); +A a1; +if (A a2 = std::move(a1)) { + std::move(a2); +} + } + for (int i = 0; i < 10; ++i) { +A a1; +if (A a2 = std::move(a1); A a3 = std::move(a2)) { + std::move(a3); +} + } + while (A a = A()) { +std::move(a); + } + for (int i = 0; i < 10; ++i) { +A a1; +switch (A a2 = a1; A a3 = std::move(a2)) { + case true: +std::move(a3); } } } Index: clang-tidy/utils/ExprSequence.cpp === --- clang-tidy/utils/ExprSequence.cpp +++ clang-tidy/utils/ExprSequence.cpp @@ -139,11 +139,26 @@ if (S == ForRange->getLoopVarStmt()) return ForRange->getBody(); } else if (const auto *TheIfStmt = dyn_cast(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(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(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(); } } Index: test/clang-tidy/bugprone-use-after-move.cpp === --- test/clang-tidy/bugprone-use-after-move.cpp +++ test/clang-tidy/bugprone-use-after-move.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s bugprone-use-after-move %t -- -- -std=c++11 -fno-delayed-template-parsing +// RUN: %check_clang_tidy %s bugprone-use-after-move %t -- -- -std=c++17 -fno-delayed-template-parsing typedef decltype(nullptr) nullptr_t; @@ -1132,13 +1132,30 @@ } } -// If a variable is declared in an if statement, the declaration of the variable -// (which is treated like a reinitialization by the check) is sequenced before -// the evaluation of the condition (which constitutes a use). -void ifStmtSequencesDeclAndCondition() { +// If a variable is declared in an if, while or switch statement, the init +// statement (for if and switch) is sequenced before the variable declaration, +// which in turn is sequenced before the evaluation of the condition. +void ifWhileAndSwitchSequenceInitDeclAndCondition() { for (int i = 0; i < 10; ++i) { -if (A a = A()) { - std::move(a); +A a1; +if (A a2 = std::move(a1)) { + std::move(a2); +} + } + for (int i = 0; i < 10; ++i) { +A a1; +if (A a2 = std::move(a1); A a3 = std::move(a2)) { + std::move(a3); +} + } + while (A a = A()) { +std::move(a); + } + for (int i = 0; i < 10; ++i) { +A a1; +switch (A a2 = a1; A a3 = std::move(a2)) { + case true: +std::move(a3); } } } Index:
[PATCH] D49918: [clang-tidy] Sequence init statements, declarations, and conditions correctly in if, switch, and while
alexfh accepted this revision. alexfh added a comment. Still LG Comment at: test/clang-tidy/bugprone-use-after-move.cpp:1141 +A a1; +if (A a2= std::move(a1)) { + std::move(a2); nit: clang-format this, please. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D49918 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D49918: [clang-tidy] Sequence init statements, declarations, and conditions correctly in if, switch, and while
mboehme marked an inline comment as done. mboehme added a comment. Any further comments? This is marked ready to land, but I've made some non-trivial changes since then, so I didn't just want to land this. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D49918 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits