Skip to content

Commit efd44f8

Browse files
committed
[clang-tidy] Fix crash when diagnostic is emit with invalid location
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: llvm#64602 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D157649
1 parent f76ffc1 commit efd44f8

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,11 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic(
437437
SmallString<100> Message;
438438
Info.FormatDiagnostic(Message);
439439
FullSourceLoc Loc;
440-
if (Info.getLocation().isValid() && Info.hasSourceManager())
440+
if (Info.hasSourceManager())
441441
Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
442+
else if (Context.DiagEngine->hasSourceManager())
443+
Loc = FullSourceLoc(Info.getLocation(),
444+
Context.DiagEngine->getSourceManager());
442445
Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(),
443446
Info.getFixItHints());
444447
}

clang-tools-extra/test/clang-tidy/infrastructure/diagnostic.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// 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
2626
// 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
2727
// 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
28+
// RUN: not clang-tidy -checks='-*,modernize-use-override' %s -- -std=c++20 -DPR64602 | FileCheck -check-prefix=CHECK8 -implicit-check-not='{{warning:|error:}}' %s
2829

2930
// CHECK1: error: no input files [clang-diagnostic-error]
3031
// CHECK1: error: no such file or directory: '{{.*}}nonexistent.cpp' [clang-diagnostic-error]
@@ -54,3 +55,18 @@ void f(int a) {
5455
// CHECK6: :[[@LINE-1]]:3: error: cannot take the address of an rvalue of type 'int' [clang-diagnostic-error]
5556
}
5657
#endif
58+
59+
#ifdef PR64602 // Should not crash
60+
template <class T = void>
61+
struct S
62+
{
63+
auto foo(auto);
64+
};
65+
66+
template <>
67+
auto S<>::foo(auto)
68+
{
69+
return 1;
70+
}
71+
// CHECK8: error: template parameter list matching the non-templated nested type 'S<>' should be empty ('template<>') [clang-diagnostic-error]
72+
#endif

0 commit comments

Comments
 (0)