Skip to content

Commit 6973395

Browse files
committed
eliminate the std::ostream forms of the bitcode writing APIs.
llvm-svn: 79840
1 parent 9192225 commit 6973395

File tree

9 files changed

+80
-128
lines changed

9 files changed

+80
-128
lines changed

llvm/examples/BrainF/BrainFDriver.cpp

+20-19
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@
3232
#include "llvm/Bitcode/ReaderWriter.h"
3333
#include "llvm/ExecutionEngine/GenericValue.h"
3434
#include "llvm/ExecutionEngine/JIT.h"
35+
#include "llvm/Target/TargetSelect.h"
3536
#include "llvm/Support/CommandLine.h"
3637
#include "llvm/Support/ManagedStatic.h"
37-
#include "llvm/Target/TargetSelect.h"
38-
#include <fstream>
38+
#include "llvm/Support/raw_ostream.h"
3939
#include <iostream>
40+
#include <fstream>
4041
using namespace llvm;
4142

4243
//Command line options
@@ -91,58 +92,57 @@ int main(int argc, char **argv) {
9192
LLVMContext &Context = getGlobalContext();
9293

9394
if (InputFilename == "") {
94-
std::cerr<<"Error: You must specify the filename of the program to "
95+
errs() << "Error: You must specify the filename of the program to "
9596
"be compiled. Use --help to see the options.\n";
9697
abort();
9798
}
9899

99100
//Get the output stream
100-
std::ostream *out = &std::cout;
101+
raw_ostream *out = &outs();
101102
if (!JIT) {
102103
if (OutputFilename == "") {
103104
std::string base = InputFilename;
104-
if (InputFilename == "-") {base = "a";}
105+
if (InputFilename == "-") { base = "a"; }
105106

106-
//Use default filename
107-
const char *suffix = ".bc";
108-
OutputFilename = base+suffix;
107+
// Use default filename.
108+
OutputFilename = base+".bc";
109109
}
110110
if (OutputFilename != "-") {
111-
out = new std::
112-
ofstream(OutputFilename.c_str(),
113-
std::ios::out | std::ios::trunc | std::ios::binary);
111+
std::string ErrInfo;
112+
out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
113+
raw_fd_ostream::F_Force|
114+
raw_fd_ostream::F_Binary);
114115
}
115116
}
116117

117118
//Get the input stream
118119
std::istream *in = &std::cin;
119-
if (InputFilename != "-") {
120+
if (InputFilename != "-")
120121
in = new std::ifstream(InputFilename.c_str());
121-
}
122122

123123
//Gather the compile flags
124124
BrainF::CompileFlags cf = BrainF::flag_off;
125-
if (ArrayBoundsChecking) {
125+
if (ArrayBoundsChecking)
126126
cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
127-
}
128127

129128
//Read the BrainF program
130129
BrainF bf;
131130
Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
132-
if (in != &std::cin) {delete in;}
131+
if (in != &std::cin)
132+
delete in;
133133
addMainFunction(mod);
134134

135135
//Verify generated code
136136
if (verifyModule(*mod)) {
137-
std::cerr<<"Error: module failed verification. This shouldn't happen.\n";
137+
errs() << "Error: module failed verification. This shouldn't happen.\n";
138138
abort();
139139
}
140140

