Skip to content

Commit e190d07

Browse files
committed
[Codeview] Moved flattening of commandline arguments into frontend.
This allows non-clang frontends use their own version. Made Arv0 owned. Output empty string for commandline and Argv0 in LF_BUILDINFO, if those are not set.
1 parent 50a02e7 commit e190d07

File tree

8 files changed

+57
-52
lines changed

8 files changed

+57
-52
lines changed

clang/lib/CodeGen/BackendUtil.cpp

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

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