Skip to content

Commit 53671fc

Browse files
PiotrZSLtru
authored andcommitted
[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 (cherry picked from commit efd44f8)
1 parent 38908de commit 53671fc

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,11 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic(
436436
SmallString<100> Message;
437437
Info.FormatDiagnostic(Message);
438438
FullSourceLoc Loc;
439-
if (Info.getLocation().isValid() && Info.hasSourceManager())
439+
if (Info.hasSourceManager())
440440
Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
441+
else if (Context.DiagEngine->hasSourceManager())
442+
Loc = FullSourceLoc(Info.getLocation(),
443+
Context.DiagEngine->getSourceManager());
441444
Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(),
442445
Info.getFixItHints());
443446
}

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

Lines changed: 16 additions & 0 deletions
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)