Skip to content

[clang][ExtractAPI] improve template argument name deduction #77716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions clang/include/clang/ExtractAPI/DeclarationFragments.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Lex/MacroInfo.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include <vector>

namespace clang {
Expand Down Expand Up @@ -315,13 +313,9 @@ class DeclarationFragmentsBuilder {
static DeclarationFragments
getFragmentsForTemplateParameters(ArrayRef<NamedDecl *>);

static std::string
getNameForTemplateArgument(const ArrayRef<NamedDecl *>, std::string);

static DeclarationFragments
getFragmentsForTemplateArguments(const ArrayRef<TemplateArgument>,
ASTContext &,
const std::optional<ArrayRef<NamedDecl *>>);
static DeclarationFragments getFragmentsForTemplateArguments(
const ArrayRef<TemplateArgument>, ASTContext &,
const std::optional<ArrayRef<TemplateArgumentLoc>>);

static DeclarationFragments getFragmentsForConcept(const ConceptDecl *);

Expand Down Expand Up @@ -430,12 +424,7 @@ DeclarationFragmentsBuilder::getFunctionSignature(const FunctionT *Function) {
if (isa<FunctionDecl>(Function) &&
dyn_cast<FunctionDecl>(Function)->getDescribedFunctionTemplate() &&
StringRef(ReturnType.begin()->Spelling).starts_with("type-parameter")) {
std::string ProperArgName =
getNameForTemplateArgument(dyn_cast<FunctionDecl>(Function)
->getDescribedFunctionTemplate()
->getTemplateParameters()
->asArray(),
ReturnType.begin()->Spelling);
std::string ProperArgName = Function->getReturnType().getAsString();
ReturnType.begin()->Spelling.swap(ProperArgName);
}
ReturnType.append(std::move(After));
Expand Down
51 changes: 10 additions & 41 deletions clang/lib/ExtractAPI/DeclarationFragments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
#include "clang/ExtractAPI/DeclarationFragments.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/QualTypeNames.h"
#include "clang/AST/Type.h"
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/OperatorKinds.h"
#include "clang/ExtractAPI/TypedefUnderlyingTypeResolver.h"
#include "clang/Index/USRGeneration.h"
#include "llvm/ADT/StringSwitch.h"
#include <typeinfo>

using namespace clang::extractapi;
using namespace llvm;
Expand Down Expand Up @@ -535,9 +532,7 @@ DeclarationFragmentsBuilder::getFragmentsForVarTemplate(const VarDecl *Var) {
getFragmentsForType(T, Var->getASTContext(), After);
if (StringRef(ArgumentFragment.begin()->Spelling)
.starts_with("type-parameter")) {
std::string ProperArgName = getNameForTemplateArgument(
Var->getDescribedVarTemplate()->getTemplateParameters()->asArray(),
ArgumentFragment.begin()->Spelling);
std::string ProperArgName = T.getAsString();
ArgumentFragment.begin()->Spelling.swap(ProperArgName);
}
Fragments.append(std::move(ArgumentFragment))
Expand Down Expand Up @@ -570,12 +565,7 @@ DeclarationFragmentsBuilder::getFragmentsForParam(const ParmVarDecl *Param) {

if (StringRef(TypeFragments.begin()->Spelling)
.starts_with("type-parameter")) {
std::string ProperArgName = getNameForTemplateArgument(
dyn_cast<FunctionDecl>(Param->getDeclContext())
->getDescribedFunctionTemplate()
->getTemplateParameters()
->asArray(),
TypeFragments.begin()->Spelling);
std::string ProperArgName = Param->getOriginalType().getAsString();
TypeFragments.begin()->Spelling.swap(ProperArgName);
}

Expand Down Expand Up @@ -668,11 +658,7 @@ DeclarationFragmentsBuilder::getFragmentsForFunction(const FunctionDecl *Func) {
getFragmentsForType(Func->getReturnType(), Func->getASTContext(), After);
if (StringRef(ReturnValueFragment.begin()->Spelling)
.starts_with("type-parameter")) {
std::string ProperArgName =
getNameForTemplateArgument(Func->getDescribedFunctionTemplate()
->getTemplateParameters()
->asArray(),
ReturnValueFragment.begin()->Spelling);
std::string ProperArgName = Func->getReturnType().getAsString();
ReturnValueFragment.begin()->Spelling.swap(ProperArgName);
}

Expand Down Expand Up @@ -961,25 +947,6 @@ DeclarationFragmentsBuilder::getFragmentsForTemplateParameters(
return Fragments;
}

// Find the name of a template argument from the template's parameters.
std::string DeclarationFragmentsBuilder::getNameForTemplateArgument(
const ArrayRef<NamedDecl *> TemplateParameters, std::string TypeParameter) {
// The arg is a generic parameter from a partial spec, e.g.
// T in template<typename T> Foo<T, int>.
//
// Those names appear as "type-parameter-<index>-<depth>", so we must find its
// name from the template's parameter list.
for (unsigned i = 0; i < TemplateParameters.size(); ++i) {
const auto *Parameter =
dyn_cast<TemplateTypeParmDecl>(TemplateParameters[i]);
if (TypeParameter.compare("type-parameter-" +
std::to_string(Parameter->getDepth()) + "-" +
std::to_string(Parameter->getIndex())) == 0)
return std::string(TemplateParameters[i]->getName());
}
llvm_unreachable("Could not find the name of a template argument.");
}

// Get fragments for template arguments, e.g. int in template<typename T>
// Foo<int>;
//
Expand All @@ -989,7 +956,7 @@ std::string DeclarationFragmentsBuilder::getNameForTemplateArgument(
DeclarationFragments
DeclarationFragmentsBuilder::getFragmentsForTemplateArguments(
const ArrayRef<TemplateArgument> TemplateArguments, ASTContext &Context,
const std::optional<ArrayRef<NamedDecl *>> TemplateParameters) {
const std::optional<ArrayRef<TemplateArgumentLoc>> TemplateArgumentLocs) {
DeclarationFragments Fragments;
for (unsigned i = 0, end = TemplateArguments.size(); i != end; ++i) {
if (i)
Expand All @@ -1003,8 +970,10 @@ DeclarationFragmentsBuilder::getFragmentsForTemplateArguments(

if (StringRef(ArgumentFragment.begin()->Spelling)
.starts_with("type-parameter")) {
std::string ProperArgName = getNameForTemplateArgument(
TemplateParameters.value(), ArgumentFragment.begin()->Spelling);
std::string ProperArgName = TemplateArgumentLocs.value()[i]
.getTypeSourceInfo()
->getType()
.getAsString();
ArgumentFragment.begin()->Spelling.swap(ProperArgName);
}
Fragments.append(std::move(ArgumentFragment));
Expand Down Expand Up @@ -1089,7 +1058,7 @@ DeclarationFragmentsBuilder::getFragmentsForClassTemplatePartialSpecialization(
.append("<", DeclarationFragments::FragmentKind::Text)
.append(getFragmentsForTemplateArguments(
Decl->getTemplateArgs().asArray(), Decl->getASTContext(),
Decl->getTemplateParameters()->asArray()))
Decl->getTemplateArgsAsWritten()->arguments()))
.append(">", DeclarationFragments::FragmentKind::Text)
.append(";", DeclarationFragments::FragmentKind::Text);
}
Expand Down Expand Up @@ -1130,7 +1099,7 @@ DeclarationFragmentsBuilder::getFragmentsForVarTemplatePartialSpecialization(
.append("<", DeclarationFragments::FragmentKind::Text)
.append(getFragmentsForTemplateArguments(
Decl->getTemplateArgs().asArray(), Decl->getASTContext(),
Decl->getTemplateParameters()->asArray()))
Decl->getTemplateArgsAsWritten()->arguments()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the right thing to me!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this patch getFragmentsForTemplateArguments, which is being called here, is changed to accept ArrayRef<TemplateArgumentLoc> (which is what the changed line returns) instead of ArrayRef<NamedDecl *> because of the nasty loop that was deleted in DeclarationFragments.cpp:962. A TemplateArgumentLoc gives us the name of the specialization's type argument directly, instead of comparing with the declaration's template parameters.

.append(">", DeclarationFragments::FragmentKind::Text)
.append(";", DeclarationFragments::FragmentKind::Text);
}
Expand Down