[clang-tidy] Fix an assert failure in 'readability-braces-around-statements' check.
authorHaojian Wu <hokein@google.com>
Thu, 11 Feb 2016 09:57:55 +0000 (09:57 +0000)
committerHaojian Wu <hokein@google.com>
Thu, 11 Feb 2016 09:57:55 +0000 (09:57 +0000)
Summary:
The check will trigger a assert failure("CondEndLoc.isValid") when
checking the IfStmt whose condition expression is not parsed.

In this case, we should ignore that.

Reviewers: alexfh

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D17069

llvm-svn: 260505

clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
clang-tools-extra/test/clang-tidy/readability-braces-around-statements-assert-failure.cpp [new file with mode: 0644]

index 9077a02..6293881 100644 (file)
@@ -180,7 +180,10 @@ BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
   if (const DeclStmt *CondVar = S->getConditionVariableDeclStmt())
     CondEndLoc = CondVar->getLocEnd();
 
-  assert(CondEndLoc.isValid());
+  if (!CondEndLoc.isValid()) {
+    return SourceLocation();
+  }
+
   SourceLocation PastCondEndLoc =
       Lexer::getLocForEndOfToken(CondEndLoc, 0, SM, Context->getLangOpts());
   if (PastCondEndLoc.isInvalid())
diff --git a/clang-tools-extra/test/clang-tidy/readability-braces-around-statements-assert-failure.cpp b/clang-tools-extra/test/clang-tidy/readability-braces-around-statements-assert-failure.cpp
new file mode 100644 (file)
index 0000000..fe9bcf4
--- /dev/null
@@ -0,0 +1,7 @@
+// RUN: %check_clang_tidy %s readability-braces-around-statements %t
+
+int test_failure() {
+  if (std::rand()) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: error: use of undeclared identifier 'std'
+  }
+}