[clang-format] recursive alignment

Ben Harper via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 2 06:43:00 PDT 2016


I'm calling this "recursive alignment", but what it really fixes is issues
with AlignConsecutiveAssignments and AlignConsecutiveDeclarations such as
this:

OLD:
  int fun1(int a);
  double fun2(int b);

NEW
  int    fun1(int a);
  double fun2(int b);

Also...

OLD:
  void fun(int x = 1) {
      int y      = 2;
  }

NEW:
  void fun(int x = 1) {
      int y = 2;
  }

The old alignment function was incapable of maintaining alignment whenever
the scope changed. To illustrate - in the first example mentioned, the
alignment of fun1 is lost by entering the nested scope of (int a).
This would cause the alignment to give up, and cause fun2 to start from a
blank slate. It would also cause false alignment, which is illustrated in
the second example above.

This modification changes the alignment function so that it calls itself
recursively, at each change in scope depth. This allows it to maintain state
across different scope depths.

There are some new test cases which stress this functionality. In addition,
there were two historical test cases marked as "FIX ME", which now work as
intended.

In order to sense check, I have run this new implementation against the
Clang
source code, with AlignConsecutiveAssignments:true and
AlignConsecutiveDeclarations:true. I then compared the old output with those
settings, vs the new output, with the same settings. All changes that I
observed are explainable by the new logic, and IMO the formatted code looks
better with the new method.

Regards,
Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160602/142140e3/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: FormatRecursiveAlignment.patch
Type: application/octet-stream
Size: 19647 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160602/142140e3/attachment-0001.obj>


More information about the cfe-commits mailing list