From 1a480c403a3b141ab89c9c59cf7b681102a1bfab Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 27 Aug 2010 17:35:51 +0000 Subject: [PATCH] Suggest "const" and "volatile" code completions after a function declarator, the very definition of "low-hanging fruit". git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@112274 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/CodeCompleteConsumer.h | 4 +++- include/clang/Sema/Sema.h | 1 + lib/Frontend/ASTUnit.cpp | 1 + lib/Parse/ParseDecl.cpp | 5 +++++ lib/Sema/SemaCodeComplete.cpp | 16 ++++++++++++++++ test/Index/complete-memfunc-cvquals.cpp | 8 ++++++++ 6 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/clang/Sema/CodeCompleteConsumer.h b/include/clang/Sema/CodeCompleteConsumer.h index 28210576e0..edc6331af0 100644 --- a/include/clang/Sema/CodeCompleteConsumer.h +++ b/include/clang/Sema/CodeCompleteConsumer.h @@ -201,7 +201,9 @@ public: /// unless they come from an appropriate natural-language dictionary. CCC_NaturalLanguage, /// \brief Code completion for a selector, as in an @selector expression. - CCC_SelectorName + CCC_SelectorName, + /// \brief Code completion within a type-qualifier list. + CCC_TypeQualifiers }; private: diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 43f99ae284..974381668b 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -4332,6 +4332,7 @@ public: SourceLocation OpLoc, bool IsArrow); virtual void CodeCompleteTag(Scope *S, unsigned TagSpec); + virtual void CodeCompleteTypeQualifiers(DeclSpec &DS); virtual void CodeCompleteCase(Scope *S); virtual void CodeCompleteCall(Scope *S, Expr *Fn, Expr **Args, unsigned NumArgs); diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 851c6669fe..c76488b2c6 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -1555,6 +1555,7 @@ void CalculateHiddenNames(const CodeCompletionContext &Context, case CodeCompletionContext::CCC_PreprocessorDirective: case CodeCompletionContext::CCC_NaturalLanguage: case CodeCompletionContext::CCC_SelectorName: + case CodeCompletionContext::CCC_TypeQualifiers: // We're looking for nothing, or we're looking for names that cannot // be hidden. return; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 0cc9103c05..c73b6085ea 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2442,6 +2442,11 @@ void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed, SourceLocation Loc = Tok.getLocation(); switch (Tok.getKind()) { + case tok::code_completion: + Actions.CodeCompleteTypeQualifiers(DS); + ConsumeCodeCompletionToken(); + break; + case tok::kw_const: isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, getLang()); diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 384cf524c9..57ec3297dc 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -2761,6 +2761,22 @@ void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { Results.data(),Results.size()); } +void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { + ResultBuilder Results(*this); + Results.EnterNewScope(); + if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) + Results.AddResult("const"); + if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) + Results.AddResult("volatile"); + if (getLangOptions().C99 && + !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) + Results.AddResult("restrict"); + Results.ExitScope(); + HandleCodeCompleteResults(this, CodeCompleter, + CodeCompletionContext::CCC_TypeQualifiers, + Results.data(), Results.size()); +} + void Sema::CodeCompleteCase(Scope *S) { if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) return; diff --git a/test/Index/complete-memfunc-cvquals.cpp b/test/Index/complete-memfunc-cvquals.cpp index 08acdd18c1..6635bed6ff 100644 --- a/test/Index/complete-memfunc-cvquals.cpp +++ b/test/Index/complete-memfunc-cvquals.cpp @@ -76,3 +76,11 @@ void Foo::bingo() volatile { // CHECK-IMPLICIT-VOLATILE: FunctionDecl:{ResultType void}{TypedText babble}{LeftParen (}{RightParen )}{Informative const volatile} (15) // CHECK-IMPLICIT-VOLATILE-NOT: baz // CHECK-IMPLICIT-VOLATILE: FunctionDecl:{ResultType void}{TypedText bingo}{LeftParen (}{RightParen )}{Informative volatile} (14) + +// RUN: c-index-test -code-completion-at=%s:4:17 %s | FileCheck -check-prefix=CHECK-CVQUAL-AFTER %s +// CHECK-CVQUAL-AFTER: NotImplemented:{TypedText const} (30) +// CHECK-CVQUAL-AFTER: NotImplemented:{TypedText volatile} (30) + +// RUN: c-index-test -code-completion-at=%s:4:23 %s | FileCheck -check-prefix=CHECK-CVQUAL-AFTER2 %s +// CHECK-CVQUAL-AFTER2-NOT: NotImplemented:{TypedText const} (30) +// CHECK-CVQUAL-AFTER2: NotImplemented:{TypedText volatile} (30) -- 2.40.0