Make the default module cache user-specific

Appends the username to the first component (after the temp dir) of the
module cache path.  If the username contains a character that shouldn't
go into a path (for now conservatively allow [a-zA-Z0-9_]), we fallback
to the user id.

llvm-svn: 228013
This commit is contained in:
Ben Langmuir 2015-02-03 19:28:37 +00:00
parent a5ef4905a5
commit 3b7b540680
2 changed files with 43 additions and 1 deletions

View File

@ -40,6 +40,10 @@
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#ifdef LLVM_ON_UNIX
#include <unistd.h> // For getuid().
#endif
using namespace clang::driver;
using namespace clang::driver::tools;
using namespace clang;
@ -2456,6 +2460,38 @@ static void claimNoWarnArgs(const ArgList &Args) {
Args.ClaimAllArgs(options::OPT_fno_lto);
}
static void appendUserToPath(SmallVectorImpl<char> &Result) {
#ifdef LLVM_ON_UNIX
const char *Username = getenv("LOGNAME");
#else
const char *Username = getenv("USERNAME");
#endif
if (Username) {
// Validate that LoginName can be used in a path, and get its length.
size_t Len = 0;
for (const char *P = Username; *P; ++P, ++Len) {
if (!isalnum(*P) && *P != '_') {
Username = nullptr;
break;
}
}
if (Username && Len > 0) {
Result.append(Username, Username + Len);
return;
}
}
// Fallback to user id.
#ifdef LLVM_ON_UNIX
std::string UID = llvm::utostr(getuid());
#else
// FIXME: Windows seems to have an 'SID' that might work.
std::string UID = "9999";
#endif
Result.append(UID.begin(), UID.end());
}
void Clang::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output,
const InputInfoList &Inputs,
@ -3878,7 +3914,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
// No module path was provided: use the default.
llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
ModuleCachePath);
llvm::sys::path::append(ModuleCachePath, "org.llvm.clang");
llvm::sys::path::append(ModuleCachePath, "org.llvm.clang.");
appendUserToPath(ModuleCachePath);
llvm::sys::path::append(ModuleCachePath, "ModuleCache");
}
const char Arg[] = "-fmodules-cache-path=";

View File

@ -0,0 +1,5 @@
// RUN: env USERNAME=asdf LOGNAME=asdf %clang -fmodules -### %s 2>&1 | FileCheck %s -check-prefix=CHECK-SET
// CHECK-SET: -fmodules-cache-path={{.*}}org.llvm.clang.asdf{{[/\\]+}}ModuleCache
// RUN: %clang -fmodules -### %s 2>&1 | FileCheck %s -check-prefix=CHECK-DEFAULT
// CHECK-DEFAULT: -fmodules-cache-path={{.*}}org.llvm.clang.{{[A-Za-z0-9_]*[/\\]+}}ModuleCache