[clang] 924996e - [clang] [Driver] Disable default configs via envvar during testing

Michał Górny via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 30 00:12:04 PDT 2022


Author: Michał Górny
Date: 2022-09-30T09:11:50+02:00
New Revision: 924996e0a011eb833a72d2a2cac9b40fa8a42e34

URL: https://github.com/llvm/llvm-project/commit/924996e0a011eb833a72d2a2cac9b40fa8a42e34
DIFF: https://github.com/llvm/llvm-project/commit/924996e0a011eb833a72d2a2cac9b40fa8a42e34.diff

LOG: [clang] [Driver] Disable default configs via envvar during testing

Add support for a CLANG_NO_DEFAULT_CONFIG envvar that works like
the --no-default-config option when set to a non-empty value.  Use it
to disable loading system configuration files during the test suite
runs.

Configuration files can change the driver behavior in extensive ways,
and it is neither really possible nor feasible to account for or undo
the effects of even the most common configuration uses.  Therefore,
the most reasonable option seems to be to ignore configuration files
while running the majority of tests (with the notable exception of tests
for configuration file support).

Due to the diversity of ways that %clang is used in the test suite,
including using it to copy or symlink the clang executable, as well to
call -cc1 and -cc1as modes, it is not feasible to pass the explicit
options to disable config loading either.  Using an environment variable
has the advantage of being easily applied across the test suite
and easily unset for default configuration file loading tests.

Differential Revision: https://reviews.llvm.org/D134905

Added: 
    

Modified: 
    clang/lib/Driver/Driver.cpp
    clang/test/Driver/config-file3.c
    clang/test/Driver/env.c
    clang/test/Unit/lit.cfg.py
    clang/test/lit.cfg.py

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index b8ef37904574c..a4bd2ded3c17a 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -94,6 +94,7 @@
 #include "llvm/Support/StringSaver.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Support/raw_ostream.h"
+#include <cstdlib> // ::getenv
 #include <map>
 #include <memory>
 #include <utility>
@@ -1066,6 +1067,12 @@ bool Driver::loadConfigFiles() {
 }
 
 bool Driver::loadDefaultConfigFiles(ArrayRef<StringRef> CfgFileSearchDirs) {
+  // Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
+  // value.
+  if (const char *NoConfigEnv = ::getenv("CLANG_NO_DEFAULT_CONFIG")) {
+    if (*NoConfigEnv)
+      return false;
+  }
   if (CLOptions && CLOptions->hasArg(options::OPT_no_default_config))
     return false;
 

diff  --git a/clang/test/Driver/config-file3.c b/clang/test/Driver/config-file3.c
index 633ad740c0401..5f712020e79fd 100644
--- a/clang/test/Driver/config-file3.c
+++ b/clang/test/Driver/config-file3.c
@@ -1,6 +1,7 @@
 // REQUIRES: shell
 // REQUIRES: x86-registered-target
 
+// RUN: unset CLANG_NO_DEFAULT_CONFIG
 // RUN: rm -rf %t && mkdir %t
 
 //--- If config file is specified by relative path (workdir/cfg-s2), it is searched for by that path.

diff  --git a/clang/test/Driver/env.c b/clang/test/Driver/env.c
index 413b04e2cb629..0a60739db8718 100644
--- a/clang/test/Driver/env.c
+++ b/clang/test/Driver/env.c
@@ -4,13 +4,13 @@
 //
 // REQUIRES: shell
 // The PATH variable is heavily used when trying to find a linker.
-// RUN: env -i LC_ALL=C LD_LIBRARY_PATH="$LD_LIBRARY_PATH" \
+// RUN: env -i LC_ALL=C LD_LIBRARY_PATH="$LD_LIBRARY_PATH" CLANG_NO_DEFAULT_CONFIG=1 \
 // RUN:   %clang %s -### -o %t.o --target=i386-unknown-linux \
 // RUN:     --sysroot=%S/Inputs/basic_linux_tree \
 // RUN:     --rtlib=platform -no-pie \
 // RUN:     --gcc-toolchain="" 2>&1 | FileCheck --check-prefix=CHECK-LD-32 %s
 //
-// RUN: env -i LC_ALL=C PATH="" LD_LIBRARY_PATH="$LD_LIBRARY_PATH" \
+// RUN: env -i LC_ALL=C PATH="" LD_LIBRARY_PATH="$LD_LIBRARY_PATH" CLANG_NO_DEFAULT_CONFIG=1 \
 // RUN:   %clang %s -### -o %t.o --target=i386-unknown-linux \
 // RUN:     --sysroot=%S/Inputs/basic_linux_tree \
 // RUN:     --rtlib=platform -no-pie \

diff  --git a/clang/test/Unit/lit.cfg.py b/clang/test/Unit/lit.cfg.py
index 8e3e6f2e862d7..bd75b3bb39682 100644
--- a/clang/test/Unit/lit.cfg.py
+++ b/clang/test/Unit/lit.cfg.py
@@ -71,3 +71,8 @@ def find_shlibpath_var():
 else:
     lit_config.warning("unable to inject shared library path on '{}'"
                        .format(platform.system()))
+
+# It is not realistically possible to account for all options that could
+# possibly be present in system and user configuration files, so disable
+# default configs for the test runs.
+config.environment["CLANG_NO_DEFAULT_CONFIG"] = "1"

diff  --git a/clang/test/lit.cfg.py b/clang/test/lit.cfg.py
index 590b73a702572..278fbdc79c601 100644
--- a/clang/test/lit.cfg.py
+++ b/clang/test/lit.cfg.py
@@ -284,3 +284,8 @@ def exclude_unsupported_files_for_aix(dirname):
 
 if 'system-aix' in config.available_features:
         config.substitutions.append(('llvm-nm', 'env OBJECT_MODE=any llvm-nm'))
+
+# It is not realistically possible to account for all options that could
+# possibly be present in system and user configuration files, so disable
+# default configs for the test runs.
+config.environment["CLANG_NO_DEFAULT_CONFIG"] = "1"


        


More information about the cfe-commits mailing list