[PATCH] D36252: [diagtool] Add ability to get name from id

don hinton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 2 18:15:00 PDT 2017


hintonda updated this revision to Diff 109472.
hintonda added a comment.

- Use cat|xargs which should work on window -- used in other tests.


https://reviews.llvm.org/D36252

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


Index: tools/diagtool/FindDiagnosticID.cpp
===================================================================
--- tools/diagtool/FindDiagnosticID.cpp
+++ tools/diagtool/FindDiagnosticID.cpp
@@ -18,6 +18,15 @@
 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 @@
       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 @@
   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;
   }
Index: test/Misc/find-diagnostic-id.c
===================================================================
--- test/Misc/find-diagnostic-id.c
+++ test/Misc/find-diagnostic-id.c
@@ -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'


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36252.109472.patch
Type: text/x-patch
Size: 1990 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170803/f40e626d/attachment.bin>


More information about the cfe-commits mailing list