Improve bool and char integral template argument printing in
diagnostics, resolving PR9227.
Patch originally by Mihai Rusu and Stephen Hines with some minimal style
tweaks from me.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@125999 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/TemplateBase.cpp b/lib/AST/TemplateBase.cpp
index f3def3e..5ab5f46 100644
--- a/lib/AST/TemplateBase.cpp
+++ b/lib/AST/TemplateBase.cpp
@@ -18,13 +18,46 @@
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
+#include "clang/AST/Type.h"
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/Diagnostic.h"
#include "llvm/ADT/FoldingSet.h"
#include <algorithm>
+#include <cctype>
+#include <iomanip>
+#include <sstream>
using namespace clang;
+/// \brief Print a template integral argument value.
+///
+/// \param TemplArg the TemplateArgument instance to print.
+///
+/// \param Out the raw_ostream instance to use for printing.
+static void printIntegral(const TemplateArgument &TemplArg,
+ llvm::raw_ostream &Out) {
+ const ::clang::Type *T = TemplArg.getIntegralType().getTypePtr();
+ const llvm::APSInt *Val = TemplArg.getAsIntegral();
+
+ if (T->isBooleanType()) {
+ Out << (Val->getBoolValue() ? "true" : "false");
+ } else if (T->isCharType()) {
+ char Ch = Val->getSExtValue();
+ if (std::isprint(Ch)) {
+ Out << "'";
+ if (Ch == '\'' || Ch == '\\')
+ Out << '\\';
+ Out << Ch << "'";
+ } else {
+ std::ostringstream Str;
+ Str << std::setw(2) << std::setfill('0') << std::hex << (int)Ch;
+ Out << "'\\x" << Str.str() << "'";
+ }
+ } else {
+ Out << Val->toString(10);
+ }
+}
+
//===----------------------------------------------------------------------===//
// TemplateArgument Implementation
//===----------------------------------------------------------------------===//
@@ -283,7 +316,7 @@
break;
case Integral: {
- Out << getAsIntegral()->toString(10);
+ printIntegral(*this, Out);
break;
}