Skip to content

Commit d41c2ae

Browse files
committed
Address review comments.
1 parent a480c0c commit d41c2ae

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

llvm/examples/OptSubcommand/llvm-hello-sub.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ static constexpr OptTable::Info InfoTable[] = {
4444
class HelloSubOptTable : public GenericOptTable {
4545
public:
4646
HelloSubOptTable()
47-
: GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable, false,
48-
OptionCommands, OptionCommandIDsTable) {}
47+
: GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable,
48+
/*IgnoreCase=*/false, OptionCommands,
49+
OptionCommandIDsTable) {}
4950
};
5051
} // namespace
5152

@@ -97,9 +98,8 @@ int main(int argc, char **argv) {
9798
return 1;
9899
}
99100
if (Subcommand.empty()) {
100-
if (Args.hasArg(OPT_version)) {
101+
if (Args.hasArg(OPT_version))
101102
llvm::outs() << "LLVM Hello Subcommand Example 1.0\n";
102-
}
103103
} else if (Subcommand == "foo") {
104104
if (Args.hasArg(OPT_uppercase))
105105
llvm::outs() << "FOO\n";

llvm/include/llvm/Option/OptTable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class Visibility {
4545
operator unsigned() const { return Mask; }
4646
};
4747

48+
inline constexpr StringRef TopLevelCommandName = "TopLevelCommand";
49+
4850
/// Provide access to the Option info table.
4951
///
5052
/// The OptTable class provides a layer of indirection which allows Option

llvm/lib/Option/ArgList.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ StringRef ArgList::getSubcommand(
218218

219219
size_t OldSize = SubCommands.size();
220220
for (const OptTable::Command &CMD : Commands) {
221-
if (StringRef(CMD.Name) == "TopLevelCommand")
221+
if (StringRef(CMD.Name) == opt::TopLevelCommandName)
222222
continue;
223223
if (StringRef(CMD.Name) == A->getValue())
224224
SubCommands.push_back(A->getValue());
@@ -227,12 +227,12 @@ StringRef ArgList::getSubcommand(
227227
if (SubCommands.size() == OldSize)
228228
OtherPositionals.push_back(A->getValue());
229229
}
230-
if (SubCommands.size() > 1) {
230+
// Invoke callbacks if necessary.
231+
if (SubCommands.size() > 1)
231232
HandleMultipleSubcommands(SubCommands);
232-
}
233-
if (!OtherPositionals.empty()) {
233+
if (!OtherPositionals.empty())
234234
HandleOtherPositionals(OtherPositionals);
235-
}
235+
236236
if (SubCommands.size() == 1)
237237
return SubCommands.front();
238238
return {}; // No valid usage of subcommand found.

llvm/lib/Option/OptTable.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ std::unique_ptr<Arg> OptTable::parseOneArgGrouped(InputArgList &Args,
418418

419419
std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
420420
Visibility VisibilityMask) const {
421-
return internalParseOneArg(Args, Index, nullptr,
421+
return internalParseOneArg(Args, Index, /*ActiveCommand=*/nullptr,
422422
[VisibilityMask](const Option &Opt) {
423423
return !Opt.hasVisibilityFlag(VisibilityMask);
424424
});
@@ -428,7 +428,7 @@ std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
428428
unsigned FlagsToInclude,
429429
unsigned FlagsToExclude) const {
430430
return internalParseOneArg(
431-
Args, Index, nullptr,
431+
Args, Index, /*ActiveCommand=*/nullptr,
432432
[FlagsToInclude, FlagsToExclude](const Option &Opt) {
433433
if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
434434
return true;
@@ -766,7 +766,7 @@ void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
766766
bool ShowHidden = !(FlagsToExclude & HelpHidden);
767767
FlagsToExclude &= ~HelpHidden;
768768
return internalPrintHelp(
769-
OS, Usage, Title, {}, ShowHidden, ShowAllAliases,
769+
OS, Usage, Title, /*Subcommand=*/{}, ShowHidden, ShowAllAliases,
770770
[FlagsToInclude, FlagsToExclude](const Info &CandidateInfo) {
771771
if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
772772
return true;
@@ -800,7 +800,7 @@ void OptTable::internalPrintHelp(
800800
// This loop prints subcommands list and sets ActiveCommand to
801801
// TopLevelCommand while iterating over all commands.
802802
for (const auto &C : Commands) {
803-
if (StringRef(C.Name) == "TopLevelCommand") {
803+
if (StringRef(C.Name) == opt::TopLevelCommandName) {
804804
ActiveCommand = &C;
805805
continue;
806806
}

llvm/utils/TableGen/OptionParserEmitter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/ADT/SmallVector.h"
1313
#include "llvm/ADT/StringExtras.h"
1414
#include "llvm/ADT/Twine.h"
15+
#include "llvm/Option/OptTable.h"
1516
#include "llvm/Support/InterleavedRange.h"
1617
#include "llvm/Support/raw_ostream.h"
1718
#include "llvm/TableGen/Record.h"
@@ -263,7 +264,7 @@ static void emitOptionParser(const RecordKeeper &Records, raw_ostream &OS) {
263264
Records.getAllDerivedDefinitions("Command");
264265
// TopLevelCommand should come first.
265266
std::stable_partition(Commands.begin(), Commands.end(), [](const Record *R) {
266-
return R->getName() == "TopLevelCommand";
267+
return R->getName() == opt::TopLevelCommandName;
267268
});
268269

269270
emitSourceFileHeader("Option Parsing Definitions", OS);

0 commit comments

Comments
 (0)