[clang] [clang] Add frontend flag to enable support for broken external resugarers (PR #103219)
Yitzhak Mandelbaum via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 13 11:02:41 PDT 2024
https://github.com/ymand updated https://github.com/llvm/llvm-project/pull/103219
>From cde9e0eb8090a95df22bc83527a68d385fa847a9 Mon Sep 17 00:00:00 2001
From: Matheus Izvekov <mizvekov at gmail.com>
Date: Thu, 8 Aug 2024 14:22:03 -0300
Subject: [PATCH 1/4] [clang] Add frontend flag to enable support for broken
external resugarers
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 either defective
or working under extremely limited scenarios.
---
clang/include/clang/Basic/LangOptions.def | 1 +
clang/include/clang/Driver/Options.td | 5 +++++
clang/lib/Sema/SemaTemplate.cpp | 11 +++++++++--
...dump-support-broken-external-resugarers.cpp | 18 ++++++++++++++++++
4 files changed, 33 insertions(+), 2 deletions(-)
create mode 100644 clang/test/AST/ast-dump-support-broken-external-resugarers.cpp
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index ca9c00a1473bbd..de1f441124b2ec 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -162,6 +162,7 @@ LANGOPT(CoroAlignedAllocation, 1, 0, "prefer Aligned Allocation according to P20
LANGOPT(DllExportInlines , 1, 1, "dllexported classes dllexport inline methods")
LANGOPT(RelaxedTemplateTemplateArgs, 1, 1, "C++17 relaxed matching of template template arguments")
LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library features")
+LANGOPT(SupportBrokenExternalResugarers, 1, 0, "Suppress transforms which would impede broken external resugarers")
LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
LANGOPT(PointerAuthCalls , 1, 0, "function pointer authentication")
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 75320cafaefa5f..4c1b9d2b059add 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3455,6 +3455,11 @@ defm relaxed_template_template_args : BoolFOption<"relaxed-template-template-arg
PosFlag<SetTrue, [], [], "Enable">,
NegFlag<SetFalse, [], [CC1Option], "Disable">,
BothFlags<[], [ClangOption], " C++17 relaxed template template argument matching">>;
+defm support_broken_external_resugarers : BoolFOption<"support-broken-external-resugarers",
+ LangOpts<"SupportBrokenExternalResugarers">, DefaultFalse,
+ PosFlag<SetTrue, [], [CC1Option], "Enable">,
+ NegFlag<SetFalse, [], [], "Disable">,
+ BothFlags<[], [], " support for broken external resugarers">>;
defm sized_deallocation : BoolFOption<"sized-deallocation",
LangOpts<"SizedDeallocation">, Default<cpp14.KeyPath>,
PosFlag<SetTrue, [], [], "Enable C++14 sized global deallocation functions">,
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 29e7978ba5b1f8..1a1b39519ac7c7 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -3333,9 +3333,16 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
return QualType();
// Only substitute for the innermost template argument list.
+ // NOTE: Some external resugarers rely on leaving a Subst* node here.
+ // Make the substituion non-final in that case.
+ // Note that these external resugarers are essentially broken
+ // for depending on this, because we don't provide
+ // enough context in the Subst* nodes in order
+ // to tell different template type alias specializations apart.
MultiLevelTemplateArgumentList TemplateArgLists;
- TemplateArgLists.addOuterTemplateArguments(Template, SugaredConverted,
- /*Final=*/true);
+ TemplateArgLists.addOuterTemplateArguments(
+ Template, SugaredConverted,
+ /*Final=*/!getLangOpts().SupportBrokenExternalResugarers);
TemplateArgLists.addOuterRetainedLevels(
AliasTemplate->getTemplateParameters()->getDepth());
diff --git a/clang/test/AST/ast-dump-support-broken-external-resugarers.cpp b/clang/test/AST/ast-dump-support-broken-external-resugarers.cpp
new file mode 100644
index 00000000000000..2f2e4ce16bcdd1
--- /dev/null
+++ b/clang/test/AST/ast-dump-support-broken-external-resugarers.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fsyntax-only -fsupport-broken-external-resugarers -ast-dump -ast-dump-filter=dump %s | FileCheck -strict-whitespace %s
+
+namespace t1 {
+template<class T> using X = T;
+using dump = X<int>;
+
+// CHECK-LABEL: Dumping t1::dump:
+// CHECK-NEXT: TypeAliasDecl
+// CHECK-NEXT: `-ElaboratedType
+// CHECK-NEXT: `-TemplateSpecializationType
+// CHECK-NEXT: |-name: 'X':'t1::X' qualified
+// CHECK-NEXT: | `-TypeAliasTemplateDecl
+// CHECK-NEXT: |-TemplateArgument
+// CHECK-NEXT: | `-BuiltinType {{.+}} 'int'
+// CHECK-NEXT: `-SubstTemplateTypeParmType 0x{{[0-9]+}} 'int' sugar class depth 0 index 0 T
+// CHECK-NEXT: |-TypeAliasTemplate {{.+}} 'X'
+// CHECK-NEXT: `-BuiltinType {{.+}} 'int'
+} // namespace t1
>From f1f72b0f164ce649c1c3a20176160d579fa8da6a Mon Sep 17 00:00:00 2001
From: Yitzhak Mandelbaum <yitzhakm at google.com>
Date: Tue, 13 Aug 2024 14:47:02 +0000
Subject: [PATCH 2/4] fixup! [clang] Add frontend flag to enable support for
broken external resugarers
rename flag and adjust the comments
---
clang/include/clang/Basic/LangOptions.def | 2 +-
clang/include/clang/Driver/Options.td | 7 ++++---
clang/lib/Sema/SemaTemplate.cpp | 15 +++++++--------
...st-dump-support-broken-external-resugarers.cpp | 2 +-
4 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index de1f441124b2ec..6086f0ee73d27c 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -162,7 +162,7 @@ LANGOPT(CoroAlignedAllocation, 1, 0, "prefer Aligned Allocation according to P20
LANGOPT(DllExportInlines , 1, 1, "dllexported classes dllexport inline methods")
LANGOPT(RelaxedTemplateTemplateArgs, 1, 1, "C++17 relaxed matching of template template arguments")
LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library features")
-LANGOPT(SupportBrokenExternalResugarers, 1, 0, "Suppress transforms which would impede broken external resugarers")
+LANGOPT(RetainSubstTemplateTypeParmTypeAstNodes, 1, 0, "retain SubstTemplateTypeParmType nodes in the AST's representationof alias template specializations")
LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
LANGOPT(PointerAuthCalls , 1, 0, "function pointer authentication")
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 4c1b9d2b059add..6df3a6a5943a97 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3455,11 +3455,12 @@ defm relaxed_template_template_args : BoolFOption<"relaxed-template-template-arg
PosFlag<SetTrue, [], [], "Enable">,
NegFlag<SetFalse, [], [CC1Option], "Disable">,
BothFlags<[], [ClangOption], " C++17 relaxed template template argument matching">>;
-defm support_broken_external_resugarers : BoolFOption<"support-broken-external-resugarers",
- LangOpts<"SupportBrokenExternalResugarers">, DefaultFalse,
+defm retain_subst_template_type_parm_type_ast_nodes : BoolFOption<"retain-subst-template-type-parm-type-ast-nodes",
+ LangOpts<"RetainSubstTemplateTypeParmTypeAstNodes">, DefaultFalse,
PosFlag<SetTrue, [], [CC1Option], "Enable">,
NegFlag<SetFalse, [], [], "Disable">,
- BothFlags<[], [], " support for broken external resugarers">>;
+ BothFlags<[], [], " retain SubstTemplateTypeParmType nodes in the AST's representation"
+ " of alias template specializations">>;
defm sized_deallocation : BoolFOption<"sized-deallocation",
LangOpts<"SizedDeallocation">, Default<cpp14.KeyPath>,
PosFlag<SetTrue, [], [], "Enable C++14 sized global deallocation functions">,
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 1a1b39519ac7c7..876921a6b311d4 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -3332,17 +3332,16 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
if (Pattern->isInvalidDecl())
return QualType();
- // Only substitute for the innermost template argument list.
- // NOTE: Some external resugarers rely on leaving a Subst* node here.
- // Make the substituion non-final in that case.
- // Note that these external resugarers are essentially broken
- // for depending on this, because we don't provide
- // enough context in the Subst* nodes in order
- // to tell different template type alias specializations apart.
+ // Only substitute for the innermost template argument list. NOTE: Some
+ // external resugarers rely on leaving a Subst* node here. Make the
+ // substitution non-final in that case. Note that these external resugarers
+ // will still miss some information in this representation, because we don't
+ // provide enough context in the Subst* nodes in order to tell different
+ // template type alias specializations apart.
MultiLevelTemplateArgumentList TemplateArgLists;
TemplateArgLists.addOuterTemplateArguments(
Template, SugaredConverted,
- /*Final=*/!getLangOpts().SupportBrokenExternalResugarers);
+ /*Final=*/!getLangOpts().RetainSubstTemplateTypeParmTypeAstNodes);
TemplateArgLists.addOuterRetainedLevels(
AliasTemplate->getTemplateParameters()->getDepth());
diff --git a/clang/test/AST/ast-dump-support-broken-external-resugarers.cpp b/clang/test/AST/ast-dump-support-broken-external-resugarers.cpp
index 2f2e4ce16bcdd1..de1a1051a09933 100644
--- a/clang/test/AST/ast-dump-support-broken-external-resugarers.cpp
+++ b/clang/test/AST/ast-dump-support-broken-external-resugarers.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -fsupport-broken-external-resugarers -ast-dump -ast-dump-filter=dump %s | FileCheck -strict-whitespace %s
+// RUN: %clang_cc1 -fsyntax-only -fretain-subst-template-type-parm-type-ast-nodes -ast-dump -ast-dump-filter=dump %s | FileCheck -strict-whitespace %s
namespace t1 {
template<class T> using X = T;
>From 4d5bfeb2f8d758ab56a75dc3006f252bea29bca7 Mon Sep 17 00:00:00 2001
From: Yitzhak Mandelbaum <yitzhakm at google.com>
Date: Tue, 13 Aug 2024 16:36:38 +0000
Subject: [PATCH 3/4] fixup! fixup! [clang] Add frontend flag to enable support
for broken external resugarers
fix typo and rename file
---
clang/include/clang/Basic/LangOptions.def | 2 +-
...ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp} | 0
2 files changed, 1 insertion(+), 1 deletion(-)
rename clang/test/AST/{ast-dump-support-broken-external-resugarers.cpp => ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp} (100%)
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index 6086f0ee73d27c..d454a7ff2f8cf4 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -162,7 +162,7 @@ LANGOPT(CoroAlignedAllocation, 1, 0, "prefer Aligned Allocation according to P20
LANGOPT(DllExportInlines , 1, 1, "dllexported classes dllexport inline methods")
LANGOPT(RelaxedTemplateTemplateArgs, 1, 1, "C++17 relaxed matching of template template arguments")
LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library features")
-LANGOPT(RetainSubstTemplateTypeParmTypeAstNodes, 1, 0, "retain SubstTemplateTypeParmType nodes in the AST's representationof alias template specializations")
+LANGOPT(RetainSubstTemplateTypeParmTypeAstNodes, 1, 0, "retain SubstTemplateTypeParmType nodes in the AST's representation of alias template specializations")
LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
LANGOPT(PointerAuthCalls , 1, 0, "function pointer authentication")
diff --git a/clang/test/AST/ast-dump-support-broken-external-resugarers.cpp b/clang/test/AST/ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp
similarity index 100%
rename from clang/test/AST/ast-dump-support-broken-external-resugarers.cpp
rename to clang/test/AST/ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp
>From c8ad3785b8f6d407a00b3c31ebc9978ee00a6528 Mon Sep 17 00:00:00 2001
From: Yitzhak Mandelbaum <yitzhakm at google.com>
Date: Tue, 13 Aug 2024 17:59:35 +0000
Subject: [PATCH 4/4] fixup! fixup! fixup! [clang] Add frontend flag to enable
support for broken external resugarers
fix broken test
---
.../ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/AST/ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp b/clang/test/AST/ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp
index de1a1051a09933..97dc983e2436cb 100644
--- a/clang/test/AST/ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp
+++ b/clang/test/AST/ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp
@@ -12,7 +12,7 @@ using dump = X<int>;
// CHECK-NEXT: | `-TypeAliasTemplateDecl
// CHECK-NEXT: |-TemplateArgument
// CHECK-NEXT: | `-BuiltinType {{.+}} 'int'
-// CHECK-NEXT: `-SubstTemplateTypeParmType 0x{{[0-9]+}} 'int' sugar class depth 0 index 0 T
+// CHECK-NEXT: `-SubstTemplateTypeParmType 0x{{[0-9a-f]+}} 'int' sugar class depth 0 index 0 T
// CHECK-NEXT: |-TypeAliasTemplate {{.+}} 'X'
// CHECK-NEXT: `-BuiltinType {{.+}} 'int'
} // namespace t1
More information about the cfe-commits
mailing list