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

Kirill Bobyrev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 27 04:45:06 PDT 2021


kbobyrev updated this revision to Diff 382608.
kbobyrev added a comment.

Also handle the Command-Line defines.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112608/new/

https://reviews.llvm.org/D112608

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


Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -234,7 +234,7 @@
                                    "<system_header.h>"));
 }
 
-TEST(IncludeCleaner, ScratchBuffer) {
+TEST(IncludeCleaner, VirtualBuffers) {
   TestTU TU;
   TU.Filename = "foo.cpp";
   TU.Code = R"cpp(
@@ -242,6 +242,8 @@
 
     using flags::FLAGS_FOO;
 
+    int y = CLI;
+
     int concat(a, b) = 42;
     )cpp";
   // The pasting operator in combination with DEFINE_FLAG will create
@@ -258,6 +260,7 @@
     #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();
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -133,7 +133,8 @@
   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))
       return;
     assert(SM.isInFileID(Loc, FID));
     if (Loc.isFileID()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112608.382608.patch
Type: text/x-patch
Size: 1468 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211027/daa8da82/attachment.bin>


More information about the cfe-commits mailing list