[clang] 34aa2e2 - Save the result of getenv() to a string
Akira Hatanaka via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 5 22:59:56 PST 2023
Author: Akira Hatanaka
Date: 2023-01-05T22:59:07-08:00
New Revision: 34aa2e24c89ae39c0db4254d8aafcae0285dbe34
URL: https://github.com/llvm/llvm-project/commit/34aa2e24c89ae39c0db4254d8aafcae0285dbe34
DIFF: https://github.com/llvm/llvm-project/commit/34aa2e24c89ae39c0db4254d8aafcae0285dbe34.diff
LOG: Save the result of getenv() to a string
The result has to be saved to a string as the result might be
overwritten by subsequent calls to getenv.
https://pubs.opengroup.org/onlinepubs/009696899/functions/getenv.html
See the discussion here: https://reviews.llvm.org/D137996#4029305
Added:
Modified:
clang/tools/driver/driver.cpp
Removed:
################################################################################
diff --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp
index 126670052421f..0d46f5ec8d1fa 100644
--- a/clang/tools/driver/driver.cpp
+++ b/clang/tools/driver/driver.cpp
@@ -248,11 +248,13 @@ static void getCLEnvVarOptions(std::string &EnvValue, llvm::StringSaver &Saver,
template <class T>
static T checkEnvVar(const char *EnvOptSet, const char *EnvOptFile,
std::string &OptFile) {
- T OptVal = ::getenv(EnvOptSet);
- if (OptVal) {
- if (const char *Var = ::getenv(EnvOptFile))
- OptFile = Var;
- }
+ const char *Str = ::getenv(EnvOptSet);
+ if (!Str)
+ return T{};
+
+ T OptVal = Str;
+ if (const char *Var = ::getenv(EnvOptFile))
+ OptFile = Var;
return OptVal;
}
@@ -264,32 +266,37 @@ static bool SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
TheDriver.CCPrintHeadersFilename)) {
TheDriver.CCPrintHeadersFormat = HIFMT_Textual;
TheDriver.CCPrintHeadersFiltering = HIFIL_None;
- } else if (const char *EnvVar = checkEnvVar<const char *>(
- "CC_PRINT_HEADERS_FORMAT", "CC_PRINT_HEADERS_FILE",
- TheDriver.CCPrintHeadersFilename)) {
- TheDriver.CCPrintHeadersFormat = stringToHeaderIncludeFormatKind(EnvVar);
- if (!TheDriver.CCPrintHeadersFormat) {
- TheDriver.Diag(clang::diag::err_drv_print_header_env_var) << 0 << EnvVar;
- return false;
- }
+ } else {
+ std::string EnvVar = checkEnvVar<std::string>(
+ "CC_PRINT_HEADERS_FORMAT", "CC_PRINT_HEADERS_FILE",
+ TheDriver.CCPrintHeadersFilename);
+ if (!EnvVar.empty()) {
+ TheDriver.CCPrintHeadersFormat =
+ stringToHeaderIncludeFormatKind(EnvVar.c_str());
+ if (!TheDriver.CCPrintHeadersFormat) {
+ TheDriver.Diag(clang::diag::err_drv_print_header_env_var)
+ << 0 << EnvVar;
+ return false;
+ }
- const char *FilteringStr = ::getenv("CC_PRINT_HEADERS_FILTERING");
- HeaderIncludeFilteringKind Filtering;
- if (!stringToHeaderIncludeFiltering(FilteringStr, Filtering)) {
- TheDriver.Diag(clang::diag::err_drv_print_header_env_var)
- << 1 << FilteringStr;
- return false;
- }
+ const char *FilteringStr = ::getenv("CC_PRINT_HEADERS_FILTERING");
+ HeaderIncludeFilteringKind Filtering;
+ if (!stringToHeaderIncludeFiltering(FilteringStr, Filtering)) {
+ TheDriver.Diag(clang::diag::err_drv_print_header_env_var)
+ << 1 << FilteringStr;
+ return false;
+ }
- if ((TheDriver.CCPrintHeadersFormat == HIFMT_Textual &&
- Filtering != HIFIL_None) ||
- (TheDriver.CCPrintHeadersFormat == HIFMT_JSON &&
- Filtering != HIFIL_Only_Direct_System)) {
- TheDriver.Diag(clang::diag::err_drv_print_header_env_var_combination)
- << EnvVar << FilteringStr;
- return false;
+ if ((TheDriver.CCPrintHeadersFormat == HIFMT_Textual &&
+ Filtering != HIFIL_None) ||
+ (TheDriver.CCPrintHeadersFormat == HIFMT_JSON &&
+ Filtering != HIFIL_Only_Direct_System)) {
+ TheDriver.Diag(clang::diag::err_drv_print_header_env_var_combination)
+ << EnvVar << FilteringStr;
+ return false;
+ }
+ TheDriver.CCPrintHeadersFiltering = Filtering;
}
- TheDriver.CCPrintHeadersFiltering = Filtering;
}
TheDriver.CCLogDiagnostics =
More information about the cfe-commits
mailing list