From 8ffd0a4eb8875f452e407effe49d86d1620a4499 Mon Sep 17 00:00:00 2001 From: Rafael Espindola <rafael.espindola@gmail.com> Date: Fri, 28 Jun 2013 03:49:04 +0000 Subject: [PATCH] Update for llvm::sys::fs::unique_file not creating directories. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185127 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Frontend/CompilerInstance.cpp | 34 +++++++++++++++++++------------ test/Frontend/output-failures.c | 2 +- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp index ec1f9590606..5ffee3032df 100644 --- a/lib/Frontend/CompilerInstance.cpp +++ b/lib/Frontend/CompilerInstance.cpp @@ -539,24 +539,27 @@ CompilerInstance::createOutputFile(StringRef OutputPath, } if (UseTemporary) { - SmallString<256> AbsPath(OutputPath); - llvm::sys::fs::make_absolute(AbsPath); - - // If the parent directory doesn't exist and we can't create it, fail. - bool ParentExists = - llvm::sys::fs::exists(llvm::sys::path::parent_path(AbsPath.str())); - if (!CreateMissingDirectories && !ParentExists) { - Error = "Parent directory doesn't exist"; - return 0; - } - // Create a temporary file. SmallString<128> TempPath; TempPath = OutFile; TempPath += "-%%%%%%%%"; int fd; - if (!llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, - /*makeAbsolute=*/false, 0664)) { + llvm::error_code EC = llvm::sys::fs::unique_file( + TempPath.str(), fd, TempPath, /*makeAbsolute=*/ false, 0664); + + if (CreateMissingDirectories && + (EC == llvm::errc::no_such_file_or_directory || + EC == llvm::windows_error::file_not_found || + EC == llvm::windows_error::path_not_found)) { + StringRef Parent = llvm::sys::path::parent_path(OutputPath); + EC = llvm::sys::fs::create_directories(Parent); + if (!EC) { + EC = llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, + /*makeAbsolute=*/ false, 0664); + } + } + + if (!EC) { OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); OSFile = TempFile = TempPath.str(); } @@ -780,6 +783,11 @@ static void compileModule(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName) { + // FIXME: have LockFileManager return an error_code so that we can + // avoid the mkdir when the directory already exists. + StringRef Dir = llvm::sys::path::parent_path(ModuleFileName); + llvm::sys::fs::create_directories(Dir); + llvm::LockFileManager Locked(ModuleFileName); switch (Locked) { case llvm::LockFileManager::LFS_Error: diff --git a/test/Frontend/output-failures.c b/test/Frontend/output-failures.c index 2b5aba6ab9e..e2af7c7ddc9 100644 --- a/test/Frontend/output-failures.c +++ b/test/Frontend/output-failures.c @@ -1,4 +1,4 @@ // RUN: not %clang_cc1 -emit-llvm -o %S/doesnotexist/somename %s 2> %t // RUN: FileCheck -check-prefix=OUTPUTFAIL -input-file=%t %s -// OUTPUTFAIL: unable to open output file '{{.*}}doesnotexist{{.*}}': 'Parent directory doesn't exist' +// OUTPUTFAIL: Error opening output file '{{.*}}doesnotexist{{.*}}' -- GitLab