Alex Lorenz | ada1192 | 2017-07-12 16:41:49 +0000 | [diff] [blame^] | 1 | //===- FindDiagnosticID.cpp - diagtool tool for finding diagnostic id -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "DiagTool.h" |
| 11 | #include "DiagnosticNames.h" |
| 12 | #include "clang/Basic/AllDiagnostics.h" |
| 13 | #include "llvm/Support/CommandLine.h" |
| 14 | |
| 15 | DEF_DIAGTOOL("find-diagnostic-id", "Print the id of the given diagnostic", |
| 16 | FindDiagnosticID) |
| 17 | |
| 18 | using namespace clang; |
| 19 | using namespace diagtool; |
| 20 | |
| 21 | static Optional<DiagnosticRecord> |
| 22 | findDiagnostic(ArrayRef<DiagnosticRecord> Diagnostics, StringRef Name) { |
| 23 | for (const auto &Diag : Diagnostics) { |
| 24 | StringRef DiagName = Diag.getName(); |
| 25 | if (DiagName == Name) |
| 26 | return Diag; |
| 27 | } |
| 28 | return None; |
| 29 | } |
| 30 | |
| 31 | int FindDiagnosticID::run(unsigned int argc, char **argv, |
| 32 | llvm::raw_ostream &OS) { |
| 33 | static llvm::cl::OptionCategory FindDiagnosticIDOptions( |
| 34 | "diagtool find-diagnostic-id options"); |
| 35 | |
| 36 | static llvm::cl::opt<std::string> DiagnosticName( |
| 37 | llvm::cl::Positional, llvm::cl::desc("<diagnostic-name>"), |
| 38 | llvm::cl::Required, llvm::cl::cat(FindDiagnosticIDOptions)); |
| 39 | |
| 40 | std::vector<const char *> Args; |
| 41 | Args.push_back("find-diagnostic-id"); |
| 42 | for (const char *A : llvm::makeArrayRef(argv, argc)) |
| 43 | Args.push_back(A); |
| 44 | |
| 45 | llvm::cl::HideUnrelatedOptions(FindDiagnosticIDOptions); |
| 46 | llvm::cl::ParseCommandLineOptions((int)Args.size(), Args.data(), |
| 47 | "Diagnostic ID mapping utility"); |
| 48 | |
| 49 | ArrayRef<DiagnosticRecord> AllDiagnostics = getBuiltinDiagnosticsByName(); |
| 50 | Optional<DiagnosticRecord> Diag = |
| 51 | findDiagnostic(AllDiagnostics, DiagnosticName); |
| 52 | if (!Diag) { |
| 53 | llvm::errs() << "error: invalid diagnostic '" << DiagnosticName << "'\n"; |
| 54 | return 1; |
| 55 | } |
| 56 | OS << Diag->DiagID << "\n"; |
| 57 | return 0; |
| 58 | } |