Skip to content

[clangd] Fix is spelled in source bug #76668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clang-tools-extra/clangd/SourceCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM) {
if (Loc.isFileID())
return true;
auto Spelling = SM.getDecomposedSpellingLoc(Loc);
StringRef SpellingFile = SM.getSLocEntry(Spelling.first).getFile().getName();
bool InvalidSLocEntry = false;
const auto SLocEntry = SM.getSLocEntry(Spelling.first, &InvalidSLocEntry);
if (InvalidSLocEntry)
return false;
StringRef SpellingFile = SLocEntry.getFile().getName();
if (SpellingFile == "<scratch space>")
return false;
if (SpellingFile == "<built-in>")
Expand Down
19 changes: 19 additions & 0 deletions clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,25 @@ TEST(SourceCodeTests, isKeywords) {
EXPECT_FALSE(isKeyword("override", LangOpts));
}

TEST(SourceCodeTests, isSpelledInSource) {
Annotations Test("");
ParsedAST AST = TestTU::withCode(Test.code()).build();
const SourceManager &SM = AST.getSourceManager();

EXPECT_TRUE(
isSpelledInSource(SM.getLocForStartOfFile(SM.getMainFileID()), SM));

// Check that isSpelledInSource() handles various invalid source locations
// gracefully.
//
// Returning true for SourceLocation() is a behavior that falls out of the
// current implementation, which has an early exit for isFileID().
// FIXME: Should it return false on SourceLocation()? Does it matter?
EXPECT_TRUE(isSpelledInSource(SourceLocation(), SM));
EXPECT_FALSE(isSpelledInSource(
SourceLocation::getFromRawEncoding(SourceLocation::UIntTy(1 << 31)), SM));
}

struct IncrementalTestStep {
llvm::StringRef Src;
llvm::StringRef Contents;
Expand Down