[clang-tools-extra] r239054 - [clang-tidy] Force braces around leaf 'else if' for consistency.
Samuel Benzaquen
sbenza at google.com
Thu Jun 4 09:36:59 PDT 2015
Author: sbenza
Date: Thu Jun 4 11:36:58 2015
New Revision: 239054
URL: http://llvm.org/viewvc/llvm-project?rev=239054&view=rev
Log:
[clang-tidy] Force braces around leaf 'else if' for consistency.
Summary:
Force braces around leaf 'else if' for consistency.
This complements r233697.
Reviewers: alexfh
Subscribers: curdeius, cfe-commits
Differential Revision: http://reviews.llvm.org/D10245
Modified:
clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.cpp
clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.h
clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityModuleTest.cpp
Modified: clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.cpp?rev=239054&r1=239053&r2=239054&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.cpp Thu Jun 4 11:36:58 2015
@@ -151,11 +151,15 @@ BracesAroundStatementsCheck::check(const
SourceLocation StartLoc = findRParenLoc(S, SM, Context);
if (StartLoc.isInvalid())
return;
+ if (ForceBracesStmts.erase(S))
+ ForceBracesStmts.insert(S->getThen());
bool BracedIf = checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
const Stmt *Else = S->getElse();
+ if (Else && BracedIf)
+ ForceBracesStmts.insert(Else);
if (Else && !isa<IfStmt>(Else)) {
// Omit 'else if' statements here, they will be handled directly.
- checkStmt(Result, Else, S->getElseLoc(), SourceLocation(), BracedIf);
+ checkStmt(Result, Else, S->getElseLoc(), SourceLocation());
}
} else {
llvm_unreachable("Invalid match");
@@ -203,7 +207,7 @@ BracesAroundStatementsCheck::findRParenL
/// Returns true if braces where added.
bool BracesAroundStatementsCheck::checkStmt(
const MatchFinder::MatchResult &Result, const Stmt *S,
- SourceLocation InitialLoc, SourceLocation EndLocHint, bool ForceBraces) {
+ SourceLocation InitialLoc, SourceLocation EndLocHint) {
// 1) If there's a corresponding "else" or "while", the check inserts "} "
// right before that token.
// 2) If there's a multi-line block comment starting on the same line after
@@ -241,7 +245,7 @@ bool BracesAroundStatementsCheck::checkS
assert(EndLoc.isValid());
// Don't require braces for statements spanning less than certain number of
// lines.
- if (ShortStatementLines && !ForceBraces) {
+ if (ShortStatementLines && !ForceBracesStmts.erase(S)) {
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
if (EndLine - StartLine < ShortStatementLines)
@@ -254,6 +258,10 @@ bool BracesAroundStatementsCheck::checkS
return true;
}
+void BracesAroundStatementsCheck::onEndOfTranslationUnit() {
+ ForceBracesStmts.clear();
+}
+
} // namespace readability
} // namespace tidy
} // namespace clang
Modified: clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.h?rev=239054&r1=239053&r2=239054&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/BracesAroundStatementsCheck.h Thu Jun 4 11:36:58 2015
@@ -41,17 +41,18 @@ public:
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ void onEndOfTranslationUnit() override;
private:
bool checkStmt(const ast_matchers::MatchFinder::MatchResult &Result,
const Stmt *S, SourceLocation StartLoc,
- SourceLocation EndLocHint = SourceLocation(),
- bool ForceBraces = false);
+ SourceLocation EndLocHint = SourceLocation());
template <typename IfOrWhileStmt>
SourceLocation findRParenLoc(const IfOrWhileStmt *S, const SourceManager &SM,
const ASTContext *Context);
private:
+ std::set<const Stmt*> ForceBracesStmts;
const unsigned ShortStatementLines;
};
Modified: clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityModuleTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityModuleTest.cpp?rev=239054&r1=239053&r2=239054&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityModuleTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityModuleTest.cpp Thu Jun 4 11:36:58 2015
@@ -254,6 +254,19 @@ TEST(BracesAroundStatementsCheck, IfElse
" else return -3;\n"
"}",
nullptr, "input.cc", None, Options));
+
+ // If the last else is an else-if, we also force it.
+ EXPECT_EQ("int main() {\n"
+ " if (false) { return -1;\n"
+ " } else if (1 == 2) { return -2;\n"
+ "}\n"
+ "}",
+ runCheckOnCode<BracesAroundStatementsCheck>(
+ "int main() {\n"
+ " if (false) return -1;\n"
+ " else if (1 == 2) return -2;\n"
+ "}",
+ nullptr, "input.cc", None, Options));
}
TEST(BracesAroundStatementsCheck, For) {
More information about the cfe-commits
mailing list