[llvm-commits] [llvm-gcc-4.2] r60171 - /llvm-gcc-4.2/trunk/gcc/llvm-backend.cpp

Duncan Sands baldrick at free.fr
Thu Nov 27 06:22:31 PST 2008


Author: baldrick
Date: Thu Nov 27 08:22:29 2008
New Revision: 60171

URL: http://llvm.org/viewvc/llvm-project?rev=60171&view=rev
Log:
Make "-mllvm --disable-llvm-optzns" work again.  Two
problems: the option parser expects the first option
to be the program name, but here a real option was
being passed meaning it was skipped.  The other problem
is that the std::string returned by getToken was being
freed at the end of the local block, resulting in the
option parser reading freed memory, with very strange
consequences.  The first problem is solved by parsing
all options at the same time, rather than it two sets
(the first lot of option parsing code correctly added
the program name).  The second by introducing an array
of std::string in order to keep the tokens alive until
the end of the function.

Modified:
    llvm-gcc-4.2/trunk/gcc/llvm-backend.cpp

Modified: llvm-gcc-4.2/trunk/gcc/llvm-backend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/llvm-backend.cpp?rev=60171&r1=60170&r2=60171&view=diff

==============================================================================
--- llvm-gcc-4.2/trunk/gcc/llvm-backend.cpp (original)
+++ llvm-gcc-4.2/trunk/gcc/llvm-backend.cpp Thu Nov 27 08:22:29 2008
@@ -174,8 +174,15 @@
 
   for (unsigned i = 0, e = ArgStrings.size(); i != e; ++i)
     Args.push_back(ArgStrings[i].c_str());
+
+  std::vector<std::string> LLVM_Optns; // Avoid deallocation before opts parsed!
+  if (llvm_optns) {
+    SplitString(llvm_optns, LLVM_Optns);
+    for(unsigned i = 0, e = LLVM_Optns.size(); i != e; ++i)
+      Args.push_back(LLVM_Optns[i].c_str());
+  }
+ 
   Args.push_back(0);  // Null terminator.
-  
   int pseudo_argc = Args.size()-1;
   cl::ParseCommandLineOptions(pseudo_argc, (char**)&Args[0]);
 
@@ -239,19 +246,7 @@
     RegisterRegAlloc::setDefault(createLinearScanRegisterAllocator);
   else
     RegisterRegAlloc::setDefault(createLocalRegisterAllocator);
-  
-  Args.clear();
-  if (llvm_optns) {
-    std::string Opts = llvm_optns;
-    for (std::string Opt = getToken(Opts); !Opt.empty(); Opt = getToken(Opts))
-      Args.push_back(Opt.c_str());
-  }
-  if (!Args.empty()) {
-    Args.push_back(0);  // Null terminator.
-    pseudo_argc = Args.size()-1;
-    cl::ParseCommandLineOptions(pseudo_argc, (char**)&Args[0]);
-  }
- 
+
   if (!optimize && debug_info_level > DINFO_LEVEL_NONE)
     TheDebugInfo = new DebugInfo(TheModule);
 }





More information about the llvm-commits mailing list