Skip to content

Commit f00a20e

Browse files
committed
[clang-format] Add the possibility to align assignments spanning empty lines or comments
Currently, empty lines and comments break alignment of assignments on consecutive lines. This makes the AlignConsecutiveAssignments option an enum that allows controlling whether empty lines or empty lines and comments should be ignored when aligning assignments. Reviewed By: MyDeveloperDay, HazardyKnusperkeks, tinloaf Differential Revision: https://reviews.llvm.org/D93986
1 parent 01d9f13 commit f00a20e

File tree

7 files changed

+1489
-110
lines changed

7 files changed

+1489
-110
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 266 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -195,47 +195,231 @@ the configuration (without a prefix: ``Auto``).
195195

196196

197197

198-
**AlignConsecutiveAssignments** (``bool``)
199-
If ``true``, aligns consecutive assignments.
198+
**AlignConsecutiveAssignments** (``AlignConsecutiveStyle``)
199+
Style of aligning consecutive assignments.
200200

201-
This will align the assignment operators of consecutive lines. This
202-
will result in formattings like
201+
``Consecutive`` will result in formattings like:
203202

204203
.. code-block:: c++
205204

206-
int aaaa = 12;
207-
int b = 23;
208-
int ccc = 23;
205+
int a = 1;
206+
int somelongname = 2;
207+
double c = 3;
209208

210-
**AlignConsecutiveBitFields** (``bool``)
211-
If ``true``, aligns consecutive bitfield members.
209+
Possible values:
210+
211+
* ``ACS_None`` (in configuration: ``None``)
212+
Do not align assignments on consecutive lines.
213+
214+
* ``ACS_Consecutive`` (in configuration: ``Consecutive``)
215+
Align assignments on consecutive lines. This will result in
216+
formattings like:
217+
218+
.. code-block:: c++
219+
220+
int a = 1;
221+
int somelongname = 2;
222+
double c = 3;
223+
224+
int d = 3;
225+
/* A comment. */
226+
double e = 4;
227+
228+
* ``ACS_AcrossEmptyLines`` (in configuration: ``AcrossEmptyLines``)
229+
Same as ACS_Consecutive, but also spans over empty lines, e.g.
230+
231+
.. code-block:: c++
232+
233+
int a = 1;
234+
int somelongname = 2;
235+
double c = 3;
236+
237+
int d = 3;
238+
/* A comment. */
239+
double e = 4;
240+
241+
* ``ACS_AcrossComments`` (in configuration: ``AcrossComments``)
242+
Same as ACS_Consecutive, but also spans over lines only containing
243+
comments, e.g.
244+
245+
.. code-block:: c++
246+
247+
int a = 1;
248+
int somelongname = 2;
249+
double c = 3;
250+
251+
int d = 3;
252+
/* A comment. */
253+
double e = 4;
254+
255+
* ``ACS_AcrossEmptyLinesAndComments``
256+
(in configuration: ``AcrossEmptyLinesAndComments``)
257+
258+
Same as ACS_Consecutive, but also spans over lines only containing
259+
comments and empty lines, e.g.
260+
261+
.. code-block:: c++
262+
263+
int a = 1;
264+
int somelongname = 2;
265+
double c = 3;
266+
267+
int d = 3;
268+
/* A comment. */
269+
double e = 4;
270+
271+
**AlignConsecutiveBitFields** (``AlignConsecutiveStyle``)
272+
Style of aligning consecutive bit field.
212273

213-
This will align the bitfield separators of consecutive lines. This
214-
will result in formattings like
274+
``Consecutive`` will align the bitfield separators of consecutive lines.
275+
This will result in formattings like:
215276

216277
.. code-block:: c++
217278

218279
int aaaa : 1;
219280
int b : 12;
220281
int ccc : 8;
221282

