@@ -80,7 +80,6 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
8080 sys::fs::TempFile TempFile;
8181 std::string ModuleName;
8282 unsigned Task;
83- bool Committed = false ;
8483
8584 CacheStream (std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
8685 sys::fs::TempFile TempFile, std::string EntryPath,
@@ -89,10 +88,9 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
8988 AddBuffer (std::move(AddBuffer)), TempFile(std::move(TempFile)),
9089 ModuleName(ModuleName), Task(Task) {}
9190
92- Error commit () override {
93- if (Committed)
94- return Error::success ();
95- Committed = true ;
91+ ~CacheStream () {
92+ // TODO: Manually commit rather than using non-trivial destructor,
93+ // allowing to replace report_fatal_errors with a return Error.
9694
9795 // Make sure the stream is closed before committing it.
9896 OS.reset ();
@@ -102,12 +100,10 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
102100 MemoryBuffer::getOpenFile (
103101 sys::fs::convertFDToNativeFile (TempFile.FD ), ObjectPathName,
104102 /* FileSize=*/ -1 , /* RequiresNullTerminator=*/ false );
105- if (!MBOrErr) {
106- std::error_code EC = MBOrErr.getError ();
107- return createStringError (EC, Twine (" Failed to open new cache file " ) +
108- TempFile.TmpName + " : " +
109- EC.message () + " \n " );
110- }
103+ if (!MBOrErr)
104+ report_fatal_error (Twine (" Failed to open new cache file " ) +
105+ TempFile.TmpName + " : " +
106+ MBOrErr.getError ().message () + " \n " );
111107
112108 // On POSIX systems, this will atomically replace the destination if
113109 // it already exists. We try to emulate this on Windows, but this may
@@ -122,10 +118,7 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
122118 E = handleErrors (std::move (E), [&](const ECError &E) -> Error {
123119 std::error_code EC = E.convertToErrorCode ();
124120 if (EC != errc::permission_denied)
125- return createStringError (
126- EC, Twine (" Failed to rename temporary file " ) +
127- TempFile.TmpName + " to " + ObjectPathName + " : " +
128- EC.message () + " \n " );
121+ return errorCodeToError (EC);
129122
130123 auto MBCopy = MemoryBuffer::getMemBufferCopy ((*MBOrErr)->getBuffer (),
131124 ObjectPathName);
@@ -138,22 +131,11 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
138131 });
139132
140133 if (E)
141- return E;
134+ report_fatal_error (Twine (" Failed to rename temporary file " ) +
135+ TempFile.TmpName + " to " + ObjectPathName + " : " +
136+ toString (std::move (E)) + " \n " );
142137
143138 AddBuffer (Task, ModuleName, std::move (*MBOrErr));
144- return Error::success ();
145- }
146-
147- ~CacheStream () {
148- // In Debug builds, try to track down places where commit() was not
149- // called before destruction.
150- assert (Committed);
151- // In Release builds, fall back to the previous behaviour of committing
152- // during destruction and reporting errors with report_fatal_error.
153- if (Committed)
154- return ;
155- if (Error Err = commit ())
156- report_fatal_error (Twine (toString (std::move (Err))));
157139 }
158140 };
159141
0 commit comments