llvm.org GIT mirror llvm / bc5df29
[GSoC] Flag value completion for clang This is patch for GSoC project, bash-completion for clang. To use this on bash, please run `source clang/utils/bash-autocomplete.sh`. bash-autocomplete.sh is code for bash-completion. In this patch, Options.td was mainly changed in order to add value class in Options.inc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305805 91177308-0d34-0410-b5e6-96231b3b80d8 Yuka Takahashi 2 years ago
9 changed file(s) with 80 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
9191 int NumArgs = 0;
9292 string HelpText = ?;
9393 string MetaVarName = ?;
94 string Values = ?;
9495 list Flags = [];
9596 OptionGroup Group = ?;
9697 Option Alias = ?;
125126 class Group { OptionGroup Group = group; }
126127 class HelpText { string HelpText = text; }
127128 class MetaVarName { string MetaVarName = name; }
129 class Values { string Values = value; }
128130
129131 // Predefined options.
130132
5252 unsigned short GroupID;
5353 unsigned short AliasID;
5454 const char *AliasArgs;
55 const char *Values;
5556 };
5657
5758 private:
118119 const char *getOptionMetaVar(OptSpecifier id) const {
119120 return getInfo(id).MetaVar;
120121 }
122
123 /// Find possible value for given flags. This is used for shell
124 /// autocompletion.
125 ///
126 /// \param [in] Option - Key flag like "-stdlib=" when "-stdlib=l"
127 /// was passed to clang.
128 ///
129 /// \param [in] Arg - Value which we want to autocomplete like "l"
130 /// when "-stdlib=l" was passed to clang.
131 ///
132 /// \return The vector of possible values.
133 std::vector suggestValueCompletions(StringRef Option,
134 StringRef Arg) const;
121135
122136 /// Find flags from OptTable which starts with Cur.
123137 ///
5656 UnknownClass,
5757 FlagClass,
5858 JoinedClass,
59 ValuesClass,
5960 SeparateClass,
6061 RemainingArgsClass,
6162 RemainingArgsJoinedClass,
154155 case CommaJoinedClass:
155156 return RenderCommaJoinedStyle;
156157 case FlagClass:
158 case ValuesClass:
157159 case SeparateClass:
158160 case MultiArgClass:
159161 case JoinedOrSeparateClass:
193193 return 0;
194194 }
195195
196 // Returns true if one of the Prefixes + In.Names matches Option
197 static bool optionMatches(const OptTable::Info &In, StringRef Option) {
198 if (In.Values && In.Prefixes)
199 for (size_t I = 0; In.Prefixes[I]; I++)
200 if (Option == std::string(In.Prefixes[I]) + In.Name)
201 return true;
202 return false;
203 }
204
205 // This function is for flag value completion.
206 // Eg. When "-stdlib=" and "l" was passed to this function, it will return
207 // appropiriate values for stdlib, which starts with l.
208 std::vector
209 OptTable::suggestValueCompletions(StringRef Option, StringRef Arg) const {
210 // Search all options and return possible values.
211 for (const Info &In : OptionInfos.slice(FirstSearchableIndex)) {
212 if (!optionMatches(In, Option))
213 continue;
214
215 SmallVector Candidates;
216 StringRef(In.Values).split(Candidates, ",", -1, false);
217
218 std::vector Result;
219 for (StringRef Val : Candidates)
220 if (Val.startswith(Arg))
221 Result.push_back(Val);
222 return Result;
223 }
224 return {};
225 }
226
196227 std::vector OptTable::findByPrefix(StringRef Cur) const {
197228 std::vector Ret;
198229 for (const Info &In : OptionInfos.slice(FirstSearchableIndex)) {
335366 case Option::FlagClass:
336367 break;
337368
369 case Option::ValuesClass:
370 break;
371
338372 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
339373 case Option::RemainingArgsClass: case Option::RemainingArgsJoinedClass:
340374 Name += ' ';
4646 P(UnknownClass);
4747 P(FlagClass);
4848 P(JoinedClass);
49 P(ValuesClass);
4950 P(SeparateClass);
5051 P(CommaJoinedClass);
5152 P(MultiArgClass);
3030
3131 enum {
3232 OPT_INVALID = 0,
33 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
33 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
3434 #include "Options.inc"
3535 #undef OPTION
3636 };
4040 #undef PREFIX
4141
4242 static const llvm::opt::OptTable::Info infoTable[] = {
43 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
44 { \
45 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
46 OPT_##GROUP, OPT_##ALIAS, X6 \
47 },
43 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10, X11) \
44 {X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, \
45 X8, X7, OPT_##GROUP, OPT_##ALIAS, X6, X11},
4846 #include "Options.inc"
4947 #undef OPTION
5048 };
3636 enum ID {
3737 OPT_INVALID = 0, // This is not an option ID.
3838 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
39 HELPTEXT, METAVAR) \
39 HELPTEXT, METAVAR, VALUES) \
4040 OPT_##ID,
4141 #include "Opts.inc"
4242 #undef OPTION
4848
4949 static const opt::OptTable::Info InfoTable[] = {
5050 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
51 HELPTEXT, METAVAR) \
51 HELPTEXT, METAVAR, VALUES) \
5252 { \
53 PREFIX, NAME, HELPTEXT, \
54 METAVAR, OPT_##ID, opt::Option::KIND##Class, \
55 PARAM, FLAGS, OPT_##GROUP, \
56 OPT_##ALIAS, ALIASARGS},
53 PREFIX, NAME, HELPTEXT, \
54 METAVAR, OPT_##ID, opt::Option::KIND##Class, \
55 PARAM, FLAGS, OPT_##GROUP, \
56 OPT_##ALIAS, ALIASARGS, VALUES},
5757 #include "Opts.inc"
5858 #undef OPTION
5959 };
1717
1818 enum ID {
1919 OPT_INVALID = 0, // This is not an option ID.
20 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
21 HELPTEXT, METAVAR) OPT_##ID,
20 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
21 HELPTEXT, METAVAR, VALUES) \
22 OPT_##ID,
2223 #include "Opts.inc"
2324 LastOption
2425 #undef OPTION
3536 };
3637
3738 static const OptTable::Info InfoTable[] = {
38 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
39 HELPTEXT, METAVAR) \
40 { PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, PARAM, \
41 FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS },
39 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
40 HELPTEXT, METAVAR, VALUES) \
41 {PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, \
42 PARAM, FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS, VALUES},
4243 #include "Opts.inc"
4344 #undef OPTION
4445 };
195195 OS << ", nullptr";
196196
197197 // The option meta-variable name (unused).
198 OS << ", nullptr";
199
200 // The option Values (unused for groups).
198201 OS << ", nullptr)\n";
199202 }
200203 OS << "\n";
284287 else
285288 OS << "nullptr";
286289
290 // The option Values. Used for shell autocompletion.
291 OS << ", ";
292 if (!isa(R.getValueInit("Values")))
293 write_cstring(OS, R.getValueAsString("Values"));
294 else
295 OS << "nullptr";
296
287297 OS << ")\n";
288298 }
289299 OS << "#endif // OPTION\n";