[Lldb-commits] [lldb] r291759 - Add format_provider for the Error class

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Jan 12 03:13:25 PST 2017


Author: labath
Date: Thu Jan 12 05:13:24 2017
New Revision: 291759

URL: http://llvm.org/viewvc/llvm-project?rev=291759&view=rev
Log:
Add format_provider for the Error class

Summary:
The formatter supports the same options as the string-like classes, i.e. the
ability to truncate the displayed string. I don't anticipate it would be much
used, but it seems consistent.

Reviewers: zturner, clayborg

Subscribers: mgorny, lldb-commits

Differential Revision: https://reviews.llvm.org/D28519

Added:
    lldb/trunk/unittests/Core/ErrorTest.cpp
Modified:
    lldb/trunk/include/lldb/Core/Error.h
    lldb/trunk/unittests/Core/CMakeLists.txt

Modified: lldb/trunk/include/lldb/Core/Error.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Error.h?rev=291759&r1=291758&r2=291759&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/Error.h (original)
+++ lldb/trunk/include/lldb/Core/Error.h Thu Jan 12 05:13:24 2017
@@ -12,6 +12,7 @@
 #if defined(__cplusplus)
 
 #include "llvm/Support/DataTypes.h"
+#include "llvm/Support/FormatVariadic.h"
 
 #include <cstdarg>
 #include <cstdio>
@@ -300,5 +301,15 @@ protected:
 
 } // namespace lldb_private
 
+namespace llvm {
+template <> struct llvm::format_provider<lldb_private::Error> {
+  static void format(const lldb_private::Error &error, llvm::raw_ostream &OS,
+                     llvm::StringRef Options) {
+    llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
+                                                   Options);
+  }
+};
+}
+
 #endif // #if defined(__cplusplus)
 #endif // #ifndef __DCError_h__

Modified: lldb/trunk/unittests/Core/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/CMakeLists.txt?rev=291759&r1=291758&r2=291759&view=diff
==============================================================================
--- lldb/trunk/unittests/Core/CMakeLists.txt (original)
+++ lldb/trunk/unittests/Core/CMakeLists.txt Thu Jan 12 05:13:24 2017
@@ -2,6 +2,7 @@ add_lldb_unittest(LLDBCoreTests
   ArchSpecTest.cpp
   BroadcasterTest.cpp
   DataExtractorTest.cpp
+  ErrorTest.cpp
   ListenerTest.cpp
   ScalarTest.cpp
   StructuredDataTest.cpp

Added: lldb/trunk/unittests/Core/ErrorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/ErrorTest.cpp?rev=291759&view=auto
==============================================================================
--- lldb/trunk/unittests/Core/ErrorTest.cpp (added)
+++ lldb/trunk/unittests/Core/ErrorTest.cpp Thu Jan 12 05:13:24 2017
@@ -0,0 +1,19 @@
+//===-- ErrorTest.cpp -------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "lldb/Core/Error.h"
+
+using namespace lldb_private;
+
+TEST(ErrorTest, Formatv) {
+  EXPECT_EQ("", llvm::formatv("{0}", Error()).str());
+  EXPECT_EQ("Hello Error", llvm::formatv("{0}", Error("Hello Error")).str());
+  EXPECT_EQ("Hello", llvm::formatv("{0:5}", Error("Hello Error")).str());
+}




More information about the lldb-commits mailing list