From 85c25b4d508baee01ef8f5546649fcecf54fc1d0 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Sun, 24 Jul 2016 17:44:03 +0000 Subject: [PATCH] [Driver] Switch some getenv calls to llvm::sys::Process::GetEnv No functional change is intended. llvm-svn: 276573 --- clang/lib/Driver/Driver.cpp | 5 ++-- clang/lib/Driver/MSVCToolChain.cpp | 51 +++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 8034e47..1b95852 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -455,8 +455,9 @@ Compilation *Driver::BuildCompilation(ArrayRef ArgList) { // FIXME: Handle environment options which affect driver behavior, somewhere // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS. - if (char *env = ::getenv("COMPILER_PATH")) { - StringRef CompilerPath = env; + if (Optional CompilerPathValue = + llvm::sys::Process::GetEnv("COMPILER_PATH")) { + StringRef CompilerPath = *CompilerPathValue; while (!CompilerPath.empty()) { std::pair Split = CompilerPath.split(llvm::sys::EnvPathSeparator); diff --git a/clang/lib/Driver/MSVCToolChain.cpp b/clang/lib/Driver/MSVCToolChain.cpp index 64323c4..fc2128f 100644 --- a/clang/lib/Driver/MSVCToolChain.cpp +++ b/clang/lib/Driver/MSVCToolChain.cpp @@ -513,9 +513,9 @@ VersionTuple MSVCToolChain::getMSVCVersionFromExe() const { // Get Visual Studio installation directory. bool MSVCToolChain::getVisualStudioInstallDir(std::string &path) const { // First check the environment variables that vsvars32.bat sets. - const char *vcinstalldir = getenv("VCINSTALLDIR"); - if (vcinstalldir) { - path = vcinstalldir; + if (llvm::Optional VcInstallDir = + llvm::sys::Process::GetEnv("VCINSTALLDIR")) { + path = std::move(*VcInstallDir); path = path.substr(0, path.find("\\VC")); return true; } @@ -541,26 +541,26 @@ bool MSVCToolChain::getVisualStudioInstallDir(std::string &path) const { } // Try the environment. - const char *vs120comntools = getenv("VS120COMNTOOLS"); - const char *vs100comntools = getenv("VS100COMNTOOLS"); - const char *vs90comntools = getenv("VS90COMNTOOLS"); - const char *vs80comntools = getenv("VS80COMNTOOLS"); - - const char *vscomntools = nullptr; - - // Find any version we can - if (vs120comntools) - vscomntools = vs120comntools; - else if (vs100comntools) - vscomntools = vs100comntools; - else if (vs90comntools) - vscomntools = vs90comntools; - else if (vs80comntools) - vscomntools = vs80comntools; - - if (vscomntools && *vscomntools) { - const char *p = strstr(vscomntools, "\\Common7\\Tools"); - path = p ? std::string(vscomntools, p) : vscomntools; + std::string vcomntools; + if (llvm::Optional vs120comntools = + llvm::sys::Process::GetEnv("VS120COMNTOOLS")) + vcomntools = std::move(*vs120comntools); + else if (llvm::Optional vs100comntools = + llvm::sys::Process::GetEnv("VS100COMNTOOLS")) + vcomntools = std::move(*vs100comntools); + else if (llvm::Optional vs90comntools = + llvm::sys::Process::GetEnv("VS90COMNTOOLS")) + vcomntools = std::move(*vs90comntools); + else if (llvm::Optional vs80comntools = + llvm::sys::Process::GetEnv("VS80COMNTOOLS")) + vcomntools = std::move(*vs80comntools); + + // Find any version we can. + if (!vcomntools.empty()) { + size_t p = vcomntools.find("\\Common7\\Tools"); + if (p != std::string::npos) + vcomntools.resize(p); + path = std::move(vcomntools); return true; } return false; @@ -593,9 +593,10 @@ void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, return; // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat. - if (const char *cl_include_dir = getenv("INCLUDE")) { + if (llvm::Optional cl_include_dir = + llvm::sys::Process::GetEnv("INCLUDE")) { SmallVector Dirs; - StringRef(cl_include_dir) + StringRef(*cl_include_dir) .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false); for (StringRef Dir : Dirs) addSystemInclude(DriverArgs, CC1Args, Dir); -- 2.7.4