Index: include/clang/Driver/Driver.h =================================================================== --- include/clang/Driver/Driver.h +++ include/clang/Driver/Driver.h @@ -505,6 +505,10 @@ /// GCC goes to extra lengths here to be a bit more robust. std::string GetTemporaryPath(StringRef Prefix, StringRef Suffix) const; + /// GetTemporaryPath - Return the pathname of a temporary directory to use + /// as part of compilation; the directory will have the given prefix. + std::string GetTemporaryDirectory(StringRef Prefix) const; + /// Return the pathname of the pch file in clang-cl mode. std::string GetClPchPath(Compilation &C, StringRef BaseName) const; Index: lib/Driver/Driver.cpp =================================================================== --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -4478,6 +4478,17 @@ return Path.str(); } +std::string Driver::GetTemporaryDirectory(StringRef Prefix) const { + SmallString<128> Path; + std::error_code EC = llvm::sys::fs::createUniqueDirectory(Prefix, Path); + if (EC) { + Diag(clang::diag::err_unable_to_make_temp) << EC.message(); + return ""; + } + + return Path.str(); +} + std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const { SmallString<128> Output; if (Arg *FpArg = C.getArgs().getLastArg(options::OPT__SLASH_Fp)) { Index: lib/Driver/ToolChains/Darwin.cpp =================================================================== --- lib/Driver/ToolChains/Darwin.cpp +++ lib/Driver/ToolChains/Darwin.cpp @@ -224,13 +224,20 @@ options::OPT_fno_application_extension, false)) CmdArgs.push_back("-application_extension"); - if (D.isUsingLTO()) { - // If we are using LTO, then automatically create a temporary file path for - // the linker to use, so that it's lifetime will extend past a possible - // dsymutil step. - if (Version[0] >= 116 && NeedsTempPath(Inputs)) { - const char *TmpPath = C.getArgs().MakeArgString( - D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object))); + if (D.isUsingLTO() && Version[0] >= 116 && NeedsTempPath(Inputs)) { + std::string TmpPathName; + if (D.getLTOMode() == LTOK_Full) { + // If we are using full LTO, then automatically create a temporary file + // path for the linker to use, so that it's lifetime will extend past a + // possible dsymutil step. + TmpPathName = + D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)); + } else if (D.getLTOMode() == LTOK_Thin) + // If we are using thin LTO, then create a directory instead. + TmpPathName = D.GetTemporaryDirectory("thinlto"); + + if (!TmpPathName.empty()) { + auto *TmpPath = C.getArgs().MakeArgString(TmpPathName); C.addTempFile(TmpPath); CmdArgs.push_back("-object_path_lto"); CmdArgs.push_back(TmpPath);