Coverage Report

Created: 2022-01-18 06:27

/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
156k
void OptionParser::Prepare(std::unique_lock<std::mutex> &lock) {
19
156k
  static std::mutex g_mutex;
20
156k
  lock = std::unique_lock<std::mutex>(g_mutex);
21
#ifdef __GLIBC__
22
  optind = 0;
23
#else
24
156k
  optreset = 1;
25
156k
  optind = 1;
26
156k
#endif
27
156k
}
28
29
135
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
287k
                        int *longindex) {
34
287k
  std::vector<option> opts;
35
4.92M
  while (longopts->definition != nullptr) {
36
4.63M
    option opt;
37
4.63M
    opt.flag = longopts->flag;
38
4.63M
    opt.val = longopts->val;
39
4.63M
    opt.name = longopts->definition->long_option;
40
4.63M
    opt.has_arg = longopts->definition->option_has_arg;
41
4.63M
    opts.push_back(opt);
42
4.63M
    ++longopts;
43
4.63M
  }
44
287k
  opts.push_back(option());
45
287k
  std::string opt_cstr = std::string(optstring);
46
287k
  return getopt_long_only(argv.size() - 1, argv.data(), opt_cstr.c_str(),
47
287k
                          &opts[0], longindex);
48
287k
}
49
50
207k
char *OptionParser::GetOptionArgument() { return optarg; }
51
52
85.1k
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
}