From 60c9316d62581e93a1f8292343885344c90994f7 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Thu, 11 Feb 2016 09:57:55 +0000 Subject: [PATCH] [clang-tidy] Fix an assert failure in 'readability-braces-around-statements' check. 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-tidy/readability/BracesAroundStatementsCheck.cpp | 5 ++++- ...readability-braces-around-statements-assert-failure.cpp | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/test/clang-tidy/readability-braces-around-statements-assert-failure.cpp diff --git a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp index 9077a0275493..629388100c1e 100644 --- a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp @@ -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 index 000000000000..fe9bcf4e9d19 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/readability-braces-around-statements-assert-failure.cpp @@ -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' + } +}