From d68ef916acfd99f100f403a165f1b9faade718b6 Mon Sep 17 00:00:00 2001 From: Frederich Munch Date: Wed, 14 Dec 2016 15:32:18 -0500 Subject: [PATCH] Use LLVM stream wrappers to reduce complexity and line counts. --- lib/Interpreter/CIFactory.cpp | 25 +++++++++------------ lib/Interpreter/ClangInternalState.cpp | 6 ++--- lib/Interpreter/DeclExtractor.cpp | 5 ++--- lib/Interpreter/DynamicLookup.cpp | 29 ++++++++++-------------- lib/Interpreter/DynamicLookup.h | 4 ++-- lib/Interpreter/ForwardDeclPrinter.cpp | 6 ++--- lib/Interpreter/ForwardDeclPrinter.h | 18 +++++++-------- lib/Interpreter/IncrementalJIT.h | 12 +++++----- lib/Interpreter/IncrementalParser.cpp | 18 +++++---------- lib/Interpreter/Interpreter.cpp | 31 +++++++++++--------------- lib/Interpreter/LookupHelper.cpp | 4 ++-- 11 files changed, 65 insertions(+), 93 deletions(-) diff --git a/lib/Interpreter/CIFactory.cpp b/lib/Interpreter/CIFactory.cpp index e634b6c6b4..ecde713478 100644 --- a/lib/Interpreter/CIFactory.cpp +++ b/lib/Interpreter/CIFactory.cpp @@ -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 @@ -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 diff --git a/lib/Interpreter/ClangInternalState.cpp b/lib/Interpreter/ClangInternalState.cpp index 848b589dfe..46ff119e1a 100644 --- a/lib/Interpreter/ClangInternalState.cpp +++ b/lib/Interpreter/ClangInternalState.cpp @@ -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 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); diff --git a/lib/Interpreter/DeclExtractor.cpp b/lib/Interpreter/DeclExtractor.cpp index e360370417..16c0b64318 100644 --- a/lib/Interpreter/DeclExtractor.cpp +++ b/lib/Interpreter/DeclExtractor.cpp @@ -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& Stmts){ diff --git a/lib/Interpreter/DynamicLookup.cpp b/lib/Interpreter/DynamicLookup.cpp index ef4b5bf5c3..82eb9d4d77 100644 --- a/lib/Interpreter/DynamicLookup.cpp +++ b/lib/Interpreter/DynamicLookup.cpp @@ -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. @@ -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 Addresses; - llvm::raw_string_ostream OS(Template); + ostrstream OS; const PrintingPolicy& Policy = m_Context->getPrintingPolicy(); StmtPrinterHelper helper(Policy, Addresses, m_Sema); @@ -702,10 +700,8 @@ namespace cling { if (!isa(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, @@ -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); @@ -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 @@ -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 diff --git a/lib/Interpreter/DynamicLookup.h b/lib/Interpreter/DynamicLookup.h index a580d437ea..8d19bd26cf 100644 --- a/lib/Interpreter/DynamicLookup.h +++ b/lib/Interpreter/DynamicLookup.h @@ -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. /// @@ -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 diff --git a/lib/Interpreter/ForwardDeclPrinter.cpp b/lib/Interpreter/ForwardDeclPrinter.cpp index 406d00838e..96eb9f0f00 100644 --- a/lib/Interpreter/ForwardDeclPrinter.cpp +++ b/lib/Interpreter/ForwardDeclPrinter.cpp @@ -960,8 +960,7 @@ namespace cling { D = RD; } - std::string Output; - llvm::raw_string_ostream Stream(Output); + stdstrstream Stream; std::string closeBraces; if (!isa(D)) @@ -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) { diff --git a/lib/Interpreter/ForwardDeclPrinter.h b/lib/Interpreter/ForwardDeclPrinter.h index cbeded159a..f044d7a38e 100644 --- a/lib/Interpreter/ForwardDeclPrinter.h +++ b/lib/Interpreter/ForwardDeclPrinter.h @@ -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" @@ -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(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(); } }; }; diff --git a/lib/Interpreter/IncrementalJIT.h b/lib/Interpreter/IncrementalJIT.h index 1777fd2815..185d26b998 100644 --- a/lib/Interpreter/IncrementalJIT.h +++ b/lib/Interpreter/IncrementalJIT.h @@ -10,6 +10,8 @@ #ifndef CLING_INCREMENTAL_JIT_H #define CLING_INCREMENTAL_JIT_H +#include "cling/Utils/Output.h" + #include #include #include @@ -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; diff --git a/lib/Interpreter/IncrementalParser.cpp b/lib/Interpreter/IncrementalParser.cpp index b679735a6a..cbc6d40525 100644 --- a/lib/Interpreter/IncrementalParser.cpp +++ b/lib/Interpreter/IncrementalParser.cpp @@ -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()); } @@ -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()); } diff --git a/lib/Interpreter/Interpreter.cpp b/lib/Interpreter/Interpreter.cpp index baeff7878e..b6d515df14 100644 --- a/lib/Interpreter/Interpreter.cpp +++ b/lib/Interpreter/Interpreter.cpp @@ -845,29 +845,24 @@ 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) { @@ -875,9 +870,9 @@ namespace cling { } 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) { diff --git a/lib/Interpreter/LookupHelper.cpp b/lib/Interpreter/LookupHelper.cpp index f265cce849..79bd59da6e 100644 --- a/lib/Interpreter/LookupHelper.cpp +++ b/lib/Interpreter/LookupHelper.cpp @@ -8,6 +8,7 @@ //------------------------------------------------------------------------------ #include "cling/Interpreter/LookupHelper.h" +#include "cling/Utils/Output.h" #include "DeclUnloader.h" #include "cling/Interpreter/Interpreter.h" @@ -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();