clang API Documentation
00001 //===--- OptTable.cpp - Option Table Implementation ---------------------*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "clang/Driver/OptTable.h" 00011 #include "clang/Driver/Arg.h" 00012 #include "clang/Driver/ArgList.h" 00013 #include "clang/Driver/Option.h" 00014 #include "llvm/Support/raw_ostream.h" 00015 #include <algorithm> 00016 #include <cassert> 00017 #include <map> 00018 using namespace clang::driver; 00019 using namespace clang::driver::options; 00020 00021 // Ordering on Info. The ordering is *almost* lexicographic, with two 00022 // exceptions. First, '\0' comes at the end of the alphabet instead of 00023 // the beginning (thus options preceed any other options which prefix 00024 // them). Second, for options with the same name, the less permissive 00025 // version should come first; a Flag option should preceed a Joined 00026 // option, for example. 00027 00028 static int StrCmpOptionName(const char *A, const char *B) { 00029 char a = *A, b = *B; 00030 while (a == b) { 00031 if (a == '\0') 00032 return 0; 00033 00034 a = *++A; 00035 b = *++B; 00036 } 00037 00038 if (a == '\0') // A is a prefix of B. 00039 return 1; 00040 if (b == '\0') // B is a prefix of A. 00041 return -1; 00042 00043 // Otherwise lexicographic. 00044 return (a < b) ? -1 : 1; 00045 } 00046 00047 namespace clang { 00048 namespace driver { 00049 static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { 00050 if (&A == &B) 00051 return false; 00052 00053 if (int N = StrCmpOptionName(A.Name, B.Name)) 00054 return N == -1; 00055 00056 // Names are the same, check that classes are in order; exactly one 00057 // should be joined, and it should succeed the other. 00058 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) && 00059 "Unexpected classes for options with same name."); 00060 return B.Kind == Option::JoinedClass; 00061 } 00062 00063 // Support lower_bound between info and an option name. 00064 static inline bool operator<(const OptTable::Info &I, const char *Name) { 00065 return StrCmpOptionName(I.Name, Name) == -1; 00066 } 00067 static inline bool operator<(const char *Name, const OptTable::Info &I) { 00068 return StrCmpOptionName(Name, I.Name) == -1; 00069 } 00070 } 00071 } 00072 00073 // 00074 00075 OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {} 00076 00077 // 00078 00079 OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos) 00080 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos), 00081 Options(new Option*[NumOptionInfos]), 00082 TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0) 00083 { 00084 // Explicitly zero initialize the error to work around a bug in array 00085 // value-initialization on MinGW with gcc 4.3.5. 00086 memset(Options, 0, sizeof(*Options) * NumOptionInfos); 00087 00088 // Find start of normal options. 00089 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { 00090 unsigned Kind = getInfo(i + 1).Kind; 00091 if (Kind == Option::InputClass) { 00092 assert(!TheInputOption && "Cannot have multiple input options!"); 00093 TheInputOption = getOption(i + 1); 00094 } else if (Kind == Option::UnknownClass) { 00095 assert(!TheUnknownOption && "Cannot have multiple input options!"); 00096 TheUnknownOption = getOption(i + 1); 00097 } else if (Kind != Option::GroupClass) { 00098 FirstSearchableIndex = i; 00099 break; 00100 } 00101 } 00102 assert(FirstSearchableIndex != 0 && "No searchable options?"); 00103 00104 #ifndef NDEBUG 00105 // Check that everything after the first searchable option is a 00106 // regular option class. 00107 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) { 00108 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind; 00109 assert((Kind != Option::InputClass && Kind != Option::UnknownClass && 00110 Kind != Option::GroupClass) && 00111 "Special options should be defined first!"); 00112 } 00113 00114 // Check that options are in order. 00115 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) { 00116 if (!(getInfo(i) < getInfo(i + 1))) { 00117 getOption(i)->dump(); 00118 getOption(i + 1)->dump(); 00119 assert(0 && "Options are not in order!"); 00120 } 00121 } 00122 #endif 00123 } 00124 00125 OptTable::~OptTable() { 00126 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) 00127 delete Options[i]; 00128 delete[] Options; 00129 } 00130 00131 Option *OptTable::CreateOption(unsigned id) const { 00132 const Info &info = getInfo(id); 00133 const OptionGroup *Group = 00134 cast_or_null<OptionGroup>(getOption(info.GroupID)); 00135 const Option *Alias = getOption(info.AliasID); 00136 00137 Option *Opt = 0; 00138 switch (info.Kind) { 00139 case Option::InputClass: 00140 Opt = new InputOption(id); break; 00141 case Option::UnknownClass: 00142 Opt = new UnknownOption(id); break; 00143 case Option::GroupClass: 00144 Opt = new OptionGroup(id, info.Name, Group); break; 00145 case Option::FlagClass: 00146 Opt = new FlagOption(id, info.Name, Group, Alias); break; 00147 case Option::JoinedClass: 00148 Opt = new JoinedOption(id, info.Name, Group, Alias); break; 00149 case Option::SeparateClass: 00150 Opt = new SeparateOption(id, info.Name, Group, Alias); break; 00151 case Option::CommaJoinedClass: 00152 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break; 00153 case Option::MultiArgClass: 00154 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break; 00155 case Option::JoinedOrSeparateClass: 00156 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break; 00157 case Option::JoinedAndSeparateClass: 00158 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break; 00159 } 00160 00161 if (info.Flags & DriverOption) 00162 Opt->setDriverOption(true); 00163 if (info.Flags & LinkerInput) 00164 Opt->setLinkerInput(true); 00165 if (info.Flags & NoArgumentUnused) 00166 Opt->setNoArgumentUnused(true); 00167 if (info.Flags & RenderAsInput) 00168 Opt->setNoOptAsInput(true); 00169 if (info.Flags & RenderJoined) { 00170 assert((info.Kind == Option::JoinedOrSeparateClass || 00171 info.Kind == Option::SeparateClass) && "Invalid option."); 00172 Opt->setForceJoinedRender(true); 00173 } 00174 if (info.Flags & RenderSeparate) { 00175 assert((info.Kind == Option::JoinedOrSeparateClass || 00176 info.Kind == Option::JoinedClass) && "Invalid option."); 00177 Opt->setForceSeparateRender(true); 00178 } 00179 if (info.Flags & Unsupported) 00180 Opt->setUnsupported(true); 00181 00182 return Opt; 00183 } 00184 00185 Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const { 00186 unsigned Prev = Index; 00187 const char *Str = Args.getArgString(Index); 00188 00189 // Anything that doesn't start with '-' is an input, as is '-' itself. 00190 if (Str[0] != '-' || Str[1] == '\0') 00191 return new PositionalArg(TheInputOption, Index++); 00192 00193 const Info *Start = OptionInfos + FirstSearchableIndex; 00194 const Info *End = OptionInfos + getNumOptions(); 00195 00196 // Search for the first next option which could be a prefix. 00197 Start = std::lower_bound(Start, End, Str); 00198 00199 // Options are stored in sorted order, with '\0' at the end of the 00200 // alphabet. Since the only options which can accept a string must 00201 // prefix it, we iteratively search for the next option which could 00202 // be a prefix. 00203 // 00204 // FIXME: This is searching much more than necessary, but I am 00205 // blanking on the simplest way to make it fast. We can solve this 00206 // problem when we move to TableGen. 00207 for (; Start != End; ++Start) { 00208 // Scan for first option which is a proper prefix. 00209 for (; Start != End; ++Start) 00210 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0) 00211 break; 00212 if (Start == End) 00213 break; 00214 00215 // See if this option matches. 00216 if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index)) 00217 return A; 00218 00219 // Otherwise, see if this argument was missing values. 00220 if (Prev != Index) 00221 return 0; 00222 } 00223 00224 return new PositionalArg(TheUnknownOption, Index++); 00225 } 00226 00227 InputArgList *OptTable::ParseArgs(const char **ArgBegin, const char **ArgEnd, 00228 unsigned &MissingArgIndex, 00229 unsigned &MissingArgCount) const { 00230 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd); 00231 00232 // FIXME: Handle '@' args (or at least error on them). 00233 00234 MissingArgIndex = MissingArgCount = 0; 00235 unsigned Index = 0, End = ArgEnd - ArgBegin; 00236 while (Index < End) { 00237 // Ignore empty arguments (other things may still take them as arguments). 00238 if (Args->getArgString(Index)[0] == '\0') { 00239 ++Index; 00240 continue; 00241 } 00242 00243 unsigned Prev = Index; 00244 Arg *A = ParseOneArg(*Args, Index); 00245 assert(Index > Prev && "Parser failed to consume argument."); 00246 00247 // Check for missing argument error. 00248 if (!A) { 00249 assert(Index >= End && "Unexpected parser error."); 00250 assert(Index - Prev - 1 && "No missing arguments!"); 00251 MissingArgIndex = Prev; 00252 MissingArgCount = Index - Prev - 1; 00253 break; 00254 } 00255 00256 Args->append(A); 00257 } 00258 00259 return Args; 00260 } 00261 00262 static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) { 00263 std::string Name = Opts.getOptionName(Id); 00264 00265 // Add metavar, if used. 00266 switch (Opts.getOptionKind(Id)) { 00267 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: 00268 assert(0 && "Invalid option with help text."); 00269 00270 case Option::MultiArgClass: case Option::JoinedAndSeparateClass: 00271 assert(0 && "Cannot print metavar for this kind of option."); 00272 00273 case Option::FlagClass: 00274 break; 00275 00276 case Option::SeparateClass: case Option::JoinedOrSeparateClass: 00277 Name += ' '; 00278 // FALLTHROUGH 00279 case Option::JoinedClass: case Option::CommaJoinedClass: 00280 if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) 00281 Name += MetaVarName; 00282 else 00283 Name += "<value>"; 00284 break; 00285 } 00286 00287 return Name; 00288 } 00289 00290 static void PrintHelpOptionList(llvm::raw_ostream &OS, llvm::StringRef Title, 00291 std::vector<std::pair<std::string, 00292 const char*> > &OptionHelp) { 00293 OS << Title << ":\n"; 00294 00295 // Find the maximum option length. 00296 unsigned OptionFieldWidth = 0; 00297 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 00298 // Skip titles. 00299 if (!OptionHelp[i].second) 00300 continue; 00301 00302 // Limit the amount of padding we are willing to give up for alignment. 00303 unsigned Length = OptionHelp[i].first.size(); 00304 if (Length <= 23) 00305 OptionFieldWidth = std::max(OptionFieldWidth, Length); 00306 } 00307 00308 const unsigned InitialPad = 2; 00309 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 00310 const std::string &Option = OptionHelp[i].first; 00311 int Pad = OptionFieldWidth - int(Option.size()); 00312 OS.indent(InitialPad) << Option; 00313 00314 // Break on long option names. 00315 if (Pad < 0) { 00316 OS << "\n"; 00317 Pad = OptionFieldWidth + InitialPad; 00318 } 00319 OS.indent(Pad + 1) << OptionHelp[i].second << '\n'; 00320 } 00321 } 00322 00323 static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) { 00324 unsigned GroupID = Opts.getOptionGroupID(Id); 00325 00326 // If not in a group, return the default help group. 00327 if (!GroupID) 00328 return "OPTIONS"; 00329 00330 // Abuse the help text of the option groups to store the "help group" 00331 // name. 00332 // 00333 // FIXME: Split out option groups. 00334 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID)) 00335 return GroupHelp; 00336 00337 // Otherwise keep looking. 00338 return getOptionHelpGroup(Opts, GroupID); 00339 } 00340 00341 void OptTable::PrintHelp(llvm::raw_ostream &OS, const char *Name, 00342 const char *Title, bool ShowHidden) const { 00343 OS << "OVERVIEW: " << Title << "\n"; 00344 OS << '\n'; 00345 OS << "USAGE: " << Name << " [options] <inputs>\n"; 00346 OS << '\n'; 00347 00348 // Render help text into a map of group-name to a list of (option, help) 00349 // pairs. 00350 typedef std::map<std::string, 00351 std::vector<std::pair<std::string, const char*> > > helpmap_ty; 00352 helpmap_ty GroupedOptionHelp; 00353 00354 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { 00355 unsigned Id = i + 1; 00356 00357 // FIXME: Split out option groups. 00358 if (getOptionKind(Id) == Option::GroupClass) 00359 continue; 00360 00361 if (!ShowHidden && isOptionHelpHidden(Id)) 00362 continue; 00363 00364 if (const char *Text = getOptionHelpText(Id)) { 00365 const char *HelpGroup = getOptionHelpGroup(*this, Id); 00366 const std::string &OptName = getOptionHelpName(*this, Id); 00367 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text)); 00368 } 00369 } 00370 00371 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(), 00372 ie = GroupedOptionHelp.end(); it != ie; ++it) { 00373 if (it != GroupedOptionHelp .begin()) 00374 OS << "\n"; 00375 PrintHelpOptionList(OS, it->first, it->second); 00376 } 00377 00378 OS.flush(); 00379 }