[clang-tools-extra] r219611 - [clang-tidy] misc-braces-around-statements.ShortStatementLines option
Alexander Kornienko
alexfh at google.com
Mon Oct 13 05:46:22 PDT 2014
Author: alexfh
Date: Mon Oct 13 07:46:22 2014
New Revision: 219611
URL: http://llvm.org/viewvc/llvm-project?rev=219611&view=rev
Log:
[clang-tidy] misc-braces-around-statements.ShortStatementLines option
Add option ShortStatementLines to trigger this check only if the statement spans
over at least a given number of lines.
Modifications from the original patch:
merged test/clang-tidy/misc-braces-around-statements-always.cpp into
test/clang-tidy/misc-braces-around-statements.cpp and removed unnecessary
CHECK-NOTs from the tests.
http://reviews.llvm.org/D5642
Patch by Marek Kurdej!
Added:
clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-few-lines.cpp
clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-same-line.cpp
clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-single-line.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.cpp
clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.h
clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements.cpp
Modified: clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.cpp?rev=219611&r1=219610&r2=219611&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.cpp Mon Oct 13 07:46:22 2014
@@ -110,6 +110,17 @@ SourceLocation findEndLocation(SourceLoc
} // namespace
+BracesAroundStatementsCheck::BracesAroundStatementsCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ // Always add braces by default.
+ ShortStatementLines(Options.get("ShortStatementLines", 0U)) {}
+
+void
+BracesAroundStatementsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "ShortStatementLines", ShortStatementLines);
+}
+
void BracesAroundStatementsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(ifStmt().bind("if"), this);
Finder->addMatcher(whileStmt().bind("while"), this);
@@ -206,11 +217,6 @@ BracesAroundStatementsCheck::checkStmt(c
if (S->getLocStart().isMacroID())
return;
- // TODO: Add an option to insert braces if:
- // * the body doesn't fit on the same line with the control statement
- // * the body takes more than one line
- // * always.
-
const SourceManager &SM = *Result.SourceManager;
const ASTContext *Context = Result.Context;
@@ -230,6 +236,16 @@ BracesAroundStatementsCheck::checkStmt(c
}
assert(StartLoc.isValid());
+ assert(EndLoc.isValid());
+ // Don't require braces for statements spanning less than certain number of
+ // lines.
+ if (ShortStatementLines) {
+ unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
+ unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
+ if (EndLine - StartLine < ShortStatementLines)
+ return;
+ }
+
auto Diag = diag(StartLoc, "statement should be inside braces");
Diag << FixItHint::CreateInsertion(StartLoc, " {")
<< FixItHint::CreateInsertion(EndLoc, ClosingInsertion);
Modified: clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.h?rev=219611&r1=219610&r2=219611&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/BracesAroundStatementsCheck.h Mon Oct 13 07:46:22 2014
@@ -15,10 +15,29 @@
namespace clang {
namespace tidy {
+/// \brief Checks that bodies of 'if' statements and loops ('for', 'range-for',
+/// 'do-while', and 'while') are inside braces
+///
+/// Before:
+/// if (condition)
+/// statement;
+///
+/// After:
+/// if (condition) {
+/// statement;
+/// }
+///
+/// Additionally, one can define an option `ShortStatementLines` defining the
+/// minimal number of lines that the statement should have in order to trigger
+/// this check.
+/// The number of lines is counted from the end of condition or initial keyword
+/// (do/else) until the last line of the inner statement.
+/// Default value 0 means that braces will be added to all statements (not
+/// having them already).
class BracesAroundStatementsCheck : public ClangTidyCheck {
public:
- BracesAroundStatementsCheck(StringRef Name, ClangTidyContext *Context)
- : ClangTidyCheck(Name, Context) {}
+ BracesAroundStatementsCheck(StringRef Name, ClangTidyContext *Context);
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
@@ -29,6 +48,9 @@ private:
template <typename IfOrWhileStmt>
SourceLocation findRParenLoc(const IfOrWhileStmt *S, const SourceManager &SM,
const ASTContext *Context);
+
+private:
+ const unsigned ShortStatementLines;
};
} // namespace tidy
Added: clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-few-lines.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-few-lines.cpp?rev=219611&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-few-lines.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-few-lines.cpp Mon Oct 13 07:46:22 2014
@@ -0,0 +1,31 @@
+// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s misc-braces-around-statements %t -config="{CheckOptions: [{key: misc-braces-around-statements.ShortStatementLines, value: 4}]}" --
+// REQUIRES: shell
+
+void do_something(const char *) {}
+
+bool cond(const char *) {
+ return false;
+}
+
+void test() {
+ if (cond("if1") /*comment*/) do_something("same-line");
+
+ if (cond("if2"))
+ do_something("single-line");
+
+ if (cond("if3") /*comment*/)
+ // some comment
+ do_something("three"
+ "lines");
+
+ if (cond("if4") /*comment*/)
+ // some comment
+ do_something("many"
+ "many"
+ "many"
+ "many"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-7]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if4") /*comment*/) {
+ // CHECK-FIXES: }
+}
Added: clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-same-line.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-same-line.cpp?rev=219611&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-same-line.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-same-line.cpp Mon Oct 13 07:46:22 2014
@@ -0,0 +1,37 @@
+// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s misc-braces-around-statements %t -config="{CheckOptions: [{key: misc-braces-around-statements.ShortStatementLines, value: 1}]}" --
+// REQUIRES: shell
+
+void do_something(const char *) {}
+
+bool cond(const char *) {
+ return false;
+}
+
+void test() {
+ if (cond("if1") /*comment*/) do_something("same-line");
+
+ if (cond("if2"))
+ do_something("single-line");
+ // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if2")) {
+ // CHECK-FIXES: }
+
+ if (cond("if3") /*comment*/)
+ // some comment
+ do_something("three"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-4]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if3") /*comment*/) {
+ // CHECK-FIXES: }
+
+ if (cond("if4") /*comment*/)
+ // some comment
+ do_something("many"
+ "many"
+ "many"
+ "many"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-7]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if4") /*comment*/) {
+ // CHECK-FIXES: }
+}
Added: clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-single-line.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-single-line.cpp?rev=219611&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-single-line.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements-single-line.cpp Mon Oct 13 07:46:22 2014
@@ -0,0 +1,34 @@
+// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s misc-braces-around-statements %t -config="{CheckOptions: [{key: misc-braces-around-statements.ShortStatementLines, value: 2}]}" --
+// REQUIRES: shell
+
+void do_something(const char *) {}
+
+bool cond(const char *) {
+ return false;
+}
+
+void test() {
+ if (cond("if1") /*comment*/) do_something("same-line");
+
+ if (cond("if2"))
+ do_something("single-line");
+
+ if (cond("if3") /*comment*/)
+ // some comment
+ do_something("three"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-4]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if3") /*comment*/) {
+ // CHECK-FIXES: }
+
+ if (cond("if4") /*comment*/)
+ // some comment
+ do_something("many"
+ "many"
+ "many"
+ "many"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-7]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if4") /*comment*/) {
+ // CHECK-FIXES: }
+}
Modified: clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements.cpp?rev=219611&r1=219610&r2=219611&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-braces-around-statements.cpp Mon Oct 13 07:46:22 2014
@@ -11,8 +11,17 @@ bool cond(const char *) {
#define EMPTY_MACRO_FUN()
void test() {
- // CHECK-NOT: warning
- // if
+ if (cond("if0") /*comment*/) do_something("same-line");
+ // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if0") /*comment*/) { do_something("same-line");
+ // CHECK-FIXES: }
+
+ if (cond("if0.1"))
+ do_something("single-line");
+ // CHECK-MESSAGES: :[[@LINE-2]]:21: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if0.1")) {
+ // CHECK-FIXES: }
+
if (cond("if1") /*comment*/)
// some comment
do_something("if1");
@@ -28,7 +37,6 @@ void test() {
// CHECK-FIXES: if (cond("if3")) {
// CHECK-FIXES: }
- // if-else
if (cond("if-else1"))
do_something("if-else1");
// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: statement should be inside braces
@@ -44,8 +52,6 @@ void test() {
do_something("if-else2 else");
}
- // CHECK-NOT: warning
- // if-else if-else
if (cond("if-else if-else1"))
do_something("if");
// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: statement should be inside braces
@@ -65,7 +71,6 @@ void test() {
do_something("else");
}
- // CHECK-NOT: warning
for (;;)
do_something("for");
// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: statement should be inside braces
@@ -80,7 +85,6 @@ void test() {
// CHECK-FIXES: for (;;) {
// CHECK-FIXES: }
- // CHECK-NOT: warning
int arr[4] = {1, 2, 3, 4};
for (int a : arr)
do_something("for-range");
@@ -96,7 +100,6 @@ void test() {
// CHECK-FIXES: for (int a : arr) {
// CHECK-FIXES: }
- // CHECK-NOT: warning
while (cond("while1"))
do_something("while");
// CHECK-MESSAGES: :[[@LINE-2]]:25: warning: statement should be inside braces
@@ -111,7 +114,6 @@ void test() {
// CHECK-FIXES: while (false) {
// CHECK-FIXES: }
- // CHECK-NOT: warning
do
do_something("do1");
while (cond("do1"));
@@ -129,7 +131,6 @@ void test() {
// CHECK-FIXES: do {
// CHECK-FIXES: } while (false);
- // CHECK-NOT: warning
if (cond("ifif1"))
// comment
if (cond("ifif2"))
@@ -159,7 +160,6 @@ void test() {
// CHECK-FIXES: if (cond("ifif5")) {
// CHECK-FIXES: }/* multi-line
- // CHECK-NOT: warning
if (1) while (2) if (3) for (;;) do ; while(false) /**/;/**/
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: statement should be inside braces
// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: statement should be inside braces
@@ -171,8 +171,4 @@ void test() {
// CHECK-FIXES-NEXT: }
// CHECK-FIXES-NEXT: }
// CHECK-FIXES-NEXT: }
-
- // CHECK-NOT: warning
-
- // CHECK-MESSAGES: {{clang-tidy applied [0-9]+ of [0-9]+ suggested fixes.}}
}
More information about the cfe-commits
mailing list