222-
**AlignConsecutiveDeclarations** (``bool``)
223-
If ``true``, aligns consecutive declarations.
283+
Possible values:
284+
285+
* ``ACS_None`` (in configuration: ``None``)
286+
Do not align bit fields on consecutive lines.
287+
288+
* ``ACS_Consecutive`` (in configuration: ``Consecutive``)
289+
Align bit fields on consecutive lines. This will result in
290+
formattings like:
291+
292+
.. code-block:: c++
293+
294+
int aaaa : 1;
295+
int b : 12;
296+
int ccc : 8;
297+
298+
int d : 2;
299+
/* A comment. */
300+
int ee : 3;
301+
302+
* ``ACS_AcrossEmptyLines`` (in configuration: ``AcrossEmptyLines``)
303+
Same as ACS_Consecutive, but also spans over empty lines, e.g.
304+
305+
.. code-block:: c++
306+
307+
int aaaa : 1;
308+
int b : 12;
309+
int ccc : 8;
310+
311+
int d : 2;
312+
/* A comment. */
313+
int ee : 3;
314+
315+
* ``ACS_AcrossComments`` (in configuration: ``AcrossComments``)
316+
Same as ACS_Consecutive, but also spans over lines only containing
317+
comments, e.g.
318+
319+
.. code-block:: c++
320+
321+
int aaaa : 1;
322+
int b : 12;
323+
int ccc : 8;
324+
325+
int d : 2;
326+
/* A comment. */
327+
int ee : 3;
328+
329+
* ``ACS_AcrossEmptyLinesAndComments``
330+
(in configuration: ``AcrossEmptyLinesAndComments``)
331+
332+
Same as ACS_Consecutive, but also spans over lines only containing
333+
comments and empty lines, e.g.
334+
335+
.. code-block:: c++
336+
337+
int aaaa : 1;
338+
int b : 12;
339+
int ccc : 8;
340+
341+
int d : 2;
342+
/* A comment. */
343+
int ee : 3;
344+
345+
**AlignConsecutiveDeclarations** (``AlignConsecutiveStyle``)
346+
Style of aligning consecutive declarations.
224347

225-
This will align the declaration names of consecutive lines. This
226-
will result in formattings like
348+
``Consecutive`` will align the declaration names of consecutive lines.
349+
This will result in formattings like:
227350

228351
.. code-block:: c++
229352

230353
int aaaa = 12;
231354
float b = 23;
232-
std::string ccc = 23;
355+
std::string ccc;
233356

234-
**AlignConsecutiveMacros** (``bool``)
235-
If ``true``, aligns consecutive C/C++ preprocessor macros.
357+
Possible values:
358+
359+
* ``ACS_None`` (in configuration: ``None``)
360+
Do not align bit declarations on consecutive lines.
361+
362+
* ``ACS_Consecutive`` (in configuration: ``Consecutive``)
363+
Align declarations on consecutive lines. This will result in
364+
formattings like:
365+
366+
.. code-block:: c++
367+
368+
int aaaa = 12;
369+
float b = 23;
370+
std::string ccc;
371+
372+
int a = 42;
373+
/* A comment. */
374+
bool c = false;
375+
376+
* ``ACS_AcrossEmptyLines`` (in configuration: ``AcrossEmptyLines``)
377+
Same as ACS_Consecutive, but also spans over empty lines, e.g.
378+
379+
.. code-block:: c++
380+
381+
int aaaa = 12;
382+
float b = 23;
383+
std::string ccc;
384+
385+
int a = 42;
386+
/* A comment. */
387+
bool c = false;
388+
389+
* ``ACS_AcrossComments`` (in configuration: ``AcrossComments``)
390+
Same as ACS_Consecutive, but also spans over lines only containing
391+
comments, e.g.
392+
393+
.. code-block:: c++
394+
395+
int aaaa = 12;
396+
float b = 23;
397+
std::string ccc;
398+
399+
int a = 42;
400+
/* A comment. */
401+
bool c = false;
402+
403+
* ``ACS_AcrossEmptyLinesAndComments``
404+
(in configuration: ``AcrossEmptyLinesAndComments``)
405+
406+
Same as ACS_Consecutive, but also spans over lines only containing
407+
comments and empty lines, e.g.
408+
409+
.. code-block:: c++
410+
411+
int aaaa = 12;
412+
float b = 23;
413+
std::string ccc;
414+
415+
int a = 42;
416+
/* A comment. */
417+
bool c = false;
418+
419+
**AlignConsecutiveMacros** (``AlignConsecutiveStyle``)
420+
Style of aligning consecutive macro definitions.
236421

237-
This will align C/C++ preprocessor macros of consecutive lines.
238-
Will result in formattings like
422+
``Consecutive`` will result in formattings like:
239423

240424
.. code-block:: c++
241425

@@ -245,6 +429,68 @@ the configuration (without a prefix: ``Auto``).
245429
#define foo(x) (x * x)
246430
#define bar(y, z) (y + z)
247431

