Skip to content

Commit 6033291

Browse files
committed
Flatten compiler args in Frontend, keep existing behaviour as fallback for other frontends
1 parent d4c519e commit 6033291

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "llvm/Support/CommandLine.h"
5151
#include "llvm/Support/MemoryBuffer.h"
5252
#include "llvm/Support/PrettyStackTrace.h"
53+
#include "llvm/Support/Program.h"
5354
#include "llvm/Support/TimeProfiler.h"
5455
#include "llvm/Support/Timer.h"
5556
#include "llvm/Support/ToolOutputFile.h"
@@ -322,6 +323,41 @@ static bool actionRequiresCodeGen(BackendAction Action) {
322323
Action != Backend_EmitLL;
323324
}
324325

326+
static std::string flattenClangCommandLine(ArrayRef<std::string> Args,
327+
StringRef MainFilename) {
328+
std::string FlatCmdLine;
329+
raw_string_ostream OS(FlatCmdLine);
330+
bool PrintedOneArg = false;
331+
if (!StringRef(Args[0]).contains("-cc1")) {
332+
llvm::sys::printArg(OS, "-cc1", /*Quote=*/true);
333+
PrintedOneArg = true;
334+
}
335+
for (unsigned i = 0; i < Args.size(); i++) {
336+
StringRef Arg = Args[i];
337+
if (Arg.empty()) {
338+
continue;
339+
}
340+
if (Arg == "-main-file-name" || Arg == "-o") {
341+
i++; // Skip this argument and next one.
342+
continue;
343+
}
344+
if (Arg.starts_with("-object-file-name") || Arg == MainFilename) {
345+
continue;
346+
}
347+
// Skip fmessage-length for reproduciability.
348+
if (Arg.starts_with("-fmessage-length")) {
349+
continue;
350+
}
351+
if (PrintedOneArg) {
352+
OS << " ";
353+
}
354+
llvm::sys::printArg(OS, Arg, /*Quote=*/true);
355+
PrintedOneArg = true;
356+
}
357+
OS.flush();
358+
return FlatCmdLine;
359+
}
360+
325361
static bool initTargetOptions(DiagnosticsEngine &Diags,
326362
llvm::TargetOptions &Options,
327363
const CodeGenOptions &CodeGenOpts,
@@ -486,6 +522,8 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
486522
Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
487523
Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
488524
Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
525+
Options.MCOptions.CommandlineArgsFlat = flattenClangCommandLine(
526+
CodeGenOpts.CommandLineArgs, CodeGenOpts.MainFileName);
489527
Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
490528
Options.MCOptions.PPCUseFullRegisterNames =
491529
CodeGenOpts.PPCUseFullRegisterNames;

llvm/include/llvm/MC/MCTargetOptions.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,15 @@ class MCTargetOptions {
9696
std::string AsSecureLogFile;
9797

9898
const char *Argv0 = nullptr;
99+
100+
// Deprecated: Use FlatCommandlineArgs instead
101+
// Arguments here are interpreted as coming from clang, formated and end up in LF_BUILDINFO as CommandLineArgs
99102
ArrayRef<std::string> CommandLineArgs;
100103

104+
// Arguments here end up in LF_BUILDINFO as CommandLineArgs without any additional formating
105+
// If empty falls back to CommandLineArgs
106+
std::string CommandlineArgsFlat;
107+
101108
/// Additional paths to search for `.include` directives when using the
102109
/// integrated assembler.
103110
std::vector<std::string> IASSearchPaths;

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,8 @@ static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable,
893893
return TypeTable.writeLeafType(SIR);
894894
}
895895

896+
// This just exists for backwards compatability for the deprecated MCTargetOptions::CommandLineArgs
897+
// It assumed a clang compiler frontend
896898
static std::string flattenCommandLine(ArrayRef<std::string> Args,
897899
StringRef MainFilename) {
898900
std::string FlatCmdLine;
@@ -950,9 +952,16 @@ void CodeViewDebug::emitBuildInfo() {
950952
if (Asm->TM.Options.MCOptions.Argv0 != nullptr) {
951953
BuildInfoArgs[BuildInfoRecord::BuildTool] =
952954
getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);
953-
BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
954-
TypeTable, flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs,
955-
MainSourceFile->getFilename()));
955+
956+
if (!Asm->TM.Options.MCOptions.CommandlineArgsFlat.empty()) {
957+
BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
958+
TypeTable, Asm->TM.Options.MCOptions.CommandlineArgsFlat);
959+
} else {
960+
BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
961+
TypeTable,
962+
flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs,
963+
MainSourceFile->getFilename()));
964+
}
956965
}
957966
BuildInfoRecord BIR(BuildInfoArgs);
958967
TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);

0 commit comments

Comments
 (0)