[clang] [llvm] [CodeView] Flatten cmd args in frontend for LF_BUILDINFO (PR #106369)

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 1 04:33:06 PDT 2024


https://github.com/nebulark updated https://github.com/llvm/llvm-project/pull/106369

>From 60332916c1fba8ad20d3353e279a867807f46273 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/4] 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 564efa3181d188..3ddc6704383016 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"
@@ -322,6 +323,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,
@@ -486,6 +522,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 a5371b3387a13d..0937089d8a2c80 100644
--- a/llvm/include/llvm/MC/MCTargetOptions.h
+++ b/llvm/include/llvm/MC/MCTargetOptions.h
@@ -96,8 +96,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 7700ffd6da8030..60387e4688996b 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 36bd45dca14c49ae43610954b6866e930f549538 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/4] 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 3ddc6704383016..79587b1b19a8ff 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -521,8 +521,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 0937089d8a2c80..6fe028f6c9d96a 100644
--- a/llvm/include/llvm/MC/MCTargetOptions.h
+++ b/llvm/include/llvm/MC/MCTargetOptions.h
@@ -95,15 +95,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 60387e4688996b..f184f320e89c90 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);
 

>From 8977dbb7f70a99444c86e0b03008ff0e206e9063 Mon Sep 17 00:00:00 2001
From: Florian Schmiderer <florian.schmiderer at posteo.net>
Date: Thu, 29 Aug 2024 21:37:37 +0200
Subject: [PATCH 3/4] improved comments

