blob: 6aed423a01e388b0138ebcce28108c5ec82b6c5d [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
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// Utility for creating a in-memory buffer that will be written to a file.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
15#define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
16
17#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/DataTypes.h"
20#include "llvm/Support/Error.h"
21#include "llvm/Support/FileSystem.h"
22
23namespace llvm {
24/// FileOutputBuffer - This interface provides simple way to create an in-memory
25/// buffer which will be written to a file. During the lifetime of these
26/// objects, the content or existence of the specified file is undefined. That
27/// is, creating an OutputBuffer for a file may immediately remove the file.
28/// If the FileOutputBuffer is committed, the target file's content will become
29/// the buffer content at the time of the commit. If the FileOutputBuffer is
30/// not committed, the file will be deleted in the FileOutputBuffer destructor.
31class FileOutputBuffer {
32public:
33 enum {
34 F_executable = 1 /// set the 'x' bit on the resulting file
35 };
36
37 /// Factory method to create an OutputBuffer object which manages a read/write
38 /// buffer of the specified size. When committed, the buffer will be written
39 /// to the file at the specified path.
40 static Expected<std::unique_ptr<FileOutputBuffer>>
41 create(StringRef FilePath, size_t Size, unsigned Flags = 0);
42
43 /// Returns a pointer to the start of the buffer.
44 virtual uint8_t *getBufferStart() const = 0;
45
46 /// Returns a pointer to the end of the buffer.
47 virtual uint8_t *getBufferEnd() const = 0;
48
49 /// Returns size of the buffer.
50 virtual size_t getBufferSize() const = 0;
51
52 /// Returns path where file will show up if buffer is committed.
53 StringRef getPath() const { return FinalPath; }
54
55 /// Flushes the content of the buffer to its file and deallocates the
56 /// buffer. If commit() is not called before this object's destructor
57 /// is called, the file is deleted in the destructor. The optional parameter
58 /// is used if it turns out you want the file size to be smaller than
59 /// initially requested.
60 virtual Error commit() = 0;
61
62 /// If this object was previously committed, the destructor just deletes
63 /// this object. If this object was not committed, the destructor
64 /// deallocates the buffer and the target file is never written.
65 virtual ~FileOutputBuffer() {}
66
67protected:
68 FileOutputBuffer(StringRef Path) : FinalPath(Path) {}
69
70 std::string FinalPath;
71};
72} // end namespace llvm
73
74#endif