From 75b0bbca1d0cb1967025bdaabddac8f75f1369bc Mon Sep 17 00:00:00 2001 From: Momchil Velikov Date: Mon, 22 Jun 2020 11:14:53 +0100 Subject: [PATCH] [LTO] Use StringRef instead of C-style strings in setCodeGenDebugOptions Fixes an issue with missing nul-terminators and saves us some string copying, compared to a version which would insert nul-terminators. Differential Revision: https://reviews.llvm.org/D82033 --- llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h | 2 +- llvm/lib/LTO/LTOCodeGenerator.cpp | 4 ++-- llvm/tools/lto/lto.cpp | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h b/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h index 5bcfaeedfcf6..d7ccc0d5a6c5 100644 --- a/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h +++ b/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h @@ -123,7 +123,7 @@ struct LTOCodeGenerator { /// name is misleading). This function should be called before /// LTOCodeGenerator::compilexxx(), and /// LTOCodeGenerator::writeMergedModules(). - void setCodeGenDebugOptions(ArrayRef Opts); + void setCodeGenDebugOptions(ArrayRef Opts); /// Parse the options set in setCodeGenDebugOptions. /// diff --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp index a8a7877f66da..25ab1404b4e1 100644 --- a/llvm/lib/LTO/LTOCodeGenerator.cpp +++ b/llvm/lib/LTO/LTOCodeGenerator.cpp @@ -632,9 +632,9 @@ bool LTOCodeGenerator::compileOptimized(ArrayRef Out) { return true; } -void LTOCodeGenerator::setCodeGenDebugOptions(ArrayRef Options) { +void LTOCodeGenerator::setCodeGenDebugOptions(ArrayRef Options) { for (StringRef Option : Options) - CodegenOptions.push_back(std::string(Option)); + CodegenOptions.push_back(Option.str()); } void LTOCodeGenerator::parseCodeGenDebugOptions() { diff --git a/llvm/tools/lto/lto.cpp b/llvm/tools/lto/lto.cpp index d48580606c4b..ea44430f66a7 100644 --- a/llvm/tools/lto/lto.cpp +++ b/llvm/tools/lto/lto.cpp @@ -474,17 +474,20 @@ bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) { } void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) { - std::vector Options; + SmallVector Options; for (std::pair o = getToken(opt); !o.first.empty(); o = getToken(o.second)) - Options.push_back(o.first.data()); + Options.push_back(o.first); unwrap(cg)->setCodeGenDebugOptions(Options); } void lto_codegen_debug_options_array(lto_code_gen_t cg, const char *const *options, int number) { - unwrap(cg)->setCodeGenDebugOptions(makeArrayRef(options, number)); + SmallVector Options; + for (int i = 0; i < number; ++i) + Options.push_back(options[i]); + unwrap(cg)->setCodeGenDebugOptions(makeArrayRef(Options)); } unsigned int lto_api_version() { return LTO_API_VERSION; }