[PATCH] D128204: [clangd] Add fix-it for inserting IWYU pragma: keep

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 20 07:20:02 PDT 2022


njames93 created this revision.
njames93 added reviewers: sammccall, kadircet, kbobyrev.
Herald added subscribers: usaxena95, arphaman.
Herald added a project: All.
njames93 requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Since IncludeCleaner has some support for IWYU keep pragmas, we can suggest it as a fix-it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128204

Files:
  clang-tools-extra/clangd/IncludeCleaner.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
@@ -1792,7 +1792,7 @@
 
 TEST(DiagnosticsTest, IncludeCleaner) {
   Annotations Test(R"cpp(
-$fix[[  $diag[[#include "unused.h"]]
+$fix[[  $diag[[#include "unused.h"]]$insert[[]]
 ]]
   #include "used.h"
 
@@ -1831,11 +1831,13 @@
   auto AST = TU.build();
   EXPECT_THAT(
       *AST.getDiagnostics(),
-      UnorderedElementsAre(AllOf(
-          Diag(Test.range("diag"),
-               "included header unused.h is not used directly"),
-          withTag(DiagnosticTag::Unnecessary), diagSource(Diag::Clangd),
-          withFix(Fix(Test.range("fix"), "", "remove #include directive")))));
+      UnorderedElementsAre(
+          AllOf(Diag(Test.range("diag"),
+                     "included header unused.h is not used directly"),
+                withTag(DiagnosticTag::Unnecessary), diagSource(Diag::Clangd),
+                withFix(Fix(Test.range("fix"), "", "remove #include directive"),
+                        Fix(Test.range("insert"), " // IWYU pragma: keep",
+                            "add pragma keep directive")))));
   auto &Diag = AST.getDiagnostics()->front();
   EXPECT_EQ(getDiagnosticDocURI(Diag.Source, Diag.ID, Diag.Name),
             std::string("https://clangd.llvm.org/guides/include-cleaner"));
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -506,6 +506,13 @@
     D.Fixes.back().Edits.emplace_back();
     D.Fixes.back().Edits.back().range.start.line = Inc->HashLine;
     D.Fixes.back().Edits.back().range.end.line = Inc->HashLine + 1;
+    // Add a fix to append IWYU pragma: keep
+    D.Fixes.emplace_back();
+    D.Fixes.back().Message = "add pragma keep directive";
+    D.Fixes.back().Edits.emplace_back();
+    D.Fixes.back().Edits.back().newText = " // IWYU pragma: keep";
+    D.Fixes.back().Edits.back().range.start =
+        D.Fixes.back().Edits.back().range.end = D.Range.end;
     D.InsideMainFile = true;
     Result.push_back(std::move(D));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128204.438383.patch
Type: text/x-patch
Size: 2318 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220620/05ea0528/attachment.bin>


More information about the cfe-commits mailing list