[PATCH] D79796: [DO NOT REVIEW] Sketch support for generating CC1 command line from CompilerInvocation

Daniel Grumberg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 13 11:25:20 PDT 2020


dang updated this revision to Diff 263795.
dang added a comment.

Rebase on top of llvm/master


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79796/new/

https://reviews.llvm.org/D79796

Files:
  clang/include/clang/Frontend/CompilerInvocation.h
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/unittests/Frontend/CompilerInvocationTest.cpp


Index: clang/unittests/Frontend/CompilerInvocationTest.cpp
===================================================================
--- clang/unittests/Frontend/CompilerInvocationTest.cpp
+++ clang/unittests/Frontend/CompilerInvocationTest.cpp
@@ -24,7 +24,15 @@
   CompilerInvocation CInvok;
   CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags);
 
-  ASSERT_EQ(CInvok.getCC1CommandLine(), " -fmodules-strict-context-hash");
+  SmallVector<const char *, 32> GeneratedArgs;
+  SmallVector<std::string, 32> GeneratedArgsStorage;
+  auto StringAlloc = [&GeneratedArgsStorage](const Twine &Arg) {
+    return GeneratedArgsStorage.emplace_back(Arg.str()).c_str();
+  };
+
+  CInvok.generateCC1CommandLine(GeneratedArgs, StringAlloc);
+
+  ASSERT_STREQ(GeneratedArgs[0], "-fmodules-strict-context-hash");
 }
 
 } // anonymous namespace
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -51,6 +51,7 @@
 #include "llvm/ADT/Hashing.h"
 #include "llvm/ADT/None.h"
 #include "llvm/ADT/Optional.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
@@ -3831,9 +3832,9 @@
   return llvm::APInt(64, code).toString(36, /*Signed=*/false);
 }
 
-std::string CompilerInvocation::getCC1CommandLine() const {
-  std::string CMDLine;
-  llvm::raw_string_ostream CMDStream(CMDLine);
+void CompilerInvocation::generateCC1CommandLine(
+    SmallVectorImpl<const char *> &Args,
+    llvm::function_ref<const char *(const Twine &)> StringAllocator) const {
 #define PREFIX(PREFIX_TYPE, BRACED_INIT)                                       \
   const char *PREFIX_TYPE[4] = BRACED_INIT;
 #define OPTION_WITH_MARSHALLING(PREFIX_TYPE, NAME, ID, KIND, GROUP, ALIAS,     \
@@ -3841,11 +3842,10 @@
                                 VALUES, KEYPATH, IS_POSITIVE, DEFAULT_VALUE)   \
   if (Option::KIND##Class == Option::FlagClass &&                              \
       IS_POSITIVE != DEFAULT_VALUE && this->KEYPATH != DEFAULT_VALUE)          \
-    CMDStream << " " << PREFIX_TYPE[0] << NAME;
+    Args.push_back(StringAllocator(Twine(PREFIX_TYPE[0]) + NAME));
 #include "clang/Driver/Options.inc"
 #undef OPTION_WITH_MARSHALLING
 #undef PREFIX
-  return CMDStream.str();
 }
 
 namespace clang {
Index: clang/include/clang/Frontend/CompilerInvocation.h
===================================================================
--- clang/include/clang/Frontend/CompilerInvocation.h
+++ clang/include/clang/Frontend/CompilerInvocation.h
@@ -185,7 +185,19 @@
   /// identifying the conditions under which the module was built.
   std::string getModuleHash() const;
 
-  std::string getCC1CommandLine() const;
+  /// Generate a cc1-compatible command line arguments from this instance.
+  ///
+  /// \param [out] Args - The generated arguments. Note that the caller is
+  /// responsible for insersting the path to the clang executable and "-cc1" if
+  /// desired.
+  /// \param StringAllocator - A function that given a Twine can allocate
+  /// storage for a given command line argument and return a pointer to the
+  /// newly allocated string. The returned pointer is what gets appended to
+  /// Args.
+  void
+  generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
+                         llvm::function_ref<const char *(const llvm::Twine &)>
+                             StringAllocator) const;
 
   /// @}
   /// @name Option Subgroups


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79796.263795.patch
Type: text/x-patch
Size: 3566 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200513/da594f6f/attachment.bin>


More information about the llvm-commits mailing list