[PATCH] D40562: [Sema] Ignore decls in namespaces when global decls are not wanted.

Eric Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 28 08:28:51 PST 2017


ioeric created this revision.

... in qualified code completion and decl lookup.


https://reviews.llvm.org/D40562

Files:
  lib/Sema/SemaCodeComplete.cpp
  lib/Sema/SemaLookup.cpp
  test/CodeCompletion/ignore-global-decls.cpp


Index: test/CodeCompletion/ignore-global-decls.cpp
===================================================================
--- /dev/null
+++ test/CodeCompletion/ignore-global-decls.cpp
@@ -0,0 +1,20 @@
+namespace ns {
+  struct bar {
+  };
+
+  struct baz {
+  };
+
+  int func(int a, bar b, baz c);
+}
+
+void test() {
+  ns::
+// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:12:7 %s -o - | FileCheck %s --check-prefix=CHECK-1
+// CHECK-1-DAG: COMPLETION: bar : bar
+// CHECK-1-DAG: COMPLETION: baz : baz
+// CHECK-1-DAG: COMPLETION: func : [#int#]func(<#int a#>, <#bar b#>, <#baz c#>)
+
+// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:12:7 -no-code-completion-globals %s -o - | FileCheck %s -allow-empty --check-prefix=CHECK-EMPTY
+// CHECK-EMPTY: {{^}}{{$}}
+}
Index: lib/Sema/SemaLookup.cpp
===================================================================
--- lib/Sema/SemaLookup.cpp
+++ lib/Sema/SemaLookup.cpp
@@ -3785,8 +3785,12 @@
   LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind);
   Result.setAllowHidden(Consumer.includeHiddenDecls());
   VisibleDeclsRecord Visited;
-  if (!IncludeGlobalScope)
+  if (!IncludeGlobalScope) {
     Visited.visitedContext(Context.getTranslationUnitDecl());
+    // Declarations in namespace are also considered global.
+    if (Ctx->isNamespace() || Ctx->isTranslationUnit())
+      Visited.visitedContext(Ctx);
+  }
   ShadowContextRAII Shadow(Visited);
   ::LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/true,
                        /*InBaseClass=*/false, Consumer, Visited,
Index: lib/Sema/SemaCodeComplete.cpp
===================================================================
--- lib/Sema/SemaCodeComplete.cpp
+++ lib/Sema/SemaCodeComplete.cpp
@@ -4639,7 +4639,7 @@
   
   CodeCompletionDeclConsumer Consumer(Results, CurContext);
   LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer,
-                     /*IncludeGlobalScope=*/true,
+                     CodeCompleter->includeGlobals(),
                      /*IncludeDependentBases=*/true);
 
   HandleCodeCompleteResults(this, CodeCompleter, 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40562.124582.patch
Type: text/x-patch
Size: 2100 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171128/fbc47b3a/attachment.bin>


More information about the cfe-commits mailing list