Skip to content

Commit ac0eaad

Browse files
committed
[lldb] Fix that trailing backslashes in source lines break the Clang highlighter
Summary: Clang's raw Lexer doesn't produce any tokens for trailing backslashes in a line. This doesn't work with LLDB's Clang highlighter which builds the source code to display from the list of tokens the Lexer returns. This causes that lines with trailing backslashes are lacking the backslash and the following newline when rendering source code in LLDB. This patch removes the trailing newline from the current line we are highlighting. This way Clang doesn't drop the backslash token and we just restore the newline after tokenising. Fixes rdar://57091487 Reviewers: JDevlieghere, labath Reviewed By: JDevlieghere, labath Subscribers: labath, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D70177
1 parent b5f1775 commit ac0eaad

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,22 @@ void ClangHighlighter::Highlight(const HighlightStyle &options,
139139
FileManager file_mgr(file_opts,
140140
FileSystem::Instance().GetVirtualFileSystem());
141141

142+
// The line might end in a backslash which would cause Clang to drop the
143+
// backslash and the terminating new line. This makes sense when parsing C++,
144+
// but when highlighting we care about preserving the backslash/newline. To
145+
// not lose this information we remove the new line here so that Clang knows
146+
// this is just a single line we are highlighting. We add back the newline
147+
// after tokenizing.
148+
llvm::StringRef line_ending = "";
149+
// There are a few legal line endings Clang recognizes and we need to
150+
// temporarily remove from the string.
151+
if (line.consume_back("\r\n"))
152+
line_ending = "\r\n";
153+
else if (line.consume_back("\n"))
154+
line_ending = "\n";
155+
else if (line.consume_back("\r"))
156+
line_ending = "\r";
157+
142158
unsigned line_number = previous_lines.count('\n') + 1U;
143159

144160
// Let's build the actual source code Clang needs and setup some utility
@@ -227,6 +243,9 @@ void ClangHighlighter::Highlight(const HighlightStyle &options,
227243
color.Apply(result, to_print);
228244
}
229245

246+
// Add the line ending we trimmed before tokenizing.
247+
result << line_ending;
248+
230249
// If we went over the whole file but couldn't find our own file, then
231250
// somehow our setup was wrong. When we're in release mode we just give the
232251
// user the normal line and pretend we don't know how to highlight it. In

lldb/unittests/Language/Highlighting/HighlighterTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,44 @@ TEST_F(HighlighterTest, ClangPPDirectives) {
205205
highlightC("#include \"foo\" //c", s));
206206
}
207207

208+
TEST_F(HighlighterTest, ClangPreserveNewLine) {
209+
HighlightStyle s;
210+
s.comment.Set("<cc>", "</cc>");
211+
212+
EXPECT_EQ("<cc>//</cc>\n", highlightC("//\n", s));
213+
}
214+
215+
TEST_F(HighlighterTest, ClangTrailingBackslashBeforeNewline) {
216+
HighlightStyle s;
217+
218+
EXPECT_EQ("\\\n", highlightC("\\\n", s));
219+
EXPECT_EQ("\\\r\n", highlightC("\\\r\n", s));
220+
221+
EXPECT_EQ("#define a \\\n", highlightC("#define a \\\n", s));
222+
EXPECT_EQ("#define a \\\r\n", highlightC("#define a \\\r\n", s));
223+
EXPECT_EQ("#define a \\\r", highlightC("#define a \\\r", s));
224+
}
225+
226+
TEST_F(HighlighterTest, ClangTrailingBackslashWithWhitespace) {
227+
HighlightStyle s;
228+
229+
EXPECT_EQ("\\ \n", highlightC("\\ \n", s));
230+
EXPECT_EQ("\\ \t\n", highlightC("\\ \t\n", s));
231+
EXPECT_EQ("\\ \n", highlightC("\\ \n", s));
232+
EXPECT_EQ("\\\t\n", highlightC("\\\t\n", s));
233+
234+
EXPECT_EQ("#define a \\ \n", highlightC("#define a \\ \n", s));
235+
EXPECT_EQ("#define a \\ \t\n", highlightC("#define a \\ \t\n", s));
236+
EXPECT_EQ("#define a \\ \n", highlightC("#define a \\ \n", s));
237+
EXPECT_EQ("#define a \\\t\n", highlightC("#define a \\\t\n", s));
238+
}
239+
240+
TEST_F(HighlighterTest, ClangTrailingBackslashMissingNewLine) {
241+
HighlightStyle s;
242+
EXPECT_EQ("\\", highlightC("\\", s));
243+
EXPECT_EQ("#define a\\", highlightC("#define a\\", s));
244+
}
245+
208246
TEST_F(HighlighterTest, ClangComments) {
209247
HighlightStyle s;
210248
s.comment.Set("<cc>", "</cc>");

0 commit comments

Comments
 (0)