This revision was automatically updated to reflect the committed changes.
Closed by commit rLLDB339695: Remove manual byte counting from Highlighter 
code. (authored by teemperor, committed by ).

Repository:
  rLLDB LLDB

https://reviews.llvm.org/D50676

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

Index: source/Core/SourceManager.cpp
===================================================================
--- source/Core/SourceManager.cpp
+++ source/Core/SourceManager.cpp
@@ -533,6 +533,8 @@
   if (!m_data_sp)
     return 0;
 
+  size_t bytes_written = s->GetWrittenBytes();
+
   std::string previous_content;
 
   HighlightStyle style = HighlightStyle::MakeVimStyle();
@@ -553,7 +555,6 @@
       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 @@
 
       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 @@
             // 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 @@
             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 @@
       // 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(
Index: source/Core/Highlighter.cpp
===================================================================
--- source/Core/Highlighter.cpp
+++ source/Core/Highlighter.cpp
@@ -15,26 +15,21 @@
 
 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,
                                      llvm::StringRef suffix) {
   m_prefix = lldb_utility::ansi::FormatAnsiTerminalCodes(prefix);
   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) {
Index: source/Plugins/Language/ClangCommon/ClangHighlighter.h
===================================================================
--- source/Plugins/Language/ClangCommon/ClangHighlighter.h
+++ source/Plugins/Language/ClangCommon/ClangHighlighter.h
@@ -28,9 +28,8 @@
   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.
Index: source/Plugins/Language/ClangCommon/ClangHighlighter.cpp
===================================================================
--- source/Plugins/Language/ClangCommon/ClangHighlighter.cpp
+++ source/Plugins/Language/ClangCommon/ClangHighlighter.cpp
@@ -128,14 +128,12 @@
   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,18 +208,15 @@
     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
   // somehow our setup was wrong. When we're in release mode we just give the
   // user the normal line and pretend we don't know how to highlight it. In
   // 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;
 }
Index: include/lldb/Core/Highlighter.h
===================================================================
--- include/lldb/Core/Highlighter.h
+++ include/lldb/Core/Highlighter.h
@@ -45,9 +45,7 @@
     ///     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 @@
   /// \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 @@
 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.
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to