smeenai created this revision.
smeenai added reviewers: compnerd, phosek, rnk.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
smeenai added a parent revision: D65838: [Driver] Use enumeration for quoting 
mode. NFC.

When writing driver tests, it's useful to have a way to output arguments
verbatim (i.e. without any quoting and escaping). For example, on
Windows today, the installation directory is output without backslashes
being escaped, but any -internal-isystem arguments passed by the driver
to the frontend will have backslashes escaped in the -### output, making
it impossible to write a FileCheck match against the installation
directory. Add a new argument to avoid any quoting and escaping to ease
driver testing.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65839

Files:
  clang/include/clang/Driver/Job.h
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/Job.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/CommonArgs.cpp
  clang/lib/Frontend/CreateInvocationFromCommandLine.cpp

Index: clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
===================================================================
--- clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
+++ clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
@@ -61,6 +61,13 @@
     return nullptr;
   }
 
+  // Just print the cc1 options verbatim if -###-verbatim was present.
+  if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH_VERBATIM)) {
+    C->getJobs().Print(llvm::errs(), "\n",
+                       driver::Command::PrintingMode::Verbatim);
+    return nullptr;
+  }
+
   // We expect to get back exactly one command job, if we didn't something
   // failed. Offload compilation is an exception as it creates multiple jobs. If
   // that's the case, we proceed with the first job. If caller needs a
Index: clang/lib/Driver/ToolChains/CommonArgs.cpp
===================================================================
--- clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1345,7 +1345,8 @@
     llvm::errs() << LksBuffer;
 
   // If this is a dry run, do not create the linker script file.
-  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
+  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) ||
+      C.getArgs().hasArg(options::OPT__HASH_HASH_HASH_VERBATIM))
     return;
 
   // Open script file and write the contents.
@@ -1453,7 +1454,8 @@
     llvm::errs() << LksBuffer;
 
   // If this is a dry run, do not create the linker script file.
-  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
+  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) ||
+      C.getArgs().hasArg(options::OPT__HASH_HASH_HASH_VERBATIM))
     return;
 
   // Open script file and write the contents.
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -1994,7 +1994,8 @@
                                     StringRef Target, const InputInfo &Output,
                                     const InputInfo &Input, const ArgList &Args) const {
   // If this is a dry run, do not create the compilation database file.
-  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
+  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) ||
+      C.getArgs().hasArg(options::OPT__HASH_HASH_HASH_VERBATIM))
     return;
 
   using llvm::yaml::escape;
Index: clang/lib/Driver/Job.cpp
===================================================================
--- clang/lib/Driver/Job.cpp
+++ clang/lib/Driver/Job.cpp
@@ -101,7 +101,8 @@
 void Command::printArg(raw_ostream &OS, StringRef Arg, PrintingMode Mode) {
   const bool Escape = Arg.find_first_of(" \"\\$") != StringRef::npos;
 
-  if (Mode == PrintingMode::QuoteIfNeeded && !Escape) {
+  if (Mode == PrintingMode::Verbatim ||
+      (Mode == PrintingMode::QuoteIfNeeded && !Escape)) {
     OS << Arg;
     return;
   }
@@ -213,9 +214,11 @@
 
 void Command::Print(raw_ostream &OS, const char *Terminator, PrintingMode Mode,
                     CrashReportInfo *CrashInfo) const {
-  // Always quote the exe.
+  // Always quote the exe unless printing verbatim.
   OS << ' ';
-  printArg(OS, Executable, PrintingMode::AlwaysQuote);
+  printArg(OS, Executable,
+           Mode == PrintingMode::Verbatim ? PrintingMode::Verbatim
+                                          : PrintingMode::AlwaysQuote);
 
   ArrayRef<const char *> Args = Arguments;
   SmallVector<const char *, 128> ArgsRespFile;
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -1451,6 +1451,12 @@
     return 0;
   }
 
+  // Just print verbatim if -###-verbatim was present.
+  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH_VERBATIM)) {
+    C.getJobs().Print(llvm::errs(), "\n", Command::PrintingMode::Verbatim);
+    return 0;
+  }
+
   // If there were errors building the compilation, quit now.
   if (Diags.hasErrorOccurred())
     return 1;
@@ -1677,6 +1683,7 @@
 
   if (C.getArgs().hasArg(options::OPT_v) ||
       C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) ||
+      C.getArgs().hasArg(options::OPT__HASH_HASH_HASH_VERBATIM) ||
       C.getArgs().hasArg(options::OPT_print_supported_cpus)) {
     PrintVersion(C, llvm::errs());
     SuppressMissingInputWarning = true;
@@ -3565,8 +3572,9 @@
       C.getArgs().hasArg(options::OPT_Qunused_arguments))
     return;
 
-  // Claim -### here.
+  // Claim -### and -###-verbatim here.
   (void)C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
+  (void)C.getArgs().hasArg(options::OPT__HASH_HASH_HASH_VERBATIM);
 
   // Claim --driver-mode, --rsp-quoting, it was handled earlier.
   (void)C.getArgs().hasArg(options::OPT_driver_mode);
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -330,6 +330,10 @@
 
 def _HASH_HASH_HASH : Flag<["-"], "###">, Flags<[DriverOption, CoreOption]>,
     HelpText<"Print (but do not run) the commands to run for this compilation">;
+def _HASH_HASH_HASH_VERBATIM : Flag<["-"], "###-verbatim">,
+    Flags<[DriverOption, CoreOption]>,
+    HelpText<"Print (but do not run) the commands to run for this compilation, "
+             "unquoted and unescaped">;
 def _DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>,
     Flags<[DriverOption, CoreOption]>;
 def A : JoinedOrSeparate<["-"], "A">, Flags<[RenderJoined]>, Group<gfortran_Group>;
Index: clang/include/clang/Driver/Job.h
===================================================================
--- clang/include/clang/Driver/Job.h
+++ clang/include/clang/Driver/Job.h
@@ -89,6 +89,7 @@
   enum class PrintingMode {
     AlwaysQuote,
     QuoteIfNeeded,
+    Verbatim,
   };
 
   Command(const Action &Source, const Tool &Creator, const char *Executable,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to