r332457 - [CodeComplete] Expose helpers to get RawComment of completion result.

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Wed May 16 05:30:01 PDT 2018


Author: ibiryukov
Date: Wed May 16 05:30:01 2018
New Revision: 332457

URL: http://llvm.org/viewvc/llvm-project?rev=332457&view=rev
Log:
[CodeComplete] Expose helpers to get RawComment of completion result.

Summary: Used in clangd, see D45999.

Reviewers: sammccall, hokein, ioeric, arphaman

Reviewed By: sammccall

Subscribers: arphaman, cfe-commits

Differential Revision: https://reviews.llvm.org/D46001

Modified:
    cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h
    cfe/trunk/lib/Sema/SemaCodeComplete.cpp

Modified: cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h?rev=332457&r1=332456&r2=332457&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h (original)
+++ cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h Wed May 16 05:30:01 2018
@@ -46,6 +46,7 @@ class NamedDecl;
 class NestedNameSpecifier;
 class Preprocessor;
 class Sema;
+class RawComment;
 
 /// Default priority values for code-completion results based
 /// on their kind.
@@ -1073,6 +1074,23 @@ public:
   virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0;
 };
 
+/// Get the documentation comment used to produce
+/// CodeCompletionString::BriefComment for RK_Declaration.
+const RawComment *getCompletionComment(const ASTContext &Ctx,
+                                       const NamedDecl *Decl);
+
+/// Get the documentation comment used to produce
+/// CodeCompletionString::BriefComment for RK_Pattern.
+const RawComment *getPatternCompletionComment(const ASTContext &Ctx,
+                                              const NamedDecl *Decl);
+
+/// Get the documentation comment used to produce
+/// CodeCompletionString::BriefComment for OverloadCandidate.
+const RawComment *
+getParameterComment(const ASTContext &Ctx,
+                    const CodeCompleteConsumer::OverloadCandidate &Result,
+                    unsigned ArgIndex);
+
 /// A simple code-completion consumer that prints the results it
 /// receives in a simple format.
 class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {

Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=332457&r1=332456&r2=332457&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Wed May 16 05:30:01 2018
@@ -2765,27 +2765,11 @@ CodeCompletionResult::CreateCodeCompleti
     if (Declaration) {
       Result.addParentContext(Declaration->getDeclContext());
       Pattern->ParentName = Result.getParentName();
-      // Provide code completion comment for self.GetterName where
-      // GetterName is the getter method for a property with name
-      // different from the property name (declared via a property
-      // getter attribute.
-      const NamedDecl *ND = Declaration;
-      if (const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND))
-        if (M->isPropertyAccessor())
-          if (const ObjCPropertyDecl *PDecl = M->findPropertyDecl())
-            if (PDecl->getGetterName() == M->getSelector() &&
-                PDecl->getIdentifier() != M->getIdentifier()) {
-              if (const RawComment *RC = 
-                    Ctx.getRawCommentForAnyRedecl(M)) {
-                Result.addBriefComment(RC->getBriefText(Ctx));
-                Pattern->BriefComment = Result.getBriefComment();
-              }
-              else if (const RawComment *RC = 
-                         Ctx.getRawCommentForAnyRedecl(PDecl)) {
-                Result.addBriefComment(RC->getBriefText(Ctx));
-                Pattern->BriefComment = Result.getBriefComment();
-              }
-            }
+      if (const RawComment *RC =
+              getPatternCompletionComment(Ctx, Declaration)) {
+        Result.addBriefComment(RC->getBriefText(Ctx));
+        Pattern->BriefComment = Result.getBriefComment();
+      }
     }
     
     return Pattern;
@@ -2845,14 +2829,9 @@ CodeCompletionResult::CreateCodeCompleti
 
   if (IncludeBriefComments) {
     // Add documentation comment, if it exists.
-    if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) {
+    if (const RawComment *RC = getCompletionComment(Ctx, Declaration)) {
       Result.addBriefComment(RC->getBriefText(Ctx));
     } 
-    else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
-      if (OMD->isPropertyAccessor())
-        if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl())
-          if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(PDecl))
-            Result.addBriefComment(RC->getBriefText(Ctx));
   }
 
   if (StartsNestedNameSpecifier) {
@@ -3042,6 +3021,59 @@ CodeCompletionResult::CreateCodeCompleti
   return Result.TakeString();
 }
 
+const RawComment *clang::getCompletionComment(const ASTContext &Ctx,
+                                              const NamedDecl *ND) {
+  if (!ND)
+    return nullptr;
+  if (auto *RC = Ctx.getRawCommentForAnyRedecl(ND))
+    return RC;
+
+  // Try to find comment from a property for ObjC methods.
+  const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND);
+  if (!M)
+    return nullptr;
+  const ObjCPropertyDecl *PDecl = M->findPropertyDecl();
+  if (!PDecl)
+    return nullptr;
+
+  return Ctx.getRawCommentForAnyRedecl(PDecl);
+}
+
+const RawComment *clang::getPatternCompletionComment(const ASTContext &Ctx,
+                                                     const NamedDecl *ND) {
+  const ObjCMethodDecl *M = dyn_cast_or_null<ObjCMethodDecl>(ND);
+  if (!M || !M->isPropertyAccessor())
+    return nullptr;
+
+  // Provide code completion comment for self.GetterName where
+  // GetterName is the getter method for a property with name
+  // different from the property name (declared via a property
+  // getter attribute.
+  const ObjCPropertyDecl *PDecl = M->findPropertyDecl();
+  if (!PDecl)
+    return nullptr;
+  if (PDecl->getGetterName() == M->getSelector() &&
+      PDecl->getIdentifier() != M->getIdentifier()) {
+    if (auto *RC = Ctx.getRawCommentForAnyRedecl(M))
+      return RC;
+    if (auto *RC = Ctx.getRawCommentForAnyRedecl(PDecl))
+      return RC;
+  }
+  return nullptr;
+}
+
+const RawComment *clang::getParameterComment(
+    const ASTContext &Ctx,
+    const CodeCompleteConsumer::OverloadCandidate &Result,
+    unsigned ArgIndex) {
+  auto FDecl = Result.getFunction();
+  if (!FDecl)
+    return nullptr;
+  if (ArgIndex < FDecl->getNumParams())
+    return Ctx.getRawCommentForAnyRedecl(FDecl->getParamDecl(ArgIndex));
+  return nullptr;
+}
+
 /// Add function overload parameter chunks to the given code completion
 /// string.
 static void AddOverloadParameterChunks(ASTContext &Context,
@@ -3137,10 +3169,10 @@ CodeCompleteConsumer::OverloadCandidate:
   }
 
   if (FDecl) {
-    if (IncludeBriefComments && CurrentArg < FDecl->getNumParams())
-      if (auto RC = S.getASTContext().getRawCommentForAnyRedecl(
-          FDecl->getParamDecl(CurrentArg)))
+    if (IncludeBriefComments) {
+      if (auto RC = getParameterComment(S.getASTContext(), *this, CurrentArg))
         Result.addBriefComment(RC->getBriefText(S.getASTContext()));
+    }
     AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result);
     Result.AddTextChunk(
       Result.getAllocator().CopyString(FDecl->getNameAsString()));




More information about the cfe-commits mailing list