Skip to content

Commit

Permalink
Use LLVM stream wrappers to reduce complexity and line counts.
Browse files Browse the repository at this point in the history
  • Loading branch information
marsupial authored and sftnight committed Dec 20, 2016
1 parent 2a7ff15 commit d68ef91
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 93 deletions.
25 changes: 11 additions & 14 deletions lib/Interpreter/CIFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,7 @@ namespace {
// This must be a copy of clang::getClangToolFullVersion(). Luckily
// we'll notice quickly if it ever changes! :-)
static std::string CopyOfClanggetClangToolFullVersion(StringRef ToolName) {
std::string buf;
llvm::raw_string_ostream OS(buf);
cling::stdstrstream OS;
#ifdef CLANG_VENDOR
OS << CLANG_VENDOR;
#endif
Expand Down Expand Up @@ -476,28 +475,26 @@ namespace {
}

#ifdef _MSC_VER
std::string stringifyPreprocSetting(const char* name, int val) {
std::string ret(name);
{
llvm::raw_string_ostream sstr(ret);
sstr << "=" << val;
}
return ret;
static void stringifyPreprocSetting(PreprocessorOptions& PPOpts,
const char* Name, int Val) {
smallstream Strm;
Strm << Name << "=" << Val;
PPOpts.addMacroDef(Strm.str());
}

#define STRINGIFY_PREPROC_SETTING(name) \
stringifyPreprocSetting(#name, name).c_str()
#define STRINGIFY_PREPROC_SETTING(PP, name) \
stringifyPreprocSetting(PP, #name, name)
#endif

/// Set cling's preprocessor defines to match the cling binary.
static void SetPreprocessorFromBinary(PreprocessorOptions& PPOpts) {
#ifdef _MSC_VER
PPOpts.addMacroDef(STRINGIFY_PREPROC_SETTING(_HAS_EXCEPTIONS));
STRINGIFY_PREPROC_SETTING(PPOpts, _HAS_EXCEPTIONS);
#ifdef _DEBUG
PPOpts.addMacroDef(STRINGIFY_PREPROC_SETTING(_DEBUG));
STRINGIFY_PREPROC_SETTING(PPOpts, _DEBUG);
#endif
#ifdef NDEBUG
PPOpts.addMacroDef(STRINGIFY_PREPROC_SETTING(NDEBUG));
STRINGIFY_PREPROC_SETTING(PPOpts, NDEBUG);
#endif
#endif

Expand Down
6 changes: 2 additions & 4 deletions lib/Interpreter/ClangInternalState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,14 @@ namespace cling {

void ClangInternalState::printMacroDefinitions(llvm::raw_ostream& Out,
const clang::Preprocessor& PP) {
std::string contents;
llvm::raw_string_ostream contentsOS(contents);
stdstrstream contentsOS;
PP.printMacros(contentsOS);
contentsOS.flush();
Out << "Ordered Alphabetically:\n";
std::vector<std::string> elems;
{
// Split the string into lines.
char delim = '\n';
std::stringstream ss(contents);
std::stringstream ss(contentsOS.str());
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
Expand Down
5 changes: 2 additions & 3 deletions lib/Interpreter/DeclExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,8 @@ namespace cling {
if (out.empty())
out += '_';

out += "_init_order";
out += utils::Synthesize::UniquePrefix;
llvm::raw_string_ostream(out) << m_UniqueNameCounter++;
llvm::raw_string_ostream(out) << "_init_order"
<< utils::Synthesize::UniquePrefix << m_UniqueNameCounter++;
}

void DeclExtractor::EnforceInitOrder(llvm::SmallVectorImpl<Stmt*>& Stmts){
Expand Down
29 changes: 12 additions & 17 deletions lib/Interpreter/DynamicLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ namespace cling {
ASTNodeInfo NewNode;
// 2.1 Get unique name for the LifetimeHandler instance and
// initialize it
std::string UniqueName;
createUniqueName(UniqueName);
std::string UniqueName = createUniqueName();
IdentifierInfo& II = m_Context->Idents.get(UniqueName);

// Prepare the initialization Exprs.
Expand Down Expand Up @@ -686,9 +685,8 @@ namespace cling {
Sema::ContextRAII pushedDC(*m_Sema, m_CurDeclContext);

// 1. Get the expression containing @-s and get the variable addresses
std::string Template;
llvm::SmallVector<DeclRefExpr*, 4> Addresses;
llvm::raw_string_ostream OS(Template);
ostrstream OS;
const PrintingPolicy& Policy = m_Context->getPrintingPolicy();

StmtPrinterHelper helper(Policy, Addresses, m_Sema);
Expand All @@ -702,10 +700,8 @@ namespace cling {
if (!isa<ParenListExpr>(SubTree))
OS << ')';

OS.flush();

// 2. Build the template
Expr* ExprTemplate = ConstructConstCharPtrExpr(Template.c_str());
Expr* ExprTemplate = ConstructConstCharPtrExpr(OS.str());

// 3. Build the array of addresses
QualType VarAddrTy = m_Sema->BuildArrayType(m_Context->VoidPtrTy,
Expand Down Expand Up @@ -793,9 +789,8 @@ namespace cling {
return Result;
}

Expr* EvaluateTSynthesizer::ConstructConstCharPtrExpr(const char* Val) {
Expr* EvaluateTSynthesizer::ConstructConstCharPtrExpr(llvm::StringRef Value) {
const QualType CChar = m_Context->CharTy.withConst();
llvm::StringRef Value(Val);

unsigned bitSize = m_Context->getTypeSize(m_Context->VoidPtrTy);
llvm::APInt ArraySize(bitSize, Value.size() + 1);
Expand Down Expand Up @@ -923,11 +918,13 @@ namespace cling {
return true;
}

void EvaluateTSynthesizer::createUniqueName(std::string& out) {
out = "__dynamic";
out += utils::Synthesize::UniquePrefix;
llvm::raw_string_ostream(out) << m_UniqueNameCounter++;
std::string EvaluateTSynthesizer::createUniqueName() {
stdstrstream Strm;
Strm << "__dynamic" << utils::Synthesize::UniquePrefix
<< m_UniqueNameCounter++;
return Strm.str();
}


// end Helpers

Expand All @@ -949,11 +946,9 @@ namespace cling {
}

LifetimeHandler::~LifetimeHandler() {
std::string str;
llvm::raw_string_ostream stream(str);
ostrstream stream;
stream << "delete (" << m_Type << "*) " << m_Memory << ";";
stream.flush();
m_Interpreter->execute(str);
m_Interpreter->execute(stream.str());
}
} // end namespace internal
} // end namespace runtime
Expand Down
4 changes: 2 additions & 2 deletions lib/Interpreter/DynamicLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ namespace cling {
bool ValuePrinterReq = false);

///\brief Creates const char* expression from given value.
clang::Expr* ConstructConstCharPtrExpr(const char* Val);
clang::Expr* ConstructConstCharPtrExpr(llvm::StringRef Val);

///\brief Checks if the given node is marked as dependent by us.
///
Expand All @@ -284,7 +284,7 @@ namespace cling {
/// \brief Creates unique name (eg. of a variable). Used internally for
/// AST node synthesis.
///
void createUniqueName(std::string& out);
std::string createUniqueName();
/// @}
};
} // end namespace cling
Expand Down
6 changes: 2 additions & 4 deletions lib/Interpreter/ForwardDeclPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,7 @@ namespace cling {
D = RD;
}

std::string Output;
llvm::raw_string_ostream Stream(Output);
stdstrstream Stream;

std::string closeBraces;
if (!isa<TemplateTemplateParmDecl>(D))
Expand Down Expand Up @@ -989,8 +988,7 @@ namespace cling {
}
Stream << SubStream.take(true);
}
Stream.flush();
Out() << Output << closeBraces << '\n';
Out() << Stream.str() << closeBraces << '\n';
}

void ForwardDeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Expand Down
18 changes: 8 additions & 10 deletions lib/Interpreter/ForwardDeclPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#ifndef CLING_AUTOLOADING_VISITOR_H
#define CLING_AUTOLOADING_VISITOR_H

#include "cling/Utils/Output.h"

#include "clang/AST/DeclVisitor.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/Basic/Specifiers.h"
Expand Down Expand Up @@ -262,35 +264,31 @@ namespace cling {
class StreamRAII {
ForwardDeclPrinter& m_pr;
clang::PrintingPolicy m_oldPol;
std::string m_Output;
llvm::raw_string_ostream m_Stream;
largestream m_Stream;
bool m_HavePopped;
public:
StreamRAII(ForwardDeclPrinter& pr, clang::PrintingPolicy* pol = 0):
m_pr(pr), m_oldPol(pr.m_Policy), m_Stream(m_Output),
m_HavePopped(false) {
m_pr.m_StreamStack.push(&m_Stream);
m_pr(pr), m_oldPol(pr.m_Policy), m_HavePopped(false) {
m_pr.m_StreamStack.push(&static_cast<llvm::raw_ostream&>(m_Stream));
if (pol)
m_pr.m_Policy = *pol;
}
~StreamRAII() {
if (!m_HavePopped) {
m_pr.m_StreamStack.pop();
m_Stream.flush();
if (!m_pr.m_SkipFlag) {
m_pr.Out() << m_Output;
m_pr.Out() << m_Stream.str();
}
}
m_pr.m_Policy = m_oldPol;
}
std::string take(bool pop = false) {
m_Stream.flush();
llvm::StringRef take(bool pop = false) {
if (pop) {
assert(!m_HavePopped && "No popping twice");
m_HavePopped = true;
m_pr.m_StreamStack.pop();
}
return m_Output;
return m_Stream.str();
}
};
};
Expand Down
12 changes: 5 additions & 7 deletions lib/Interpreter/IncrementalJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#ifndef CLING_INCREMENTAL_JIT_H
#define CLING_INCREMENTAL_JIT_H

#include "cling/Utils/Output.h"

#include <map>
#include <memory>
#include <set>
Expand Down Expand Up @@ -172,13 +174,9 @@ class IncrementalJIT {


std::string Mangle(llvm::StringRef Name) {
std::string MangledName;
{
llvm::raw_string_ostream MangledNameStream(MangledName);
llvm::Mangler::getNameWithPrefix(MangledNameStream, Name,
m_TMDataLayout);
}
return MangledName;
stdstrstream MangledName;
llvm::Mangler::getNameWithPrefix(MangledName, Name, m_TMDataLayout);
return MangledName.str();
}

llvm::orc::JITSymbol getInjectedSymbols(const std::string& Name) const;
Expand Down
18 changes: 6 additions & 12 deletions lib/Interpreter/IncrementalParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,9 @@ namespace cling {

if (MustStartNewModule) {
// Create a new module.
std::string ModuleName;
{
llvm::raw_string_ostream strm(ModuleName);
strm << "cling-module-" << ++m_ModuleNo;
}
getCodeGenerator()->StartModule(ModuleName,
stdstrstream ModuleName;
ModuleName << "cling-module-" << ++m_ModuleNo;
getCodeGenerator()->StartModule(ModuleName.str(),
*m_Interpreter->getLLVMContext(),
getCI()->getCodeGenOpts());
}
Expand Down Expand Up @@ -478,12 +475,9 @@ namespace cling {
}

// Create a new module.
std::string ModuleName;
{
llvm::raw_string_ostream strm(ModuleName);
strm << "cling-module-" << ++m_ModuleNo;
}
getCodeGenerator()->StartModule(ModuleName,
smallstream ModuleName;
ModuleName << "cling-module-" << ++m_ModuleNo;
getCodeGenerator()->StartModule(ModuleName.str(),
*m_Interpreter->getLLVMContext(),
getCI()->getCodeGenOpts());
}
Expand Down
31 changes: 13 additions & 18 deletions lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,39 +845,34 @@ namespace cling {
if (addr)
return addr;

std::string funcname;
{
llvm::raw_string_ostream namestr(funcname);
namestr << "__cling_Destruct_" << RD;
}
smallstream funcname;
funcname << "__cling_Destruct_" << RD;

std::string code = "extern \"C\" void ";
clang::QualType RDQT(RD->getTypeForDecl(), 0);
std::string typeName
= utils::TypeName::GetFullyQualifiedName(RDQT, RD->getASTContext());
std::string dtorName = RD->getNameAsString();
code += funcname + "(void* obj){((" + typeName + "*)obj)->~"
+ dtorName + "();}";
largestream code;
code << "extern \"C\" void " << funcname.str() << "(void* obj){(("
<< utils::TypeName::GetFullyQualifiedName(
clang::QualType(RD->getTypeForDecl(), 0), RD->getASTContext())
<< "*)obj)->~" << RD->getNameAsString() << "();}";

// ifUniq = false: we know it's unique, no need to check.
addr = compileFunction(funcname, code, false /*ifUniq*/,
addr = compileFunction(funcname.str(), code.str(), false /*ifUniq*/,
false /*withAccessControl*/);
return addr;
}

void Interpreter::createUniqueName(std::string& out) {
out += utils::Synthesize::UniquePrefix;
llvm::raw_string_ostream(out) << m_UniqueCounter++;
llvm::raw_string_ostream(out)
<< utils::Synthesize::UniquePrefix << m_UniqueCounter++;
}

bool Interpreter::isUniqueName(llvm::StringRef name) {
return name.startswith(utils::Synthesize::UniquePrefix);
}

llvm::StringRef Interpreter::createUniqueWrapper() const {
llvm::SmallString<128> out(utils::Synthesize::UniquePrefix);
llvm::raw_svector_ostream(out) << m_UniqueCounter++;
return (getCI()->getASTContext().Idents.getOwn(out)).getName();
smallstream Stream;
Stream << utils::Synthesize::UniquePrefix << m_UniqueCounter++;
return (getCI()->getASTContext().Idents.getOwn(Stream.str())).getName();
}

bool Interpreter::isUniqueWrapper(llvm::StringRef name) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Interpreter/LookupHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//------------------------------------------------------------------------------

#include "cling/Interpreter/LookupHelper.h"
#include "cling/Utils/Output.h"

#include "DeclUnloader.h"
#include "cling/Interpreter/Interpreter.h"
Expand Down Expand Up @@ -1777,8 +1778,7 @@ namespace cling {
else {
proto += ',';
}
std::string empty;
llvm::raw_string_ostream tmp(empty);
stdstrstream tmp;
expr->printPretty(tmp, /*PrinterHelper=*/0, Policy,
/*Indentation=*/0);
proto += tmp.str();
Expand Down

0 comments on commit d68ef91

Please sign in to comment.