From ff5ce6eefc7c253ef6edf4d4bfc996fdd82d09aa Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 18 Dec 2009 18:53:37 +0000 Subject: Extend code-completion results with the type of each result git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91702 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaCodeComplete.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib/Sema/SemaCodeComplete.cpp') diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 6b485ba7a9..1391adfd48 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -829,6 +829,39 @@ static void AddTypeSpecifierResults(const LangOptions &LangOpts, unsigned Rank, } } +/// \brief If the given declaration has an associated type, add it as a result +/// type chunk. +static void AddResultTypeChunk(ASTContext &Context, + NamedDecl *ND, + CodeCompletionString *Result) { + if (!ND) + return; + + // Determine the type of the declaration (if it has a type). + QualType T; + if (FunctionDecl *Function = dyn_cast(ND)) + T = Function->getResultType(); + else if (ObjCMethodDecl *Method = dyn_cast(ND)) + T = Method->getResultType(); + else if (FunctionTemplateDecl *FunTmpl = dyn_cast(ND)) + T = FunTmpl->getTemplatedDecl()->getResultType(); + else if (EnumConstantDecl *Enumerator = dyn_cast(ND)) + T = Context.getTypeDeclType(cast(Enumerator->getDeclContext())); + else if (isa(ND)) { + /* Do nothing: ignore unresolved using declarations*/ + } else if (ValueDecl *Value = dyn_cast(ND)) + T = Value->getType(); + else if (ObjCPropertyDecl *Property = dyn_cast(ND)) + T = Property->getType(); + + if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) + return; + + std::string TypeStr; + T.getAsStringInternal(TypeStr, Context.PrintingPolicy); + Result->AddResultTypeChunk(TypeStr); +} + /// \brief Add function parameter chunks to the given code completion string. static void AddFunctionParameterChunks(ASTContext &Context, FunctionDecl *Function, @@ -1042,6 +1075,8 @@ CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) { return Result; } + AddResultTypeChunk(S.Context, ND, Result); + if (FunctionDecl *Function = dyn_cast(ND)) { AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, S.Context); @@ -1189,6 +1224,7 @@ CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( CodeCompletionString *Result = new CodeCompletionString; FunctionDecl *FDecl = getFunction(); + AddResultTypeChunk(S.Context, FDecl, Result); const FunctionProtoType *Proto = dyn_cast(getFunctionType()); if (!FDecl && !Proto) { -- cgit v1.2.3