[clang-tools-extra] r368549 - [clangd] Drop diags from non-written #include.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 12 02:35:04 PDT 2019


Author: hokein
Date: Mon Aug 12 02:35:04 2019
New Revision: 368549

URL: http://llvm.org/viewvc/llvm-project?rev=368549&view=rev
Log:
[clangd] Drop diags from non-written #include.

Summary: This would fix that we show weird diagnostics on random lines of the main file.

Reviewers: ilya-biryukov

Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D66074

Modified:
    clang-tools-extra/trunk/clangd/Diagnostics.cpp
    clang-tools-extra/trunk/clangd/unittests/DiagnosticsTests.cpp

Modified: clang-tools-extra/trunk/clangd/Diagnostics.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Diagnostics.cpp?rev=368549&r1=368548&r2=368549&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Diagnostics.cpp (original)
+++ clang-tools-extra/trunk/clangd/Diagnostics.cpp Mon Aug 12 02:35:04 2019
@@ -122,8 +122,12 @@ bool adjustDiagFromHeader(Diag &D, const
     return SM.getIncludeLoc(SM.getFileID(SLoc));
   };
   for (auto IncludeLocation = GetIncludeLoc(DiagLoc); IncludeLocation.isValid();
-       IncludeLocation = GetIncludeLoc(IncludeLocation))
-    IncludeInMainFile = IncludeLocation;
+       IncludeLocation = GetIncludeLoc(IncludeLocation)) {
+    if (clangd::isInsideMainFile(IncludeLocation, SM)) {
+      IncludeInMainFile = IncludeLocation;
+      break;
+    }
+  }
   if (IncludeInMainFile.isInvalid())
     return false;
 

Modified: clang-tools-extra/trunk/clangd/unittests/DiagnosticsTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/DiagnosticsTests.cpp?rev=368549&r1=368548&r2=368549&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/DiagnosticsTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/DiagnosticsTests.cpp Mon Aug 12 02:35:04 2019
@@ -948,6 +948,15 @@ TEST(IgnoreDiags, FromNonWrittenSources)
   EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
 }
 
+TEST(IgnoreDiags, FromNonWrittenInclude) {
+  TestTU TU = TestTU::withCode("");
+  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
+  // to render it in the main file as there is no written location there.
+  EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
+}
+
 } // namespace
 
 } // namespace clangd




More information about the cfe-commits mailing list