[PATCH] D61077: [clangd] Query index in code completion no-compile mode.

2019-04-26 Thread Sam McCall via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL359284: [clangd] Query index in code completion no-compile 
mode. (authored by sammccall, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

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

https://reviews.llvm.org/D61077

Files:
  clang-tools-extra/trunk/clangd/CodeComplete.cpp
  clang-tools-extra/trunk/clangd/SourceCode.cpp
  clang-tools-extra/trunk/clangd/SourceCode.h
  clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp
  clang-tools-extra/trunk/unittests/clangd/SourceCodeTests.cpp

Index: clang-tools-extra/trunk/unittests/clangd/SourceCodeTests.cpp
===
--- clang-tools-extra/trunk/unittests/clangd/SourceCodeTests.cpp
+++ clang-tools-extra/trunk/unittests/clangd/SourceCodeTests.cpp
@@ -322,6 +322,74 @@
   EXPECT_EQ(IDs["foo"], 2u);
 }
 
+TEST(SourceCodeTests, VisibleNamespaces) {
+  std::vector>> Cases = {
+  {
+  R"cpp(
+// Using directive resolved against enclosing namespaces.
+using namespace foo;
+namespace ns {
+using namespace bar;
+  )cpp",
+  {"ns", "", "bar", "foo", "ns::bar"},
+  },
+  {
+  R"cpp(
+// Don't include namespaces we've closed, ignore namespace aliases.
+using namespace clang;
+using std::swap;
+namespace clang {
+namespace clangd {}
+namespace ll = ::llvm;
+}
+namespace clang {
+  )cpp",
+  {"clang", ""},
+  },
+  {
+  R"cpp(
+// Using directives visible even if a namespace is reopened.
+// Ignore anonymous namespaces.
+namespace foo{ using namespace bar; }
+namespace foo{ namespace {
+  )cpp",
+  {"foo", "", "bar", "foo::bar"},
+  },
+  {
+  R"cpp(
+// Mismatched braces
+namespace foo{}
+}}}
+namespace bar{
+  )cpp",
+  {"bar", ""},
+  },
+  {
+  R"cpp(
+// Namespaces with multiple chunks.
+namespace a::b {
+  using namespace c::d;
+  namespace e::f {
+  )cpp",
+  {
+  "a::b::e::f",
+  "",
+  "a",
+  "a::b",
+  "a::b::c::d",
+  "a::b::e",
+  "a::c::d",
+  "c::d",
+  },
+  },
+  };
+  for (const auto& Case : Cases) {
+EXPECT_EQ(Case.second,
+  visibleNamespaces(Case.first, format::getLLVMStyle()))
+<< Case.first;
+  }
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp
===
--- clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp
+++ clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp
@@ -18,6 +18,7 @@
 #include "TestFS.h"
 #include "TestIndex.h"
 #include "TestTU.h"
+#include "index/Index.h"
 #include "index/MemIndex.h"
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "clang/Tooling/CompilationDatabase.h"
@@ -2452,6 +2453,71 @@
   UnorderedElementsAre(Named("sym1"), Named("sym2")));
 }
 
