[PATCH] D112559: [clangd] Fix filename ranges while replaying preamble

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 26 10:12:39 PDT 2021


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

Clangd used first token of filename as filename range rather than the
synthezied filename token. Unfortunately the former only contains `"` or `<` in
the raw lexing mode, resulting in wrong range information and breaking tidy
checks that relied on it.

Fixes https://github.com/clangd/clangd/issues/896.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112559

Files:
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/unittests/ParsedASTTests.cpp


Index: clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
+++ clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
@@ -361,7 +361,11 @@
         : HashOffset(SM.getDecomposedLoc(HashLoc).second), IncTok(IncludeTok),
           IncDirective(IncludeTok.getIdentifierInfo()->getName()),
           FileNameOffset(SM.getDecomposedLoc(FilenameRange.getBegin()).second),
-          FileName(FileName), IsAngled(IsAngled) {}
+          FileName(FileName), IsAngled(IsAngled) {
+      EXPECT_EQ(
+          toSourceCode(SM, FilenameRange.getAsRange()).drop_back().drop_front(),
+          FileName);
+    }
     size_t HashOffset;
     syntax::Token IncTok;
     llvm::StringRef IncDirective;
Index: clang-tools-extra/clangd/ParsedAST.cpp
===================================================================
--- clang-tools-extra/clangd/ParsedAST.cpp
+++ clang-tools-extra/clangd/ParsedAST.cpp
@@ -222,11 +222,14 @@
       const FileEntry *FE = File ? &File->getFileEntry() : nullptr;
       llvm::StringRef WrittenFilename =
           llvm::StringRef(Inc.Written).drop_front().drop_back();
-      Delegate->InclusionDirective(HashTok->location(), SynthesizedIncludeTok,
-                                   WrittenFilename, Inc.Written.front() == '<',
-                                   FileTok->range(SM).toCharRange(SM), FE,
-                                   "SearchPath", "RelPath",
-                                   /*Imported=*/nullptr, Inc.FileKind);
+      Delegate->InclusionDirective(
+          HashTok->location(), SynthesizedIncludeTok, WrittenFilename,
+          Inc.Written.front() == '<',
+          syntax::FileRange(SM, SynthesizedFilenameTok.getLocation(),
+                            SynthesizedFilenameTok.getEndLoc())
+              .toCharRange(SM),
+          FE, "SearchPath", "RelPath",
+          /*Imported=*/nullptr, Inc.FileKind);
       if (File)
         Delegate->FileSkipped(*File, SynthesizedFilenameTok, Inc.FileKind);
       else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112559.382383.patch
Type: text/x-patch
Size: 2097 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211026/97b9e6da/attachment.bin>


More information about the cfe-commits mailing list