This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] avoid introducing multiline comments
ClosedPublic

Authored by krasimir on Nov 6 2020, 8:16 AM.

Details

Summary

In C++ with -Werror=comment, multiline comments are not allowed.
clang-format could accidentally introduce multiline comments when reflowing.
This adapts clang-format to not introduce multiline comments by not allowing a
break after \. Note that this does not apply to comment lines that already are
multiline comments, such as comments in macros.

Diff Detail

Event Timeline

krasimir created this revision.Nov 6 2020, 8:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 6 2020, 8:16 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
krasimir requested review of this revision.Nov 6 2020, 8:16 AM
krasimir added a project: Restricted Project.Nov 6 2020, 8:26 AM
sammccall accepted this revision.Nov 6 2020, 8:59 AM

One concern about looping, otherwise just style nits.

clang/lib/Format/BreakableToken.cpp
104

Can you add a high-level comment to this loop?

I guess the idea is "Some spaces are unacceptable to break on, rewind past them."

105

The warning is worth mentioning, but fundamentally we have the warning because such code is confusing, and we shouldn't produce confusing code.

maybe:

// If a line-comment ends with `\`, the next line continues the comment,
// whether or not it starts with `//`. This is confusing and triggers -Wcomment.
// Avoid introducing multiline comments by not allowing a break after '\'.
109

Do we really want to predicate this on isCpp()? // comments are allowed by C99.
Even if the warning only applies to C++ for some reason, the reasons for confusion do not.

125

doesn't this mean that we won't loop? if Text ends with "blah \ \" then you'll split between "blah" and the first "\"?

I guess this could be structured:

while () {
  if (special case 1) {
    // adjust pos
    continue;
  }
  if (special case 2) {
    // adjust pos
    continue;
  }
  break;
}

(This is equivalent to the old if/elseif/break which is too hard to add complex conditions to)

This revision is now accepted and ready to land.Nov 6 2020, 8:59 AM
krasimir updated this revision to Diff 303788.Nov 9 2020, 2:17 AM
krasimir marked 3 inline comments as done.
  • address review comments
krasimir added inline comments.Nov 9 2020, 2:20 AM
clang/lib/Format/BreakableToken.cpp
109

I think in Java and other non-C++-y languages, an \ at the end of a line-comment line does not have any special meaning, hence I didn't want it to trigger in those cases.

125

Thank you! Updated this block and added a test case for this.

sammccall accepted this revision.Nov 9 2020, 3:03 AM

Still LG

clang/lib/Format/BreakableToken.cpp
109

Sigh, I guess I forgot what isCpp() means again... :)

This revision was landed with ongoing or failed builds.Nov 9 2020, 6:30 AM
This revision was automatically updated to reflect the committed changes.