141141
//Write it out
142142
if (JIT) {
143143
InitializeNativeTarget();
144144

145-
std::cout << "------- Running JIT -------\n";
145+
outs() << "------- Running JIT -------\n";
146146
ExecutionEngine *ee = EngineBuilder(mod).create();
147147
std::vector<GenericValue> args;
148148
Function *brainf_func = mod->getFunction("brainf");
@@ -152,7 +152,8 @@ int main(int argc, char **argv) {
152152
}
153153

154154
//Clean up
155-
if (out != &std::cout) {delete out;}
155+
if (out != &outs())
156+
delete out;
156157
delete mod;
157158

158159
llvm_shutdown();

llvm/examples/ModuleMaker/ModuleMaker.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "llvm/Constants.h"
2020
#include "llvm/Instructions.h"
2121
#include "llvm/Bitcode/ReaderWriter.h"
22-
#include <iostream>
22+
#include "llvm/Support/raw_ostream.h"
2323
using namespace llvm;
2424

2525
int main() {
@@ -56,7 +56,7 @@ int main() {
5656
BB->getInstList().push_back(ReturnInst::Create(Context, Add));
5757

5858
// Output the bitcode file to stdout
59-
WriteBitcodeToFile(M, std::cout);
59+
WriteBitcodeToFile(M, outs());
6060

6161
// Delete the module and all of its contents.
6262
delete M;

llvm/include/llvm/Bitcode/ReaderWriter.h

-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#ifndef LLVM_BITCODE_H
1515
#define LLVM_BITCODE_H
1616

17-
#include <iosfwd>
1817
#include <string>
1918

2019
namespace llvm {
@@ -41,10 +40,6 @@ namespace llvm {
4140
Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
4241
std::string *ErrMsg = 0);
4342

44-
/// WriteBitcodeToFile - Write the specified module to the specified output
45-
/// stream.
46-
void WriteBitcodeToFile(const Module *M, std::ostream &Out);
47-
4843
/// WriteBitcodeToFile - Write the specified module to the specified
4944
/// raw output stream.
5045
void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
@@ -53,10 +48,6 @@ namespace llvm {
5348
/// raw output stream.
5449
void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
5550

56-
/// CreateBitcodeWriterPass - Create and return a pass that writes the module
57-
/// to the specified ostream.
58-
ModulePass *CreateBitcodeWriterPass(std::ostream &Str);
59-
6051
/// createBitcodeWriterPass - Create and return a pass that writes the module
6152
/// to the specified ostream.
6253
ModulePass *createBitcodeWriterPass(raw_ostream &Str);

llvm/lib/Bitcode/Reader/Deserialize.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "llvm/Bitcode/Deserialize.h"
15-
16-
#ifdef DEBUG_BACKPATCH
17-
#include "llvm/Support/Streams.h"
18-
#endif
19-
15+
#include "llvm/Support/raw_ostream.h"
2016
using namespace llvm;
2117

2218
Deserializer::Deserializer(BitstreamReader& stream)
@@ -357,7 +353,7 @@ void Deserializer::RegisterPtr(const SerializedPtrID& PtrId,
357353
assert (!HasFinalPtr(E) && "Pointer already registered.");
358354

359355
#ifdef DEBUG_BACKPATCH
360-
llvm::cerr << "RegisterPtr: " << PtrId << " => " << Ptr << "\n";
356+
errs() << "RegisterPtr: " << PtrId << " => " << Ptr << "\n";
361357
#endif
362358

363359
SetPtr(E,Ptr);
@@ -377,16 +373,16 @@ void Deserializer::ReadUIntPtr(uintptr_t& PtrRef,
377373
PtrRef = GetFinalPtr(E);
378374

379375
#ifdef DEBUG_BACKPATCH
380-
llvm::cerr << "ReadUintPtr: " << PtrId
381-
<< " <-- " << (void*) GetFinalPtr(E) << '\n';
376+
errs() << "ReadUintPtr: " << PtrId
377+
<< " <-- " << (void*) GetFinalPtr(E) << '\n';
382378
#endif
383379
}
384380
else {
385381
assert (AllowBackpatch &&
386382
"Client forbids backpatching for this pointer.");
387383

388384
#ifdef DEBUG_BACKPATCH
389-
llvm::cerr << "ReadUintPtr: " << PtrId << " (NO PTR YET)\n";
385+
errs() << "ReadUintPtr: " << PtrId << " (NO PTR YET)\n";
390386
#endif
391387

392388
// Register backpatch. Check the freelist for a BPNode.

llvm/lib/Bitcode/Writer/BitWriter.cpp

+8-20
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,31 @@
99

1010
#include "llvm-c/BitWriter.h"
1111
#include "llvm/Bitcode/ReaderWriter.h"
12-
#include <fstream>
13-
12+
#include "llvm/Support/raw_ostream.h"
1413
using namespace llvm;
1514

1615

1716
/*===-- Operations on modules ---------------------------------------------===*/
1817

1918
int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
20-
std::ofstream OS(Path, std::ios_base::out|std::ios::trunc|std::ios::binary);
21-
22-
if (!OS.fail())
23-
WriteBitcodeToFile(unwrap(M), OS);
19+
std::string ErrorInfo;
20+
raw_fd_ostream OS(Path, ErrorInfo,
21+
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
2422

25-
if (OS.fail())
23+
if (!ErrorInfo.empty())
2624
return -1;
2725

26+
WriteBitcodeToFile(unwrap(M), OS);
2827
return 0;
2928
}
3029

3130
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR >= 4)
3231
#include <ext/stdio_filebuf.h>
3332

34-
// FIXME: Control this with configure? Provide some portable abstraction in
35-
// libSystem? As is, the user will just get a linker error if they use this on
36-
// non-GCC. Some C++ stdlibs even have ofstream::ofstream(int fd).
3733
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
38-
__gnu_cxx::stdio_filebuf<char> Buffer(FileHandle, std::ios_base::out |
39-
std::ios::trunc |
40-
std::ios::binary);
41-
std::ostream OS(&Buffer);
42-
43-
if (!OS.fail())
44-
WriteBitcodeToFile(unwrap(M), OS);
45-
46-
if (OS.fail())
47-
return -1;
34+
raw_fd_ostream OS(FileHandle, false);
4835

36+
WriteBitcodeToFile(unwrap(M), OS);
4937
return 0;
5038
}
5139

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "llvm/ValueSymbolTable.h"
2727
#include "llvm/Support/ErrorHandling.h"
2828
#include "llvm/Support/MathExtras.h"
29-
#include "llvm/Support/Streams.h"
3029
#include "llvm/Support/raw_ostream.h"
3130
#include "llvm/System/Program.h"
3231
using namespace llvm;
@@ -1466,16 +1465,6 @@ static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
14661465
}
14671466

14681467

1469-
/// WriteBitcodeToFile - Write the specified module to the specified output
1470-
/// stream.
1471-
void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1472-
raw_os_ostream RawOut(Out);
1473-
// If writing to stdout, set binary mode.
1474-
if (llvm::cout == Out)
1475-
sys::Program::ChangeStdoutToBinary();
1476-
WriteBitcodeToFile(M, RawOut);
1477-
}
1478-
14791468
/// WriteBitcodeToFile - Write the specified module to the specified output
14801469
/// stream.
14811470
void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {

llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp

+3-18
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,23 @@ using namespace llvm;
1717

1818
namespace {
1919
class WriteBitcodePass : public ModulePass {
20-
// FIXME: Kill off std::ostream
21-
std::ostream *Out;
22-
raw_ostream *RawOut; // raw_ostream to print on
20+
raw_ostream &OS; // raw_ostream to print on
2321
public:
2422
static char ID; // Pass identification, replacement for typeid
25-
explicit WriteBitcodePass(std::ostream &o)
26-
: ModulePass(&ID), Out(&o), RawOut(0) {}
2723
explicit WriteBitcodePass(raw_ostream &o)
28-
: ModulePass(&ID), Out(0), RawOut(&o) {}
24+
: ModulePass(&ID), OS(o) {}
2925

3026
const char *getPassName() const { return "Bitcode Writer"; }
3127

3228
bool runOnModule(Module &M) {
33-
if (Out) {
34-
WriteBitcodeToFile(&M, *Out);
35-
} else {
36-
WriteBitcodeToFile(&M, *RawOut);
37-
}
29+
WriteBitcodeToFile(&M, OS);
3830
return false;
3931
}
4032
};
4133
}
4234

4335
char WriteBitcodePass::ID = 0;
4436

45-
/// CreateBitcodeWriterPass - Create and return a pass that writes the module
46-
/// to the specified ostream.
47-
ModulePass *llvm::CreateBitcodeWriterPass(std::ostream &Str) {
48-
return new WriteBitcodePass(Str);
49-
}
50-
51-
5237
/// createBitcodeWriterPass - Create and return a pass that writes the module
5338
/// to the specified ostream.
5439
ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str) {

llvm/tools/bugpoint/OptimizerDriver.cpp

+17-15
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ namespace {
5151
///
5252
bool BugDriver::writeProgramToFile(const std::string &Filename,
5353
Module *M) const {
54-
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
55-
std::ios::binary;
56-
std::ofstream Out(Filename.c_str(), io_mode);
57-
if (!Out.good()) return true;
54+
std::string ErrInfo;
55+
raw_fd_ostream Out(Filename.c_str(), ErrInfo,
56+
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
57+
if (!ErrInfo.empty()) return true;
5858

5959
WriteBitcodeToFile(M ? M : Program, Out);
6060
return false;
@@ -83,11 +83,10 @@ void BugDriver::EmitProgressBitcode(const std::string &ID, bool NoFlyer) {
8383
}
8484

8585
int BugDriver::runPassesAsChild(const std::vector<const PassInfo*> &Passes) {
86-
87-
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
88-
std::ios::binary;
89-
std::ofstream OutFile(ChildOutput.c_str(), io_mode);
90-
if (!OutFile.good()) {
86+
std::string ErrInfo;
87+
raw_fd_ostream OutFile(ChildOutput.c_str(), ErrInfo,
88+
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
89+
if (!ErrInfo.empty()) {
9190
errs() << "Error opening bitcode file: " << ChildOutput << "\n";
9291
return 1;
9392
}
@@ -106,7 +105,7 @@ int BugDriver::runPassesAsChild(const std::vector<const PassInfo*> &Passes) {
106105
PM.add(createVerifierPass());
107106

108107
// Write bitcode out to disk as the last step...
109-
PM.add(CreateBitcodeWriterPass(OutFile));
108+
PM.add(createBitcodeWriterPass(OutFile));
110109

111110
// Run all queued passes.
112111
PM.run(*Program);
@@ -146,12 +145,15 @@ bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
146145
<< ErrMsg << "\n";
147146
return(1);
148147
}
149-
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
150-
std::ios::binary;
151-
std::ofstream InFile(inputFilename.c_str(), io_mode);
152-
if (!InFile.good()) {
148+
149+
std::string ErrInfo;
150+
raw_fd_ostream InFile(inputFilename.c_str(), ErrInfo,
151+
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
152+
153+
154+
if (!ErrInfo.empty()) {
153155
errs() << "Error opening bitcode file: " << inputFilename << "\n";
154-
return(1);
156+
return 1;
155157
}
156158
WriteBitcodeToFile(Program, InFile);
157159
InFile.close();

0 commit comments

Comments
 (0)