Skip to content

Commit 8345265

Browse files
committed
[clang] Abstract away string allocation in command line generation
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
1 parent be7a546 commit 8345265

File tree

2 files changed

+327
-340
lines changed

2 files changed

+327
-340
lines changed

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class CompilerInvocation : public CompilerInvocationRefBase,
223223
/// identifying the conditions under which the module was built.
224224
std::string getModuleHash() const;
225225

226-
using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
226+
using StringAllocator = llvm::function_ref<const char *(const Twine &)>;
227227
/// Generate cc1-compatible command line arguments from this instance.
228228
///
229229
/// \param [out] Args - The generated arguments. Note that the caller is
@@ -233,7 +233,21 @@ class CompilerInvocation : public CompilerInvocationRefBase,
233233
/// command line argument and return a pointer to the newly allocated string.
234234
/// The returned pointer is what gets appended to Args.
235235
void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
236-
StringAllocator SA) const;
236+
StringAllocator SA) const {
237+
generateCC1CommandLine([&](const Twine &Arg) {
238+
// No need to allocate static string literals.
239+
Args.push_back(Arg.isSingleStringLiteral()
240+
? Arg.getSingleStringRef().data()
241+
: SA(Arg));
242+
});
243+
}
244+
245+
using ArgumentConsumer = llvm::function_ref<void(const Twine &)>;
246+
/// Generate cc1-compatible command line arguments from this instance.
247+
///
248+
/// \param Consumer - Callback that gets invoked for every single generated
249+
/// command line argument.
250+
void generateCC1CommandLine(ArgumentConsumer Consumer) const;
237251

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

268282
/// Generate command line options from DiagnosticOptions.
269283
static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
270-
SmallVectorImpl<const char *> &Args,
271-
StringAllocator SA, bool DefaultDiagColor);
284+
ArgumentConsumer Consumer,
285+
bool DefaultDiagColor);
272286

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

279293
/// Generate command line options from LangOptions.
280294
static void GenerateLangArgs(const LangOptions &Opts,
281-
SmallVectorImpl<const char *> &Args,
282-
StringAllocator SA, const llvm::Triple &T,
295+
ArgumentConsumer Consumer, const llvm::Triple &T,
283296
InputKind IK);
284297

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

292305
// Generate command line options from CodeGenOptions.
293306
static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
294-
SmallVectorImpl<const char *> &Args,
295-
StringAllocator SA, const llvm::Triple &T,
307+
ArgumentConsumer Consumer,
308+
const llvm::Triple &T,
296309
const std::string &OutputFile,
297310
const LangOptions *LangOpts);
298311
};

0 commit comments

Comments
 (0)