This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Separate block comments with CRLF correctly
Needs RevisionPublic

Authored by Uran198 on May 31 2018, 2:35 AM.

Details

Reviewers
krasimir
djasper
Summary

When formatting the following string:

"/*\r\n"
" * Comment with\r\n"
"    \r\n"
" * blanks.\r\n"
" */\r\n"

clang-format produced:

"/*\r\n"
" * Comment with\r\n"
"    \r\r\n"
" * blanks.\r\n"
" */\r\n"

And when formatting

"#define A(\\\r\n"
"    x) /* \\\r\n"
"a comment \\\r\n"
"inside */ \\\r\n"
"   f();"

with line length 17, clang-format produced:

"#define A(      \\\r"
"    x) /* \\     \\\r"
"a comment     \\ \\\r"
"inside */       \\\r"
"   f();"

So in one case it added additional \r instead of replacing with the blank
line and in another it added additional newline escape character \.

After the change the result are respectively:

"/*\r\n"
" * Comment with\r\n"
"\r\n"
" * blanks
.\r\
n"
" */\r\n"

and

"#define A(x) /* \\\r\n"
"  a comment     \\\r\n"
"  inside */     \\\r\n"
"  f();"

Diff Detail

Event Timeline

Uran198 created this revision.May 31 2018, 2:35 AM
alexfh edited reviewers, added: krasimir; removed: alexfh.Jun 7 2018, 7:19 AM
alexfh added a subscriber: alexfh.
alexfh added inline comments.
lib/Format/BreakableToken.cpp
327

FYI, there's a global UseCRLF flag in WhitespaceManager. It may make sense to use it everywhere instead of deciding for each comment. But I'll let actual clang-format maintainers decide on pros and cons of this.

krasimir requested changes to this revision.Jun 8 2018, 2:14 AM
krasimir added inline comments.
lib/Format/BreakableToken.cpp
327

In case the text contains both \r\n-s and \n not preceded by \r, this would not break on the \n; neither will a version using WhitespaceManager::UsesCRLF. That is computed by:

bool inputUsesCRLF(StringRef Text) {
  return Text.count('\r') * 2 > Text.count('\n');
}

which implies that we at least tentatively support such mixed cases.

I'd try keeping the split here just by "\n" and stripping a trailing "\r"from each line as a second step.

This revision now requires changes to proceed.Jun 8 2018, 2:14 AM
djan added a subscriber: djan.Feb 7 2019, 4:01 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 7 2019, 4:01 AM