llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clangd Author: None (schenka0) <details> <summary>Changes</summary> This fixes the issue reported in #<!-- -->76667 and adds an initial unit test for isSpelledInSource(). Note that in that issue there was still some underlying corrupted AST, but this at least makes isSpelledInSource() robust to it. --- Full diff: https://github.com/llvm/llvm-project/pull/76668.diff 2 Files Affected: - (modified) clang-tools-extra/clangd/SourceCode.cpp (+6-1) - (modified) clang-tools-extra/clangd/unittests/SourceCodeTests.cpp (+15) ``````````diff diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp index 835038423fdf37..8c573cc6fc064a 100644 --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -232,7 +232,12 @@ 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; + } + const StringRef SpellingFile = SLocEntry.getFile().getName(); if (SpellingFile == "<scratch space>") return false; if (SpellingFile == "<built-in>") diff --git a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp index 08abde87df6d4d..5dced4d317c605 100644 --- a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp +++ b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp @@ -813,6 +813,21 @@ TEST(SourceCodeTests, isKeywords) { EXPECT_FALSE(isKeyword("override", LangOpts)); } +TEST(SourceCodeTests, isSpelledInSource) { + Annotations Test(R"cpp( + int abc = 1; + )cpp"); + + ParsedAST AST = TestTU::withCode(Test.code()).build(); + llvm::errs() << Test.code(); + const SourceManager &SM = AST.getSourceManager(); + + EXPECT_TRUE( + isSpelledInSource(SM.getLocForStartOfFile(SM.getMainFileID()), SM)); + EXPECT_TRUE(isSpelledInSource(SourceLocation(), SM)); + EXPECT_FALSE(isSpelledInSource(SourceLocation::getFromRawEncoding(-1), SM)); +} + struct IncrementalTestStep { llvm::StringRef Src; llvm::StringRef Contents; `````````` </details> https://github.com/llvm/llvm-project/pull/76668 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits