]> granicus.if.org Git - clang/commitdiff
[diagtool] Add ability to pass in the id and return the name for a
authorDon Hinton <hintonda@gmail.com>
Thu, 3 Aug 2017 16:13:13 +0000 (16:13 +0000)
committerDon Hinton <hintonda@gmail.com>
Thu, 3 Aug 2017 16:13:13 +0000 (16:13 +0000)
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

test/Misc/find-diagnostic-id.c
tools/diagtool/FindDiagnosticID.cpp

index bef66178f96f20da0ddd3e1f2cc68875cfb6d678..1cddde747f7dbf3bcaaa53bf54bc9096c3c8ba79 100644 (file)
@@ -1,5 +1,7 @@
-// RUN: diagtool find-diagnostic-id warn_unused_variable | FileCheck %s
+// RUN: diagtool find-diagnostic-id warn_unused_variable > %t; FileCheck %s < %t
+// RUN: cat %t | xargs diagtool find-diagnostic-id | FileCheck %s --check-prefix=INVERSE
 // RUN: not diagtool find-diagnostic-id warn_unused_vars 2>&1 | FileCheck --check-prefix=ERROR %s
 
 // CHECK: {{^[0-9]+$}}
+// INVERSE: warn_unused_variable
 // ERROR: error: invalid diagnostic 'warn_unused_vars'
index 167b9925eedc0fa40fda23b74465cec324f1cb04..db6fe5e472a9ff38f9a14ee94d84cc54861faf93 100644 (file)
@@ -18,6 +18,15 @@ DEF_DIAGTOOL("find-diagnostic-id", "Print the id of the given diagnostic",
 using namespace clang;
 using namespace diagtool;
 
+static StringRef getNameFromID(StringRef Name) {
+  int DiagID;
+  if(!Name.getAsInteger(0, DiagID)) {
+    const DiagnosticRecord &Diag = getDiagnosticForID(DiagID);
+    return Diag.getName();
+  }
+  return StringRef();
+}
+
 static Optional<DiagnosticRecord>
 findDiagnostic(ArrayRef<DiagnosticRecord> Diagnostics, StringRef Name) {
   for (const auto &Diag : Diagnostics) {
@@ -38,7 +47,7 @@ int FindDiagnosticID::run(unsigned int argc, char **argv,
       llvm::cl::Required, llvm::cl::cat(FindDiagnosticIDOptions));
 
   std::vector<const char *> Args;
-  Args.push_back("find-diagnostic-id");
+  Args.push_back("diagtool find-diagnostic-id");
   for (const char *A : llvm::makeArrayRef(argv, argc))
     Args.push_back(A);
 
@@ -50,6 +59,13 @@ int FindDiagnosticID::run(unsigned int argc, char **argv,
   Optional<DiagnosticRecord> Diag =
       findDiagnostic(AllDiagnostics, DiagnosticName);
   if (!Diag) {
+    // Name to id failed, so try id to name.
+    auto Name = getNameFromID(DiagnosticName);
+    if (!Name.empty()) {
+      OS << Name << '\n';
+      return 0;
+    }
+
     llvm::errs() << "error: invalid diagnostic '" << DiagnosticName << "'\n";
     return 1;
   }