@@ -123,6 +123,15 @@ void printParams(llvm::raw_ostream &OS,
123
123
}
124
124
}
125
125
126
+ std::string printType (QualType QT, const PrintingPolicy &Policy) {
127
+ // TypePrinter doesn't resolve decltypes, so resolve them here.
128
+ // FIXME: This doesn't handle composite types that contain a decltype in them.
129
+ // We should rather have a printing policy for that.
130
+ while (const auto *DT = QT->getAs <DecltypeType>())
131
+ QT = DT->getUnderlyingType ();
132
+ return QT.getAsString (Policy);
133
+ }
134
+
126
135
std::vector<HoverInfo::Param>
127
136
fetchTemplateParameters (const TemplateParameterList *Params,
128
137
const PrintingPolicy &PP) {
@@ -131,8 +140,7 @@ fetchTemplateParameters(const TemplateParameterList *Params,
131
140
132
141
for (const Decl *Param : *Params) {
133
142
HoverInfo::Param P;
134
- P.Type .emplace ();
135
- if (const auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
143
+ if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
136
144
P.Type = TTP->wasDeclaredWithTypename () ? " typename" : " class" ;
137
145
if (TTP->isParameterPack ())
138
146
*P.Type += " ..." ;
@@ -141,21 +149,21 @@ fetchTemplateParameters(const TemplateParameterList *Params,
141
149
P.Name = TTP->getNameAsString ();
142
150
if (TTP->hasDefaultArgument ())
143
151
P.Default = TTP->getDefaultArgument ().getAsString (PP);
144
- } else if (const auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
152
+ } else if (const auto * NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
145
153
if (IdentifierInfo *II = NTTP->getIdentifier ())
146
154
P.Name = II->getName ().str ();
147
155
148
- llvm::raw_string_ostream Out (*P.Type );
149
- NTTP->getType ().print (Out, PP);
156
+ P.Type = printType (NTTP->getType (), PP);
150
157
if (NTTP->isParameterPack ())
151
- Out << " ..." ;
158
+ *P. Type += " ..." ;
152
159
153
160
if (NTTP->hasDefaultArgument ()) {
154
161
P.Default .emplace ();
155
162
llvm::raw_string_ostream Out (*P.Default );
156
163
NTTP->getDefaultArgument ()->printPretty (Out, nullptr , PP);
157
164
}
158
- } else if (const auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
165
+ } else if (const auto *TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
166
+ P.Type .emplace ();
159
167
llvm::raw_string_ostream OS (*P.Type );
160
168
OS << " template <" ;
161
169
printParams (OS,
@@ -241,7 +249,7 @@ void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
241
249
HI.Parameters ->emplace_back ();
242
250
auto &P = HI.Parameters ->back ();
243
251
if (!PVD->getType ().isNull ()) {
244
- P.Type = PVD->getType (). getAsString ( Policy);
252
+ P.Type = printType ( PVD->getType (), Policy);
245
253
} else {
246
254
std::string Param;
247
255
llvm::raw_string_ostream OS (Param);
@@ -265,12 +273,12 @@ void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
265
273
} else if (llvm::isa<CXXDestructorDecl>(FD)) {
266
274
HI.ReturnType = " void" ;
267
275
} else {
268
- HI.ReturnType = FD->getReturnType (). getAsString ( Policy);
276
+ HI.ReturnType = printType ( FD->getReturnType (), Policy);
269
277
270
- QualType FunctionType = FD->getType ();
278
+ QualType QT = FD->getType ();
271
279
if (const VarDecl *VD = llvm::dyn_cast<VarDecl>(D)) // Lambdas
272
- FunctionType = VD->getType ().getDesugaredType (D->getASTContext ());
273
- HI.Type = FunctionType. getAsString ( Policy);
280
+ QT = VD->getType ().getDesugaredType (D->getASTContext ());
281
+ HI.Type = printType (QT, Policy);
274
282
}
275
283
// FIXME: handle variadics.
276
284
}
@@ -342,7 +350,7 @@ HoverInfo getHoverContents(const NamedDecl *D, const SymbolIndex *Index) {
342
350
fetchTemplateParameters (TD->getTemplateParameters (), Policy);
343
351
D = TD;
344
352
} else if (const FunctionDecl *FD = D->getAsFunction ()) {
345
- if (const auto FTD = FD->getDescribedTemplate ()) {
353
+ if (const auto * FTD = FD->getDescribedTemplate ()) {
346
354
HI.TemplateParameters =
347
355
fetchTemplateParameters (FTD->getTemplateParameters (), Policy);
348
356
D = FTD;
@@ -353,7 +361,7 @@ HoverInfo getHoverContents(const NamedDecl *D, const SymbolIndex *Index) {
353
361
if (const FunctionDecl *FD = getUnderlyingFunction (D))
354
362
fillFunctionTypeAndParams (HI, D, FD, Policy);
355
363
else if (const auto *VD = dyn_cast<ValueDecl>(D))
356
- HI.Type = VD->getType (). getAsString ( Policy);
364
+ HI.Type = printType ( VD->getType (), Policy);
357
365
358
366
// Fill in value with evaluated initializer if possible.
359
367
if (const auto *Var = dyn_cast<VarDecl>(D)) {
@@ -449,7 +457,7 @@ llvm::Optional<HoverInfo> getHoverContents(const Expr *E, ParsedAST &AST) {
449
457
auto Policy =
450
458
printingPolicyForDecls (AST.getASTContext ().getPrintingPolicy ());
451
459
Policy.SuppressTagKeyword = true ;
452
- HI.Type = E->getType (). getAsString ( Policy);
460
+ HI.Type = printType ( E->getType (), Policy);
453
461
HI.Value = *Val;
454
462
HI.Name = getNameForExpr (E);
455
463
return HI;
0 commit comments