aboutsummaryrefslogtreecommitdiff
path: root/clang/lib
diff options
context:
space:
mode:
authorDavid Zarzycki <dave@znu.io>2020-06-23 05:43:51 -0400
committerDavid Zarzycki <dave@znu.io>2020-06-26 07:46:03 -0400
commitbb26838ceffb5feaa18186f55f7525a08084899e (patch)
tree048bc4c02a056d9bcbb9a303d85f372d7b0a89d2 /clang/lib
parentda2f852e1913a16a1c6940ce3d3e47158ae5ba0e (diff)
downloadllvm-bb26838ceffb5feaa18186f55f7525a08084899e.zip
llvm-bb26838ceffb5feaa18186f55f7525a08084899e.tar.gz
llvm-bb26838ceffb5feaa18186f55f7525a08084899e.tar.bz2
[clang driver] Move default module cache from system temporary directory
1) Shared writable directories like /tmp are a security problem. 2) Systems provide dedicated cache directories these days anyway. 3) This also refines LLVM's cache_directory() on Darwin platforms to use the Darwin per-user cache directory. Reviewers: compnerd, aprantl, jakehehrlich, espindola, respindola, ilya-biryukov, pcc, sammccall Reviewed By: compnerd, sammccall Subscribers: hiraditya, llvm-commits, cfe-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D82362
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/Driver/ToolChains/Clang.cpp57
1 files changed, 16 insertions, 41 deletions
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 8903641..1fd638f 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -721,38 +721,6 @@ static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
}
}
-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 (!clang::isAlphanumeric(*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());
-}
-
static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
const Driver &D, const InputInfo &Output,
const ArgList &Args,
@@ -3201,11 +3169,13 @@ static void RenderBuiltinOptions(const ToolChain &TC, const llvm::Triple &T,
CmdArgs.push_back("-fno-math-builtin");
}
-void Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
- llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false, Result);
- llvm::sys::path::append(Result, "org.llvm.clang.");
- appendUserToPath(Result);
- llvm::sys::path::append(Result, "ModuleCache");
+bool Driver::getDefaultModuleCachePath(SmallVectorImpl<char> &Result) {
+ if (llvm::sys::path::cache_directory(Result)) {
+ llvm::sys::path::append(Result, "clang");
+ llvm::sys::path::append(Result, "ModuleCache");
+ return true;
+ }
+ return false;
}
static void RenderModulesOptions(Compilation &C, const Driver &D,
@@ -3262,6 +3232,7 @@ static void RenderModulesOptions(Compilation &C, const Driver &D,
if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path))
Path = A->getValue();
+ bool HasPath = true;
if (C.isForDiagnostics()) {
// When generating crash reports, we want to emit the modules along with
// the reproduction sources, so we ignore any provided module path.
@@ -3270,12 +3241,16 @@ static void RenderModulesOptions(Compilation &C, const Driver &D,
llvm::sys::path::append(Path, "modules");
} else if (Path.empty()) {
// No module path was provided: use the default.
- Driver::getDefaultModuleCachePath(Path);
+ HasPath = Driver::getDefaultModuleCachePath(Path);
}
- const char Arg[] = "-fmodules-cache-path=";
- Path.insert(Path.begin(), Arg, Arg + strlen(Arg));
- CmdArgs.push_back(Args.MakeArgString(Path));
+ // `HasPath` will only be false if getDefaultModuleCachePath() fails.
+ // That being said, that failure is unlikely and not caching is harmless.
+ if (HasPath) {
+ const char Arg[] = "-fmodules-cache-path=";
+ Path.insert(Path.begin(), Arg, Arg + strlen(Arg));
+ CmdArgs.push_back(Args.MakeArgString(Path));
+ }
}
if (HaveModules) {