Implement toString interface to gInterpreter

This is the final version of "printValue" discussion.

We agreed that printValue interface should be altered to ToString
interface, which can be invoked `gInterpreter->ToString(XYZ)`. (ToString is in TCling and toString is in Interpreter :D)

This patch contains:

- Implementation of toString in Interpreter.cpp
- Re-Implementation of ClingPrintValue to use ToString because I changed to use Evaluate some time ago.
- Removing of RVec version of printValue which wasn't used at all
- Fix test/vecops_rvec.cxx, printValue is never supposed to be called by a normal user.
This commit is contained in:
Yuka Takahashi 2018-09-26 23:20:40 +02:00 committed by sftnight
parent 2fbb8cb0f1
commit 9c7abadde0
4 changed files with 43 additions and 4 deletions

View File

@ -405,6 +405,9 @@ namespace cling {
void GetIncludePaths(llvm::SmallVectorImpl<std::string>& incpaths,
bool withSystem, bool withFlags);
///\brief Call printValue( "(type*)" + obj ) and return string
std::string toString(const char* type, void* obj);
///\brief Prints the current include paths that are used.
///
///\param[in] S - stream to dump to or nullptr for default (cling::outs)

View File

@ -60,6 +60,7 @@
#include <string>
#include <vector>
#include <sstream>
using namespace clang;
@ -607,6 +608,22 @@ namespace cling {
ClangInternalState::printIncludedFiles(Out, getCI()->getSourceManager());
}
namespace valuePrinterInternal {
void declarePrintValue(Interpreter &Interp);
}
std::string Interpreter::toString(const char* type, void* obj) {
LockCompilationDuringUserCodeExecutionRAII LCDUCER(*this);
cling::valuePrinterInternal::declarePrintValue(*this);
std::string ret;
std::stringstream ss;
ss << "*((std::string*)" << &ret << ") = cling::printValue((" << type << "*)" << obj << ");";
CompilationResult result = process(ss.str().c_str());
if (result != cling::Interpreter::kSuccess)
llvm::errs() << "Error in Interpreter::toString: the input " << ss.str() << " cannot be evaluated";
return ret;
}
void Interpreter::GetIncludePaths(llvm::SmallVectorImpl<std::string>& incpaths,
bool withSystem, bool withFlags) {

View File

@ -926,16 +926,20 @@ namespace cling {
return printQualType(V.getASTContext(), V.getType());
}
std::string printValueInternal(const Value &V) {
void declarePrintValue(Interpreter &Interp) {
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) {
Interpreter* Interp = V.getInterpreter();
LockCompilationDuringUserCodeExecutionRAII LCDUCER(*Interp);
Interp->declare("#include \"cling/Interpreter/RuntimePrintValue.h\"");
Interp.declare("#include \"cling/Interpreter/RuntimePrintValue.h\"");
includedRuntimePrintValue = true;
}
}
std::string printValueInternal(const Value &V) {
Interpreter* Interp = V.getInterpreter();
LockCompilationDuringUserCodeExecutionRAII LCDUCER(*Interp);
declarePrintValue(*Interp);
return printUnpackedClingValue(V);
}
} // end namespace valuePrinterInternal

15
test/Interfaces/print.C Normal file
View File

@ -0,0 +1,15 @@
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------
// RUN: cat %s | %cling 2>&1 | FileCheck %s
#include "cling/Interpreter/Interpreter.h"
int a = 21;
gCling->toString("a");
// CHECK: "21"