diff --git a/include/cling/MetaProcessor/MetaProcessor.h b/include/cling/MetaProcessor/MetaProcessor.h index ea91ce56..64fd4373 100644 --- a/include/cling/MetaProcessor/MetaProcessor.h +++ b/include/cling/MetaProcessor/MetaProcessor.h @@ -68,6 +68,9 @@ namespace cling { ///\brief Stores the stack for the redirect file paths for err. llvm::SmallVector, 2> m_PrevStderrFileName; + //Counter to handle more than one redirection RAAI's + int m_RedirectionRAIILevel = 0; + public: enum RedirectionScope { kSTDOUT = 1, @@ -85,7 +88,14 @@ namespace cling { public: MaybeRedirectOutputRAII(MetaProcessor* p); - ~MaybeRedirectOutputRAII() { pop(); } + ~MaybeRedirectOutputRAII(); + + // Don't mess with m_RedirectionRAIILevel + MaybeRedirectOutputRAII(const MaybeRedirectOutputRAII&) = delete; + MaybeRedirectOutputRAII(MaybeRedirectOutputRAII&&) = delete; + MaybeRedirectOutputRAII& operator=(const MaybeRedirectOutputRAII&) = delete; + MaybeRedirectOutputRAII& operator=(MaybeRedirectOutputRAII&&) = delete; + private: void pop(); void redirect(FILE* file, const std::string& fileName, @@ -114,6 +124,11 @@ namespace cling { m_Outs = &outs; return prev; } + void increaseRedirectionRAIILevel() { m_RedirectionRAIILevel++; } + + void decreaseRedirectionRAIILevel() { m_RedirectionRAIILevel--; } + + int getRedirectionRAIILevel() { return m_RedirectionRAIILevel; } ///\brief Process the input coming from the prompt and possibli returns /// result of the execution of the last statement diff --git a/lib/MetaProcessor/MetaProcessor.cpp b/lib/MetaProcessor/MetaProcessor.cpp index 3239aebb..c8205c31 100644 --- a/lib/MetaProcessor/MetaProcessor.cpp +++ b/lib/MetaProcessor/MetaProcessor.cpp @@ -44,6 +44,7 @@ namespace cling { MetaProcessor* p) :m_MetaProcessor(p), m_isCurrentlyRedirecting(0) { StringRef redirectionFile; + m_MetaProcessor->increaseRedirectionRAIILevel(); if (!m_MetaProcessor->m_PrevStdoutFileName.empty()) { redirectionFile = m_MetaProcessor->m_PrevStdoutFileName.back(); redirect(stdout, redirectionFile.str(), kSTDOUT); @@ -63,6 +64,11 @@ namespace cling { } } + MetaProcessor::MaybeRedirectOutputRAII::~MaybeRedirectOutputRAII() { + pop(); + m_MetaProcessor->decreaseRedirectionRAIILevel(); + } + void MetaProcessor::MaybeRedirectOutputRAII::redirect(FILE* file, const std::string& fileName, MetaProcessor::RedirectionScope scope) { @@ -79,6 +85,11 @@ namespace cling { } void MetaProcessor::MaybeRedirectOutputRAII::pop() { + //If we have only one redirection RAII + //only then do the unredirection. + if (m_MetaProcessor->getRedirectionRAIILevel() != 1) + return; + if (m_isCurrentlyRedirecting & kSTDOUT) { unredirect(m_MetaProcessor->m_backupFDStdout, STDOUT_FILENO, stdout); }