Skip to content

Commit baa7f42

Browse files
committed
[diagtool] Add ability to pass in the id and return the name for a
particular diagnostic. Differential Revision: https://reviews.llvm.org/D36252 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@309955 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 71ca9f8 commit baa7f42

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

test/Misc/find-diagnostic-id.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// RUN: diagtool find-diagnostic-id warn_unused_variable | FileCheck %s
1+
// RUN: diagtool find-diagnostic-id warn_unused_variable > %t; FileCheck %s < %t
2+
// RUN: cat %t | xargs diagtool find-diagnostic-id | FileCheck %s --check-prefix=INVERSE
23
// RUN: not diagtool find-diagnostic-id warn_unused_vars 2>&1 | FileCheck --check-prefix=ERROR %s
34

45
// CHECK: {{^[0-9]+$}}
6+
// INVERSE: warn_unused_variable
57
// ERROR: error: invalid diagnostic 'warn_unused_vars'

tools/diagtool/FindDiagnosticID.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ DEF_DIAGTOOL("find-diagnostic-id", "Print the id of the given diagnostic",
1818
using namespace clang;
1919
using namespace diagtool;
2020

21+
static StringRef getNameFromID(StringRef Name) {
22+
int DiagID;
23+
if(!Name.getAsInteger(0, DiagID)) {
24+
const DiagnosticRecord &Diag = getDiagnosticForID(DiagID);
25+
return Diag.getName();
26+
}
27+
return StringRef();
28+
}
29+
2130
static Optional<DiagnosticRecord>
2231
findDiagnostic(ArrayRef<DiagnosticRecord> Diagnostics, StringRef Name) {
2332
for (const auto &Diag : Diagnostics) {
@@ -38,7 +47,7 @@ int FindDiagnosticID::run(unsigned int argc, char **argv,
3847
llvm::cl::Required, llvm::cl::cat(FindDiagnosticIDOptions));
3948

4049
std::vector<const char *> Args;
41-
Args.push_back("find-diagnostic-id");
50+
Args.push_back("diagtool find-diagnostic-id");
4251
for (const char *A : llvm::makeArrayRef(argv, argc))
4352
Args.push_back(A);
4453

@@ -50,6 +59,13 @@ int FindDiagnosticID::run(unsigned int argc, char **argv,
5059
Optional<DiagnosticRecord> Diag =
5160
findDiagnostic(AllDiagnostics, DiagnosticName);
5261
if (!Diag) {
62+
// Name to id failed, so try id to name.
63+
auto Name = getNameFromID(DiagnosticName);
64+
if (!Name.empty()) {
65+
OS << Name << '\n';
66+
return 0;
67+
}
68+
5369
llvm::errs() << "error: invalid diagnostic '" << DiagnosticName << "'\n";
5470
return 1;
5571
}

0 commit comments

Comments
 (0)