diff options
author | Mingming Liu <mingmingl@google.com> | 2023-11-12 23:31:31 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-12 23:31:31 -0800 |
commit | 38b34c61e028751b6778493d6185d07a8af1a3b5 (patch) | |
tree | 1d0a3dc83b4fbbd675d47fab7ef3fe04ba3b6bdf /llvm/lib/Support/CommandLine.cpp | |
parent | 58bb2d19560471ad94dea505f2283bad9d7c2850 (diff) | |
download | llvm-38b34c61e028751b6778493d6185d07a8af1a3b5.zip llvm-38b34c61e028751b6778493d6185d07a8af1a3b5.tar.gz llvm-38b34c61e028751b6778493d6185d07a8af1a3b5.tar.bz2 |
Reland "[Support]Look up in top-level subcommand as a fallback when looking options for a custom subcommand (#71981)
Fixed build bot errors.
- Use `StackOption<std::string>` type for the top level option. This
way, a per test-case option is unregistered when destructor of
`StackOption` cleans up state for subsequent test cases.
- Repro the crash with no test sharding `/usr/bin/python3
/path/to/llvm-project/build/./bin/llvm-lit -vv --no-gtest-sharding -j128
/path/to/llvm-project/llvm/test/Unit`. The crash is gone with the fix
(same no-sharding repro)
**Original commit message:**
**Context:**
- In https://lists.llvm.org/pipermail/llvm-dev/2016-June/101804.html and
commit
https://github.com/llvm/llvm-project/commit/07670b3e984db32f291373fe12c392959f2aff67,
`cl::SubCommand` is introduced.
- Options that don't specify subcommand goes into a special 'top level'
subcommand.
**Motivating Use Case:**
- The motivating use case is to refactor `llvm-profdata` to use
`cl::SubCommand` to organize subcommands. See
https://github.com/llvm/llvm-project/pull/71328. A valid use case that's
not supported before this patch is shown below
```
// show-option{1,2} are associated with 'show' subcommand.
// top-level-option3 is in top-level subcomand (e.g., `profile-isfs` in SampleProfReader.cpp)
llvm-profdata show --show-option1 --show-option2 --top-level-option3
```
- Before this patch, option handler look-up will fail with the following
error message "Unknown command line argument --top-level-option3".
- After this patch, option handler look-up will look up in sub-command
options first, and use top-level subcommand as a fallback, so
'top-level-option3' is parsed correctly.
Diffstat (limited to 'llvm/lib/Support/CommandLine.cpp')
-rw-r--r-- | llvm/lib/Support/CommandLine.cpp | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp index 55633d7..a7e0cae 100644 --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -1667,6 +1667,13 @@ bool CommandLineParser::ParseCommandLineOptions(int argc, Handler = LookupLongOption(*ChosenSubCommand, ArgName, Value, LongOptionsUseDoubleDash, HaveDoubleDash); + // If Handler is not found in a specialized subcommand, look up handler + // in the top-level subcommand. + // cl::opt without cl::sub belongs to top-level subcommand. + if (!Handler && ChosenSubCommand != &SubCommand::getTopLevel()) + Handler = LookupLongOption(SubCommand::getTopLevel(), ArgName, Value, + LongOptionsUseDoubleDash, HaveDoubleDash); + // Check to see if this "option" is really a prefixed or grouped argument. if (!Handler && !(LongOptionsUseDoubleDash && HaveDoubleDash)) Handler = HandlePrefixedOrGroupedOption(ArgName, Value, ErrorParsing, |