432+
Possible values:
433+
434+
* ``ACS_None`` (in configuration: ``None``)
435+
Do not align macro definitions on consecutive lines.
436+
437+
* ``ACS_Consecutive`` (in configuration: ``Consecutive``)
438+
Align macro definitions on consecutive lines. This will result in
439+
formattings like:
440+
441+
.. code-block:: c++
442+
443+
#define SHORT_NAME 42
444+
#define LONGER_NAME 0x007f
445+
#define EVEN_LONGER_NAME (2)
446+
447+
#define foo(x) (x * x)
448+
/* some comment */
449+
#define bar(y, z) (y + z)
450+
451+
* ``ACS_AcrossEmptyLines`` (in configuration: ``AcrossEmptyLines``)
452+
Same as ACS_Consecutive, but also spans over empty lines, e.g.
453+
454+
.. code-block:: c++
455+
456+
#define SHORT_NAME 42
457+
#define LONGER_NAME 0x007f
458+
#define EVEN_LONGER_NAME (2)
459+
460+
#define foo(x) (x * x)
461+
/* some comment */
462+
#define bar(y, z) (y + z)
463+
464+
* ``ACS_AcrossComments`` (in configuration: ``AcrossComments``)
465+
Same as ACS_Consecutive, but also spans over lines only containing
466+
comments, e.g.
467+
468+
.. code-block:: c++
469+
470+
#define SHORT_NAME 42
471+
#define LONGER_NAME 0x007f
472+
#define EVEN_LONGER_NAME (2)
473+
474+
#define foo(x) (x * x)
475+
/* some comment */
476+
#define bar(y, z) (y + z)
477+
478+
* ``ACS_AcrossEmptyLinesAndComments``
479+
(in configuration: ``AcrossEmptyLinesAndComments``)
480+
481+
Same as ACS_Consecutive, but also spans over lines only containing
482+
comments and empty lines, e.g.
483+
484+
.. code-block:: c++
485+
486+
#define SHORT_NAME 42
487+
#define LONGER_NAME 0x007f
488+
#define EVEN_LONGER_NAME (2)
489+
490+
#define foo(x) (x * x)
491+
/* some comment */
492+
#define bar(y, z) (y + z)
493+
248494
**AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``)
249495
Options for aligning backslashes in escaped newlines.
250496

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,11 @@ clang-format
289289

290290
- Option ``IndentRequires`` has been added to indent the ``requires`` keyword
291291
in templates.
292+
292293
- Option ``BreakBeforeConceptDeclarations`` has been added to aid the formatting of concepts.
293294

294-
- Option ``IndentPragmas`` has been added to allow #pragma to indented with the current scope level. This is especially useful when using #pragma to mark OpenMP sections of code.
295+
- Option ``IndentPragmas`` has been added to allow #pragma to indented with the current scope
296+
level. This is especially useful when using #pragma to mark OpenMP sections of code.
295297

296298
- Option ``SpaceBeforeCaseColon`` has been added to add a space before the
297299
colon in a case or default statement.
@@ -300,6 +302,9 @@ clang-format
300302
macros which are not parsed as a type in front of a statement. See
301303
the documentation for an example.
302304

305+
- Options ``AlignConsecutiveAssignments``, ``AlignConsecutiveBitFields``,
306+
``AlignConsecutiveDeclarations`` and ``AlignConsecutiveMacros`` have been modified to allow
307+
alignment across empty lines and/or comments.
303308

304309
libclang
305310
--------

clang/docs/tools/dump_format_style.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(self, name, type, comment):
4242
def __str__(self):
4343
s = '**%s** (``%s``)\n%s' % (self.name, self.type,
4444
doxygen2rst(indent(self.comment, 2)))
45-
if self.enum:
45+
if self.enum and self.enum.values:
4646
s += indent('\n\nPossible values:\n\n%s\n' % self.enum, 2)
4747
if self.nested_struct:
4848
s += indent('\n\nNested configuration flags:\n\n%s\n' %self.nested_struct,
@@ -104,13 +104,18 @@ def __str__(self):
104104
doxygen2rst(indent(self.comment, 2)))
105105

106106
def clean_comment_line(line):
107-
match = re.match(r'^/// \\code(\{.(\w+)\})?$', line)
107+
match = re.match(r'^/// (?P<indent> +)?\\code(\{.(?P<lang>\w+)\})?$', line)
108108
if match:
109-
lang = match.groups()[1]
109+
indent = match.group('indent')
110+
if not indent:
111+
indent = ''
112+
lang = match.group('lang')
110113
if not lang:
111114
lang = 'c++'
112-
return '\n.. code-block:: %s\n\n' % lang
113-
if line == '/// \\endcode':
115+
return '\n%s.. code-block:: %s\n\n' % (indent, lang)
116+
117+
endcode_match = re.match(r'^/// +\\endcode$', line)
118+
if endcode_match:
114119
return ''
115120
return line[4:] + '\n'
116121

@@ -184,7 +189,9 @@ class State(object):
184189
state = State.InStruct
185190
enums[enum.name] = enum
186191
else:
187-
raise Exception('Invalid format, expected enum field comment or };')
192+
# Enum member without documentation. Must be documented where the enum
193+
# is used.
194+
pass
188195
elif state == State.InEnumMemberComment:
189196
if line.startswith('///'):
190197
comment += clean_comment_line(line)

0 commit comments

Comments
 (0)