blob: ce9344954bcc3ca1c564c207228916cb0523d309 [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>);
Benjamin Kramerb3514562011-09-15 21:17:37 +000047TEMPLATE_INSTANTIATION(class basic_parser<unsigned long long>);
Chris Lattner7422a762006-08-27 12:45:47 +000048TEMPLATE_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
David Blaikie0becc962011-12-01 08:00:17 +000060void GenericOptionValue::anchor() {}
61void OptionValue<boolOrDefault>::anchor() {}
62void OptionValue<std::string>::anchor() {}
Chris Lattner7422a762006-08-27 12:45:47 +000063void Option::anchor() {}
64void basic_parser_impl::anchor() {}
65void parser<bool>::anchor() {}
Dale Johannesen81da02b2007-05-22 17:14:46 +000066void parser<boolOrDefault>::anchor() {}
Chris Lattner7422a762006-08-27 12:45:47 +000067void parser<int>::anchor() {}
68void parser<unsigned>::anchor() {}
Benjamin Kramerb3514562011-09-15 21:17:37 +000069void parser<unsigned long long>::anchor() {}
Chris Lattner7422a762006-08-27 12:45:47 +000070void parser<double>::anchor() {}
71void parser<float>::anchor() {}
72void parser<std::string>::anchor() {}
Bill Wendlingb587f962009-04-29 23:26:16 +000073void parser<char>::anchor() {}
Chris Lattner7422a762006-08-27 12:45:47 +000074
75//===----------------------------------------------------------------------===//
76
Chris Lattnerefa3da52006-10-13 00:06:24 +000077// Globals for name and overview of program. Program name is not a string to
78// avoid static ctor/dtor issues.
79static char ProgramName[80] = "<premain>";
Reid Spencere1cc1502004-09-01 04:41:28 +000080static const char *ProgramOverview = 0;
81
Chris Lattnerc540ebb2004-11-19 17:08:15 +000082// This collects additional help to be printed.
Chris Lattner90aa8392006-10-04 21:52:35 +000083static ManagedStatic<std::vector<const char*> > MoreHelp;
Chris Lattnerc540ebb2004-11-19 17:08:15 +000084
Chris Lattner90aa8392006-10-04 21:52:35 +000085extrahelp::extrahelp(const char *Help)
Chris Lattnerc540ebb2004-11-19 17:08:15 +000086 : morehelp(Help) {
Chris Lattner90aa8392006-10-04 21:52:35 +000087 MoreHelp->push_back(Help);
Chris Lattnerc540ebb2004-11-19 17:08:15 +000088}
89
Chris Lattner69d6f132007-04-12 00:36:29 +000090static bool OptionListChanged = false;
91
92// MarkOptionsChanged - Internal helper function.
93void cl::MarkOptionsChanged() {
94 OptionListChanged = true;
95}
96
Chris Lattner9878d6a2007-04-06 21:06:55 +000097/// RegisteredOptionList - This is the list of the command line options that
98/// have statically constructed themselves.
99static Option *RegisteredOptionList = 0;
100
101void Option::addArgument() {
102 assert(NextRegistered == 0 && "argument multiply registered!");
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000103
Chris Lattner9878d6a2007-04-06 21:06:55 +0000104 NextRegistered = RegisteredOptionList;
105 RegisteredOptionList = this;
Chris Lattner69d6f132007-04-12 00:36:29 +0000106 MarkOptionsChanged();
Chris Lattner9878d6a2007-04-06 21:06:55 +0000107}
108
Chris Lattner69d6f132007-04-12 00:36:29 +0000109
Chris Lattner331de232002-07-22 02:07:59 +0000110//===----------------------------------------------------------------------===//
Chris Lattner7422a762006-08-27 12:45:47 +0000111// Basic, shared command line option processing machinery.
Chris Lattner331de232002-07-22 02:07:59 +0000112//
113
Chris Lattner9878d6a2007-04-06 21:06:55 +0000114/// GetOptionInfo - Scan the list of registered options, turning them into data
115/// structures that are easier to handle.
Chris Lattner49b301c2009-09-20 06:18:38 +0000116static void GetOptionInfo(SmallVectorImpl<Option*> &PositionalOpts,
117 SmallVectorImpl<Option*> &SinkOpts,
Benjamin Kramer461c8762009-09-19 10:01:45 +0000118 StringMap<Option*> &OptionsMap) {
Chris Lattner1908aea2009-09-20 06:21:43 +0000119 SmallVector<const char*, 16> OptionNames;
Chris Lattneree2b3202007-04-07 05:38:53 +0000120 Option *CAOpt = 0; // The ConsumeAfter option if it exists.
Chris Lattner9878d6a2007-04-06 21:06:55 +0000121 for (Option *O = RegisteredOptionList; O; O = O->getNextRegisteredOption()) {
122 // If this option wants to handle multiple option names, get the full set.
123 // This handles enum options like "-O1 -O2" etc.
124 O->getExtraOptionNames(OptionNames);
125 if (O->ArgStr[0])
126 OptionNames.push_back(O->ArgStr);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000127
Chris Lattner9878d6a2007-04-06 21:06:55 +0000128 // Handle named options.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000129 for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
Chris Lattner9878d6a2007-04-06 21:06:55 +0000130 // Add argument to the argument map!
Benjamin Kramer461c8762009-09-19 10:01:45 +0000131 if (OptionsMap.GetOrCreateValue(OptionNames[i], O).second != O) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000132 errs() << ProgramName << ": CommandLine Error: Argument '"
Matthijs Kooijman33540ad2008-05-30 13:26:11 +0000133 << OptionNames[i] << "' defined more than once!\n";
Chris Lattner9878d6a2007-04-06 21:06:55 +0000134 }
135 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000136
Chris Lattner9878d6a2007-04-06 21:06:55 +0000137 OptionNames.clear();
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000138
Chris Lattner9878d6a2007-04-06 21:06:55 +0000139 // Remember information about positional options.
140 if (O->getFormattingFlag() == cl::Positional)
141 PositionalOpts.push_back(O);
Dan Gohman61e015f2008-02-23 01:55:25 +0000142 else if (O->getMiscFlags() & cl::Sink) // Remember sink options
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000143 SinkOpts.push_back(O);
Chris Lattner9878d6a2007-04-06 21:06:55 +0000144 else if (O->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattneree2b3202007-04-07 05:38:53 +0000145 if (CAOpt)
Chris Lattner9878d6a2007-04-06 21:06:55 +0000146 O->error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattneree2b3202007-04-07 05:38:53 +0000147 CAOpt = O;
Chris Lattner9878d6a2007-04-06 21:06:55 +0000148 }
Chris Lattnere8e258b2002-07-29 20:58:42 +0000149 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000150
Chris Lattneree2b3202007-04-07 05:38:53 +0000151 if (CAOpt)
152 PositionalOpts.push_back(CAOpt);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000153
Chris Lattneree2b3202007-04-07 05:38:53 +0000154 // Make sure that they are in order of registration not backwards.
155 std::reverse(PositionalOpts.begin(), PositionalOpts.end());
Chris Lattnere8e258b2002-07-29 20:58:42 +0000156}
157
Chris Lattner9878d6a2007-04-06 21:06:55 +0000158
Chris Lattneraf035f32007-04-05 21:58:17 +0000159/// LookupOption - Lookup the option specified by the specified option on the
160/// command line. If there is a value specified (after an equal sign) return
Chris Lattnerb1687372009-09-20 05:03:30 +0000161/// that as well. This assumes that leading dashes have already been stripped.
Chris Lattner8a7a0582009-09-20 02:02:24 +0000162static Option *LookupOption(StringRef &Arg, StringRef &Value,
163 const StringMap<Option*> &OptionsMap) {
Chris Lattner8a7a0582009-09-20 02:02:24 +0000164 // Reject all dashes.
165 if (Arg.empty()) return 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000166
Chris Lattner8a7a0582009-09-20 02:02:24 +0000167 size_t EqualPos = Arg.find('=');
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000168
Chris Lattner4e247ec2009-09-20 01:53:12 +0000169 // If we have an equals sign, remember the value.
Chris Lattnerb1687372009-09-20 05:03:30 +0000170 if (EqualPos == StringRef::npos) {
171 // Look up the option.
172 StringMap<Option*>::const_iterator I = OptionsMap.find(Arg);
173 return I != OptionsMap.end() ? I->second : 0;
Chris Lattner8a7a0582009-09-20 02:02:24 +0000174 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000175
Chris Lattnerb1687372009-09-20 05:03:30 +0000176 // If the argument before the = is a valid option name, we match. If not,
177 // return Arg unmolested.
178 StringMap<Option*>::const_iterator I =
179 OptionsMap.find(Arg.substr(0, EqualPos));
180 if (I == OptionsMap.end()) return 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000181
Chris Lattnerb1687372009-09-20 05:03:30 +0000182 Value = Arg.substr(EqualPos+1);
183 Arg = Arg.substr(0, EqualPos);
184 return I->second;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000185}
186
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000187/// LookupNearestOption - Lookup the closest match to the option specified by
188/// the specified option on the command line. If there is a value specified
189/// (after an equal sign) return that as well. This assumes that leading dashes
190/// have already been stripped.
191static Option *LookupNearestOption(StringRef Arg,
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000192 const StringMap<Option*> &OptionsMap,
Nick Lewycky95d206a2011-05-02 05:24:47 +0000193 std::string &NearestString) {
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000194 // Reject all dashes.
195 if (Arg.empty()) return 0;
196
197 // Split on any equal sign.
Nick Lewycky95d206a2011-05-02 05:24:47 +0000198 std::pair<StringRef, StringRef> SplitArg = Arg.split('=');
199 StringRef &LHS = SplitArg.first; // LHS == Arg when no '=' is present.
200 StringRef &RHS = SplitArg.second;
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000201
202 // Find the closest match.
203 Option *Best = 0;
204 unsigned BestDistance = 0;
205 for (StringMap<Option*>::const_iterator it = OptionsMap.begin(),
206 ie = OptionsMap.end(); it != ie; ++it) {
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000207 Option *O = it->second;
208 SmallVector<const char*, 16> OptionNames;
209 O->getExtraOptionNames(OptionNames);
210 if (O->ArgStr[0])
211 OptionNames.push_back(O->ArgStr);
212
Nick Lewycky95d206a2011-05-02 05:24:47 +0000213 bool PermitValue = O->getValueExpectedFlag() != cl::ValueDisallowed;
214 StringRef Flag = PermitValue ? LHS : Arg;
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000215 for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
216 StringRef Name = OptionNames[i];
217 unsigned Distance = StringRef(Name).edit_distance(
Nick Lewycky95d206a2011-05-02 05:24:47 +0000218 Flag, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000219 if (!Best || Distance < BestDistance) {
220 Best = O;
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000221 BestDistance = Distance;
Nick Lewycky95d206a2011-05-02 05:24:47 +0000222 if (RHS.empty() || !PermitValue)
223 NearestString = OptionNames[i];
224 else
225 NearestString = std::string(OptionNames[i]) + "=" + RHS.str();
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000226 }
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000227 }
228 }
229
230 return Best;
231}
232
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000233/// CommaSeparateAndAddOccurence - A wrapper around Handler->addOccurence() that
234/// does special handling of cl::CommaSeparated options.
235static bool CommaSeparateAndAddOccurence(Option *Handler, unsigned pos,
236 StringRef ArgName,
237 StringRef Value, bool MultiArg = false)
238{
239 // Check to see if this option accepts a comma separated list of values. If
240 // it does, we have to split up the value into multiple values.
241 if (Handler->getMiscFlags() & CommaSeparated) {
242 StringRef Val(Value);
243 StringRef::size_type Pos = Val.find(',');
Chris Lattnerb1687372009-09-20 05:03:30 +0000244
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000245 while (Pos != StringRef::npos) {
246 // Process the portion before the comma.
247 if (Handler->addOccurrence(pos, ArgName, Val.substr(0, Pos), MultiArg))
248 return true;
249 // Erase the portion before the comma, AND the comma.
250 Val = Val.substr(Pos+1);
251 Value.substr(Pos+1); // Increment the original value pointer as well.
252 // Check for another comma.
253 Pos = Val.find(',');
254 }
255
256 Value = Val;
257 }
258
259 if (Handler->addOccurrence(pos, ArgName, Value, MultiArg))
260 return true;
261
262 return false;
263}
Chris Lattnerb1687372009-09-20 05:03:30 +0000264
Chris Lattner341620b2009-09-20 01:49:31 +0000265/// ProvideOption - For Value, this differentiates between an empty value ("")
266/// and a null value (StringRef()). The later is accepted for arguments that
267/// don't allow a value (-foo) the former is rejected (-foo=).
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000268static inline bool ProvideOption(Option *Handler, StringRef ArgName,
Chris Lattner341620b2009-09-20 01:49:31 +0000269 StringRef Value, int argc, char **argv,
Chris Lattnercaccd762001-10-27 05:54:17 +0000270 int &i) {
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000271 // Is this a multi-argument option?
272 unsigned NumAdditionalVals = Handler->getNumAdditionalVals();
273
Chris Lattnercaccd762001-10-27 05:54:17 +0000274 // Enforce value requirements
275 switch (Handler->getValueExpectedFlag()) {
276 case ValueRequired:
Chris Lattner341620b2009-09-20 01:49:31 +0000277 if (Value.data() == 0) { // No value specified?
Chris Lattnerba112292009-09-20 00:07:40 +0000278 if (i+1 >= argc)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000279 return Handler->error("requires a value!");
Chris Lattnerba112292009-09-20 00:07:40 +0000280 // Steal the next argument, like for '-o filename'
281 Value = argv[++i];
Chris Lattnercaccd762001-10-27 05:54:17 +0000282 }
283 break;
284 case ValueDisallowed:
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000285 if (NumAdditionalVals > 0)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000286 return Handler->error("multi-valued option specified"
Chris Lattnerba112292009-09-20 00:07:40 +0000287 " with ValueDisallowed modifier!");
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000288
Chris Lattner341620b2009-09-20 01:49:31 +0000289 if (Value.data())
Benjamin Kramere6864c12009-08-02 12:13:02 +0000290 return Handler->error("does not allow a value! '" +
Chris Lattnera460beb2009-09-19 18:55:05 +0000291 Twine(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000292 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000293 case ValueOptional:
Reid Spencere1cc1502004-09-01 04:41:28 +0000294 break;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000295
Misha Brukmanf976c852005-04-21 22:55:34 +0000296 default:
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000297 errs() << ProgramName
Bill Wendlinge8156192006-12-07 01:30:32 +0000298 << ": Bad ValueMask flag! CommandLine usage error:"
299 << Handler->getValueExpectedFlag() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000300 llvm_unreachable(0);
Chris Lattnercaccd762001-10-27 05:54:17 +0000301 }
302
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000303 // If this isn't a multi-arg option, just run the handler.
Chris Lattnera460beb2009-09-19 18:55:05 +0000304 if (NumAdditionalVals == 0)
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000305 return CommaSeparateAndAddOccurence(Handler, i, ArgName, Value);
Chris Lattnera460beb2009-09-19 18:55:05 +0000306
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000307 // If it is, run the handle several times.
Chris Lattnera460beb2009-09-19 18:55:05 +0000308 bool MultiArg = false;
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000309
Chris Lattner341620b2009-09-20 01:49:31 +0000310 if (Value.data()) {
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000311 if (CommaSeparateAndAddOccurence(Handler, i, ArgName, Value, MultiArg))
Chris Lattnera460beb2009-09-19 18:55:05 +0000312 return true;
313 --NumAdditionalVals;
314 MultiArg = true;
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000315 }
Chris Lattnera460beb2009-09-19 18:55:05 +0000316
317 while (NumAdditionalVals > 0) {
Chris Lattnera460beb2009-09-19 18:55:05 +0000318 if (i+1 >= argc)
319 return Handler->error("not enough values!");
320 Value = argv[++i];
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000321
Mikhail Glushenkov37628e02009-11-20 17:23:17 +0000322 if (CommaSeparateAndAddOccurence(Handler, i, ArgName, Value, MultiArg))
Chris Lattnera460beb2009-09-19 18:55:05 +0000323 return true;
324 MultiArg = true;
325 --NumAdditionalVals;
326 }
327 return false;
Chris Lattnercaccd762001-10-27 05:54:17 +0000328}
329
Chris Lattnerba112292009-09-20 00:07:40 +0000330static bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000331 int Dummy = i;
Chris Lattner341620b2009-09-20 01:49:31 +0000332 return ProvideOption(Handler, Handler->ArgStr, Arg, 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000333}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000334
Chris Lattner331de232002-07-22 02:07:59 +0000335
336// Option predicates...
337static inline bool isGrouping(const Option *O) {
338 return O->getFormattingFlag() == cl::Grouping;
339}
340static inline bool isPrefixedOrGrouping(const Option *O) {
341 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
342}
343
344// getOptionPred - Check to see if there are any options that satisfy the
345// specified predicate with names that are the prefixes in Name. This is
346// checked by progressively stripping characters off of the name, checking to
347// see if there options that satisfy the predicate. If we find one, return it,
348// otherwise return null.
349//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000350static Option *getOptionPred(StringRef Name, size_t &Length,
Chris Lattner9878d6a2007-04-06 21:06:55 +0000351 bool (*Pred)(const Option*),
Chris Lattnerb1687372009-09-20 05:03:30 +0000352 const StringMap<Option*> &OptionsMap) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000353
Chris Lattnerb1687372009-09-20 05:03:30 +0000354 StringMap<Option*>::const_iterator OMI = OptionsMap.find(Name);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000355
Chris Lattnerb1687372009-09-20 05:03:30 +0000356 // Loop while we haven't found an option and Name still has at least two
357 // characters in it (so that the next iteration will not be the empty
358 // string.
359 while (OMI == OptionsMap.end() && Name.size() > 1) {
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000360 Name = Name.substr(0, Name.size()-1); // Chop off the last character.
Chris Lattner9878d6a2007-04-06 21:06:55 +0000361 OMI = OptionsMap.find(Name);
Chris Lattnerb1687372009-09-20 05:03:30 +0000362 }
Chris Lattner331de232002-07-22 02:07:59 +0000363
Chris Lattner9878d6a2007-04-06 21:06:55 +0000364 if (OMI != OptionsMap.end() && Pred(OMI->second)) {
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000365 Length = Name.size();
Chris Lattner9878d6a2007-04-06 21:06:55 +0000366 return OMI->second; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000367 }
368 return 0; // No option found!
369}
370
Chris Lattnerb1687372009-09-20 05:03:30 +0000371/// HandlePrefixedOrGroupedOption - The specified argument string (which started
372/// with at least one '-') does not fully match an available option. Check to
373/// see if this is a prefix or grouped option. If so, split arg into output an
374/// Arg/Value pair and return the Option to parse it with.
375static Option *HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value,
376 bool &ErrorParsing,
377 const StringMap<Option*> &OptionsMap) {
378 if (Arg.size() == 1) return 0;
379
380 // Do the lookup!
381 size_t Length = 0;
382 Option *PGOpt = getOptionPred(Arg, Length, isPrefixedOrGrouping, OptionsMap);
383 if (PGOpt == 0) return 0;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000384
Chris Lattnerb1687372009-09-20 05:03:30 +0000385 // If the option is a prefixed option, then the value is simply the
386 // rest of the name... so fall through to later processing, by
387 // setting up the argument name flags and value fields.
388 if (PGOpt->getFormattingFlag() == cl::Prefix) {
389 Value = Arg.substr(Length);
390 Arg = Arg.substr(0, Length);
391 assert(OptionsMap.count(Arg) && OptionsMap.find(Arg)->second == PGOpt);
392 return PGOpt;
393 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000394
Chris Lattnerb1687372009-09-20 05:03:30 +0000395 // This must be a grouped option... handle them now. Grouping options can't
396 // have values.
397 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000398
Chris Lattnerb1687372009-09-20 05:03:30 +0000399 do {
400 // Move current arg name out of Arg into OneArgName.
401 StringRef OneArgName = Arg.substr(0, Length);
402 Arg = Arg.substr(Length);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000403
Chris Lattnerb1687372009-09-20 05:03:30 +0000404 // Because ValueRequired is an invalid flag for grouped arguments,
405 // we don't need to pass argc/argv in.
406 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
407 "Option can not be cl::Grouping AND cl::ValueRequired!");
Duncan Sands1fa8b002010-01-09 08:30:33 +0000408 int Dummy = 0;
Chris Lattnerb1687372009-09-20 05:03:30 +0000409 ErrorParsing |= ProvideOption(PGOpt, OneArgName,
410 StringRef(), 0, 0, Dummy);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000411
Chris Lattnerb1687372009-09-20 05:03:30 +0000412 // Get the next grouping option.
413 PGOpt = getOptionPred(Arg, Length, isGrouping, OptionsMap);
414 } while (PGOpt && Length != Arg.size());
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000415
Chris Lattnerb1687372009-09-20 05:03:30 +0000416 // Return the last option with Arg cut down to just the last one.
417 return PGOpt;
418}
419
420
421
Chris Lattner331de232002-07-22 02:07:59 +0000422static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000423 return O->getNumOccurrencesFlag() == cl::Required ||
424 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000425}
426
427static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000428 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
429 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000430}
Chris Lattnercaccd762001-10-27 05:54:17 +0000431
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000432/// ParseCStringVector - Break INPUT up wherever one or more
433/// whitespace characters are found, and store the resulting tokens in
434/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
Chris Lattnerfb2674d2009-09-20 01:11:23 +0000435/// using strdup(), so it is the caller's responsibility to free()
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000436/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000437///
Chris Lattner63e944b2009-09-24 05:38:36 +0000438static void ParseCStringVector(std::vector<char *> &OutputVector,
439 const char *Input) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000440 // Characters which will be treated as token separators:
Chris Lattner63e944b2009-09-24 05:38:36 +0000441 StringRef Delims = " \v\f\t\r\n";
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000442
Chris Lattner63e944b2009-09-24 05:38:36 +0000443 StringRef WorkStr(Input);
444 while (!WorkStr.empty()) {
445 // If the first character is a delimiter, strip them off.
446 if (Delims.find(WorkStr[0]) != StringRef::npos) {
447 size_t Pos = WorkStr.find_first_not_of(Delims);
448 if (Pos == StringRef::npos) Pos = WorkStr.size();
449 WorkStr = WorkStr.substr(Pos);
450 continue;
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000451 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000452
Chris Lattner63e944b2009-09-24 05:38:36 +0000453 // Find position of first delimiter.
454 size_t Pos = WorkStr.find_first_of(Delims);
455 if (Pos == StringRef::npos) Pos = WorkStr.size();
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000456
Chris Lattner63e944b2009-09-24 05:38:36 +0000457 // Everything from 0 to Pos is the next word to copy.
458 char *NewStr = (char*)malloc(Pos+1);
459 memcpy(NewStr, WorkStr.data(), Pos);
460 NewStr[Pos] = 0;
461 OutputVector.push_back(NewStr);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000462
Chris Lattner63e944b2009-09-24 05:38:36 +0000463 WorkStr = WorkStr.substr(Pos);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000464 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000465}
466
467/// ParseEnvironmentOptions - An alternative entry point to the
468/// CommandLine library, which allows you to read the program's name
469/// from the caller (as PROGNAME) and its command-line arguments from
470/// an environment variable (whose name is given in ENVVAR).
471///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000472void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000473 const char *Overview, bool ReadResponseFiles) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000474 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000475 assert(progName && "Program name not specified");
476 assert(envVar && "Environment variable name missing");
Misha Brukmanf976c852005-04-21 22:55:34 +0000477
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000478 // Get the environment variable they want us to parse options out of.
Chris Lattner23288582006-08-27 22:10:29 +0000479 const char *envValue = getenv(envVar);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000480 if (!envValue)
481 return;
482
Brian Gaeke06b06c52003-08-14 22:00:59 +0000483 // Get program's "name", which we wouldn't know without the caller
484 // telling us.
Chris Lattner23288582006-08-27 22:10:29 +0000485 std::vector<char*> newArgv;
486 newArgv.push_back(strdup(progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000487
488 // Parse the value of the environment variable into a "command line"
489 // and hand it off to ParseCommandLineOptions().
Chris Lattner23288582006-08-27 22:10:29 +0000490 ParseCStringVector(newArgv, envValue);
Evan Cheng34cd4a42008-05-05 18:30:58 +0000491 int newArgc = static_cast<int>(newArgv.size());
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000492 ParseCommandLineOptions(newArgc, &newArgv[0], Overview, ReadResponseFiles);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000493
494 // Free all the strdup()ed strings.
Chris Lattner23288582006-08-27 22:10:29 +0000495 for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
496 i != e; ++i)
Chris Lattnerfb2674d2009-09-20 01:11:23 +0000497 free(*i);
Brian Gaeke06b06c52003-08-14 22:00:59 +0000498}
499
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000500
501/// ExpandResponseFiles - Copy the contents of argv into newArgv,
502/// substituting the contents of the response files for the arguments
503/// of type @file.
Chris Lattnerb1687372009-09-20 05:03:30 +0000504static void ExpandResponseFiles(unsigned argc, char** argv,
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000505 std::vector<char*>& newArgv) {
Chris Lattnerb1687372009-09-20 05:03:30 +0000506 for (unsigned i = 1; i != argc; ++i) {
507 char *arg = argv[i];
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000508
509 if (arg[0] == '@') {
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000510 sys::PathWithStatus respFile(++arg);
511
512 // Check that the response file is not empty (mmap'ing empty
513 // files can be problematic).
514 const sys::FileStatus *FileStat = respFile.getFileStatus();
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000515 if (FileStat && FileStat->getSize() != 0) {
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000516
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000517 // If we could open the file, parse its contents, otherwise
518 // pass the @file option verbatim.
Mikhail Glushenkov6c55b1c2009-01-28 03:46:22 +0000519
520 // TODO: we should also support recursive loading of response files,
521 // since this is how gcc behaves. (From their man page: "The file may
522 // itself contain additional @file options; any such options will be
523 // processed recursively.")
524
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000525 // Mmap the response file into memory.
526 OwningPtr<MemoryBuffer> respFilePtr;
527 if (!MemoryBuffer::getFile(respFile.c_str(), respFilePtr)) {
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000528 ParseCStringVector(newArgv, respFilePtr->getBufferStart());
529 continue;
530 }
531 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000532 }
Mikhail Glushenkov1421b7b2009-01-21 13:14:02 +0000533 newArgv.push_back(strdup(arg));
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000534 }
535}
536
Dan Gohman9a526322007-10-09 16:04:57 +0000537void cl::ParseCommandLineOptions(int argc, char **argv,
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000538 const char *Overview, bool ReadResponseFiles) {
Chris Lattner9878d6a2007-04-06 21:06:55 +0000539 // Process all registered options.
Chris Lattner49b301c2009-09-20 06:18:38 +0000540 SmallVector<Option*, 4> PositionalOpts;
541 SmallVector<Option*, 4> SinkOpts;
Benjamin Kramer461c8762009-09-19 10:01:45 +0000542 StringMap<Option*> Opts;
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000543 GetOptionInfo(PositionalOpts, SinkOpts, Opts);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000544
Chris Lattner9878d6a2007-04-06 21:06:55 +0000545 assert((!Opts.empty() || !PositionalOpts.empty()) &&
546 "No options specified!");
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000547
548 // Expand response files.
549 std::vector<char*> newArgv;
550 if (ReadResponseFiles) {
551 newArgv.push_back(strdup(argv[0]));
552 ExpandResponseFiles(argc, argv, newArgv);
553 argv = &newArgv[0];
Evan Cheng34cd4a42008-05-05 18:30:58 +0000554 argc = static_cast<int>(newArgv.size());
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000555 }
556
Chris Lattnerefa3da52006-10-13 00:06:24 +0000557 // Copy the program name into ProgName, making sure not to overflow it.
Michael J. Spencerb3127bb2010-12-18 00:19:10 +0000558 std::string ProgName = sys::path::filename(argv[0]);
Benjamin Kramer12ea66a2010-01-28 18:04:38 +0000559 size_t Len = std::min(ProgName.size(), size_t(79));
560 memcpy(ProgramName, ProgName.data(), Len);
561 ProgramName[Len] = '\0';
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000562
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000563 ProgramOverview = Overview;
564 bool ErrorParsing = false;
565
Chris Lattner331de232002-07-22 02:07:59 +0000566 // Check out the positional arguments to collect information about them.
567 unsigned NumPositionalRequired = 0;
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000568
Chris Lattnerde013242005-08-08 17:25:38 +0000569 // Determine whether or not there are an unlimited number of positionals
570 bool HasUnlimitedPositionals = false;
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000571
Chris Lattner331de232002-07-22 02:07:59 +0000572 Option *ConsumeAfterOpt = 0;
573 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000574 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000575 assert(PositionalOpts.size() > 1 &&
576 "Cannot specify cl::ConsumeAfter without a positional argument!");
577 ConsumeAfterOpt = PositionalOpts[0];
578 }
579
580 // Calculate how many positional values are _required_.
581 bool UnboundedFound = false;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000582 for (size_t i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
Chris Lattner331de232002-07-22 02:07:59 +0000583 i != e; ++i) {
584 Option *Opt = PositionalOpts[i];
585 if (RequiresValue(Opt))
586 ++NumPositionalRequired;
587 else if (ConsumeAfterOpt) {
588 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000589 // unless there is only one positional argument...
590 if (PositionalOpts.size() > 2)
591 ErrorParsing |=
Benjamin Kramere6864c12009-08-02 12:13:02 +0000592 Opt->error("error - this positional option will never be matched, "
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000593 "because it does not Require a value, and a "
594 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000595 } else if (UnboundedFound && !Opt->ArgStr[0]) {
596 // This option does not "require" a value... Make sure this option is
597 // not specified after an option that eats all extra arguments, or this
598 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000599 //
Benjamin Kramere6864c12009-08-02 12:13:02 +0000600 ErrorParsing |= Opt->error("error - option can never match, because "
Chris Lattner331de232002-07-22 02:07:59 +0000601 "another positional argument will match an "
602 "unbounded number of values, and this option"
603 " does not require a value!");
604 }
605 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
606 }
Chris Lattner21e1a792005-08-08 21:57:27 +0000607 HasUnlimitedPositionals = UnboundedFound || ConsumeAfterOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000608 }
609
Reid Spencer1e13fd22004-08-13 19:47:30 +0000610 // PositionalVals - A vector of "positional" arguments we accumulate into
Chris Lattnerba112292009-09-20 00:07:40 +0000611 // the process at the end.
Chris Lattner331de232002-07-22 02:07:59 +0000612 //
Chris Lattnerba112292009-09-20 00:07:40 +0000613 SmallVector<std::pair<StringRef,unsigned>, 4> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000614
Chris Lattner9cf3d472003-07-30 17:34:02 +0000615 // If the program has named positional arguments, and the name has been run
616 // across, keep track of which positional argument was named. Otherwise put
617 // the positional args into the PositionalVals list...
618 Option *ActivePositionalArg = 0;
619
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000620 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000621 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000622 for (int i = 1; i < argc; ++i) {
623 Option *Handler = 0;
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000624 Option *NearestHandler = 0;
Nick Lewycky95d206a2011-05-02 05:24:47 +0000625 std::string NearestHandlerString;
Chris Lattner4e247ec2009-09-20 01:53:12 +0000626 StringRef Value;
Chris Lattner8a7a0582009-09-20 02:02:24 +0000627 StringRef ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000628
Chris Lattner69d6f132007-04-12 00:36:29 +0000629 // If the option list changed, this means that some command line
Chris Lattner159b0a432007-04-11 15:35:18 +0000630 // option has just been registered or deregistered. This can occur in
631 // response to things like -load, etc. If this happens, rescan the options.
Chris Lattner69d6f132007-04-12 00:36:29 +0000632 if (OptionListChanged) {
Chris Lattner159b0a432007-04-11 15:35:18 +0000633 PositionalOpts.clear();
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000634 SinkOpts.clear();
Chris Lattner159b0a432007-04-11 15:35:18 +0000635 Opts.clear();
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000636 GetOptionInfo(PositionalOpts, SinkOpts, Opts);
Chris Lattner69d6f132007-04-12 00:36:29 +0000637 OptionListChanged = false;
Chris Lattner159b0a432007-04-11 15:35:18 +0000638 }
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000639
Chris Lattner331de232002-07-22 02:07:59 +0000640 // Check to see if this is a positional argument. This argument is
641 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000642 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000643 //
644 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
645 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000646 if (ActivePositionalArg) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000647 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000648 continue; // We are done!
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000649 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000650
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000651 if (!PositionalOpts.empty()) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000652 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000653
654 // All of the positional arguments have been fulfulled, give the rest to
655 // the consume after option... if it's specified...
656 //
Misha Brukmanf976c852005-04-21 22:55:34 +0000657 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000658 ConsumeAfterOpt != 0) {
659 for (++i; i < argc; ++i)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000660 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000661 break; // Handle outside of the argument processing loop...
662 }
663
664 // Delay processing positional arguments until the end...
665 continue;
666 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000667 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
668 !DashDashFound) {
669 DashDashFound = true; // This is the mythical "--"?
670 continue; // Don't try to process it as an argument itself.
671 } else if (ActivePositionalArg &&
672 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
673 // If there is a positional argument eating options, check to see if this
674 // option is another positional argument. If so, treat it as an argument,
675 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000676 ArgName = argv[i]+1;
Chris Lattnerb1687372009-09-20 05:03:30 +0000677 // Eat leading dashes.
678 while (!ArgName.empty() && ArgName[0] == '-')
679 ArgName = ArgName.substr(1);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000680
Chris Lattner9878d6a2007-04-06 21:06:55 +0000681 Handler = LookupOption(ArgName, Value, Opts);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000682 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000683 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000684 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000685 }
686
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000687 } else { // We start with a '-', must be an argument.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000688 ArgName = argv[i]+1;
Chris Lattnerb1687372009-09-20 05:03:30 +0000689 // Eat leading dashes.
690 while (!ArgName.empty() && ArgName[0] == '-')
691 ArgName = ArgName.substr(1);
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000692
Chris Lattner9878d6a2007-04-06 21:06:55 +0000693 Handler = LookupOption(ArgName, Value, Opts);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000694
Chris Lattnerbf455c22004-05-06 22:04:31 +0000695 // Check to see if this "option" is really a prefixed or grouped argument.
Chris Lattnerb1687372009-09-20 05:03:30 +0000696 if (Handler == 0)
697 Handler = HandlePrefixedOrGroupedOption(ArgName, Value,
698 ErrorParsing, Opts);
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000699
700 // Otherwise, look for the closest available option to report to the user
701 // in the upcoming error.
702 if (Handler == 0 && SinkOpts.empty())
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000703 NearestHandler = LookupNearestOption(ArgName, Opts,
704 NearestHandlerString);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000705 }
706
707 if (Handler == 0) {
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000708 if (SinkOpts.empty()) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000709 errs() << ProgramName << ": Unknown command line argument '"
Duncan Sands7e7ae5a2010-02-18 14:08:13 +0000710 << argv[i] << "'. Try: '" << argv[0] << " -help'\n";
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000711
Daniel Dunbarc98d82a2011-01-24 17:27:17 +0000712 if (NearestHandler) {
713 // If we know a near match, report it as well.
714 errs() << ProgramName << ": Did you mean '-"
715 << NearestHandlerString << "'?\n";
716 }
Daniel Dunbarf4fb66a2011-01-18 01:59:24 +0000717
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000718 ErrorParsing = true;
719 } else {
Chris Lattner49b301c2009-09-20 06:18:38 +0000720 for (SmallVectorImpl<Option*>::iterator I = SinkOpts.begin(),
Anton Korobeynikovd57160d2008-02-20 12:38:07 +0000721 E = SinkOpts.end(); I != E ; ++I)
722 (*I)->addOccurrence(i, "", argv[i]);
723 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000724 continue;
725 }
726
Chris Lattner9cf3d472003-07-30 17:34:02 +0000727 // If this is a named positional argument, just remember that it is the
728 // active one...
729 if (Handler->getFormattingFlag() == cl::Positional)
730 ActivePositionalArg = Handler;
Chris Lattner341620b2009-09-20 01:49:31 +0000731 else
Chris Lattner4e247ec2009-09-20 01:53:12 +0000732 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000733 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000734
Chris Lattner331de232002-07-22 02:07:59 +0000735 // Check and handle positional arguments now...
736 if (NumPositionalRequired > PositionalVals.size()) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000737 errs() << ProgramName
Bill Wendlinge8156192006-12-07 01:30:32 +0000738 << ": Not enough positional command line arguments specified!\n"
739 << "Must specify at least " << NumPositionalRequired
Duncan Sands7e7ae5a2010-02-18 14:08:13 +0000740 << " positional arguments: See: " << argv[0] << " -help\n";
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000741
Chris Lattner331de232002-07-22 02:07:59 +0000742 ErrorParsing = true;
Dan Gohman16e02092010-03-24 19:38:02 +0000743 } else if (!HasUnlimitedPositionals &&
744 PositionalVals.size() > PositionalOpts.size()) {
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000745 errs() << ProgramName
Bill Wendlinge8156192006-12-07 01:30:32 +0000746 << ": Too many positional arguments specified!\n"
747 << "Can specify at most " << PositionalOpts.size()
Duncan Sands7e7ae5a2010-02-18 14:08:13 +0000748 << " positional arguments: See: " << argv[0] << " -help\n";
Chris Lattnerde013242005-08-08 17:25:38 +0000749 ErrorParsing = true;
Chris Lattner331de232002-07-22 02:07:59 +0000750
751 } else if (ConsumeAfterOpt == 0) {
Chris Lattnerb1687372009-09-20 05:03:30 +0000752 // Positional args have already been handled if ConsumeAfter is specified.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000753 unsigned ValNo = 0, NumVals = static_cast<unsigned>(PositionalVals.size());
754 for (size_t i = 0, e = PositionalOpts.size(); i != e; ++i) {
Chris Lattner331de232002-07-22 02:07:59 +0000755 if (RequiresValue(PositionalOpts[i])) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000756 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000757 PositionalVals[ValNo].second);
758 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000759 --NumPositionalRequired; // We fulfilled our duty...
760 }
761
762 // If we _can_ give this option more arguments, do so now, as long as we
763 // do not give it values that others need. 'Done' controls whether the
764 // option even _WANTS_ any more.
765 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000766 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000767 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000768 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000769 case cl::Optional:
770 Done = true; // Optional arguments want _at most_ one value
771 // FALL THROUGH
772 case cl::ZeroOrMore: // Zero or more will take all they can get...
773 case cl::OneOrMore: // One or more will take all they can get...
Reid Spencer1e13fd22004-08-13 19:47:30 +0000774 ProvidePositionalOption(PositionalOpts[i],
775 PositionalVals[ValNo].first,
776 PositionalVals[ValNo].second);
777 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000778 break;
779 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000780 llvm_unreachable("Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000781 "positional argument processing!");
782 }
783 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000784 }
Chris Lattner331de232002-07-22 02:07:59 +0000785 } else {
786 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
787 unsigned ValNo = 0;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000788 for (size_t j = 1, e = PositionalOpts.size(); j != e; ++j)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000789 if (RequiresValue(PositionalOpts[j])) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000790 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000791 PositionalVals[ValNo].first,
792 PositionalVals[ValNo].second);
793 ValNo++;
794 }
Chris Lattnerfaba8092002-07-24 20:15:13 +0000795
796 // Handle the case where there is just one positional option, and it's
797 // optional. In this case, we want to give JUST THE FIRST option to the
798 // positional option and keep the rest for the consume after. The above
799 // loop would have assigned no values to positional options in this case.
800 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000801 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty()) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000802 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000803 PositionalVals[ValNo].first,
804 PositionalVals[ValNo].second);
805 ValNo++;
806 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000807
Chris Lattner331de232002-07-22 02:07:59 +0000808 // Handle over all of the rest of the arguments to the
809 // cl::ConsumeAfter command line option...
810 for (; ValNo != PositionalVals.size(); ++ValNo)
811 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000812 PositionalVals[ValNo].first,
813 PositionalVals[ValNo].second);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000814 }
815
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000816 // Loop over args and make sure all required args are specified!
Benjamin Kramer461c8762009-09-19 10:01:45 +0000817 for (StringMap<Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000818 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000819 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000820 case Required:
821 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000822 if (I->second->getNumOccurrences() == 0) {
Benjamin Kramere6864c12009-08-02 12:13:02 +0000823 I->second->error("must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000824 ErrorParsing = true;
825 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000826 // Fall through
827 default:
828 break;
829 }
830 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000831
Rafael Espindolaa962b402010-11-19 21:14:29 +0000832 // Now that we know if -debug is specified, we can use it.
833 // Note that if ReadResponseFiles == true, this must be done before the
834 // memory allocated for the expanded command line is free()d below.
835 DEBUG(dbgs() << "Args: ";
836 for (int i = 0; i < argc; ++i)
837 dbgs() << argv[i] << ' ';
838 dbgs() << '\n';
839 );
840
Chris Lattner331de232002-07-22 02:07:59 +0000841 // Free all of the memory allocated to the map. Command line options may only
842 // be processed once!
Chris Lattner90aa8392006-10-04 21:52:35 +0000843 Opts.clear();
Chris Lattner331de232002-07-22 02:07:59 +0000844 PositionalOpts.clear();
Chris Lattner90aa8392006-10-04 21:52:35 +0000845 MoreHelp->clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000846
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000847 // Free the memory allocated by ExpandResponseFiles.
848 if (ReadResponseFiles) {
849 // Free all the strdup()ed strings.
850 for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
851 i != e; ++i)
Chris Lattnerfd40d032009-09-20 07:16:54 +0000852 free(*i);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000853 }
854
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000855 // If we had an error processing our arguments, don't let the program execute
856 if (ErrorParsing) exit(1);
857}
858
859//===----------------------------------------------------------------------===//
860// Option Base class implementation
861//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000862
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000863bool Option::error(const Twine &Message, StringRef ArgName) {
864 if (ArgName.data() == 0) ArgName = ArgStr;
865 if (ArgName.empty())
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000866 errs() << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000867 else
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000868 errs() << ProgramName << ": for the -" << ArgName;
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +0000869
Benjamin Kramerd227a3f2009-08-23 10:01:13 +0000870 errs() << " option: " << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000871 return true;
872}
873
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000874bool Option::addOccurrence(unsigned pos, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000875 StringRef Value, bool MultiArg) {
Mikhail Glushenkov7059d472009-01-16 22:54:19 +0000876 if (!MultiArg)
877 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000878
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000879 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000880 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000881 if (NumOccurrences > 1)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000882 return error("may only occur zero or one times!", ArgName);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000883 break;
884 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000885 if (NumOccurrences > 1)
Benjamin Kramere6864c12009-08-02 12:13:02 +0000886 return error("must occur exactly one time!", ArgName);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000887 // Fall through
888 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000889 case ZeroOrMore:
890 case ConsumeAfter: break;
Benjamin Kramere6864c12009-08-02 12:13:02 +0000891 default: return error("bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000892 }
893
Reid Spencer1e13fd22004-08-13 19:47:30 +0000894 return handleOccurrence(pos, ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000895}
896
Chris Lattner331de232002-07-22 02:07:59 +0000897
898// getValueStr - Get the value description string, using "DefaultMsg" if nothing
899// has been specified yet.
900//
901static const char *getValueStr(const Option &O, const char *DefaultMsg) {
902 if (O.ValueStr[0] == 0) return DefaultMsg;
903 return O.ValueStr;
904}
905
906//===----------------------------------------------------------------------===//
907// cl::alias class implementation
908//
909
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000910// Return the width of the option tag for printing...
Evan Cheng34cd4a42008-05-05 18:30:58 +0000911size_t alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000912 return std::strlen(ArgStr)+6;
913}
914
Chris Lattnera0de8432006-04-28 05:36:25 +0000915// Print out the option for the alias.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000916void alias::printOptionInfo(size_t GlobalWidth) const {
917 size_t L = std::strlen(ArgStr);
Evan Chengff276b42011-06-13 20:45:54 +0000918 outs() << " -" << ArgStr;
919 outs().indent(GlobalWidth-L-6) << " - " << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000920}
921
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000922//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000923// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000924//
925
Chris Lattner9b14eb52002-08-07 18:36:37 +0000926// basic_parser implementation
927//
928
929// Return the width of the option tag for printing...
Evan Cheng34cd4a42008-05-05 18:30:58 +0000930size_t basic_parser_impl::getOptionWidth(const Option &O) const {
931 size_t Len = std::strlen(O.ArgStr);
Chris Lattner9b14eb52002-08-07 18:36:37 +0000932 if (const char *ValName = getValueName())
933 Len += std::strlen(getValueStr(O, ValName))+3;
934
935 return Len + 6;
936}
937
Misha Brukmanf976c852005-04-21 22:55:34 +0000938// printOptionInfo - Print out information about this option. The
Chris Lattner9b14eb52002-08-07 18:36:37 +0000939// to-be-maintained width is specified.
940//
941void basic_parser_impl::printOptionInfo(const Option &O,
Evan Cheng34cd4a42008-05-05 18:30:58 +0000942 size_t GlobalWidth) const {
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000943 outs() << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000944
945 if (const char *ValName = getValueName())
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000946 outs() << "=<" << getValueStr(O, ValName) << '>';
Chris Lattner9b14eb52002-08-07 18:36:37 +0000947
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000948 outs().indent(GlobalWidth-getOptionWidth(O)) << " - " << O.HelpStr << '\n';
Chris Lattner9b14eb52002-08-07 18:36:37 +0000949}
950
Andrew Trickce969022011-04-05 18:54:36 +0000951void basic_parser_impl::printOptionName(const Option &O,
952 size_t GlobalWidth) const {
953 outs() << " -" << O.ArgStr;
954 outs().indent(GlobalWidth-std::strlen(O.ArgStr));
955}
Chris Lattner9b14eb52002-08-07 18:36:37 +0000956
957
Chris Lattner331de232002-07-22 02:07:59 +0000958// parser<bool> implementation
959//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000960bool parser<bool>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000961 StringRef Arg, bool &Value) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000962 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000963 Arg == "1") {
964 Value = true;
Chris Lattnera460beb2009-09-19 18:55:05 +0000965 return false;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000966 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000967
Chris Lattnera460beb2009-09-19 18:55:05 +0000968 if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
969 Value = false;
970 return false;
971 }
972 return O.error("'" + Arg +
973 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000974}
975
Dale Johannesen81da02b2007-05-22 17:14:46 +0000976// parser<boolOrDefault> implementation
977//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000978bool parser<boolOrDefault>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000979 StringRef Arg, boolOrDefault &Value) {
Dale Johannesen81da02b2007-05-22 17:14:46 +0000980 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
981 Arg == "1") {
982 Value = BOU_TRUE;
Chris Lattnera460beb2009-09-19 18:55:05 +0000983 return false;
Dale Johannesen81da02b2007-05-22 17:14:46 +0000984 }
Chris Lattnera460beb2009-09-19 18:55:05 +0000985 if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
986 Value = BOU_FALSE;
987 return false;
988 }
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +0000989
Chris Lattnera460beb2009-09-19 18:55:05 +0000990 return O.error("'" + Arg +
991 "' is invalid value for boolean argument! Try 0 or 1");
Dale Johannesen81da02b2007-05-22 17:14:46 +0000992}
993
Chris Lattner331de232002-07-22 02:07:59 +0000994// parser<int> implementation
995//
Chris Lattner99c5c7b2009-09-20 00:40:49 +0000996bool parser<int>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +0000997 StringRef Arg, int &Value) {
Chris Lattner970e7df2009-09-19 23:59:02 +0000998 if (Arg.getAsInteger(0, Value))
Benjamin Kramere6864c12009-08-02 12:13:02 +0000999 return O.error("'" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001000 return false;
1001}
1002
Chris Lattnerd2a6fc32003-06-28 15:47:20 +00001003// parser<unsigned> implementation
1004//
Chris Lattner99c5c7b2009-09-20 00:40:49 +00001005bool parser<unsigned>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +00001006 StringRef Arg, unsigned &Value) {
Chris Lattner970e7df2009-09-19 23:59:02 +00001007
1008 if (Arg.getAsInteger(0, Value))
Benjamin Kramere6864c12009-08-02 12:13:02 +00001009 return O.error("'" + Arg + "' value invalid for uint argument!");
Chris Lattnerd2a6fc32003-06-28 15:47:20 +00001010 return false;
1011}
1012
Benjamin Kramerb3514562011-09-15 21:17:37 +00001013// parser<unsigned long long> implementation
1014//
1015bool parser<unsigned long long>::parse(Option &O, StringRef ArgName,
1016 StringRef Arg, unsigned long long &Value){
1017
1018 if (Arg.getAsInteger(0, Value))
1019 return O.error("'" + Arg + "' value invalid for uint argument!");
1020 return false;
1021}
1022
Chris Lattner9b14eb52002-08-07 18:36:37 +00001023// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +00001024//
Chris Lattnera460beb2009-09-19 18:55:05 +00001025static bool parseDouble(Option &O, StringRef Arg, double &Value) {
Chris Lattner970e7df2009-09-19 23:59:02 +00001026 SmallString<32> TmpStr(Arg.begin(), Arg.end());
1027 const char *ArgStart = TmpStr.c_str();
Chris Lattner331de232002-07-22 02:07:59 +00001028 char *End;
1029 Value = strtod(ArgStart, &End);
Misha Brukmanf976c852005-04-21 22:55:34 +00001030 if (*End != 0)
Benjamin Kramere6864c12009-08-02 12:13:02 +00001031 return O.error("'" + Arg + "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +00001032 return false;
1033}
1034
Chris Lattner99c5c7b2009-09-20 00:40:49 +00001035bool parser<double>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +00001036 StringRef Arg, double &Val) {
Chris Lattner9b14eb52002-08-07 18:36:37 +00001037 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +00001038}
1039
Chris Lattner99c5c7b2009-09-20 00:40:49 +00001040bool parser<float>::parse(Option &O, StringRef ArgName,
Chris Lattnera460beb2009-09-19 18:55:05 +00001041 StringRef Arg, float &Val) {
Chris Lattner9b14eb52002-08-07 18:36:37 +00001042 double dVal;
1043 if (parseDouble(O, Arg, dVal))
1044 return true;
1045 Val = (float)dVal;
1046 return false;
Chris Lattner331de232002-07-22 02:07:59 +00001047}
1048
1049
Chris Lattner331de232002-07-22 02:07:59 +00001050
1051// generic_parser_base implementation
1052//
1053
Chris Lattneraa852bb2002-07-23 17:15:12 +00001054// findOption - Return the option number corresponding to the specified
1055// argument string. If the option is not found, getNumOptions() is returned.
1056//
1057unsigned generic_parser_base::findOption(const char *Name) {
Benjamin Kramer461c8762009-09-19 10:01:45 +00001058 unsigned e = getNumOptions();
Chris Lattneraa852bb2002-07-23 17:15:12 +00001059
Benjamin Kramer461c8762009-09-19 10:01:45 +00001060 for (unsigned i = 0; i != e; ++i) {
1061 if (strcmp(getOption(i), Name) == 0)
Chris Lattneraa852bb2002-07-23 17:15:12 +00001062 return i;
Benjamin Kramer461c8762009-09-19 10:01:45 +00001063 }
Chris Lattneraa852bb2002-07-23 17:15:12 +00001064 return e;
1065}
1066
1067
Chris Lattner331de232002-07-22 02:07:59 +00001068// Return the width of the option tag for printing...
Evan Cheng34cd4a42008-05-05 18:30:58 +00001069size_t generic_parser_base::getOptionWidth(const Option &O) const {
Chris Lattner331de232002-07-22 02:07:59 +00001070 if (O.hasArgStr()) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001071 size_t Size = std::strlen(O.ArgStr)+6;
Chris Lattner331de232002-07-22 02:07:59 +00001072 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Evan Cheng34cd4a42008-05-05 18:30:58 +00001073 Size = std::max(Size, std::strlen(getOption(i))+8);
Chris Lattner331de232002-07-22 02:07:59 +00001074 return Size;
1075 } else {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001076 size_t BaseSize = 0;
Chris Lattner331de232002-07-22 02:07:59 +00001077 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Evan Cheng34cd4a42008-05-05 18:30:58 +00001078 BaseSize = std::max(BaseSize, std::strlen(getOption(i))+8);
Chris Lattner331de232002-07-22 02:07:59 +00001079 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001080 }
1081}
1082
Misha Brukmanf976c852005-04-21 22:55:34 +00001083// printOptionInfo - Print out information about this option. The
Chris Lattner331de232002-07-22 02:07:59 +00001084// to-be-maintained width is specified.
1085//
1086void generic_parser_base::printOptionInfo(const Option &O,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001087 size_t GlobalWidth) const {
Chris Lattner331de232002-07-22 02:07:59 +00001088 if (O.hasArgStr()) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001089 size_t L = std::strlen(O.ArgStr);
Chris Lattnerb1687372009-09-20 05:03:30 +00001090 outs() << " -" << O.ArgStr;
1091 outs().indent(GlobalWidth-L-6) << " - " << O.HelpStr << '\n';
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001092
Chris Lattner331de232002-07-22 02:07:59 +00001093 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001094 size_t NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerb1687372009-09-20 05:03:30 +00001095 outs() << " =" << getOption(i);
1096 outs().indent(NumSpaces) << " - " << getDescription(i) << '\n';
Chris Lattner9c9be482002-01-31 00:42:56 +00001097 }
Chris Lattner331de232002-07-22 02:07:59 +00001098 } else {
1099 if (O.HelpStr[0])
Chris Lattnerb1687372009-09-20 05:03:30 +00001100 outs() << " " << O.HelpStr << '\n';
Chris Lattner331de232002-07-22 02:07:59 +00001101 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001102 size_t L = std::strlen(getOption(i));
Chris Lattnerb1687372009-09-20 05:03:30 +00001103 outs() << " -" << getOption(i);
1104 outs().indent(GlobalWidth-L-8) << " - " << getDescription(i) << '\n';
Chris Lattner331de232002-07-22 02:07:59 +00001105 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001106 }
1107}
1108
Andrew Trickce969022011-04-05 18:54:36 +00001109static const size_t MaxOptWidth = 8; // arbitrary spacing for printOptionDiff
1110
1111// printGenericOptionDiff - Print the value of this option and it's default.
1112//
1113// "Generic" options have each value mapped to a name.
1114void generic_parser_base::
1115printGenericOptionDiff(const Option &O, const GenericOptionValue &Value,
1116 const GenericOptionValue &Default,
1117 size_t GlobalWidth) const {
1118 outs() << " -" << O.ArgStr;
1119 outs().indent(GlobalWidth-std::strlen(O.ArgStr));
1120
1121 unsigned NumOpts = getNumOptions();
1122 for (unsigned i = 0; i != NumOpts; ++i) {
1123 if (Value.compare(getOptionValue(i)))
1124 continue;
1125
1126 outs() << "= " << getOption(i);
1127 size_t L = std::strlen(getOption(i));
1128 size_t NumSpaces = MaxOptWidth > L ? MaxOptWidth - L : 0;
1129 outs().indent(NumSpaces) << " (default: ";
1130 for (unsigned j = 0; j != NumOpts; ++j) {
1131 if (Default.compare(getOptionValue(j)))
1132 continue;
1133 outs() << getOption(j);
1134 break;
1135 }
1136 outs() << ")\n";
1137 return;
1138 }
1139 outs() << "= *unknown option value*\n";
1140}
1141
1142// printOptionDiff - Specializations for printing basic value types.
1143//
1144#define PRINT_OPT_DIFF(T) \
1145 void parser<T>:: \
1146 printOptionDiff(const Option &O, T V, OptionValue<T> D, \
1147 size_t GlobalWidth) const { \
1148 printOptionName(O, GlobalWidth); \
1149 std::string Str; \
1150 { \
1151 raw_string_ostream SS(Str); \
1152 SS << V; \
1153 } \
1154 outs() << "= " << Str; \
1155 size_t NumSpaces = MaxOptWidth > Str.size() ? MaxOptWidth - Str.size() : 0;\
1156 outs().indent(NumSpaces) << " (default: "; \
1157 if (D.hasValue()) \
1158 outs() << D.getValue(); \
1159 else \
1160 outs() << "*no default*"; \
1161 outs() << ")\n"; \
1162 } \
1163
Frits van Bommel090771f2011-04-06 12:29:56 +00001164PRINT_OPT_DIFF(bool)
1165PRINT_OPT_DIFF(boolOrDefault)
1166PRINT_OPT_DIFF(int)
1167PRINT_OPT_DIFF(unsigned)
Benjamin Kramerb3514562011-09-15 21:17:37 +00001168PRINT_OPT_DIFF(unsigned long long)
Frits van Bommel090771f2011-04-06 12:29:56 +00001169PRINT_OPT_DIFF(double)
1170PRINT_OPT_DIFF(float)
1171PRINT_OPT_DIFF(char)
Andrew Trickce969022011-04-05 18:54:36 +00001172
1173void parser<std::string>::
1174printOptionDiff(const Option &O, StringRef V, OptionValue<std::string> D,
1175 size_t GlobalWidth) const {
1176 printOptionName(O, GlobalWidth);
1177 outs() << "= " << V;
1178 size_t NumSpaces = MaxOptWidth > V.size() ? MaxOptWidth - V.size() : 0;
1179 outs().indent(NumSpaces) << " (default: ";
1180 if (D.hasValue())
1181 outs() << D.getValue();
1182 else
1183 outs() << "*no default*";
1184 outs() << ")\n";
1185}
1186
1187// Print a placeholder for options that don't yet support printOptionDiff().
1188void basic_parser_impl::
1189printOptionNoValue(const Option &O, size_t GlobalWidth) const {
1190 printOptionName(O, GlobalWidth);
1191 outs() << "= *cannot print option value*\n";
1192}
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001193
1194//===----------------------------------------------------------------------===//
Duncan Sands7e7ae5a2010-02-18 14:08:13 +00001195// -help and -help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001196//
Reid Spencerad0846b2004-11-14 22:04:00 +00001197
Chris Lattner0fd48b12009-09-20 05:37:24 +00001198static int OptNameCompare(const void *LHS, const void *RHS) {
1199 typedef std::pair<const char *, Option*> pair_ty;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +00001200
Chris Lattner0fd48b12009-09-20 05:37:24 +00001201 return strcmp(((pair_ty*)LHS)->first, ((pair_ty*)RHS)->first);
1202}
1203
Andrew Trickce969022011-04-05 18:54:36 +00001204// Copy Options into a vector so we can sort them as we like.
1205static void
1206sortOpts(StringMap<Option*> &OptMap,
1207 SmallVectorImpl< std::pair<const char *, Option*> > &Opts,
1208 bool ShowHidden) {
1209 SmallPtrSet<Option*, 128> OptionSet; // Duplicate option detection.
1210
1211 for (StringMap<Option*>::iterator I = OptMap.begin(), E = OptMap.end();
1212 I != E; ++I) {
1213 // Ignore really-hidden options.
1214 if (I->second->getOptionHiddenFlag() == ReallyHidden)
1215 continue;
1216
1217 // Unless showhidden is set, ignore hidden flags.
1218 if (I->second->getOptionHiddenFlag() == Hidden && !ShowHidden)
1219 continue;
1220
1221 // If we've already seen this option, don't add it to the list again.
1222 if (!OptionSet.insert(I->second))
1223 continue;
1224
1225 Opts.push_back(std::pair<const char *, Option*>(I->getKey().data(),
1226 I->second));
1227 }
1228
1229 // Sort the options list alphabetically.
1230 qsort(Opts.data(), Opts.size(), sizeof(Opts[0]), OptNameCompare);
1231}
1232
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001233namespace {
1234
Chris Lattner331de232002-07-22 02:07:59 +00001235class HelpPrinter {
Evan Cheng34cd4a42008-05-05 18:30:58 +00001236 size_t MaxArgLen;
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001237 const Option *EmptyArg;
1238 const bool ShowHidden;
1239
Chris Lattner331de232002-07-22 02:07:59 +00001240public:
Dan Gohman950a4c42008-03-25 22:06:05 +00001241 explicit HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
Chris Lattner331de232002-07-22 02:07:59 +00001242 EmptyArg = 0;
1243 }
1244
1245 void operator=(bool Value) {
1246 if (Value == false) return;
1247
Chris Lattner9878d6a2007-04-06 21:06:55 +00001248 // Get all the options.
Chris Lattner49b301c2009-09-20 06:18:38 +00001249 SmallVector<Option*, 4> PositionalOpts;
1250 SmallVector<Option*, 4> SinkOpts;
Benjamin Kramer461c8762009-09-19 10:01:45 +00001251 StringMap<Option*> OptMap;
Anton Korobeynikovd57160d2008-02-20 12:38:07 +00001252 GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +00001253
Chris Lattner0fd48b12009-09-20 05:37:24 +00001254 SmallVector<std::pair<const char *, Option*>, 128> Opts;
Andrew Trickce969022011-04-05 18:54:36 +00001255 sortOpts(OptMap, Opts, ShowHidden);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001256
1257 if (ProgramOverview)
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001258 outs() << "OVERVIEW: " << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001259
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001260 outs() << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +00001261
Chris Lattner90aa8392006-10-04 21:52:35 +00001262 // Print out the positional options.
Chris Lattner331de232002-07-22 02:07:59 +00001263 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Mikhail Glushenkovbeb4d822008-04-28 16:44:25 +00001264 if (!PositionalOpts.empty() &&
Chris Lattner9878d6a2007-04-06 21:06:55 +00001265 PositionalOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
1266 CAOpt = PositionalOpts[0];
Chris Lattner331de232002-07-22 02:07:59 +00001267
Evan Cheng34cd4a42008-05-05 18:30:58 +00001268 for (size_t i = CAOpt != 0, e = PositionalOpts.size(); i != e; ++i) {
Chris Lattner9878d6a2007-04-06 21:06:55 +00001269 if (PositionalOpts[i]->ArgStr[0])
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001270 outs() << " --" << PositionalOpts[i]->ArgStr;
1271 outs() << " " << PositionalOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +00001272 }
Chris Lattner331de232002-07-22 02:07:59 +00001273
1274 // Print the consume after option info if it exists...
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001275 if (CAOpt) outs() << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +00001276
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001277 outs() << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001278
1279 // Compute the maximum argument length...
1280 MaxArgLen = 0;
Evan Cheng34cd4a42008-05-05 18:30:58 +00001281 for (size_t i = 0, e = Opts.size(); i != e; ++i)
Chris Lattner0fd48b12009-09-20 05:37:24 +00001282 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001283
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001284 outs() << "OPTIONS:\n";
Evan Cheng34cd4a42008-05-05 18:30:58 +00001285 for (size_t i = 0, e = Opts.size(); i != e; ++i)
Chris Lattner0fd48b12009-09-20 05:37:24 +00001286 Opts[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001287
Chris Lattnerc540ebb2004-11-19 17:08:15 +00001288 // Print any extra help the user has declared.
Chris Lattner90aa8392006-10-04 21:52:35 +00001289 for (std::vector<const char *>::iterator I = MoreHelp->begin(),
1290 E = MoreHelp->end(); I != E; ++I)
Chris Lattnerd9ea85a2009-08-23 08:43:55 +00001291 outs() << *I;
Chris Lattner90aa8392006-10-04 21:52:35 +00001292 MoreHelp->clear();
Reid Spencerad0846b2004-11-14 22:04:00 +00001293
Reid Spencer9bbba0912004-11-16 06:11:52 +00001294 // Halt the program since help information was printed
Chris Lattner331de232002-07-22 02:07:59 +00001295 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001296 }
1297};
Chris Lattner500d8bf2006-10-12 22:09:17 +00001298} // End anonymous namespace
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001299
Chris Lattner331de232002-07-22 02:07:59 +00001300// Define the two HelpPrinter instances that are used to print out help, or
1301// help-hidden...
1302//
Chris Lattner500d8bf2006-10-12 22:09:17 +00001303static HelpPrinter NormalPrinter(false);
1304static HelpPrinter HiddenPrinter(true);
Chris Lattner331de232002-07-22 02:07:59 +00001305
Chris Lattner500d8bf2006-10-12 22:09:17 +00001306static cl::opt<HelpPrinter, true, parser<bool> >
Duncan Sands7e7ae5a2010-02-18 14:08:13 +00001307HOp("help", cl::desc("Display available options (-help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +00001308 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +00001309
Chris Lattner500d8bf2006-10-12 22:09:17 +00001310static cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +00001311HHOp("help-hidden", cl::desc("Display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +00001312 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001313
Andrew Trickce969022011-04-05 18:54:36 +00001314static cl::opt<bool>
1315PrintOptions("print-options",
1316 cl::desc("Print non-default options after command line parsing"),
1317 cl::Hidden, cl::init(false));
1318
1319static cl::opt<bool>
1320PrintAllOptions("print-all-options",
1321 cl::desc("Print all option values after command line parsing"),
1322 cl::Hidden, cl::init(false));
1323
1324// Print the value of each option.
1325void cl::PrintOptionValues() {
1326 if (!PrintOptions && !PrintAllOptions) return;
1327
1328 // Get all the options.
1329 SmallVector<Option*, 4> PositionalOpts;
1330 SmallVector<Option*, 4> SinkOpts;
1331 StringMap<Option*> OptMap;
1332 GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
1333
1334 SmallVector<std::pair<const char *, Option*>, 128> Opts;
1335 sortOpts(OptMap, Opts, /*ShowHidden*/true);
1336
1337 // Compute the maximum argument length...
1338 size_t MaxArgLen = 0;
1339 for (size_t i = 0, e = Opts.size(); i != e; ++i)
1340 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
1341
1342 for (size_t i = 0, e = Opts.size(); i != e; ++i)
1343 Opts[i].second->printOptionValue(MaxArgLen, PrintAllOptions);
1344}
1345
Chris Lattner500d8bf2006-10-12 22:09:17 +00001346static void (*OverrideVersionPrinter)() = 0;
Reid Spencer515b5b32006-06-05 16:22:56 +00001347
Chandler Carruth6d51d262011-07-22 07:50:40 +00001348static std::vector<void (*)()>* ExtraVersionPrinters = 0;
1349
Chris Lattner500d8bf2006-10-12 22:09:17 +00001350namespace {
Reid Spencer515b5b32006-06-05 16:22:56 +00001351class VersionPrinter {
1352public:
Devang Patelaed293d2007-02-01 01:43:37 +00001353 void print() {
Chris Lattner49b301c2009-09-20 06:18:38 +00001354 raw_ostream &OS = outs();
1355 OS << "Low Level Virtual Machine (http://llvm.org/):\n"
1356 << " " << PACKAGE_NAME << " version " << PACKAGE_VERSION;
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001357#ifdef LLVM_VERSION_INFO
Chris Lattner49b301c2009-09-20 06:18:38 +00001358 OS << LLVM_VERSION_INFO;
Reid Spencer515b5b32006-06-05 16:22:56 +00001359#endif
Chris Lattner49b301c2009-09-20 06:18:38 +00001360 OS << "\n ";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001361#ifndef __OPTIMIZE__
Chris Lattner49b301c2009-09-20 06:18:38 +00001362 OS << "DEBUG build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001363#else
Chris Lattner49b301c2009-09-20 06:18:38 +00001364 OS << "Optimized build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001365#endif
1366#ifndef NDEBUG
Chris Lattner49b301c2009-09-20 06:18:38 +00001367 OS << " with assertions";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +00001368#endif
Daniel Dunbarba43e072009-11-14 21:36:07 +00001369 std::string CPU = sys::getHostCPUName();
Benjamin Kramer110e7bb2009-11-17 17:57:04 +00001370 if (CPU == "generic") CPU = "(unknown)";
Chris Lattner49b301c2009-09-20 06:18:38 +00001371 OS << ".\n"
Daniel Dunbardd464df2010-05-10 20:11:56 +00001372#if (ENABLE_TIMESTAMPS == 1)
Chris Lattner49b301c2009-09-20 06:18:38 +00001373 << " Built " << __DATE__ << " (" << __TIME__ << ").\n"
Daniel Dunbardd464df2010-05-10 20:11:56 +00001374#endif
Sebastian Pop01738642011-11-01 21:32:20 +00001375 << " Default target: " << sys::getDefaultTargetTriple() << '\n'
Chandler Carruth40393132011-07-22 07:50:48 +00001376 << " Host CPU: " << CPU << '\n';
Devang Patelaed293d2007-02-01 01:43:37 +00001377 }
1378 void operator=(bool OptionWasSpecified) {
Chris Lattner043b8b52009-09-20 05:48:01 +00001379 if (!OptionWasSpecified) return;
Mikhail Glushenkoveeebecf2009-11-19 17:29:36 +00001380
Chandler Carruth6d51d262011-07-22 07:50:40 +00001381 if (OverrideVersionPrinter != 0) {
1382 (*OverrideVersionPrinter)();
Chris Lattner043b8b52009-09-20 05:48:01 +00001383 exit(1);
Reid Spencer515b5b32006-06-05 16:22:56 +00001384 }
Chandler Carruth6d51d262011-07-22 07:50:40 +00001385 print();
1386
1387 // Iterate over any registered extra printers and call them to add further
1388 // information.
1389 if (ExtraVersionPrinters != 0) {
Chandler Carruth40393132011-07-22 07:50:48 +00001390 outs() << '\n';
Chandler Carruth6d51d262011-07-22 07:50:40 +00001391 for (std::vector<void (*)()>::iterator I = ExtraVersionPrinters->begin(),
1392 E = ExtraVersionPrinters->end();
1393 I != E; ++I)
1394 (*I)();
1395 }
1396
Chris Lattner043b8b52009-09-20 05:48:01 +00001397 exit(1);
Reid Spencer515b5b32006-06-05 16:22:56 +00001398 }
1399};
Chris Lattner500d8bf2006-10-12 22:09:17 +00001400} // End anonymous namespace
Reid Spencer515b5b32006-06-05 16:22:56 +00001401
1402
Reid Spencer69105f32004-08-04 00:36:06 +00001403// Define the --version option that prints out the LLVM version for the tool
Chris Lattner500d8bf2006-10-12 22:09:17 +00001404static VersionPrinter VersionPrinterInstance;
1405
1406static cl::opt<VersionPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +00001407VersOp("version", cl::desc("Display the version of this program"),
Reid Spencer69105f32004-08-04 00:36:06 +00001408 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
1409
Reid Spencer9bbba0912004-11-16 06:11:52 +00001410// Utility function for printing the help message.
1411void cl::PrintHelpMessage() {
Misha Brukmanf976c852005-04-21 22:55:34 +00001412 // This looks weird, but it actually prints the help message. The
Reid Spencer5cc498f2004-11-16 06:50:36 +00001413 // NormalPrinter variable is a HelpPrinter and the help gets printed when
1414 // its operator= is invoked. That's because the "normal" usages of the
Misha Brukmanf976c852005-04-21 22:55:34 +00001415 // help printer is to be assigned true/false depending on whether the
Duncan Sands7e7ae5a2010-02-18 14:08:13 +00001416 // -help option was given or not. Since we're circumventing that we have
1417 // to make it look like -help was given, so we assign true.
Reid Spencer9bbba0912004-11-16 06:11:52 +00001418 NormalPrinter = true;
1419}
Reid Spencer515b5b32006-06-05 16:22:56 +00001420
Devang Patelaed293d2007-02-01 01:43:37 +00001421/// Utility function for printing version number.
1422void cl::PrintVersionMessage() {
1423 VersionPrinterInstance.print();
1424}
1425
Reid Spencer515b5b32006-06-05 16:22:56 +00001426void cl::SetVersionPrinter(void (*func)()) {
1427 OverrideVersionPrinter = func;
1428}
Chandler Carruth6d51d262011-07-22 07:50:40 +00001429
1430void cl::AddExtraVersionPrinter(void (*func)()) {
1431 if (ExtraVersionPrinters == 0)
1432 ExtraVersionPrinters = new std::vector<void (*)()>;
1433
1434 ExtraVersionPrinters->push_back(func);
1435}