llvm.org GIT mirror llvm / e4f1a9b
Move tool_output_file into its own file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@115973 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
18 changed file(s) with 119 addition(s) and 84 deletion(s). Raw diff Collapse all Expand all
0 //===- ToolOutputFile.h - Output files for compiler-like tools -----------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the tool_output_file class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SUPPORT_TOOL_OUTPUT_FILE_H
14 #define LLVM_SUPPORT_TOOL_OUTPUT_FILE_H
15
16 #include "llvm/Support/raw_ostream.h"
17
18 namespace llvm {
19
20 /// tool_output_file - This class contains a raw_fd_ostream and adds a
21 /// few extra features commonly needed for compiler-like tool output files:
22 /// - The file is automatically deleted if the process is killed.
23 /// - The file is automatically deleted when the tool_output_file
24 /// object is destroyed unless the client calls keep().
25 class tool_output_file {
26 /// Installer - This class is declared before the raw_fd_ostream so that
27 /// it is constructed before the raw_fd_ostream is constructed and
28 /// destructed after the raw_fd_ostream is destructed. It installs
29 /// cleanups in its constructor and uninstalls them in its destructor.
30 class CleanupInstaller {
31 /// Filename - The name of the file.
32 std::string Filename;
33 public:
34 /// Keep - The flag which indicates whether we should not delete the file.
35 bool Keep;
36
37 explicit CleanupInstaller(const char *filename);
38 ~CleanupInstaller();
39 } Installer;
40
41 /// OS - The contained stream. This is intentionally declared after
42 /// Installer.
43 raw_fd_ostream OS;
44
45 public:
46 /// tool_output_file - This constructor's arguments are passed to
47 /// to raw_fd_ostream's constructor.
48 tool_output_file(const char *filename, std::string &ErrorInfo,
49 unsigned Flags = 0);
50
51 /// os - Return the contained raw_fd_ostream.
52 raw_fd_ostream &os() { return OS; }
53
54 /// keep - Indicate that the tool's job wrt this output file has been
55 /// successful and the file should not be deleted.
56 void keep() { Installer.Keep = true; }
57 };
58
59 } // end llvm namespace
60
61 #endif
474474 ~raw_null_ostream();
475475 };
476476
477 /// tool_output_file - This class contains a raw_fd_ostream and adds a
478 /// few extra features commonly needed for compiler-like tool output files:
479 /// - The file is automatically deleted if the process is killed.
480 /// - The file is automatically deleted when the tool_output_file
481 /// object is destroyed unless the client calls keep().
482 class tool_output_file {
483 /// Installer - This class is declared before the raw_fd_ostream so that
484 /// it is constructed before the raw_fd_ostream is constructed and
485 /// destructed after the raw_fd_ostream is destructed. It installs
486 /// cleanups in its constructor and uninstalls them in its destructor.
487 class CleanupInstaller {
488 /// Filename - The name of the file.
489 std::string Filename;
490 public:
491 /// Keep - The flag which indicates whether we should not delete the file.
492 bool Keep;
493
494 explicit CleanupInstaller(const char *filename);
495 ~CleanupInstaller();
496 } Installer;
497
498 /// OS - The contained stream. This is intentionally declared after
499 /// Installer.
500 raw_fd_ostream OS;
501
502 public:
503 /// tool_output_file - This constructor's arguments are passed to
504 /// to raw_fd_ostream's constructor.
505 tool_output_file(const char *filename, std::string &ErrorInfo,
506 unsigned Flags = 0);
507
508 /// os - Return the contained raw_fd_ostream.
509 raw_fd_ostream &os() { return OS; }
510
511 /// keep - Indicate that the tool's job wrt this output file has been
512 /// successful and the file should not be deleted.
513 void keep() { Installer.Keep = true; }
514 };
515
516477 } // end llvm namespace
517478
518479 #endif
0 //===--- ToolOutputFile.cpp - Implement the tool_output_file class --------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This implements the tool_output_file class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Support/ToolOutputFile.h"
14 #include "llvm/System/Signals.h"
15 using namespace llvm;
16
17 tool_output_file::CleanupInstaller::CleanupInstaller(const char *filename)
18 : Filename(filename), Keep(false) {
19 // Arrange for the file to be deleted if the process is killed.
20 if (Filename != "-")
21 sys::RemoveFileOnSignal(sys::Path(Filename));
22 }
23
24 tool_output_file::CleanupInstaller::~CleanupInstaller() {
25 // Delete the file if the client hasn't told us not to.
26 if (!Keep && Filename != "-")
27 sys::Path(Filename).eraseFromDisk();
28
29 // Ok, the file is successfully written and closed, or deleted. There's no
30 // further need to clean it up on signals.
31 if (Filename != "-")
32 sys::DontRemoveFileOnSignal(sys::Path(Filename));
33 }
34
35 tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo,
36 unsigned Flags)
37 : Installer(filename),
38 OS(filename, ErrorInfo, Flags) {
39 // If open fails, no cleanup is needed.
40 if (!ErrorInfo.empty())
41 Installer.Keep = true;
42 }
1818 #include "llvm/Config/config.h"
1919 #include "llvm/Support/Compiler.h"
2020 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/System/Signals.h"
2221 #include "llvm/ADT/STLExtras.h"
2322 #include
2423 #include
664663 uint64_t raw_null_ostream::current_pos() const {
665664 return 0;
666665 }
667
668 //===----------------------------------------------------------------------===//
669 // tool_output_file
670 //===----------------------------------------------------------------------===//
671
672 tool_output_file::CleanupInstaller::CleanupInstaller(const char *filename)
673 : Filename(filename), Keep(false) {
674 // Arrange for the file to be deleted if the process is killed.
675 if (Filename != "-")
676 sys::RemoveFileOnSignal(sys::Path(Filename));
677 }
678
679 tool_output_file::CleanupInstaller::~CleanupInstaller() {
680 // Delete the file if the client hasn't told us not to.
681 if (!Keep && Filename != "-")
682 sys::Path(Filename).eraseFromDisk();
683
684 // Ok, the file is successfully written and closed, or deleted. There's no
685 // further need to clean it up on signals.
686 if (Filename != "-")
687 sys::DontRemoveFileOnSignal(sys::Path(Filename));
688 }
689
690 tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo,
691 unsigned Flags)
692 : Installer(filename),
693 OS(filename, ErrorInfo, Flags) {
694 // If open fails, no cleanup is needed.
695 if (!ErrorInfo.empty())
696 Installer.Keep = true;
697 }
2828 #include "llvm/Support/CommandLine.h"
2929 #include "llvm/Support/Debug.h"
3030 #include "llvm/Support/FileUtilities.h"
31 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/ToolOutputFile.h"
3232 #include "llvm/System/Path.h"
3333 #include "llvm/System/Signals.h"
3434 #include
2828 #include "llvm/Support/CommandLine.h"
2929 #include "llvm/Support/SystemUtils.h"
3030 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/ToolOutputFile.h"
3232 #include "llvm/System/Path.h"
3333 #include "llvm/System/Program.h"
3434
1616
1717 #include "llvm-c/lto.h"
1818
19 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Support/ToolOutputFile.h"
2020 #include "llvm/System/Errno.h"
2121 #include "llvm/System/Path.h"
2222 #include "llvm/System/Program.h"
2727 #include "llvm/Support/ManagedStatic.h"
2828 #include "llvm/Support/PluginLoader.h"
2929 #include "llvm/Support/PrettyStackTrace.h"
30 #include "llvm/Support/ToolOutputFile.h"
3031 #include "llvm/System/Host.h"
3132 #include "llvm/System/Signals.h"
3233 #include "llvm/Target/SubtargetFeature.h"
2424 #include "llvm/Support/PrettyStackTrace.h"
2525 #include "llvm/Support/SourceMgr.h"
2626 #include "llvm/Support/SystemUtils.h"
27 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/ToolOutputFile.h"
2828 #include "llvm/System/Signals.h"
2929 #include
3030 using namespace llvm;
2525 #include "llvm/Support/ManagedStatic.h"
2626 #include "llvm/Support/MemoryBuffer.h"
2727 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/ToolOutputFile.h"
2929 #include "llvm/System/Signals.h"
3030 using namespace llvm;
3131
2222 #include "llvm/Support/IRReader.h"
2323 #include "llvm/Support/ManagedStatic.h"
2424 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/ToolOutputFile.h"
2626 #include "llvm/Support/SystemUtils.h"
2727 #include "llvm/System/Signals.h"
2828 #include "llvm/ADT/SmallPtrSet.h"
3434 #include "llvm/Support/MemoryBuffer.h"
3535 #include "llvm/Support/PrettyStackTrace.h"
3636 #include "llvm/Support/SystemUtils.h"
37 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/ToolOutputFile.h"
3838 #include "llvm/System/Signals.h"
3939 #include "llvm/Config/config.h"
4040 #include
1919 #include "llvm/Support/CommandLine.h"
2020 #include "llvm/Support/ManagedStatic.h"
2121 #include "llvm/Support/PrettyStackTrace.h"
22 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/ToolOutputFile.h"
2323 #include "llvm/Support/SystemUtils.h"
2424 #include "llvm/Support/IRReader.h"
2525 #include "llvm/System/Signals.h"
3232 #include "llvm/Support/MemoryBuffer.h"
3333 #include "llvm/Support/PrettyStackTrace.h"
3434 #include "llvm/Support/SourceMgr.h"
35 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Support/ToolOutputFile.h"
3636 #include "llvm/System/Host.h"
3737 #include "llvm/System/Signals.h"
3838 #include "Disassembler.h"
1818 #include "llvm/Value.h"
1919 #include "llvm/Analysis/CallGraph.h"
2020 #include "llvm/Analysis/Dominators.h"
21 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/ToolOutputFile.h"
2222 using namespace llvm;
2323
2424 template
3131 #include "llvm/Support/PrettyStackTrace.h"
3232 #include "llvm/Support/StandardPasses.h"
3333 #include "llvm/Support/SystemUtils.h"
34 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Support/ToolOutputFile.h"
3535 #include "llvm/LinkAllPasses.h"
3636 #include "llvm/LinkAllVMCore.h"
3737 #include
1515 #include "llvm/Support/CommandLine.h"
1616 #include "llvm/Support/MemoryBuffer.h"
1717 #include "llvm/Support/PrettyStackTrace.h"
18 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Support/ToolOutputFile.h"
1919 #include "llvm/System/Signals.h"
2020 using namespace llvm;
2121
3939 #include "llvm/Support/CommandLine.h"
4040 #include "llvm/Support/MemoryBuffer.h"
4141 #include "llvm/Support/PrettyStackTrace.h"
42 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Support/ToolOutputFile.h"
4343 #include "llvm/System/Signals.h"
4444 #include
4545 #include