Skip to content

Commit f5ba3e1

Browse files
authored
[CodeView] Flatten cmd args in frontend for LF_BUILDINFO (#106369)
1 parent 5c348f6 commit f5ba3e1

File tree

8 files changed

+56
-52
lines changed

8 files changed

+56
-52
lines changed

clang/lib/CodeGen/BackendUtil.cpp

+38-2
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"
@@ -321,6 +322,40 @@ static bool actionRequiresCodeGen(BackendAction Action) {
321322
Action != Backend_EmitLL;
322323
}
323324

325+
static std::string flattenClangCommandLine(ArrayRef<std::string> Args,
326+
StringRef MainFilename) {
327+
if (Args.empty())
328+
return std::string{};
329+
330+
std::string FlatCmdLine;
331+
raw_string_ostream OS(FlatCmdLine);
332+
bool PrintedOneArg = false;
333+
if (!StringRef(Args[0]).contains("-cc1")) {
334+
llvm::sys::printArg(OS, "-cc1", /*Quote=*/true);
335+
PrintedOneArg = true;
336+
}
337+
for (unsigned i = 0; i < Args.size(); i++) {
338+
StringRef Arg = Args[i];
339+
if (Arg.empty())
340+
continue;
341+
if (Arg == "-main-file-name" || Arg == "-o") {
342+
i++; // Skip this argument and next one.
343+
continue;
344+
}
345+
if (Arg.starts_with("-object-file-name") || Arg == MainFilename)
346+
continue;
347+
// Skip fmessage-length for reproducibility.
348+
if (Arg.starts_with("-fmessage-length"))
349+
continue;
350+
if (PrintedOneArg)
351+
OS << " ";
352+
llvm::sys::printArg(OS, Arg, /*Quote=*/true);
353+
PrintedOneArg = true;
354+
}
355+
OS.flush();
356+
return FlatCmdLine;
357+
}
358+
324359
static bool initTargetOptions(DiagnosticsEngine &Diags,
325360
llvm::TargetOptions &Options,
326361
const CodeGenOptions &CodeGenOpts,
@@ -483,8 +518,9 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
483518
Entry.Group == frontend::IncludeDirGroup::System))
484519
Options.MCOptions.IASSearchPaths.push_back(
485520
Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
486-
Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
487-
Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
521+
Options.MCOptions.Argv0 = CodeGenOpts.Argv0 ? CodeGenOpts.Argv0 : "";
522+
Options.MCOptions.CommandlineArgs = flattenClangCommandLine(
523+
CodeGenOpts.CommandLineArgs, CodeGenOpts.MainFileName);
488524
Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
489525
Options.MCOptions.PPCUseFullRegisterNames =
490526
CodeGenOpts.PPCUseFullRegisterNames;

clang/test/CodeGen/debug-info-codeview-buildinfo.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ int main(void) { return 42; }
3636
// DISABLE-NOT: "-cc1"
3737
// DISABLE: 0x{{.+}} | LF_BUILDINFO [size = {{.+}}]
3838
// DISABLE-NEXT: 0x{{.+}}: `{{.*}}`
39-
// DISABLE-NEXT: <no type>: ``
39+
// DISABLE-NEXT: 0x{{.+}}: `{{.*}}`
4040
// DISABLE-NEXT: 0x{{.+}}: `{{.*}}`
4141
// DISABLE-NEXT: 0x{{.+}}: ``
42-
// DISABLE-NEXT: <no type>: ``
42+
// DISABLE-NEXT: 0x{{.+}}: `{{.*}}`
4343

4444
// MESSAGELEN: Types (.debug$T)
4545
// MESSAGELEN: ============================================================

llvm/include/llvm/MC/MCTargetOptions.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ class MCTargetOptions {
9595
std::string SplitDwarfFile;
9696
std::string AsSecureLogFile;
9797

98-
const char *Argv0 = nullptr;
99-
ArrayRef<std::string> CommandLineArgs;
98+
// Used for codeview debug info. These will be set as compiler path and commandline arguments in LF_BUILDINFO
99+
std::string Argv0;
100+
std::string CommandlineArgs;
100101

101102
/// Additional paths to search for `.include` directives when using the
102103
/// integrated assembler.

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

+5-38
Original file line numberDiff line numberDiff line change
@@ -893,37 +893,6 @@ static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable,
893893
return TypeTable.writeLeafType(SIR);
894894
}
895895

896-
static std::string flattenCommandLine(ArrayRef<std::string> Args,
897-
StringRef MainFilename) {
898-
std::string FlatCmdLine;
899-
raw_string_ostream OS(FlatCmdLine);
900-
bool PrintedOneArg = false;
901-
if (!StringRef(Args[0]).contains("-cc1")) {
902-
llvm::sys::printArg(OS, "-cc1", /*Quote=*/true);
903-
PrintedOneArg = true;
904-
}
905-
for (unsigned i = 0; i < Args.size(); i++) {
906-
StringRef Arg = Args[i];
907-
if (Arg.empty())
908-
continue;
909-
if (Arg == "-main-file-name" || Arg == "-o") {
910-
i++; // Skip this argument and next one.
911-
continue;
912-
}
913-
if (Arg.starts_with("-object-file-name") || Arg == MainFilename)
914-
continue;
915-
// Skip fmessage-length for reproduciability.
916-
if (Arg.starts_with("-fmessage-length"))
917-
continue;
918-
if (PrintedOneArg)
919-
OS << " ";
920-
llvm::sys::printArg(OS, Arg, /*Quote=*/true);
921-
PrintedOneArg = true;
922-
}
923-
OS.flush();
924-
return FlatCmdLine;
925-
}
926-
927896
void CodeViewDebug::emitBuildInfo() {
928897
// First, make LF_BUILDINFO. It's a sequence of strings with various bits of
929898
// build info. The known prefix is:
@@ -947,13 +916,11 @@ void CodeViewDebug::emitBuildInfo() {
947916
// FIXME: PDB is intentionally blank unless we implement /Zi type servers.
948917
BuildInfoArgs[BuildInfoRecord::TypeServerPDB] =
949918
getStringIdTypeIdx(TypeTable, "");
950-
if (Asm->TM.Options.MCOptions.Argv0 != nullptr) {
951-
BuildInfoArgs[BuildInfoRecord::BuildTool] =
952-
getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);
953-
BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
954-
TypeTable, flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs,
955-
MainSourceFile->getFilename()));
956-
}
919+
BuildInfoArgs[BuildInfoRecord::BuildTool] =
920+
getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);
921+
BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
922+
TypeTable, Asm->TM.Options.MCOptions.CommandlineArgs);
923+
957924
BuildInfoRecord BIR(BuildInfoArgs);
958925
TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);
959926

