smeenai created this revision.
smeenai added reviewers: compnerd, phosek, rnk.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Boolean parameters are generally hard to understand, especially when we
don't consistently have a comment for them. Change to an enumeration.

While I believe this change is worthwhile by itself, its main purpose is
to serve as cleanup for a follow-up which will add a third mode to this
enumeration, to allow verbatim printing of arguments (without any
quoting or escaping).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65838

Files:
  clang/include/clang/Driver/Job.h
  clang/lib/Driver/Compilation.cpp
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/Job.cpp
  clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
  clang/lib/Tooling/Tooling.cpp

Index: clang/lib/Tooling/Tooling.cpp
===================================================================
--- clang/lib/Tooling/Tooling.cpp
+++ clang/lib/Tooling/Tooling.cpp
@@ -94,7 +94,7 @@
   if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
     SmallString<256> error_msg;
     llvm::raw_svector_ostream error_stream(error_msg);
-    Jobs.Print(error_stream, "; ", true);
+    Jobs.Print(error_stream, "; ", driver::Command::PrintingMode::AlwaysQuote);
     Diagnostics->Report(diag::err_fe_expected_compiler_job)
         << error_stream.str();
     return nullptr;
@@ -337,7 +337,8 @@
   // Show the invocation, with -v.
   if (Invocation->getHeaderSearchOpts().Verbose) {
     llvm::errs() << "clang Invocation:\n";
-    Compilation->getJobs().Print(llvm::errs(), "\n", true);
+    Compilation->getJobs().Print(llvm::errs(), "\n",
+                                 driver::Command::PrintingMode::AlwaysQuote);
     llvm::errs() << "\n";
   }
 
Index: clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
===================================================================
--- clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
+++ clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
@@ -56,7 +56,8 @@
 
   // Just print the cc1 options if -### was present.
   if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
-    C->getJobs().Print(llvm::errs(), "\n", true);
+    C->getJobs().Print(llvm::errs(), "\n",
+                       driver::Command::PrintingMode::AlwaysQuote);
     return nullptr;
   }
 
@@ -82,7 +83,7 @@
       (Jobs.size() > 1 && !OffloadCompilation)) {
     SmallString<256> Msg;
     llvm::raw_svector_ostream OS(Msg);
-    Jobs.Print(OS, "; ", true);
+    Jobs.Print(OS, "; ", driver::Command::PrintingMode::AlwaysQuote);
     Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
     return nullptr;
   }
Index: clang/lib/Driver/Job.cpp
===================================================================
--- clang/lib/Driver/Job.cpp
+++ clang/lib/Driver/Job.cpp
@@ -98,10 +98,10 @@
   return false;
 }
 
-void Command::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
+void Command::printArg(raw_ostream &OS, StringRef Arg, PrintingMode Mode) {
   const bool Escape = Arg.find_first_of(" \"\\$") != StringRef::npos;
 
-  if (!Quote && !Escape) {
+  if (Mode == PrintingMode::QuoteIfNeeded && !Escape) {
     OS << Arg;
     return;
   }
@@ -211,11 +211,11 @@
   IncFlags.push_back(std::move(NewInc));
 }
 
-void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
+void Command::Print(raw_ostream &OS, const char *Terminator, PrintingMode Mode,
                     CrashReportInfo *CrashInfo) const {
   // Always quote the exe.
   OS << ' ';
-  printArg(OS, Executable, /*Quote=*/true);
+  printArg(OS, Executable, PrintingMode::AlwaysQuote);
 
   ArrayRef<const char *> Args = Arguments;
   SmallVector<const char *, 128> ArgsRespFile;
@@ -243,7 +243,7 @@
         if (!NewIncFlags.empty()) {
           for (auto &F : NewIncFlags) {
             OS << ' ';
-            printArg(OS, F.c_str(), Quote);
+            printArg(OS, F.c_str(), Mode);
           }
           i += NumArgs - 1;
           continue;
@@ -257,20 +257,20 @@
         // Replace the input file name with the crashinfo's file name.
         OS << ' ';
         StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
-        printArg(OS, ShortName.str(), Quote);
+        printArg(OS, ShortName.str(), Mode);
         continue;
       }
     }
 
     OS << ' ';
-    printArg(OS, Arg, Quote);
+    printArg(OS, Arg, Mode);
   }
 
   if (CrashInfo && HaveCrashVFS) {
     OS << ' ';
-    printArg(OS, "-ivfsoverlay", Quote);
+    printArg(OS, "-ivfsoverlay", Mode);
     OS << ' ';
-    printArg(OS, CrashInfo->VFSPath.str(), Quote);
+    printArg(OS, CrashInfo->VFSPath.str(), Mode);
 
     // The leftover modules from the crash are stored in
     //  <name>.cache/vfs/modules
@@ -285,7 +285,7 @@
     ModCachePath.append(RelModCacheDir.c_str());
 
     OS << ' ';
-    printArg(OS, ModCachePath, Quote);
+    printArg(OS, ModCachePath, Mode);
   }
 
   if (ResponseFile != nullptr) {
@@ -379,10 +379,11 @@
       Fallback(std::move(Fallback_)) {}
 
 void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
-                            bool Quote, CrashReportInfo *CrashInfo) const {
-  Command::Print(OS, "", Quote, CrashInfo);
+                            PrintingMode Mode,
+                            CrashReportInfo *CrashInfo) const {
+  Command::Print(OS, "", Mode, CrashInfo);
   OS << " ||";
-  Fallback->Print(OS, Terminator, Quote, CrashInfo);
+  Fallback->Print(OS, Terminator, Mode, CrashInfo);
 }
 
 static bool ShouldFallback(int ExitCode) {
@@ -417,8 +418,9 @@
     : Command(Source_, Creator_, Executable_, Arguments_, Inputs) {}
 
 void ForceSuccessCommand::Print(raw_ostream &OS, const char *Terminator,
-                            bool Quote, CrashReportInfo *CrashInfo) const {
-  Command::Print(OS, "", Quote, CrashInfo);
+                                PrintingMode Mode,
+                                CrashReportInfo *CrashInfo) const {
+  Command::Print(OS, "", Mode, CrashInfo);
   OS << " || (exit 0)" << Terminator;
 }
 
@@ -432,10 +434,11 @@
   return 0;
 }
 
