This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Convert AlignEscapedNewlinesLeft to an enum, adding DontAlign
ClosedPublic

Authored by jtbandes on May 1 2017, 11:11 PM.

Details

Reviewers
djasper
Summary

This converts the clang-format option AlignEscapedNewlinesLeft from a boolean to an enum, named AlignEscapedNewlines, with options Left (prev. true), Right (prev. false), and a new option DontAlign.

When set to DontAlign, the backslashes are placed just after the last token in each line:

#define EXAMPLE \
    do { \
        int x = aaaaa; \
        int b; \
        int dddddddddd; \
    } while (0)

Event Timeline

jtbandes created this revision.May 1 2017, 11:11 PM
djasper added inline comments.May 1 2017, 11:43 PM
lib/Format/WhitespaceManager.cpp
523

I think we should not duplicate this loop. Two alternatives:

  1. Move this into the other loop. As long as you reset StartOfMacro in each iteration, it should do the right thing.
  2. Make this work if we just return here. In theory, the "\" should not need any special-casing with this style.

I'd prefer #2.

jtbandes added inline comments.May 1 2017, 11:49 PM
lib/Format/WhitespaceManager.cpp
523

I first tried returning here, but the backslashes were butting up against the content, as in int x = foo;\. I can look around to see if that's fixable.

jtbandes updated this revision to Diff 97404.May 2 2017, 12:32 AM

Modified appendNewlineText so we can simply return in the DontAlign case.

jtbandes marked an inline comment as done.May 2 2017, 12:38 AM

This seems to work fine.

Separately I noticed a strange edge case, which I think is an existing bug:

#define One\
two                                                               \
  three;                                                                       \
  four;

The lack of space in One\ seems to break the formatting on the next line. But as far as I can tell this isn't related to my change.

djasper accepted this revision.May 2 2017, 12:41 AM

This is an edge case in actual C++. An escaped newline literally gets expanded to nothing. So what this reads is

#define Onetwo \
...

This revision is now accepted and ready to land.May 2 2017, 12:41 AM
jtbandes added a comment.EditedMay 2 2017, 12:45 AM

This is an edge case in actual C++. An escaped newline literally gets expanded to nothing. So what this reads is

#define Onetwo \
...

Yeah, I noticed that, but nonetheless it shouldn't break the alignment of \ in the subsequent lines...

Thanks for reviewing! I don't have permissions to commit code; could you do it for me?

(If you have some time, I also have https://reviews.llvm.org/D32475)

djasper closed this revision.May 8 2017, 8:21 AM

Submitted as r302428.