summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKadir Cetinkaya <kadircet@google.com>2019-11-20 16:17:03 +0100
committerKadir Cetinkaya <kadircet@google.com>2019-11-25 10:45:14 +0100
commite841029aef74d99d1cb9443edd4a7b761d84ff45 (patch)
tree6cfdadd0d0dd8b86feb311fcc5906eff08be7685
parent5075c68219826a199c67f7450c7cf60a55a71c0b (diff)
downloadbcm5719-llvm-e841029aef74d99d1cb9443edd4a7b761d84ff45.tar.gz
bcm5719-llvm-e841029aef74d99d1cb9443edd4a7b761d84ff45.zip
[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
-rw-r--r--clang-tools-extra/clangd/Diagnostics.cpp2
-rw-r--r--clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp45
2 files changed, 42 insertions, 5 deletions
diff --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp
index c9e1ed6bc68..cd95807162b 100644
--- a/clang-tools-extra/clangd/Diagnostics.cpp
+++ b/clang-tools-extra/clangd/Diagnostics.cpp
@@ -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));
diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 374dae073e0..fe7a8898c5d 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -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
OpenPOWER on IntegriCloud