Skip to content

Commit ad0eceb

Browse files
committed
rustc_llvm: adapt to flattened CLI args in LLVM
This changed in llvm/llvm-project@e190d07. I decided to stick with more duplication between the ifdef blocks to make the code easier to read for the next two years before we can plausibly drop LLVM 19. @rustbot label: +llvm-main
1 parent 3a22be3 commit ad0eceb

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,21 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
498498
Options.EmitStackSizeSection = EmitStackSizeSection;
499499

500500
if (ArgsCstrBuff != nullptr) {
501+
#if LLVM_VERSION_GE(20, 0)
502+
int buffer_offset = 0;
503+
assert(ArgsCstrBuff[ArgsCstrBuffLen - 1] == '\0');
504+
auto Arg0 = std::string(ArgsCstrBuff);
505+
buffer_offset = Arg0.size() + 1;
506+
auto ArgsCppStr = std::string(ArgsCstrBuff + buffer_offset, ArgsCstrBuffLen - 1);
507+
auto i = 0;
508+
while (i != std::string::npos) {
509+
i = ArgsCppStr.find('\0', i + 1);
510+
if (i != std::string::npos)
511+
ArgsCppStr.replace(i, i + 1, " ");
512+
}
513+
Options.MCOptions.Argv0 = Arg0;
514+
Options.MCOptions.CommandlineArgs = ArgsCppStr;
515+
#else
501516
int buffer_offset = 0;
502517
assert(ArgsCstrBuff[ArgsCstrBuffLen - 1] == '\0');
503518

@@ -523,6 +538,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
523538
Options.MCOptions.Argv0 = arg0;
524539
Options.MCOptions.CommandLineArgs =
525540
llvm::ArrayRef<std::string>(cmd_arg_strings, num_cmd_arg_strings);
541+
#endif
526542
}
527543

528544
TargetMachine *TM = TheTarget->createTargetMachine(
@@ -531,10 +547,11 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
531547
}
532548

533549
extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
534-
550+
#if LLVM_VERSION_LT(20, 0)
535551
MCTargetOptions &MCOptions = unwrap(TM)->Options.MCOptions;
536552
delete[] MCOptions.Argv0;
537553
delete[] MCOptions.CommandLineArgs.data();
554+
#endif
538555

539556
delete unwrap(TM);
540557
}

0 commit comments

Comments
 (0)