Minor fixes around printValue, based on comments after the integration.

This commit is contained in:
Boris Perovic 2015-08-14 22:49:01 +02:00 committed by sftnight
parent 62c71df1fa
commit 688ad34b3f
3 changed files with 72 additions and 60 deletions

View File

@ -26,7 +26,7 @@ CLINGETC_CLING := DynamicExprInfo.h DynamicLookupRuntimeUniverse.h \
CLINGETC_LLVM := llvm/ADT/IntrusiveRefCntPtr.h \
llvm/ADT/StringRef.h \
llvm/ADT/SmallVector.h \
llvm/ADT/iterator_range.h \
llvm/ADT/iterator_range.h \
llvm/Config/llvm-config.h \
llvm/Support/AlignOf.h \
llvm/Support/Allocator.h \

View File

@ -12,10 +12,6 @@
#include <string>
#include "Value.h"
#ifndef _Bool
#define _Bool bool
#endif
namespace cling {
// void pointer
@ -66,25 +62,27 @@ namespace cling {
// cling::Value
std::string printValue(const Value &value);
// Maps declaration
template<typename CollectionType>
auto printValue_impl(const CollectionType &obj, short)
namespace internal {
// Maps declaration
template<typename CollectionType>
auto printValue_impl(const CollectionType &obj, short)
-> decltype(
++(obj.begin()), obj.end(),
obj.begin()->first, obj.begin()->second,
std::string());
obj.begin()->first, obj.begin()->second,
std::string());
// Collections like vector, set, deque etc. declaration
template<typename CollectionType>
auto printValue_impl(const CollectionType &obj, int)
// Collections like vector, set, deque etc. declaration
template<typename CollectionType>
auto printValue_impl(const CollectionType &obj, int)
-> decltype(
++(obj.begin()), obj.end(),
*(obj.begin()),
std::string());
*(obj.begin()),
std::string());
// General fallback - print object address declaration
template<typename T>
std::string printValue_impl(const T &obj, long);
// General fallback - print object address declaration
template<typename T>
std::string printValue_impl(const T &obj, long);
}
// Collections and general fallback entry function
template<typename CollectionType>
@ -99,7 +97,7 @@ namespace cling {
std::string printValue(const T (&obj)[N]) {
std::string str = "{ ";
for(int i = 0; i < N; ++i) {
for (int i = 0; i < N; ++i) {
str = str + printValue(obj[i]);
if (i < N-1) str = str + ", ";
}
@ -107,54 +105,60 @@ namespace cling {
return str + " }";
}
// Maps
template<typename CollectionType>
auto printValue_impl(const CollectionType &obj, short)
namespace internal {
// Maps
template<typename CollectionType>
auto printValue_impl(const CollectionType &obj, short)
-> decltype(
++(obj.begin()), obj.end(),
obj.begin()->first, obj.begin()->second,
std::string())
{
std::string str = "{ ";
++(obj.begin()), obj.end(),
obj.begin()->first, obj.begin()->second,
std::string())
{
std::string str = "{ ";
auto iter = obj.begin();
auto iterEnd = obj.end();
while (iter != iterEnd) {
str = str + printValue(iter->first);
str = str + " => ";
str = str + printValue(iter->second);
++iter;
if (iter != iterEnd) str = str + ", ";
auto iter = obj.begin();
auto iterEnd = obj.end();
while (iter != iterEnd) {
str = str + printValue(iter->first);
str = str + " => ";
str = str + printValue(iter->second);
++iter;
if (iter != iterEnd) {
str = str + ", ";
}
}
return str + " }";
}
return str + " }";
}
// Collections like vector, set, deque etc.
template<typename CollectionType>
auto printValue_impl(const CollectionType &obj, int)
// Collections like vector, set, deque etc.
template<typename CollectionType>
auto printValue_impl(const CollectionType &obj, int)
-> decltype(
++(obj.begin()), obj.end(),
*(obj.begin()),
std::string())
{
std::string str = "{ ";
++(obj.begin()), obj.end(),
*(obj.begin()),
std::string())
{
std::string str = "{ ";
auto iter = obj.begin();
auto iterEnd = obj.end();
while (iter != iterEnd) {
str = str + printValue(*iter);
++iter;
if (iter != iterEnd) str = str + ", ";
auto iter = obj.begin();
auto iterEnd = obj.end();
while (iter != iterEnd) {
str = str + printValue(*iter);
++iter;
if (iter != iterEnd) {
str = str + ", ";
}
}
return str + " }";
}
return str + " }";
}
// General fallback - print object address
template<typename T>
std::string printValue_impl(const T &obj, long) {
return "@" + printValue((void *) &obj);
// General fallback - print object address
template<typename T>
std::string printValue_impl(const T &obj, long) {
return "@" + printValue((void *) &obj);
}
}
}

View File

@ -433,8 +433,10 @@ static std::string getCastValueString(const Value &V) {
typeWithOptDeref << "(" << type << ")";
} else if (Ty->isPointerType()) {
if (Ty->getPointeeType()->isCharType()) {
// Print char pointers as strings.
typeWithOptDeref << "(" << type << ")";
} else {
// Fallback to void pointer for other pointers and print the address.
typeWithOptDeref << "(void*)";
}
} else if (Ty->isArrayType()) {
@ -445,11 +447,15 @@ static std::string getCastValueString(const Value &V) {
const llvm::APInt& APSize = CArrTy->getSize();
size_t size = (size_t)APSize.getZExtValue();
// typeWithOptDeref example for int[40] array: "((int(&)[40])*(void*)0x5c8f260)"
typeWithOptDeref << "(" << ElementTy.getAsString() << "(&)[" << size << "])*(void*)";
} else {
typeWithOptDeref << "(void*)";
}
} else {
// In other cases, dereference the address of the object.
// If no overload or specific template matches,
// the general template will be used which only prints the address.
typeWithOptDeref << "*(" << type << "*)";
}
@ -457,7 +463,6 @@ static std::string getCastValueString(const Value &V) {
return strm.str();
}
static std::string printEnumValue(const Value &V){
std::stringstream enumString;
clang::ASTContext& C = V.getASTContext();
@ -766,6 +771,8 @@ namespace valuePrinterInternal {
std::string printValue_New(const Value& V) {
static bool includedRuntimePrintValue = false; // initialized only once as a static function variable
// Include "RuntimePrintValue.h" only on the first printing.
// This keeps the interpreter lightweight and reduces the startup time.
if(!includedRuntimePrintValue) {
V.getInterpreter()->declare("#include \"cling/Interpreter/RuntimePrintValue.h\"");
includedRuntimePrintValue = true;
@ -774,6 +781,7 @@ namespace valuePrinterInternal {
clang::QualType Ty = V.getType().getDesugaredType(C);
if(Ty-> isNullPtrType()) {
// special case nullptr_t
return "@0x0";
} else if (Ty->isEnumeralType()) {
// special case enum printing, using compiled information