[clangd] Fix filename ranges while replaying preamble

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
This commit is contained in:
Kadir Cetinkaya 2021-10-26 19:10:26 +02:00
parent ae27c57b18
commit e42f5d4b48
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
2 changed files with 13 additions and 6 deletions

View 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 {

View 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;