14
14
#include " clang/ExtractAPI/DeclarationFragments.h"
15
15
#include " clang/AST/Decl.h"
16
16
#include " clang/AST/DeclCXX.h"
17
- #include " clang/AST/QualTypeNames.h"
18
17
#include " clang/AST/Type.h"
19
18
#include " clang/AST/TypeLoc.h"
20
- #include " clang/Basic/OperatorKinds.h"
21
19
#include " clang/ExtractAPI/TypedefUnderlyingTypeResolver.h"
22
20
#include " clang/Index/USRGeneration.h"
23
21
#include " llvm/ADT/StringSwitch.h"
24
- #include < typeinfo>
25
22
26
23
using namespace clang ::extractapi;
27
24
using namespace llvm ;
@@ -535,9 +532,7 @@ DeclarationFragmentsBuilder::getFragmentsForVarTemplate(const VarDecl *Var) {
535
532
getFragmentsForType (T, Var->getASTContext (), After);
536
533
if (ArgumentFragment.begin ()->Spelling .substr (0 , 14 ).compare (
537
534
" type-parameter" ) == 0 ) {
538
- std::string ProperArgName = getNameForTemplateArgument (
539
- Var->getDescribedVarTemplate ()->getTemplateParameters ()->asArray (),
540
- ArgumentFragment.begin ()->Spelling );
535
+ std::string ProperArgName = T.getAsString ();
541
536
ArgumentFragment.begin ()->Spelling .swap (ProperArgName);
542
537
}
543
538
Fragments.append (std::move (ArgumentFragment))
@@ -570,12 +565,7 @@ DeclarationFragmentsBuilder::getFragmentsForParam(const ParmVarDecl *Param) {
570
565
571
566
if (TypeFragments.begin ()->Spelling .substr (0 , 14 ).compare (" type-parameter" ) ==
572
567
0 ) {
573
- std::string ProperArgName = getNameForTemplateArgument (
574
- dyn_cast<FunctionDecl>(Param->getDeclContext ())
575
- ->getDescribedFunctionTemplate ()
576
- ->getTemplateParameters ()
577
- ->asArray (),
578
- TypeFragments.begin ()->Spelling );
568
+ std::string ProperArgName = Param->getOriginalType ().getAsString ();
579
569
TypeFragments.begin ()->Spelling .swap (ProperArgName);
580
570
}
581
571
@@ -668,11 +658,7 @@ DeclarationFragmentsBuilder::getFragmentsForFunction(const FunctionDecl *Func) {
668
658
getFragmentsForType (Func->getReturnType (), Func->getASTContext (), After);
669
659
if (ReturnValueFragment.begin ()->Spelling .substr (0 , 14 ).compare (
670
660
" type-parameter" ) == 0 ) {
671
- std::string ProperArgName =
672
- getNameForTemplateArgument (Func->getDescribedFunctionTemplate ()
673
- ->getTemplateParameters ()
674
- ->asArray (),
675
- ReturnValueFragment.begin ()->Spelling );
661
+ std::string ProperArgName = Func->getReturnType ().getAsString ();
676
662
ReturnValueFragment.begin ()->Spelling .swap (ProperArgName);
677
663
}
678
664
@@ -958,25 +944,6 @@ DeclarationFragmentsBuilder::getFragmentsForTemplateParameters(
958
944
return Fragments;
959
945
}
960
946
961
- // Find the name of a template argument from the template's parameters.
962
- std::string DeclarationFragmentsBuilder::getNameForTemplateArgument (
963
- const ArrayRef<NamedDecl *> TemplateParameters, std::string TypeParameter) {
964
- // The arg is a generic parameter from a partial spec, e.g.
965
- // T in template<typename T> Foo<T, int>.
966
- //
967
- // Those names appear as "type-parameter-<index>-<depth>", so we must find its
968
- // name from the template's parameter list.
969
- for (unsigned i = 0 ; i < TemplateParameters.size (); ++i) {
970
- const auto *Parameter =
971
- dyn_cast<TemplateTypeParmDecl>(TemplateParameters[i]);
972
- if (TypeParameter.compare (" type-parameter-" +
973
- std::to_string (Parameter->getDepth ()) + " -" +
974
- std::to_string (Parameter->getIndex ())) == 0 )
975
- return std::string (TemplateParameters[i]->getName ());
976
- }
977
- llvm_unreachable (" Could not find the name of a template argument." );
978
- }
979
-
980
947
// Get fragments for template arguments, e.g. int in template<typename T>
981
948
// Foo<int>;
982
949
//
@@ -986,7 +953,7 @@ std::string DeclarationFragmentsBuilder::getNameForTemplateArgument(
986
953
DeclarationFragments
987
954
DeclarationFragmentsBuilder::getFragmentsForTemplateArguments (
988
955
const ArrayRef<TemplateArgument> TemplateArguments, ASTContext &Context,
989
- const std::optional<ArrayRef<NamedDecl * >> TemplateParameters ) {
956
+ const std::optional<ArrayRef<TemplateArgumentLoc >> TemplateArgumentLocs ) {
990
957
DeclarationFragments Fragments;
991
958
for (unsigned i = 0 , end = TemplateArguments.size (); i != end; ++i) {
992
959
if (i)
@@ -1000,8 +967,10 @@ DeclarationFragmentsBuilder::getFragmentsForTemplateArguments(
1000
967
1001
968
if (ArgumentFragment.begin ()->Spelling .substr (0 , 14 ).compare (
1002
969
" type-parameter" ) == 0 ) {
1003
- std::string ProperArgName = getNameForTemplateArgument (
1004
- TemplateParameters.value (), ArgumentFragment.begin ()->Spelling );
970
+ std::string ProperArgName = TemplateArgumentLocs.value ()[i]
971
+ .getTypeSourceInfo ()
972
+ ->getType ()
973
+ .getAsString ();
1005
974
ArgumentFragment.begin ()->Spelling .swap (ProperArgName);
1006
975
}
1007
976
Fragments.append (std::move (ArgumentFragment));
@@ -1086,7 +1055,7 @@ DeclarationFragmentsBuilder::getFragmentsForClassTemplatePartialSpecialization(
1086
1055
.append (" <" , DeclarationFragments::FragmentKind::Text)
1087
1056
.append (getFragmentsForTemplateArguments (
1088
1057
Decl->getTemplateArgs ().asArray (), Decl->getASTContext (),
1089
- Decl->getTemplateParameters ()->asArray ()))
1058
+ Decl->getTemplateArgsAsWritten ()->arguments ()))
1090
1059
.append (" >" , DeclarationFragments::FragmentKind::Text)
1091
1060
.append (" ;" , DeclarationFragments::FragmentKind::Text);
1092
1061
}
@@ -1127,7 +1096,7 @@ DeclarationFragmentsBuilder::getFragmentsForVarTemplatePartialSpecialization(
1127
1096
.append (" <" , DeclarationFragments::FragmentKind::Text)
1128
1097
.append (getFragmentsForTemplateArguments (
1129
1098
Decl->getTemplateArgs ().asArray (), Decl->getASTContext (),
1130
- Decl->getTemplateParameters ()->asArray ()))
1099
+ Decl->getTemplateArgsAsWritten ()->arguments ()))
1131
1100
.append (" >" , DeclarationFragments::FragmentKind::Text)
1132
1101
.append (" ;" , DeclarationFragments::FragmentKind::Text);
1133
1102
}
0 commit comments