[PATCH] D97123: [clangd] Support FixIts that use InsertFromRange instead of inserting raw text

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 20 11:33:17 PST 2021


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

Currently clangd will ignore FixItHint::InsertFromRange when computing edits.
This leads to malformed fixes for diagnostics that choose to use it.
In this patch we will try to grab source text if CodeToInsert is empty.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97123

Files:
  clang-tools-extra/clangd/Diagnostics.cpp
  clang-tools-extra/clangd/SourceCode.cpp


Index: clang-tools-extra/clangd/SourceCode.cpp
===================================================================
--- clang-tools-extra/clangd/SourceCode.cpp
+++ clang-tools-extra/clangd/SourceCode.cpp
@@ -549,6 +549,12 @@
   Result.range =
       halfOpenToRange(M, Lexer::makeFileCharRange(FixIt.RemoveRange, M, L));
   Result.newText = FixIt.CodeToInsert;
+  if (Result.newText.empty() && FixIt.InsertFromRange.isValid()) {
+    bool Invalid = false;
+    auto Insert = Lexer::getSourceText(FixIt.InsertFromRange, M, L, &Invalid);
+    if (!Invalid)
+      Result.newText = Insert.str();
+  }
   return Result;
 }
 
Index: clang-tools-extra/clangd/Diagnostics.cpp
===================================================================
--- clang-tools-extra/clangd/Diagnostics.cpp
+++ clang-tools-extra/clangd/Diagnostics.cpp
@@ -691,6 +691,12 @@
       llvm::StringRef Remove =
           Lexer::getSourceText(FixIt.RemoveRange, SM, *LangOpts, &Invalid);
       llvm::StringRef Insert = FixIt.CodeToInsert;
+      if (Insert.empty() && FixIt.InsertFromRange.isValid()) {
+        bool InvalidInsert = false;
+        Insert = Lexer::getSourceText(FixIt.InsertFromRange, SM, *LangOpts,
+                                      &InvalidInsert);
+        Invalid |= InvalidInsert;
+      }
       if (!Invalid) {
         llvm::raw_svector_ostream M(Message);
         if (!Remove.empty() && !Insert.empty()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97123.325223.patch
Type: text/x-patch
Size: 1407 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210220/bb86e535/attachment-0001.bin>


More information about the cfe-commits mailing list