[clang] [llvm] [CodeView] Flatten cmd args in frontend for LF_BUILDINFO (PR #106369)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 28 10:08:02 PDT 2024
https://github.com/nebulark updated https://github.com/llvm/llvm-project/pull/106369
>From aad31f04028994801a6034b966820ef5353da219 Mon Sep 17 00:00:00 2001
From: Florian Schmiderer <florian.schmiderer at posteo.net>
Date: Wed, 28 Aug 2024 12:36:39 +0200
Subject: [PATCH 1/2] Flatten compiler args in Frontend, keep existing
behaviour as fallback for other frontends
---
clang/lib/CodeGen/BackendUtil.cpp | 38 +++++++++++++++++++
llvm/include/llvm/MC/MCTargetOptions.h | 7 ++++
llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp | 15 ++++++--
3 files changed, 57 insertions(+), 3 deletions(-)
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index e765bbf637a661..cc6bb5824cbe98 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -50,6 +50,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/Program.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/ToolOutputFile.h"
@@ -321,6 +322,41 @@ static bool actionRequiresCodeGen(BackendAction Action) {
Action != Backend_EmitLL;
}
+static std::string flattenClangCommandLine(ArrayRef<std::string> Args,
+ StringRef MainFilename) {
+ std::string FlatCmdLine;
+ raw_string_ostream OS(FlatCmdLine);
+ bool PrintedOneArg = false;
+ if (!StringRef(Args[0]).contains("-cc1")) {
+ llvm::sys::printArg(OS, "-cc1", /*Quote=*/true);
+ PrintedOneArg = true;
+ }
+ for (unsigned i = 0; i < Args.size(); i++) {
+ StringRef Arg = Args[i];
+ if (Arg.empty()) {
+ continue;
+ }
+ if (Arg == "-main-file-name" || Arg == "-o") {
+ i++; // Skip this argument and next one.
+ continue;
+ }
+ if (Arg.starts_with("-object-file-name") || Arg == MainFilename) {
+ continue;
+ }
+ // Skip fmessage-length for reproduciability.
+ if (Arg.starts_with("-fmessage-length")) {
+ continue;
+ }
+ if (PrintedOneArg) {
+ OS << " ";
+ }
+ llvm::sys::printArg(OS, Arg, /*Quote=*/true);
+ PrintedOneArg = true;
+ }
+ OS.flush();
+ return FlatCmdLine;
+}
+
static bool initTargetOptions(DiagnosticsEngine &Diags,
llvm::TargetOptions &Options,
const CodeGenOptions &CodeGenOpts,
@@ -484,6 +520,8 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
+ Options.MCOptions.CommandlineArgsFlat = flattenClangCommandLine(
+ CodeGenOpts.CommandLineArgs, CodeGenOpts.MainFileName);
Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
Options.MCOptions.PPCUseFullRegisterNames =
CodeGenOpts.PPCUseFullRegisterNames;
diff --git a/llvm/include/llvm/MC/MCTargetOptions.h b/llvm/include/llvm/MC/MCTargetOptions.h
index 98317712250bf9..f4a002c82ef329 100644
--- a/llvm/include/llvm/MC/MCTargetOptions.h
+++ b/llvm/include/llvm/MC/MCTargetOptions.h
@@ -92,8 +92,15 @@ class MCTargetOptions {
std::string AsSecureLogFile;
const char *Argv0 = nullptr;
+
+ // Deprecated: Use FlatCommandlineArgs instead
+ // Arguments here are interpreted as coming from clang, formated and end up in LF_BUILDINFO as CommandLineArgs
ArrayRef<std::string> CommandLineArgs;
+ // Arguments here end up in LF_BUILDINFO as CommandLineArgs without any additional formating
+ // If empty falls back to CommandLineArgs
+ std::string CommandlineArgsFlat;
+
/// Additional paths to search for `.include` directives when using the
/// integrated assembler.
std::vector<std::string> IASSearchPaths;
diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
index dddc08b3bc0166..f95a8f1c428b86 100644
--- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
@@ -893,6 +893,8 @@ static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable,
return TypeTable.writeLeafType(SIR);
}
+// This just exists for backwards compatability for the deprecated MCTargetOptions::CommandLineArgs
+// It assumed a clang compiler frontend
static std::string flattenCommandLine(ArrayRef<std::string> Args,
StringRef MainFilename) {
std::string FlatCmdLine;
@@ -950,9 +952,16 @@ void CodeViewDebug::emitBuildInfo() {
if (Asm->TM.Options.MCOptions.Argv0 != nullptr) {
BuildInfoArgs[BuildInfoRecord::BuildTool] =
getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);
- BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
- TypeTable, flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs,
- MainSourceFile->getFilename()));
+
+ if (!Asm->TM.Options.MCOptions.CommandlineArgsFlat.empty()) {
+ BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
+ TypeTable, Asm->TM.Options.MCOptions.CommandlineArgsFlat);
+ } else {
+ BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
+ TypeTable,
+ flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs,
+ MainSourceFile->getFilename()));
+ }
}
BuildInfoRecord BIR(BuildInfoArgs);
TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);
>From 2ba83b5fd1bfbdcd5f65e34e1a2b7f14999f3654 Mon Sep 17 00:00:00 2001
From: Florian Schmiderer <florian.schmiderer at posteo.net>
Date: Wed, 28 Aug 2024 19:07:48 +0200
Subject: [PATCH 2/2] removed old commanline arguments, applied suggested
changes, made Arg0 an owned string
---
clang/lib/CodeGen/BackendUtil.cpp | 3 +-
llvm/include/llvm/MC/MCTargetOptions.h | 12 ++---
llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp | 50 ++-----------------
3 files changed, 9 insertions(+), 56 deletions(-)
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index cc6bb5824cbe98..c62894578122e7 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -519,8 +519,7 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
Options.MCOptions.IASSearchPaths.push_back(
Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
- Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
- Options.MCOptions.CommandlineArgsFlat = flattenClangCommandLine(
+ Options.MCOptions.CommandlineArgs = flattenClangCommandLine(
CodeGenOpts.CommandLineArgs, CodeGenOpts.MainFileName);
Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
Options.MCOptions.PPCUseFullRegisterNames =
diff --git a/llvm/include/llvm/MC/MCTargetOptions.h b/llvm/include/llvm/MC/MCTargetOptions.h
index f4a002c82ef329..d83671e1070f18 100644
--- a/llvm/include/llvm/MC/MCTargetOptions.h
+++ b/llvm/include/llvm/MC/MCTargetOptions.h
@@ -91,15 +91,11 @@ class MCTargetOptions {
std::string SplitDwarfFile;
std::string AsSecureLogFile;
- const char *Argv0 = nullptr;
+ // This will be set as compiler path in LF_BUILDINFO
+ std::string Argv0;
- // Deprecated: Use FlatCommandlineArgs instead
- // Arguments here are interpreted as coming from clang, formated and end up in LF_BUILDINFO as CommandLineArgs
- ArrayRef<std::string> CommandLineArgs;
-
- // Arguments here end up in LF_BUILDINFO as CommandLineArgs without any additional formating
- // If empty falls back to CommandLineArgs
- std::string CommandlineArgsFlat;
+ // This will be set as commandline arguments in LF_BUILDINFO
+ std::string CommandlineArgs;
/// Additional paths to search for `.include` directives when using the
/// integrated assembler.
diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
index f95a8f1c428b86..6b7675bb969e2c 100644
--- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
@@ -893,39 +893,6 @@ static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable,
return TypeTable.writeLeafType(SIR);
}
-// This just exists for backwards compatability for the deprecated MCTargetOptions::CommandLineArgs
-// It assumed a clang compiler frontend
-static std::string flattenCommandLine(ArrayRef<std::string> Args,
- StringRef MainFilename) {
- std::string FlatCmdLine;
- raw_string_ostream OS(FlatCmdLine);
- bool PrintedOneArg = false;
- if (!StringRef(Args[0]).contains("-cc1")) {
- llvm::sys::printArg(OS, "-cc1", /*Quote=*/true);
- PrintedOneArg = true;
- }
- for (unsigned i = 0; i < Args.size(); i++) {
- StringRef Arg = Args[i];
- if (Arg.empty())
- continue;
- if (Arg == "-main-file-name" || Arg == "-o") {
- i++; // Skip this argument and next one.
- continue;
- }
- if (Arg.starts_with("-object-file-name") || Arg == MainFilename)
- continue;
- // Skip fmessage-length for reproduciability.
- if (Arg.starts_with("-fmessage-length"))
- continue;
- if (PrintedOneArg)
- OS << " ";
- llvm::sys::printArg(OS, Arg, /*Quote=*/true);
- PrintedOneArg = true;
- }
- OS.flush();
- return FlatCmdLine;
-}
-
void CodeViewDebug::emitBuildInfo() {
// First, make LF_BUILDINFO. It's a sequence of strings with various bits of
// build info. The known prefix is:
@@ -949,20 +916,11 @@ void CodeViewDebug::emitBuildInfo() {
// FIXME: PDB is intentionally blank unless we implement /Zi type servers.
BuildInfoArgs[BuildInfoRecord::TypeServerPDB] =
getStringIdTypeIdx(TypeTable, "");
- if (Asm->TM.Options.MCOptions.Argv0 != nullptr) {
- BuildInfoArgs[BuildInfoRecord::BuildTool] =
- getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);
+ BuildInfoArgs[BuildInfoRecord::BuildTool] =
+ getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);
+ BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
+ TypeTable, Asm->TM.Options.MCOptions.CommandlineArgs);
- if (!Asm->TM.Options.MCOptions.CommandlineArgsFlat.empty()) {
- BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
- TypeTable, Asm->TM.Options.MCOptions.CommandlineArgsFlat);
- } else {
- BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
- TypeTable,
- flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs,
- MainSourceFile->getFilename()));
- }
- }
BuildInfoRecord BIR(BuildInfoArgs);
TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);
More information about the cfe-commits
mailing list