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