@@ -721,6 +721,38 @@ static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
721721 }
722722}
723723
724+ static void appendUserToPath (SmallVectorImpl<char > &Result) {
725+ #ifdef LLVM_ON_UNIX
726+ const char *Username = getenv (" LOGNAME" );
727+ #else
728+ const char *Username = getenv (" USERNAME" );
729+ #endif
730+ if (Username) {
731+ // Validate that LoginName can be used in a path, and get its length.
732+ size_t Len = 0 ;
733+ for (const char *P = Username; *P; ++P, ++Len) {
734+ if (!clang::isAlphanumeric (*P) && *P != ' _' ) {
735+ Username = nullptr ;
736+ break ;
737+ }
738+ }
739+
740+ if (Username && Len > 0 ) {
741+ Result.append (Username, Username + Len);
742+ return ;
743+ }
744+ }
745+
746+ // Fallback to user id.
747+ #ifdef LLVM_ON_UNIX
748+ std::string UID = llvm::utostr (getuid ());
749+ #else
750+ // FIXME: Windows seems to have an 'SID' that might work.
751+ std::string UID = " 9999" ;
752+ #endif
753+ Result.append (UID.begin (), UID.end ());
754+ }
755+
724756static void addPGOAndCoverageFlags (const ToolChain &TC, Compilation &C,
725757 const Driver &D, const InputInfo &Output,
726758 const ArgList &Args,
@@ -3169,13 +3201,11 @@ static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T,
31693201 CmdArgs.push_back (" -fno-math-builtin" );
31703202}
31713203
3172- bool Driver::getDefaultModuleCachePath (SmallVectorImpl<char > &Result) {
3173- if (llvm::sys::path::cache_directory (Result)) {
3174- llvm::sys::path::append (Result, " clang" );
3175- llvm::sys::path::append (Result, " ModuleCache" );
3176- return true ;
3177- }
3178- return false ;
3204+ void Driver::getDefaultModuleCachePath (SmallVectorImpl<char > &Result) {
3205+ llvm::sys::path::system_temp_directory (/* erasedOnReboot=*/ false , Result);
3206+ llvm::sys::path::append (Result, " org.llvm.clang." );
3207+ appendUserToPath (Result);
3208+ llvm::sys::path::append (Result, " ModuleCache" );
31793209}
31803210
31813211static void RenderModulesOptions (Compilation &C, const Driver &D,
@@ -3232,7 +3262,6 @@ static void RenderModulesOptions(Compilation &C, const Driver &D,
32323262 if (Arg *A = Args.getLastArg (options::OPT_fmodules_cache_path))
32333263 Path = A->getValue ();
32343264
3235- bool HasPath = true ;
32363265 if (C.isForDiagnostics ()) {
32373266 // When generating crash reports, we want to emit the modules along with
32383267 // the reproduction sources, so we ignore any provided module path.
@@ -3241,16 +3270,12 @@ static void RenderModulesOptions(Compilation &C, const Driver &D,
32413270 llvm::sys::path::append (Path, " modules" );
32423271 } else if (Path.empty ()) {
32433272 // No module path was provided: use the default.
3244- HasPath = Driver::getDefaultModuleCachePath (Path);
3273+ Driver::getDefaultModuleCachePath (Path);
32453274 }
32463275
3247- // `HasPath` will only be false if getDefaultModuleCachePath() fails.
3248- // That being said, that failure is unlikely and not caching is harmless.
3249- if (HasPath) {
3250- const char Arg[] = " -fmodules-cache-path=" ;
3251- Path.insert (Path.begin (), Arg, Arg + strlen (Arg));
3252- CmdArgs.push_back (Args.MakeArgString (Path));
3253- }
3276+ const char Arg[] = " -fmodules-cache-path=" ;
3277+ Path.insert (Path.begin (), Arg, Arg + strlen (Arg));
3278+ CmdArgs.push_back (Args.MakeArgString (Path));
32543279 }
32553280
32563281 if (HaveModules) {
0 commit comments