Skip to content

Commit a3f955b

Browse files
committed
[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 llvm-svn: 332457
1 parent d67ec24 commit a3f955b

File tree

2 files changed

+80
-30
lines changed

2 files changed

+80
-30
lines changed

clang/include/clang/Sema/CodeCompleteConsumer.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class NamedDecl;
4646
class NestedNameSpecifier;
4747
class Preprocessor;
4848
class Sema;
49+
class RawComment;
4950

5051
/// Default priority values for code-completion results based
5152
/// on their kind.
@@ -1073,6 +1074,23 @@ class CodeCompleteConsumer {
10731074
virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0;
10741075
};
10751076

1077+
/// Get the documentation comment used to produce
1078+
/// CodeCompletionString::BriefComment for RK_Declaration.
1079+
const RawComment *getCompletionComment(const ASTContext &Ctx,
1080+
const NamedDecl *Decl);
1081+
1082+
/// Get the documentation comment used to produce
1083+
/// CodeCompletionString::BriefComment for RK_Pattern.
1084+
const RawComment *getPatternCompletionComment(const ASTContext &Ctx,
1085+
const NamedDecl *Decl);
1086+
1087+
/// Get the documentation comment used to produce
1088+
/// CodeCompletionString::BriefComment for OverloadCandidate.
1089+
const RawComment *
1090+
getParameterComment(const ASTContext &Ctx,
1091+
const CodeCompleteConsumer::OverloadCandidate &Result,
1092+
unsigned ArgIndex);
1093+
10761094
/// A simple code-completion consumer that prints the results it
10771095
/// receives in a simple format.
10781096
class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,27 +2765,11 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
27652765
if (Declaration) {
27662766
Result.addParentContext(Declaration->getDeclContext());
27672767
Pattern->ParentName = Result.getParentName();
2768-
// Provide code completion comment for self.GetterName where
2769-
// GetterName is the getter method for a property with name
2770-
// different from the property name (declared via a property
2771-
// getter attribute.
2772-
const NamedDecl *ND = Declaration;
2773-
if (const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND))
2774-
if (M->isPropertyAccessor())
2775-
if (const ObjCPropertyDecl *PDecl = M->findPropertyDecl())
2776-
if (PDecl->getGetterName() == M->getSelector() &&
2777-
PDecl->getIdentifier() != M->getIdentifier()) {
2778-
if (const RawComment *RC =
2779-
Ctx.getRawCommentForAnyRedecl(M)) {
2780-
Result.addBriefComment(RC->getBriefText(Ctx));
2781-
Pattern->BriefComment = Result.getBriefComment();
2782-
}
2783-
else if (const RawComment *RC =
2784-
Ctx.getRawCommentForAnyRedecl(PDecl)) {
2785-
Result.addBriefComment(RC->getBriefText(Ctx));
2786-
Pattern->BriefComment = Result.getBriefComment();
2787-
}
2788-
}
2768+
if (const RawComment *RC =
2769+
getPatternCompletionComment(Ctx, Declaration)) {
2770+
Result.addBriefComment(RC->getBriefText(Ctx));
2771+
Pattern->BriefComment = Result.getBriefComment();
2772+
}
27892773
}
27902774

27912775
return Pattern;
@@ -2845,14 +2829,9 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
28452829

28462830
if (IncludeBriefComments) {
28472831
// Add documentation comment, if it exists.
2848-
if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) {
2832+
if (const RawComment *RC = getCompletionComment(Ctx, Declaration)) {
28492833
Result.addBriefComment(RC->getBriefText(Ctx));
28502834
}
2851-
else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
2852-
if (OMD->isPropertyAccessor())
2853-
if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl())
2854-
if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(PDecl))
2855-
Result.addBriefComment(RC->getBriefText(Ctx));
28562835
}
28572836

28582837
if (StartsNestedNameSpecifier) {
@@ -3042,6 +3021,59 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
30423021
return Result.TakeString();
30433022
}
30443023

3024+
const RawComment *clang::getCompletionComment(const ASTContext &Ctx,
3025+
const NamedDecl *ND) {
3026+
if (!ND)
3027+
return nullptr;
3028+
if (auto *RC = Ctx.getRawCommentForAnyRedecl(ND))
3029+
return RC;
3030+
3031+
// Try to find comment from a property for ObjC methods.
3032+
const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND);
3033+
if (!M)
3034+
return nullptr;
3035+
const ObjCPropertyDecl *PDecl = M->findPropertyDecl();
3036+
if (!PDecl)
3037+
return nullptr;
3038+
3039+
return Ctx.getRawCommentForAnyRedecl(PDecl);
3040+
}
3041+
3042+
const RawComment *clang::getPatternCompletionComment(const ASTContext &Ctx,
3043+
const NamedDecl *ND) {
3044+
const ObjCMethodDecl *M = dyn_cast_or_null<ObjCMethodDecl>(ND);
3045+
if (!M || !M->isPropertyAccessor())
3046+
return nullptr;
3047+
3048+
// Provide code completion comment for self.GetterName where
3049+
// GetterName is the getter method for a property with name
3050+
// different from the property name (declared via a property
3051+
// getter attribute.
3052+
const ObjCPropertyDecl *PDecl = M->findPropertyDecl();
3053+
if (!PDecl)
3054+
return nullptr;
3055+
if (PDecl->getGetterName() == M->getSelector() &&
3056+
PDecl->getIdentifier() != M->getIdentifier()) {
3057+
if (auto *RC = Ctx.getRawCommentForAnyRedecl(M))
3058+
return RC;
3059+
if (auto *RC = Ctx.getRawCommentForAnyRedecl(PDecl))
3060+
return RC;
3061+
}
3062+
return nullptr;
3063+
}
3064+
3065+
const RawComment *clang::getParameterComment(
3066+
const ASTContext &Ctx,
3067+
const CodeCompleteConsumer::OverloadCandidate &Result,
3068+
unsigned ArgIndex) {
3069+
auto FDecl = Result.getFunction();
3070+
if (!FDecl)
3071+
return nullptr;
3072+
if (ArgIndex < FDecl->getNumParams())
3073+
return Ctx.getRawCommentForAnyRedecl(FDecl->getParamDecl(ArgIndex));
3074+
return nullptr;
3075+
}
3076+
30453077
/// Add function overload parameter chunks to the given code completion
30463078
/// string.
30473079
static void AddOverloadParameterChunks(ASTContext &Context,
@@ -3137,10 +3169,10 @@ CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
31373169
}
31383170

31393171
if (FDecl) {
3140-
if (IncludeBriefComments && CurrentArg < FDecl->getNumParams())
3141-
if (auto RC = S.getASTContext().getRawCommentForAnyRedecl(
3142-
FDecl->getParamDecl(CurrentArg)))
3172+
if (IncludeBriefComments) {
3173+
if (auto RC = getParameterComment(S.getASTContext(), *this, CurrentArg))
31433174
Result.addBriefComment(RC->getBriefText(S.getASTContext()));
3175+
}
31443176
AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result);
31453177
Result.AddTextChunk(
31463178
Result.getAllocator().CopyString(FDecl->getNameAsString()));

0 commit comments

Comments
 (0)