[MC] Pass down argv0 & cc1 cmd-line to the back-end and store in MCTargetOptions

When targetting CodeView, the goal is to store argv0 & cc1 cmd-line in the emitted .OBJ, in order to allow a reproducer from the .OBJ alone.

This patch is to simplify https://reviews.llvm.org/D80833
This commit is contained in:
Alexandre Ganea 2020-06-18 08:57:50 -04:00
parent 24eff42ba4
commit 89ea0b0520
9 changed files with 30 additions and 10 deletions

View File

@ -332,6 +332,11 @@ public:
/// coverage pass should actually not be instrumented.
std::vector<std::string> SanitizeCoverageBlacklistFiles;
/// Executable and command-line used to create a given CompilerInvocation.
/// Most of the time this will be the full -cc1 command.
const char *Argv0 = nullptr;
ArrayRef<const char *> CommandLineArgs;
public:
// Define accessors/mutators for code generation options of enumeration type.
#define CODEGENOPT(Name, Bits, Default)

View File

@ -155,7 +155,8 @@ public:
/// \param [out] Res - The resulting invocation.
static bool CreateFromArgs(CompilerInvocation &Res,
ArrayRef<const char *> CommandLineArgs,
DiagnosticsEngine &Diags);
DiagnosticsEngine &Diags,
const char *Argv0 = nullptr);
/// Get the directory where the compiler headers
/// reside, relative to the compiler binary (found by the passed in

View File

@ -537,6 +537,8 @@ static void initTargetOptions(DiagnosticsEngine &Diags,
Entry.Group == frontend::IncludeDirGroup::System))
Options.MCOptions.IASSearchPaths.push_back(
Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
}
static Optional<GCOVOptions> getGCOVOptions(const CodeGenOptions &CodeGenOpts) {
if (CodeGenOpts.DisableGCov)

View File

@ -3627,7 +3627,8 @@ static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args,
bool CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
ArrayRef<const char *> CommandLineArgs,
DiagnosticsEngine &Diags) {
DiagnosticsEngine &Diags,
const char *Argv0) {
bool Success = true;
// Parse the arguments.
@ -3747,6 +3748,11 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
Res.getCodeGenOpts().FineGrainedBitfieldAccesses = false;
Diags.Report(diag::warn_drv_fine_grained_bitfield_accesses_ignored);
}
// Store the command-line for using in the CodeView backend.
Res.getCodeGenOpts().Argv0 = Argv0;
Res.getCodeGenOpts().CommandLineArgs = CommandLineArgs;
return Success;
}

View File

@ -93,7 +93,7 @@ std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
if (CC1Args)
*CC1Args = {CCArgs.begin(), CCArgs.end()};
auto CI = std::make_unique<CompilerInvocation>();
if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags) &&
if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
!ShouldRecoverOnErorrs)
return nullptr;
return CI;

View File

@ -141,11 +141,13 @@ namespace clang {
namespace tooling {
/// Returns a clang build invocation initialized from the CC1 flags.
CompilerInvocation *newInvocation(
DiagnosticsEngine *Diagnostics, const llvm::opt::ArgStringList &CC1Args) {
CompilerInvocation *newInvocation(DiagnosticsEngine *Diagnostics,
const llvm::opt::ArgStringList &CC1Args,
const char *const BinaryName) {
assert(!CC1Args.empty() && "Must at least contain the program name!");
CompilerInvocation *Invocation = new CompilerInvocation;
CompilerInvocation::CreateFromArgs(*Invocation, CC1Args, *Diagnostics);
CompilerInvocation::CreateFromArgs(*Invocation, CC1Args, *Diagnostics,
BinaryName);
Invocation->getFrontendOpts().DisableFree = false;
Invocation->getCodeGenOpts().DisableFree = false;
return Invocation;
@ -345,7 +347,7 @@ bool ToolInvocation::run() {
if (!CC1Args)
return false;
std::unique_ptr<CompilerInvocation> Invocation(
newInvocation(&Diagnostics, *CC1Args));
newInvocation(&Diagnostics, *CC1Args, BinaryName));
// FIXME: remove this when all users have migrated!
for (const auto &It : MappedFileContents) {
// Inject the code as the given file name into the preprocessor options.

View File

@ -203,8 +203,8 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
bool Success =
CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv, Diags);
bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
Argv, Diags, Argv0);
if (Clang->getFrontendOpts().TimeTrace) {
llvm::timeTraceProfilerInitialize(

View File

@ -327,7 +327,7 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV) {
StringRef Tool = ArgV[1];
void *GetExecutablePathVP = (void *)(intptr_t)GetExecutablePath;
if (Tool == "-cc1")
return cc1_main(makeArrayRef(ArgV).slice(2), ArgV[0], GetExecutablePathVP);
return cc1_main(makeArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
if (Tool == "-cc1as")
return cc1as_main(makeArrayRef(ArgV).slice(2), ArgV[0],
GetExecutablePathVP);

View File

@ -9,6 +9,7 @@
#ifndef LLVM_MC_MCTARGETOPTIONS_H
#define LLVM_MC_MCTARGETOPTIONS_H
#include "llvm/ADT/ArrayRef.h"
#include <string>
#include <vector>
@ -60,6 +61,9 @@ public:
std::string AssemblyLanguage;
std::string SplitDwarfFile;
const char *Argv0 = nullptr;
ArrayRef<const char *> CommandLineArgs;
/// Additional paths to search for `.include` directives when using the
/// integrated assembler.
std::vector<std::string> IASSearchPaths;