llvm/test/DebugInfo/COFF/build-info.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
; CHECK: [[INFO_IDX:0x[^ ]*]] | LF_BUILDINFO
55
; CHECK-NEXT: 0x{{.*}}: `D:\src\scopes\clang`
6-
; CHECK-NEXT: <no type>: ``
6+
; CHECK-NEXT: 0x{{.*}}: ``
77
; CHECK-NEXT: 0x{{.*}}: `D:\src\scopes\foo.cpp`
88
; CHECK-NEXT: 0x{{.*}}: ``
9-
; CHECK-NEXT: <no type>: ``
9+
; CHECK-NEXT: 0x{{.*}}: ``
1010

1111
; CHECK: {{.*}} | S_BUILDINFO [size = 8] BuildId = `[[INFO_IDX]]`
1212

llvm/test/DebugInfo/COFF/global-type-hashes.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ attributes #3 = { mustprogress noinline nounwind optnone "frame-pointer"="none"
300300
; YAML: String: ''
301301
; YAML: - Kind: LF_BUILDINFO
302302
; YAML: BuildInfo:
303-
; YAML: ArgIndices: [ 4113, 0, 4114, 4115, 0 ]
303+
; YAML: ArgIndices: [ 4113, 4115, 4114, 4115, 4115 ]
304304
; YAML: - Name: '.debug$H'
305305
; YAML: Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
306306
; YAML: Alignment: 4
@@ -328,7 +328,7 @@ attributes #3 = { mustprogress noinline nounwind optnone "frame-pointer"="none"
328328
; YAML: - 174CF4A3F5448049
329329
; YAML: - 5349856AF14E2246
330330
; YAML: - 55A48E0466FDCDA6
331-
; YAML: - 886A1B73D31E9877
331+
; YAML: - EE6329A02D9F4959
332332

333333
; ASM: .section .debug$H,"dr"
334334
; ASM-NEXT: .p2align 2

llvm/test/DebugInfo/COFF/types-basic.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,10 @@
525525
; ASM: .short 0x1603 # Record kind: LF_BUILDINFO
526526
; ASM: .short 0x5 # NumArgs
527527
; ASM: .long 0x1013 # Argument: D:\src\llvm\build
528-
; ASM: .long 0x0 # Argument
528+
; ASM: .long 0x1015 # Argument
529529
; ASM: .long 0x1014 # Argument: t.cpp
530530
; ASM: .long 0x1015 # Argument
531-
; ASM: .long 0x0 # Argument
531+
; ASM: .long 0x1015 # Argument
532532
; ASM: .byte 242
533533
; ASM: .byte 241
534534

llvm/test/DebugInfo/COFF/types-data-members.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,10 @@
740740
; ASM: .short 0x1603 # Record kind: LF_BUILDINFO
741741
; ASM: .short 0x5 # NumArgs
742742
; ASM: .long 0x1020 # Argument: D:\src\llvm\build
743-
; ASM: .long 0x0 # Argument
743+
; ASM: .long 0x1022 # Argument
744744
; ASM: .long 0x1021 # Argument: t.cpp
745745
; ASM: .long 0x1022 # Argument
746-
; ASM: .long 0x0 # Argument
746+
; ASM: .long 0x1022 # Argument
747747
; ASM: .byte 242
748748
; ASM: .byte 241
749749

0 commit comments

Comments
 (0)