[clang-tools-extra] [clang-doc] Update serializer for improved template handling (PR #138065)
Petr Hosek via cfe-commits
cfe-commits at lists.llvm.org
Mon May 26 23:29:21 PDT 2025
================
@@ -35,6 +35,169 @@ static void populateMemberTypeInfo(RecordInfo &I, AccessSpecifier &Access,
const DeclaratorDecl *D,
bool IsStatic = false);
+static void getTemplateParameters(const TemplateParameterList *TemplateParams,
+ llvm::raw_ostream &Stream) {
+ Stream << "template <";
+
+ for (unsigned i = 0; i < TemplateParams->size(); ++i) {
+ if (i > 0)
+ Stream << ", ";
+
+ const NamedDecl *Param = TemplateParams->getParam(i);
+ if (const auto *TTP = llvm::dyn_cast<TemplateTypeParmDecl>(Param)) {
+ if (TTP->wasDeclaredWithTypename())
+ Stream << "typename";
+ else
+ Stream << "class";
+ if (TTP->isParameterPack())
+ Stream << "...";
+ Stream << " " << TTP->getNameAsString();
+ } else if (const auto *NTTP =
+ llvm::dyn_cast<NonTypeTemplateParmDecl>(Param)) {
+ NTTP->getType().print(Stream, NTTP->getASTContext().getPrintingPolicy());
+ if (NTTP->isParameterPack())
+ Stream << "...";
+ Stream << " " << NTTP->getNameAsString();
+ } else if (const auto *TTPD =
+ llvm::dyn_cast<TemplateTemplateParmDecl>(Param)) {
+ Stream << "template <";
+ getTemplateParameters(TTPD->getTemplateParameters(), Stream);
+ Stream << "> class " << TTPD->getNameAsString();
+ }
+ }
+
+ Stream << "> ";
+}
+
+// Extract the full function prototype from a FunctionDecl including
+// Full Decl
+static llvm::SmallString<256>
+getFunctionPrototype(const FunctionDecl *FuncDecl) {
+ llvm::SmallString<256> Result;
+ llvm::raw_svector_ostream Stream(Result);
+ const ASTContext &Ctx = FuncDecl->getASTContext();
+ const auto *Method = llvm::dyn_cast<CXXMethodDecl>(FuncDecl);
+ // If it's a templated function, handle the template parameters
+ if (const auto *TmplDecl = FuncDecl->getDescribedTemplate())
+ getTemplateParameters(TmplDecl->getTemplateParameters(), Stream);
+
+ // If it's a virtual method
+ if (Method && Method->isVirtual())
+ Stream << "virtual ";
+
+ // Print return type
+ FuncDecl->getReturnType().print(Stream, Ctx.getPrintingPolicy());
+
+ // Print function name
+ Stream << " " << FuncDecl->getNameAsString() << "(";
+
+ // Print parameter list with types, names, and default values
+ for (unsigned I = 0; I < FuncDecl->getNumParams(); ++I) {
+ if (I > 0)
+ Stream << ", ";
+ const ParmVarDecl *ParamDecl = FuncDecl->getParamDecl(I);
+ QualType ParamType = ParamDecl->getType();
+ ParamType.print(Stream, Ctx.getPrintingPolicy());
+
+ // Print parameter name if it has one
+ if (!ParamDecl->getName().empty())
+ Stream << " " << ParamDecl->getNameAsString();
+
+ // Print default argument if it exists
+ if (ParamDecl->hasDefaultArg()) {
+ const Expr *DefaultArg = ParamDecl->getDefaultArg();
+ if (DefaultArg) {
+ Stream << " = ";
+ DefaultArg->printPretty(Stream, nullptr, Ctx.getPrintingPolicy());
+ }
+ }
+ }
+
+ // If it is a variadic function, add '...'
+ if (FuncDecl->isVariadic()) {
+ if (FuncDecl->getNumParams() > 0)
+ Stream << ", ";
+ Stream << "...";
+ }
+
+ Stream << ")";
+
+ // If it's a const method, add 'const' qualifier
+ if (Method) {
----------------
petrhosek wrote:
This also needs to handle `= delete`.
https://github.com/llvm/llvm-project/pull/138065
More information about the cfe-commits
mailing list