[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
https://github.com/evelez7 closed https://github.com/llvm/llvm-project/pull/77716 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
https://github.com/daniel-grumberg approved this pull request. https://github.com/llvm/llvm-project/pull/77716 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
https://github.com/evelez7 updated https://github.com/llvm/llvm-project/pull/77716 >From f0f25f2eb4f654c9a9f04c92deea9df2da6fc64c Mon Sep 17 00:00:00 2001 From: Erick Velez Date: Wed, 10 Jan 2024 18:28:19 -0800 Subject: [PATCH] [clang][ExtractAPI] improve template argument name deduction The names of template arguments in partial specializations or parameters used as types might be mangled according to index and depth. Instead of looping through parameter lists to find matches like we do now, they can be deduced via their QualTypes or as written from the AST. --- .../clang/ExtractAPI/DeclarationFragments.h | 19 ++- clang/lib/ExtractAPI/DeclarationFragments.cpp | 51 --- 2 files changed, 14 insertions(+), 56 deletions(-) diff --git a/clang/include/clang/ExtractAPI/DeclarationFragments.h b/clang/include/clang/ExtractAPI/DeclarationFragments.h index b85a5d21d61217..8a3a22d9a594c6 100644 --- a/clang/include/clang/ExtractAPI/DeclarationFragments.h +++ b/clang/include/clang/ExtractAPI/DeclarationFragments.h @@ -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 namespace clang { @@ -315,13 +313,9 @@ class DeclarationFragmentsBuilder { static DeclarationFragments getFragmentsForTemplateParameters(ArrayRef); - static std::string - getNameForTemplateArgument(const ArrayRef, std::string); - - static DeclarationFragments - getFragmentsForTemplateArguments(const ArrayRef, - ASTContext &, - const std::optional>); + static DeclarationFragments getFragmentsForTemplateArguments( + const ArrayRef, ASTContext &, + const std::optional>); static DeclarationFragments getFragmentsForConcept(const ConceptDecl *); @@ -430,12 +424,7 @@ DeclarationFragmentsBuilder::getFunctionSignature(const FunctionT *Function) { if (isa(Function) && dyn_cast(Function)->getDescribedFunctionTemplate() && StringRef(ReturnType.begin()->Spelling).starts_with("type-parameter")) { -std::string ProperArgName = -getNameForTemplateArgument(dyn_cast(Function) - ->getDescribedFunctionTemplate() - ->getTemplateParameters() - ->asArray(), - ReturnType.begin()->Spelling); +std::string ProperArgName = Function->getReturnType().getAsString(); ReturnType.begin()->Spelling.swap(ProperArgName); } ReturnType.append(std::move(After)); diff --git a/clang/lib/ExtractAPI/DeclarationFragments.cpp b/clang/lib/ExtractAPI/DeclarationFragments.cpp index 80a0a498dc4001..22b98e07c2c890 100644 --- a/clang/lib/ExtractAPI/DeclarationFragments.cpp +++ b/clang/lib/ExtractAPI/DeclarationFragments.cpp @@ -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 using namespace clang::extractapi; using namespace llvm; @@ -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)) @@ -570,12 +565,7 @@ DeclarationFragmentsBuilder::getFragmentsForParam(const ParmVarDecl *Param) { if (StringRef(TypeFragments.begin()->Spelling) .starts_with("type-parameter")) { -std::string ProperArgName = getNameForTemplateArgument( -dyn_cast(Param->getDeclContext()) -->getDescribedFunctionTemplate() -->getTemplateParameters() -->asArray(), -TypeFragments.begin()->Spelling); +std::string ProperArgName = Param->getOriginalType().getAsString(); TypeFragments.begin()->Spelling.swap(ProperArgName); } @@ -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 = -
[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
@@ -1127,7 +1096,7 @@ DeclarationFragmentsBuilder::getFragmentsForVarTemplatePartialSpecialization( .append("<", DeclarationFragments::FragmentKind::Text) .append(getFragmentsForTemplateArguments( Decl->getTemplateArgs().asArray(), Decl->getASTContext(), - Decl->getTemplateParameters()->asArray())) + Decl->getTemplateArgsAsWritten()->arguments())) evelez7 wrote: In this patch `getFragmentsForTemplateArguments`, which is being called here, is changed to accept `ArrayRef` (which is what the changed line returns) instead of `ArrayRef` 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. https://github.com/llvm/llvm-project/pull/77716 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
@@ -1127,7 +1096,7 @@ DeclarationFragmentsBuilder::getFragmentsForVarTemplatePartialSpecialization( .append("<", DeclarationFragments::FragmentKind::Text) .append(getFragmentsForTemplateArguments( Decl->getTemplateArgs().asArray(), Decl->getASTContext(), - Decl->getTemplateParameters()->asArray())) + Decl->getTemplateArgsAsWritten()->arguments())) daniel-grumberg wrote: Looks like the right thing to me! https://github.com/llvm/llvm-project/pull/77716 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
evelez7 wrote: ping https://github.com/llvm/llvm-project/pull/77716 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
https://github.com/evelez7 updated https://github.com/llvm/llvm-project/pull/77716 >From c6373dcc5f8c647f483266954fd95a6f7b5df44c Mon Sep 17 00:00:00 2001 From: Erick Velez Date: Wed, 10 Jan 2024 18:28:19 -0800 Subject: [PATCH] [clang][ExtractAPI] improve template argument name deduction The names of template arguments in partial specializations or parameters used as types might be mangled according to index and depth. Instead of looping through parameter lists to find matches like we do now, they can be deduced via their QualTypes or as written from the AST. --- .../clang/ExtractAPI/DeclarationFragments.h | 19 ++- clang/lib/ExtractAPI/DeclarationFragments.cpp | 51 --- 2 files changed, 14 insertions(+), 56 deletions(-) diff --git a/clang/include/clang/ExtractAPI/DeclarationFragments.h b/clang/include/clang/ExtractAPI/DeclarationFragments.h index d719196b9a43ec..84b4fc5a54377f 100644 --- a/clang/include/clang/ExtractAPI/DeclarationFragments.h +++ b/clang/include/clang/ExtractAPI/DeclarationFragments.h @@ -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 namespace clang { @@ -314,13 +312,9 @@ class DeclarationFragmentsBuilder { static DeclarationFragments getFragmentsForTemplateParameters(ArrayRef); - static std::string - getNameForTemplateArgument(const ArrayRef, std::string); - - static DeclarationFragments - getFragmentsForTemplateArguments(const ArrayRef, - ASTContext &, - const std::optional>); + static DeclarationFragments getFragmentsForTemplateArguments( + const ArrayRef, ASTContext &, + const std::optional>); static DeclarationFragments getFragmentsForConcept(const ConceptDecl *); @@ -430,12 +424,7 @@ DeclarationFragmentsBuilder::getFunctionSignature(const FunctionT *Function) { dyn_cast(Function)->getDescribedFunctionTemplate() && ReturnType.begin()->Spelling.substr(0, 14).compare("type-parameter") == 0) { -std::string ProperArgName = -getNameForTemplateArgument(dyn_cast(Function) - ->getDescribedFunctionTemplate() - ->getTemplateParameters() - ->asArray(), - ReturnType.begin()->Spelling); +std::string ProperArgName = Function->getReturnType().getAsString(); ReturnType.begin()->Spelling.swap(ProperArgName); } ReturnType.append(std::move(After)); diff --git a/clang/lib/ExtractAPI/DeclarationFragments.cpp b/clang/lib/ExtractAPI/DeclarationFragments.cpp index eb6eea0aaf5465..9f45d2eaeb8875 100644 --- a/clang/lib/ExtractAPI/DeclarationFragments.cpp +++ b/clang/lib/ExtractAPI/DeclarationFragments.cpp @@ -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 using namespace clang::extractapi; using namespace llvm; @@ -535,9 +532,7 @@ DeclarationFragmentsBuilder::getFragmentsForVarTemplate(const VarDecl *Var) { getFragmentsForType(T, Var->getASTContext(), After); if (ArgumentFragment.begin()->Spelling.substr(0, 14).compare( "type-parameter") == 0) { -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)) @@ -570,12 +565,7 @@ DeclarationFragmentsBuilder::getFragmentsForParam(const ParmVarDecl *Param) { if (TypeFragments.begin()->Spelling.substr(0, 14).compare("type-parameter") == 0) { -std::string ProperArgName = getNameForTemplateArgument( -dyn_cast(Param->getDeclContext()) -->getDescribedFunctionTemplate() -->getTemplateParameters() -->asArray(), -TypeFragments.begin()->Spelling); +std::string ProperArgName = Param->getOriginalType().getAsString(); TypeFragments.begin()->Spelling.swap(ProperArgName); } @@ -668,11 +658,7 @@ DeclarationFragmentsBuilder::getFragmentsForFunction(const FunctionDecl *Func) { getFragmentsForType(Func->getReturnType(), Func->getASTContext(), After); if (ReturnValueFragment.begin()->Spelling.substr(0, 14).compare( "type-parameter") == 0) { -std::string ProperArgName = -
[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
@@ -196,8 +196,7 @@ template class Foo {}; "spelling": "<" }, { - "kind": "typeIdentifier", - "preciseIdentifier": "c:t0.0", + "kind": "genericArgument", daniel-grumberg wrote: might be best to leave these as generic text fragments https://github.com/llvm/llvm-project/pull/77716 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Erick Velez (evelez7) Changes The names of template arguments in partial specializations or parameters used as types might be mangled according to index and depth. Instead of looping through parameter lists to find matches like we do now, they can be deduced via their QualTypes or as written from the AST. --- Full diff: https://github.com/llvm/llvm-project/pull/77716.diff 4 Files Affected: - (modified) clang/include/clang/ExtractAPI/DeclarationFragments.h (+7-15) - (modified) clang/lib/ExtractAPI/DeclarationFragments.cpp (+16-42) - (modified) clang/test/ExtractAPI/class_template_partial_spec.cpp (+1-2) - (modified) clang/test/ExtractAPI/global_var_template_partial_spec.cpp (+1-2) ``diff diff --git a/clang/include/clang/ExtractAPI/DeclarationFragments.h b/clang/include/clang/ExtractAPI/DeclarationFragments.h index d719196b9a43ec..5af4a01fa6bb9c 100644 --- a/clang/include/clang/ExtractAPI/DeclarationFragments.h +++ b/clang/include/clang/ExtractAPI/DeclarationFragments.h @@ -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 namespace clang { @@ -67,6 +65,9 @@ class DeclarationFragments { /// parameters. GenericParameter, +/// Argument for partial template specialization. +GenericArgument, + /// External parameters in Objective-C methods. /// For example, \c forKey in /// \code{.m} @@ -314,13 +315,9 @@ class DeclarationFragmentsBuilder { static DeclarationFragments getFragmentsForTemplateParameters(ArrayRef); - static std::string - getNameForTemplateArgument(const ArrayRef, std::string); - - static DeclarationFragments - getFragmentsForTemplateArguments(const ArrayRef, - ASTContext &, - const std::optional>); + static DeclarationFragments getFragmentsForTemplateArguments( + const ArrayRef, ASTContext &, + const std::optional>); static DeclarationFragments getFragmentsForConcept(const ConceptDecl *); @@ -430,12 +427,7 @@ DeclarationFragmentsBuilder::getFunctionSignature(const FunctionT *Function) { dyn_cast(Function)->getDescribedFunctionTemplate() && ReturnType.begin()->Spelling.substr(0, 14).compare("type-parameter") == 0) { -std::string ProperArgName = -getNameForTemplateArgument(dyn_cast(Function) - ->getDescribedFunctionTemplate() - ->getTemplateParameters() - ->asArray(), - ReturnType.begin()->Spelling); +std::string ProperArgName = Function->getReturnType().getAsString(); ReturnType.begin()->Spelling.swap(ProperArgName); } ReturnType.append(std::move(After)); diff --git a/clang/lib/ExtractAPI/DeclarationFragments.cpp b/clang/lib/ExtractAPI/DeclarationFragments.cpp index eb6eea0aaf5465..2c82aeab6083ba 100644 --- a/clang/lib/ExtractAPI/DeclarationFragments.cpp +++ b/clang/lib/ExtractAPI/DeclarationFragments.cpp @@ -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 using namespace clang::extractapi; using namespace llvm; @@ -102,6 +99,8 @@ StringRef DeclarationFragments::getFragmentKindString( return "internalParam"; case DeclarationFragments::FragmentKind::Text: return "text"; + case DeclarationFragments::FragmentKind::GenericArgument: +return "genericArgument"; } llvm_unreachable("Unhandled FragmentKind"); @@ -122,6 +121,8 @@ DeclarationFragments::parseFragmentKindFromString(StringRef S) { .Case("internalParam", DeclarationFragments::FragmentKind::InternalParam) .Case("externalParam", DeclarationFragments::FragmentKind::ExternalParam) .Case("text", DeclarationFragments::FragmentKind::Text) + .Case("genericArgument", +DeclarationFragments::FragmentKind::GenericArgument) .Default(DeclarationFragments::FragmentKind::None); } @@ -535,9 +536,7 @@ DeclarationFragmentsBuilder::getFragmentsForVarTemplate(const VarDecl *Var) { getFragmentsForType(T, Var->getASTContext(), After); if (ArgumentFragment.begin()->Spelling.substr(0, 14).compare( "type-parameter") == 0) { -std::string ProperArgName = getNameForTemplateArgument( -Var->getDescribedVarTemplate()->getTemplateParameters()->asArray(), -ArgumentFragment.begin()->Spelling); +std::string ProperArgName = T.getAsString();
[clang] [clang][ExtractAPI] improve template argument name deduction (PR #77716)
https://github.com/evelez7 created https://github.com/llvm/llvm-project/pull/77716 The names of template arguments in partial specializations or parameters used as types might be mangled according to index and depth. Instead of looping through parameter lists to find matches like we do now, they can be deduced via their QualTypes or as written from the AST. >From ac995ade6d9e4e0ebf4e897594ccfa5c4f5e4b60 Mon Sep 17 00:00:00 2001 From: Erick Velez Date: Wed, 10 Jan 2024 18:28:19 -0800 Subject: [PATCH] [clang][ExtractAPI] improve template argument name deduction The names of template arguments in partial specializations or parameters used as types might be mangled according to index and depth. Instead of looping through parameter lists to find matches like we do now, they can be deduced via their QualTypes or as written from the AST. --- .../clang/ExtractAPI/DeclarationFragments.h | 22 +++ clang/lib/ExtractAPI/DeclarationFragments.cpp | 58 +-- .../class_template_partial_spec.cpp | 3 +- .../global_var_template_partial_spec.cpp | 3 +- 4 files changed, 25 insertions(+), 61 deletions(-) diff --git a/clang/include/clang/ExtractAPI/DeclarationFragments.h b/clang/include/clang/ExtractAPI/DeclarationFragments.h index d719196b9a43ec..5af4a01fa6bb9c 100644 --- a/clang/include/clang/ExtractAPI/DeclarationFragments.h +++ b/clang/include/clang/ExtractAPI/DeclarationFragments.h @@ -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 namespace clang { @@ -67,6 +65,9 @@ class DeclarationFragments { /// parameters. GenericParameter, +/// Argument for partial template specialization. +GenericArgument, + /// External parameters in Objective-C methods. /// For example, \c forKey in /// \code{.m} @@ -314,13 +315,9 @@ class DeclarationFragmentsBuilder { static DeclarationFragments getFragmentsForTemplateParameters(ArrayRef); - static std::string - getNameForTemplateArgument(const ArrayRef, std::string); - - static DeclarationFragments - getFragmentsForTemplateArguments(const ArrayRef, - ASTContext &, - const std::optional>); + static DeclarationFragments getFragmentsForTemplateArguments( + const ArrayRef, ASTContext &, + const std::optional>); static DeclarationFragments getFragmentsForConcept(const ConceptDecl *); @@ -430,12 +427,7 @@ DeclarationFragmentsBuilder::getFunctionSignature(const FunctionT *Function) { dyn_cast(Function)->getDescribedFunctionTemplate() && ReturnType.begin()->Spelling.substr(0, 14).compare("type-parameter") == 0) { -std::string ProperArgName = -getNameForTemplateArgument(dyn_cast(Function) - ->getDescribedFunctionTemplate() - ->getTemplateParameters() - ->asArray(), - ReturnType.begin()->Spelling); +std::string ProperArgName = Function->getReturnType().getAsString(); ReturnType.begin()->Spelling.swap(ProperArgName); } ReturnType.append(std::move(After)); diff --git a/clang/lib/ExtractAPI/DeclarationFragments.cpp b/clang/lib/ExtractAPI/DeclarationFragments.cpp index eb6eea0aaf5465..2c82aeab6083ba 100644 --- a/clang/lib/ExtractAPI/DeclarationFragments.cpp +++ b/clang/lib/ExtractAPI/DeclarationFragments.cpp @@ -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 using namespace clang::extractapi; using namespace llvm; @@ -102,6 +99,8 @@ StringRef DeclarationFragments::getFragmentKindString( return "internalParam"; case DeclarationFragments::FragmentKind::Text: return "text"; + case DeclarationFragments::FragmentKind::GenericArgument: +return "genericArgument"; } llvm_unreachable("Unhandled FragmentKind"); @@ -122,6 +121,8 @@ DeclarationFragments::parseFragmentKindFromString(StringRef S) { .Case("internalParam", DeclarationFragments::FragmentKind::InternalParam) .Case("externalParam", DeclarationFragments::FragmentKind::ExternalParam) .Case("text", DeclarationFragments::FragmentKind::Text) + .Case("genericArgument", +DeclarationFragments::FragmentKind::GenericArgument) .Default(DeclarationFragments::FragmentKind::None); } @@ -535,9 +536,7 @@ DeclarationFragmentsBuilder::getFragmentsForVarTemplate(const VarDecl *Var) {