Skip to content

Commit

Permalink
[clang] Abstract away string allocation in command line generation
Browse files Browse the repository at this point in the history
This patch abstracts away the string allocation and vector push-back from command line generation. Instead, **all** generated arguments are passed into `ArgumentConsumer`, which may choose to do the string allocation and vector push-back, or something else entirely.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D157046
  • Loading branch information
jansvoboda11 committed Aug 4, 2023
1 parent be7a546 commit 8345265
Show file tree
Hide file tree
Showing 2 changed files with 327 additions and 340 deletions.
29 changes: 21 additions & 8 deletions clang/include/clang/Frontend/CompilerInvocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class CompilerInvocation : public CompilerInvocationRefBase,
/// identifying the conditions under which the module was built.
std::string getModuleHash() const;

using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
using StringAllocator = llvm::function_ref<const char *(const Twine &)>;
/// Generate cc1-compatible command line arguments from this instance.
///
/// \param [out] Args - The generated arguments. Note that the caller is
Expand All @@ -233,7 +233,21 @@ class CompilerInvocation : public CompilerInvocationRefBase,
/// 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,
StringAllocator SA) const;
StringAllocator SA) const {
generateCC1CommandLine([&](const Twine &Arg) {
// No need to allocate static string literals.
Args.push_back(Arg.isSingleStringLiteral()
? Arg.getSingleStringRef().data()
: SA(Arg));
});
}

using ArgumentConsumer = llvm::function_ref<void(const Twine &)>;
/// Generate cc1-compatible command line arguments from this instance.
///
/// \param Consumer - Callback that gets invoked for every single generated
/// command line argument.
void generateCC1CommandLine(ArgumentConsumer Consumer) const;

/// Generate cc1-compatible command line arguments from this instance,
/// wrapping the result as a std::vector<std::string>.
Expand Down Expand Up @@ -267,8 +281,8 @@ class CompilerInvocation : public CompilerInvocationRefBase,

/// Generate command line options from DiagnosticOptions.
static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
SmallVectorImpl<const char *> &Args,
StringAllocator SA, bool DefaultDiagColor);
ArgumentConsumer Consumer,
bool DefaultDiagColor);

/// Parse command line options that map to LangOptions.
static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
Expand All @@ -278,8 +292,7 @@ class CompilerInvocation : public CompilerInvocationRefBase,

/// Generate command line options from LangOptions.
static void GenerateLangArgs(const LangOptions &Opts,
SmallVectorImpl<const char *> &Args,
StringAllocator SA, const llvm::Triple &T,
ArgumentConsumer Consumer, const llvm::Triple &T,
InputKind IK);

/// Parse command line options that map to CodeGenOptions.
Expand All @@ -291,8 +304,8 @@ class CompilerInvocation : public CompilerInvocationRefBase,

// Generate command line options from CodeGenOptions.
static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
SmallVectorImpl<const char *> &Args,
StringAllocator SA, const llvm::Triple &T,
ArgumentConsumer Consumer,
const llvm::Triple &T,
const std::string &OutputFile,
const LangOptions *LangOpts);
};
Expand Down

0 comments on commit 8345265

Please sign in to comment.