Coverage Report

Created: 2020-11-24 06:42

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/diagtool/FindDiagnosticID.cpp
Line
Count
Source
1
//===- FindDiagnosticID.cpp - diagtool tool for finding diagnostic id -----===//
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 "DiagTool.h"
10
#include "DiagnosticNames.h"
11
#include "clang/Basic/AllDiagnostics.h"
12
#include "llvm/Support/CommandLine.h"
13
14
DEF_DIAGTOOL("find-diagnostic-id", "Print the id of the given diagnostic",
15
             FindDiagnosticID)
16
17
using namespace clang;
18
using namespace diagtool;
19
20
2
static StringRef getNameFromID(StringRef Name) {
21
2
  int DiagID;
22
2
  if(!Name.getAsInteger(0, DiagID)) {
23
1
    const DiagnosticRecord &Diag = getDiagnosticForID(DiagID);
24
1
    return Diag.getName();
25
1
  }
26
1
  return StringRef();
27
1
}
28
29
static Optional<DiagnosticRecord>
30
3
findDiagnostic(ArrayRef<DiagnosticRecord> Diagnostics, StringRef Name) {
31
16.7k
  for (const auto &Diag : Diagnostics) {
32
16.7k
    StringRef DiagName = Diag.getName();
33
16.7k
    if (DiagName == Name)
34
1
      return Diag;
35
16.7k
  }
36
2
  return None;
37
3
}
38
39
int FindDiagnosticID::run(unsigned int argc, char **argv,
40
3
                          llvm::raw_ostream &OS) {
41
3
  static llvm::cl::OptionCategory FindDiagnosticIDOptions(
42
3
      "diagtool find-diagnostic-id options");
43
44
3
  static llvm::cl::opt<std::string> DiagnosticName(
45
3
      llvm::cl::Positional, llvm::cl::desc("<diagnostic-name>"),
46
3
      llvm::cl::Required, llvm::cl::cat(FindDiagnosticIDOptions));
47
48
3
  std::vector<const char *> Args;
49
3
  Args.push_back("diagtool find-diagnostic-id");
50
3
  for (const char *A : llvm::makeArrayRef(argv, argc))
51
3
    Args.push_back(A);
52
53
3
  llvm::cl::HideUnrelatedOptions(FindDiagnosticIDOptions);
54
3
  llvm::cl::ParseCommandLineOptions((int)Args.size(), Args.data(),
55
3
                                    "Diagnostic ID mapping utility");
56
57
3
  ArrayRef<DiagnosticRecord> AllDiagnostics = getBuiltinDiagnosticsByName();
58
3
  Optional<DiagnosticRecord> Diag =
59
3
      findDiagnostic(AllDiagnostics, DiagnosticName);
60
3
  if (!Diag) {
61
    // Name to id failed, so try id to name.
62
2
    auto Name = getNameFromID(DiagnosticName);
63
2
    if (!Name.empty()) {
64
1
      OS << Name << '\n';
65
1
      return 0;
66
1
    }
67
68
1
    llvm::errs() << "error: invalid diagnostic '" << DiagnosticName << "'\n";
69
1
    return 1;
70
1
  }
71
1
  OS << Diag->DiagID << "\n";
72
1
  return 0;
73
1
}