Process: convert Optional to std::optional

This applies to GetEnv and FindInEnvPath.
This commit is contained in:
Krzysztof Parzyszek 2022-12-06 09:53:37 -08:00
parent aa6ea6009f
commit 3c255f679c
19 changed files with 73 additions and 49 deletions

View File

@ -30,6 +30,7 @@
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
#include <map>
#include <optional>
#include <unordered_map>
#include <utility>
@ -144,7 +145,7 @@ void DataAggregator::deleteTempFiles() {
}
void DataAggregator::findPerfExecutable() {
Optional<std::string> PerfExecutable =
std::optional<std::string> PerfExecutable =
sys::Process::FindInEnvPath("PATH", "perf");
if (!PerfExecutable) {
outs() << "PERF2BOLT: No perf executable found!\n";

View File

@ -308,10 +308,19 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider(
DefaultOptions.HeaderFilterRegex = HeaderFilter;
DefaultOptions.SystemHeaders = SystemHeaders;
DefaultOptions.FormatStyle = FormatStyle;
DefaultOptions.User = llvm::sys::Process::GetEnv("USER");
if (auto User = llvm::sys::Process::GetEnv("USER")) // FIXME(kparzysz-quic)
DefaultOptions.User = *User;
else
DefaultOptions.User = std::nullopt;
// USERNAME is used on Windows.
if (!DefaultOptions.User)
DefaultOptions.User = llvm::sys::Process::GetEnv("USERNAME");
// if (!DefaultOptions.User)
// DefaultOptions.User = llvm::sys::Process::GetEnv("USERNAME");
if (!DefaultOptions.User) { // FIXME(kparzysz-quic)
if (auto Username = llvm::sys::Process::GetEnv("USERNAME"))
DefaultOptions.User = *Username;
else
DefaultOptions.User = std::nullopt;
}
ClangTidyOptions OverrideOptions;
if (Checks.getNumOccurrences() > 0)

View File

@ -149,12 +149,13 @@ static void mergeCheckList(llvm::Optional<std::string> &Checks,
TidyProviderRef provideEnvironment() {
static const llvm::Optional<std::string> User = [] {
llvm::Optional<std::string> Ret = llvm::sys::Process::GetEnv("USER");
std::optional<std::string> Ret = llvm::sys::Process::GetEnv("USER");
#ifdef _WIN32
if (!Ret)
return llvm::sys::Process::GetEnv("USERNAME");
Ret = llvm::sys::Process::GetEnv("USERNAME");
#endif
return Ret;
return Ret ? llvm::Optional<std::string>(*Ret)
: llvm::Optional<std::string>();
}();
if (User)

View File

@ -97,6 +97,7 @@
#include <cstdlib> // ::getenv
#include <map>
#include <memory>
#include <optional>
#include <utility>
#if LLVM_ON_UNIX
#include <unistd.h> // getpid
@ -575,7 +576,7 @@ static llvm::Triple computeTargetTriple(const Driver &D,
// On AIX, the env OBJECT_MODE may affect the resulting arch variant.
if (Target.isOSAIX()) {
if (Optional<std::string> ObjectModeValue =
if (std::optional<std::string> ObjectModeValue =
llvm::sys::Process::GetEnv("OBJECT_MODE")) {
StringRef ObjectMode = *ObjectModeValue;
llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
@ -1277,7 +1278,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
A->claim();
PrefixDirs.push_back(A->getValue(0));
}
if (Optional<std::string> CompilerPathValue =
if (std::optional<std::string> CompilerPathValue =
llvm::sys::Process::GetEnv("COMPILER_PATH")) {
StringRef CompilerPath = *CompilerPathValue;
while (!CompilerPath.empty()) {
@ -5460,7 +5461,7 @@ const char *Driver::CreateTempFile(Compilation &C, StringRef Prefix,
StringRef BoundArch) const {
SmallString<128> TmpName;
Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir);
Optional<std::string> CrashDirectory =
std::optional<std::string> CrashDirectory =
CCGenDiagnostics && A
? std::string(A->getValue())
: llvm::sys::Process::GetEnv("CLANG_CRASH_DIAGNOSTICS_DIR");

View File

@ -22,6 +22,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <optional>
#include <system_error>
#define AMDGPU_ARCH_PROGRAM_NAME "amdgpu-arch"
@ -200,7 +201,7 @@ RocmInstallationDetector::getInstallationPathCandidates() {
ROCmSearchDirs.emplace_back(RocmPathArg.str());
DoPrintROCmSearchDirs();
return ROCmSearchDirs;
} else if (Optional<std::string> RocmPathEnv =
} else if (std::optional<std::string> RocmPathEnv =
llvm::sys::Process::GetEnv("ROCM_PATH")) {
if (!RocmPathEnv->empty()) {
ROCmSearchDirs.emplace_back(std::move(*RocmPathEnv));
@ -376,7 +377,7 @@ void RocmInstallationDetector::detectDeviceLibrary() {
if (!RocmDeviceLibPathArg.empty())
LibDevicePath = RocmDeviceLibPathArg[RocmDeviceLibPathArg.size() - 1];
else if (Optional<std::string> LibPathEnv =
else if (std::optional<std::string> LibPathEnv =
llvm::sys::Process::GetEnv("HIP_DEVICE_LIB_PATH"))
LibDevicePath = std::move(*LibPathEnv);

View File

@ -63,6 +63,7 @@
#include "llvm/Support/Threading.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/YAMLParser.h"
#include <optional>
using namespace clang::driver;
using namespace clang::driver::tools;
@ -2142,7 +2143,7 @@ void tools::AddStaticDeviceLibs(Compilation *C, const Tool *T,
SmallVector<std::string, 8> LibraryPaths;
// Add search directories from LIBRARY_PATH env variable
llvm::Optional<std::string> LibPath =
std::optional<std::string> LibPath =
llvm::sys::Process::GetEnv("LIBRARY_PATH");
if (LibPath) {
SmallVector<StringRef, 8> Frags;
@ -2309,7 +2310,7 @@ void tools::addOpenMPDeviceRTL(const Driver &D,
LibraryPaths.emplace_back(DefaultLibPath.c_str());
// Add user defined library paths from LIBRARY_PATH.
llvm::Optional<std::string> LibPath =
std::optional<std::string> LibPath =
llvm::sys::Process::GetEnv("LIBRARY_PATH");
if (LibPath) {
SmallVector<StringRef, 8> Frags;

View File

@ -48,6 +48,7 @@
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
#include <optional>
#include <set>
#include <system_error>
using namespace clang;
@ -412,7 +413,7 @@ int clang_main(int Argc, char **Argv) {
// prepended or appended.
if (ClangCLMode) {
// Arguments in "CL" are prepended.
llvm::Optional<std::string> OptCL = llvm::sys::Process::GetEnv("CL");
std::optional<std::string> OptCL = llvm::sys::Process::GetEnv("CL");
if (OptCL) {
SmallVector<const char *, 8> PrependedOpts;
getCLEnvVarOptions(OptCL.value(), Saver, PrependedOpts);
@ -421,7 +422,7 @@ int clang_main(int Argc, char **Argv) {
Args.insert(Args.begin() + 1, PrependedOpts.begin(), PrependedOpts.end());
}
// Arguments in "_CL_" are appended.
llvm::Optional<std::string> Opt_CL_ = llvm::sys::Process::GetEnv("_CL_");
std::optional<std::string> Opt_CL_ = llvm::sys::Process::GetEnv("_CL_");
if (Opt_CL_) {
SmallVector<const char *, 8> AppendedOpts;
getCLEnvVarOptions(Opt_CL_.value(), Saver, AppendedOpts);

View File

@ -639,7 +639,7 @@ void LinkerDriver::addWinSysRootLibSearchPaths() {
// Parses LIB environment which contains a list of search paths.
void LinkerDriver::addLibSearchPaths() {
Optional<std::string> envOpt = Process::GetEnv("LIB");
std::optional<std::string> envOpt = Process::GetEnv("LIB");
if (!envOpt)
return;
StringRef env = saver().save(*envOpt);

View File

@ -34,6 +34,7 @@
#include "llvm/WindowsManifest/WindowsManifestMerger.h"
#include <limits>
#include <memory>
#include <optional>
using namespace llvm::COFF;
using namespace llvm;
@ -931,11 +932,11 @@ ParsedDirectives ArgParser::parseDirectives(StringRef s) {
// So you can pass extra arguments using them.
void ArgParser::addLINK(SmallVector<const char *, 256> &argv) {
// Concatenate LINK env and command line arguments, and then parse them.
if (Optional<std::string> s = Process::GetEnv("LINK")) {
if (std::optional<std::string> s = Process::GetEnv("LINK")) {
std::vector<const char *> v = tokenize(*s);
argv.insert(std::next(argv.begin()), v.begin(), v.end());
}
if (Optional<std::string> s = Process::GetEnv("_LINK_")) {
if (std::optional<std::string> s = Process::GetEnv("_LINK_")) {
std::vector<const char *> v = tokenize(*s);
argv.insert(std::next(argv.begin()), v.begin(), v.end());
}

View File

@ -24,11 +24,11 @@
#ifndef LLVM_SUPPORT_PROCESS_H
#define LLVM_SUPPORT_PROCESS_H
#include "llvm/ADT/Optional.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Program.h"
#include <optional>
#include <system_error>
namespace llvm {
@ -97,7 +97,7 @@ public:
// This function returns the environment variable \arg name's value as a UTF-8
// string. \arg Name is assumed to be in UTF-8 encoding too.
static Optional<std::string> GetEnv(StringRef name);
static std::optional<std::string> GetEnv(StringRef name);
/// This function searches for an existing file in the list of directories
/// in a PATH like environment variable, and returns the first file found,
@ -105,14 +105,14 @@ public:
/// variable. If an ignore list is specified, then any folder which is in
/// the PATH like environment variable but is also in IgnoreList is not
/// considered.
static Optional<std::string> FindInEnvPath(StringRef EnvName,
StringRef FileName,
ArrayRef<std::string> IgnoreList,
char Separator = EnvPathSeparator);
static std::optional<std::string>
FindInEnvPath(StringRef EnvName, StringRef FileName,
ArrayRef<std::string> IgnoreList,
char Separator = EnvPathSeparator);
static Optional<std::string> FindInEnvPath(StringRef EnvName,
StringRef FileName,
char Separator = EnvPathSeparator);
static std::optional<std::string>
FindInEnvPath(StringRef EnvName, StringRef FileName,
char Separator = EnvPathSeparator);
// This functions ensures that the standard file descriptors (input, output,
// and error) are properly mapped to a file descriptor before we use any of

View File

@ -62,6 +62,7 @@
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
#include "llvm/Transforms/ObjCARC.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <optional>
#include <system_error>
using namespace llvm;
@ -274,7 +275,7 @@ bool LTOCodeGenerator::runAIXSystemAssembler(SmallString<128> &AssemblyFile) {
// Setup the LDR_CNTRL variable
std::string LDR_CNTRL_var = "LDR_CNTRL=MAXDATA32=0xA0000000@DSA";
if (Optional<std::string> V = sys::Process::GetEnv("LDR_CNTRL"))
if (std::optional<std::string> V = sys::Process::GetEnv("LDR_CNTRL"))
LDR_CNTRL_var += ("@" + *V);
// Prepare inputs for the assember.

View File

@ -45,6 +45,7 @@
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdlib>
#include <optional>
#include <string>
using namespace llvm;
using namespace cl;
@ -1363,7 +1364,7 @@ bool cl::expandResponseFiles(int Argc, const char *const *Argv,
: cl::TokenizeGNUCommandLine;
// The environment variable specifies initial options.
if (EnvVar)
if (llvm::Optional<std::string> EnvValue = sys::Process::GetEnv(EnvVar))
if (std::optional<std::string> EnvValue = sys::Process::GetEnv(EnvVar))
Tokenize(*EnvValue, Saver, NewArgv, /*MarkEOLs=*/false);
// Command line options can override the environment variable.
@ -1456,7 +1457,7 @@ bool cl::ParseCommandLineOptions(int argc, const char *const *argv,
// Parse options from environment variable.
if (EnvVar) {
if (llvm::Optional<std::string> EnvValue =
if (std::optional<std::string> EnvValue =
sys::Process::GetEnv(StringRef(EnvVar)))
TokenizeGNUCommandLine(*EnvValue, Saver, NewArgv);
}

View File

@ -20,6 +20,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include <optional>
#include <stdlib.h> // for _Exit
using namespace llvm;
@ -30,18 +31,17 @@ using namespace sys;
//=== independent code.
//===----------------------------------------------------------------------===//
Optional<std::string>
std::optional<std::string>
Process::FindInEnvPath(StringRef EnvName, StringRef FileName, char Separator) {
return FindInEnvPath(EnvName, FileName, {}, Separator);
}
Optional<std::string> Process::FindInEnvPath(StringRef EnvName,
StringRef FileName,
ArrayRef<std::string> IgnoreList,
char Separator) {
std::optional<std::string>
Process::FindInEnvPath(StringRef EnvName, StringRef FileName,
ArrayRef<std::string> IgnoreList, char Separator) {
assert(!path::is_absolute(FileName));
Optional<std::string> FoundPath;
Optional<std::string> OptPath = Process::GetEnv(EnvName);
std::optional<std::string> FoundPath;
std::optional<std::string> OptPath = Process::GetEnv(EnvName);
if (!OptPath)
return FoundPath;

View File

@ -15,6 +15,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Config/config.h"
#include <mutex>
#include <optional>
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
@ -173,7 +174,7 @@ void Process::PreventCoreFiles() {
coreFilesPrevented = true;
}
Optional<std::string> Process::GetEnv(StringRef Name) {
std::optional<std::string> Process::GetEnv(StringRef Name) {
std::string NameStr = Name.str();
const char *Val = ::getenv(NameStr.c_str());
if (!Val)

View File

@ -17,6 +17,7 @@
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/WindowsError.h"
#include <malloc.h>
#include <optional>
// The Windows.h header must be after LLVM and standard headers.
#include "llvm/Support/Windows/WindowsSupport.h"
@ -116,7 +117,7 @@ void Process::PreventCoreFiles() {
/// Returns the environment variable \arg Name's value as a string encoded in
/// UTF-8. \arg Name is assumed to be in UTF-8 encoding.
Optional<std::string> Process::GetEnv(StringRef Name) {
std::optional<std::string> Process::GetEnv(StringRef Name) {
// Convert the argument to UTF-16 to pass it to _wgetenv().
SmallVector<wchar_t, 128> NameUTF16;
if (windows::UTF8ToUTF16(Name, NameUTF16))

View File

@ -28,6 +28,7 @@
#include "llvm/Support/Process.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
using namespace llvm;
@ -76,7 +77,7 @@ static std::vector<StringRef> getSearchPaths(opt::InputArgList *Args,
Ret.push_back(Arg->getValue());
// Add $LIB.
Optional<std::string> EnvOpt = sys::Process::GetEnv("LIB");
std::optional<std::string> EnvOpt = sys::Process::GetEnv("LIB");
if (!EnvOpt)
return Ret;
StringRef Env = Saver.save(*EnvOpt);

View File

@ -19,6 +19,7 @@
#include "llvm/Support/Program.h"
#include "llvm/Support/VersionTuple.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <optional>
#include <string>
#ifdef _WIN32
@ -504,7 +505,7 @@ bool findVCToolChainViaEnvironment(vfs::FileSystem &VFS, std::string &Path,
ToolsetLayout &VSLayout) {
// These variables are typically set by vcvarsall.bat
// when launching a developer command prompt.
if (Optional<std::string> VCToolsInstallDir =
if (std::optional<std::string> VCToolsInstallDir =
sys::Process::GetEnv("VCToolsInstallDir")) {
// This is only set by newer Visual Studios, and it leads straight to
// the toolchain directory.
@ -512,7 +513,7 @@ bool findVCToolChainViaEnvironment(vfs::FileSystem &VFS, std::string &Path,
VSLayout = ToolsetLayout::VS2017OrNewer;
return true;
}
if (Optional<std::string> VCInstallDir =
if (std::optional<std::string> VCInstallDir =
sys::Process::GetEnv("VCINSTALLDIR")) {
// If the previous variable isn't set but this one is, then we've found
// an older Visual Studio. This variable is set by newer Visual Studios too,
@ -526,7 +527,7 @@ bool findVCToolChainViaEnvironment(vfs::FileSystem &VFS, std::string &Path,
// We couldn't find any VC environment variables. Let's walk through PATH and
// see if it leads us to a VC toolchain bin directory. If it does, pick the
// first one that we find.
if (Optional<std::string> PathEnv = sys::Process::GetEnv("PATH")) {
if (std::optional<std::string> PathEnv = sys::Process::GetEnv("PATH")) {
SmallVector<StringRef, 8> PathEntries;
StringRef(*PathEnv).split(PathEntries, sys::EnvPathSeparator);
for (StringRef PathEntry : PathEntries) {

View File

@ -44,6 +44,7 @@
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/WithColor.h"
#include <ctime>
#include <optional>
using namespace llvm;
using namespace llvm::opt;
@ -303,7 +304,7 @@ int main(int Argc, char **Argv) {
std::vector<std::string> IncludeDirs =
InputArgs.getAllArgValues(OPT_include_path);
if (!InputArgs.hasArg(OPT_ignore_include_envvar)) {
if (llvm::Optional<std::string> IncludeEnvVar =
if (std::optional<std::string> IncludeEnvVar =
llvm::sys::Process::GetEnv("INCLUDE")) {
SmallVector<StringRef, 8> Dirs;
StringRef(*IncludeEnvVar)

View File

@ -11,6 +11,7 @@
#include "llvm/Support/Host.h"
#include "llvm/Support/Process.h"
#include "gtest/gtest.h"
#include <optional>
#ifdef _WIN32
#include <windows.h>
@ -45,13 +46,13 @@ TEST(ProcessTest, GetRandomNumberTest) {
#if HAVE_SETENV || _MSC_VER
TEST(ProcessTest, Basic) {
setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
std::optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
EXPECT_TRUE(val.has_value());
EXPECT_STREQ("abc", val->c_str());
}
TEST(ProcessTest, None) {
Optional<std::string> val(
std::optional<std::string> val(
Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
EXPECT_FALSE(val.has_value());
}
@ -61,14 +62,14 @@ TEST(ProcessTest, None) {
TEST(ProcessTest, EmptyVal) {
SetEnvironmentVariableA("__LLVM_TEST_ENVIRON_VAR__", "");
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
std::optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
EXPECT_TRUE(val.has_value());
EXPECT_STREQ("", val->c_str());
}
TEST(ProcessTest, Wchar) {
SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
std::optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
EXPECT_TRUE(val.has_value());
EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
}