Skip to content

Commit

Permalink
Process: convert Optional to std::optional
Browse files Browse the repository at this point in the history
This applies to GetEnv and FindInEnvPath.
  • Loading branch information
Krzysztof Parzyszek committed Dec 6, 2022
1 parent aa6ea60 commit 3c255f6
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 50 deletions.
3 changes: 2 additions & 1 deletion bolt/lib/Profile/DataAggregator.cpp
Expand Up @@ -30,6 +30,7 @@
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
#include <map>
#include <optional>
#include <unordered_map>
#include <utility>

Expand Down Expand Up @@ -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";
Expand Down
15 changes: 12 additions & 3 deletions clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions clang-tools-extra/clangd/TidyProvider.cpp
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/Driver/Driver.cpp
Expand Up @@ -97,6 +97,7 @@
#include <cstdlib> // ::getenv
#include <map>
#include <memory>
#include <optional>
#include <utility>
#if LLVM_ON_UNIX
#include <unistd.h> // getpid
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Driver/ToolChains/AMDGPU.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions clang/tools/driver/driver.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lld/COFF/Driver.cpp
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions lld/COFF/DriverUtils.cpp
Expand Up @@ -34,6 +34,7 @@
#include "llvm/WindowsManifest/WindowsManifestMerger.h"
#include <limits>
#include <memory>
#include <optional>

using namespace llvm::COFF;
using namespace llvm;
Expand Down Expand Up @@ -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());
}
Expand Down
20 changes: 10 additions & 10 deletions llvm/include/llvm/Support/Process.h
Expand Up @@ -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 {
Expand Down Expand Up @@ -97,22 +97,22 @@ class Process {

// 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,
/// according to the order of the entries in the PATH like environment
/// 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 Optional<std::string> FindInEnvPath(StringRef EnvName,
StringRef FileName,
char Separator = EnvPathSeparator);
static std::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,
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
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/LTO/LTOCodeGenerator.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Support/CommandLine.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions llvm/lib/Support/Process.cpp
Expand Up @@ -20,6 +20,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"

#include <optional>
#include <stdlib.h> // for _Exit

using namespace llvm;
Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Support/Unix/Process.inc
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Support/Windows/Process.inc
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3c255f6

Please sign in to comment.