[Lldb-commits] [lldb] r296592 - Modernize Enable/DisableLogChannel interface a bit

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Mar 1 02:08:41 PST 2017


Author: labath
Date: Wed Mar  1 04:08:40 2017
New Revision: 296592

URL: http://llvm.org/viewvc/llvm-project?rev=296592&view=rev
Log:
Modernize Enable/DisableLogChannel interface a bit

Summary:
Use StringRef and ArrayRef where possible. This adds an accessor to the
Args class to get a view of the arguments as ArrayRef<const char *>.

Reviewers: zturner

Subscribers: lldb-commits

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

Modified:
    lldb/trunk/include/lldb/Core/Debugger.h
    lldb/trunk/include/lldb/Core/Log.h
    lldb/trunk/include/lldb/Interpreter/Args.h
    lldb/trunk/source/API/SBDebugger.cpp
    lldb/trunk/source/Commands/CommandObjectLog.cpp
    lldb/trunk/source/Core/Debugger.cpp
    lldb/trunk/source/Core/Log.cpp
    lldb/trunk/tools/lldb-server/LLDBServerUtilities.cpp
    lldb/trunk/unittests/Core/LogTest.cpp
    lldb/trunk/unittests/Interpreter/TestArgs.cpp

Modified: lldb/trunk/include/lldb/Core/Debugger.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Debugger.h?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/Debugger.h (original)
+++ lldb/trunk/include/lldb/Core/Debugger.h Wed Mar  1 04:08:40 2017
@@ -192,8 +192,9 @@ public:
 
   void SetCloseInputOnEOF(bool b);
 
-  bool EnableLog(const char *channel, const char **categories,
-                 const char *log_file, uint32_t log_options,
+  bool EnableLog(llvm::StringRef channel,
+                 llvm::ArrayRef<const char *> categories,
+                 llvm::StringRef log_file, uint32_t log_options,
                  Stream &error_stream);
 
   void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);

Modified: lldb/trunk/include/lldb/Core/Log.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Log.h?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/Log.h (original)
+++ lldb/trunk/include/lldb/Core/Log.h Wed Mar  1 04:08:40 2017
@@ -100,10 +100,12 @@ public:
   static bool
   EnableLogChannel(const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
                    uint32_t log_options, llvm::StringRef channel,
-                   const char **categories, Stream &error_stream);
+                   llvm::ArrayRef<const char *> categories,
+                   Stream &error_stream);
 
   static bool DisableLogChannel(llvm::StringRef channel,
-                                const char **categories, Stream &error_stream);
+                                llvm::ArrayRef<const char *> categories,
+                                Stream &error_stream);
 
   static bool ListChannelCategories(llvm::StringRef channel, Stream &stream);
 

Modified: lldb/trunk/include/lldb/Interpreter/Args.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/Args.h?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/Args.h (original)
+++ lldb/trunk/include/lldb/Interpreter/Args.h Wed Mar  1 04:08:40 2017
@@ -193,6 +193,15 @@ public:
   const char **GetConstArgumentVector() const;
 
   //------------------------------------------------------------------
+  /// Gets the argument as an ArrayRef. Note that the return value does *not*
+  /// have a nullptr const char * at the end, as the size of the list is
+  /// embedded in the ArrayRef object.
+  //------------------------------------------------------------------
+  llvm::ArrayRef<const char *> GetArgumentArrayRef() const {
+    return llvm::makeArrayRef(m_argv).drop_back();
+  }
+
+  //------------------------------------------------------------------
   /// Appends a new argument to the end of the list argument list.
   ///
   /// @param[in] arg_cstr

Modified: lldb/trunk/source/API/SBDebugger.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBDebugger.cpp?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/source/API/SBDebugger.cpp (original)
+++ lldb/trunk/source/API/SBDebugger.cpp Wed Mar  1 04:08:40 2017
@@ -1120,13 +1120,22 @@ SBTypeSynthetic SBDebugger::GetSynthetic
 }
 #endif // LLDB_DISABLE_PYTHON
 
+static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) {
+  if (categories == nullptr)
+    return {};
+  size_t len = 0;
+  while (categories[len] != nullptr)
+    ++len;
+  return llvm::makeArrayRef(categories, len);
+}
+
 bool SBDebugger::EnableLog(const char *channel, const char **categories) {
   if (m_opaque_sp) {
     uint32_t log_options =
         LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
     StreamString errors;
-    return m_opaque_sp->EnableLog(channel, categories, nullptr, log_options,
-                                  errors);
+    return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
+                                  log_options, errors);
   } else
     return false;
 }

