[clang] b3f94fe - [clang][Driver] allow tilde in user config dir

Michał Górny via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 16 04:23:35 PST 2022


Author: Michał Górny
Date: 2022-11-16T13:23:25+01:00
New Revision: b3f94fe1c3c4f4fd7a937288b5543c4067b3e2f2

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

LOG: [clang][Driver] allow tilde in user config dir

This patch allows users to configure clang with option
e.g. `-DCLANG_CONFIG_FILE_USER_DIR=~/.config/clang` or invoke clang
with `--config-user-dir=~/.config/clang`.

Patch merged on behalf of @paperchalice (LJC)

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

Added: 
    

Modified: 
    clang/lib/Driver/Driver.cpp
    clang/test/Driver/config-file3.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index c63160e55ea3..b6f6122ff40a 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -220,7 +220,11 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
   SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR;
 #endif
 #if defined(CLANG_CONFIG_FILE_USER_DIR)
-  UserConfigDir = CLANG_CONFIG_FILE_USER_DIR;
+  {
+    SmallString<128> P;
+    llvm::sys::fs::expand_tilde(CLANG_CONFIG_FILE_USER_DIR, P);
+    UserConfigDir = static_cast<std::string>(P);
+  }
 #endif
 
   // Compute the path to the resource directory.
@@ -1008,8 +1012,8 @@ bool Driver::loadConfigFiles() {
     }
     if (CLOptions->hasArg(options::OPT_config_user_dir_EQ)) {
       SmallString<128> CfgDir;
-      CfgDir.append(
-          CLOptions->getLastArgValue(options::OPT_config_user_dir_EQ));
+      llvm::sys::fs::expand_tilde(
+          CLOptions->getLastArgValue(options::OPT_config_user_dir_EQ), CfgDir);
       if (CfgDir.empty() || getVFS().makeAbsolute(CfgDir))
         UserConfigDir.clear();
       else

diff  --git a/clang/test/Driver/config-file3.c b/clang/test/Driver/config-file3.c
index 5f712020e79f..7bd86a2ae9dc 100644
--- a/clang/test/Driver/config-file3.c
+++ b/clang/test/Driver/config-file3.c
@@ -221,3 +221,8 @@
 //
 // RUN: rm %t/testdmode/clang-g++.cfg
 // RUN: %t/testdmode/x86_64-unknown-linux-gnu-clang-g++ --config-system-dir= --config-user-dir= -no-canonical-prefixes --version 2>&1 | FileCheck %s -check-prefix NO-CONFIG
+
+//--- Tilde expansion in user configuration file directory
+//
+// RUN: HOME=%S/Inputs/config %clang --config-user-dir=~ -v 2>&1 | FileCheck %s -check-prefix CHECK-TILDE
+// CHECK-TILDE: User configuration file directory: {{.*}}/Inputs/config


        


More information about the cfe-commits mailing list