[clang-tidy] Fix invalid location in readability-misleading-indentation diagnostic
authorAlexander Kornienko <alexfh@google.com>
Wed, 17 Apr 2019 16:19:47 +0000 (16:19 +0000)
committerAlexander Kornienko <alexfh@google.com>
Wed, 17 Apr 2019 16:19:47 +0000 (16:19 +0000)
Before this patch readability-misleading-indentation could issue diagnostics
with an invalid location, which would lead to an assertion failure in
ClangTidyContext::diag()

llvm-svn: 358589

clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
clang-tools-extra/test/clang-tidy/readability-misleading-indentation.cpp

index 767406e..0fd5c1f 100644 (file)
@@ -81,6 +81,10 @@ void MisleadingIndentationCheck::missingBracesCheck(const SourceManager &SM,
     SourceLocation InnerLoc = Inner->getBeginLoc();
     SourceLocation OuterLoc = CurrentStmt->getBeginLoc();
 
+    if (InnerLoc.isInvalid() || InnerLoc.isMacroID() || OuterLoc.isInvalid() ||
+        OuterLoc.isMacroID())
+      continue;
+
     if (SM.getExpansionLineNumber(InnerLoc) ==
         SM.getExpansionLineNumber(OuterLoc))
       continue;
@@ -88,7 +92,7 @@ void MisleadingIndentationCheck::missingBracesCheck(const SourceManager &SM,
     const Stmt *NextStmt = CStmt->body_begin()[i + 1];
     SourceLocation NextLoc = NextStmt->getBeginLoc();
 
-    if (InnerLoc.isMacroID() || OuterLoc.isMacroID() || NextLoc.isMacroID())
+    if (NextLoc.isInvalid() || NextLoc.isMacroID())
       continue;
 
     if (SM.getExpansionColumnNumber(InnerLoc) ==
index 59d5f40..4039949 100644 (file)
@@ -8,7 +8,7 @@ void foo2();
     foo1();   \
     foo2();
 
-int main()
+void f()
 {
   bool cond1 = true;
   bool cond2 = true;
@@ -90,7 +90,7 @@ int main()
        else {
   }
   // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: different indentation for 'if' and corresponding 'else' [readability-misleading-indentation]
-  
+
   if (cond1) {
     if (cond1) {
     }
@@ -109,3 +109,12 @@ int main()
 
   BLOCK
 }
+
+void g(bool x) {
+  if (x)
+    #pragma unroll
+    for (int k = 0; k < 1; ++k) {}
+
+  #pragma unroll
+  for (int k = 0; k < 1; ++k) {}
+}