PiotrZSL created this revision.
PiotrZSL added reviewers: njames93, aaron.ballman, carlosgalvezp.
Herald added a subscriber: xazax.hun.
Herald added a project: All.
PiotrZSL requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Fix crash when diagnostic is emit with invalid location,
but with attached valid ranges. Diagnostic can contain
invalid location, but SourceManager attached to it still
can be valid, use it in such case or fallback to known
SourceManager.

Fixes: #64602


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157649

Files:
  clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
  clang-tools-extra/test/clang-tidy/infrastructure/diagnostic.cpp


Index: clang-tools-extra/test/clang-tidy/infrastructure/diagnostic.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/infrastructure/diagnostic.cpp
+++ clang-tools-extra/test/clang-tidy/infrastructure/diagnostic.cpp
@@ -25,6 +25,7 @@
 // RUN: not clang-tidy -checks='-*,modernize-use-override' 
%T/diagnostics/input.cpp -- -DCOMPILATION_ERROR 2>&1 | FileCheck 
-check-prefix=CHECK6 -implicit-check-not='{{warning:|error:}}' %s
 // RUN: clang-tidy 
-checks='-*,modernize-use-override,clang-diagnostic-macro-redefined' %s -- 
-DMACRO_FROM_COMMAND_LINE -std=c++20 | FileCheck -check-prefix=CHECK4 
-implicit-check-not='{{warning:|error:}}' %s
 // RUN: clang-tidy 
-checks='-*,modernize-use-override,clang-diagnostic-macro-redefined,clang-diagnostic-literal-conversion'
 %s -- -DMACRO_FROM_COMMAND_LINE -std=c++20 -Wno-macro-redefined | FileCheck 
--check-prefix=CHECK7 -implicit-check-not='{{warning:|error:}}' %s
+// RUN: not clang-tidy -checks='-*,modernize-use-override' %s -- -std=c++20 
-DPR64602 | FileCheck -check-prefix=CHECK8 
-implicit-check-not='{{warning:|error:}}' %s
 
 // CHECK1: error: no input files [clang-diagnostic-error]
 // CHECK1: error: no such file or directory: '{{.*}}nonexistent.cpp' 
[clang-diagnostic-error]
@@ -54,3 +55,18 @@
   // CHECK6: :[[@LINE-1]]:3: error: cannot take the address of an rvalue of 
type 'int' [clang-diagnostic-error]
 }
 #endif
+
+#ifdef PR64602 // Should not crash
+template <class T = void>
+struct S
+{
+    auto foo(auto);
+};
+
+template <>
+auto S<>::foo(auto)
+{
+    return 1;
+}
+// CHECK8: error: template parameter list matching the non-templated nested 
type 'S<>' should be empty ('template<>') [clang-diagnostic-error]
+#endif
Index: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -436,9 +436,10 @@
         Errors.back());
     SmallString<100> Message;
     Info.FormatDiagnostic(Message);
-    FullSourceLoc Loc;
-    if (Info.getLocation().isValid() && Info.hasSourceManager())
-      Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
+    FullSourceLoc Loc(Info.getLocation(),
+                      Info.hasSourceManager()
+                          ? Info.getSourceManager()
+                          : Context.DiagEngine->getSourceManager());
     Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(),
                              Info.getFixItHints());
   }


Index: clang-tools-extra/test/clang-tidy/infrastructure/diagnostic.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/infrastructure/diagnostic.cpp
+++ clang-tools-extra/test/clang-tidy/infrastructure/diagnostic.cpp
@@ -25,6 +25,7 @@
 // RUN: not clang-tidy -checks='-*,modernize-use-override' %T/diagnostics/input.cpp -- -DCOMPILATION_ERROR 2>&1 | FileCheck -check-prefix=CHECK6 -implicit-check-not='{{warning:|error:}}' %s
 // RUN: clang-tidy -checks='-*,modernize-use-override,clang-diagnostic-macro-redefined' %s -- -DMACRO_FROM_COMMAND_LINE -std=c++20 | FileCheck -check-prefix=CHECK4 -implicit-check-not='{{warning:|error:}}' %s
 // RUN: clang-tidy -checks='-*,modernize-use-override,clang-diagnostic-macro-redefined,clang-diagnostic-literal-conversion' %s -- -DMACRO_FROM_COMMAND_LINE -std=c++20 -Wno-macro-redefined | FileCheck --check-prefix=CHECK7 -implicit-check-not='{{warning:|error:}}' %s
+// RUN: not clang-tidy -checks='-*,modernize-use-override' %s -- -std=c++20 -DPR64602 | FileCheck -check-prefix=CHECK8 -implicit-check-not='{{warning:|error:}}' %s
 
 // CHECK1: error: no input files [clang-diagnostic-error]
 // CHECK1: error: no such file or directory: '{{.*}}nonexistent.cpp' [clang-diagnostic-error]
@@ -54,3 +55,18 @@
   // CHECK6: :[[@LINE-1]]:3: error: cannot take the address of an rvalue of type 'int' [clang-diagnostic-error]
 }
 #endif
+
+#ifdef PR64602 // Should not crash
+template <class T = void>
+struct S
+{
+    auto foo(auto);
+};
+
+template <>
+auto S<>::foo(auto)
+{
+    return 1;
+}
+// CHECK8: error: template parameter list matching the non-templated nested type 'S<>' should be empty ('template<>') [clang-diagnostic-error]
+#endif
Index: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -436,9 +436,10 @@
         Errors.back());
     SmallString<100> Message;
     Info.FormatDiagnostic(Message);
-    FullSourceLoc Loc;
-    if (Info.getLocation().isValid() && Info.hasSourceManager())
-      Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
+    FullSourceLoc Loc(Info.getLocation(),
+                      Info.hasSourceManager()
+                          ? Info.getSourceManager()
+                          : Context.DiagEngine->getSourceManager());
     Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(),
                              Info.getFixItHints());
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to