Patch by Csaba Hruska!
"Here is a patch what replaces std::ostream with llvm::raw_ostream. This patch
covers the AST library, but ignores Analysis lib."
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@56185 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp
index ec809ab..9c74301 100644
--- a/Driver/ASTConsumers.cpp
+++ b/Driver/ASTConsumers.cpp
@@ -24,6 +24,7 @@
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/Streams.h"
#include "llvm/Support/Timer.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/OwningPtr.h"
#include <fstream>
@@ -35,10 +36,11 @@
namespace {
class DeclPrinter {
public:
- std::ostream& Out;
+ llvm::raw_ostream& Out;
- DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
- DeclPrinter() : Out(*llvm::cerr.stream()) {}
+ DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()) {}
+ DeclPrinter() : Out(llvm::errs()) {}
+ virtual ~DeclPrinter();
void PrintDecl(Decl *D);
void PrintFunctionDeclStart(FunctionDecl *FD);
@@ -56,6 +58,10 @@
};
} // end anonymous namespace
+DeclPrinter::~DeclPrinter() {
+ Out.flush();
+}
+
void DeclPrinter:: PrintDecl(Decl *D) {
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
PrintFunctionDeclStart(FD);
@@ -422,7 +428,7 @@
namespace {
class ASTPrinter : public ASTConsumer, public DeclPrinter {
public:
- ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
+ ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
virtual void HandleTopLevelDecl(Decl *D) {
PrintDecl(D);
@@ -430,7 +436,7 @@
};
}
-ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
+ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
return new ASTPrinter(out);
}
diff --git a/Driver/ASTConsumers.h b/Driver/ASTConsumers.h
index 7cb4ffb..e56ad7c 100644
--- a/Driver/ASTConsumers.h
+++ b/Driver/ASTConsumers.h
@@ -14,6 +14,7 @@
#ifndef DRIVER_ASTCONSUMERS_H
#define DRIVER_ASTCONSUMERS_H
+#include "llvm/Support/raw_ostream.h"
#include <string>
#include <iosfwd>
@@ -30,7 +31,7 @@
class Preprocessor;
class PreprocessorFactory;
-ASTConsumer *CreateASTPrinter(std::ostream* OS = NULL);
+ASTConsumer *CreateASTPrinter(llvm::raw_ostream* OS = NULL);
ASTConsumer *CreateASTDumper();
diff --git a/Driver/RewriteMacros.cpp b/Driver/RewriteMacros.cpp
index 5500186..5106367 100644
--- a/Driver/RewriteMacros.cpp
+++ b/Driver/RewriteMacros.cpp
@@ -17,8 +17,9 @@
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/Support/Streams.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Path.h"
-#include <fstream>
+#include "llvm/ADT/OwningPtr.h"
using namespace clang;
/// isSameToken - Return true if the two specified tokens start have the same
@@ -205,20 +206,23 @@
}
// Create the output file.
- std::ostream *OutFile;
+ llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
+ llvm::raw_ostream *OutFile;
if (OutFileName == "-") {
- OutFile = llvm::cout.stream();
+ OutFile = &llvm::outs();
} else if (!OutFileName.empty()) {
- OutFile = new std::ofstream(OutFileName.c_str(),
- std::ios_base::binary|std::ios_base::out);
+ std::string Err;
+ OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), Err);
+ OwnedStream.reset(OutFile);
} else if (InFileName == "-") {
- OutFile = llvm::cout.stream();
+ OutFile = &llvm::outs();
} else {
llvm::sys::Path Path(InFileName);
Path.eraseSuffix();
Path.appendSuffix("cpp");
- OutFile = new std::ofstream(Path.toString().c_str(),
- std::ios_base::binary|std::ios_base::out);
+ std::string Err;
+ OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), Err);
+ OwnedStream.reset(OutFile);
}
// Get the buffer corresponding to MainFileID. If we haven't changed it, then
@@ -230,4 +234,5 @@
} else {
fprintf(stderr, "No changes\n");
}
+ OutFile->flush();
}
diff --git a/Driver/RewriteObjC.cpp b/Driver/RewriteObjC.cpp
index bd809be..1ec83bf 100644
--- a/Driver/RewriteObjC.cpp
+++ b/Driver/RewriteObjC.cpp
@@ -22,12 +22,12 @@
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Streams.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Path.h"
-#include <sstream>
-#include <fstream>
using namespace clang;
using llvm::utostr;
@@ -453,20 +453,23 @@
// Create the output file.
- std::ostream *OutFile;
+ llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
+ llvm::raw_ostream *OutFile;
if (OutFileName == "-") {
- OutFile = llvm::cout.stream();
+ OutFile = &llvm::outs();
} else if (!OutFileName.empty()) {
- OutFile = new std::ofstream(OutFileName.c_str(),
- std::ios_base::binary|std::ios_base::out);
+ std::string Err;
+ OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), Err);
+ OwnedStream.reset(OutFile);
} else if (InFileName == "-") {
- OutFile = llvm::cout.stream();
+ OutFile = &llvm::outs();
} else {
llvm::sys::Path Path(InFileName);
Path.eraseSuffix();
Path.appendSuffix("cpp");
- OutFile = new std::ofstream(Path.toString().c_str(),
- std::ios_base::binary|std::ios_base::out);
+ std::string Err;
+ OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), Err);
+ OwnedStream.reset(OutFile);
}
RewriteInclude();
@@ -489,6 +492,7 @@
}
// Emit metadata.
*OutFile << ResultStr;
+ OutFile->flush();
}
//===----------------------------------------------------------------------===//
@@ -1040,7 +1044,8 @@
if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation());
// Get the new text.
- std::ostringstream Buf;
+ std::string SStr;
+ llvm::raw_string_ostream Buf(SStr);
Replacement->printPretty(Buf);
const std::string &Str = Buf.str();
@@ -1333,7 +1338,8 @@
buf += " objc_sync_exit(";
Expr *syncExpr = new ExplicitCastExpr(Context->getObjCIdType(),
S->getSynchExpr(), SourceLocation());
- std::ostringstream syncExprBuf;
+ std::string syncExprBufS;
+ llvm::raw_string_ostream syncExprBuf(syncExprBufS);
syncExpr->printPretty(syncExprBuf);
buf += syncExprBuf.str();
buf += ");\n";
@@ -1942,7 +1948,8 @@
Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,";
Preamble += "0x000007c8,"; // utf8_str
// The pretty printer for StringLiteral handles escape characters properly.
- std::ostringstream prettyBuf;
+ std::string prettyBufS;
+ llvm::raw_string_ostream prettyBuf(prettyBufS);
Exp->getString()->printPretty(prettyBuf);
Preamble += prettyBuf.str();
Preamble += ",";
diff --git a/Driver/SerializationTest.cpp b/Driver/SerializationTest.cpp
index 50d06d4..9f93479 100644
--- a/Driver/SerializationTest.cpp
+++ b/Driver/SerializationTest.cpp
@@ -64,8 +64,9 @@
TranslationUnit& TU) {
{
// Pretty-print the decls to a temp file.
- std::ofstream DeclPP(FNameDeclPrint.c_str());
- assert (DeclPP && "Could not open file for printing out decls.");
+ std::string Err;
+ llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), Err);
+ assert (Err.empty() && "Could not open file for printing out decls.");
llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
for (TranslationUnit::iterator I=TU.begin(), E=TU.end(); I!=E; ++I)
@@ -87,8 +88,9 @@
{
// Pretty-print the deserialized decls to a temp file.
- std::ofstream DeclPP(FNameDeclPrint.c_str());
- assert (DeclPP && "Could not open file for printing out decls.");
+ std::string Err;
+ llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), Err);
+ assert (Err.empty() && "Could not open file for printing out decls.");
llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
for (TranslationUnit::iterator I=NewTU->begin(), E=NewTU->end(); I!=E; ++I)