[PATCH] D70494: [clangd] Fix diagnostic location for macro expansions
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 20 07:28:18 PST 2019
kadircet created this revision.
kadircet added a reviewer: hokein.
Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D70494
Files:
clang-tools-extra/clangd/Diagnostics.cpp
clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -951,7 +951,11 @@
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(IgnoreDiags, FromNonWrittenInclude) {
@@ -963,6 +967,23 @@
EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
}
+TEST(IgnoreDiags, 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'?")));
+}
+
} // namespace
} // namespace clangd
Index: clang-tools-extra/clangd/Diagnostics.cpp
===================================================================
--- clang-tools-extra/clangd/Diagnostics.cpp
+++ clang-tools-extra/clangd/Diagnostics.cpp
@@ -117,8 +117,8 @@
if (D.Severity < DiagnosticsEngine::Level::Error)
return false;
- const SourceLocation &DiagLoc = Info.getLocation();
const SourceManager &SM = Info.getSourceManager();
+ const SourceLocation &DiagLoc = SM.getFileLoc(Info.getLocation());
SourceLocation IncludeInMainFile;
auto GetIncludeLoc = [&SM](SourceLocation SLoc) {
return SM.getIncludeLoc(SM.getFileID(SLoc));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70494.230260.patch
Type: text/x-patch
Size: 2096 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191120/d9de9e67/attachment-0001.bin>
More information about the cfe-commits
mailing list