summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clangd/IncludeFixer.cpp13
-rw-r--r--clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp15
2 files changed, 22 insertions, 6 deletions
diff --git a/clang-tools-extra/clangd/IncludeFixer.cpp b/clang-tools-extra/clangd/IncludeFixer.cpp
index adcb51a4bb1..cdd973a24a9 100644
--- a/clang-tools-extra/clangd/IncludeFixer.cpp
+++ b/clang-tools-extra/clangd/IncludeFixer.cpp
@@ -192,12 +192,6 @@ public:
if (!SemaPtr->SourceMgr.isWrittenInMainFile(Typo.getLoc()))
return clang::TypoCorrection();
- assert(S && "Enclosing scope must be set.");
-
- UnresolvedName Unresolved;
- Unresolved.Name = Typo.getAsString();
- Unresolved.Loc = Typo.getBeginLoc();
-
// FIXME: support invalid scope before a type name. In the following
// example, namespace "clang::tidy::" hasn't been declared/imported.
// namespace clang {
@@ -228,6 +222,12 @@ public:
return TypoCorrection();
}
}
+ if (!SpecifiedScope && !S) // Give up if no scope available.
+ return TypoCorrection();
+
+ UnresolvedName Unresolved;
+ Unresolved.Name = Typo.getAsString();
+ Unresolved.Loc = Typo.getBeginLoc();
auto *Sem = SemaPtr; // Avoid capturing `this`.
Unresolved.GetScopes = [Sem, SpecifiedScope, S, LookupKind]() {
@@ -235,6 +235,7 @@ public:
if (SpecifiedScope) {
Scopes.push_back(*SpecifiedScope);
} else {
+ assert(S);
// No scope qualifier is specified. Collect all accessible scopes in the
// context.
VisitedContextCollector Collector;
diff --git a/clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp b/clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp
index e9f24315447..a42e5a71647 100644
--- a/clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp
+++ b/clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp
@@ -423,6 +423,21 @@ void foo() {
"Add include \"b.h\" for symbol na::nb::X")))));
}
+TEST(IncludeFixerTest, NoCrashMemebrAccess) {
+ Annotations Test(R"cpp(
+ struct X { int xyz; };
+ void g() { X x; x.$[[xy]] }
+ )cpp");
+ auto TU = TestTU::withCode(Test.code());
+ auto Index = buildIndexWithSymbol(
+ SymbolWithHeader{"na::X", "unittest:///a.h", "\"a.h\""});
+ TU.ExternalIndex = Index.get();
+
+ EXPECT_THAT(
+ TU.build().getDiagnostics(),
+ UnorderedElementsAre(Diag(Test.range(), "no member named 'xy' in 'X'")));
+}
+
} // namespace
} // namespace clangd
} // namespace clang
OpenPOWER on IntegriCloud