r224184 - Pretty print support for template arg enum constants

Will Wilson will at indefiant.com
Fri Dec 12 20:31:08 PST 2014


Author: lantictac
Date: Fri Dec 12 22:31:07 2014
New Revision: 224184

URL: http://llvm.org/viewvc/llvm-project?rev=224184&view=rev
Log:
Pretty print support for template arg enum constants

Added:
    cfe/trunk/test/SemaTemplate/temp_arg_enum_printing.cpp
Modified:
    cfe/trunk/lib/AST/TemplateBase.cpp
    cfe/trunk/test/SemaCXX/return-noreturn.cpp

Modified: cfe/trunk/lib/AST/TemplateBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/TemplateBase.cpp?rev=224184&r1=224183&r2=224184&view=diff
==============================================================================
--- cfe/trunk/lib/AST/TemplateBase.cpp (original)
+++ cfe/trunk/lib/AST/TemplateBase.cpp Fri Dec 12 22:31:07 2014
@@ -33,11 +33,22 @@ using namespace clang;
 /// \param TemplArg the TemplateArgument instance to print.
 ///
 /// \param Out the raw_ostream instance to use for printing.
+///
+/// \param Policy the printing policy for EnumConstantDecl printing.
 static void printIntegral(const TemplateArgument &TemplArg,
-                          raw_ostream &Out) {
+                          raw_ostream &Out, const PrintingPolicy& Policy) {
   const ::clang::Type *T = TemplArg.getIntegralType().getTypePtr();
   const llvm::APSInt &Val = TemplArg.getAsIntegral();
 
+  if (const EnumType* ET = T->getAs<EnumType>()) {
+    for (const EnumConstantDecl* ECD : ET->getDecl()->enumerators()) {
+      if (ECD->getInitVal() == Val) {
+        ECD->printQualifiedName(Out, Policy);
+        return;
+      }
+    }
+  }
+
   if (T->isBooleanType()) {
     Out << (Val.getBoolValue() ? "true" : "false");
   } else if (T->isCharType()) {
@@ -378,7 +389,7 @@ void TemplateArgument::print(const Print
     break;
       
   case Integral: {
-    printIntegral(*this, Out);
+    printIntegral(*this, Out, Policy);
     break;
   }
     

Modified: cfe/trunk/test/SemaCXX/return-noreturn.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/return-noreturn.cpp?rev=224184&r1=224183&r2=224184&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/return-noreturn.cpp (original)
+++ cfe/trunk/test/SemaCXX/return-noreturn.cpp Fri Dec 12 22:31:07 2014
@@ -143,7 +143,7 @@ template <PR9412_MatchType type> int PR9
 } // expected-warning {{control reaches end of non-void function}}
 
 void PR9412_f() {
-    PR9412_t<PR9412_Exact>(); // expected-note {{in instantiation of function template specialization 'PR9412_t<0>' requested here}}
+    PR9412_t<PR9412_Exact>(); // expected-note {{in instantiation of function template specialization 'PR9412_t<PR9412_MatchType::PR9412_Exact>' requested here}}
 }
 
 struct NoReturn {

Added: cfe/trunk/test/SemaTemplate/temp_arg_enum_printing.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/temp_arg_enum_printing.cpp?rev=224184&view=auto
==============================================================================
--- cfe/trunk/test/SemaTemplate/temp_arg_enum_printing.cpp (added)
+++ cfe/trunk/test/SemaTemplate/temp_arg_enum_printing.cpp Fri Dec 12 22:31:07 2014
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -fsyntax-only -ast-print %s | FileCheck %s
+
+namespace NamedEnumNS
+{
+  
+enum NamedEnum
+{
+  Val0,
+  Val1
+};
+  
+template <NamedEnum E>
+void foo();
+  
+void test() {
+  // CHECK: template <NamedEnumNS::NamedEnum E = NamedEnumNS::NamedEnum::Val0>
+  NamedEnumNS::foo<Val0>();
+  // CHECK: template <NamedEnumNS::NamedEnum E = NamedEnumNS::NamedEnum::Val1>
+  NamedEnumNS::foo<(NamedEnum)1>();
+  // CHECK: template <NamedEnumNS::NamedEnum E = 2>
+  NamedEnumNS::foo<(NamedEnum)2>();
+}
+  
+} // NamedEnumNS





More information about the cfe-commits mailing list