[clangd] Fix filename ranges while replaying preamble
authorKadir Cetinkaya <kadircet@google.com>
Tue, 26 Oct 2021 17:10:26 +0000 (19:10 +0200)
committerKadir Cetinkaya <kadircet@google.com>
Wed, 27 Oct 2021 07:34:17 +0000 (09:34 +0200)
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.

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

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

index a62169c..8fce756 100644 (file)
@@ -222,11 +222,14 @@ private:
       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 {
index 211f289..eabd174 100644 (file)
@@ -361,7 +361,11 @@ TEST(ParsedASTTest, ReplayPreambleForTidyCheckers) {
         : 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;