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. 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: 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: 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: {{^}}{{$}} +}