blob: 839ec799d8090346ae9b4fc3656b0bc8e5dc2229 [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"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000026#include "llvm/Support/Host.h"
27#include "llvm/Support/Path.h"
Chris Lattnerca179342009-08-23 18:09:02 +000028#include "llvm/ADT/OwningPtr.h"
Chris Lattner67aead62009-09-20 05:12:14 +000029#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner970e7df2009-09-19 23:59:02 +000030#include "llvm/ADT/SmallString.h"
Chris Lattner67aead62009-09-20 05:12:14 +000031#include "llvm/ADT/StringMap.h"
Chris Lattnera460beb2009-09-19 18:55:05 +000032#include "llvm/ADT/Twine.h"
Chris Lattnerca179342009-08-23 18:09:02 +000033#include "llvm/Config/config.h"
Brian Gaeke2d6a2362003-10-10 17:01:36 +000034#include <cerrno>
Chris Lattnerca179342009-08-23 18:09:02 +000035#include <cstdlib>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000036using namespace llvm;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000037using namespace cl;
38
Chris Lattner7422a762006-08-27 12:45:47 +000039//===----------------------------------------------------------------------===//
40// Template instantiations and anchors.
41//
Douglas Gregorb3587cf2009-11-25 06:04:18 +000042namespace llvm { namespace cl {
Chris Lattner7422a762006-08-27 12:45:47 +000043TEMPLATE_INSTANTIATION(class basic_parser<bool>);
Dale Johannesen81da02b2007-05-22 17:14:46 +000044TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
Chris Lattner7422a762006-08-27 12:45:47 +000045TEMPLATE_INSTANTIATION(class basic_parser<int>);
46TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
47TEMPLATE_INSTANTIATION(class basic_parser<double>);
48TEMPLATE_INSTANTIATION(class basic_parser<float>);
49TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
Bill Wendlingb587f962009-04-29 23:26:16 +000050TEMPLATE_INSTANTIATION(class basic_parser<char>);
Chris Lattner7422a762006-08-27 12:45:47 +000051
52TEMPLATE_INSTANTIATION(class opt<unsigned>);
53TEMPLATE_INSTANTIATION(class opt<int>);
54TEMPLATE_INSTANTIATION(class opt<std::string>);
Bill Wendlingb587f962009-04-29 23:26:16 +000055TEMPLATE_INSTANTIATION(class opt<char>);
Chris Lattner7422a762006-08-27 12:45:47 +000056TEMPLATE_INSTANTIATION(class opt<bool>);
Douglas Gregorb3587cf2009-11-25 06:04:18 +000057} } // end namespace llvm::cl
Chris Lattner7422a762006-08-27 12:45:47 +000058
59void Option::anchor() {}
60void basic_parser_impl::anchor() {}
61void parser<bool>::anchor() {}
Dale Johannesen81da02b2007-05-22 17:14:46 +000062void parser<boolOrDefault>::anchor() {}
Chris Lattner7422a762006-08-27 12:45:47 +000063void parser<int>::anchor() {}
64void parser<unsigned>::anchor() {}
65void parser<double>::anchor() {}
66void parser<float>::anchor() {}
67void parser<std::string>::anchor() {}
Bill Wendlingb587f962009-04-29 23:26:16 +000068void parser<char>::anchor() {}
Chris Lattner7422a762006-08-27 12:45:47 +000069
70//===----------------------------------------------------------------------===//
71
Chris Lattnerefa3da52006-10-13 00:06:24 +000072// Globals for name and overview of program. Program name is not a string to
73// avoid static ctor/dtor issues.
74static char ProgramName[80] = "<premain>";
Reid Spencere1cc1502004-09-01 04:41:28 +000075static const char *ProgramOverview = 0;
76
Chris Lattnerc540ebb2004-11-19 17:08:15 +000077// This collects additional help to be printed.
Chris Lattner90aa8392006-10-04 21:52:35 +000078static ManagedStatic<std::vector<const char*> > MoreHelp;
Chris Lattnerc540ebb2004-11-19 17:08:15 +000079
Chris Lattner90aa8392006-10-04 21:52:35 +000080extrahelp::extrahelp(const char *Help)
Chris Lattnerc540ebb2004-11-19 17:08:15 +000081 : morehelp(Help) {
Chris Lattner90aa8392006-10-04 21:52:35 +000082 MoreHelp->push_back(Help);
Chris Lattnerc540ebb2004-11-19 17:08:15 +000083}
84
Chris Lattner69d6f132007-04-12 00:36:29 +000085static bool OptionListChanged = false;
86
87// MarkOptionsChanged - Internal helper function.
88void cl::MarkOptionsChanged() {
89 OptionListChanged = true;
90}
91
Chris Lattner9878d6a2007-04-06 21:06:55 +000092/// RegisteredOptionList - This is the list of the command line options that
93/// have statically constructed themselves.
94static Option *RegisteredOptionList = 0;
95
96void Option::addArgument() {
97 assert(NextRegistered == 0 && "argument multiply registered!");
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +000098
Chris Lattner9878d6a2007-04-06 21:06:55 +000099 NextRegistered = RegisteredOptionList;
100 RegisteredOptionList = this;
Chris Lattner69d6f132007-04-12 00:36:29 +0000101 MarkOptionsChanged();
Chris Lattner9878d6a2007-04-06 21:06:55 +0000102}
103
Chris Lattner69d6f132007-04-12 00:36:29 +0000104
Chris Lattner331de232002-07-22 02:07:59 +0000105//===----------------------------------------------------------------------===//
Chris Lattner7422a762006-08-27 12:45:47 +0000106// Basic, shared command line option processing machinery.
Chris Lattner331de232002-07-22 02:07:59 +0000107//
108
Chris Lattner9878d6a2007-04-06 21:06:55 +0000109/// GetOptionInfo - Scan the list of registered options, turning them into data
110/// structures that are easier to handle.
Chris Lattner49b301c2009-09-20 06:18:38 +0000111static void GetOptionInfo(SmallVectorImpl<Option*> &PositionalOpts,
112 SmallVectorImpl<Option*> &SinkOpts,
Benjamin Kramer461c8762009-09-19 10:01:45 +0000113 StringMap<Option*> &OptionsMap) {
Chris Lattner1908aea2009-09-20 06:21:43 +0000114 SmallVector<const char*, 16> OptionNames;
Chris Lattneree2b3202007-04-07 05:38:53 +0000115 Option *CAOpt = 0; // The ConsumeAfter option if it exists.
Chris Lattner9878d6a2007-04-06 21:06:55 +0000116 for (Option *O = RegisteredOptionList; O; O = O->getNextRegisteredOption()) {
117 // If this option wants to handle multiple option names, get the full set.
118 // This handles enum options like "-O1 -O2" etc.
119 O->getExtraOptionNames(OptionNames);
120 if (O->ArgStr[0])
121 OptionNames.push_back(O->ArgStr);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000122
Chris Lattner9878d6a2007-04-06 21:06:55 +0000123 // Handle named options.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000124 for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
Chris Lattner9878d6a2007-04-06 21:06:55 +0000125 // Add argument to the argument map!
Benjamin Kramer461c8762009-09-19 10:01:45 +0000126 if (OptionsMap.GetOrCreateValue(OptionNames[i], O).second != O) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000127 errs() << ProgramName << ": CommandLine Error: Argument '"
Matthijs Kooijman33540ad2008-05-30 13:26:11 +0000128 << OptionNames[i] << "' defined more than once!\n";
Chris Lattner9878d6a2007-04-06 21:06:55 +0000129 }
130 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000131
Chris Lattner9878d6a2007-04-06 21:06:55 +0000132 OptionNames.clear();
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000133
Chris Lattner9878d6a2007-04-06 21:06:55 +0000134 // Remember information about positional options.
135 if (O->getFormattingFlag() == cl::Positional)
136 PositionalOpts.push_back(O);
Dan Gohman61e015f2008-02-23 01:55:25 +0000137 else if (O->getMiscFlags() & cl::Sink) // Remember sink options
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000138 SinkOpts.push_back(O);
Chris Lattner9878d6a2007-04-06 21:06:55 +0000139 else if (O->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattneree2b3202007-04-07 05:38:53 +0000140 if (CAOpt)
Chris Lattner9878d6a2007-04-06 21:06:55 +0000141 O->error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattneree2b3202007-04-07 05:38:53 +0000142 CAOpt = O;
Chris Lattner9878d6a2007-04-06 21:06:55 +0000143 }
Chris Lattnere8e258b2002-07-29 20:58:42 +0000144 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000145
Chris Lattneree2b3202007-04-07 05:38:53 +0000146 if (CAOpt)
147 PositionalOpts.push_back(CAOpt);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000148
Chris Lattneree2b3202007-04-07 05:38:53 +0000149 // Make sure that they are in order of registration not backwards.
150 std::reverse(PositionalOpts.begin(), PositionalOpts.end());
Chris Lattnere8e258b2002-07-29 20:58:42 +0000151}
152
Chris Lattner9878d6a2007-04-06 21:06:55 +0000153
Chris Lattneraf035f32007-04-05 21:58:17 +0000154/// LookupOption - Lookup the option specified by the specified option on the
155/// command line. If there is a value specified (after an equal sign) return
Chris Lattnerb1687372009-09-20 05:03:30 +0000156/// that as well. This assumes that leading dashes have already been stripped.
Chris Lattner8a7a0582009-09-20 02:02:24 +0000157static Option *LookupOption(StringRef &Arg, StringRef &Value,
158 const StringMap<Option*> &OptionsMap) {
Chris Lattner8a7a0582009-09-20 02:02:24 +0000159 // Reject all dashes.
160 if (Arg.empty()) return 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000161
Chris Lattner8a7a0582009-09-20 02:02:24 +0000162 size_t EqualPos = Arg.find('=');
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000163
Chris Lattner4e247ec2009-09-20 01:53:12 +0000164 // If we have an equals sign, remember the value.
Chris Lattnerb1687372009-09-20 05:03:30 +0000165 if (EqualPos == StringRef::npos) {
166 // Look up the option.
167 StringMap<Option*>::const_iterator I = OptionsMap.find(Arg);
168 return I != OptionsMap.end() ? I->second : 0;
Chris Lattner8a7a0582009-09-20 02:02:24 +0000169 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000170
Chris Lattnerb1687372009-09-20 05:03:30 +0000171 // If the argument before the = is a valid option name, we match. If not,
172 // return Arg unmolested.
173 StringMap<Option*>::const_iterator I =
174 OptionsMap.find(Arg.substr(0, EqualPos));
175 if (I == OptionsMap.end()) return 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000176
Chris Lattnerb1687372009-09-20 05:03:30 +0000177 Value = Arg.substr(EqualPos+1);
178 Arg = Arg.substr(0, EqualPos);
179 return I->second;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000180}
181
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000182/// LookupNearestOption - Lookup the closest match to the option specified by
183/// the specified option on the command line. If there is a value specified
184/// (after an equal sign) return that as well. This assumes that leading dashes
185/// have already been stripped.
186static Option *LookupNearestOption(StringRef Arg,
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000187 const StringMap<Option*> &OptionsMap,
Nick Lewycky95d206a2011-05-02 05:24:47 +0000188 std::string &NearestString) {
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000189 // Reject all dashes.
190 if (Arg.empty()) return 0;
191
192 // Split on any equal sign.
Nick Lewycky95d206a2011-05-02 05:24:47 +0000193 std::pair<StringRef, StringRef> SplitArg = Arg.split('=');
194 StringRef &LHS = SplitArg.first; // LHS == Arg when no '=' is present.
195 StringRef &RHS = SplitArg.second;
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000196
197 // Find the closest match.
198 Option *Best = 0;
199 unsigned BestDistance = 0;
200 for (StringMap<Option*>::const_iterator it = OptionsMap.begin(),
201 ie = OptionsMap.end(); it != ie; ++it) {
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000202 Option *O = it->second;
203 SmallVector<const char*, 16> OptionNames;
204 O->getExtraOptionNames(OptionNames);
205 if (O->ArgStr[0])
206 OptionNames.push_back(O->ArgStr);
207
Nick Lewycky95d206a2011-05-02 05:24:47 +0000208 bool PermitValue = O->getValueExpectedFlag() != cl::ValueDisallowed;
209 StringRef Flag = PermitValue ? LHS : Arg;
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000210 for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
211 StringRef Name = OptionNames[i];
212 unsigned Distance = StringRef(Name).edit_distance(
Nick Lewycky95d206a2011-05-02 05:24:47 +0000213 Flag, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000214 if (!Best || Distance < BestDistance) {
215 Best = O;
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000216 BestDistance = Distance;
Nick Lewycky95d206a2011-05-02 05:24:47 +0000217 if (RHS.empty() || !PermitValue)
218 NearestString = OptionNames[i];
219 else
220 NearestString = std::string(OptionNames[i]) + "=" + RHS.str();
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000221 }
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000222 }
223 }
224
225 return Best;
226}
227
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000228/// CommaSeparateAndAddOccurence - A wrapper around Handler->addOccurence() that
229/// does special handling of cl::CommaSeparated options.
230static bool CommaSeparateAndAddOccurence(Option *Handler, unsigned pos,
231 StringRef ArgName,
232 StringRef Value, bool MultiArg = false)
233{
234 // Check to see if this option accepts a comma separated list of values. If
235 // it does, we have to split up the value into multiple values.
236 if (Handler->getMiscFlags() & CommaSeparated) {
237 StringRef Val(Value);
238 StringRef::size_type Pos = Val.find(',');
Chris Lattnerb1687372009-09-20 05:03:30 +0000239
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000240 while (Pos != StringRef::npos) {
241 // Process the portion before the comma.
242 if (Handler->addOccurrence(pos, ArgName, Val.substr(0, Pos), MultiArg))
243 return true;
244 // Erase the portion before the comma, AND the comma.
245 Val = Val.substr(Pos+1);
246 Value.substr(Pos+1); // Increment the original value pointer as well.
247 // Check for another comma.
248 Pos = Val.find(',');
249 }
250
251 Value = Val;
252 }
253
254 if (Handler->addOccurrence(pos, ArgName, Value, MultiArg))
255 return true;
256
257 return false;
258}
Chris Lattnerb1687372009-09-20 05:03:30 +0000259
Chris Lattner341620b2009-09-20 01:49:31 +0000260/// ProvideOption - For Value, this differentiates between an empty value ("")
261/// and a null value (StringRef()). The later is accepted for arguments that
262/// don't allow a value (-foo) the former is rejected (-foo=).
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000263static inline bool ProvideOption(Option *Handler, StringRef ArgName,
Chris Lattner341620b2009-09-20 01:49:31 +0000264 StringRef Value, int argc, char **argv,
Chris Lattnercaccd762001-10-27 05:54:17 +0000265 int &i) {
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000266 // Is this a multi-argument option?
267 unsigned NumAdditionalVals = Handler->getNumAdditionalVals();
268
Chris Lattnercaccd762001-10-27 05:54:17 +0000269 // Enforce value requirements
270 switch (Handler->getValueExpectedFlag()) {
271 case ValueRequired:
Chris Lattner341620b2009-09-20 01:49:31 +0000272 if (Value.data() == 0) { // No value specified?
Chris Lattnerba112292009-09-20 00:07:40 +0000273 if (i+1 >= argc)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000274 return Handler->error("requires a value!");
Chris Lattnerba112292009-09-20 00:07:40 +0000275 // Steal the next argument, like for '-o filename'
276 Value = argv[++i];
Chris Lattnercaccd762001-10-27 05:54:17 +0000277 }
278 break;
279 case ValueDisallowed:
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000280 if (NumAdditionalVals > 0)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000281 return Handler->error("multi-valued option specified"
Chris Lattnerba112292009-09-20 00:07:40 +0000282 " with ValueDisallowed modifier!");
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000283
Chris Lattner341620b2009-09-20 01:49:31 +0000284 if (Value.data())
Benjamin Kramere6864c12009-08-02 12:13:02 +0000285 return Handler->error("does not allow a value! '" +
Chris Lattnera460beb2009-09-19 18:55:05 +0000286 Twine(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000287 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000288 case ValueOptional:
Reid Spencere1cc1502004-09-01 04:41:28 +0000289 break;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000290
Misha Brukmanf976c852005-04-21 22:55:34 +0000291 default:
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000292 errs() << ProgramName
Bill Wendlinge8156192006-12-07 01:30:32 +0000293 << ": Bad ValueMask flag! CommandLine usage error:"
294 << Handler->getValueExpectedFlag() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000295 llvm_unreachable(0);
Chris Lattnercaccd762001-10-27 05:54:17 +0000296 }
297
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000298 // If this isn't a multi-arg option, just run the handler.
Chris Lattnera460beb2009-09-19 18:55:05 +0000299 if (NumAdditionalVals == 0)
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000300 return CommaSeparateAndAddOccurence(Handler, i, ArgName, Value);
Chris Lattnera460beb2009-09-19 18:55:05 +0000301
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000302 // If it is, run the handle several times.
Chris Lattnera460beb2009-09-19 18:55:05 +0000303 bool MultiArg = false;
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000304
Chris Lattner341620b2009-09-20 01:49:31 +0000305 if (Value.data()) {
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000306 if (CommaSeparateAndAddOccurence(Handler, i, ArgName, Value, MultiArg))
Chris Lattnera460beb2009-09-19 18:55:05 +0000307 return true;
308 --NumAdditionalVals;
309 MultiArg = true;
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000310 }
Chris Lattnera460beb2009-09-19 18:55:05 +0000311
312 while (NumAdditionalVals > 0) {
Chris Lattnera460beb2009-09-19 18:55:05 +0000313 if (i+1 >= argc)
314 return Handler->error("not enough values!");
315 Value = argv[++i];
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000316
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000317 if (CommaSeparateAndAddOccurence(Handler, i, ArgName, Value, MultiArg))
Chris Lattnera460beb2009-09-19 18:55:05 +0000318 return true;
319 MultiArg = true;
320 --NumAdditionalVals;
321 }
322 return false;
Chris Lattnercaccd762001-10-27 05:54:17 +0000323}
324
Chris Lattnerba112292009-09-20 00:07:40 +0000325static bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000326 int Dummy = i;
Chris Lattner341620b2009-09-20 01:49:31 +0000327 return ProvideOption(Handler, Handler->ArgStr, Arg, 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000328}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000329
Chris Lattner331de232002-07-22 02:07:59 +0000330
331// Option predicates...
332static inline bool isGrouping(const Option *O) {
333 return O->getFormattingFlag() == cl::Grouping;
334}
335static inline bool isPrefixedOrGrouping(const Option *O) {
336 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
337}
338
339// getOptionPred - Check to see if there are any options that satisfy the
340// specified predicate with names that are the prefixes in Name. This is
341// checked by progressively stripping characters off of the name, checking to
342// see if there options that satisfy the predicate. If we find one, return it,
343// otherwise return null.
344//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000345static Option *getOptionPred(StringRef Name, size_t &Length,
Chris Lattner9878d6a2007-04-06 21:06:55 +0000346 bool (*Pred)(const Option*),
Chris Lattnerb1687372009-09-20 05:03:30 +0000347 const StringMap<Option*> &OptionsMap) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000348
Chris Lattnerb1687372009-09-20 05:03:30 +0000349 StringMap<Option*>::const_iterator OMI = OptionsMap.find(Name);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000350
Chris Lattnerb1687372009-09-20 05:03:30 +0000351 // Loop while we haven't found an option and Name still has at least two
352 // characters in it (so that the next iteration will not be the empty
353 // string.
354 while (OMI == OptionsMap.end() && Name.size() > 1) {
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000355 Name = Name.substr(0, Name.size()-1); // Chop off the last character.
Chris Lattner9878d6a2007-04-06 21:06:55 +0000356 OMI = OptionsMap.find(Name);
Chris Lattnerb1687372009-09-20 05:03:30 +0000357 }
Chris Lattner331de232002-07-22 02:07:59 +0000358
Chris Lattner9878d6a2007-04-06 21:06:55 +0000359 if (OMI != OptionsMap.end() && Pred(OMI->second)) {
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000360 Length = Name.size();
Chris Lattner9878d6a2007-04-06 21:06:55 +0000361 return OMI->second; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000362 }
363 return 0; // No option found!
364}
365
Chris Lattnerb1687372009-09-20 05:03:30 +0000366/// HandlePrefixedOrGroupedOption - The specified argument string (which started
367/// with at least one '-') does not fully match an available option. Check to
368/// see if this is a prefix or grouped option. If so, split arg into output an
369/// Arg/Value pair and return the Option to parse it with.
370static Option *HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value,
371 bool &ErrorParsing,
372 const StringMap<Option*> &OptionsMap) {
373 if (Arg.size() == 1) return 0;
374
375 // Do the lookup!
376 size_t Length = 0;
377 Option *PGOpt = getOptionPred(Arg, Length, isPrefixedOrGrouping, OptionsMap);
378 if (PGOpt == 0) return 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000379
Chris Lattnerb1687372009-09-20 05:03:30 +0000380 // If the option is a prefixed option, then the value is simply the
381 // rest of the name... so fall through to later processing, by
382 // setting up the argument name flags and value fields.
383 if (PGOpt->getFormattingFlag() == cl::Prefix) {
384 Value = Arg.substr(Length);
385 Arg = Arg.substr(0, Length);
386 assert(OptionsMap.count(Arg) && OptionsMap.find(Arg)->second == PGOpt);
387 return PGOpt;
388 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000389
Chris Lattnerb1687372009-09-20 05:03:30 +0000390 // This must be a grouped option... handle them now. Grouping options can't
391 // have values.
392 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000393
Chris Lattnerb1687372009-09-20 05:03:30 +0000394 do {
395 // Move current arg name out of Arg into OneArgName.
396 StringRef OneArgName = Arg.substr(0, Length);
397 Arg = Arg.substr(Length);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000398
Chris Lattnerb1687372009-09-20 05:03:30 +0000399 // Because ValueRequired is an invalid flag for grouped arguments,
400 // we don't need to pass argc/argv in.
401 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
402 "Option can not be cl::Grouping AND cl::ValueRequired!");
Duncan Sands1fa8b002010-01-09 08:30:33 +0000403 int Dummy = 0;
Chris Lattnerb1687372009-09-20 05:03:30 +0000404 ErrorParsing |= ProvideOption(PGOpt, OneArgName,
405 StringRef(), 0, 0, Dummy);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000406
Chris Lattnerb1687372009-09-20 05:03:30 +0000407 // Get the next grouping option.
408 PGOpt = getOptionPred(Arg, Length, isGrouping, OptionsMap);
409 } while (PGOpt && Length != Arg.size());
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000410
Chris Lattnerb1687372009-09-20 05:03:30 +0000411 // Return the last option with Arg cut down to just the last one.
412 return PGOpt;
413}
414
415
416
Chris Lattner331de232002-07-22 02:07:59 +0000417static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000418 return O->getNumOccurrencesFlag() == cl::Required ||
419 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000420}
421
422static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000423 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
424 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000425}
Chris Lattnercaccd762001-10-27 05:54:17 +0000426
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000427/// ParseCStringVector - Break INPUT up wherever one or more
428/// whitespace characters are found, and store the resulting tokens in
429/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
Chris Lattnerfb2674d2009-09-20 01:11:23 +0000430/// using strdup(), so it is the caller's responsibility to free()
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000431/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000432///
Chris Lattner63e944b2009-09-24 05:38:36 +0000433static void ParseCStringVector(std::vector<char *> &OutputVector,
434 const char *Input) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000435 // Characters which will be treated as token separators:
Chris Lattner63e944b2009-09-24 05:38:36 +0000436 StringRef Delims = " \v\f\t\r\n";
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000437
Chris Lattner63e944b2009-09-24 05:38:36 +0000438 StringRef WorkStr(Input);
439 while (!WorkStr.empty()) {
440 // If the first character is a delimiter, strip them off.
441 if (Delims.find(WorkStr[0]) != StringRef::npos) {
442 size_t Pos = WorkStr.find_first_not_of(Delims);
443 if (Pos == StringRef::npos) Pos = WorkStr.size();
444 WorkStr = WorkStr.substr(Pos);
445 continue;
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000446 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000447
Chris Lattner63e944b2009-09-24 05:38:36 +0000448 // Find position of first delimiter.
449 size_t Pos = WorkStr.find_first_of(Delims);
450 if (Pos == StringRef::npos) Pos = WorkStr.size();
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000451
Chris Lattner63e944b2009-09-24 05:38:36 +0000452 // Everything from 0 to Pos is the next word to copy.
453 char *NewStr = (char*)malloc(Pos+1);
454 memcpy(NewStr, WorkStr.data(), Pos);
455 NewStr[Pos] = 0;
456 OutputVector.push_back(NewStr);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000457
Chris Lattner63e944b2009-09-24 05:38:36 +0000458 WorkStr = WorkStr.substr(Pos);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000459 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000460}
461
462/// ParseEnvironmentOptions - An alternative entry point to the
463/// CommandLine library, which allows you to read the program's name
464/// from the caller (as PROGNAME) and its command-line arguments from
465/// an environment variable (whose name is given in ENVVAR).
466///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000467void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000468 const char *Overview, bool ReadResponseFiles) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000469 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000470 assert(progName && "Program name not specified");
471 assert(envVar && "Environment variable name missing");
Misha Brukmanf976c852005-04-21 22:55:34 +0000472
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000473 // Get the environment variable they want us to parse options out of.
Chris Lattner23288582006-08-27 22:10:29 +0000474 const char *envValue = getenv(envVar);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000475 if (!envValue)
476 return;
477
Brian Gaeke06b06c52003-08-14 22:00:59 +0000478 // Get program's "name", which we wouldn't know without the caller
479 // telling us.
Chris Lattner23288582006-08-27 22:10:29 +0000480 std::vector<char*> newArgv;
481 newArgv.push_back(strdup(progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000482
483 // Parse the value of the environment variable into a "command line"
484 // and hand it off to ParseCommandLineOptions().
Chris Lattner23288582006-08-27 22:10:29 +0000485 ParseCStringVector(newArgv, envValue);
Evan Cheng34cd4a42008-05-05 18:30:58 +0000486 int newArgc = static_cast<int>(newArgv.size());
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000487 ParseCommandLineOptions(newArgc, &newArgv[0], Overview, ReadResponseFiles);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000488
489 // Free all the strdup()ed strings.
Chris Lattner23288582006-08-27 22:10:29 +0000490 for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
491 i != e; ++i)
Chris Lattnerfb2674d2009-09-20 01:11:23 +0000492 free(*i);
Brian Gaeke06b06c52003-08-14 22:00:59 +0000493}
494
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000495
496/// ExpandResponseFiles - Copy the contents of argv into newArgv,
497/// substituting the contents of the response files for the arguments
498/// of type @file.
Chris Lattnerb1687372009-09-20 05:03:30 +0000499static void ExpandResponseFiles(unsigned argc, char** argv,
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000500 std::vector<char*>& newArgv) {
Chris Lattnerb1687372009-09-20 05:03:30 +0000501 for (unsigned i = 1; i != argc; ++i) {
502 char *arg = argv[i];
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000503
504 if (arg[0] == '@') {
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000505 sys::PathWithStatus respFile(++arg);
506
507 // Check that the response file is not empty (mmap'ing empty
508 // files can be problematic).
509 const sys::FileStatus *FileStat = respFile.getFileStatus();
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000510 if (FileStat && FileStat->getSize() != 0) {
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000511
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000512 // If we could open the file, parse its contents, otherwise
513 // pass the @file option verbatim.
Mikhail Glushenkov6c55b1c2009-01-28 03:46:22 +0000514
515 // TODO: we should also support recursive loading of response files,
516 // since this is how gcc behaves. (From their man page: "The file may
517 // itself contain additional @file options; any such options will be
518 // processed recursively.")
519
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000520 // Mmap the response file into memory.
521 OwningPtr<MemoryBuffer> respFilePtr;
522 if (!MemoryBuffer::getFile(respFile.c_str(), respFilePtr)) {
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000523 ParseCStringVector(newArgv, respFilePtr->getBufferStart());
524 continue;
525 }
526 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000527 }
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000528 newArgv.push_back(strdup(arg));
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000529 }
530}
531
Dan Gohman9a526322007-10-09 16:04:57 +0000532void cl::ParseCommandLineOptions(int argc, char **argv,
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000533 const char *Overview, bool ReadResponseFiles) {
Chris Lattner9878d6a2007-04-06 21:06:55 +0000534 // Process all registered options.
Chris Lattner49b301c2009-09-20 06:18:38 +0000535 SmallVector<Option*, 4> PositionalOpts;
536 SmallVector<Option*, 4> SinkOpts;
Benjamin Kramer461c8762009-09-19 10:01:45 +0000537 StringMap<Option*> Opts;
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000538 GetOptionInfo(PositionalOpts, SinkOpts, Opts);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000539
Chris Lattner9878d6a2007-04-06 21:06:55 +0000540 assert((!Opts.empty() || !PositionalOpts.empty()) &&
541 "No options specified!");
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000542
543 // Expand response files.
544 std::vector<char*> newArgv;
545 if (ReadResponseFiles) {
546 newArgv.push_back(strdup(argv[0]));
547 ExpandResponseFiles(argc, argv, newArgv);
548 argv = &newArgv[0];
Evan Cheng34cd4a42008-05-05 18:30:58 +0000549 argc = static_cast<int>(newArgv.size());
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000550 }
551
Chris Lattnerefa3da52006-10-13 00:06:24 +0000552 // Copy the program name into ProgName, making sure not to overflow it.
Michael J. Spencerb3127bb2010-12-18 00:19:10 +0000553 std::string ProgName = sys::path::filename(argv[0]);
Benjamin Kramer12ea66a2010-01-28 18:04:38 +0000554 size_t Len = std::min(ProgName.size(), size_t(79));
555 memcpy(ProgramName, ProgName.data(), Len);
556 ProgramName[Len] = '\0';
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000557
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000558 ProgramOverview = Overview;
559 bool ErrorParsing = false;
560
Chris Lattner331de232002-07-22 02:07:59 +0000561 // Check out the positional arguments to collect information about them.
562 unsigned NumPositionalRequired = 0;
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000563
Chris Lattnerde013242005-08-08 17:25:38 +0000564 // Determine whether or not there are an unlimited number of positionals
565 bool HasUnlimitedPositionals = false;
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000566
Chris Lattner331de232002-07-22 02:07:59 +0000567 Option *ConsumeAfterOpt = 0;
568 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000569 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000570 assert(PositionalOpts.size() > 1 &&
571 "Cannot specify cl::ConsumeAfter without a positional argument!");
572 ConsumeAfterOpt = PositionalOpts[0];
573 }
574
575 // Calculate how many positional values are _required_.
576 bool UnboundedFound = false;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000577 for (size_t i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
Chris Lattner331de232002-07-22 02:07:59 +0000578 i != e; ++i) {
579 Option *Opt = PositionalOpts[i];
580 if (RequiresValue(Opt))
581 ++NumPositionalRequired;
582 else if (ConsumeAfterOpt) {
583 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000584 // unless there is only one positional argument...
585 if (PositionalOpts.size() > 2)
586 ErrorParsing |=
Benjamin Kramere6864c12009-08-02 12:13:02 +0000587 Opt->error("error - this positional option will never be matched, "
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000588 "because it does not Require a value, and a "
589 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000590 } else if (UnboundedFound && !Opt->ArgStr[0]) {
591 // This option does not "require" a value... Make sure this option is
592 // not specified after an option that eats all extra arguments, or this
593 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000594 //
Benjamin Kramere6864c12009-08-02 12:13:02 +0000595 ErrorParsing |= Opt->error("error - option can never match, because "
Chris Lattner331de232002-07-22 02:07:59 +0000596 "another positional argument will match an "
597 "unbounded number of values, and this option"
598 " does not require a value!");
599 }
600 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
601 }
Chris Lattner21e1a792005-08-08 21:57:27 +0000602 HasUnlimitedPositionals = UnboundedFound || ConsumeAfterOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000603 }
604
Reid Spencer1e13fd22004-08-13 19:47:30 +0000605 // PositionalVals - A vector of "positional" arguments we accumulate into
Chris Lattnerba112292009-09-20 00:07:40 +0000606 // the process at the end.
Chris Lattner331de232002-07-22 02:07:59 +0000607 //
Chris Lattnerba112292009-09-20 00:07:40 +0000608 SmallVector<std::pair<StringRef,unsigned>, 4> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000609
Chris Lattner9cf3d472003-07-30 17:34:02 +0000610 // If the program has named positional arguments, and the name has been run
611 // across, keep track of which positional argument was named. Otherwise put
612 // the positional args into the PositionalVals list...
613 Option *ActivePositionalArg = 0;
614
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000615 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000616 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000617 for (int i = 1; i < argc; ++i) {
618 Option *Handler = 0;
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000619 Option *NearestHandler = 0;
Nick Lewycky95d206a2011-05-02 05:24:47 +0000620 std::string NearestHandlerString;
Chris Lattner4e247ec2009-09-20 01:53:12 +0000621 StringRef Value;
Chris Lattner8a7a0582009-09-20 02:02:24 +0000622 StringRef ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000623
Chris Lattner69d6f132007-04-12 00:36:29 +0000624 // If the option list changed, this means that some command line
Chris Lattner159b0a432007-04-11 15:35:18 +0000625 // option has just been registered or deregistered. This can occur in
626 // response to things like -load, etc. If this happens, rescan the options.
Chris Lattner69d6f132007-04-12 00:36:29 +0000627 if (OptionListChanged) {
Chris Lattner159b0a432007-04-11 15:35:18 +0000628 PositionalOpts.clear();
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000629 SinkOpts.clear();
Chris Lattner159b0a432007-04-11 15:35:18 +0000630 Opts.clear();
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000631 GetOptionInfo(PositionalOpts, SinkOpts, Opts);
Chris Lattner69d6f132007-04-12 00:36:29 +0000632 OptionListChanged = false;
Chris Lattner159b0a432007-04-11 15:35:18 +0000633 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000634
Chris Lattner331de232002-07-22 02:07:59 +0000635 // Check to see if this is a positional argument. This argument is
636 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000637 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000638 //
639 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
640 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000641 if (ActivePositionalArg) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000642 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000643 continue; // We are done!
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000644 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000645
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000646 if (!PositionalOpts.empty()) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000647 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000648
649 // All of the positional arguments have been fulfulled, give the rest to
650 // the consume after option... if it's specified...
651 //
Misha Brukmanf976c852005-04-21 22:55:34 +0000652 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000653 ConsumeAfterOpt != 0) {
654 for (++i; i < argc; ++i)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000655 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000656 break; // Handle outside of the argument processing loop...
657 }
658
659 // Delay processing positional arguments until the end...
660 continue;
661 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000662 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
663 !DashDashFound) {
664 DashDashFound = true; // This is the mythical "--"?
665 continue; // Don't try to process it as an argument itself.
666 } else if (ActivePositionalArg &&
667 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
668 // If there is a positional argument eating options, check to see if this
669 // option is another positional argument. If so, treat it as an argument,
670 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000671 ArgName = argv[i]+1;
Chris Lattnerb1687372009-09-20 05:03:30 +0000672 // Eat leading dashes.
673 while (!ArgName.empty() && ArgName[0] == '-')
674 ArgName = ArgName.substr(1);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000675
Chris Lattner9878d6a2007-04-06 21:06:55 +0000676 Handler = LookupOption(ArgName, Value, Opts);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000677 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000678 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000679 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000680 }
681
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000682 } else { // We start with a '-', must be an argument.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000683 ArgName = argv[i]+1;
Chris Lattnerb1687372009-09-20 05:03:30 +0000684 // Eat leading dashes.
685 while (!ArgName.empty() && ArgName[0] == '-')
686 ArgName = ArgName.substr(1);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000687
Chris Lattner9878d6a2007-04-06 21:06:55 +0000688 Handler = LookupOption(ArgName, Value, Opts);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000689
Chris Lattnerbf455c22004-05-06 22:04:31 +0000690 // Check to see if this "option" is really a prefixed or grouped argument.
Chris Lattnerb1687372009-09-20 05:03:30 +0000691 if (Handler == 0)
692 Handler = HandlePrefixedOrGroupedOption(ArgName, Value,
693 ErrorParsing, Opts);
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000694
695 // Otherwise, look for the closest available option to report to the user
696 // in the upcoming error.
697 if (Handler == 0 && SinkOpts.empty())
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000698 NearestHandler = LookupNearestOption(ArgName, Opts,
699 NearestHandlerString);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000700 }
701
702 if (Handler == 0) {
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000703 if (SinkOpts.empty()) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000704 errs() << ProgramName << ": Unknown command line argument '"
Duncan Sands7e7ae5a2010-02-18 14:08:13 +0000705 << argv[i] << "'. Try: '" << argv[0] << " -help'\n";
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000706
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000707 if (NearestHandler) {
708 // If we know a near match, report it as well.
709 errs() << ProgramName << ": Did you mean '-"
710 << NearestHandlerString << "'?\n";
711 }
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000712
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000713 ErrorParsing = true;
714 } else {
Chris Lattner49b301c2009-09-20 06:18:38 +0000715 for (SmallVectorImpl<Option*>::iterator I = SinkOpts.begin(),
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000716 E = SinkOpts.end(); I != E ; ++I)
717 (*I)->addOccurrence(i, "", argv[i]);
718 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000719 continue;
720 }
721
Chris Lattner9cf3d472003-07-30 17:34:02 +0000722 // If this is a named positional argument, just remember that it is the
723 // active one...
724 if (Handler->getFormattingFlag() == cl::Positional)
725 ActivePositionalArg = Handler;
Chris Lattner341620b2009-09-20 01:49:31 +0000726 else
Chris Lattner4e247ec2009-09-20 01:53:12 +0000727 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000728 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000729
Chris Lattner331de232002-07-22 02:07:59 +0000730 // Check and handle positional arguments now...
731 if (NumPositionalRequired > PositionalVals.size()) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000732 errs() << ProgramName
Bill Wendlinge8156192006-12-07 01:30:32 +0000733 << ": Not enough positional command line arguments specified!\n"
734 << "Must specify at least " << NumPositionalRequired
Duncan Sands7e7ae5a2010-02-18 14:08:13 +0000735 << " positional arguments: See: " << argv[0] << " -help\n";
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000736
Chris Lattner331de232002-07-22 02:07:59 +0000737 ErrorParsing = true;
Dan Gohman16e02092010-03-24 19:38:02 +0000738 } else if (!HasUnlimitedPositionals &&
739 PositionalVals.size() > PositionalOpts.size()) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000740 errs() << ProgramName
Bill Wendlinge8156192006-12-07 01:30:32 +0000741 << ": Too many positional arguments specified!\n"
742 << "Can specify at most " << PositionalOpts.size()
Duncan Sands7e7ae5a2010-02-18 14:08:13 +0000743 << " positional arguments: See: " << argv[0] << " -help\n";
Chris Lattnerde013242005-08-08 17:25:38 +0000744 ErrorParsing = true;
Chris Lattner331de232002-07-22 02:07:59 +0000745
746 } else if (ConsumeAfterOpt == 0) {
Chris Lattnerb1687372009-09-20 05:03:30 +0000747 // Positional args have already been handled if ConsumeAfter is specified.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000748 unsigned ValNo = 0, NumVals = static_cast<unsigned>(PositionalVals.size());
749 for (size_t i = 0, e = PositionalOpts.size(); i != e; ++i) {
Chris Lattner331de232002-07-22 02:07:59 +0000750 if (RequiresValue(PositionalOpts[i])) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000751 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000752 PositionalVals[ValNo].second);
753 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000754 --NumPositionalRequired; // We fulfilled our duty...
755 }
756
757 // If we _can_ give this option more arguments, do so now, as long as we
758 // do not give it values that others need. 'Done' controls whether the
759 // option even _WANTS_ any more.
760 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000761 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000762 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000763 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000764 case cl::Optional:
765 Done = true; // Optional arguments want _at most_ one value
766 // FALL THROUGH
767 case cl::ZeroOrMore: // Zero or more will take all they can get...
768 case cl::OneOrMore: // One or more will take all they can get...
Reid Spencer1e13fd22004-08-13 19:47:30 +0000769 ProvidePositionalOption(PositionalOpts[i],
770 PositionalVals[ValNo].first,
771 PositionalVals[ValNo].second);
772 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000773 break;
774 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000775 llvm_unreachable("Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000776 "positional argument processing!");
777 }
778 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000779 }
Chris Lattner331de232002-07-22 02:07:59 +0000780 } else {
781 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
782 unsigned ValNo = 0;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000783 for (size_t j = 1, e = PositionalOpts.size(); j != e; ++j)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000784 if (RequiresValue(PositionalOpts[j])) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000785 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000786 PositionalVals[ValNo].first,
787 PositionalVals[ValNo].second);
788 ValNo++;
789 }
Chris Lattnerfaba8092002-07-24 20:15:13 +0000790
791 // Handle the case where there is just one positional option, and it's
792 // optional. In this case, we want to give JUST THE FIRST option to the
793 // positional option and keep the rest for the consume after. The above
794 // loop would have assigned no values to positional options in this case.
795 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000796 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty()) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000797 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000798 PositionalVals[ValNo].first,
799 PositionalVals[ValNo].second);
800 ValNo++;
801 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000802
Chris Lattner331de232002-07-22 02:07:59 +0000803 // Handle over all of the rest of the arguments to the
804 // cl::ConsumeAfter command line option...
805 for (; ValNo != PositionalVals.size(); ++ValNo)
806 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000807 PositionalVals[ValNo].first,
808 PositionalVals[ValNo].second);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000809 }
810
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000811 // Loop over args and make sure all required args are specified!
Benjamin Kramer461c8762009-09-19 10:01:45 +0000812 for (StringMap<Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000813 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000814 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000815 case Required:
816 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000817 if (I->second->getNumOccurrences() == 0) {
Benjamin Kramere6864c12009-08-02 12:13:02 +0000818 I->second->error("must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000819 ErrorParsing = true;
820 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000821 // Fall through
822 default:
823 break;
824 }
825 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000826
Rafael Espindolaa962b402010-11-19 21:14:29 +0000827 // Now that we know if -debug is specified, we can use it.
828 // Note that if ReadResponseFiles == true, this must be done before the
829 // memory allocated for the expanded command line is free()d below.
830 DEBUG(dbgs() << "Args: ";
831 for (int i = 0; i < argc; ++i)
832 dbgs() << argv[i] << ' ';
833 dbgs() << '\n';
834 );
835
Chris Lattner331de232002-07-22 02:07:59 +0000836 // Free all of the memory allocated to the map. Command line options may only
837 // be processed once!
Chris Lattner90aa8392006-10-04 21:52:35 +0000838 Opts.clear();
Chris Lattner331de232002-07-22 02:07:59 +0000839 PositionalOpts.clear();
Chris Lattner90aa8392006-10-04 21:52:35 +0000840 MoreHelp->clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000841
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000842 // Free the memory allocated by ExpandResponseFiles.
843 if (ReadResponseFiles) {
844 // Free all the strdup()ed strings.
845 for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
846 i != e; ++i)
Chris Lattnerfd40d032009-09-20 07:16:54 +0000847 free(*i);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000848 }
849
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000850 // If we had an error processing our arguments, don't let the program execute
851 if (ErrorParsing) exit(1);
852}
853
854//===----------------------------------------------------------------------===//
855// Option Base class implementation
856//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000857
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000858bool Option::error(const Twine &Message, StringRef ArgName) {
859 if (ArgName.data() == 0) ArgName = ArgStr;
860 if (ArgName.empty())
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000861 errs() << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000862 else
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000863 errs() << ProgramName << ": for the -" << ArgName;
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000864
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000865 errs() << " option: " << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000866 return true;
867}
868
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000869bool Option::addOccurrence(unsigned pos, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000870 StringRef Value, bool MultiArg) {
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000871 if (!MultiArg)
872 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000873
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000874 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000875 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000876 if (NumOccurrences > 1)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000877 return error("may only occur zero or one times!", ArgName);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000878 break;
879 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000880 if (NumOccurrences > 1)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000881 return error("must occur exactly one time!", ArgName);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000882 // Fall through
883 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000884 case ZeroOrMore:
885 case ConsumeAfter: break;
Benjamin Kramere6864c12009-08-02 12:13:02 +0000886 default: return error("bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000887 }
888
Reid Spencer1e13fd22004-08-13 19:47:30 +0000889 return handleOccurrence(pos, ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000890}
891
Chris Lattner331de232002-07-22 02:07:59 +0000892
893// getValueStr - Get the value description string, using "DefaultMsg" if nothing
894// has been specified yet.
895//
896static const char *getValueStr(const Option &O, const char *DefaultMsg) {
897 if (O.ValueStr[0] == 0) return DefaultMsg;
898 return O.ValueStr;
899}
900
901//===----------------------------------------------------------------------===//
902// cl::alias class implementation
903//
904
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000905// Return the width of the option tag for printing...
Evan Cheng34cd4a42008-05-05 18:30:58 +0000906size_t alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000907 return std::strlen(ArgStr)+6;
908}
909
Chris Lattnera0de8432006-04-28 05:36:25 +0000910// Print out the option for the alias.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000911void alias::printOptionInfo(size_t GlobalWidth) const {
912 size_t L = std::strlen(ArgStr);
Evan Chengff276b42011-06-13 20:45:54 +0000913 outs() << " -" << ArgStr;
914 outs().indent(GlobalWidth-L-6) << " - " << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000915}
916
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000917//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000918// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000919//
920
Chris Lattner9b14eb52002-08-07 18:36:37 +0000921// basic_parser implementation
922//
923
924// Return the width of the option tag for printing...
Evan Cheng34cd4a42008-05-05 18:30:58 +0000925size_t basic_parser_impl::getOptionWidth(const Option &O) const {
926 size_t Len = std::strlen(O.ArgStr);
Chris Lattner9b14eb52002-08-07 18:36:37 +0000927 if (const char *ValName = getValueName())
928 Len += std::strlen(getValueStr(O, ValName))+3;
929
930 return Len + 6;
931}
932
Misha Brukmanf976c852005-04-21 22:55:34 +0000933// printOptionInfo - Print out information about this option. The
Chris Lattner9b14eb52002-08-07 18:36:37 +0000934// to-be-maintained width is specified.
935//
936void basic_parser_impl::printOptionInfo(const Option &O,
Evan Cheng34cd4a42008-05-05 18:30:58 +0000937 size_t GlobalWidth) const {
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000938 outs() << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000939
940 if (const char *ValName = getValueName())
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000941 outs() << "=<" << getValueStr(O, ValName) << '>';
Chris Lattner9b14eb52002-08-07 18:36:37 +0000942
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000943 outs().indent(GlobalWidth-getOptionWidth(O)) << " - " << O.HelpStr << '\n';
Chris Lattner9b14eb52002-08-07 18:36:37 +0000944}
945
Andrew Trickce969022011-04-05 18:54:36 +0000946void basic_parser_impl::printOptionName(const Option &O,
947 size_t GlobalWidth) const {
948 outs() << " -" << O.ArgStr;
949 outs().indent(GlobalWidth-std::strlen(O.ArgStr));
950}
Chris Lattner9b14eb52002-08-07 18:36:37 +0000951
952
Chris Lattner331de232002-07-22 02:07:59 +0000953// parser<bool> implementation
954//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000955bool parser<bool>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000956 StringRef Arg, bool &Value) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000957 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000958 Arg == "1") {
959 Value = true;
Chris Lattnera460beb2009-09-19 18:55:05 +0000960 return false;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000961 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000962
Chris Lattnera460beb2009-09-19 18:55:05 +0000963 if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
964 Value = false;
965 return false;
966 }
967 return O.error("'" + Arg +
968 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000969}
970
Dale Johannesen81da02b2007-05-22 17:14:46 +0000971// parser<boolOrDefault> implementation
972//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000973bool parser<boolOrDefault>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000974 StringRef Arg, boolOrDefault &Value) {
Dale Johannesen81da02b2007-05-22 17:14:46 +0000975 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
976 Arg == "1") {
977 Value = BOU_TRUE;
Chris Lattnera460beb2009-09-19 18:55:05 +0000978 return false;
Dale Johannesen81da02b2007-05-22 17:14:46 +0000979 }
Chris Lattnera460beb2009-09-19 18:55:05 +0000980 if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
981 Value = BOU_FALSE;
982 return false;
983 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000984
Chris Lattnera460beb2009-09-19 18:55:05 +0000985 return O.error("'" + Arg +
986 "' is invalid value for boolean argument! Try 0 or 1");
Dale Johannesen81da02b2007-05-22 17:14:46 +0000987}
988
Chris Lattner331de232002-07-22 02:07:59 +0000989// parser<int> implementation
990//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000991bool parser<int>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000992 StringRef Arg, int &Value) {
Chris Lattner970e7df2009-09-19 23:59:02 +0000993 if (Arg.getAsInteger(0, Value))
Benjamin Kramere6864c12009-08-02 12:13:02 +0000994 return O.error("'" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000995 return false;
996}
997
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000998// parser<unsigned> implementation
999//
Chris Lattner99c5c7b2009-09-20 00:40:49 +00001000bool parser<unsigned>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +00001001 StringRef Arg, unsigned &Value) {
Chris Lattner970e7df2009-09-19 23:59:02 +00001002
1003 if (Arg.getAsInteger(0, Value))
Benjamin Kramere6864c12009-08-02 12:13:02 +00001004 return O.error("'" + Arg + "' value invalid for uint argument!");
Chris Lattnerd2a6fc32003-06-28 15:47:20 +00001005 return false;
1006}
1007
Chris Lattner9b14eb52002-08-07 18:36:37 +00001008// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +00001009//
Chris Lattnera460beb2009-09-19 18:55:05 +00001010static bool parseDouble(Option &O, StringRef Arg, double &Value) {
Chris Lattner970e7df2009-09-19 23:59:02 +00001011 SmallString<32> TmpStr(Arg.begin(), Arg.end());
1012 const char *ArgStart = TmpStr.c_str();
Chris Lattner331de232002-07-22 02:07:59 +00001013 char *End;
1014 Value = strtod(ArgStart, &End);
Misha Brukmanf976c852005-04-21 22:55:34 +00001015 if (*End != 0)
Benjamin Kramere6864c12009-08-02 12:13:02 +00001016 return O.error("'" + Arg + "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +00001017 return false;
1018}
1019
Chris Lattner99c5c7b2009-09-20 00:40:49 +00001020bool parser<double>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +00001021 StringRef Arg, double &Val) {
Chris Lattner9b14eb52002-08-07 18:36:37 +00001022 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +00001023}
1024
Chris Lattner99c5c7b2009-09-20 00:40:49 +00001025bool parser<float>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +00001026 StringRef Arg, float &Val) {
Chris Lattner9b14eb52002-08-07 18:36:37 +00001027 double dVal;
1028 if (parseDouble(O, Arg, dVal))
1029 return true;
1030 Val = (float)dVal;
1031 return false;
Chris Lattner331de232002-07-22 02:07:59 +00001032}
1033
1034
Chris Lattner331de232002-07-22 02:07:59 +00001035
1036// generic_parser_base implementation
1037//
1038
Chris Lattneraa852bb2002-07-23 17:15:12 +00001039// findOption - Return the option number corresponding to the specified
1040// argument string. If the option is not found, getNumOptions() is returned.
1041//
1042unsigned generic_parser_base::findOption(const char *Name) {
Benjamin Kramer461c8762009-09-19 10:01:45 +00001043 unsigned e = getNumOptions();
Chris Lattneraa852bb2002-07-23 17:15:12 +00001044
Benjamin Kramer461c8762009-09-19 10:01:45 +00001045 for (unsigned i = 0; i != e; ++i) {
1046 if (strcmp(getOption(i), Name) == 0)
Chris Lattneraa852bb2002-07-23 17:15:12 +00001047 return i;
Benjamin Kramer461c8762009-09-19 10:01:45 +00001048 }
Chris Lattneraa852bb2002-07-23 17:15:12 +00001049 return e;
1050}
1051
1052
Chris Lattner331de232002-07-22 02:07:59 +00001053// Return the width of the option tag for printing...
Evan Cheng34cd4a42008-05-05 18:30:58 +00001054size_t generic_parser_base::getOptionWidth(const Option &O) const {
Chris Lattner331de232002-07-22 02:07:59 +00001055 if (O.hasArgStr()) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001056 size_t Size = std::strlen(O.ArgStr)+6;
Chris Lattner331de232002-07-22 02:07:59 +00001057 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Evan Cheng34cd4a42008-05-05 18:30:58 +00001058 Size = std::max(Size, std::strlen(getOption(i))+8);
Chris Lattner331de232002-07-22 02:07:59 +00001059 return Size;
1060 } else {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001061 size_t BaseSize = 0;
Chris Lattner331de232002-07-22 02:07:59 +00001062 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Evan Cheng34cd4a42008-05-05 18:30:58 +00001063 BaseSize = std::max(BaseSize, std::strlen(getOption(i))+8);
Chris Lattner331de232002-07-22 02:07:59 +00001064 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001065 }
1066}
1067
Misha Brukmanf976c852005-04-21 22:55:34 +00001068// printOptionInfo - Print out information about this option. The
Chris Lattner331de232002-07-22 02:07:59 +00001069// to-be-maintained width is specified.
1070//
1071void generic_parser_base::printOptionInfo(const Option &O,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001072 size_t GlobalWidth) const {
Chris Lattner331de232002-07-22 02:07:59 +00001073 if (O.hasArgStr()) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001074 size_t L = std::strlen(O.ArgStr);
Chris Lattnerb1687372009-09-20 05:03:30 +00001075 outs() << " -" << O.ArgStr;
1076 outs().indent(GlobalWidth-L-6) << " - " << O.HelpStr << '\n';
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001077
Chris Lattner331de232002-07-22 02:07:59 +00001078 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001079 size_t NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerb1687372009-09-20 05:03:30 +00001080 outs() << " =" << getOption(i);
1081 outs().indent(NumSpaces) << " - " << getDescription(i) << '\n';
Chris Lattner9c9be482002-01-31 00:42:56 +00001082 }
Chris Lattner331de232002-07-22 02:07:59 +00001083 } else {
1084 if (O.HelpStr[0])
Chris Lattnerb1687372009-09-20 05:03:30 +00001085 outs() << " " << O.HelpStr << '\n';
Chris Lattner331de232002-07-22 02:07:59 +00001086 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001087 size_t L = std::strlen(getOption(i));
Chris Lattnerb1687372009-09-20 05:03:30 +00001088 outs() << " -" << getOption(i);
1089 outs().indent(GlobalWidth-L-8) << " - " << getDescription(i) << '\n';
Chris Lattner331de232002-07-22 02:07:59 +00001090 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001091 }
1092}
1093
Andrew Trickce969022011-04-05 18:54:36 +00001094static const size_t MaxOptWidth = 8; // arbitrary spacing for printOptionDiff
1095
1096// printGenericOptionDiff - Print the value of this option and it's default.
1097//
1098// "Generic" options have each value mapped to a name.
1099void generic_parser_base::
1100printGenericOptionDiff(const Option &O, const GenericOptionValue &Value,
1101 const GenericOptionValue &Default,
1102 size_t GlobalWidth) const {
1103 outs() << " -" << O.ArgStr;
1104 outs().indent(GlobalWidth-std::strlen(O.ArgStr));
1105
1106 unsigned NumOpts = getNumOptions();
1107 for (unsigned i = 0; i != NumOpts; ++i) {
1108 if (Value.compare(getOptionValue(i)))
1109 continue;
1110
1111 outs() << "= " << getOption(i);
1112 size_t L = std::strlen(getOption(i));
1113 size_t NumSpaces = MaxOptWidth > L ? MaxOptWidth - L : 0;
1114 outs().indent(NumSpaces) << " (default: ";
1115 for (unsigned j = 0; j != NumOpts; ++j) {
1116 if (Default.compare(getOptionValue(j)))
1117 continue;
1118 outs() << getOption(j);
1119 break;
1120 }
1121 outs() << ")\n";
1122 return;
1123 }
1124 outs() << "= *unknown option value*\n";
1125}
1126
1127// printOptionDiff - Specializations for printing basic value types.
1128//
1129#define PRINT_OPT_DIFF(T) \
1130 void parser<T>:: \
1131 printOptionDiff(const Option &O, T V, OptionValue<T> D, \
1132 size_t GlobalWidth) const { \
1133 printOptionName(O, GlobalWidth); \
1134 std::string Str; \
1135 { \
1136 raw_string_ostream SS(Str); \
1137 SS << V; \
1138 } \
1139 outs() << "= " << Str; \
1140 size_t NumSpaces = MaxOptWidth > Str.size() ? MaxOptWidth - Str.size() : 0;\
1141 outs().indent(NumSpaces) << " (default: "; \
1142 if (D.hasValue()) \
1143 outs() << D.getValue(); \
1144 else \
1145 outs() << "*no default*"; \
1146 outs() << ")\n"; \
1147 } \
1148
Frits van Bommel090771f2011-04-06 12:29:56 +00001149PRINT_OPT_DIFF(bool)
1150PRINT_OPT_DIFF(boolOrDefault)
1151PRINT_OPT_DIFF(int)
1152PRINT_OPT_DIFF(unsigned)
1153PRINT_OPT_DIFF(double)
1154PRINT_OPT_DIFF(float)
1155PRINT_OPT_DIFF(char)
Andrew Trickce969022011-04-05 18:54:36 +00001156
1157void parser<std::string>::
1158printOptionDiff(const Option &O, StringRef V, OptionValue<std::string> D,
1159 size_t GlobalWidth) const {
1160 printOptionName(O, GlobalWidth);
1161 outs() << "= " << V;
1162 size_t NumSpaces = MaxOptWidth > V.size() ? MaxOptWidth - V.size() : 0;
1163 outs().indent(NumSpaces) << " (default: ";
1164 if (D.hasValue())
1165 outs() << D.getValue();
1166 else
1167 outs() << "*no default*";
1168 outs() << ")\n";
1169}
1170
1171// Print a placeholder for options that don't yet support printOptionDiff().
1172void basic_parser_impl::
1173printOptionNoValue(const Option &O, size_t GlobalWidth) const {
1174 printOptionName(O, GlobalWidth);
1175 outs() << "= *cannot print option value*\n";
1176}
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001177
1178//===----------------------------------------------------------------------===//
Duncan Sands7e7ae5a2010-02-18 14:08:13 +00001179// -help and -help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001180//
Reid Spencerad0846b2004-11-14 22:04:00 +00001181
Chris Lattner0fd48b12009-09-20 05:37:24 +00001182static int OptNameCompare(const void *LHS, const void *RHS) {
1183 typedef std::pair<const char *, Option*> pair_ty;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +00001184
Chris Lattner0fd48b12009-09-20 05:37:24 +00001185 return strcmp(((pair_ty*)LHS)->first, ((pair_ty*)RHS)->first);
1186}
1187
Andrew Trickce969022011-04-05 18:54:36 +00001188// Copy Options into a vector so we can sort them as we like.
1189static void
1190sortOpts(StringMap<Option*> &OptMap,
1191 SmallVectorImpl< std::pair<const char *, Option*> > &Opts,
1192 bool ShowHidden) {
1193 SmallPtrSet<Option*, 128> OptionSet; // Duplicate option detection.
1194
1195 for (StringMap<Option*>::iterator I = OptMap.begin(), E = OptMap.end();
1196 I != E; ++I) {
1197 // Ignore really-hidden options.
1198 if (I->second->getOptionHiddenFlag() == ReallyHidden)
1199 continue;
1200
1201 // Unless showhidden is set, ignore hidden flags.
1202 if (I->second->getOptionHiddenFlag() == Hidden && !ShowHidden)
1203 continue;
1204
1205 // If we've already seen this option, don't add it to the list again.
1206 if (!OptionSet.insert(I->second))
1207 continue;
1208
1209 Opts.push_back(std::pair<const char *, Option*>(I->getKey().data(),
1210 I->second));
1211 }
1212
1213 // Sort the options list alphabetically.
1214 qsort(Opts.data(), Opts.size(), sizeof(Opts[0]), OptNameCompare);
1215}
1216
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001217namespace {
1218
Chris Lattner331de232002-07-22 02:07:59 +00001219class HelpPrinter {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001220 size_t MaxArgLen;
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001221 const Option *EmptyArg;
1222 const bool ShowHidden;
1223
Chris Lattner331de232002-07-22 02:07:59 +00001224public:
Dan Gohman950a4c42008-03-25 22:06:05 +00001225 explicit HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
Chris Lattner331de232002-07-22 02:07:59 +00001226 EmptyArg = 0;
1227 }
1228
1229 void operator=(bool Value) {
1230 if (Value == false) return;
1231
Chris Lattner9878d6a2007-04-06 21:06:55 +00001232 // Get all the options.
Chris Lattner49b301c2009-09-20 06:18:38 +00001233 SmallVector<Option*, 4> PositionalOpts;
1234 SmallVector<Option*, 4> SinkOpts;
Benjamin Kramer461c8762009-09-19 10:01:45 +00001235 StringMap<Option*> OptMap;
Anton Korobeynikovd57160d2008-02-20 12:38:07 +00001236 GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +00001237
Chris Lattner0fd48b12009-09-20 05:37:24 +00001238 SmallVector<std::pair<const char *, Option*>, 128> Opts;
Andrew Trickce969022011-04-05 18:54:36 +00001239 sortOpts(OptMap, Opts, ShowHidden);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001240
1241 if (ProgramOverview)
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001242 outs() << "OVERVIEW: " << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001243
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001244 outs() << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +00001245
Chris Lattner90aa8392006-10-04 21:52:35 +00001246 // Print out the positional options.
Chris Lattner331de232002-07-22 02:07:59 +00001247 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +00001248 if (!PositionalOpts.empty() &&
Chris Lattner9878d6a2007-04-06 21:06:55 +00001249 PositionalOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
1250 CAOpt = PositionalOpts[0];
Chris Lattner331de232002-07-22 02:07:59 +00001251
Evan Cheng34cd4a42008-05-05 18:30:58 +00001252 for (size_t i = CAOpt != 0, e = PositionalOpts.size(); i != e; ++i) {
Chris Lattner9878d6a2007-04-06 21:06:55 +00001253 if (PositionalOpts[i]->ArgStr[0])
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001254 outs() << " --" << PositionalOpts[i]->ArgStr;
1255 outs() << " " << PositionalOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +00001256 }
Chris Lattner331de232002-07-22 02:07:59 +00001257
1258 // Print the consume after option info if it exists...
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001259 if (CAOpt) outs() << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +00001260
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001261 outs() << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001262
1263 // Compute the maximum argument length...
1264 MaxArgLen = 0;
Evan Cheng34cd4a42008-05-05 18:30:58 +00001265 for (size_t i = 0, e = Opts.size(); i != e; ++i)
Chris Lattner0fd48b12009-09-20 05:37:24 +00001266 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001267
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001268 outs() << "OPTIONS:\n";
Evan Cheng34cd4a42008-05-05 18:30:58 +00001269 for (size_t i = 0, e = Opts.size(); i != e; ++i)
Chris Lattner0fd48b12009-09-20 05:37:24 +00001270 Opts[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001271
Chris Lattnerc540ebb2004-11-19 17:08:15 +00001272 // Print any extra help the user has declared.
Chris Lattner90aa8392006-10-04 21:52:35 +00001273 for (std::vector<const char *>::iterator I = MoreHelp->begin(),
1274 E = MoreHelp->end(); I != E; ++I)
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001275 outs() << *I;
Chris Lattner90aa8392006-10-04 21:52:35 +00001276 MoreHelp->clear();
Reid Spencerad0846b2004-11-14 22:04:00 +00001277
Reid Spencer9bbba0912004-11-16 06:11:52 +00001278 // Halt the program since help information was printed
Chris Lattner331de232002-07-22 02:07:59 +00001279 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001280 }
1281};
Chris Lattner500d8bf2006-10-12 22:09:17 +00001282} // End anonymous namespace
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001283
Chris Lattner331de232002-07-22 02:07:59 +00001284// Define the two HelpPrinter instances that are used to print out help, or
1285// help-hidden...
1286//
Chris Lattner500d8bf2006-10-12 22:09:17 +00001287static HelpPrinter NormalPrinter(false);
1288static HelpPrinter HiddenPrinter(true);
Chris Lattner331de232002-07-22 02:07:59 +00001289
Chris Lattner500d8bf2006-10-12 22:09:17 +00001290static cl::opt<HelpPrinter, true, parser<bool> >
Duncan Sands7e7ae5a2010-02-18 14:08:13 +00001291HOp("help", cl::desc("Display available options (-help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +00001292 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +00001293
Chris Lattner500d8bf2006-10-12 22:09:17 +00001294static cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +00001295HHOp("help-hidden", cl::desc("Display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +00001296 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001297
Andrew Trickce969022011-04-05 18:54:36 +00001298static cl::opt<bool>
1299PrintOptions("print-options",
1300 cl::desc("Print non-default options after command line parsing"),
1301 cl::Hidden, cl::init(false));
1302
1303static cl::opt<bool>
1304PrintAllOptions("print-all-options",
1305 cl::desc("Print all option values after command line parsing"),
1306 cl::Hidden, cl::init(false));
1307
1308// Print the value of each option.
1309void cl::PrintOptionValues() {
1310 if (!PrintOptions && !PrintAllOptions) return;
1311
1312 // Get all the options.
1313 SmallVector<Option*, 4> PositionalOpts;
1314 SmallVector<Option*, 4> SinkOpts;
1315 StringMap<Option*> OptMap;
1316 GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
1317
1318 SmallVector<std::pair<const char *, Option*>, 128> Opts;
1319 sortOpts(OptMap, Opts, /*ShowHidden*/true);
1320
1321 // Compute the maximum argument length...
1322 size_t MaxArgLen = 0;
1323 for (size_t i = 0, e = Opts.size(); i != e; ++i)
1324 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
1325
1326 for (size_t i = 0, e = Opts.size(); i != e; ++i)
1327 Opts[i].second->printOptionValue(MaxArgLen, PrintAllOptions);
1328}
1329
Chris Lattner500d8bf2006-10-12 22:09:17 +00001330static void (*OverrideVersionPrinter)() = 0;
Reid Spencer515b5b32006-06-05 16:22:56 +00001331
Chandler Carruth6d51d262011-07-22 07:50:40 +00001332static std::vector<void (*)()>* ExtraVersionPrinters = 0;
1333
Chris Lattner500d8bf2006-10-12 22:09:17 +00001334namespace {
Reid Spencer515b5b32006-06-05 16:22:56 +00001335class VersionPrinter {
1336public:
Devang Patelaed293d2007-02-01 01:43:37 +00001337 void print() {
Chris Lattner49b301c2009-09-20 06:18:38 +00001338 raw_ostream &OS = outs();
1339 OS << "Low Level Virtual Machine (http://llvm.org/):\n"
1340 << " " << PACKAGE_NAME << " version " << PACKAGE_VERSION;
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001341#ifdef LLVM_VERSION_INFO
Chris Lattner49b301c2009-09-20 06:18:38 +00001342 OS << LLVM_VERSION_INFO;
Reid Spencer515b5b32006-06-05 16:22:56 +00001343#endif
Chris Lattner49b301c2009-09-20 06:18:38 +00001344 OS << "\n ";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001345#ifndef __OPTIMIZE__
Chris Lattner49b301c2009-09-20 06:18:38 +00001346 OS << "DEBUG build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001347#else
Chris Lattner49b301c2009-09-20 06:18:38 +00001348 OS << "Optimized build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001349#endif
1350#ifndef NDEBUG
Chris Lattner49b301c2009-09-20 06:18:38 +00001351 OS << " with assertions";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001352#endif
Daniel Dunbarba43e072009-11-14 21:36:07 +00001353 std::string CPU = sys::getHostCPUName();
Benjamin Kramer110e7bb2009-11-17 17:57:04 +00001354 if (CPU == "generic") CPU = "(unknown)";
Chris Lattner49b301c2009-09-20 06:18:38 +00001355 OS << ".\n"
Daniel Dunbardd464df2010-05-10 20:11:56 +00001356#if (ENABLE_TIMESTAMPS == 1)
Chris Lattner49b301c2009-09-20 06:18:38 +00001357 << " Built " << __DATE__ << " (" << __TIME__ << ").\n"
Daniel Dunbardd464df2010-05-10 20:11:56 +00001358#endif
Chris Lattner49b301c2009-09-20 06:18:38 +00001359 << " Host: " << sys::getHostTriple() << '\n'
Chandler Carruth40393132011-07-22 07:50:48 +00001360 << " Host CPU: " << CPU << '\n';
Devang Patelaed293d2007-02-01 01:43:37 +00001361 }
1362 void operator=(bool OptionWasSpecified) {
Chris Lattner043b8b52009-09-20 05:48:01 +00001363 if (!OptionWasSpecified) return;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +00001364
Chandler Carruth6d51d262011-07-22 07:50:40 +00001365 if (OverrideVersionPrinter != 0) {
1366 (*OverrideVersionPrinter)();
Chris Lattner043b8b52009-09-20 05:48:01 +00001367 exit(1);
Reid Spencer515b5b32006-06-05 16:22:56 +00001368 }
Chandler Carruth6d51d262011-07-22 07:50:40 +00001369 print();
1370
1371 // Iterate over any registered extra printers and call them to add further
1372 // information.
1373 if (ExtraVersionPrinters != 0) {
Chandler Carruth40393132011-07-22 07:50:48 +00001374 outs() << '\n';
Chandler Carruth6d51d262011-07-22 07:50:40 +00001375 for (std::vector<void (*)()>::iterator I = ExtraVersionPrinters->begin(),
1376 E = ExtraVersionPrinters->end();
1377 I != E; ++I)
1378 (*I)();
1379 }
1380
Chris Lattner043b8b52009-09-20 05:48:01 +00001381 exit(1);
Reid Spencer515b5b32006-06-05 16:22:56 +00001382 }
1383};
Chris Lattner500d8bf2006-10-12 22:09:17 +00001384} // End anonymous namespace
Reid Spencer515b5b32006-06-05 16:22:56 +00001385
1386
Reid Spencer69105f32004-08-04 00:36:06 +00001387// Define the --version option that prints out the LLVM version for the tool
Chris Lattner500d8bf2006-10-12 22:09:17 +00001388static VersionPrinter VersionPrinterInstance;
1389
1390static cl::opt<VersionPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +00001391VersOp("version", cl::desc("Display the version of this program"),
Reid Spencer69105f32004-08-04 00:36:06 +00001392 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
1393
Reid Spencer9bbba0912004-11-16 06:11:52 +00001394// Utility function for printing the help message.
1395void cl::PrintHelpMessage() {
Misha Brukmanf976c852005-04-21 22:55:34 +00001396 // This looks weird, but it actually prints the help message. The
Reid Spencer5cc498f2004-11-16 06:50:36 +00001397 // NormalPrinter variable is a HelpPrinter and the help gets printed when
1398 // its operator= is invoked. That's because the "normal" usages of the
Misha Brukmanf976c852005-04-21 22:55:34 +00001399 // help printer is to be assigned true/false depending on whether the
Duncan Sands7e7ae5a2010-02-18 14:08:13 +00001400 // -help option was given or not. Since we're circumventing that we have
1401 // to make it look like -help was given, so we assign true.
Reid Spencer9bbba0912004-11-16 06:11:52 +00001402 NormalPrinter = true;
1403}
Reid Spencer515b5b32006-06-05 16:22:56 +00001404
Devang Patelaed293d2007-02-01 01:43:37 +00001405/// Utility function for printing version number.
1406void cl::PrintVersionMessage() {
1407 VersionPrinterInstance.print();
1408}
1409
Reid Spencer515b5b32006-06-05 16:22:56 +00001410void cl::SetVersionPrinter(void (*func)()) {
1411 OverrideVersionPrinter = func;
1412}
Chandler Carruth6d51d262011-07-22 07:50:40 +00001413
1414void cl::AddExtraVersionPrinter(void (*func)()) {
1415 if (ExtraVersionPrinters == 0)
1416 ExtraVersionPrinters = new std::vector<void (*)()>;
1417
1418 ExtraVersionPrinters->push_back(func);
1419}