opentitan icon indicating copy to clipboard operation
opentitan copied to clipboard

C/C++ style guide: Allow exceptions for manual whitespace formatting?

Open imphil opened this issue 7 years ago • 5 comments

In lowRISC/stwg-base#167 we initially discussed, and in lowRISC/stwg-base#862 we merged the C/C++ style guide, which is based on the Google C++ style guide. A rendered version is available here: https://bubble.servers.lowrisc.org/doc/rm/CCppCodingStyle.html.

The style guide is enforced through clang-format, a automatic code formatter. No exceptions to the formatting as produced by clang-format are allowed.

Using clang-format is uncontroversial. What is controversial, however, is the question of exceptions, more specifically: Should the style guide allow exceptions for manual whitespace formatting? Note that the discussion here is only about whitespace formatting, it's not about other potential exceptions to the style guide.

clang-format can be told to ignore certain code blocks by using clang-format off/on comments around the affected code block (documentation). There have been no objections so far to require the use of these comments around manually indented code blocks.

In the discussion up to now many people have voiced their opinion. I'm trying to summarize the points as I understood them below.

Arguments against whitespace exceptions:

  • Allowing any kind of exception opens the flood gates for more and more exceptions in the future.
  • Instead of allowing exceptions, clang-format should be improved to give better results.
  • Especially on auto-generated header files (like the *_regs.h files that regtool generates): "I don't tend to care about how pretty autogenerated headers look, since in general you don't need to look at them. I see even less point in doing this just for autogenerated stuff." (Ron)

Arguments for whitespace exceptions:

  • In some pieces of code, especially in lists of registers or struct fields and when writing macros, manually adjusted whitespace helps readability. A full diff of changes produced by our initial run of clang-format is available here and here, here are some examples:

    1. Manually formatted: https://github.com/lowRISC/stwg-base/blob/5c510fdcd081c32441f8999473ef3c7e8e2fdc09/sw/lib/usb_controlep.h#L46-L58

      clang-format: https://github.com/lowRISC/stwg-base/blob/18ac97e18bbd8ff89bcc7c6ad6b21a580068ae04/sw/lib/usb_controlep.h#L46-L60

    2. Manually formatted: https://github.com/lowRISC/stwg-base/blob/5c510fdcd081c32441f8999473ef3c7e8e2fdc09/hw/dv/verilator/simutil_verilator/cpp/verilated_toplevel.h#L10-L20

      clang-format https://github.com/lowRISC/stwg-base/blob/18ac97e18bbd8ff89bcc7c6ad6b21a580068ae04/hw/dv/verilator/simutil_verilator/cpp/verilated_toplevel.h#L10-L20

    3. Manually formatted: https://github.com/lowRISC/stwg-base/blob/f8951ae0078156d39343fd494ebd31cb82da694e/hw/ip/flash_ctrl/sw/flash_ctrl_regs.h#L6-L13 clang-format: https://github.com/lowRISC/stwg-base/blob/18ac97e18bbd8ff89bcc7c6ad6b21a580068ae04/hw/ip/flash_ctrl/sw/flash_ctrl_regs.h#L6-L13

  • Improving clang-format to produce good results for the previously mentioned cases isn't possible, as it cannot reliably deduct the semantic meaning of the code piece.

  • All exceptions are reviewed during code review, requiring the use of clang-format on/off comments ensures that no exceptions can be "hidden" during the code review.

  • Even the LLVM project, where clang-format originates from, allows exceptions to preserve whitespace formatting.

  • Especially on auto-generated header files (like the *_regs.h files that regtool generates): hardware developers tend to look at these files (especially the values associated with constants) to match the software with hardware behavior, e.g. in waveforms. Readability of these files is important to them.

Proposed next steps

  • I invite everyone to
    • voice their arguments and concerns, especially if they are not listed yet or if I misinterpreted a remark, and
    • think about solutions which help us avoid exceptions, and comment in this issue.
  • I propose to have a video call with all interested parties in approx. one week from now to come to a conclusion. Please send me an email to [email protected] if you'd like to be included in this discussion.

Thanks to everyone who participated in this discussion so far. OpenTitan brings together developers from the software and the hardware world, with open source and with industry experience, and from different programming language environments. All these experiences reflect into what we think is good and what is bad practice, into what is useful and what is harmful. I hope this discussion can help us to understand all of that better, and create a solution we all understand and that we're reasonably happy with.

imphil avatar Aug 15 '19 13:08 imphil

Clang-format has a configuration option AlignTrailingComments which may or may not cover the comments-in-a-macro case shown in example i.

Example number ii should be supported too. The configuration directive is called PPDirectiveIndentStyle and the value you want to use to match the code as written is AfterHash.

~Clang-format does not seem to have anything like AlignConsecutiveDefines which would cover example iii. (It does have AlignConsecutiveAssignments and similar configuration options).~ Update: Clang-format is about to get AlignConsecutiveMacros, which would cover example iii. We can presumably update the style guide when this option makes it into a public release: https://reviews.llvm.org/rC364938

lenary avatar Aug 15 '19 13:08 lenary

On a mission to clean up old issues, scraping from stalest to freshest.

Did the proposed next steps from Aug 15 result in any conclusions @imphil ?

Anything we can do to converge on a conclusion or recommendation?

sjgitty avatar Dec 05 '19 02:12 sjgitty

I'm in favour of allowing exceptions when justified. But I'm also against burning through lots of time on whitespace related discussions.

I note that #1078 appears to have a good example of a case where disabling clang-format improves readability.

@gkelly @mcy - thoughts on this issue?

asb avatar Dec 09 '19 14:12 asb

I think that the style guide already contains an implicitly understood "if the formatter outputs unreadable garbage, // clang-format off is an acceptable workaround for readability", subject to case-by-case approval; I think this is just part of the social contract of a style guide.

I have no problem with making case-by-case judgements as a reviewer, but codifying it seems like overkill.

mcy avatar Dec 10 '19 17:12 mcy

I think we can close this. Our approach right now is "avoid exceptions as much as possible", which is enforced by reviews.

There are two exceptions in the codebase (so far) from before we became stricter, which I don't think we need to change just for the sake of being stricter (if that code changes, this might change).

lenary avatar Jul 06 '20 11:07 lenary