[llvm] 75b0bbc - [LTO] Use StringRef instead of C-style strings in setCodeGenDebugOptions

Momchil Velikov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 22 03:22:49 PDT 2020


Author: Momchil Velikov
Date: 2020-06-22T11:22:18+01:00
New Revision: 75b0bbca1d0cb1967025bdaabddac8f75f1369bc

URL: https://github.com/llvm/llvm-project/commit/75b0bbca1d0cb1967025bdaabddac8f75f1369bc
DIFF: https://github.com/llvm/llvm-project/commit/75b0bbca1d0cb1967025bdaabddac8f75f1369bc.diff

LOG: [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

Added: 
    

Modified: 
    llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
    llvm/lib/LTO/LTOCodeGenerator.cpp
    llvm/tools/lto/lto.cpp

Removed: 
    


################################################################################
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<const char *> Opts);
+  void setCodeGenDebugOptions(ArrayRef<StringRef> 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<raw_pwrite_stream *> Out) {
   return true;
 }
 
-void LTOCodeGenerator::setCodeGenDebugOptions(ArrayRef<const char *> Options) {
+void LTOCodeGenerator::setCodeGenDebugOptions(ArrayRef<StringRef> 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<const char *> Options;
+  SmallVector<StringRef, 4> Options;
   for (std::pair<StringRef, StringRef> 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<StringRef, 4> 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; }


        


More information about the llvm-commits mailing list