8
8
9
9
#include " Serialize.h"
10
10
#include " BitcodeWriter.h"
11
+ #include " clang/AST/Attr.h"
11
12
#include " clang/AST/Comment.h"
12
13
#include " clang/Index/USRGeneration.h"
13
14
#include " clang/Lex/Lexer.h"
14
- #include " llvm/ADT/Hashing.h"
15
15
#include " llvm/ADT/StringExtras.h"
16
16
#include " llvm/Support/SHA1.h"
17
17
@@ -35,6 +35,175 @@ static void populateMemberTypeInfo(RecordInfo &I, AccessSpecifier &Access,
35
35
const DeclaratorDecl *D,
36
36
bool IsStatic = false );
37
37
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->isDeleted ())
128
+ Stream << " = delete" ;
129
+ if (Method->size_overridden_methods ())
130
+ Stream << " override" ;
131
+ if (Method->hasAttr <clang::FinalAttr>())
132
+ Stream << " final" ;
133
+ if (Method->isConst ())
134
+ Stream << " const" ;
135
+ if (Method->isPureVirtual ())
136
+ Stream << " = 0" ;
137
+ }
138
+
139
+ if (auto ExceptionSpecType = FuncDecl->getExceptionSpecType ())
140
+ Stream << " " << ExceptionSpecType;
141
+
142
+ return Result; // Convert SmallString to std::string for return
143
+ }
144
+
145
+ static llvm::SmallString<16 > getTypeAlias (const TypeAliasDecl *Alias) {
146
+ llvm::SmallString<16 > Result;
147
+ llvm::raw_svector_ostream Stream (Result);
148
+ const ASTContext &Ctx = Alias->getASTContext ();
149
+ if (const auto *TmplDecl = Alias->getDescribedTemplate ())
150
+ getTemplateParameters (TmplDecl->getTemplateParameters (), Stream);
151
+ Stream << " using " << Alias->getNameAsString () << " = " ;
152
+ QualType Q = Alias->getUnderlyingType ();
153
+ Q.print (Stream, Ctx.getPrintingPolicy ());
154
+
155
+ return Result;
156
+ }
157
+
158
+ // extract full syntax for record declaration
159
+ static llvm::SmallString<16 > getRecordPrototype (const CXXRecordDecl *CXXRD) {
160
+ llvm::SmallString<16 > Result;
161
+ LangOptions LangOpts;
162
+ PrintingPolicy Policy (LangOpts);
163
+ Policy.SuppressTagKeyword = false ;
164
+ Policy.FullyQualifiedName = true ;
165
+ Policy.IncludeNewlines = false ;
166
+ llvm::raw_svector_ostream OS (Result);
167
+ if (const auto *TD = CXXRD->getDescribedClassTemplate ()) {
168
+ OS << " template <" ;
169
+ bool FirstParam = true ;
170
+ for (const auto *Param : *TD->getTemplateParameters ()) {
171
+ if (!FirstParam)
172
+ OS << " , " ;
173
+ Param->print (OS, Policy);
174
+ FirstParam = false ;
175
+ }
176
+ OS << " >\n " ;
177
+ }
178
+
179
+ if (CXXRD->isStruct ())
180
+ OS << " struct " ;
181
+ else if (CXXRD->isClass ())
182
+ OS << " class " ;
183
+ else if (CXXRD->isUnion ())
184
+ OS << " union " ;
185
+
186
+ OS << CXXRD->getNameAsString ();
187
+
188
+ // We need to make sure we have a good enough declaration to check. In the
189
+ // case where the class is a forward declaration, we'll fail assertions in
190
+ // DeclCXX.
191
+ if (CXXRD->isCompleteDefinition () && CXXRD->getNumBases () > 0 ) {
192
+ OS << " : " ;
193
+ bool FirstBase = true ;
194
+ for (const auto &Base : CXXRD->bases ()) {
195
+ if (!FirstBase)
196
+ OS << " , " ;
197
+ if (Base.isVirtual ())
198
+ OS << " virtual " ;
199
+ OS << getAccessSpelling (Base.getAccessSpecifier ()) << " " ;
200
+ OS << Base.getType ().getAsString (Policy);
201
+ FirstBase = false ;
202
+ }
203
+ }
204
+ return Result;
205
+ }
206
+
38
207
// A function to extract the appropriate relative path for a given info's
39
208
// documentation. The path returned is a composite of the parent namespaces.
40
209
//
@@ -408,7 +577,6 @@ static void parseEnumerators(EnumInfo &I, const EnumDecl *D) {
408
577
ASTContext &Context = E->getASTContext ();
409
578
if (RawComment *Comment =
410
579
E->getASTContext ().getRawCommentForDeclNoCache (E)) {
411
- CommentInfo CInfo;
412
580
Comment->setAttached ();
413
581
if (comments::FullComment *Fc = Comment->parse (Context, nullptr , E)) {
414
582
EnumValueInfo &Member = I.Members .back ();
@@ -434,6 +602,7 @@ static void parseBases(RecordInfo &I, const CXXRecordDecl *D) {
434
602
// Don't parse bases if this isn't a definition.
435
603
if (!D->isThisDeclarationADefinition ())
436
604
return ;
605
+
437
606
for (const CXXBaseSpecifier &B : D->bases ()) {
438
607
if (B.isVirtual ())
439
608
continue ;
@@ -555,6 +724,7 @@ static void populateFunctionInfo(FunctionInfo &I, const FunctionDecl *D,
555
724
populateSymbolInfo (I, D, FC, Loc, IsInAnonymousNamespace);
556
725
auto &LO = D->getLangOpts ();
557
726
I.ReturnType = getTypeInfoForType (D->getReturnType (), LO);
727
+ I.Prototype = getFunctionPrototype (D);
558
728
parseParameters (I, D);
559
729
560
730
populateTemplateParameters (I.Template , D);
@@ -686,15 +856,19 @@ emitInfo(const NamespaceDecl *D, const FullComment *FC, Location Loc,
686
856
std::pair<std::unique_ptr<Info>, std::unique_ptr<Info>>
687
857
emitInfo (const RecordDecl *D, const FullComment *FC, Location Loc,
688
858
bool PublicOnly) {
859
+
689
860
auto RI = std::make_unique<RecordInfo>();
690
861
bool IsInAnonymousNamespace = false ;
862
+
691
863
populateSymbolInfo (*RI, D, FC, Loc, IsInAnonymousNamespace);
692
864
if (!shouldSerializeInfo (PublicOnly, IsInAnonymousNamespace, D))
693
865
return {};
694
866
695
867
RI->TagType = D->getTagKind ();
696
868
parseFields (*RI, D, PublicOnly);
869
+
697
870
if (const auto *C = dyn_cast<CXXRecordDecl>(D)) {
871
+ RI->FullName = getRecordPrototype (C);
698
872
if (const TypedefNameDecl *TD = C->getTypedefNameForAnonDecl ()) {
699
873
RI->Name = TD->getNameAsString ();
700
874
RI->IsTypeDef = true ;
@@ -716,11 +890,11 @@ emitInfo(const RecordDecl *D, const FullComment *FC, Location Loc,
716
890
717
891
// What this is a specialization of.
718
892
auto SpecOf = CTSD->getSpecializedTemplateOrPartial ();
719
- if (auto *CTD = dyn_cast<ClassTemplateDecl *>(SpecOf))
720
- Specialization.SpecializationOf = getUSRForDecl (CTD );
721
- else if (auto *CTPSD =
893
+ if (auto *SpecTD = dyn_cast<ClassTemplateDecl *>(SpecOf))
894
+ Specialization.SpecializationOf = getUSRForDecl (SpecTD );
895
+ else if (auto *SpecTD =
722
896
dyn_cast<ClassTemplatePartialSpecializationDecl *>(SpecOf))
723
- Specialization.SpecializationOf = getUSRForDecl (CTPSD );
897
+ Specialization.SpecializationOf = getUSRForDecl (SpecTD );
724
898
725
899
// Parameters to the specialization. For partial specializations, get the
726
900
// parameters "as written" from the ClassTemplatePartialSpecializationDecl
@@ -792,25 +966,42 @@ emitInfo(const CXXMethodDecl *D, const FullComment *FC, Location Loc,
792
966
return {nullptr , makeAndInsertIntoParent<FunctionInfo &&>(std::move (Func))};
793
967
}
794
968
969
+ static void extractCommentFromDecl (const Decl *D, TypedefInfo &Info) {
970
+ assert (D && " Invalid Decl when extracting comment" );
971
+ ASTContext &Context = D->getASTContext ();
972
+ RawComment *Comment = Context.getRawCommentForDeclNoCache (D);
973
+ if (!Comment)
974
+ return ;
975
+
976
+ Comment->setAttached ();
977
+ if (comments::FullComment *Fc = Comment->parse (Context, nullptr , D)) {
978
+ Info.Description .emplace_back ();
979
+ parseFullComment (Fc, Info.Description .back ());
980
+ }
981
+ }
982
+
795
983
std::pair<std::unique_ptr<Info>, std::unique_ptr<Info>>
796
984
emitInfo (const TypedefDecl *D, const FullComment *FC, Location Loc,
797
985
bool PublicOnly) {
798
986
TypedefInfo Info;
799
987
bool IsInAnonymousNamespace = false ;
800
988
populateInfo (Info, D, FC, IsInAnonymousNamespace);
989
+
801
990
if (!shouldSerializeInfo (PublicOnly, IsInAnonymousNamespace, D))
802
991
return {};
803
992
804
993
Info.DefLoc = Loc;
805
994
auto &LO = D->getLangOpts ();
806
995
Info.Underlying = getTypeInfoForType (D->getUnderlyingType (), LO);
996
+
807
997
if (Info.Underlying .Type .Name .empty ()) {
808
998
// Typedef for an unnamed type. This is like "typedef struct { } Foo;"
809
999
// The record serializer explicitly checks for this syntax and constructs
810
1000
// a record with that name, so we don't want to emit a duplicate here.
811
1001
return {};
812
1002
}
813
1003
Info.IsUsing = false ;
1004
+ extractCommentFromDecl (D, Info);
814
1005
815
1006
// Info is wrapped in its parent scope so is returned in the second position.
816
1007
return {nullptr , makeAndInsertIntoParent<TypedefInfo &&>(std::move (Info))};
@@ -822,17 +1013,19 @@ std::pair<std::unique_ptr<Info>, std::unique_ptr<Info>>
822
1013
emitInfo (const TypeAliasDecl *D, const FullComment *FC, Location Loc,
823
1014
bool PublicOnly) {
824
1015
TypedefInfo Info;
825
-
826
1016
bool IsInAnonymousNamespace = false ;
827
1017
populateInfo (Info, D, FC, IsInAnonymousNamespace);
828
1018
if (!shouldSerializeInfo (PublicOnly, IsInAnonymousNamespace, D))
829
1019
return {};
830
1020
831
1021
Info.DefLoc = Loc;
832
- auto &LO = D->getLangOpts ();
1022
+ const LangOptions &LO = D->getLangOpts ();
833
1023
Info.Underlying = getTypeInfoForType (D->getUnderlyingType (), LO);
1024
+ Info.TypeDeclaration = getTypeAlias (D);
834
1025
Info.IsUsing = true ;
835
1026
1027
+ extractCommentFromDecl (D, Info);
1028
+
836
1029
// Info is wrapped in its parent scope so is returned in the second position.
837
1030
return {nullptr , makeAndInsertIntoParent<TypedefInfo &&>(std::move (Info))};
838
1031
}
0 commit comments