---
 clang/lib/CodeGen/BackendUtil.cpp      | 2 +-
 llvm/include/llvm/MC/MCTargetOptions.h | 4 +---
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 79587b1b19a8ff..0c39e3252bb088 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -344,7 +344,7 @@ static std::string flattenClangCommandLine(ArrayRef<std::string> Args,
     if (Arg.starts_with("-object-file-name") || Arg == MainFilename) {
       continue;
     }
-    // Skip fmessage-length for reproduciability.
+    // Skip fmessage-length for reproducibility.
     if (Arg.starts_with("-fmessage-length")) {
       continue;
     }
diff --git a/llvm/include/llvm/MC/MCTargetOptions.h b/llvm/include/llvm/MC/MCTargetOptions.h
index 6fe028f6c9d96a..2e2025c2e7b2c8 100644
--- a/llvm/include/llvm/MC/MCTargetOptions.h
+++ b/llvm/include/llvm/MC/MCTargetOptions.h
@@ -95,10 +95,8 @@ class MCTargetOptions {
   std::string SplitDwarfFile;
   std::string AsSecureLogFile;
 
-  // This will be set as compiler path in LF_BUILDINFO
+  // Used for codeview debug info. These will be set as compiler path and commandline arguments in LF_BUILDINFO
   std::string Argv0;
-
-  // This will be set as commandline arguments in LF_BUILDINFO
   std::string CommandlineArgs;
 
   /// Additional paths to search for `.include` directives when using the

>From ddb2b4333907c3a718ab1d511b9a118780cd9fc1 Mon Sep 17 00:00:00 2001
From: Florian Schmiderer <florian.schmiderer at posteo.net>
Date: Sun, 1 Sep 2024 13:32:52 +0200
Subject: [PATCH 4/4] fix debuginfo coff tests

---
 llvm/test/DebugInfo/COFF/build-info.ll         | 4 ++--
 llvm/test/DebugInfo/COFF/global-type-hashes.ll | 4 ++--
 llvm/test/DebugInfo/COFF/types-basic.ll        | 4 ++--
 llvm/test/DebugInfo/COFF/types-data-members.ll | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/llvm/test/DebugInfo/COFF/build-info.ll b/llvm/test/DebugInfo/COFF/build-info.ll
index 983aa22214bce9..429aee2a236b7d 100644
--- a/llvm/test/DebugInfo/COFF/build-info.ll
+++ b/llvm/test/DebugInfo/COFF/build-info.ll
@@ -3,10 +3,10 @@
 
 ; CHECK: [[INFO_IDX:0x[^ ]*]] | LF_BUILDINFO
 ; CHECK-NEXT:          0x{{.*}}: `D:\src\scopes\clang`
-; CHECK-NEXT:          <no type>: ``
+; CHECK-NEXT:          0x{{.*}}: ``
 ; CHECK-NEXT:          0x{{.*}}: `D:\src\scopes\foo.cpp`
 ; CHECK-NEXT:          0x{{.*}}: ``
-; CHECK-NEXT:          <no type>: ``
+; CHECK-NEXT:          0x{{.*}}: ``
 
 ; CHECK: {{.*}} | S_BUILDINFO [size = 8] BuildId = `[[INFO_IDX]]`
 
diff --git a/llvm/test/DebugInfo/COFF/global-type-hashes.ll b/llvm/test/DebugInfo/COFF/global-type-hashes.ll
index 586b97d8fd193a..6dda6fefe6f727 100644
--- a/llvm/test/DebugInfo/COFF/global-type-hashes.ll
+++ b/llvm/test/DebugInfo/COFF/global-type-hashes.ll
@@ -300,7 +300,7 @@ attributes #3 = { mustprogress noinline nounwind optnone "frame-pointer"="none"
 ; YAML:           String:          ''
 ; YAML:       - Kind:            LF_BUILDINFO
 ; YAML:         BuildInfo:
-; YAML:           ArgIndices:      [ 4113, 0, 4114, 4115, 0 ]
+; YAML:           ArgIndices:      [ 4113, 4115, 4114, 4115, 4115 ]
 ; YAML:   - Name:            '.debug$H'
 ; YAML:     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
 ; YAML:     Alignment:       4
@@ -328,7 +328,7 @@ attributes #3 = { mustprogress noinline nounwind optnone "frame-pointer"="none"
 ; YAML:         - 174CF4A3F5448049
 ; YAML:         - 5349856AF14E2246
 ; YAML:         - 55A48E0466FDCDA6
-; YAML:         - 886A1B73D31E9877
+; YAML:         - EE6329A02D9F4959
 
 ; ASM:      .section        .debug$H,"dr"
 ; ASM-NEXT: .p2align        2
diff --git a/llvm/test/DebugInfo/COFF/types-basic.ll b/llvm/test/DebugInfo/COFF/types-basic.ll
index a3741cca742491..897c63268a4004 100644
--- a/llvm/test/DebugInfo/COFF/types-basic.ll
+++ b/llvm/test/DebugInfo/COFF/types-basic.ll
@@ -525,10 +525,10 @@
 ; ASM: .short	0x1603                  # Record kind: LF_BUILDINFO
 ; ASM: .short	0x5                     # NumArgs
 ; ASM: .long	0x1013                  # Argument: D:\src\llvm\build
-; ASM: .long	0x0                     # Argument
+; ASM: .long	0x1015                  # Argument
 ; ASM: .long	0x1014                  # Argument: t.cpp
 ; ASM: .long	0x1015                  # Argument
-; ASM: .long	0x0                     # Argument
+; ASM: .long	0x1015                  # Argument
 ; ASM: .byte	242
 ; ASM: .byte	241
 
diff --git a/llvm/test/DebugInfo/COFF/types-data-members.ll b/llvm/test/DebugInfo/COFF/types-data-members.ll
index c1f0df7bb7ef98..af0af47ee64739 100644
--- a/llvm/test/DebugInfo/COFF/types-data-members.ll
+++ b/llvm/test/DebugInfo/COFF/types-data-members.ll
@@ -740,10 +740,10 @@
 ; ASM: .short	0x1603                  # Record kind: LF_BUILDINFO
 ; ASM: .short	0x5                     # NumArgs
 ; ASM: .long	0x1020                  # Argument: D:\src\llvm\build
-; ASM: .long	0x0                     # Argument
+; ASM: .long	0x1022                  # Argument
 ; ASM: .long	0x1021                  # Argument: t.cpp
 ; ASM: .long	0x1022                  # Argument
-; ASM: .long	0x0                     # Argument
+; ASM: .long	0x1022                  # Argument
 ; ASM: .byte	242
 ; ASM: .byte	241
 



More information about the cfe-commits mailing list