Skip to content

Commit 2d437f6

Browse files
committedAug 14, 2018
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 llvm-svn: 339695
1 parent 148c445 commit 2d437f6

File tree

5 files changed

+25
-45
lines changed

5 files changed

+25
-45
lines changed
 

‎lldb/include/lldb/Core/Highlighter.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ struct HighlightStyle {
4545
/// The stream to which the result should be appended.
4646
/// \param value
4747
/// The value that we should place our strings around.
48-
/// \return
49-
/// The number of bytes that have been written to the given stream.
50-
std::size_t Apply(Stream &s, llvm::StringRef value) const;
48+
void Apply(Stream &s, llvm::StringRef value) const;
5149

5250
/// Sets the prefix and suffix strings.
5351
/// @param prefix
@@ -114,12 +112,8 @@ class Highlighter {
114112
/// \param s
115113
/// The stream to which the highlighted version of the user string should
116114
/// be written.
117-
/// \return
118-
/// The number of bytes that have been written to the stream.
119-
virtual std::size_t Highlight(const HighlightStyle &options,
120-
llvm::StringRef line,
121-
llvm::StringRef previous_lines,
122-
Stream &s) const = 0;
115+
virtual void Highlight(const HighlightStyle &options, llvm::StringRef line,
116+
llvm::StringRef previous_lines, Stream &s) const = 0;
123117

124118
/// Utility method for calling Highlight without a stream.
125119
std::string Highlight(const HighlightStyle &options, llvm::StringRef line,
@@ -131,9 +125,8 @@ class NoHighlighter : public Highlighter {
131125
public:
132126
llvm::StringRef GetName() const override { return "none"; }
133127

134-
std::size_t Highlight(const HighlightStyle &options, llvm::StringRef line,
135-
llvm::StringRef previous_lines,
136-
Stream &s) const override;
128+
void Highlight(const HighlightStyle &options, llvm::StringRef line,
129+
llvm::StringRef previous_lines, Stream &s) const override;
137130
};
138131

139132
/// Manages the available highlighters.

‎lldb/source/Core/Highlighter.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515

1616
using namespace lldb_private;
1717

18-
std::size_t HighlightStyle::ColorStyle::Apply(Stream &s,
19-
llvm::StringRef value) const {
18+
void HighlightStyle::ColorStyle::Apply(Stream &s, llvm::StringRef value) const {
2019
s << m_prefix << value << m_suffix;
21-
// Calculate how many bytes we have written.
22-
return m_prefix.size() + value.size() + m_suffix.size();
2320
}
2421

2522
void HighlightStyle::ColorStyle::Set(llvm::StringRef prefix,
@@ -28,13 +25,11 @@ void HighlightStyle::ColorStyle::Set(llvm::StringRef prefix,
2825
m_suffix = lldb_utility::ansi::FormatAnsiTerminalCodes(suffix);
2926
}
3027

31-
std::size_t NoHighlighter::Highlight(const HighlightStyle &options,
32-
llvm::StringRef line,
33-
llvm::StringRef previous_lines,
34-
Stream &s) const {
28+
void NoHighlighter::Highlight(const HighlightStyle &options,
29+
llvm::StringRef line,
30+
llvm::StringRef previous_lines, Stream &s) const {
3531
// We just forward the input to the output and do no highlighting.
3632
s << line;
37-
return line.size();
3833
}
3934

4035
static HighlightStyle::ColorStyle GetColor(const char *c) {

‎lldb/source/Core/SourceManager.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,8 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
533533
if (!m_data_sp)
534534
return 0;
535535

536+
size_t bytes_written = s->GetWrittenBytes();
537+
536538
std::string previous_content;
537539

538540
HighlightStyle style = HighlightStyle::MakeVimStyle();
@@ -553,7 +555,6 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
553555
end_line_offset = m_data_sp->GetByteSize();
554556

555557
assert(start_line_offset <= end_line_offset);
556-
size_t bytes_written = 0;
557558
if (start_line_offset < end_line_offset) {
558559
size_t count = end_line_offset - start_line_offset;
559560
const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
@@ -563,8 +564,7 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
563564

564565
auto debugger_sp = m_debugger_wp.lock();
565566
if (should_highlight_source(debugger_sp)) {
566-
bytes_written +=
567-
highlighter.Highlight(style, ref, previous_content, *s);
567+
highlighter.Highlight(style, ref, previous_content, *s);
568568
displayed_line = true;
569569
// Add the new line to the previous lines.
570570
previous_content += ref.str();
@@ -586,7 +586,7 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
586586
// formatting the column (e.g. underline, inverse, etc.)
587587

588588
// First print the part before the column to mark.
589-
bytes_written = s->Write(cstr, column - 1);
589+
s->Write(cstr, column - 1);
590590

591591
// Write the pre escape sequence.
592592
const SymbolContext *sc = nullptr;
@@ -599,15 +599,14 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
599599
FormatEntity::Format(*ansi_prefix_entry, *s, sc, exe_ctx, &addr,
600600
valobj, function_changed, initial_function);
601601

602-
// Write the marked column.
603-
bytes_written += s->Write(cstr + column - 1, 1);
602+
s->Write(cstr + column - 1, 1);
604603

605604
// Write the post escape sequence.
606605
FormatEntity::Format(*ansi_suffix_entry, *s, sc, exe_ctx, &addr,
607606
valobj, function_changed, initial_function);
608607

609608
// And finish up with the rest of the line.
610-
bytes_written += s->Write(cstr + column, count - column);
609+
s->Write(cstr + column, count - column);
611610

612611
// Keep track of the fact that we just wrote the line.
613612
displayed_line = true;
@@ -618,15 +617,14 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
618617
// If we didn't end up displaying the line with ANSI codes for whatever
619618
// reason, display it now sans codes.
620619
if (!displayed_line)
621-
bytes_written = s->PutCString(ref);
620+
s->PutCString(ref);
622621

623622
// Ensure we get an end of line character one way or another.
624623
if (!is_newline_char(ref.back()))
625-
bytes_written += s->EOL();
624+
s->EOL();
626625
}
627-
return bytes_written;
628626
}
629-
return 0;
627+
return s->GetWrittenBytes() - bytes_written;
630628
}
631629

632630
void SourceManager::File::FindLinesMatchingRegex(

‎lldb/source/Plugins/Language/ClangCommon/ClangHighlighter.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,12 @@ determineClangStyle(const ClangHighlighter &highlighter,
128128
return HighlightStyle::ColorStyle();
129129
}
130130

131-
std::size_t ClangHighlighter::Highlight(const HighlightStyle &options,
132-
llvm::StringRef line,
133-
llvm::StringRef previous_lines,
134-
Stream &result) const {
131+
void ClangHighlighter::Highlight(const HighlightStyle &options,
132+
llvm::StringRef line,
133+
llvm::StringRef previous_lines,
134+
Stream &result) const {
135135
using namespace clang;
136136

137-
std::size_t written_bytes = 0;
138-
139137
FileSystemOptions file_opts;
140138
FileManager file_mgr(file_opts);
141139

@@ -210,7 +208,7 @@ std::size_t ClangHighlighter::Highlight(const HighlightStyle &options,
210208
HighlightStyle::ColorStyle color =
211209
determineClangStyle(*this, token, tok_str, options, in_pp_directive);
212210

213-
written_bytes += color.Apply(result, tok_str);
211+
color.Apply(result, tok_str);
214212
}
215213

216214
// If we went over the whole file but couldn't find our own file, then
@@ -219,9 +217,6 @@ std::size_t ClangHighlighter::Highlight(const HighlightStyle &options,
219217
// debug mode we bail out with an assert as this should never happen.
220218
if (!found_user_line) {
221219
result << line;
222-
written_bytes += line.size();
223220
assert(false && "We couldn't find the user line in the input file?");
224221
}
225-
226-
return written_bytes;
227222
}

‎lldb/source/Plugins/Language/ClangCommon/ClangHighlighter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ class ClangHighlighter : public Highlighter {
2828
ClangHighlighter();
2929
llvm::StringRef GetName() const override { return "clang"; }
3030

31-
std::size_t Highlight(const HighlightStyle &options, llvm::StringRef line,
32-
llvm::StringRef previous_lines,
33-
Stream &s) const override;
31+
void Highlight(const HighlightStyle &options, llvm::StringRef line,
32+
llvm::StringRef previous_lines, Stream &s) const override;
3433

3534
/// Returns true if the given string represents a keywords in any Clang
3635
/// supported language.

0 commit comments

Comments
 (0)
Please sign in to comment.