+TEST(NoCompileCompletionTest, WithIndex) {
+  std::vector Syms = {func("xxx"), func("a::xxx"), func("ns::b::xxx"),
+  func("c::xxx"), func("ns::d::xxx")};
+  auto Results = completionsNoCompile(
+  R"cpp(
+// Current-scopes, unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::";
+  CodeCompleteOptions Opts;
+  Opts.AllScopes = true;
+  Results = completionsNoCompile(
+  R"cpp(
+// All-scopes unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms, Opts);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::")),
+   AllOf(Qualifier("c::"), Scope("c::")),
+   AllOf(Qualifier("d::"), Scope("ns::d::";
+  Results = completionsNoCompile(
+  R"cpp(
+// Qualified completion.
+using namespace a;
+  

[PATCH] D61077: [clangd] Query index in code completion no-compile mode.

2019-04-26 Thread Kadir Cetinkaya via Phabricator via cfe-commits
kadircet accepted this revision.
kadircet added a comment.
This revision is now accepted and ready to land.

LGTM, thanks!




Comment at: unittests/clangd/SourceCodeTests.cpp:325
 
+TEST(SourceCodeTests, VisibleNamespaces) {
+  std::vector>> Cases = {

sammccall wrote:
> kadircet wrote:
> > NIT: maybe switch to TEST_P ?
> I find TEST_P much less readable and prefer to avoid it unless absolutely 
> necessary.
> Does it buy anything here?(
I just wanted to make sure we don't have more huge test cases as in 
`Hover.All`. I believe it would've helped if we've kept cases in small groups. 

But I guess we won't gain much here, since number of cases is not huge and I 
think there won't be many additions.


Repository:
  rCTE Clang Tools Extra

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

https://reviews.llvm.org/D61077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D61077: [clangd] Query index in code completion no-compile mode.

2019-04-25 Thread Sam McCall via Phabricator via cfe-commits
sammccall updated this revision to Diff 196676.
sammccall added a comment.

Fix test.


Repository:
  rCTE Clang Tools Extra

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

https://reviews.llvm.org/D61077

Files:
  clangd/CodeComplete.cpp
  clangd/SourceCode.cpp
  clangd/SourceCode.h
  unittests/clangd/CodeCompleteTests.cpp
  unittests/clangd/SourceCodeTests.cpp

Index: unittests/clangd/SourceCodeTests.cpp
===
--- unittests/clangd/SourceCodeTests.cpp
+++ unittests/clangd/SourceCodeTests.cpp
@@ -322,6 +322,74 @@
   EXPECT_EQ(IDs["foo"], 2u);
 }
 
+TEST(SourceCodeTests, VisibleNamespaces) {
+  std::vector>> Cases = {
+  {
+  R"cpp(
+// Using directive resolved against enclosing namespaces.
+using namespace foo;
+namespace ns {
+using namespace bar;
+  )cpp",
+  {"ns", "", "bar", "foo", "ns::bar"},
+  },
+  {
+  R"cpp(
+// Don't include namespaces we've closed, ignore namespace aliases.
+using namespace clang;
+using std::swap;
+namespace clang {
+namespace clangd {}
+namespace ll = ::llvm;
+}
+namespace clang {
+  )cpp",
+  {"clang", ""},
+  },
+  {
+  R"cpp(
+// Using directives visible even if a namespace is reopened.
+// Ignore anonymous namespaces.
+namespace foo{ using namespace bar; }
+namespace foo{ namespace {
+  )cpp",
+  {"foo", "", "bar", "foo::bar"},
+  },
+  {
+  R"cpp(
+// Mismatched braces
+namespace foo{}
+}}}
+namespace bar{
+  )cpp",
+  {"bar", ""},
+  },
+  {
+  R"cpp(
+// Namespaces with multiple chunks.
+namespace a::b {
+  using namespace c::d;
+  namespace e::f {
+  )cpp",
+  {
+  "a::b::e::f",
+  "",
+  "a",
+  "a::b",
+  "a::b::c::d",
+  "a::b::e",
+  "a::c::d",
+  "c::d",
+  },
+  },
+  };
+  for (const auto& Case : Cases) {
+EXPECT_EQ(Case.second,
+  visibleNamespaces(Case.first, format::getLLVMStyle()))
+<< Case.first;
+  }
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: unittests/clangd/CodeCompleteTests.cpp
===
--- unittests/clangd/CodeCompleteTests.cpp
+++ unittests/clangd/CodeCompleteTests.cpp
@@ -18,6 +18,7 @@
 #include "TestFS.h"
 #include "TestIndex.h"
 #include "TestTU.h"
+#include "index/Index.h"
 #include "index/MemIndex.h"
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "clang/Tooling/CompilationDatabase.h"
@@ -2452,6 +2453,71 @@
   UnorderedElementsAre(Named("sym1"), Named("sym2")));
 }
 
+TEST(NoCompileCompletionTest, WithIndex) {
+  std::vector Syms = {func("xxx"), func("a::xxx"), func("ns::b::xxx"),
+  func("c::xxx"), func("ns::d::xxx")};
+  auto Results = completionsNoCompile(
+  R"cpp(
+// Current-scopes, unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::";
+  CodeCompleteOptions Opts;
+  Opts.AllScopes = true;
+  Results = completionsNoCompile(
+  R"cpp(
+// All-scopes unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms, Opts);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::")),
+   AllOf(Qualifier("c::"), Scope("c::")),
+   AllOf(Qualifier("d::"), Scope("ns::d::";
+  Results = completionsNoCompile(
+  R"cpp(
+// Qualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+b::xx^
+}
+  )cpp",
+  Syms, Opts);
+  EXPECT_THAT(Results.Completions,
+  ElementsAre(AllOf(Qualifier(""), Scope("ns::b::";
+  Results = completionsNoCompile(
+  R"cpp(
+// Absolutely qualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() 

[PATCH] D61077: [clangd] Query index in code completion no-compile mode.

2019-04-25 Thread Sam McCall via Phabricator via cfe-commits
sammccall updated this revision to Diff 196674.
sammccall marked an inline comment as done.
sammccall added a comment.

Correctly handle absolutely qualifier (::foo::bar).
Fix seed scopes for proximity to be consistent with Sema case.


Repository:
  rCTE Clang Tools Extra

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

https://reviews.llvm.org/D61077

Files:
  clangd/CodeComplete.cpp
  clangd/SourceCode.cpp
  clangd/SourceCode.h
  unittests/clangd/CodeCompleteTests.cpp
  unittests/clangd/SourceCodeTests.cpp

Index: unittests/clangd/SourceCodeTests.cpp
===
--- unittests/clangd/SourceCodeTests.cpp
+++ unittests/clangd/SourceCodeTests.cpp
@@ -322,6 +322,74 @@
   EXPECT_EQ(IDs["foo"], 2u);
 }
 
+TEST(SourceCodeTests, VisibleNamespaces) {
+  std::vector>> Cases = {
+  {
+  R"cpp(
+// Using directive resolved against enclosing namespaces.
+using namespace foo;
+namespace ns {
+using namespace bar;
+  )cpp",
+  {"ns", "", "bar", "foo", "ns::bar"},
+  },
+  {
+  R"cpp(
+// Don't include namespaces we've closed, ignore namespace aliases.
+using namespace clang;
+using std::swap;
+namespace clang {
+namespace clangd {}
+namespace ll = ::llvm;
+}
+namespace clang {
+  )cpp",
+  {"clang", ""},
+  },
+  {
+  R"cpp(
+// Using directives visible even if a namespace is reopened.
+// Ignore anonymous namespaces.
+namespace foo{ using namespace bar; }
+namespace foo{ namespace {
+  )cpp",
+  {"foo", "", "bar", "foo::bar"},
+  },
+  {
+  R"cpp(
+// Mismatched braces
+namespace foo{}
+}}}
+namespace bar{
+  )cpp",
+  {"bar", ""},
+  },
+  {
+  R"cpp(
+// Namespaces with multiple chunks.
+namespace a::b {
+  using namespace c::d;
+  namespace e::f {
+  )cpp",
+  {
+  "a::b::e::f",
+  "",
+  "a",
+  "a::b",
+  "a::b::c::d",
+  "a::b::e",
+  "a::c::d",
+  "c::d",
+  },
+  },
+  };
+  for (const auto& Case : Cases) {
+EXPECT_EQ(Case.second,
+  visibleNamespaces(Case.first, format::getLLVMStyle()))
+<< Case.first;
+  }
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: unittests/clangd/CodeCompleteTests.cpp
===
--- unittests/clangd/CodeCompleteTests.cpp
+++ unittests/clangd/CodeCompleteTests.cpp
@@ -18,6 +18,7 @@
 #include "TestFS.h"
 #include "TestIndex.h"
 #include "TestTU.h"
+#include "index/Index.h"
 #include "index/MemIndex.h"
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "clang/Tooling/CompilationDatabase.h"
@@ -2452,6 +2453,71 @@
   UnorderedElementsAre(Named("sym1"), Named("sym2")));
 }
 
+TEST(NoCompileCompletionTest, WithIndex) {
+  std::vector Syms = {func("xxx"), func("a::xxx"), func("ns::b::xxx"),
+  func("c::xxx"), func("ns::d::xxx")};
+  auto Results = completionsNoCompile(
+  R"cpp(
+// Current-scopes, unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::";
+  CodeCompleteOptions Opts;
+  Opts.AllScopes = true;
+  Results = completionsNoCompile(
+  R"cpp(
+// All-scopes unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms, Opts);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::")),
+   AllOf(Qualifier("c::"), Scope("c::")),
+   AllOf(Qualifier("d::"), Scope("ns::d::";
+  Results = completionsNoCompile(
+  R"cpp(
+// Qualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+b::xx^
+}
+  )cpp",
+  Syms, Opts);
+  EXPECT_THAT(Results.Completions,
+  ElementsAre(AllOf(Qualifier(""), Scope("ns::b::";
+  Results = completionsNoCompile(
+  

[PATCH] D61077: [clangd] Query index in code completion no-compile mode.

2019-04-25 Thread Sam McCall via Phabricator via cfe-commits
sammccall marked 3 inline comments as done.
sammccall added inline comments.



Comment at: clangd/SourceCode.cpp:503
+case tok::l_brace:
+  if (State == NamespaceName) {
+// Parsed: namespace  {

kadircet wrote:
> I believe it is safe to ignore(just mark the opening brace) anonymous 
> namespaces here. Since there were no comments(and no test cases) just wanted 
> to make sure you did not miss that case.
Right, this was intended. Added a comment and a test.



Comment at: clangd/SourceCode.cpp:595
+  });
+  Found.erase(std::unique(Found.begin(), Found.end()), Found.end());
+  return Found;

kadircet wrote:
> `scopesForIndexQuery` already de-duplicates. Do you plan to have any other 
> users for the results of this function?
Only unit tests.
Seems a bit neater to always return canonical results, though.



Comment at: clangd/SourceCode.h:169
+/// The returned vector is always non-empty.
+/// - The first element is the namespace that encloses the point: a declaration
+///   near the point would be within this namespace.

kadircet wrote:
> Does the code ever make use of it?
This is passed into the `ScopeDistance`, and the first scope gets a quality 
boost. Added a comment.

I just noticed that getQueryScopes (not used on this codepath) only sometimes 
returns the scopes in the right order. Will fix in another patch.



Comment at: unittests/clangd/SourceCodeTests.cpp:325
 
+TEST(SourceCodeTests, VisibleNamespaces) {
+  std::vector>> Cases = {

kadircet wrote:
> NIT: maybe switch to TEST_P ?
I find TEST_P much less readable and prefer to avoid it unless absolutely 
necessary.
Does it buy anything here?(


Repository:
  rCTE Clang Tools Extra

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

https://reviews.llvm.org/D61077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D61077: [clangd] Query index in code completion no-compile mode.

2019-04-25 Thread Sam McCall via Phabricator via cfe-commits
sammccall updated this revision to Diff 196671.
sammccall marked 2 inline comments as done.
sammccall added a comment.

Add comments, add anon-namespacce test, tighten parsing rules slightly 
(`namespace ::...` is illegal)


Repository:
  rCTE Clang Tools Extra

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

https://reviews.llvm.org/D61077

Files:
  clangd/CodeComplete.cpp
  clangd/SourceCode.cpp
  clangd/SourceCode.h
  unittests/clangd/CodeCompleteTests.cpp
  unittests/clangd/SourceCodeTests.cpp

Index: unittests/clangd/SourceCodeTests.cpp
===
--- unittests/clangd/SourceCodeTests.cpp
+++ unittests/clangd/SourceCodeTests.cpp
@@ -322,6 +322,74 @@
   EXPECT_EQ(IDs["foo"], 2u);
 }
 
+TEST(SourceCodeTests, VisibleNamespaces) {
+  std::vector>> Cases = {
+  {
+  R"cpp(
+// Using directive resolved against enclosing namespaces.
+using namespace foo;
+namespace ns {
+using namespace bar;
+  )cpp",
+  {"ns", "", "bar", "foo", "ns::bar"},
+  },
+  {
+  R"cpp(
+// Don't include namespaces we've closed, ignore namespace aliases.
+using namespace clang;
+using std::swap;
+namespace clang {
+namespace clangd {}
+namespace ll = ::llvm;
+}
+namespace clang {
+  )cpp",
+  {"clang", ""},
+  },
+  {
+  R"cpp(
+// Using directives visible even if a namespace is reopened.
+// Ignore anonymous namespaces.
+namespace foo{ using namespace bar; }
+namespace foo{ namespace {
+  )cpp",
+  {"foo", "", "bar", "foo::bar"},
+  },
+  {
+  R"cpp(
+// Mismatched braces
+namespace foo{}
+}}}
+namespace bar{
+  )cpp",
+  {"bar", ""},
+  },
+  {
+  R"cpp(
+// Namespaces with multiple chunks.
+namespace a::b {
+  using namespace c::d;
+  namespace e::f {
+  )cpp",
+  {
+  "a::b::e::f",
+  "",
+  "a",
+  "a::b",
+  "a::b::c::d",
+  "a::b::e",
+  "a::c::d",
+  "c::d",
+  },
+  },
+  };
+  for (const auto& Case : Cases) {
+EXPECT_EQ(Case.second,
+  visibleNamespaces(Case.first, format::getLLVMStyle()))
+<< Case.first;
+  }
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: unittests/clangd/CodeCompleteTests.cpp
===
--- unittests/clangd/CodeCompleteTests.cpp
+++ unittests/clangd/CodeCompleteTests.cpp
@@ -18,6 +18,7 @@
 #include "TestFS.h"
 #include "TestIndex.h"
 #include "TestTU.h"
+#include "index/Index.h"
 #include "index/MemIndex.h"
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "clang/Tooling/CompilationDatabase.h"
@@ -2452,6 +2453,58 @@
   UnorderedElementsAre(Named("sym1"), Named("sym2")));
 }
 
+TEST(NoCompileCompletionTest, WithIndex) {
+  std::vector Syms = {func("xxx"), func("a::xxx"), func("ns::b::xxx"),
+  func("c::xxx"), func("ns::d::xxx")};
+  auto Results = completionsNoCompile(
+  R"cpp(
+// Current-scopes, unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::";
+  CodeCompleteOptions Opts;
+  Opts.AllScopes = true;
+  Results = completionsNoCompile(
+  R"cpp(
+// All-scopes unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms, Opts);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::")),
+   AllOf(Qualifier("c::"), Scope("c::")),
+   AllOf(Qualifier("d::"), Scope("ns::d::";
+  Results = completionsNoCompile(
+  R"cpp(
+// Qualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+b::xx^
+}
+  )cpp",
+  Syms, Opts);
+  EXPECT_THAT(Results.Completions,
+  ElementsAre(AllOf(Qualifier(""), Scope("ns::b::";
+}
+
 } // namespace
 } // namespace clangd
 } // 

[PATCH] D61077: [clangd] Query index in code completion no-compile mode.

2019-04-25 Thread Kadir Cetinkaya via Phabricator via cfe-commits
kadircet added inline comments.



Comment at: clangd/SourceCode.cpp:503
+case tok::l_brace:
+  if (State == NamespaceName) {
+// Parsed: namespace  {

I believe it is safe to ignore(just mark the opening brace) anonymous 
namespaces here. Since there were no comments(and no test cases) just wanted to 
make sure you did not miss that case.



Comment at: clangd/SourceCode.cpp:595
+  });
+  Found.erase(std::unique(Found.begin(), Found.end()), Found.end());
+  return Found;

`scopesForIndexQuery` already de-duplicates. Do you plan to have any other 
users for the results of this function?



Comment at: clangd/SourceCode.h:169
+/// The returned vector is always non-empty.
+/// - The first element is the namespace that encloses the point: a declaration
+///   near the point would be within this namespace.

Does the code ever make use of it?



Comment at: unittests/clangd/SourceCodeTests.cpp:325
 
+TEST(SourceCodeTests, VisibleNamespaces) {
+  std::vector>> Cases = {

NIT: maybe switch to TEST_P ?


Repository:
  rCTE Clang Tools Extra

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

https://reviews.llvm.org/D61077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D61077: [clangd] Query index in code completion no-compile mode.

2019-04-24 Thread Sam McCall via Phabricator via cfe-commits
sammccall created this revision.
sammccall added a reviewer: kadircet.
Herald added subscribers: cfe-commits, arphaman, mgrang, jkorous, MaskRay, 
ilya-biryukov.
Herald added a project: clang.

We scrape the enclosing scopes from the source file, and use them in the query.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D61077

Files:
  clangd/CodeComplete.cpp
  clangd/SourceCode.cpp
  clangd/SourceCode.h
  unittests/clangd/CodeCompleteTests.cpp
  unittests/clangd/SourceCodeTests.cpp

Index: unittests/clangd/SourceCodeTests.cpp
===
--- unittests/clangd/SourceCodeTests.cpp
+++ unittests/clangd/SourceCodeTests.cpp
@@ -322,6 +322,73 @@
   EXPECT_EQ(IDs["foo"], 2u);
 }
 
+TEST(SourceCodeTests, VisibleNamespaces) {
+  std::vector>> Cases = {
+  {
+  R"cpp(
+// Using directive resolved against enclosing namespaces.
+using namespace foo;
+namespace ns {
+using namespace bar;
+  )cpp",
+  {"ns", "", "bar", "foo", "ns::bar"},
+  },
+  {
+  R"cpp(
+// Don't include namespaces we've closed, ignore namespace aliases.
+using namespace clang;
+using std::swap;
+namespace clang {
+namespace clangd {}
+namespace ll = ::llvm;
+}
+namespace clang {
+  )cpp",
+  {"clang", ""},
+  },
+  {
+  R"cpp(
+// Using directives visible even if a namespace is reopened.
+namespace foo{ using namespace bar; }
+namespace foo{
+  )cpp",
+  {"foo", "", "bar", "foo::bar"},
+  },
+  {
+  R"cpp(
+// Mismatched braces
+namespace foo{}
+}}}
+namespace bar{
+  )cpp",
+  {"bar", ""},
+  },
+  {
+  R"cpp(
+// Namespaces with multiple chunks.
+namespace a::b {
+  using namespace c::d;
+  namespace e::f {
+  )cpp",
+  {
+  "a::b::e::f",
+  "",
+  "a",
+  "a::b",
+  "a::b::c::d",
+  "a::b::e",
+  "a::c::d",
+  "c::d",
+  },
+  },
+  };
+  for (const auto& Case : Cases) {
+EXPECT_EQ(Case.second,
+  visibleNamespaces(Case.first, format::getLLVMStyle()))
+<< Case.first;
+  }
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: unittests/clangd/CodeCompleteTests.cpp
===
--- unittests/clangd/CodeCompleteTests.cpp
+++ unittests/clangd/CodeCompleteTests.cpp
@@ -18,6 +18,7 @@
 #include "TestFS.h"
 #include "TestIndex.h"
 #include "TestTU.h"
+#include "index/Index.h"
 #include "index/MemIndex.h"
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "clang/Tooling/CompilationDatabase.h"
@@ -2452,6 +2453,58 @@
   UnorderedElementsAre(Named("sym1"), Named("sym2")));
 }
 
+TEST(NoCompileCompletionTest, WithIndex) {
+  std::vector Syms = {func("xxx"), func("a::xxx"), func("ns::b::xxx"),
+  func("c::xxx"), func("ns::d::xxx")};
+  auto Results = completionsNoCompile(
+  R"cpp(
+// Current-scopes, unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::";
+  CodeCompleteOptions Opts;
+  Opts.AllScopes = true;
+  Results = completionsNoCompile(
+  R"cpp(
+// All-scopes unqualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+xx^
+}
+  )cpp",
+  Syms, Opts);
+  EXPECT_THAT(Results.Completions,
+  UnorderedElementsAre(AllOf(Qualifier(""), Scope("")),
+   AllOf(Qualifier(""), Scope("a::")),
+   AllOf(Qualifier(""), Scope("ns::b::")),
+   AllOf(Qualifier("c::"), Scope("c::")),
+   AllOf(Qualifier("d::"), Scope("ns::d::";
+  Results = completionsNoCompile(
+  R"cpp(
+// Qualified completion.
+using namespace a;
+namespace ns {
+using namespace b;
+void foo() {
+b::xx^
+}
+  )cpp",
+  Syms, Opts);
+  EXPECT_THAT(Results.Completions,
+  ElementsAre(AllOf(Qualifier(""), Scope("ns::b::";
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clangd/SourceCode.h