From 51d76e9ec38a972a7cdf883ef4f24de3a4cd4dcd Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Wed, 19 Sep 2018 09:34:55 +0000 Subject: [PATCH] [Sema] Do not load macros from preamble when LoadExternal is false. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D52079 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342528 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/CodeCompleteOptions.h | 3 ++- lib/Sema/SemaCodeComplete.cpp | 29 +++++++++++++----------- test/Index/complete-pch-skip.cpp | 9 +++++++- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/include/clang/Sema/CodeCompleteOptions.h b/include/clang/Sema/CodeCompleteOptions.h index 1d3bbb4e58..26f7f9d19f 100644 --- a/include/clang/Sema/CodeCompleteOptions.h +++ b/include/clang/Sema/CodeCompleteOptions.h @@ -36,7 +36,8 @@ public: unsigned IncludeBriefComments : 1; /// Hint whether to load data from the external AST to provide full results. - /// If false, namespace-level declarations from the preamble may be omitted. + /// If false, namespace-level declarations and macros from the preamble may be + /// omitted. unsigned LoadExternal : 1; /// Include results after corrections (small fix-its), e.g. change '.' to '->' diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 94dd75bfe9..acf43650bb 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -3304,14 +3304,14 @@ CXCursorKind clang::getCursorKindForDecl(const Decl *D) { } static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, - bool IncludeUndefined, + bool LoadExternal, bool IncludeUndefined, bool TargetTypeIsPointer = false) { typedef CodeCompletionResult Result; Results.EnterNewScope(); - for (Preprocessor::macro_iterator M = PP.macro_begin(), - MEnd = PP.macro_end(); + for (Preprocessor::macro_iterator M = PP.macro_begin(LoadExternal), + MEnd = PP.macro_end(LoadExternal); M != MEnd; ++M) { auto MD = PP.getMacroDefinition(M->first); if (IncludeUndefined || MD) { @@ -3327,7 +3327,6 @@ static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, } Results.ExitScope(); - } static void AddPrettyFunctionResults(const LangOptions &LangOpts, @@ -3611,7 +3610,7 @@ void Sema::CodeCompleteOrdinaryName(Scope *S, } if (CodeCompleter->includeMacros()) - AddMacroResults(PP, Results, false); + AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), Results.data(),Results.size()); @@ -3749,7 +3748,8 @@ void Sema::CodeCompleteExpression(Scope *S, AddPrettyFunctionResults(getLangOpts(), Results); if (CodeCompleter->includeMacros()) - AddMacroResults(PP, Results, false, PreferredTypeIsPointer); + AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false, + PreferredTypeIsPointer); HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), Results.data(), Results.size()); } @@ -4372,7 +4372,7 @@ void Sema::CodeCompleteCase(Scope *S) { Results.ExitScope(); if (CodeCompleter->includeMacros()) { - AddMacroResults(PP, Results, false); + AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); } HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), Results.data(), Results.size()); @@ -4688,7 +4688,7 @@ void Sema::CodeCompleteAfterIf(Scope *S) { AddPrettyFunctionResults(getLangOpts(), Results); if (CodeCompleter->includeMacros()) - AddMacroResults(PP, Results, false); + AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), Results.data(),Results.size()); @@ -5722,7 +5722,7 @@ void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, CodeCompleter->loadExternal()); if (CodeCompleter->includeMacros()) - AddMacroResults(PP, Results, false); + AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), Results.data(), Results.size()); @@ -5951,10 +5951,9 @@ void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { Results.ExitScope(); if (CodeCompleter->includeMacros()) - AddMacroResults(PP, Results, false); + AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), Results.data(), Results.size()); - } void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, @@ -7967,7 +7966,9 @@ void Sema::CodeCompletePreprocessorExpression() { CodeCompletionContext::CCC_PreprocessorExpression); if (!CodeCompleter || CodeCompleter->includeMacros()) - AddMacroResults(PP, Results, true); + AddMacroResults(PP, Results, + CodeCompleter ? CodeCompleter->loadExternal() : false, + true); // defined () Results.EnterNewScope(); @@ -8141,7 +8142,9 @@ void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, } if (!CodeCompleter || CodeCompleter->includeMacros()) - AddMacroResults(PP, Builder, true); + AddMacroResults(PP, Builder, + CodeCompleter ? CodeCompleter->loadExternal() : false, + true); Results.clear(); Results.insert(Results.end(), diff --git a/test/Index/complete-pch-skip.cpp b/test/Index/complete-pch-skip.cpp index 6abf40df36..38ba228ba2 100644 --- a/test/Index/complete-pch-skip.cpp +++ b/test/Index/complete-pch-skip.cpp @@ -4,19 +4,26 @@ int bar; int main() { return ns:: } int main2() { return ns::foo(). } +int main3() { PREAMBLE_ } -// RUN: echo "namespace ns { struct foo { int baz }; }" > %t.h +// RUN: printf "namespace ns { struct foo { int baz }; }\n#define PREAMBLE_MAC" > %t.h // RUN: c-index-test -write-pch %t.h.pch -x c++-header %t.h // // RUN: c-index-test -code-completion-at=%s:5:26 -include %t.h %s | FileCheck -check-prefix=WITH-PCH %s // WITH-PCH: {TypedText bar} // WITH-PCH: {TypedText foo} +// RUN: c-index-test -code-completion-at=%s:7:24 -include %t.h %s | FileCheck -check-prefix=WITH-PCH-MACRO %s +// WITH-PCH-MACRO: {TypedText PREAMBLE_MAC} + // RUN: env CINDEXTEST_COMPLETION_SKIP_PREAMBLE=1 c-index-test -code-completion-at=%s:5:26 -include %t.h %s | FileCheck -check-prefix=SKIP-PCH %s // SKIP-PCH-NOT: foo // SKIP-PCH: {TypedText bar} // SKIP-PCH-NOT: foo +// RUN: env CINDEXTEST_COMPLETION_SKIP_PREAMBLE=1 c-index-test -code-completion-at=%s:7:24 -include %t.h %s | FileCheck -check-prefix=SKIP-PCH-MACRO %s +// SKIP-PCH-MACRO-NOT: {TypedText PREAMBLE_MAC} + // Verify that with *no* preamble (no -include flag) we still get local results. // SkipPreamble used to break this, by making lookup *too* lazy. // RUN: env CINDEXTEST_COMPLETION_SKIP_PREAMBLE=1 c-index-test -code-completion-at=%s:5:26 %s | FileCheck -check-prefix=NO-PCH %s -- 2.40.0