blob: 29143377628dced506cc987259cd3873e5d8b86c [file] [log] [blame]
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001//===-- CommandLine.cpp - Command line parser implementation --------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerdbab15a2001-07-23 17:17:47 +00009//
10// This class implements a command line argument processor that is useful when
11// creating a tool. It provides a simple, minimalistic interface that is easily
12// extensible and supports nonlocal (library) command line options.
13//
Chris Lattner03fe1bd2001-07-23 23:04:07 +000014// Note that rather than trying to figure out what this code does, you could try
15// reading the library documentation located in docs/CommandLine.html
16//
Chris Lattnerdbab15a2001-07-23 17:17:47 +000017//===----------------------------------------------------------------------===//
18
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/CommandLine.h"
Sandeep Patelbf177ee2009-11-11 03:23:46 +000020#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000021#include "llvm/Support/ErrorHandling.h"
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +000022#include "llvm/Support/MemoryBuffer.h"
Chris Lattner90aa8392006-10-04 21:52:35 +000023#include "llvm/Support/ManagedStatic.h"
Chris Lattnerca179342009-08-23 18:09:02 +000024#include "llvm/Support/raw_ostream.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000025#include "llvm/Support/system_error.h"
Daniel Dunbar603bea32009-07-16 02:06:09 +000026#include "llvm/Target/TargetRegistry.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000027#include "llvm/Support/Host.h"
28#include "llvm/Support/Path.h"
Chris Lattnerca179342009-08-23 18:09:02 +000029#include "llvm/ADT/OwningPtr.h"
Chris Lattner67aead62009-09-20 05:12:14 +000030#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner970e7df2009-09-19 23:59:02 +000031#include "llvm/ADT/SmallString.h"
Chris Lattner67aead62009-09-20 05:12:14 +000032#include "llvm/ADT/StringMap.h"
Chris Lattnera460beb2009-09-19 18:55:05 +000033#include "llvm/ADT/Twine.h"
Chris Lattnerca179342009-08-23 18:09:02 +000034#include "llvm/Config/config.h"
Brian Gaeke2d6a2362003-10-10 17:01:36 +000035#include <cerrno>
Chris Lattnerca179342009-08-23 18:09:02 +000036#include <cstdlib>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000037using namespace llvm;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000038using namespace cl;
39
Chris Lattner7422a762006-08-27 12:45:47 +000040//===----------------------------------------------------------------------===//
41// Template instantiations and anchors.
42//
Douglas Gregorb3587cf2009-11-25 06:04:18 +000043namespace llvm { namespace cl {
Chris Lattner7422a762006-08-27 12:45:47 +000044TEMPLATE_INSTANTIATION(class basic_parser<bool>);
Dale Johannesen81da02b2007-05-22 17:14:46 +000045TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
Chris Lattner7422a762006-08-27 12:45:47 +000046TEMPLATE_INSTANTIATION(class basic_parser<int>);
47TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
48TEMPLATE_INSTANTIATION(class basic_parser<double>);
49TEMPLATE_INSTANTIATION(class basic_parser<float>);
50TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
Bill Wendlingb587f962009-04-29 23:26:16 +000051TEMPLATE_INSTANTIATION(class basic_parser<char>);
Chris Lattner7422a762006-08-27 12:45:47 +000052
53TEMPLATE_INSTANTIATION(class opt<unsigned>);
54TEMPLATE_INSTANTIATION(class opt<int>);
55TEMPLATE_INSTANTIATION(class opt<std::string>);
Bill Wendlingb587f962009-04-29 23:26:16 +000056TEMPLATE_INSTANTIATION(class opt<char>);
Chris Lattner7422a762006-08-27 12:45:47 +000057TEMPLATE_INSTANTIATION(class opt<bool>);
Douglas Gregorb3587cf2009-11-25 06:04:18 +000058} } // end namespace llvm::cl
Chris Lattner7422a762006-08-27 12:45:47 +000059
60void Option::anchor() {}
61void basic_parser_impl::anchor() {}
62void parser<bool>::anchor() {}
Dale Johannesen81da02b2007-05-22 17:14:46 +000063void parser<boolOrDefault>::anchor() {}
Chris Lattner7422a762006-08-27 12:45:47 +000064void parser<int>::anchor() {}
65void parser<unsigned>::anchor() {}
66void parser<double>::anchor() {}
67void parser<float>::anchor() {}
68void parser<std::string>::anchor() {}
Bill Wendlingb587f962009-04-29 23:26:16 +000069void parser<char>::anchor() {}
Chris Lattner7422a762006-08-27 12:45:47 +000070
71//===----------------------------------------------------------------------===//
72
Chris Lattnerefa3da52006-10-13 00:06:24 +000073// Globals for name and overview of program. Program name is not a string to
74// avoid static ctor/dtor issues.
75static char ProgramName[80] = "<premain>";
Reid Spencere1cc1502004-09-01 04:41:28 +000076static const char *ProgramOverview = 0;
77
Chris Lattnerc540ebb2004-11-19 17:08:15 +000078// This collects additional help to be printed.
Chris Lattner90aa8392006-10-04 21:52:35 +000079static ManagedStatic<std::vector<const char*> > MoreHelp;
Chris Lattnerc540ebb2004-11-19 17:08:15 +000080
Chris Lattner90aa8392006-10-04 21:52:35 +000081extrahelp::extrahelp(const char *Help)
Chris Lattnerc540ebb2004-11-19 17:08:15 +000082 : morehelp(Help) {
Chris Lattner90aa8392006-10-04 21:52:35 +000083 MoreHelp->push_back(Help);
Chris Lattnerc540ebb2004-11-19 17:08:15 +000084}
85
Chris Lattner69d6f132007-04-12 00:36:29 +000086static bool OptionListChanged = false;
87
88// MarkOptionsChanged - Internal helper function.
89void cl::MarkOptionsChanged() {
90 OptionListChanged = true;
91}
92
Chris Lattner9878d6a2007-04-06 21:06:55 +000093/// RegisteredOptionList - This is the list of the command line options that
94/// have statically constructed themselves.
95static Option *RegisteredOptionList = 0;
96
97void Option::addArgument() {
98 assert(NextRegistered == 0 && "argument multiply registered!");
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +000099
Chris Lattner9878d6a2007-04-06 21:06:55 +0000100 NextRegistered = RegisteredOptionList;
101 RegisteredOptionList = this;
Chris Lattner69d6f132007-04-12 00:36:29 +0000102 MarkOptionsChanged();
Chris Lattner9878d6a2007-04-06 21:06:55 +0000103}
104
Chris Lattner69d6f132007-04-12 00:36:29 +0000105
Chris Lattner331de232002-07-22 02:07:59 +0000106//===----------------------------------------------------------------------===//
Chris Lattner7422a762006-08-27 12:45:47 +0000107// Basic, shared command line option processing machinery.
Chris Lattner331de232002-07-22 02:07:59 +0000108//
109
Chris Lattner9878d6a2007-04-06 21:06:55 +0000110/// GetOptionInfo - Scan the list of registered options, turning them into data
111/// structures that are easier to handle.
Chris Lattner49b301c2009-09-20 06:18:38 +0000112static void GetOptionInfo(SmallVectorImpl<Option*> &PositionalOpts,
113 SmallVectorImpl<Option*> &SinkOpts,
Benjamin Kramer461c8762009-09-19 10:01:45 +0000114 StringMap<Option*> &OptionsMap) {
Chris Lattner1908aea2009-09-20 06:21:43 +0000115 SmallVector<const char*, 16> OptionNames;
Chris Lattneree2b3202007-04-07 05:38:53 +0000116 Option *CAOpt = 0; // The ConsumeAfter option if it exists.
Chris Lattner9878d6a2007-04-06 21:06:55 +0000117 for (Option *O = RegisteredOptionList; O; O = O->getNextRegisteredOption()) {
118 // If this option wants to handle multiple option names, get the full set.
119 // This handles enum options like "-O1 -O2" etc.
120 O->getExtraOptionNames(OptionNames);
121 if (O->ArgStr[0])
122 OptionNames.push_back(O->ArgStr);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000123
Chris Lattner9878d6a2007-04-06 21:06:55 +0000124 // Handle named options.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000125 for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
Chris Lattner9878d6a2007-04-06 21:06:55 +0000126 // Add argument to the argument map!
Benjamin Kramer461c8762009-09-19 10:01:45 +0000127 if (OptionsMap.GetOrCreateValue(OptionNames[i], O).second != O) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000128 errs() << ProgramName << ": CommandLine Error: Argument '"
Matthijs Kooijman33540ad2008-05-30 13:26:11 +0000129 << OptionNames[i] << "' defined more than once!\n";
Chris Lattner9878d6a2007-04-06 21:06:55 +0000130 }
131 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000132
Chris Lattner9878d6a2007-04-06 21:06:55 +0000133 OptionNames.clear();
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000134
Chris Lattner9878d6a2007-04-06 21:06:55 +0000135 // Remember information about positional options.
136 if (O->getFormattingFlag() == cl::Positional)
137 PositionalOpts.push_back(O);
Dan Gohman61e015f2008-02-23 01:55:25 +0000138 else if (O->getMiscFlags() & cl::Sink) // Remember sink options
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000139 SinkOpts.push_back(O);
Chris Lattner9878d6a2007-04-06 21:06:55 +0000140 else if (O->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattneree2b3202007-04-07 05:38:53 +0000141 if (CAOpt)
Chris Lattner9878d6a2007-04-06 21:06:55 +0000142 O->error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattneree2b3202007-04-07 05:38:53 +0000143 CAOpt = O;
Chris Lattner9878d6a2007-04-06 21:06:55 +0000144 }
Chris Lattnere8e258b2002-07-29 20:58:42 +0000145 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000146
Chris Lattneree2b3202007-04-07 05:38:53 +0000147 if (CAOpt)
148 PositionalOpts.push_back(CAOpt);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000149
Chris Lattneree2b3202007-04-07 05:38:53 +0000150 // Make sure that they are in order of registration not backwards.
151 std::reverse(PositionalOpts.begin(), PositionalOpts.end());
Chris Lattnere8e258b2002-07-29 20:58:42 +0000152}
153
Chris Lattner9878d6a2007-04-06 21:06:55 +0000154
Chris Lattneraf035f32007-04-05 21:58:17 +0000155/// LookupOption - Lookup the option specified by the specified option on the
156/// command line. If there is a value specified (after an equal sign) return
Chris Lattnerb1687372009-09-20 05:03:30 +0000157/// that as well. This assumes that leading dashes have already been stripped.
Chris Lattner8a7a0582009-09-20 02:02:24 +0000158static Option *LookupOption(StringRef &Arg, StringRef &Value,
159 const StringMap<Option*> &OptionsMap) {
Chris Lattner8a7a0582009-09-20 02:02:24 +0000160 // Reject all dashes.
161 if (Arg.empty()) return 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000162
Chris Lattner8a7a0582009-09-20 02:02:24 +0000163 size_t EqualPos = Arg.find('=');
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000164
Chris Lattner4e247ec2009-09-20 01:53:12 +0000165 // If we have an equals sign, remember the value.
Chris Lattnerb1687372009-09-20 05:03:30 +0000166 if (EqualPos == StringRef::npos) {
167 // Look up the option.
168 StringMap<Option*>::const_iterator I = OptionsMap.find(Arg);
169 return I != OptionsMap.end() ? I->second : 0;
Chris Lattner8a7a0582009-09-20 02:02:24 +0000170 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000171
Chris Lattnerb1687372009-09-20 05:03:30 +0000172 // If the argument before the = is a valid option name, we match. If not,
173 // return Arg unmolested.
174 StringMap<Option*>::const_iterator I =
175 OptionsMap.find(Arg.substr(0, EqualPos));
176 if (I == OptionsMap.end()) return 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000177
Chris Lattnerb1687372009-09-20 05:03:30 +0000178 Value = Arg.substr(EqualPos+1);
179 Arg = Arg.substr(0, EqualPos);
180 return I->second;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000181}
182
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000183/// LookupNearestOption - Lookup the closest match to the option specified by
184/// the specified option on the command line. If there is a value specified
185/// (after an equal sign) return that as well. This assumes that leading dashes
186/// have already been stripped.
187static Option *LookupNearestOption(StringRef Arg,
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000188 const StringMap<Option*> &OptionsMap,
Nick Lewycky95d206a2011-05-02 05:24:47 +0000189 std::string &NearestString) {
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000190 // Reject all dashes.
191 if (Arg.empty()) return 0;
192
193 // Split on any equal sign.
Nick Lewycky95d206a2011-05-02 05:24:47 +0000194 std::pair<StringRef, StringRef> SplitArg = Arg.split('=');
195 StringRef &LHS = SplitArg.first; // LHS == Arg when no '=' is present.
196 StringRef &RHS = SplitArg.second;
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000197
198 // Find the closest match.
199 Option *Best = 0;
200 unsigned BestDistance = 0;
201 for (StringMap<Option*>::const_iterator it = OptionsMap.begin(),
202 ie = OptionsMap.end(); it != ie; ++it) {
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000203 Option *O = it->second;
204 SmallVector<const char*, 16> OptionNames;
205 O->getExtraOptionNames(OptionNames);
206 if (O->ArgStr[0])
207 OptionNames.push_back(O->ArgStr);
208
Nick Lewycky95d206a2011-05-02 05:24:47 +0000209 bool PermitValue = O->getValueExpectedFlag() != cl::ValueDisallowed;
210 StringRef Flag = PermitValue ? LHS : Arg;
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000211 for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
212 StringRef Name = OptionNames[i];
213 unsigned Distance = StringRef(Name).edit_distance(
Nick Lewycky95d206a2011-05-02 05:24:47 +0000214 Flag, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000215 if (!Best || Distance < BestDistance) {
216 Best = O;
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000217 BestDistance = Distance;
Nick Lewycky95d206a2011-05-02 05:24:47 +0000218 if (RHS.empty() || !PermitValue)
219 NearestString = OptionNames[i];
220 else
221 NearestString = std::string(OptionNames[i]) + "=" + RHS.str();
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000222 }
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000223 }
224 }
225
226 return Best;
227}
228
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000229/// CommaSeparateAndAddOccurence - A wrapper around Handler->addOccurence() that
230/// does special handling of cl::CommaSeparated options.
231static bool CommaSeparateAndAddOccurence(Option *Handler, unsigned pos,
232 StringRef ArgName,
233 StringRef Value, bool MultiArg = false)
234{
235 // Check to see if this option accepts a comma separated list of values. If
236 // it does, we have to split up the value into multiple values.
237 if (Handler->getMiscFlags() & CommaSeparated) {
238 StringRef Val(Value);
239 StringRef::size_type Pos = Val.find(',');
Chris Lattnerb1687372009-09-20 05:03:30 +0000240
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000241 while (Pos != StringRef::npos) {
242 // Process the portion before the comma.
243 if (Handler->addOccurrence(pos, ArgName, Val.substr(0, Pos), MultiArg))
244 return true;
245 // Erase the portion before the comma, AND the comma.
246 Val = Val.substr(Pos+1);
247 Value.substr(Pos+1); // Increment the original value pointer as well.
248 // Check for another comma.
249 Pos = Val.find(',');
250 }
251
252 Value = Val;
253 }
254
255 if (Handler->addOccurrence(pos, ArgName, Value, MultiArg))
256 return true;
257
258 return false;
259}
Chris Lattnerb1687372009-09-20 05:03:30 +0000260
Chris Lattner341620b2009-09-20 01:49:31 +0000261/// ProvideOption - For Value, this differentiates between an empty value ("")
262/// and a null value (StringRef()). The later is accepted for arguments that
263/// don't allow a value (-foo) the former is rejected (-foo=).
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000264static inline bool ProvideOption(Option *Handler, StringRef ArgName,
Chris Lattner341620b2009-09-20 01:49:31 +0000265 StringRef Value, int argc, char **argv,
Chris Lattnercaccd762001-10-27 05:54:17 +0000266 int &i) {
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000267 // Is this a multi-argument option?
268 unsigned NumAdditionalVals = Handler->getNumAdditionalVals();
269
Chris Lattnercaccd762001-10-27 05:54:17 +0000270 // Enforce value requirements
271 switch (Handler->getValueExpectedFlag()) {
272 case ValueRequired:
Chris Lattner341620b2009-09-20 01:49:31 +0000273 if (Value.data() == 0) { // No value specified?
Chris Lattnerba112292009-09-20 00:07:40 +0000274 if (i+1 >= argc)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000275 return Handler->error("requires a value!");
Chris Lattnerba112292009-09-20 00:07:40 +0000276 // Steal the next argument, like for '-o filename'
277 Value = argv[++i];
Chris Lattnercaccd762001-10-27 05:54:17 +0000278 }
279 break;
280 case ValueDisallowed:
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000281 if (NumAdditionalVals > 0)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000282 return Handler->error("multi-valued option specified"
Chris Lattnerba112292009-09-20 00:07:40 +0000283 " with ValueDisallowed modifier!");
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000284
Chris Lattner341620b2009-09-20 01:49:31 +0000285 if (Value.data())
Benjamin Kramere6864c12009-08-02 12:13:02 +0000286 return Handler->error("does not allow a value! '" +
Chris Lattnera460beb2009-09-19 18:55:05 +0000287 Twine(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000288 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000289 case ValueOptional:
Reid Spencere1cc1502004-09-01 04:41:28 +0000290 break;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000291
Misha Brukmanf976c852005-04-21 22:55:34 +0000292 default:
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000293 errs() << ProgramName
Bill Wendlinge8156192006-12-07 01:30:32 +0000294 << ": Bad ValueMask flag! CommandLine usage error:"
295 << Handler->getValueExpectedFlag() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000296 llvm_unreachable(0);
Chris Lattnercaccd762001-10-27 05:54:17 +0000297 }
298
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000299 // If this isn't a multi-arg option, just run the handler.
Chris Lattnera460beb2009-09-19 18:55:05 +0000300 if (NumAdditionalVals == 0)
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000301 return CommaSeparateAndAddOccurence(Handler, i, ArgName, Value);
Chris Lattnera460beb2009-09-19 18:55:05 +0000302
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000303 // If it is, run the handle several times.
Chris Lattnera460beb2009-09-19 18:55:05 +0000304 bool MultiArg = false;
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000305
Chris Lattner341620b2009-09-20 01:49:31 +0000306 if (Value.data()) {
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000307 if (CommaSeparateAndAddOccurence(Handler, i, ArgName, Value, MultiArg))
Chris Lattnera460beb2009-09-19 18:55:05 +0000308 return true;
309 --NumAdditionalVals;
310 MultiArg = true;
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000311 }
Chris Lattnera460beb2009-09-19 18:55:05 +0000312
313 while (NumAdditionalVals > 0) {
Chris Lattnera460beb2009-09-19 18:55:05 +0000314 if (i+1 >= argc)
315 return Handler->error("not enough values!");
316 Value = argv[++i];
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000317
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000318 if (CommaSeparateAndAddOccurence(Handler, i, ArgName, Value, MultiArg))
Chris Lattnera460beb2009-09-19 18:55:05 +0000319 return true;
320 MultiArg = true;
321 --NumAdditionalVals;
322 }
323 return false;
Chris Lattnercaccd762001-10-27 05:54:17 +0000324}
325
Chris Lattnerba112292009-09-20 00:07:40 +0000326static bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000327 int Dummy = i;
Chris Lattner341620b2009-09-20 01:49:31 +0000328 return ProvideOption(Handler, Handler->ArgStr, Arg, 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000329}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000330
Chris Lattner331de232002-07-22 02:07:59 +0000331
332// Option predicates...
333static inline bool isGrouping(const Option *O) {
334 return O->getFormattingFlag() == cl::Grouping;
335}
336static inline bool isPrefixedOrGrouping(const Option *O) {
337 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
338}
339
340// getOptionPred - Check to see if there are any options that satisfy the
341// specified predicate with names that are the prefixes in Name. This is
342// checked by progressively stripping characters off of the name, checking to
343// see if there options that satisfy the predicate. If we find one, return it,
344// otherwise return null.
345//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000346static Option *getOptionPred(StringRef Name, size_t &Length,
Chris Lattner9878d6a2007-04-06 21:06:55 +0000347 bool (*Pred)(const Option*),
Chris Lattnerb1687372009-09-20 05:03:30 +0000348 const StringMap<Option*> &OptionsMap) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000349
Chris Lattnerb1687372009-09-20 05:03:30 +0000350 StringMap<Option*>::const_iterator OMI = OptionsMap.find(Name);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000351
Chris Lattnerb1687372009-09-20 05:03:30 +0000352 // Loop while we haven't found an option and Name still has at least two
353 // characters in it (so that the next iteration will not be the empty
354 // string.
355 while (OMI == OptionsMap.end() && Name.size() > 1) {
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000356 Name = Name.substr(0, Name.size()-1); // Chop off the last character.
Chris Lattner9878d6a2007-04-06 21:06:55 +0000357 OMI = OptionsMap.find(Name);
Chris Lattnerb1687372009-09-20 05:03:30 +0000358 }
Chris Lattner331de232002-07-22 02:07:59 +0000359
Chris Lattner9878d6a2007-04-06 21:06:55 +0000360 if (OMI != OptionsMap.end() && Pred(OMI->second)) {
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000361 Length = Name.size();
Chris Lattner9878d6a2007-04-06 21:06:55 +0000362 return OMI->second; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000363 }
364 return 0; // No option found!
365}
366
Chris Lattnerb1687372009-09-20 05:03:30 +0000367/// HandlePrefixedOrGroupedOption - The specified argument string (which started
368/// with at least one '-') does not fully match an available option. Check to
369/// see if this is a prefix or grouped option. If so, split arg into output an
370/// Arg/Value pair and return the Option to parse it with.
371static Option *HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value,
372 bool &ErrorParsing,
373 const StringMap<Option*> &OptionsMap) {
374 if (Arg.size() == 1) return 0;
375
376 // Do the lookup!
377 size_t Length = 0;
378 Option *PGOpt = getOptionPred(Arg, Length, isPrefixedOrGrouping, OptionsMap);
379 if (PGOpt == 0) return 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000380
Chris Lattnerb1687372009-09-20 05:03:30 +0000381 // If the option is a prefixed option, then the value is simply the
382 // rest of the name... so fall through to later processing, by
383 // setting up the argument name flags and value fields.
384 if (PGOpt->getFormattingFlag() == cl::Prefix) {
385 Value = Arg.substr(Length);
386 Arg = Arg.substr(0, Length);
387 assert(OptionsMap.count(Arg) && OptionsMap.find(Arg)->second == PGOpt);
388 return PGOpt;
389 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000390
Chris Lattnerb1687372009-09-20 05:03:30 +0000391 // This must be a grouped option... handle them now. Grouping options can't
392 // have values.
393 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000394
Chris Lattnerb1687372009-09-20 05:03:30 +0000395 do {
396 // Move current arg name out of Arg into OneArgName.
397 StringRef OneArgName = Arg.substr(0, Length);
398 Arg = Arg.substr(Length);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000399
Chris Lattnerb1687372009-09-20 05:03:30 +0000400 // Because ValueRequired is an invalid flag for grouped arguments,
401 // we don't need to pass argc/argv in.
402 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
403 "Option can not be cl::Grouping AND cl::ValueRequired!");
Duncan Sands1fa8b002010-01-09 08:30:33 +0000404 int Dummy = 0;
Chris Lattnerb1687372009-09-20 05:03:30 +0000405 ErrorParsing |= ProvideOption(PGOpt, OneArgName,
406 StringRef(), 0, 0, Dummy);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000407
Chris Lattnerb1687372009-09-20 05:03:30 +0000408 // Get the next grouping option.
409 PGOpt = getOptionPred(Arg, Length, isGrouping, OptionsMap);
410 } while (PGOpt && Length != Arg.size());
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000411
Chris Lattnerb1687372009-09-20 05:03:30 +0000412 // Return the last option with Arg cut down to just the last one.
413 return PGOpt;
414}
415
416
417
Chris Lattner331de232002-07-22 02:07:59 +0000418static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000419 return O->getNumOccurrencesFlag() == cl::Required ||
420 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000421}
422
423static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000424 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
425 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000426}
Chris Lattnercaccd762001-10-27 05:54:17 +0000427
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000428/// ParseCStringVector - Break INPUT up wherever one or more
429/// whitespace characters are found, and store the resulting tokens in
430/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
Chris Lattnerfb2674d2009-09-20 01:11:23 +0000431/// using strdup(), so it is the caller's responsibility to free()
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000432/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000433///
Chris Lattner63e944b2009-09-24 05:38:36 +0000434static void ParseCStringVector(std::vector<char *> &OutputVector,
435 const char *Input) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000436 // Characters which will be treated as token separators:
Chris Lattner63e944b2009-09-24 05:38:36 +0000437 StringRef Delims = " \v\f\t\r\n";
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000438
Chris Lattner63e944b2009-09-24 05:38:36 +0000439 StringRef WorkStr(Input);
440 while (!WorkStr.empty()) {
441 // If the first character is a delimiter, strip them off.
442 if (Delims.find(WorkStr[0]) != StringRef::npos) {
443 size_t Pos = WorkStr.find_first_not_of(Delims);
444 if (Pos == StringRef::npos) Pos = WorkStr.size();
445 WorkStr = WorkStr.substr(Pos);
446 continue;
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000447 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000448
Chris Lattner63e944b2009-09-24 05:38:36 +0000449 // Find position of first delimiter.
450 size_t Pos = WorkStr.find_first_of(Delims);
451 if (Pos == StringRef::npos) Pos = WorkStr.size();
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000452
Chris Lattner63e944b2009-09-24 05:38:36 +0000453 // Everything from 0 to Pos is the next word to copy.
454 char *NewStr = (char*)malloc(Pos+1);
455 memcpy(NewStr, WorkStr.data(), Pos);
456 NewStr[Pos] = 0;
457 OutputVector.push_back(NewStr);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000458
Chris Lattner63e944b2009-09-24 05:38:36 +0000459 WorkStr = WorkStr.substr(Pos);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000460 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000461}
462
463/// ParseEnvironmentOptions - An alternative entry point to the
464/// CommandLine library, which allows you to read the program's name
465/// from the caller (as PROGNAME) and its command-line arguments from
466/// an environment variable (whose name is given in ENVVAR).
467///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000468void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000469 const char *Overview, bool ReadResponseFiles) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000470 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000471 assert(progName && "Program name not specified");
472 assert(envVar && "Environment variable name missing");
Misha Brukmanf976c852005-04-21 22:55:34 +0000473
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000474 // Get the environment variable they want us to parse options out of.
Chris Lattner23288582006-08-27 22:10:29 +0000475 const char *envValue = getenv(envVar);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000476 if (!envValue)
477 return;
478
Brian Gaeke06b06c52003-08-14 22:00:59 +0000479 // Get program's "name", which we wouldn't know without the caller
480 // telling us.
Chris Lattner23288582006-08-27 22:10:29 +0000481 std::vector<char*> newArgv;
482 newArgv.push_back(strdup(progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000483
484 // Parse the value of the environment variable into a "command line"
485 // and hand it off to ParseCommandLineOptions().
Chris Lattner23288582006-08-27 22:10:29 +0000486 ParseCStringVector(newArgv, envValue);
Evan Cheng34cd4a42008-05-05 18:30:58 +0000487 int newArgc = static_cast<int>(newArgv.size());
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000488 ParseCommandLineOptions(newArgc, &newArgv[0], Overview, ReadResponseFiles);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000489
490 // Free all the strdup()ed strings.
Chris Lattner23288582006-08-27 22:10:29 +0000491 for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
492 i != e; ++i)
Chris Lattnerfb2674d2009-09-20 01:11:23 +0000493 free(*i);
Brian Gaeke06b06c52003-08-14 22:00:59 +0000494}
495
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000496
497/// ExpandResponseFiles - Copy the contents of argv into newArgv,
498/// substituting the contents of the response files for the arguments
499/// of type @file.
Chris Lattnerb1687372009-09-20 05:03:30 +0000500static void ExpandResponseFiles(unsigned argc, char** argv,
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000501 std::vector<char*>& newArgv) {
Chris Lattnerb1687372009-09-20 05:03:30 +0000502 for (unsigned i = 1; i != argc; ++i) {
503 char *arg = argv[i];
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000504
505 if (arg[0] == '@') {
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000506 sys::PathWithStatus respFile(++arg);
507
508 // Check that the response file is not empty (mmap'ing empty
509 // files can be problematic).
510 const sys::FileStatus *FileStat = respFile.getFileStatus();
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000511 if (FileStat && FileStat->getSize() != 0) {
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000512
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000513 // If we could open the file, parse its contents, otherwise
514 // pass the @file option verbatim.
Mikhail Glushenkov6c55b1c2009-01-28 03:46:22 +0000515
516 // TODO: we should also support recursive loading of response files,
517 // since this is how gcc behaves. (From their man page: "The file may
518 // itself contain additional @file options; any such options will be
519 // processed recursively.")
520
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000521 // Mmap the response file into memory.
522 OwningPtr<MemoryBuffer> respFilePtr;
523 if (!MemoryBuffer::getFile(respFile.c_str(), respFilePtr)) {
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000524 ParseCStringVector(newArgv, respFilePtr->getBufferStart());
525 continue;
526 }
527 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000528 }
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000529 newArgv.push_back(strdup(arg));
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000530 }
531}
532
Dan Gohman9a526322007-10-09 16:04:57 +0000533void cl::ParseCommandLineOptions(int argc, char **argv,
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000534 const char *Overview, bool ReadResponseFiles) {
Chris Lattner9878d6a2007-04-06 21:06:55 +0000535 // Process all registered options.
Chris Lattner49b301c2009-09-20 06:18:38 +0000536 SmallVector<Option*, 4> PositionalOpts;
537 SmallVector<Option*, 4> SinkOpts;
Benjamin Kramer461c8762009-09-19 10:01:45 +0000538 StringMap<Option*> Opts;
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000539 GetOptionInfo(PositionalOpts, SinkOpts, Opts);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000540
Chris Lattner9878d6a2007-04-06 21:06:55 +0000541 assert((!Opts.empty() || !PositionalOpts.empty()) &&
542 "No options specified!");
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000543
544 // Expand response files.
545 std::vector<char*> newArgv;
546 if (ReadResponseFiles) {
547 newArgv.push_back(strdup(argv[0]));
548 ExpandResponseFiles(argc, argv, newArgv);
549 argv = &newArgv[0];
Evan Cheng34cd4a42008-05-05 18:30:58 +0000550 argc = static_cast<int>(newArgv.size());
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000551 }
552
Chris Lattnerefa3da52006-10-13 00:06:24 +0000553 // Copy the program name into ProgName, making sure not to overflow it.
Michael J. Spencerb3127bb2010-12-18 00:19:10 +0000554 std::string ProgName = sys::path::filename(argv[0]);
Benjamin Kramer12ea66a2010-01-28 18:04:38 +0000555 size_t Len = std::min(ProgName.size(), size_t(79));
556 memcpy(ProgramName, ProgName.data(), Len);
557 ProgramName[Len] = '\0';
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000558
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000559 ProgramOverview = Overview;
560 bool ErrorParsing = false;
561
Chris Lattner331de232002-07-22 02:07:59 +0000562 // Check out the positional arguments to collect information about them.
563 unsigned NumPositionalRequired = 0;
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000564
Chris Lattnerde013242005-08-08 17:25:38 +0000565 // Determine whether or not there are an unlimited number of positionals
566 bool HasUnlimitedPositionals = false;
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000567
Chris Lattner331de232002-07-22 02:07:59 +0000568 Option *ConsumeAfterOpt = 0;
569 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000570 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000571 assert(PositionalOpts.size() > 1 &&
572 "Cannot specify cl::ConsumeAfter without a positional argument!");
573 ConsumeAfterOpt = PositionalOpts[0];
574 }
575
576 // Calculate how many positional values are _required_.
577 bool UnboundedFound = false;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000578 for (size_t i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
Chris Lattner331de232002-07-22 02:07:59 +0000579 i != e; ++i) {
580 Option *Opt = PositionalOpts[i];
581 if (RequiresValue(Opt))
582 ++NumPositionalRequired;
583 else if (ConsumeAfterOpt) {
584 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000585 // unless there is only one positional argument...
586 if (PositionalOpts.size() > 2)
587 ErrorParsing |=
Benjamin Kramere6864c12009-08-02 12:13:02 +0000588 Opt->error("error - this positional option will never be matched, "
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000589 "because it does not Require a value, and a "
590 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000591 } else if (UnboundedFound && !Opt->ArgStr[0]) {
592 // This option does not "require" a value... Make sure this option is
593 // not specified after an option that eats all extra arguments, or this
594 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000595 //
Benjamin Kramere6864c12009-08-02 12:13:02 +0000596 ErrorParsing |= Opt->error("error - option can never match, because "
Chris Lattner331de232002-07-22 02:07:59 +0000597 "another positional argument will match an "
598 "unbounded number of values, and this option"
599 " does not require a value!");
600 }
601 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
602 }
Chris Lattner21e1a792005-08-08 21:57:27 +0000603 HasUnlimitedPositionals = UnboundedFound || ConsumeAfterOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000604 }
605
Reid Spencer1e13fd22004-08-13 19:47:30 +0000606 // PositionalVals - A vector of "positional" arguments we accumulate into
Chris Lattnerba112292009-09-20 00:07:40 +0000607 // the process at the end.
Chris Lattner331de232002-07-22 02:07:59 +0000608 //
Chris Lattnerba112292009-09-20 00:07:40 +0000609 SmallVector<std::pair<StringRef,unsigned>, 4> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000610
Chris Lattner9cf3d472003-07-30 17:34:02 +0000611 // If the program has named positional arguments, and the name has been run
612 // across, keep track of which positional argument was named. Otherwise put
613 // the positional args into the PositionalVals list...
614 Option *ActivePositionalArg = 0;
615
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000616 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000617 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000618 for (int i = 1; i < argc; ++i) {
619 Option *Handler = 0;
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000620 Option *NearestHandler = 0;
Nick Lewycky95d206a2011-05-02 05:24:47 +0000621 std::string NearestHandlerString;
Chris Lattner4e247ec2009-09-20 01:53:12 +0000622 StringRef Value;
Chris Lattner8a7a0582009-09-20 02:02:24 +0000623 StringRef ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000624
Chris Lattner69d6f132007-04-12 00:36:29 +0000625 // If the option list changed, this means that some command line
Chris Lattner159b0a432007-04-11 15:35:18 +0000626 // option has just been registered or deregistered. This can occur in
627 // response to things like -load, etc. If this happens, rescan the options.
Chris Lattner69d6f132007-04-12 00:36:29 +0000628 if (OptionListChanged) {
Chris Lattner159b0a432007-04-11 15:35:18 +0000629 PositionalOpts.clear();
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000630 SinkOpts.clear();
Chris Lattner159b0a432007-04-11 15:35:18 +0000631 Opts.clear();
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000632 GetOptionInfo(PositionalOpts, SinkOpts, Opts);
Chris Lattner69d6f132007-04-12 00:36:29 +0000633 OptionListChanged = false;
Chris Lattner159b0a432007-04-11 15:35:18 +0000634 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000635
Chris Lattner331de232002-07-22 02:07:59 +0000636 // Check to see if this is a positional argument. This argument is
637 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000638 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000639 //
640 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
641 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000642 if (ActivePositionalArg) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000643 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000644 continue; // We are done!
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000645 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000646
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000647 if (!PositionalOpts.empty()) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000648 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000649
650 // All of the positional arguments have been fulfulled, give the rest to
651 // the consume after option... if it's specified...
652 //
Misha Brukmanf976c852005-04-21 22:55:34 +0000653 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000654 ConsumeAfterOpt != 0) {
655 for (++i; i < argc; ++i)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000656 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000657 break; // Handle outside of the argument processing loop...
658 }
659
660 // Delay processing positional arguments until the end...
661 continue;
662 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000663 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
664 !DashDashFound) {
665 DashDashFound = true; // This is the mythical "--"?
666 continue; // Don't try to process it as an argument itself.
667 } else if (ActivePositionalArg &&
668 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
669 // If there is a positional argument eating options, check to see if this
670 // option is another positional argument. If so, treat it as an argument,
671 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000672 ArgName = argv[i]+1;
Chris Lattnerb1687372009-09-20 05:03:30 +0000673 // Eat leading dashes.
674 while (!ArgName.empty() && ArgName[0] == '-')
675 ArgName = ArgName.substr(1);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000676
Chris Lattner9878d6a2007-04-06 21:06:55 +0000677 Handler = LookupOption(ArgName, Value, Opts);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000678 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000679 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000680 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000681 }
682
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000683 } else { // We start with a '-', must be an argument.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000684 ArgName = argv[i]+1;
Chris Lattnerb1687372009-09-20 05:03:30 +0000685 // Eat leading dashes.
686 while (!ArgName.empty() && ArgName[0] == '-')
687 ArgName = ArgName.substr(1);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000688
Chris Lattner9878d6a2007-04-06 21:06:55 +0000689 Handler = LookupOption(ArgName, Value, Opts);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000690
Chris Lattnerbf455c22004-05-06 22:04:31 +0000691 // Check to see if this "option" is really a prefixed or grouped argument.
Chris Lattnerb1687372009-09-20 05:03:30 +0000692 if (Handler == 0)
693 Handler = HandlePrefixedOrGroupedOption(ArgName, Value,
694 ErrorParsing, Opts);
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000695
696 // Otherwise, look for the closest available option to report to the user
697 // in the upcoming error.
698 if (Handler == 0 && SinkOpts.empty())
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000699 NearestHandler = LookupNearestOption(ArgName, Opts,
700 NearestHandlerString);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000701 }
702
703 if (Handler == 0) {
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000704 if (SinkOpts.empty()) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000705 errs() << ProgramName << ": Unknown command line argument '"
Duncan Sands7e7ae5a2010-02-18 14:08:13 +0000706 << argv[i] << "'. Try: '" << argv[0] << " -help'\n";
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000707
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000708 if (NearestHandler) {
709 // If we know a near match, report it as well.
710 errs() << ProgramName << ": Did you mean '-"
711 << NearestHandlerString << "'?\n";
712 }
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000713
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000714 ErrorParsing = true;
715 } else {
Chris Lattner49b301c2009-09-20 06:18:38 +0000716 for (SmallVectorImpl<Option*>::iterator I = SinkOpts.begin(),
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000717 E = SinkOpts.end(); I != E ; ++I)
718 (*I)->addOccurrence(i, "", argv[i]);
719 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000720 continue;
721 }
722
Chris Lattner9cf3d472003-07-30 17:34:02 +0000723 // If this is a named positional argument, just remember that it is the
724 // active one...
725 if (Handler->getFormattingFlag() == cl::Positional)
726 ActivePositionalArg = Handler;
Chris Lattner341620b2009-09-20 01:49:31 +0000727 else
Chris Lattner4e247ec2009-09-20 01:53:12 +0000728 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000729 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000730
Chris Lattner331de232002-07-22 02:07:59 +0000731 // Check and handle positional arguments now...
732 if (NumPositionalRequired > PositionalVals.size()) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000733 errs() << ProgramName
Bill Wendlinge8156192006-12-07 01:30:32 +0000734 << ": Not enough positional command line arguments specified!\n"
735 << "Must specify at least " << NumPositionalRequired
Duncan Sands7e7ae5a2010-02-18 14:08:13 +0000736 << " positional arguments: See: " << argv[0] << " -help\n";
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000737
Chris Lattner331de232002-07-22 02:07:59 +0000738 ErrorParsing = true;
Dan Gohman16e02092010-03-24 19:38:02 +0000739 } else if (!HasUnlimitedPositionals &&
740 PositionalVals.size() > PositionalOpts.size()) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000741 errs() << ProgramName
Bill Wendlinge8156192006-12-07 01:30:32 +0000742 << ": Too many positional arguments specified!\n"
743 << "Can specify at most " << PositionalOpts.size()
Duncan Sands7e7ae5a2010-02-18 14:08:13 +0000744 << " positional arguments: See: " << argv[0] << " -help\n";
Chris Lattnerde013242005-08-08 17:25:38 +0000745 ErrorParsing = true;
Chris Lattner331de232002-07-22 02:07:59 +0000746
747 } else if (ConsumeAfterOpt == 0) {
Chris Lattnerb1687372009-09-20 05:03:30 +0000748 // Positional args have already been handled if ConsumeAfter is specified.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000749 unsigned ValNo = 0, NumVals = static_cast<unsigned>(PositionalVals.size());
750 for (size_t i = 0, e = PositionalOpts.size(); i != e; ++i) {
Chris Lattner331de232002-07-22 02:07:59 +0000751 if (RequiresValue(PositionalOpts[i])) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000752 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000753 PositionalVals[ValNo].second);
754 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000755 --NumPositionalRequired; // We fulfilled our duty...
756 }
757
758 // If we _can_ give this option more arguments, do so now, as long as we
759 // do not give it values that others need. 'Done' controls whether the
760 // option even _WANTS_ any more.
761 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000762 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000763 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000764 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000765 case cl::Optional:
766 Done = true; // Optional arguments want _at most_ one value
767 // FALL THROUGH
768 case cl::ZeroOrMore: // Zero or more will take all they can get...
769 case cl::OneOrMore: // One or more will take all they can get...
Reid Spencer1e13fd22004-08-13 19:47:30 +0000770 ProvidePositionalOption(PositionalOpts[i],
771 PositionalVals[ValNo].first,
772 PositionalVals[ValNo].second);
773 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000774 break;
775 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000776 llvm_unreachable("Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000777 "positional argument processing!");
778 }
779 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000780 }
Chris Lattner331de232002-07-22 02:07:59 +0000781 } else {
782 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
783 unsigned ValNo = 0;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000784 for (size_t j = 1, e = PositionalOpts.size(); j != e; ++j)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000785 if (RequiresValue(PositionalOpts[j])) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000786 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000787 PositionalVals[ValNo].first,
788 PositionalVals[ValNo].second);
789 ValNo++;
790 }
Chris Lattnerfaba8092002-07-24 20:15:13 +0000791
792 // Handle the case where there is just one positional option, and it's
793 // optional. In this case, we want to give JUST THE FIRST option to the
794 // positional option and keep the rest for the consume after. The above
795 // loop would have assigned no values to positional options in this case.
796 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000797 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty()) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000798 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000799 PositionalVals[ValNo].first,
800 PositionalVals[ValNo].second);
801 ValNo++;
802 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000803
Chris Lattner331de232002-07-22 02:07:59 +0000804 // Handle over all of the rest of the arguments to the
805 // cl::ConsumeAfter command line option...
806 for (; ValNo != PositionalVals.size(); ++ValNo)
807 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000808 PositionalVals[ValNo].first,
809 PositionalVals[ValNo].second);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000810 }
811
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000812 // Loop over args and make sure all required args are specified!
Benjamin Kramer461c8762009-09-19 10:01:45 +0000813 for (StringMap<Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000814 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000815 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000816 case Required:
817 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000818 if (I->second->getNumOccurrences() == 0) {
Benjamin Kramere6864c12009-08-02 12:13:02 +0000819 I->second->error("must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000820 ErrorParsing = true;
821 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000822 // Fall through
823 default:
824 break;
825 }
826 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000827
Rafael Espindolaa962b402010-11-19 21:14:29 +0000828 // Now that we know if -debug is specified, we can use it.
829 // Note that if ReadResponseFiles == true, this must be done before the
830 // memory allocated for the expanded command line is free()d below.
831 DEBUG(dbgs() << "Args: ";
832 for (int i = 0; i < argc; ++i)
833 dbgs() << argv[i] << ' ';
834 dbgs() << '\n';
835 );
836
Chris Lattner331de232002-07-22 02:07:59 +0000837 // Free all of the memory allocated to the map. Command line options may only
838 // be processed once!
Chris Lattner90aa8392006-10-04 21:52:35 +0000839 Opts.clear();
Chris Lattner331de232002-07-22 02:07:59 +0000840 PositionalOpts.clear();
Chris Lattner90aa8392006-10-04 21:52:35 +0000841 MoreHelp->clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000842
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000843 // Free the memory allocated by ExpandResponseFiles.
844 if (ReadResponseFiles) {
845 // Free all the strdup()ed strings.
846 for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
847 i != e; ++i)
Chris Lattnerfd40d032009-09-20 07:16:54 +0000848 free(*i);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000849 }
850
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000851 // If we had an error processing our arguments, don't let the program execute
852 if (ErrorParsing) exit(1);
853}
854
855//===----------------------------------------------------------------------===//
856// Option Base class implementation
857//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000858
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000859bool Option::error(const Twine &Message, StringRef ArgName) {
860 if (ArgName.data() == 0) ArgName = ArgStr;
861 if (ArgName.empty())
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000862 errs() << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000863 else
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000864 errs() << ProgramName << ": for the -" << ArgName;
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000865
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000866 errs() << " option: " << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000867 return true;
868}
869
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000870bool Option::addOccurrence(unsigned pos, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000871 StringRef Value, bool MultiArg) {
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000872 if (!MultiArg)
873 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000874
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000875 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000876 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000877 if (NumOccurrences > 1)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000878 return error("may only occur zero or one times!", ArgName);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000879 break;
880 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000881 if (NumOccurrences > 1)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000882 return error("must occur exactly one time!", ArgName);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000883 // Fall through
884 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000885 case ZeroOrMore:
886 case ConsumeAfter: break;
Benjamin Kramere6864c12009-08-02 12:13:02 +0000887 default: return error("bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000888 }
889
Reid Spencer1e13fd22004-08-13 19:47:30 +0000890 return handleOccurrence(pos, ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000891}
892
Chris Lattner331de232002-07-22 02:07:59 +0000893
894// getValueStr - Get the value description string, using "DefaultMsg" if nothing
895// has been specified yet.
896//
897static const char *getValueStr(const Option &O, const char *DefaultMsg) {
898 if (O.ValueStr[0] == 0) return DefaultMsg;
899 return O.ValueStr;
900}
901
902//===----------------------------------------------------------------------===//
903// cl::alias class implementation
904//
905
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000906// Return the width of the option tag for printing...
Evan Cheng34cd4a42008-05-05 18:30:58 +0000907size_t alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000908 return std::strlen(ArgStr)+6;
909}
910
Chris Lattnera0de8432006-04-28 05:36:25 +0000911// Print out the option for the alias.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000912void alias::printOptionInfo(size_t GlobalWidth) const {
913 size_t L = std::strlen(ArgStr);
Evan Chengff276b42011-06-13 20:45:54 +0000914 outs() << " -" << ArgStr;
915 outs().indent(GlobalWidth-L-6) << " - " << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000916}
917
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000918//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000919// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000920//
921
Chris Lattner9b14eb52002-08-07 18:36:37 +0000922// basic_parser implementation
923//
924
925// Return the width of the option tag for printing...
Evan Cheng34cd4a42008-05-05 18:30:58 +0000926size_t basic_parser_impl::getOptionWidth(const Option &O) const {
927 size_t Len = std::strlen(O.ArgStr);
Chris Lattner9b14eb52002-08-07 18:36:37 +0000928 if (const char *ValName = getValueName())
929 Len += std::strlen(getValueStr(O, ValName))+3;
930
931 return Len + 6;
932}
933
Misha Brukmanf976c852005-04-21 22:55:34 +0000934// printOptionInfo - Print out information about this option. The
Chris Lattner9b14eb52002-08-07 18:36:37 +0000935// to-be-maintained width is specified.
936//
937void basic_parser_impl::printOptionInfo(const Option &O,
Evan Cheng34cd4a42008-05-05 18:30:58 +0000938 size_t GlobalWidth) const {
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000939 outs() << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000940
941 if (const char *ValName = getValueName())
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000942 outs() << "=<" << getValueStr(O, ValName) << '>';
Chris Lattner9b14eb52002-08-07 18:36:37 +0000943
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000944 outs().indent(GlobalWidth-getOptionWidth(O)) << " - " << O.HelpStr << '\n';
Chris Lattner9b14eb52002-08-07 18:36:37 +0000945}
946
Andrew Trickce969022011-04-05 18:54:36 +0000947void basic_parser_impl::printOptionName(const Option &O,
948 size_t GlobalWidth) const {
949 outs() << " -" << O.ArgStr;
950 outs().indent(GlobalWidth-std::strlen(O.ArgStr));
951}
Chris Lattner9b14eb52002-08-07 18:36:37 +0000952
953
Chris Lattner331de232002-07-22 02:07:59 +0000954// parser<bool> implementation
955//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000956bool parser<bool>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000957 StringRef Arg, bool &Value) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000958 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000959 Arg == "1") {
960 Value = true;
Chris Lattnera460beb2009-09-19 18:55:05 +0000961 return false;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000962 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000963
Chris Lattnera460beb2009-09-19 18:55:05 +0000964 if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
965 Value = false;
966 return false;
967 }
968 return O.error("'" + Arg +
969 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000970}
971
Dale Johannesen81da02b2007-05-22 17:14:46 +0000972// parser<boolOrDefault> implementation
973//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000974bool parser<boolOrDefault>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000975 StringRef Arg, boolOrDefault &Value) {
Dale Johannesen81da02b2007-05-22 17:14:46 +0000976 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
977 Arg == "1") {
978 Value = BOU_TRUE;
Chris Lattnera460beb2009-09-19 18:55:05 +0000979 return false;
Dale Johannesen81da02b2007-05-22 17:14:46 +0000980 }
Chris Lattnera460beb2009-09-19 18:55:05 +0000981 if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
982 Value = BOU_FALSE;
983 return false;
984 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000985
Chris Lattnera460beb2009-09-19 18:55:05 +0000986 return O.error("'" + Arg +
987 "' is invalid value for boolean argument! Try 0 or 1");
Dale Johannesen81da02b2007-05-22 17:14:46 +0000988}
989
Chris Lattner331de232002-07-22 02:07:59 +0000990// parser<int> implementation
991//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000992bool parser<int>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000993 StringRef Arg, int &Value) {
Chris Lattner970e7df2009-09-19 23:59:02 +0000994 if (Arg.getAsInteger(0, Value))
Benjamin Kramere6864c12009-08-02 12:13:02 +0000995 return O.error("'" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000996 return false;
997}
998
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000999// parser<unsigned> implementation
1000//
Chris Lattner99c5c7b2009-09-20 00:40:49 +00001001bool parser<unsigned>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +00001002 StringRef Arg, unsigned &Value) {
Chris Lattner970e7df2009-09-19 23:59:02 +00001003
1004 if (Arg.getAsInteger(0, Value))
Benjamin Kramere6864c12009-08-02 12:13:02 +00001005 return O.error("'" + Arg + "' value invalid for uint argument!");
Chris Lattnerd2a6fc32003-06-28 15:47:20 +00001006 return false;
1007}
1008
Chris Lattner9b14eb52002-08-07 18:36:37 +00001009// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +00001010//
Chris Lattnera460beb2009-09-19 18:55:05 +00001011static bool parseDouble(Option &O, StringRef Arg, double &Value) {
Chris Lattner970e7df2009-09-19 23:59:02 +00001012 SmallString<32> TmpStr(Arg.begin(), Arg.end());
1013 const char *ArgStart = TmpStr.c_str();
Chris Lattner331de232002-07-22 02:07:59 +00001014 char *End;
1015 Value = strtod(ArgStart, &End);
Misha Brukmanf976c852005-04-21 22:55:34 +00001016 if (*End != 0)
Benjamin Kramere6864c12009-08-02 12:13:02 +00001017 return O.error("'" + Arg + "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +00001018 return false;
1019}
1020
Chris Lattner99c5c7b2009-09-20 00:40:49 +00001021bool parser<double>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +00001022 StringRef Arg, double &Val) {
Chris Lattner9b14eb52002-08-07 18:36:37 +00001023 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +00001024}
1025
Chris Lattner99c5c7b2009-09-20 00:40:49 +00001026bool parser<float>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +00001027 StringRef Arg, float &Val) {
Chris Lattner9b14eb52002-08-07 18:36:37 +00001028 double dVal;
1029 if (parseDouble(O, Arg, dVal))
1030 return true;
1031 Val = (float)dVal;
1032 return false;
Chris Lattner331de232002-07-22 02:07:59 +00001033}
1034
1035
Chris Lattner331de232002-07-22 02:07:59 +00001036
1037// generic_parser_base implementation
1038//
1039
Chris Lattneraa852bb2002-07-23 17:15:12 +00001040// findOption - Return the option number corresponding to the specified
1041// argument string. If the option is not found, getNumOptions() is returned.
1042//
1043unsigned generic_parser_base::findOption(const char *Name) {
Benjamin Kramer461c8762009-09-19 10:01:45 +00001044 unsigned e = getNumOptions();
Chris Lattneraa852bb2002-07-23 17:15:12 +00001045
Benjamin Kramer461c8762009-09-19 10:01:45 +00001046 for (unsigned i = 0; i != e; ++i) {
1047 if (strcmp(getOption(i), Name) == 0)
Chris Lattneraa852bb2002-07-23 17:15:12 +00001048 return i;
Benjamin Kramer461c8762009-09-19 10:01:45 +00001049 }
Chris Lattneraa852bb2002-07-23 17:15:12 +00001050 return e;
1051}
1052
1053
Chris Lattner331de232002-07-22 02:07:59 +00001054// Return the width of the option tag for printing...
Evan Cheng34cd4a42008-05-05 18:30:58 +00001055size_t generic_parser_base::getOptionWidth(const Option &O) const {
Chris Lattner331de232002-07-22 02:07:59 +00001056 if (O.hasArgStr()) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001057 size_t Size = std::strlen(O.ArgStr)+6;
Chris Lattner331de232002-07-22 02:07:59 +00001058 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Evan Cheng34cd4a42008-05-05 18:30:58 +00001059 Size = std::max(Size, std::strlen(getOption(i))+8);
Chris Lattner331de232002-07-22 02:07:59 +00001060 return Size;
1061 } else {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001062 size_t BaseSize = 0;
Chris Lattner331de232002-07-22 02:07:59 +00001063 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Evan Cheng34cd4a42008-05-05 18:30:58 +00001064 BaseSize = std::max(BaseSize, std::strlen(getOption(i))+8);
Chris Lattner331de232002-07-22 02:07:59 +00001065 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001066 }
1067}
1068
Misha Brukmanf976c852005-04-21 22:55:34 +00001069// printOptionInfo - Print out information about this option. The
Chris Lattner331de232002-07-22 02:07:59 +00001070// to-be-maintained width is specified.
1071//
1072void generic_parser_base::printOptionInfo(const Option &O,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001073 size_t GlobalWidth) const {
Chris Lattner331de232002-07-22 02:07:59 +00001074 if (O.hasArgStr()) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001075 size_t L = std::strlen(O.ArgStr);
Chris Lattnerb1687372009-09-20 05:03:30 +00001076 outs() << " -" << O.ArgStr;
1077 outs().indent(GlobalWidth-L-6) << " - " << O.HelpStr << '\n';
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001078
Chris Lattner331de232002-07-22 02:07:59 +00001079 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001080 size_t NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerb1687372009-09-20 05:03:30 +00001081 outs() << " =" << getOption(i);
1082 outs().indent(NumSpaces) << " - " << getDescription(i) << '\n';
Chris Lattner9c9be482002-01-31 00:42:56 +00001083 }
Chris Lattner331de232002-07-22 02:07:59 +00001084 } else {
1085 if (O.HelpStr[0])
Chris Lattnerb1687372009-09-20 05:03:30 +00001086 outs() << " " << O.HelpStr << '\n';
Chris Lattner331de232002-07-22 02:07:59 +00001087 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001088 size_t L = std::strlen(getOption(i));
Chris Lattnerb1687372009-09-20 05:03:30 +00001089 outs() << " -" << getOption(i);
1090 outs().indent(GlobalWidth-L-8) << " - " << getDescription(i) << '\n';
Chris Lattner331de232002-07-22 02:07:59 +00001091 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001092 }
1093}
1094
Andrew Trickce969022011-04-05 18:54:36 +00001095static const size_t MaxOptWidth = 8; // arbitrary spacing for printOptionDiff
1096
1097// printGenericOptionDiff - Print the value of this option and it's default.
1098//
1099// "Generic" options have each value mapped to a name.
1100void generic_parser_base::
1101printGenericOptionDiff(const Option &O, const GenericOptionValue &Value,
1102 const GenericOptionValue &Default,
1103 size_t GlobalWidth) const {
1104 outs() << " -" << O.ArgStr;
1105 outs().indent(GlobalWidth-std::strlen(O.ArgStr));
1106
1107 unsigned NumOpts = getNumOptions();
1108 for (unsigned i = 0; i != NumOpts; ++i) {
1109 if (Value.compare(getOptionValue(i)))
1110 continue;
1111
1112 outs() << "= " << getOption(i);
1113 size_t L = std::strlen(getOption(i));
1114 size_t NumSpaces = MaxOptWidth > L ? MaxOptWidth - L : 0;
1115 outs().indent(NumSpaces) << " (default: ";
1116 for (unsigned j = 0; j != NumOpts; ++j) {
1117 if (Default.compare(getOptionValue(j)))
1118 continue;
1119 outs() << getOption(j);
1120 break;
1121 }
1122 outs() << ")\n";
1123 return;
1124 }
1125 outs() << "= *unknown option value*\n";
1126}
1127
1128// printOptionDiff - Specializations for printing basic value types.
1129//
1130#define PRINT_OPT_DIFF(T) \
1131 void parser<T>:: \
1132 printOptionDiff(const Option &O, T V, OptionValue<T> D, \
1133 size_t GlobalWidth) const { \
1134 printOptionName(O, GlobalWidth); \
1135 std::string Str; \
1136 { \
1137 raw_string_ostream SS(Str); \
1138 SS << V; \
1139 } \
1140 outs() << "= " << Str; \
1141 size_t NumSpaces = MaxOptWidth > Str.size() ? MaxOptWidth - Str.size() : 0;\
1142 outs().indent(NumSpaces) << " (default: "; \
1143 if (D.hasValue()) \
1144 outs() << D.getValue(); \
1145 else \
1146 outs() << "*no default*"; \
1147 outs() << ")\n"; \
1148 } \
1149
Frits van Bommel090771f2011-04-06 12:29:56 +00001150PRINT_OPT_DIFF(bool)
1151PRINT_OPT_DIFF(boolOrDefault)
1152PRINT_OPT_DIFF(int)
1153PRINT_OPT_DIFF(unsigned)
1154PRINT_OPT_DIFF(double)
1155PRINT_OPT_DIFF(float)
1156PRINT_OPT_DIFF(char)
Andrew Trickce969022011-04-05 18:54:36 +00001157
1158void parser<std::string>::
1159printOptionDiff(const Option &O, StringRef V, OptionValue<std::string> D,
1160 size_t GlobalWidth) const {
1161 printOptionName(O, GlobalWidth);
1162 outs() << "= " << V;
1163 size_t NumSpaces = MaxOptWidth > V.size() ? MaxOptWidth - V.size() : 0;
1164 outs().indent(NumSpaces) << " (default: ";
1165 if (D.hasValue())
1166 outs() << D.getValue();
1167 else
1168 outs() << "*no default*";
1169 outs() << ")\n";
1170}
1171
1172// Print a placeholder for options that don't yet support printOptionDiff().
1173void basic_parser_impl::
1174printOptionNoValue(const Option &O, size_t GlobalWidth) const {
1175 printOptionName(O, GlobalWidth);
1176 outs() << "= *cannot print option value*\n";
1177}
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001178
1179//===----------------------------------------------------------------------===//
Duncan Sands7e7ae5a2010-02-18 14:08:13 +00001180// -help and -help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001181//
Reid Spencerad0846b2004-11-14 22:04:00 +00001182
Chris Lattner0fd48b12009-09-20 05:37:24 +00001183static int OptNameCompare(const void *LHS, const void *RHS) {
1184 typedef std::pair<const char *, Option*> pair_ty;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +00001185
Chris Lattner0fd48b12009-09-20 05:37:24 +00001186 return strcmp(((pair_ty*)LHS)->first, ((pair_ty*)RHS)->first);
1187}
1188
Andrew Trickce969022011-04-05 18:54:36 +00001189// Copy Options into a vector so we can sort them as we like.
1190static void
1191sortOpts(StringMap<Option*> &OptMap,
1192 SmallVectorImpl< std::pair<const char *, Option*> > &Opts,
1193 bool ShowHidden) {
1194 SmallPtrSet<Option*, 128> OptionSet; // Duplicate option detection.
1195
1196 for (StringMap<Option*>::iterator I = OptMap.begin(), E = OptMap.end();
1197 I != E; ++I) {
1198 // Ignore really-hidden options.
1199 if (I->second->getOptionHiddenFlag() == ReallyHidden)
1200 continue;
1201
1202 // Unless showhidden is set, ignore hidden flags.
1203 if (I->second->getOptionHiddenFlag() == Hidden && !ShowHidden)
1204 continue;
1205
1206 // If we've already seen this option, don't add it to the list again.
1207 if (!OptionSet.insert(I->second))
1208 continue;
1209
1210 Opts.push_back(std::pair<const char *, Option*>(I->getKey().data(),
1211 I->second));
1212 }
1213
1214 // Sort the options list alphabetically.
1215 qsort(Opts.data(), Opts.size(), sizeof(Opts[0]), OptNameCompare);
1216}
1217
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001218namespace {
1219
Chris Lattner331de232002-07-22 02:07:59 +00001220class HelpPrinter {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001221 size_t MaxArgLen;
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001222 const Option *EmptyArg;
1223 const bool ShowHidden;
1224
Chris Lattner331de232002-07-22 02:07:59 +00001225public:
Dan Gohman950a4c42008-03-25 22:06:05 +00001226 explicit HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
Chris Lattner331de232002-07-22 02:07:59 +00001227 EmptyArg = 0;
1228 }
1229
1230 void operator=(bool Value) {
1231 if (Value == false) return;
1232
Chris Lattner9878d6a2007-04-06 21:06:55 +00001233 // Get all the options.
Chris Lattner49b301c2009-09-20 06:18:38 +00001234 SmallVector<Option*, 4> PositionalOpts;
1235 SmallVector<Option*, 4> SinkOpts;
Benjamin Kramer461c8762009-09-19 10:01:45 +00001236 StringMap<Option*> OptMap;
Anton Korobeynikovd57160d2008-02-20 12:38:07 +00001237 GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +00001238
Chris Lattner0fd48b12009-09-20 05:37:24 +00001239 SmallVector<std::pair<const char *, Option*>, 128> Opts;
Andrew Trickce969022011-04-05 18:54:36 +00001240 sortOpts(OptMap, Opts, ShowHidden);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001241
1242 if (ProgramOverview)
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001243 outs() << "OVERVIEW: " << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001244
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001245 outs() << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +00001246
Chris Lattner90aa8392006-10-04 21:52:35 +00001247 // Print out the positional options.
Chris Lattner331de232002-07-22 02:07:59 +00001248 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +00001249 if (!PositionalOpts.empty() &&
Chris Lattner9878d6a2007-04-06 21:06:55 +00001250 PositionalOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
1251 CAOpt = PositionalOpts[0];
Chris Lattner331de232002-07-22 02:07:59 +00001252
Evan Cheng34cd4a42008-05-05 18:30:58 +00001253 for (size_t i = CAOpt != 0, e = PositionalOpts.size(); i != e; ++i) {
Chris Lattner9878d6a2007-04-06 21:06:55 +00001254 if (PositionalOpts[i]->ArgStr[0])
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001255 outs() << " --" << PositionalOpts[i]->ArgStr;
1256 outs() << " " << PositionalOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +00001257 }
Chris Lattner331de232002-07-22 02:07:59 +00001258
1259 // Print the consume after option info if it exists...
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001260 if (CAOpt) outs() << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +00001261
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001262 outs() << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001263
1264 // Compute the maximum argument length...
1265 MaxArgLen = 0;
Evan Cheng34cd4a42008-05-05 18:30:58 +00001266 for (size_t i = 0, e = Opts.size(); i != e; ++i)
Chris Lattner0fd48b12009-09-20 05:37:24 +00001267 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001268
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001269 outs() << "OPTIONS:\n";
Evan Cheng34cd4a42008-05-05 18:30:58 +00001270 for (size_t i = 0, e = Opts.size(); i != e; ++i)
Chris Lattner0fd48b12009-09-20 05:37:24 +00001271 Opts[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001272
Chris Lattnerc540ebb2004-11-19 17:08:15 +00001273 // Print any extra help the user has declared.
Chris Lattner90aa8392006-10-04 21:52:35 +00001274 for (std::vector<const char *>::iterator I = MoreHelp->begin(),
1275 E = MoreHelp->end(); I != E; ++I)
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001276 outs() << *I;
Chris Lattner90aa8392006-10-04 21:52:35 +00001277 MoreHelp->clear();
Reid Spencerad0846b2004-11-14 22:04:00 +00001278
Reid Spencer9bbba0912004-11-16 06:11:52 +00001279 // Halt the program since help information was printed
Chris Lattner331de232002-07-22 02:07:59 +00001280 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001281 }
1282};
Chris Lattner500d8bf2006-10-12 22:09:17 +00001283} // End anonymous namespace
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001284
Chris Lattner331de232002-07-22 02:07:59 +00001285// Define the two HelpPrinter instances that are used to print out help, or
1286// help-hidden...
1287//
Chris Lattner500d8bf2006-10-12 22:09:17 +00001288static HelpPrinter NormalPrinter(false);
1289static HelpPrinter HiddenPrinter(true);
Chris Lattner331de232002-07-22 02:07:59 +00001290
Chris Lattner500d8bf2006-10-12 22:09:17 +00001291static cl::opt<HelpPrinter, true, parser<bool> >
Duncan Sands7e7ae5a2010-02-18 14:08:13 +00001292HOp("help", cl::desc("Display available options (-help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +00001293 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +00001294
Chris Lattner500d8bf2006-10-12 22:09:17 +00001295static cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +00001296HHOp("help-hidden", cl::desc("Display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +00001297 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001298
Andrew Trickce969022011-04-05 18:54:36 +00001299static cl::opt<bool>
1300PrintOptions("print-options",
1301 cl::desc("Print non-default options after command line parsing"),
1302 cl::Hidden, cl::init(false));
1303
1304static cl::opt<bool>
1305PrintAllOptions("print-all-options",
1306 cl::desc("Print all option values after command line parsing"),
1307 cl::Hidden, cl::init(false));
1308
1309// Print the value of each option.
1310void cl::PrintOptionValues() {
1311 if (!PrintOptions && !PrintAllOptions) return;
1312
1313 // Get all the options.
1314 SmallVector<Option*, 4> PositionalOpts;
1315 SmallVector<Option*, 4> SinkOpts;
1316 StringMap<Option*> OptMap;
1317 GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
1318
1319 SmallVector<std::pair<const char *, Option*>, 128> Opts;
1320 sortOpts(OptMap, Opts, /*ShowHidden*/true);
1321
1322 // Compute the maximum argument length...
1323 size_t MaxArgLen = 0;
1324 for (size_t i = 0, e = Opts.size(); i != e; ++i)
1325 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
1326
1327 for (size_t i = 0, e = Opts.size(); i != e; ++i)
1328 Opts[i].second->printOptionValue(MaxArgLen, PrintAllOptions);
1329}
1330
Chris Lattner500d8bf2006-10-12 22:09:17 +00001331static void (*OverrideVersionPrinter)() = 0;
Reid Spencer515b5b32006-06-05 16:22:56 +00001332
Chris Lattnerbc2d9d32009-09-20 05:53:47 +00001333static int TargetArraySortFn(const void *LHS, const void *RHS) {
1334 typedef std::pair<const char *, const Target*> pair_ty;
1335 return strcmp(((const pair_ty*)LHS)->first, ((const pair_ty*)RHS)->first);
1336}
1337
Chris Lattner500d8bf2006-10-12 22:09:17 +00001338namespace {
Reid Spencer515b5b32006-06-05 16:22:56 +00001339class VersionPrinter {
1340public:
Devang Patelaed293d2007-02-01 01:43:37 +00001341 void print() {
Chris Lattner49b301c2009-09-20 06:18:38 +00001342 raw_ostream &OS = outs();
1343 OS << "Low Level Virtual Machine (http://llvm.org/):\n"
1344 << " " << PACKAGE_NAME << " version " << PACKAGE_VERSION;
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001345#ifdef LLVM_VERSION_INFO
Chris Lattner49b301c2009-09-20 06:18:38 +00001346 OS << LLVM_VERSION_INFO;
Reid Spencer515b5b32006-06-05 16:22:56 +00001347#endif
Chris Lattner49b301c2009-09-20 06:18:38 +00001348 OS << "\n ";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001349#ifndef __OPTIMIZE__
Chris Lattner49b301c2009-09-20 06:18:38 +00001350 OS << "DEBUG build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001351#else
Chris Lattner49b301c2009-09-20 06:18:38 +00001352 OS << "Optimized build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001353#endif
1354#ifndef NDEBUG
Chris Lattner49b301c2009-09-20 06:18:38 +00001355 OS << " with assertions";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001356#endif
Daniel Dunbarba43e072009-11-14 21:36:07 +00001357 std::string CPU = sys::getHostCPUName();
Benjamin Kramer110e7bb2009-11-17 17:57:04 +00001358 if (CPU == "generic") CPU = "(unknown)";
Chris Lattner49b301c2009-09-20 06:18:38 +00001359 OS << ".\n"
Daniel Dunbardd464df2010-05-10 20:11:56 +00001360#if (ENABLE_TIMESTAMPS == 1)
Chris Lattner49b301c2009-09-20 06:18:38 +00001361 << " Built " << __DATE__ << " (" << __TIME__ << ").\n"
Daniel Dunbardd464df2010-05-10 20:11:56 +00001362#endif
Chris Lattner49b301c2009-09-20 06:18:38 +00001363 << " Host: " << sys::getHostTriple() << '\n'
Daniel Dunbarba43e072009-11-14 21:36:07 +00001364 << " Host CPU: " << CPU << '\n'
Chris Lattner49b301c2009-09-20 06:18:38 +00001365 << '\n'
1366 << " Registered Targets:\n";
Daniel Dunbar603bea32009-07-16 02:06:09 +00001367
Chris Lattnerbc2d9d32009-09-20 05:53:47 +00001368 std::vector<std::pair<const char *, const Target*> > Targets;
Benjamin Kramerd227a3f2009-08-23 10:01:13 +00001369 size_t Width = 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +00001370 for (TargetRegistry::iterator it = TargetRegistry::begin(),
Benjamin Kramerd227a3f2009-08-23 10:01:13 +00001371 ie = TargetRegistry::end(); it != ie; ++it) {
1372 Targets.push_back(std::make_pair(it->getName(), &*it));
Chris Lattnerbc2d9d32009-09-20 05:53:47 +00001373 Width = std::max(Width, strlen(Targets.back().first));
Benjamin Kramerd227a3f2009-08-23 10:01:13 +00001374 }
Chris Lattnerbc2d9d32009-09-20 05:53:47 +00001375 if (!Targets.empty())
1376 qsort(&Targets[0], Targets.size(), sizeof(Targets[0]),
1377 TargetArraySortFn);
Daniel Dunbar77454a22009-07-26 05:09:50 +00001378
Benjamin Kramerd227a3f2009-08-23 10:01:13 +00001379 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
Chris Lattner49b301c2009-09-20 06:18:38 +00001380 OS << " " << Targets[i].first;
1381 OS.indent(Width - strlen(Targets[i].first)) << " - "
Chris Lattnerb1687372009-09-20 05:03:30 +00001382 << Targets[i].second->getShortDescription() << '\n';
Benjamin Kramerd227a3f2009-08-23 10:01:13 +00001383 }
1384 if (Targets.empty())
Chris Lattner49b301c2009-09-20 06:18:38 +00001385 OS << " (none)\n";
Devang Patelaed293d2007-02-01 01:43:37 +00001386 }
1387 void operator=(bool OptionWasSpecified) {
Chris Lattner043b8b52009-09-20 05:48:01 +00001388 if (!OptionWasSpecified) return;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +00001389
Chris Lattner043b8b52009-09-20 05:48:01 +00001390 if (OverrideVersionPrinter == 0) {
1391 print();
1392 exit(1);
Reid Spencer515b5b32006-06-05 16:22:56 +00001393 }
Chris Lattner043b8b52009-09-20 05:48:01 +00001394 (*OverrideVersionPrinter)();
1395 exit(1);
Reid Spencer515b5b32006-06-05 16:22:56 +00001396 }
1397};
Chris Lattner500d8bf2006-10-12 22:09:17 +00001398} // End anonymous namespace
Reid Spencer515b5b32006-06-05 16:22:56 +00001399
1400
Reid Spencer69105f32004-08-04 00:36:06 +00001401// Define the --version option that prints out the LLVM version for the tool
Chris Lattner500d8bf2006-10-12 22:09:17 +00001402static VersionPrinter VersionPrinterInstance;
1403
1404static cl::opt<VersionPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +00001405VersOp("version", cl::desc("Display the version of this program"),
Reid Spencer69105f32004-08-04 00:36:06 +00001406 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
1407
Reid Spencer9bbba0912004-11-16 06:11:52 +00001408// Utility function for printing the help message.
1409void cl::PrintHelpMessage() {
Misha Brukmanf976c852005-04-21 22:55:34 +00001410 // This looks weird, but it actually prints the help message. The
Reid Spencer5cc498f2004-11-16 06:50:36 +00001411 // NormalPrinter variable is a HelpPrinter and the help gets printed when
1412 // its operator= is invoked. That's because the "normal" usages of the
Misha Brukmanf976c852005-04-21 22:55:34 +00001413 // help printer is to be assigned true/false depending on whether the
Duncan Sands7e7ae5a2010-02-18 14:08:13 +00001414 // -help option was given or not. Since we're circumventing that we have
1415 // to make it look like -help was given, so we assign true.
Reid Spencer9bbba0912004-11-16 06:11:52 +00001416 NormalPrinter = true;
1417}
Reid Spencer515b5b32006-06-05 16:22:56 +00001418
Devang Patelaed293d2007-02-01 01:43:37 +00001419/// Utility function for printing version number.
1420void cl::PrintVersionMessage() {
1421 VersionPrinterInstance.print();
1422}
1423
Reid Spencer515b5b32006-06-05 16:22:56 +00001424void cl::SetVersionPrinter(void (*func)()) {
1425 OverrideVersionPrinter = func;
1426}