Skip to content

Commit 661dda9

Browse files
ymandmizvekov
andauthored
[clang] Add frontend flag to enable support for broken external resugarers (#103219)
Forked from #102510 by [mizvekov](https://github.com/mizvekov). Changes are captured as a fixup commit. There are some external projects that can't rely on our own sugar propagation for templated entities, because they need to resugar types which only exist within their framework, and so are entirely invisible to our internal tooling. This new flag is meant to prevent our transforms from removing any Subst* nodes. For this, this is wired only to template type alias subsititutions. Note that our AST does represent enough information to correctly resugar template type alias, so any users of this are limited in their capacity to reconstruct the parameter substitutions fully. --------- Co-authored-by: Matheus Izvekov <[email protected]>
1 parent edded8d commit 661dda9

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ LANGOPT(CoroAlignedAllocation, 1, 0, "prefer Aligned Allocation according to P20
162162
LANGOPT(DllExportInlines , 1, 1, "dllexported classes dllexport inline methods")
163163
LANGOPT(RelaxedTemplateTemplateArgs, 1, 1, "C++17 relaxed matching of template template arguments")
164164
LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library features")
165+
LANGOPT(RetainSubstTemplateTypeParmTypeAstNodes, 1, 0, "retain SubstTemplateTypeParmType nodes in the AST's representation of alias template specializations")
165166

166167
LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
167168
LANGOPT(PointerAuthCalls , 1, 0, "function pointer authentication")

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,6 +3455,12 @@ defm relaxed_template_template_args : BoolFOption<"relaxed-template-template-arg
34553455
PosFlag<SetTrue, [], [], "Enable">,
34563456
NegFlag<SetFalse, [], [CC1Option], "Disable">,
34573457
BothFlags<[], [ClangOption], " C++17 relaxed template template argument matching">>;
3458+
defm retain_subst_template_type_parm_type_ast_nodes : BoolFOption<"retain-subst-template-type-parm-type-ast-nodes",
3459+
LangOpts<"RetainSubstTemplateTypeParmTypeAstNodes">, DefaultFalse,
3460+
PosFlag<SetTrue, [], [CC1Option], "Enable">,
3461+
NegFlag<SetFalse, [], [], "Disable">,
3462+
BothFlags<[], [], " retain SubstTemplateTypeParmType nodes in the AST's representation"
3463+
" of alias template specializations">>;
34583464
defm sized_deallocation : BoolFOption<"sized-deallocation",
34593465
LangOpts<"SizedDeallocation">, Default<cpp14.KeyPath>,
34603466
PosFlag<SetTrue, [], [], "Enable C++14 sized global deallocation functions">,

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3332,10 +3332,16 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
33323332
if (Pattern->isInvalidDecl())
33333333
return QualType();
33343334

3335-
// Only substitute for the innermost template argument list.
3335+
// Only substitute for the innermost template argument list. NOTE: Some
3336+
// external resugarers rely on leaving a Subst* node here. Make the
3337+
// substitution non-final in that case. Note that these external resugarers
3338+
// will still miss some information in this representation, because we don't
3339+
// provide enough context in the Subst* nodes in order to tell different
3340+
// template type alias specializations apart.
33363341
MultiLevelTemplateArgumentList TemplateArgLists;
3337-
TemplateArgLists.addOuterTemplateArguments(Template, SugaredConverted,
3338-
/*Final=*/true);
3342+
TemplateArgLists.addOuterTemplateArguments(
3343+
Template, SugaredConverted,
3344+
/*Final=*/!getLangOpts().RetainSubstTemplateTypeParmTypeAstNodes);
33393345
TemplateArgLists.addOuterRetainedLevels(
33403346
AliasTemplate->getTemplateParameters()->getDepth());
33413347

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 -fsyntax-only -fretain-subst-template-type-parm-type-ast-nodes -ast-dump -ast-dump-filter=dump %s | FileCheck -strict-whitespace %s
2+
3+
namespace t1 {
4+
template<class T> using X = T;
5+
using dump = X<int>;
6+
7+
// CHECK-LABEL: Dumping t1::dump:
8+
// CHECK-NEXT: TypeAliasDecl
9+
// CHECK-NEXT: `-ElaboratedType
10+
// CHECK-NEXT: `-TemplateSpecializationType
11+
// CHECK-NEXT: |-name: 'X':'t1::X' qualified
12+
// CHECK-NEXT: | `-TypeAliasTemplateDecl
13+
// CHECK-NEXT: |-TemplateArgument
14+
// CHECK-NEXT: | `-BuiltinType {{.+}} 'int'
15+
// CHECK-NEXT: `-SubstTemplateTypeParmType 0x{{[0-9a-f]+}} 'int' sugar class depth 0 index 0 T
16+
// CHECK-NEXT: |-TypeAliasTemplate {{.+}} 'X'
17+
// CHECK-NEXT: `-BuiltinType {{.+}} 'int'
18+
} // namespace t1

0 commit comments

Comments
 (0)