-void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
+void JobList::Print(raw_ostream &OS, const char *Terminator,
+                    Command::PrintingMode Mode,
                     CrashReportInfo *CrashInfo) const {
   for (const auto &Job : *this)
-    Job.Print(OS, Terminator, Quote, CrashInfo);
+    Job.Print(OS, Terminator, Mode, CrashInfo);
 }
 
 void JobList::clear() { Jobs.clear(); }
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -1141,7 +1141,7 @@
   for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) {
     if (I != ASL.begin())
       OS << ' ';
-    Command::printArg(OS, *I, true);
+    Command::printArg(OS, *I, Command::PrintingMode::AlwaysQuote);
   }
   OS << '\n';
 }
@@ -1392,8 +1392,8 @@
              << "# Driver args: ";
     printArgList(ScriptOS, C.getInputArgs());
     ScriptOS << "# Original command: ";
-    Cmd.Print(ScriptOS, "\n", /*Quote=*/true);
-    Cmd.Print(ScriptOS, "\n", /*Quote=*/true, &CrashInfo);
+    Cmd.Print(ScriptOS, "\n", Command::PrintingMode::AlwaysQuote);
+    Cmd.Print(ScriptOS, "\n", Command::PrintingMode::AlwaysQuote, &CrashInfo);
     if (!AdditionalInformation.empty())
       ScriptOS << "\n# Additional information: " << AdditionalInformation
                << "\n";
@@ -1447,7 +1447,7 @@
     SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) {
   // Just print if -### was present.
   if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
-    C.getJobs().Print(llvm::errs(), "\n", true);
+    C.getJobs().Print(llvm::errs(), "\n", Command::PrintingMode::AlwaysQuote);
     return 0;
   }
 
Index: clang/lib/Driver/Compilation.cpp
===================================================================
--- clang/lib/Driver/Compilation.cpp
+++ clang/lib/Driver/Compilation.cpp
@@ -174,7 +174,9 @@
     if (getDriver().CCPrintOptions)
       *OS << "[Logging clang options]";
 
-    C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
+    C.Print(*OS, "\n",
+            getDriver().CCPrintOptions ? Command::PrintingMode::AlwaysQuote
+                                       : Command::PrintingMode::QuoteIfNeeded);
   }
 
   std::string Error;
Index: clang/include/clang/Driver/Job.h
===================================================================
--- clang/include/clang/Driver/Job.h
+++ clang/include/clang/Driver/Job.h
@@ -86,6 +86,11 @@
   void writeResponseFile(raw_ostream &OS) const;
 
 public:
+  enum class PrintingMode {
+    AlwaysQuote,
+    QuoteIfNeeded,
+  };
+
   Command(const Action &Source, const Tool &Creator, const char *Executable,
           const llvm::opt::ArgStringList &Arguments,
           ArrayRef<InputInfo> Inputs);
@@ -94,7 +99,8 @@
   Command(const Command &) = default;
   virtual ~Command() = default;
 
-  virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
+  virtual void Print(llvm::raw_ostream &OS, const char *Terminator,
+                     PrintingMode Mode,
                      CrashReportInfo *CrashInfo = nullptr) const;
 
   virtual int Execute(ArrayRef<Optional<StringRef>> Redirects,
@@ -126,7 +132,7 @@
   const llvm::opt::ArgStringList &getArguments() const { return Arguments; }
 
   /// Print a command argument, and optionally quote it.
-  static void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote);
+  static void printArg(llvm::raw_ostream &OS, StringRef Arg, PrintingMode Mode);
 
   /// Set whether to print the input filenames when executing.
   void setPrintInputFilenames(bool P) { PrintInputFilenames = P; }
@@ -142,7 +148,7 @@
                   ArrayRef<InputInfo> Inputs,
                   std::unique_ptr<Command> Fallback_);
 
-  void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
+  void Print(llvm::raw_ostream &OS, const char *Terminator, PrintingMode Mode,
              CrashReportInfo *CrashInfo = nullptr) const override;
 
   int Execute(ArrayRef<Optional<StringRef>> Redirects, std::string *ErrMsg,
@@ -160,7 +166,7 @@
                       const llvm::opt::ArgStringList &Arguments_,
                       ArrayRef<InputInfo> Inputs);
 
-  void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
+  void Print(llvm::raw_ostream &OS, const char *Terminator, PrintingMode Mode,
              CrashReportInfo *CrashInfo = nullptr) const override;
 
   int Execute(ArrayRef<Optional<StringRef>> Redirects, std::string *ErrMsg,
@@ -180,7 +186,8 @@
 
 public:
   void Print(llvm::raw_ostream &OS, const char *Terminator,
-             bool Quote, CrashReportInfo *CrashInfo = nullptr) const;
+             Command::PrintingMode Mode,
+             CrashReportInfo *CrashInfo = nullptr) const;
 
   /// Add a job to the list (taking ownership).
   void addJob(std::unique_ptr<Command> J) { Jobs.push_back(std::move(J)); }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to