[clang-tools-extra] 22079c6 - [clangd] IncludeCleaner: Do not process locations in built-in files

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 27 10:31:56 PDT 2021


Author: Kirill Bobyrev
Date: 2021-10-27T19:31:43+02:00
New Revision: 22079c61a82300a9ea89aeea58d5bf2780a296e0

URL: https://github.com/llvm/llvm-project/commit/22079c61a82300a9ea89aeea58d5bf2780a296e0
DIFF: https://github.com/llvm/llvm-project/commit/22079c61a82300a9ea89aeea58d5bf2780a296e0.diff

LOG: [clangd] IncludeCleaner: Do not process locations in built-in files

Doing otherwise leads to crashing. Way to reproduce: open "gmock/gmock.h" in
the LLVM source tree.

Reviewed By: kadircet

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/IncludeCleaner.cpp
    clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp
index b1d0f956157f..156129be22fe 100644
--- a/clang-tools-extra/clangd/IncludeCleaner.cpp
+++ b/clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -129,7 +129,9 @@ struct ReferencedFiles {
   void add(SourceLocation Loc) { add(SM.getFileID(Loc), Loc); }
 
   void add(FileID FID, SourceLocation Loc) {
-    if (FID.isInvalid())
+    // Check if Loc is not written in a physical file.
+    if (FID.isInvalid() || SM.isWrittenInBuiltinFile(Loc) ||
+        SM.isWrittenInCommandLineFile(Loc))
       return;
     assert(SM.isInFileID(Loc, FID));
     if (Loc.isFileID()) {

diff  --git a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
index aeb76f8dee22..99749692c541 100644
--- a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -234,7 +234,7 @@ TEST(IncludeCleaner, GetUnusedHeaders) {
                                    "<system_header.h>"));
 }
 
-TEST(IncludeCleaner, ScratchBuffer) {
+TEST(IncludeCleaner, VirtualBuffers) {
   TestTU TU;
   TU.Filename = "foo.cpp";
   TU.Code = R"cpp(
@@ -242,6 +242,10 @@ TEST(IncludeCleaner, ScratchBuffer) {
 
     using flags::FLAGS_FOO;
 
+    // CLI will come from a define, __llvm__ is a built-in. In both cases, they
+    // come from non-existent files.
+    int y = CLI + __llvm__;
+
     int concat(a, b) = 42;
     )cpp";
   // The pasting operator in combination with DEFINE_FLAG will create
@@ -258,6 +262,7 @@ TEST(IncludeCleaner, ScratchBuffer) {
     #define ab x
     #define concat(x, y) x##y
     )cpp";
+  TU.ExtraArgs = {"-DCLI=42"};
   ParsedAST AST = TU.build();
   auto &SM = AST.getSourceManager();
   auto &Includes = AST.getIncludeStructure();


        


More information about the cfe-commits mailing list