[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
Wed Nov 29 06:33:28 PST 2017


ioeric updated this revision to Diff 124740.
ioeric added a comment.

- Clarify comment for includeGlobals()


https://reviews.llvm.org/D40562

Files:
  include/clang/Sema/CodeCompleteConsumer.h
  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, 
Index: include/clang/Sema/CodeCompleteConsumer.h
===================================================================
--- include/clang/Sema/CodeCompleteConsumer.h
+++ include/clang/Sema/CodeCompleteConsumer.h
@@ -901,10 +901,9 @@
     return CodeCompleteOpts.IncludeCodePatterns;
   }
 
-  /// \brief Whether to include global (top-level) declaration results.
-  bool includeGlobals() const {
-    return CodeCompleteOpts.IncludeGlobals;
-  }
+  /// \brief Whether to include global (top-level) declaration results. These
+  /// includes declarations in namespaces or translation units.
+  bool includeGlobals() const { return CodeCompleteOpts.IncludeGlobals; }
 
   /// \brief Whether to include brief documentation comments within the set of
   /// code completions returned.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40562.124740.patch
Type: text/x-patch
Size: 2878 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171129/31e9cf66/attachment.bin>


More information about the cfe-commits mailing list