[Lldb-commits] [lldb] r339695 - Remove manual byte counting from Highlighter code.

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Tue Aug 14 10:12:55 PDT 2018


Author: teemperor
Date: Tue Aug 14 10:12:54 2018
New Revision: 339695

URL: http://llvm.org/viewvc/llvm-project?rev=339695&view=rev
Log:
Remove manual byte counting from Highlighter code.

Summary:
This removes the manual byte counting mechanism from the syntax highlighting
code. This is no longer necessary as the Stream class now has built-in support for
automatically counting the bytes that were written to it so far.

The advantage of automatic byte counting via Stream is that it is less error-prone
than the manual version and we need to write less boilerplate code.

Reviewers: labath

Reviewed By: labath

Subscribers: labath, lldb-commits

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

Modified:
    lldb/trunk/include/lldb/Core/Highlighter.h
    lldb/trunk/source/Core/Highlighter.cpp
    lldb/trunk/source/Core/SourceManager.cpp
    lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.cpp
    lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.h

Modified: lldb/trunk/include/lldb/Core/Highlighter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Highlighter.h?rev=339695&r1=339694&r2=339695&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/Highlighter.h (original)
+++ lldb/trunk/include/lldb/Core/Highlighter.h Tue Aug 14 10:12:54 2018
@@ -45,9 +45,7 @@ struct HighlightStyle {
     ///     The stream to which the result should be appended.
     /// \param value
     ///     The value that we should place our strings around.
-    /// \return
-    ///     The number of bytes that have been written to the given stream.
-    std::size_t Apply(Stream &s, llvm::StringRef value) const;
+    void Apply(Stream &s, llvm::StringRef value) const;
 
     /// Sets the prefix and suffix strings.
     /// @param prefix
@@ -114,12 +112,8 @@ public:
   /// \param s
   ///     The stream to which the highlighted version of the user string should
   ///     be written.
-  /// \return
-  ///     The number of bytes that have been written to the stream.
-  virtual std::size_t Highlight(const HighlightStyle &options,
-                                llvm::StringRef line,
-                                llvm::StringRef previous_lines,
-                                Stream &s) const = 0;
+  virtual void Highlight(const HighlightStyle &options, llvm::StringRef line,
+                         llvm::StringRef previous_lines, Stream &s) const = 0;
 
   /// Utility method for calling Highlight without a stream.
   std::string Highlight(const HighlightStyle &options, llvm::StringRef line,
@@ -131,9 +125,8 @@ class NoHighlighter : public Highlighter
 public:
   llvm::StringRef GetName() const override { return "none"; }
 
-  std::size_t Highlight(const HighlightStyle &options, llvm::StringRef line,
-                        llvm::StringRef previous_lines,
-                        Stream &s) const override;
+  void Highlight(const HighlightStyle &options, llvm::StringRef line,
+                 llvm::StringRef previous_lines, Stream &s) const override;
 };
 
 /// Manages the available highlighters.

Modified: lldb/trunk/source/Core/Highlighter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Highlighter.cpp?rev=339695&r1=339694&r2=339695&view=diff
==============================================================================
--- lldb/trunk/source/Core/Highlighter.cpp (original)
+++ lldb/trunk/source/Core/Highlighter.cpp Tue Aug 14 10:12:54 2018
@@ -15,11 +15,8 @@
 
 using namespace lldb_private;
 
-std::size_t HighlightStyle::ColorStyle::Apply(Stream &s,
-                                              llvm::StringRef value) const {
+void HighlightStyle::ColorStyle::Apply(Stream &s, llvm::StringRef value) const {
   s << m_prefix << value << m_suffix;
-  // Calculate how many bytes we have written.
-  return m_prefix.size() + value.size() + m_suffix.size();
 }
 
 void HighlightStyle::ColorStyle::Set(llvm::StringRef prefix,
@@ -28,13 +25,11 @@ void HighlightStyle::ColorStyle::Set(llv
   m_suffix = lldb_utility::ansi::FormatAnsiTerminalCodes(suffix);
 }
 
-std::size_t NoHighlighter::Highlight(const HighlightStyle &options,
-                                     llvm::StringRef line,
-                                     llvm::StringRef previous_lines,
-                                     Stream &s) const {
+void NoHighlighter::Highlight(const HighlightStyle &options,
+                              llvm::StringRef line,
+                              llvm::StringRef previous_lines, Stream &s) const {
   // We just forward the input to the output and do no highlighting.
   s << line;
-  return line.size();
 }
 
 static HighlightStyle::ColorStyle GetColor(const char *c) {

Modified: lldb/trunk/source/Core/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/SourceManager.cpp?rev=339695&r1=339694&r2=339695&view=diff
==============================================================================
--- lldb/trunk/source/Core/SourceManager.cpp (original)
+++ lldb/trunk/source/Core/SourceManager.cpp Tue Aug 14 10:12:54 2018
@@ -533,6 +533,8 @@ size_t SourceManager::File::DisplaySourc
   if (!m_data_sp)
     return 0;
 
+  size_t bytes_written = s->GetWrittenBytes();
+
   std::string previous_content;
 
   HighlightStyle style = HighlightStyle::MakeVimStyle();
@@ -553,7 +555,6 @@ size_t SourceManager::File::DisplaySourc
       end_line_offset = m_data_sp->GetByteSize();
 
     assert(start_line_offset <= end_line_offset);
-    size_t bytes_written = 0;
     if (start_line_offset < end_line_offset) {
       size_t count = end_line_offset - start_line_offset;
       const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
@@ -563,8 +564,7 @@ size_t SourceManager::File::DisplaySourc
 
       auto debugger_sp = m_debugger_wp.lock();
       if (should_highlight_source(debugger_sp)) {
-        bytes_written +=
-            highlighter.Highlight(style, ref, previous_content, *s);
+        highlighter.Highlight(style, ref, previous_content, *s);
         displayed_line = true;
         // Add the new line to the previous lines.
         previous_content += ref.str();
@@ -586,7 +586,7 @@ size_t SourceManager::File::DisplaySourc
             // formatting the column (e.g. underline, inverse, etc.)
 
             // First print the part before the column to mark.
-            bytes_written = s->Write(cstr, column - 1);
+            s->Write(cstr, column - 1);
 
             // Write the pre escape sequence.
             const SymbolContext *sc = nullptr;
@@ -599,15 +599,14 @@ size_t SourceManager::File::DisplaySourc
             FormatEntity::Format(*ansi_prefix_entry, *s, sc, exe_ctx, &addr,
                                  valobj, function_changed, initial_function);
 
-            // Write the marked column.
-            bytes_written += s->Write(cstr + column - 1, 1);
+            s->Write(cstr + column - 1, 1);
 
             // Write the post escape sequence.
             FormatEntity::Format(*ansi_suffix_entry, *s, sc, exe_ctx, &addr,
                                  valobj, function_changed, initial_function);
 
             // And finish up with the rest of the line.
-            bytes_written += s->Write(cstr + column, count - column);
+            s->Write(cstr + column, count - column);
 
             // Keep track of the fact that we just wrote the line.
             displayed_line = true;
@@ -618,15 +617,14 @@ size_t SourceManager::File::DisplaySourc
       // If we didn't end up displaying the line with ANSI codes for whatever
       // reason, display it now sans codes.
       if (!displayed_line)
-        bytes_written = s->PutCString(ref);
+        s->PutCString(ref);
 
       // Ensure we get an end of line character one way or another.
       if (!is_newline_char(ref.back()))
-        bytes_written += s->EOL();
+        s->EOL();
     }
-    return bytes_written;
   }
-  return 0;
+  return s->GetWrittenBytes() - bytes_written;
 }
 
 void SourceManager::File::FindLinesMatchingRegex(

Modified: lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.cpp?rev=339695&r1=339694&r2=339695&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.cpp (original)
+++ lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.cpp Tue Aug 14 10:12:54 2018
@@ -128,14 +128,12 @@ determineClangStyle(const ClangHighlight
   return HighlightStyle::ColorStyle();
 }
 
-std::size_t ClangHighlighter::Highlight(const HighlightStyle &options,
-                                        llvm::StringRef line,
-                                        llvm::StringRef previous_lines,
-                                        Stream &result) const {
+void ClangHighlighter::Highlight(const HighlightStyle &options,
+                                 llvm::StringRef line,
+                                 llvm::StringRef previous_lines,
+                                 Stream &result) const {
   using namespace clang;
 
-  std::size_t written_bytes = 0;
-
   FileSystemOptions file_opts;
   FileManager file_mgr(file_opts);
 
@@ -210,7 +208,7 @@ std::size_t ClangHighlighter::Highlight(
     HighlightStyle::ColorStyle color =
         determineClangStyle(*this, token, tok_str, options, in_pp_directive);
 
-    written_bytes += color.Apply(result, tok_str);
+    color.Apply(result, tok_str);
   }
 
   // If we went over the whole file but couldn't find our own file, then
@@ -219,9 +217,6 @@ std::size_t ClangHighlighter::Highlight(
   // debug mode we bail out with an assert as this should never happen.
   if (!found_user_line) {
     result << line;
-    written_bytes += line.size();
     assert(false && "We couldn't find the user line in the input file?");
   }
-
-  return written_bytes;
 }

Modified: lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.h?rev=339695&r1=339694&r2=339695&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.h (original)
+++ lldb/trunk/source/Plugins/Language/ClangCommon/ClangHighlighter.h Tue Aug 14 10:12:54 2018
@@ -28,9 +28,8 @@ public:
   ClangHighlighter();
   llvm::StringRef GetName() const override { return "clang"; }
 
-  std::size_t Highlight(const HighlightStyle &options, llvm::StringRef line,
-                        llvm::StringRef previous_lines,
-                        Stream &s) const override;
+  void Highlight(const HighlightStyle &options, llvm::StringRef line,
+                 llvm::StringRef previous_lines, Stream &s) const override;
 
   /// Returns true if the given string represents a keywords in any Clang
   /// supported language.




More information about the lldb-commits mailing list