[PATCH] D11896: [clang-cl] Add support for CL and _CL_ environment variables

David Majnemer via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 10 01:15:46 PDT 2015


majnemer created this revision.
majnemer added reviewers: hansw, rnk.
majnemer added a subscriber: cfe-commits.

cl uses 'CL' and '_CL_' to prepend and append command line options to
the given argument vector.  There is an additional quirk whereby '#' is
transformed into '='.

http://reviews.llvm.org/D11896

Files:
  test/Driver/cl-options.c
  tools/driver/driver.cpp

Index: tools/driver/driver.cpp
===================================================================
--- tools/driver/driver.cpp
+++ tools/driver/driver.cpp
@@ -445,6 +445,38 @@
     }
   }
 
+  // Handle CL and _CL_ which permits additional command line options to be
+  // prepended or appended.
+  if (Tokenizer == &llvm::cl::TokenizeWindowsCommandLine) {
+    // The first instance of '#' should be replaced with '='.
+    auto AdjustOpt = [] (const char *Opt) {
+      if (char *NumberSignPtr = const_cast<char *>(::strchr(Opt, '#')))
+        *NumberSignPtr = '=';
+      return Opt;
+    };
+
+    // Arguments in "CL" are prepended.
+    llvm::Optional<std::string> OptCL = llvm::sys::Process::GetEnv("CL");
+    if (OptCL.hasValue()) {
+      SmallVector<const char *, 8> PrependedOpts;
+      llvm::cl::TokenizeWindowsCommandLine(OptCL.getValue(), Saver,
+                                           PrependedOpts);
+      // Insert right after the program name to prepend to the argument list.
+      for (const char *PrependedOpt : PrependedOpts)
+        argv.insert(argv.begin() + 1, AdjustOpt(PrependedOpt));
+    }
+    // Arguments in "CL" are appended.
+    llvm::Optional<std::string> Opt_CL_ = llvm::sys::Process::GetEnv("_CL_");
+    if (Opt_CL_.hasValue()) {
+      SmallVector<const char *, 8> AppendedOpts;
+      llvm::cl::TokenizeWindowsCommandLine(Opt_CL_.getValue(), Saver,
+                                           AppendedOpts);
+      // Insert at the end of the argument list to append.
+      for (const char *AppendedOpt : AppendedOpts)
+        argv.push_back(AdjustOpt(AppendedOpt));
+    }
+  }
+
   std::set<std::string> SavedStrings;
   // Handle CCC_OVERRIDE_OPTIONS, used for editing a command line behind the
   // scenes.
Index: test/Driver/cl-options.c
===================================================================
--- test/Driver/cl-options.c
+++ test/Driver/cl-options.c
@@ -379,6 +379,12 @@
 // RUN: %clang_cl -fmsc-version=1900 -TP -### -- %s 2>&1 | FileCheck -check-prefix=CXX14 %s
 // CXX14: -std=c++14
 
+// RUN: env CL="/Gy" %clang_cl -### -- %s 2>&1 | FileCheck -check-prefix=ENV-CL %s
+// ENV-CL: "-ffunction-sections"
+
+// RUN: env CL="/Gy" _CL_="/Gy- -- %s" %clang_cl -### 2>&1 | FileCheck -check-prefix=ENV-_CL_ %s
+// ENV-_CL_-NOT: "-ffunction-sections"
+
 // Accept "core" clang options.
 // (/Zs is for syntax-only, -Werror makes it fail hard on unknown options)
 // RUN: %clang_cl \


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11896.31640.patch
Type: text/x-patch
Size: 2450 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150810/8a96f6ce/attachment-0001.bin>


More information about the cfe-commits mailing list