Modified: lldb/trunk/source/Commands/CommandObjectLog.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectLog.cpp?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectLog.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectLog.cpp Wed Mar  1 04:08:40 2017
@@ -172,8 +172,8 @@ protected:
     else
       log_file[0] = '\0';
     bool success = m_interpreter.GetDebugger().EnableLog(
-        channel.c_str(), args.GetConstArgumentVector(), log_file,
-        m_options.log_options, result.GetErrorStream());
+        channel, args.GetArgumentArrayRef(), log_file, m_options.log_options,
+        result.GetErrorStream());
     if (success)
       result.SetStatus(eReturnStatusSuccessFinishNoResult);
     else
@@ -233,7 +233,7 @@ protected:
       Log::DisableAllLogChannels(&result.GetErrorStream());
       result.SetStatus(eReturnStatusSuccessFinishNoResult);
     } else {
-      if (Log::DisableLogChannel(channel, args.GetConstArgumentVector(),
+      if (Log::DisableLogChannel(channel, args.GetArgumentArrayRef(),
                                  result.GetErrorStream()))
         result.SetStatus(eReturnStatusSuccessFinishNoResult);
     }

Modified: lldb/trunk/source/Core/Debugger.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Debugger.cpp?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/source/Core/Debugger.cpp (original)
+++ lldb/trunk/source/Core/Debugger.cpp Wed Mar  1 04:08:40 2017
@@ -1239,8 +1239,9 @@ void Debugger::SetLoggingCallback(lldb::
   m_log_callback_stream_sp.reset(new StreamCallback(log_callback, baton));
 }
 
-bool Debugger::EnableLog(const char *channel, const char **categories,
-                         const char *log_file, uint32_t log_options,
+bool Debugger::EnableLog(llvm::StringRef channel,
+                         llvm::ArrayRef<const char *> categories,
+                         llvm::StringRef log_file, uint32_t log_options,
                          Stream &error_stream) {
   const bool should_close = true;
   const bool unbuffered = true;
@@ -1251,7 +1252,7 @@ bool Debugger::EnableLog(const char *cha
     // For now when using the callback mode you always get thread & timestamp.
     log_options |=
         LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
-  } else if (log_file == nullptr || *log_file == '\0') {
+  } else if (log_file.empty()) {
     log_stream_sp = std::make_shared<llvm::raw_fd_ostream>(
         GetOutputFile()->GetFile().GetDescriptor(), !should_close, unbuffered);
   } else {

Modified: lldb/trunk/source/Core/Log.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Log.cpp?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/source/Core/Log.cpp (original)
+++ lldb/trunk/source/Core/Log.cpp Wed Mar  1 04:08:40 2017
@@ -59,27 +59,26 @@ static void ListCategories(Stream &strea
 }
 
 static uint32_t GetFlags(Stream &stream, const ChannelMap::value_type &entry,
-                  const char **categories) {
+                         llvm::ArrayRef<const char *> categories) {
   bool list_categories = false;
   uint32_t flags = 0;
-  for (size_t i = 0; categories[i]; ++i) {
-    if (llvm::StringRef("all").equals_lower(categories[i])) {
+  for (const char *category : categories) {
+    if (llvm::StringRef("all").equals_lower(category)) {
       flags |= UINT32_MAX;
       continue;
     }
-    if (llvm::StringRef("default").equals_lower(categories[i])) {
+    if (llvm::StringRef("default").equals_lower(category)) {
       flags |= entry.second.channel.default_flags;
       continue;
     }
-    auto cat = llvm::find_if(entry.second.channel.categories,
-                             [&](const Log::Category &c) {
-                               return c.name.equals_lower(categories[i]);
-                             });
+    auto cat = llvm::find_if(
+        entry.second.channel.categories,
+        [&](const Log::Category &c) { return c.name.equals_lower(category); });
     if (cat != entry.second.channel.categories.end()) {
       flags |= cat->flag;
       continue;
     }
-    stream.Format("error: unrecognized log category '{0}'\n", categories[i]);
+    stream.Format("error: unrecognized log category '{0}'\n", category);
     list_categories = true;
   }
   if (list_categories)
@@ -237,31 +236,32 @@ void Log::Unregister(llvm::StringRef nam
 
 bool Log::EnableLogChannel(
     const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
-    uint32_t log_options, llvm::StringRef channel, const char **categories,
-    Stream &error_stream) {
+    uint32_t log_options, llvm::StringRef channel,
+    llvm::ArrayRef<const char *> categories, Stream &error_stream) {
   auto iter = g_channel_map->find(channel);
   if (iter == g_channel_map->end()) {
     error_stream.Format("Invalid log channel '{0}'.\n", channel);
     return false;
   }
-  uint32_t flags = categories && categories[0]
-                       ? GetFlags(error_stream, *iter, categories)
-                       : iter->second.channel.default_flags;
+  uint32_t flags = categories.empty()
+                       ? iter->second.channel.default_flags
+                       : GetFlags(error_stream, *iter, categories);
   iter->second.channel.Enable(iter->second.log, log_stream_sp, log_options,
                               flags);
   return true;
 }
 
-bool Log::DisableLogChannel(llvm::StringRef channel, const char **categories,
+bool Log::DisableLogChannel(llvm::StringRef channel,
+                            llvm::ArrayRef<const char *> categories,
                             Stream &error_stream) {
   auto iter = g_channel_map->find(channel);
   if (iter == g_channel_map->end()) {
     error_stream.Format("Invalid log channel '{0}'.\n", channel);
     return false;
   }
-  uint32_t flags = categories && categories[0]
-                       ? GetFlags(error_stream, *iter, categories)
-                       : UINT32_MAX;
+  uint32_t flags = categories.empty()
+                       ? UINT32_MAX
+                       : GetFlags(error_stream, *iter, categories);
   iter->second.channel.Disable(flags);
   return true;
 }

Modified: lldb/trunk/tools/lldb-server/LLDBServerUtilities.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-server/LLDBServerUtilities.cpp?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-server/LLDBServerUtilities.cpp (original)
+++ lldb/trunk/tools/lldb-server/LLDBServerUtilities.cpp Wed Mar  1 04:08:40 2017
@@ -52,8 +52,8 @@ bool LLDBServerUtilities::SetupLogging(c
     channel_then_categories.Shift(); // Shift off the channel
 
     bool success = Log::EnableLogChannel(
-        log_stream_sp, log_options, channel.c_str(),
-        channel_then_categories.GetConstArgumentVector(), error_stream);
+        log_stream_sp, log_options, channel,
+        channel_then_categories.GetArgumentArrayRef(), error_stream);
     if (!success) {
       fprintf(stderr, "Unable to open log file '%s' for channel \"%s\"\n",
               log_file.c_str(), channel_with_categories.str().c_str());

Modified: lldb/trunk/unittests/Core/LogTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/LogTest.cpp?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/unittests/Core/LogTest.cpp (original)
+++ lldb/trunk/unittests/Core/LogTest.cpp Wed Mar  1 04:08:40 2017
@@ -93,7 +93,7 @@ TEST(LogTest, Unregister) {
   llvm::llvm_shutdown_obj obj;
   Log::Register("chan", test_channel);
   EXPECT_EQ(nullptr, test_channel.GetLogIfAny(FOO));
-  const char *cat1[] = {"foo", nullptr};
+  const char *cat1[] = {"foo"};
   std::string message;
   std::shared_ptr<llvm::raw_string_ostream> stream_sp(
       new llvm::raw_string_ostream(message));
@@ -110,20 +110,20 @@ TEST_F(LogChannelTest, Enable) {
   std::shared_ptr<llvm::raw_string_ostream> stream_sp(
       new llvm::raw_string_ostream(message));
   StreamString err;
-  EXPECT_FALSE(Log::EnableLogChannel(stream_sp, 0, "chanchan", nullptr, err));
+  EXPECT_FALSE(Log::EnableLogChannel(stream_sp, 0, "chanchan", {}, err));
   EXPECT_EQ("Invalid log channel 'chanchan'.\n", err.GetString());
   err.Clear();
 
-  EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", nullptr, err));
-  EXPECT_EQ("", err.GetString());
+  EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", {}, err));
+  EXPECT_EQ("", err.GetString()) << "err: " << err.GetString().str();
   EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO));
   EXPECT_EQ(nullptr, test_channel.GetLogIfAll(BAR));
 
-  const char *cat2[] = {"bar", nullptr};
+  const char *cat2[] = {"bar"};
   EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", cat2, err));
   EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO | BAR));
 
-  const char *cat3[] = {"baz", nullptr};
+  const char *cat3[] = {"baz"};
   EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", cat3, err));
   EXPECT_TRUE(err.GetString().contains("unrecognized log category 'baz'"))
       << "err: " << err.GetString().str();
@@ -137,7 +137,7 @@ TEST_F(LogChannelTest, EnableOptions) {
       new llvm::raw_string_ostream(message));
   StreamString err;
   EXPECT_TRUE(Log::EnableLogChannel(stream_sp, LLDB_LOG_OPTION_VERBOSE, "chan",
-                                    nullptr, err));
+                                    {}, err));
 
   Log *log = test_channel.GetLogIfAll(FOO);
   ASSERT_NE(nullptr, log);
@@ -146,7 +146,7 @@ TEST_F(LogChannelTest, EnableOptions) {
 
 TEST_F(LogChannelTest, Disable) {
   EXPECT_EQ(nullptr, test_channel.GetLogIfAll(FOO));
-  const char *cat12[] = {"foo", "bar", nullptr};
+  const char *cat12[] = {"foo", "bar"};
   std::string message;
   std::shared_ptr<llvm::raw_string_ostream> stream_sp(
       new llvm::raw_string_ostream(message));
@@ -154,12 +154,12 @@ TEST_F(LogChannelTest, Disable) {
   EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", cat12, err));
   EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO | BAR));
 
-  const char *cat2[] = {"bar", nullptr};
+  const char *cat2[] = {"bar"};
   EXPECT_TRUE(Log::DisableLogChannel("chan", cat2, err));
   EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO));
   EXPECT_EQ(nullptr, test_channel.GetLogIfAll(BAR));
 
-  const char *cat3[] = {"baz", nullptr};
+  const char *cat3[] = {"baz"};
   EXPECT_TRUE(Log::DisableLogChannel("chan", cat3, err));
   EXPECT_TRUE(err.GetString().contains("unrecognized log category 'baz'"))
       << "err: " << err.GetString().str();
@@ -167,7 +167,7 @@ TEST_F(LogChannelTest, Disable) {
   EXPECT_EQ(nullptr, test_channel.GetLogIfAll(BAR));
   err.Clear();
 
-  EXPECT_TRUE(Log::DisableLogChannel("chan", nullptr, err));
+  EXPECT_TRUE(Log::DisableLogChannel("chan", {}, err));
   EXPECT_EQ(nullptr, test_channel.GetLogIfAny(FOO | BAR));
 }
 
@@ -195,13 +195,13 @@ TEST_F(LogChannelTest, LogThread) {
   std::shared_ptr<llvm::raw_string_ostream> stream_sp(
       new llvm::raw_string_ostream(message));
   StreamString err;
-  EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", nullptr, err));
+  EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", {}, err));
 
   Log *log = test_channel.GetLogIfAll(FOO);
 
   // Start logging on one thread. Concurrently, try disabling the log channel.
   std::thread log_thread([log] { LLDB_LOG(log, "Hello World"); });
-  EXPECT_TRUE(Log::DisableLogChannel("chan", nullptr, err));
+  EXPECT_TRUE(Log::DisableLogChannel("chan", {}, err));
   log_thread.join();
 
   // The log thread either managed to write to the log in time, or it didn't. In

Modified: lldb/trunk/unittests/Interpreter/TestArgs.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Interpreter/TestArgs.cpp?rev=296592&r1=296591&r2=296592&view=diff
==============================================================================
--- lldb/trunk/unittests/Interpreter/TestArgs.cpp (original)
+++ lldb/trunk/unittests/Interpreter/TestArgs.cpp Wed Mar  1 04:08:40 2017
@@ -169,6 +169,14 @@ TEST(ArgsTest, AppendArguments) {
   EXPECT_STREQ("4", args.GetArgumentAtIndex(3));
 }
 
+TEST(ArgsTest, GetArgumentArrayRef) {
+  Args args("foo bar");
+  auto ref = args.GetArgumentArrayRef();
+  ASSERT_EQ(2u, ref.size());
+  EXPECT_STREQ("foo", ref[0]);
+  EXPECT_STREQ("bar", ref[1]);
+}
+
 TEST(ArgsTest, StringToBoolean) {
   bool success = false;
   EXPECT_TRUE(Args::StringToBoolean(llvm::StringRef("true"), false, nullptr));




More information about the lldb-commits mailing list