88
99#include " Serialize.h"
1010#include " BitcodeWriter.h"
11+ #include " clang/AST/Attr.h"
1112#include " clang/AST/Comment.h"
1213#include " clang/Index/USRGeneration.h"
1314#include " clang/Lex/Lexer.h"
14- #include " llvm/ADT/Hashing.h"
1515#include " llvm/ADT/StringExtras.h"
1616#include " llvm/Support/SHA1.h"
1717
@@ -35,6 +35,169 @@ static void populateMemberTypeInfo(RecordInfo &I, AccessSpecifier &Access,
3535 const DeclaratorDecl *D,
3636 bool IsStatic = false );
3737
38+ static void getTemplateParameters (const TemplateParameterList *TemplateParams,
39+ llvm::raw_ostream &Stream) {
40+ Stream << " template <" ;
41+
42+ for (unsigned i = 0 ; i < TemplateParams->size (); ++i) {
43+ if (i > 0 )
44+ Stream << " , " ;
45+
46+ const NamedDecl *Param = TemplateParams->getParam (i);
47+ if (const auto *TTP = llvm::dyn_cast<TemplateTypeParmDecl>(Param)) {
48+ if (TTP->wasDeclaredWithTypename ())
49+ Stream << " typename" ;
50+ else
51+ Stream << " class" ;
52+ if (TTP->isParameterPack ())
53+ Stream << " ..." ;
54+ Stream << " " << TTP->getNameAsString ();
55+ } else if (const auto *NTTP =
56+ llvm::dyn_cast<NonTypeTemplateParmDecl>(Param)) {
57+ NTTP->getType ().print (Stream, NTTP->getASTContext ().getPrintingPolicy ());
58+ if (NTTP->isParameterPack ())
59+ Stream << " ..." ;
60+ Stream << " " << NTTP->getNameAsString ();
61+ } else if (const auto *TTPD =
62+ llvm::dyn_cast<TemplateTemplateParmDecl>(Param)) {
63+ Stream << " template <" ;
64+ getTemplateParameters (TTPD->getTemplateParameters (), Stream);
65+ Stream << " > class " << TTPD->getNameAsString ();
66+ }
67+ }
68+
69+ Stream << " > " ;
70+ }
71+
72+ // Extract the full function prototype from a FunctionDecl including
73+ // Full Decl
74+ static llvm::SmallString<256 >
75+ getFunctionPrototype (const FunctionDecl *FuncDecl) {
76+ llvm::SmallString<256 > Result;
77+ llvm::raw_svector_ostream Stream (Result);
78+ const ASTContext &Ctx = FuncDecl->getASTContext ();
79+ const auto *Method = llvm::dyn_cast<CXXMethodDecl>(FuncDecl);
80+ // If it's a templated function, handle the template parameters
81+ if (const auto *TmplDecl = FuncDecl->getDescribedTemplate ())
82+ getTemplateParameters (TmplDecl->getTemplateParameters (), Stream);
83+
84+ // If it's a virtual method
85+ if (Method && Method->isVirtual ())
86+ Stream << " virtual " ;
87+
88+ // Print return type
89+ FuncDecl->getReturnType ().print (Stream, Ctx.getPrintingPolicy ());
90+
91+ // Print function name
92+ Stream << " " << FuncDecl->getNameAsString () << " (" ;
93+
94+ // Print parameter list with types, names, and default values
95+ for (unsigned I = 0 ; I < FuncDecl->getNumParams (); ++I) {
96+ if (I > 0 )
97+ Stream << " , " ;
98+ const ParmVarDecl *ParamDecl = FuncDecl->getParamDecl (I);
99+ QualType ParamType = ParamDecl->getType ();
100+ ParamType.print (Stream, Ctx.getPrintingPolicy ());
101+
102+ // Print parameter name if it has one
103+ if (!ParamDecl->getName ().empty ())
104+ Stream << " " << ParamDecl->getNameAsString ();
105+
106+ // Print default argument if it exists
107+ if (ParamDecl->hasDefaultArg ()) {
108+ const Expr *DefaultArg = ParamDecl->getDefaultArg ();
109+ if (DefaultArg) {
110+ Stream << " = " ;
111+ DefaultArg->printPretty (Stream, nullptr , Ctx.getPrintingPolicy ());
112+ }
113+ }
114+ }
115+
116+ // If it is a variadic function, add '...'
117+ if (FuncDecl->isVariadic ()) {
118+ if (FuncDecl->getNumParams () > 0 )
119+ Stream << " , " ;
120+ Stream << " ..." ;
121+ }
122+
123+ Stream << " )" ;
124+
125+ // If it's a const method, add 'const' qualifier
126+ if (Method) {
127+ if (Method->size_overridden_methods ())
128+ Stream << " override" ;
129+ if (Method->hasAttr <clang::FinalAttr>())
130+ Stream << " final" ;
131+ if (Method->isConst ())
132+ Stream << " const" ;
133+ if (Method->isPureVirtual ())
134+ Stream << " = 0" ;
135+ }
136+ return Result; // Convert SmallString to std::string for return
137+ }
138+
139+ static llvm::SmallString<16 > getTypeAlias (const TypeAliasDecl *Alias) {
140+ llvm::SmallString<16 > Result;
141+ llvm::raw_svector_ostream Stream (Result);
142+ const ASTContext &Ctx = Alias->getASTContext ();
143+ if (const auto *TmplDecl = Alias->getDescribedTemplate ())
144+ getTemplateParameters (TmplDecl->getTemplateParameters (), Stream);
145+ Stream << " using " << Alias->getNameAsString () << " = " ;
146+ QualType Q = Alias->getUnderlyingType ();
147+ Q.print (Stream, Ctx.getPrintingPolicy ());
148+
149+ return Result;
150+ }
151+
152+ // extract full syntax for record declaration
153+ static llvm::SmallString<16 > getRecordPrototype (const CXXRecordDecl *CXXRD) {
154+ llvm::SmallString<16 > Result;
155+ LangOptions LangOpts;
156+ PrintingPolicy Policy (LangOpts);
157+ Policy.SuppressTagKeyword = false ;
158+ Policy.FullyQualifiedName = true ;
159+ Policy.IncludeNewlines = false ;
160+ llvm::raw_svector_ostream OS (Result);
161+ if (const auto *TD = CXXRD->getDescribedClassTemplate ()) {
162+ OS << " template <" ;
163+ bool FirstParam = true ;
164+ for (const auto *Param : *TD->getTemplateParameters ()) {
165+ if (!FirstParam)
166+ OS << " , " ;
167+ Param->print (OS, Policy);
168+ FirstParam = false ;
169+ }
170+ OS << " >\n " ;
171+ }
172+
173+ if (CXXRD->isStruct ())
174+ OS << " struct " ;
175+ else if (CXXRD->isClass ())
176+ OS << " class " ;
177+ else if (CXXRD->isUnion ())
178+ OS << " union " ;
179+
180+ OS << CXXRD->getNameAsString ();
181+
182+ // We need to make sure we have a good enough declaration to check. In the
183+ // case where the class is a forward declaration, we'll fail assertions in
184+ // DeclCXX.
185+ if (CXXRD->isCompleteDefinition () && CXXRD->getNumBases () > 0 ) {
186+ OS << " : " ;
187+ bool FirstBase = true ;
188+ for (const auto &Base : CXXRD->bases ()) {
189+ if (!FirstBase)
190+ OS << " , " ;
191+ if (Base.isVirtual ())
192+ OS << " virtual " ;
193+ OS << getAccessSpelling (Base.getAccessSpecifier ()) << " " ;
194+ OS << Base.getType ().getAsString (Policy);
195+ FirstBase = false ;
196+ }
197+ }
198+ return Result;
199+ }
200+
38201// A function to extract the appropriate relative path for a given info's
39202// documentation. The path returned is a composite of the parent namespaces.
40203//
@@ -408,7 +571,6 @@ static void parseEnumerators(EnumInfo &I, const EnumDecl *D) {
408571 ASTContext &Context = E->getASTContext ();
409572 if (RawComment *Comment =
410573 E->getASTContext ().getRawCommentForDeclNoCache (E)) {
411- CommentInfo CInfo;
412574 Comment->setAttached ();
413575 if (comments::FullComment *Fc = Comment->parse (Context, nullptr , E)) {
414576 EnumValueInfo &Member = I.Members .back ();
@@ -434,6 +596,7 @@ static void parseBases(RecordInfo &I, const CXXRecordDecl *D) {
434596 // Don't parse bases if this isn't a definition.
435597 if (!D->isThisDeclarationADefinition ())
436598 return ;
599+
437600 for (const CXXBaseSpecifier &B : D->bases ()) {
438601 if (B.isVirtual ())
439602 continue ;
@@ -555,6 +718,7 @@ static void populateFunctionInfo(FunctionInfo &I, const FunctionDecl *D,
555718 populateSymbolInfo (I, D, FC, Loc, IsInAnonymousNamespace);
556719 auto &LO = D->getLangOpts ();
557720 I.ReturnType = getTypeInfoForType (D->getReturnType (), LO);
721+ I.Prototype = getFunctionPrototype (D);
558722 parseParameters (I, D);
559723
560724 populateTemplateParameters (I.Template , D);
@@ -686,15 +850,19 @@ emitInfo(const NamespaceDecl *D, const FullComment *FC, Location Loc,
686850std::pair<std::unique_ptr<Info>, std::unique_ptr<Info>>
687851emitInfo (const RecordDecl *D, const FullComment *FC, Location Loc,
688852 bool PublicOnly) {
853+
689854 auto RI = std::make_unique<RecordInfo>();
690855 bool IsInAnonymousNamespace = false ;
856+
691857 populateSymbolInfo (*RI, D, FC, Loc, IsInAnonymousNamespace);
692858 if (!shouldSerializeInfo (PublicOnly, IsInAnonymousNamespace, D))
693859 return {};
694860
695861 RI->TagType = D->getTagKind ();
696862 parseFields (*RI, D, PublicOnly);
863+
697864 if (const auto *C = dyn_cast<CXXRecordDecl>(D)) {
865+ RI->FullName = getRecordPrototype (C);
698866 if (const TypedefNameDecl *TD = C->getTypedefNameForAnonDecl ()) {
699867 RI->Name = TD->getNameAsString ();
700868 RI->IsTypeDef = true ;
@@ -716,11 +884,11 @@ emitInfo(const RecordDecl *D, const FullComment *FC, Location Loc,
716884
717885 // What this is a specialization of.
718886 auto SpecOf = CTSD->getSpecializedTemplateOrPartial ();
719- if (auto *CTD = dyn_cast<ClassTemplateDecl *>(SpecOf))
720- Specialization.SpecializationOf = getUSRForDecl (CTD );
721- else if (auto *CTPSD =
887+ if (auto *SpecTD = dyn_cast<ClassTemplateDecl *>(SpecOf))
888+ Specialization.SpecializationOf = getUSRForDecl (SpecTD );
889+ else if (auto *SpecTD =
722890 dyn_cast<ClassTemplatePartialSpecializationDecl *>(SpecOf))
723- Specialization.SpecializationOf = getUSRForDecl (CTPSD );
891+ Specialization.SpecializationOf = getUSRForDecl (SpecTD );
724892
725893 // Parameters to the specialization. For partial specializations, get the
726894 // parameters "as written" from the ClassTemplatePartialSpecializationDecl
@@ -792,25 +960,42 @@ emitInfo(const CXXMethodDecl *D, const FullComment *FC, Location Loc,
792960 return {nullptr , makeAndInsertIntoParent<FunctionInfo &&>(std::move (Func))};
793961}
794962
963+ static void extractCommentFromDecl (const Decl *D, TypedefInfo &Info) {
964+ assert (D && " Invalid Decl when extracting comment" );
965+ ASTContext &Context = D->getASTContext ();
966+ RawComment *Comment = Context.getRawCommentForDeclNoCache (D);
967+ if (!Comment)
968+ return ;
969+
970+ Comment->setAttached ();
971+ if (comments::FullComment *Fc = Comment->parse (Context, nullptr , D)) {
972+ Info.Description .emplace_back ();
973+ parseFullComment (Fc, Info.Description .back ());
974+ }
975+ }
976+
795977std::pair<std::unique_ptr<Info>, std::unique_ptr<Info>>
796978emitInfo (const TypedefDecl *D, const FullComment *FC, Location Loc,
797979 bool PublicOnly) {
798980 TypedefInfo Info;
799981 bool IsInAnonymousNamespace = false ;
800982 populateInfo (Info, D, FC, IsInAnonymousNamespace);
983+
801984 if (!shouldSerializeInfo (PublicOnly, IsInAnonymousNamespace, D))
802985 return {};
803986
804987 Info.DefLoc = Loc;
805988 auto &LO = D->getLangOpts ();
806989 Info.Underlying = getTypeInfoForType (D->getUnderlyingType (), LO);
990+
807991 if (Info.Underlying .Type .Name .empty ()) {
808992 // Typedef for an unnamed type. This is like "typedef struct { } Foo;"
809993 // The record serializer explicitly checks for this syntax and constructs
810994 // a record with that name, so we don't want to emit a duplicate here.
811995 return {};
812996 }
813997 Info.IsUsing = false ;
998+ extractCommentFromDecl (D, Info);
814999
8151000 // Info is wrapped in its parent scope so is returned in the second position.
8161001 return {nullptr , makeAndInsertIntoParent<TypedefInfo &&>(std::move (Info))};
@@ -822,17 +1007,19 @@ std::pair<std::unique_ptr<Info>, std::unique_ptr<Info>>
8221007emitInfo (const TypeAliasDecl *D, const FullComment *FC, Location Loc,
8231008 bool PublicOnly) {
8241009 TypedefInfo Info;
825-
8261010 bool IsInAnonymousNamespace = false ;
8271011 populateInfo (Info, D, FC, IsInAnonymousNamespace);
8281012 if (!shouldSerializeInfo (PublicOnly, IsInAnonymousNamespace, D))
8291013 return {};
8301014
8311015 Info.DefLoc = Loc;
832- auto &LO = D->getLangOpts ();
1016+ const LangOptions &LO = D->getLangOpts ();
8331017 Info.Underlying = getTypeInfoForType (D->getUnderlyingType (), LO);
1018+ Info.TypeDeclaration = getTypeAlias (D);
8341019 Info.IsUsing = true ;
8351020
1021+ extractCommentFromDecl (D, Info);
1022+
8361023 // Info is wrapped in its parent scope so is returned in the second position.
8371024 return {nullptr , makeAndInsertIntoParent<TypedefInfo &&>(std::move (Info))};
8381025}
0 commit comments