[clang] [clang-tools-extra] [llvm] [Clang] Add __type_list_dedup builtin to deduplicate types in template arguments (PR #106730)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 13 07:15:46 PDT 2024


================
@@ -3158,6 +3161,33 @@ checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD,
     int64_t N = Index.getExtValue();
     return Ts.getPackAsArray()[N].getAsType();
   }
+  case BTK__type_pack_dedup: {
+    assert(Converted.size() == 2 && "__builtin_type_pack_dedup should be given "
+                                    "a template and a parameter pack");
+    TemplateArgument Template = Converted[0];
+    TemplateArgument Ts = Converted[1];
+    if (Template.isDependent() || Ts.isDependent())
+      return Context.getCanonicalTemplateSpecializationType(TemplateName(BTD),
+                                                            Converted);
+
+    assert(Template.getKind() == clang::TemplateArgument::Template);
+    assert(Ts.getKind() == clang::TemplateArgument::Pack);
+    TemplateArgumentListInfo SyntheticTemplateArgs;
+    llvm::SmallDenseSet<QualType> Seen;
+    // Synthesize a new template argument list, removing duplicates.
+    for (auto T : Ts.getPackAsArray()) {
+      assert(T.getKind() == clang::TemplateArgument::Type);
+      if (!Seen.insert(T.getAsType().getCanonicalType()).second)
+        continue;
+      SyntheticTemplateArgs.addArgument(TemplateArgumentLoc(
+          TemplateArgument(T), SemaRef.Context.getTrivialTypeSourceInfo(
+                                   T.getAsType(),
+                                   /*FIXME: add location*/ SourceLocation())));
----------------
ilya-biryukov wrote:

After looking at the code, I am afraid the not-very-precise source location will stay a limitation of this builtin (it also seems like it's the case for some other cases involving packs? as soon as a template argument gets added into a pack, it seems we should be loosing its location information at least in some cases)

We do not seem to be tracking the `TemplateArgumentLoc`s of `TempateArgument`s we get from the pack very precisely. While it's possible to poke at the `TemplateArgumentList` provided into the function and match them with the types I get from `Converted`, I am not entirely sure what the structure of the `TemplateArgumentListInfo` is [1] and whether the complexity around it is worth it?

@zygoloid any thoughts on this? is it worth digging into the various representations the passed `TemplateArgumentListInfo` might have or is it a dead end? 

<hr>
[1]: is it always flattened? does it always have packs? can it be both?

https://github.com/llvm/llvm-project/pull/106730


More information about the cfe-commits mailing list