[PATCH] D57944: [clangd] Fix an assertion in TypoCorrection.

Eric Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 8 05:27:34 PST 2019


This revision was automatically updated to reflect the committed changes.
ioeric marked an inline comment as done.
Closed by commit rL353514: [clangd] Fix an assertion in TypoCorrection. (authored by ioeric, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D57944?vs=185944&id=185950#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57944/new/

https://reviews.llvm.org/D57944

Files:
  clang-tools-extra/trunk/clangd/IncludeFixer.cpp
  clang-tools-extra/trunk/unittests/clangd/DiagnosticsTests.cpp


Index: clang-tools-extra/trunk/unittests/clangd/DiagnosticsTests.cpp
===================================================================
--- clang-tools-extra/trunk/unittests/clangd/DiagnosticsTests.cpp
+++ clang-tools-extra/trunk/unittests/clangd/DiagnosticsTests.cpp
@@ -423,6 +423,21 @@
                               "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
Index: clang-tools-extra/trunk/clangd/IncludeFixer.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/IncludeFixer.cpp
+++ clang-tools-extra/trunk/clangd/IncludeFixer.cpp
@@ -192,12 +192,6 @@
     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 @@
           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 @@
       if (SpecifiedScope) {
         Scopes.push_back(*SpecifiedScope);
       } else {
+        assert(S);
         // No scope qualifier is specified. Collect all accessible scopes in the
         // context.
         VisitedContextCollector Collector;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57944.185950.patch
Type: text/x-patch
Size: 2297 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190208/9a276fe1/attachment.bin>


More information about the cfe-commits mailing list