[clang-tools-extra] r353514 - [clangd] Fix an assertion in TypoCorrection.
Eric Liu via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 8 05:27:47 PST 2019
Author: ioeric
Date: Fri Feb 8 05:27:47 2019
New Revision: 353514
URL: http://llvm.org/viewvc/llvm-project?rev=353514&view=rev
Log:
[clangd] Fix an assertion in TypoCorrection.
Summary: https://github.com/clangd/clangd/issues/7
Reviewers: sammccall, hokein
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D57944
Modified:
clang-tools-extra/trunk/clangd/IncludeFixer.cpp
clang-tools-extra/trunk/unittests/clangd/DiagnosticsTests.cpp
Modified: clang-tools-extra/trunk/clangd/IncludeFixer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/IncludeFixer.cpp?rev=353514&r1=353513&r2=353514&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/IncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/clangd/IncludeFixer.cpp Fri Feb 8 05:27:47 2019
@@ -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;
Modified: clang-tools-extra/trunk/unittests/clangd/DiagnosticsTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/DiagnosticsTests.cpp?rev=353514&r1=353513&r2=353514&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/DiagnosticsTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/DiagnosticsTests.cpp Fri Feb 8 05:27:47 2019
@@ -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
More information about the cfe-commits
mailing list