[cfe-users] temporary file while codegeneration

Dmitri Gribenko gribozavr at gmail.com
Mon Dec 9 10:57:04 PST 2013


On Mon, Dec 9, 2013 at 12:06 AM, Xin Tong <trent.tong at gmail.com> wrote:
> Hi
>
> 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,
>                           -/*UseTemporary=*/true-);
> }

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.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/



More information about the cfe-users mailing list