[clang-tools-extra] [clangd] Avoid invalid include-fixer fuzzy-find queries (PR #212134)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 26 18:42:20 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: Macro Terra (hongtaihu)
<details>
<summary>Changes</summary>
Fixes #<!-- -->200571.
Sema error recovery can produce special declaration names containing qualified
types, e.g. `operator std::align_val_t`.
IncludeFixer forwarded these names as `FuzzyFindRequest::Query`, but fuzzy-find
queries must be unqualified. This triggers an assertion in `MemIndex`.
Skip include fixing for unresolved names that cannot be represented as an
unqualified query plus scopes. The original diagnostic is left unchanged.
We intentionally do not attempt to normalize these recovery-generated names:
they may encode special declaration-name semantics, and inserting a header
alone cannot in general repair the corresponding source spelling.
Add a regression test for the malformed conversion-operator recovery path.
Assisted by codex.
Testing:
the regression path no longer asserts.
---
Full diff: https://github.com/llvm/llvm-project/pull/212134.diff
2 Files Affected:
- (modified) clang-tools-extra/clangd/IncludeFixer.cpp (+6)
- (modified) clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp (+13)
``````````diff
diff --git a/clang-tools-extra/clangd/IncludeFixer.cpp b/clang-tools-extra/clangd/IncludeFixer.cpp
index 5ecf853524a3f..cb65ac82446f8 100644
--- a/clang-tools-extra/clangd/IncludeFixer.cpp
+++ b/clang-tools-extra/clangd/IncludeFixer.cpp
@@ -559,6 +559,12 @@ std::vector<Fix> IncludeFixer::fixUnresolvedName() const {
vlog("Trying to fix unresolved name \"{0}\" in scopes: [{1}]",
Unresolved.Name, llvm::join(Unresolved.Scopes, ", "));
+ // FuzzyFind only supports unqualified queries. Sema recovery may produce
+ // special declaration names, such as conversion operators with qualified
+ // types, that cannot be represented as a query plus scopes.
+ if (llvm::StringRef(Unresolved.Name).contains("::"))
+ return {};
+
FuzzyFindRequest Req;
Req.AnyScope = false;
Req.Query = Unresolved.Name;
diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 6d91ac1ef1e8e..3d85e6c285f50 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -1560,6 +1560,19 @@ TEST(IncludeFixerTest, NoCrashMemberAccess) {
UnorderedElementsAre(Diag(Test.range(), "no member named 'xy' in 'X'")));
}
+TEST(IncludeFixerTest, NoCrashOnQualifiedConversionOperatorName) {
+ auto TU = TestTU::withCode(R"cpp(// error-ok
+namespace std {}
+void f() { operator new[](0, operator::align_val_t{}); }
+ )cpp");
+ TU.ExtraArgs.push_back("-std=c++17");
+ auto Index = buildIndexWithSymbol(
+ SymbolWithHeader{"std::align_val_t", "unittest:///new.h", "<new>"});
+ TU.ExternalIndex = Index.get();
+
+ EXPECT_THAT(TU.build().getDiagnostics(), Not(IsEmpty()));
+}
+
TEST(IncludeFixerTest, UseCachedIndexResults) {
// As index results for the identical request are cached, more than 5 fixes
// are generated.
``````````
</details>
https://github.com/llvm/llvm-project/pull/212134
More information about the cfe-commits
mailing list