temporary file while codegeneration


I see that clang creates a temporary file when generating bc/ll/.o files and it moves the temporary file to the final file when the code generation is complete. Any reason why a temporary file is needed here ?

CompilerInstance::createDefaultOutputFile(bool Binary,
StringRef InFile,
StringRef Extension) {
return createOutputFile(getFrontendOpts().OutputFile, Binary,
/RemoveFileOnSignal=/true, InFile, Extension,


One reason is to ensure that the result file is created atomically
with all its contents. If we would open the result file and write to
it, then not only it could be read by other programs while it is still
being written, but it could be left in inconsistent non-useable state,
if, for example, the compiler crashed or was killed.