[clang] 661dda9 - [clang] Add frontend flag to enable support for broken external resugarers (#103219)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 13 11:03:37 PDT 2024
Author: Yitzhak Mandelbaum
Date: 2024-08-13T14:03:34-04:00
New Revision: 661dda9df13c65ce021407bb726b558c7a414731
URL: https://github.com/llvm/llvm-project/commit/661dda9df13c65ce021407bb726b558c7a414731
DIFF: https://github.com/llvm/llvm-project/commit/661dda9df13c65ce021407bb726b558c7a414731.diff
LOG: [clang] Add frontend flag to enable support for broken external resugarers (#103219)
Forked from https://github.com/llvm/llvm-project/pull/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 <mizvekov at gmail.com>
Added:
clang/test/AST/ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp
Modified:
clang/include/clang/Basic/LangOptions.def
clang/include/clang/Driver/Options.td
clang/lib/Sema/SemaTemplate.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index ca9c00a1473bb..d454a7ff2f8cf 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(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/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 75320cafaefa5..6df3a6a5943a9 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3455,6 +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 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<[], [], " 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 29e7978ba5b1f..876921a6b311d 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -3332,10 +3332,16 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
if (Pattern->isInvalidDecl())
return QualType();
- // Only substitute for the innermost template argument list.
+ // 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
diff erent
+ // template type alias specializations apart.
MultiLevelTemplateArgumentList TemplateArgLists;
- TemplateArgLists.addOuterTemplateArguments(Template, SugaredConverted,
- /*Final=*/true);
+ TemplateArgLists.addOuterTemplateArguments(
+ Template, SugaredConverted,
+ /*Final=*/!getLangOpts().RetainSubstTemplateTypeParmTypeAstNodes);
TemplateArgLists.addOuterRetainedLevels(
AliasTemplate->getTemplateParameters()->getDepth());
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
new file mode 100644
index 0000000000000..97dc983e2436c
--- /dev/null
+++ b/clang/test/AST/ast-dump-retain-subst-template-type-parm-type-ast-nodes.cpp
@@ -0,0 +1,18 @@
+// 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;
+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-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