Skip to content

Commit af78d6c

Browse files
authored
Merge pull request #307 from Teemperor/BackportClangHighlightNewLineFix
[lldb] Fix that trailing backslashes in source lines break the Clang …
2 parents b5f1775 + ac0eaad commit af78d6c

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)