[clangd] Fix diagnostic location for macro expansions

Summary:
Diagnostic locations were broken when it was result of a macro
expansion. This patch fixes it by using expansion location instead of location
inside macro body.

Fixes https://github.com/clangd/clangd/issues/201.

Reviewers: hokein

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D70494
This commit is contained in:
Kadir Cetinkaya 2019-11-20 16:17:03 +01:00
parent 5075c68219
commit e841029aef
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
2 changed files with 42 additions and 5 deletions

View File

@ -117,8 +117,8 @@ bool adjustDiagFromHeader(Diag &D, const clang::Diagnostic &Info,
if (D.Severity < DiagnosticsEngine::Level::Error)
return false;
const SourceLocation &DiagLoc = Info.getLocation();
const SourceManager &SM = Info.getSourceManager();
const SourceLocation &DiagLoc = SM.getExpansionLoc(Info.getLocation());
SourceLocation IncludeInMainFile;
auto GetIncludeLoc = [&SM](SourceLocation SLoc) {
return SM.getIncludeLoc(SM.getFileID(SLoc));

View File

@ -941,7 +941,7 @@ TEST(DiagsInHeaders, OnlyErrorOrFatal) {
WithNote(Diag(Header.range(), "error occurred here")))));
}
TEST(IgnoreDiags, FromNonWrittenSources) {
TEST(DiagsInHeaders, FromNonWrittenSources) {
Annotations Main(R"cpp(
#include [["a.h"]]
void foo() {})cpp");
@ -951,11 +951,49 @@ TEST(IgnoreDiags, FromNonWrittenSources) {
TestTU TU = TestTU::withCode(Main.code());
TU.AdditionalFiles = {{"a.h", Header.code()}};
TU.ExtraArgs = {"-DFOO=NOOO"};
EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
EXPECT_THAT(TU.build().getDiagnostics(),
UnorderedElementsAre(AllOf(
Diag(Main.range(),
"in included file: use of undeclared identifier 'NOOO'"),
WithNote(Diag(Header.range(), "error occurred here")))));
}
TEST(DiagsInHeaders, ErrorFromMacroExpansion) {
Annotations Main(R"cpp(
void bar() {
int fo;
#include [["a.h"]]
})cpp");
Annotations Header(R"cpp(
#define X foo
X;)cpp");
TestTU TU = TestTU::withCode(Main.code());
TU.AdditionalFiles = {{"a.h", Header.code()}};
EXPECT_THAT(TU.build().getDiagnostics(),
UnorderedElementsAre(
Diag(Main.range(), "in included file: use of undeclared "
"identifier 'foo'; did you mean 'fo'?")));
}
TEST(DiagsInHeaders, ErrorFromMacroArgument) {
Annotations Main(R"cpp(
void bar() {
int fo;
#include [["a.h"]]
})cpp");
Annotations Header(R"cpp(
#define X(arg) arg
X(foo);)cpp");
TestTU TU = TestTU::withCode(Main.code());
TU.AdditionalFiles = {{"a.h", Header.code()}};
EXPECT_THAT(TU.build().getDiagnostics(),
UnorderedElementsAre(
Diag(Main.range(), "in included file: use of undeclared "
"identifier 'foo'; did you mean 'fo'?")));
}
TEST(IgnoreDiags, FromNonWrittenInclude) {
TestTU TU = TestTU::withCode("");
TestTU TU;
TU.ExtraArgs.push_back("--include=a.h");
TU.AdditionalFiles = {{"a.h", "void main();"}};
// The diagnostic "main must return int" is from the header, we don't attempt
@ -964,6 +1002,5 @@ TEST(IgnoreDiags, FromNonWrittenInclude) {
}
} // namespace
} // namespace clangd
} // namespace clang