aboutsummaryrefslogtreecommitdiff
path: root/lib/AST/TemplateBase.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2011-02-19 00:21:00 +0000
committerChandler Carruth <chandlerc@gmail.com>2011-02-19 00:21:00 +0000
commit781701c3d34740c53ad912ad007383ae2951c637 (patch)
tree376f9aec08cc0fc4e45f31b7831fab1c6719e4f2 /lib/AST/TemplateBase.cpp
parent7ef932429ed0edcc5e4bf44e516f5f4be6a8a03f (diff)
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
Diffstat (limited to 'lib/AST/TemplateBase.cpp')
-rw-r--r--lib/AST/TemplateBase.cpp35
1 files changed, 34 insertions, 1 deletions
diff --git a/lib/AST/TemplateBase.cpp b/lib/AST/TemplateBase.cpp
index f3def3eff2..5ab5f4644c 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 @@ void TemplateArgument::print(const PrintingPolicy &Policy,
break;
case Integral: {
- Out << getAsIntegral()->toString(10);
+ printIntegral(*this, Out);
break;
}