Skip to content

[lldb] Fix that trailing backslashes in source lines break the Clang … #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lldb/source/Plugins/Language/ClangCommon/ClangHighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ void ClangHighlighter::Highlight(const HighlightStyle &options,
FileManager file_mgr(file_opts,
FileSystem::Instance().GetVirtualFileSystem());

// The line might end in a backslash which would cause Clang to drop the
// backslash and the terminating new line. This makes sense when parsing C++,
// but when highlighting we care about preserving the backslash/newline. To
// not lose this information we remove the new line here so that Clang knows
// this is just a single line we are highlighting. We add back the newline
// after tokenizing.
llvm::StringRef line_ending = "";
// There are a few legal line endings Clang recognizes and we need to
// temporarily remove from the string.
if (line.consume_back("\r\n"))
line_ending = "\r\n";
else if (line.consume_back("\n"))
line_ending = "\n";
else if (line.consume_back("\r"))
line_ending = "\r";

unsigned line_number = previous_lines.count('\n') + 1U;

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

// Add the line ending we trimmed before tokenizing.
result << line_ending;

// 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
Expand Down
38 changes: 38 additions & 0 deletions lldb/unittests/Language/Highlighting/HighlighterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,44 @@ TEST_F(HighlighterTest, ClangPPDirectives) {
highlightC("#include \"foo\" //c", s));
}

TEST_F(HighlighterTest, ClangPreserveNewLine) {
HighlightStyle s;
s.comment.Set("<cc>", "</cc>");

EXPECT_EQ("<cc>//</cc>\n", highlightC("//\n", s));
}

TEST_F(HighlighterTest, ClangTrailingBackslashBeforeNewline) {
HighlightStyle s;

EXPECT_EQ("\\\n", highlightC("\\\n", s));
EXPECT_EQ("\\\r\n", highlightC("\\\r\n", s));

EXPECT_EQ("#define a \\\n", highlightC("#define a \\\n", s));
EXPECT_EQ("#define a \\\r\n", highlightC("#define a \\\r\n", s));
EXPECT_EQ("#define a \\\r", highlightC("#define a \\\r", s));
}

TEST_F(HighlighterTest, ClangTrailingBackslashWithWhitespace) {
HighlightStyle s;

EXPECT_EQ("\\ \n", highlightC("\\ \n", s));
EXPECT_EQ("\\ \t\n", highlightC("\\ \t\n", s));
EXPECT_EQ("\\ \n", highlightC("\\ \n", s));
EXPECT_EQ("\\\t\n", highlightC("\\\t\n", s));

EXPECT_EQ("#define a \\ \n", highlightC("#define a \\ \n", s));
EXPECT_EQ("#define a \\ \t\n", highlightC("#define a \\ \t\n", s));
EXPECT_EQ("#define a \\ \n", highlightC("#define a \\ \n", s));
EXPECT_EQ("#define a \\\t\n", highlightC("#define a \\\t\n", s));
}

TEST_F(HighlighterTest, ClangTrailingBackslashMissingNewLine) {
HighlightStyle s;
EXPECT_EQ("\\", highlightC("\\", s));
EXPECT_EQ("#define a\\", highlightC("#define a\\", s));
}

TEST_F(HighlighterTest, ClangComments) {
HighlightStyle s;
s.comment.Set("<cc>", "</cc>");
Expand Down