Hi,

BuildExpressionFromIntegralTemplateArgument can produce malformed
IntegerLiterals with an EnumType if the template parameter type
is an EnumType.  This breaks the AST printer which expects all
IntegerLiterals to have a plain integer type.  Instead, give the
IntegerLiteral the enum's promotion type and wrap in an implicit cast
to the EnumType.

OK to commit?

Thanks,
-- 
Peter
>From 8683a1a5ad93531987590e3dd11026978ea8f0b8 Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <[email protected]>
Date: Fri, 26 Nov 2010 04:39:45 +0000
Subject: [PATCH] Sema: have BuildExpressionFromIntegralTemplateArgument produce well-formed IntegerLiterals

BuildExpressionFromIntegralTemplateArgument can produce malformed
IntegerLiterals with an EnumType if the template parameter type
is an EnumType.  This breaks the AST printer which expects all
IntegerLiterals to have a plain integer type.  Instead, give the
IntegerLiteral the enum's promotion type and wrap in an implicit cast
to the EnumType.
---
 lib/Sema/SemaTemplate.cpp               |   11 ++++++++++-
 test/Coverage/cxx-language-features.inc |    6 ++++++
 2 files changed, 16 insertions(+), 1 deletions(-)

diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index 53d7c10..9f35b80 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -3463,7 +3463,16 @@ Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
                                             T,
                                             Loc));
 
-  return Owned(IntegerLiteral::Create(Context, *Arg.getAsIntegral(), T, Loc));
+  QualType BT;
+  if (const EnumType *ET = T->getAs<EnumType>())
+    BT = ET->getDecl()->getPromotionType();
+  else
+    BT = T;
+
+  Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
+  ImpCastExprToType(E, T, CK_IntegralCast);
+
+  return Owned(E);
 }
 
 
diff --git a/test/Coverage/cxx-language-features.inc b/test/Coverage/cxx-language-features.inc
index 51c1104..31b50be 100644
--- a/test/Coverage/cxx-language-features.inc
+++ b/test/Coverage/cxx-language-features.inc
@@ -19,3 +19,9 @@ class Base1 {
 class Base2 { };
 
 class Derived1 : Base1, virtual public Base2 { };
+
+/* Template classes, template functions */
+enum E1 { EC1 };
+template <E1 v> class C1 {};
+template <E1 v> C1<v> f1() { return C1<v>(); }
+void f2() { f1<EC1>(); }
-- 
1.7.1

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to