/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/common/OptionParser.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- source/Host/common/OptionParser.cpp -------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "lldb/Host/OptionParser.h" |
10 | | #include "lldb/Host/HostGetOpt.h" |
11 | | #include "lldb/Utility/OptionDefinition.h" |
12 | | #include "lldb/lldb-private-types.h" |
13 | | |
14 | | #include <vector> |
15 | | |
16 | | using namespace lldb_private; |
17 | | |
18 | 150k | void OptionParser::Prepare(std::unique_lock<std::mutex> &lock) { |
19 | 150k | static std::mutex g_mutex; |
20 | 150k | lock = std::unique_lock<std::mutex>(g_mutex); |
21 | | #ifdef __GLIBC__ |
22 | | optind = 0; |
23 | | #else |
24 | 150k | optreset = 1; |
25 | 150k | optind = 1; |
26 | 150k | #endif |
27 | 150k | } |
28 | | |
29 | 150 | void OptionParser::EnableError(bool error) { opterr = error ? 10 : 0; } |
30 | | |
31 | | int OptionParser::Parse(llvm::MutableArrayRef<char *> argv, |
32 | | llvm::StringRef optstring, const Option *longopts, |
33 | 281k | int *longindex) { |
34 | 281k | std::vector<option> opts; |
35 | 4.96M | while (longopts->definition != nullptr) { |
36 | 4.67M | option opt; |
37 | 4.67M | opt.flag = longopts->flag; |
38 | 4.67M | opt.val = longopts->val; |
39 | 4.67M | opt.name = longopts->definition->long_option; |
40 | 4.67M | opt.has_arg = longopts->definition->option_has_arg; |
41 | 4.67M | opts.push_back(opt); |
42 | 4.67M | ++longopts; |
43 | 4.67M | } |
44 | 281k | opts.push_back(option()); |
45 | 281k | std::string opt_cstr = std::string(optstring); |
46 | 281k | return getopt_long_only(argv.size() - 1, argv.data(), opt_cstr.c_str(), |
47 | 281k | &opts[0], longindex); |
48 | 281k | } |
49 | | |
50 | 211k | char *OptionParser::GetOptionArgument() { return optarg; } |
51 | | |
52 | 77.4k | int OptionParser::GetOptionIndex() { return optind; } |
53 | | |
54 | 5 | int OptionParser::GetOptionErrorCause() { return optopt; } |
55 | | |
56 | 1 | std::string OptionParser::GetShortOptionString(struct option *long_options) { |
57 | 1 | std::string s; |
58 | 1 | int i = 0; |
59 | 1 | bool done = false; |
60 | 13 | while (!done) { |
61 | 12 | if (long_options[i].name == nullptr && long_options[i].has_arg == 01 && |
62 | 12 | long_options[i].flag == nullptr1 && long_options[i].val == 01 ) { |
63 | 1 | done = true; |
64 | 11 | } else { |
65 | 11 | if (long_options[i].flag == nullptr && isalpha(long_options[i].val)8 ) { |
66 | 8 | s.append(1, (char)long_options[i].val); |
67 | 8 | switch (long_options[i].has_arg) { |
68 | 0 | default: |
69 | 0 | case no_argument: |
70 | 0 | break; |
71 | | |
72 | 0 | case optional_argument: |
73 | 0 | s.append(2, ':'); |
74 | 0 | break; |
75 | 8 | case required_argument: |
76 | 8 | s.append(1, ':'); |
77 | 8 | break; |
78 | 8 | } |
79 | 8 | } |
80 | 11 | ++i; |
81 | 11 | } |
82 | 12 | } |
83 | 1 | return s; |
84 | 1 | } |