Don't use 'using std::error_code' in include/llvm.
[oota-llvm.git] / lib / Support / DataStream.cpp
1 //===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements DataStreamer, which fetches bytes of Data from
11 // a stream source. It provides support for streaming (lazy reading) of
12 // bitcode. An example implementation of streaming from a file or stdin
13 // is included.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Support/DataStream.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Program.h"
21 #include <cerrno>
22 #include <cstdio>
23 #include <string>
24 #include <system_error>
25 #if !defined(_MSC_VER) && !defined(__MINGW32__)
26 #include <unistd.h>
27 #else
28 #include <io.h>
29 #endif
30 using namespace llvm;
31 using std::error_code;
32
33 #define DEBUG_TYPE "Data-stream"
34
35 // Interface goals:
36 // * StreamableMemoryObject doesn't care about complexities like using
37 //   threads/async callbacks to actually overlap download+compile
38 // * Don't want to duplicate Data in memory
39 // * Don't need to know total Data len in advance
40 // Non-goals:
41 // StreamableMemoryObject already has random access so this interface only does
42 // in-order streaming (no arbitrary seeking, else we'd have to buffer all the
43 // Data here in addition to MemoryObject).  This also means that if we want
44 // to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
45
46 STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
47
48 namespace llvm {
49 DataStreamer::~DataStreamer() {}
50 }
51
52 namespace {
53
54 // Very simple stream backed by a file. Mostly useful for stdin and debugging;
55 // actual file access is probably still best done with mmap.
56 class DataFileStreamer : public DataStreamer {
57  int Fd;
58 public:
59   DataFileStreamer() : Fd(0) {}
60   virtual ~DataFileStreamer() {
61     close(Fd);
62   }
63   size_t GetBytes(unsigned char *buf, size_t len) override {
64     NumStreamFetches++;
65     return read(Fd, buf, len);
66   }
67
68   error_code OpenFile(const std::string &Filename) {
69     if (Filename == "-") {
70       Fd = 0;
71       sys::ChangeStdinToBinary();
72       return error_code();
73     }
74
75     return sys::fs::openFileForRead(Filename, Fd);
76   }
77 };
78
79 }
80
81 namespace llvm {
82 DataStreamer *getDataFileStreamer(const std::string &Filename,
83                                   std::string *StrError) {
84   DataFileStreamer *s = new DataFileStreamer();
85   if (error_code e = s->OpenFile(Filename)) {
86     *StrError = std::string("Could not open ") + Filename + ": " +
87         e.message() + "\n";
88     return nullptr;
89   }
90   return s;
91 }
92
93 }