Skip to content
Snippets Groups Projects

Clang Format Configuration, master branch (2022.11.28.)

All threads resolved!

Added a default (Clang) format for C++ source (and header) files. This was triggered by the discussion on !58765 (merged).

The proposal is based on the Google C++ Style Guilde, which all of the ACTS configurations are also based on.

I tried to make the configuration as lean as possible. Only keeping "overrides" over the Google style that make the code more in line with https://twiki.cern.ch/twiki/bin/view/AtlasComputing/CodingGuidelines. Though I can't imagine that I would not have overlooked a few things.

This is mainly intended as a conversation starter. But just to point out, just adding this one file to the repository makes it possible to re-format any (C++) source file in the repository with VS Code by just pressing [Ctrl]-[Shift]-I. :smile:

Pinging @christos, @averbyts, @tadej.

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • This merge request affects no known packages. Consult an expert!

    This merge request affects 1 file:

    • .clang-format

    Could not map the following files to a domain:

    • .clang-format
  • Also tagging @jojungge

  • Andrii Verbytskyi approved this merge request

    approved this merge request

    • Resolved by Christos Anastopoulos

      Hi

      I think this needs a discussion where you actually take something that is well formatted and you show what it will happen with the 3 main editors or a bot (I assume they will do the same)

      Also we will need to advertise how this is to be used from the other 2 editors I know people use. As I said I use it a lot (really a lot) from one of them ....

  • Christos Anastopoulos marked this merge request as draft

    marked this merge request as draft

  • Hi @akraszna ,

    1. Thank you!
    2. Could you, please, remind me what would be the range of clang-format versions that one can actually use? If I'm not wrong there might be small differences in the ways the clang-format interprets the config.
    3. Maybe just add a couple of lines on the formatting and how to use this config to the documentation. The manual for shifters?

    Best regards,

    Andrii

    • Resolved by Christos Anastopoulos

      It depends how one setups ATLAS .... release since we have clang releases.

      For both tidy and format I do

      source  /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/Clang/14.0.0-x86_64-centos7-gcc11-opt/14.0.0/x86_64-centos7-gcc11-opt/setup.sh

      and then it does not matter even if I compile with gcc I can run the tidy

      Btw you are going a bit too fast again.

      anyhow look my points here and in the other MR.

      • Please take something well formatted like from CxxUtils or core and show how it changes
      • Then take something large (extrapolator) and show it.
      • i.e show what this will do to both consistently and not consistently formatted files.
      • If this is suggested keep in mind some of us have ones that we use and you can override it when we edit, which then breaks the "consistency" with what is there .
      • Prb we want it mandatory for all of us (?) but then this needs the discussion
      • Not everyone uses vscode we need the instruction for emacs and Vi
      • There are other points ....
      • If is not mandatory not sure why it goes to "shifters" manual?
      Edited by Christos Anastopoulos
    • Resolved by Christos Anastopoulos

      To clarify: This MR literally does nothing to the CI, the nightly, or any source file. It just adds one small file in the repository, without any other change actually happening.

      One thing that we could recommend for developers could be to run

      git clang-format <source file>

      before they would commit the file. So that all modifications would become formatted. This we could be able to even require in the CI if there is enough political momentum behind us. :stuck_out_tongue:

      But by default I didn't meant to re-format any of our existing code.

  • Let me try to spell out the behaviour a bit more. As I wrote before, this would just be the default for any source file in any package that does not override it. What I mean is like the following:

    [bash][celeborn]:development > find clang-format/
    clang-format/
    clang-format/source.cpp
    clang-format/source1
    clang-format/source1/source.cpp
    clang-format/source1/.clang-format
    clang-format/.clang-format
    clang-format/source2
    clang-format/source2/source.cpp
    [bash][celeborn]:development > more clang-format/.clang-format 
    BasedOnStyle: Google
    Language: Cpp
    [bash][celeborn]:development > more clang-format/source1/.clang-format 
    BasedOnStyle: Mozilla
    Language: Cpp
    [bash][celeborn]:development > more clang-format/source.cpp 
    
    #include <cstring>
    
    int func1(const char* foo) {
      if (std::strcmp(foo, "bar") == 0) {
        return 5;
      }
      return 1;
    }
    [bash][celeborn]:development > more clang-format/source1/source.cpp 
    
    #include <cstring>
    
    int
    func1(const char* foo)
    {
      if (std::strcmp(foo, "bar") == 0) {
        return 5;
      }
      return 1;
    }
    [bash][celeborn]:development > more clang-format/source2/source.cpp 
    
    #include <cstring>
    
    int func1(const char* foo) {
      if (std::strcmp(foo, "bar") == 0) {
        return 5;
      }
      return 1;
    }
    [bash][celeborn]:development >

    Hopefully this clarifies a bit what the intended behaviour is. (The difference between the Google and Mozilla style is not easy to demonstrate in just a few lines.)

    And to make it explicit: This configuration would modify every single source file in the repository if we were to apply it to everything. But that is not what I'm proposing.

  • Christos Anastopoulos resolved all threads

    resolved all threads

  • Christos Anastopoulos marked this merge request as ready

    marked this merge request as ready

  • On the editor discussion: VS Code, vi, and I'm pretty sure emacs and most other editors that people use can be configured to use clang-format. If you do, the native "reformat" functionality will just automatically produce compatible results. I've been using this for years in vim.

    If I can make one other recommendation on the configuration is to not optimize it to look like this or that. In every configuration you will find edge cases where something doesn't look as nice, and so on. The least painful way is to pick something and just roll with it.

  • Christos Anastopoulos resolved all threads

    resolved all threads

  • OK so the dump is [!58781 (comment 6233885)]

    Let's put it in as far as I am concerned . For me anyhow is good enoguh. edge cases will happen also when changing clang-version.

    The "overriding as little as possible" works for me. Just making sure that people understand the "defaults" (the ones we do not override) which for me make sense , but want them to be "clear" . There was "resistance" on one of them in the past ...

    Other items :

    • It would be lovely if we indicate people clang to use. I would naively thing is the one we build athena with /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/Clang/14.0.0-x86_64-centos7-gcc11-opt/14.0.0/x86_64-centos7-gcc11-opt/setup.sh but OK

    • Then we have some other things to decide on when is "mandatory" where "optiona"l . How is to be applied etc. At the end can be applied via a git command. One should be careful as for sure it will change some files by a lot, for others will be "epsilon".

    But these is for another time prb a meeting .

    Edited by Christos Anastopoulos
  • :x: CI Result FAILURE (hash 8c655a58)

    Athena AthSimulation AthGeneration AnalysisBase AthAnalysis DetCommon
    externals :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:
    cmake :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:
    make :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:
    tests :o: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:

    Full details available on this CI monitor view. Check the JIRA CI status board for known problems
    :white_check_mark: Athena: number of compilation errors 0, warnings 0
    :white_check_mark: AthSimulation: number of compilation errors 0, warnings 0
    :white_check_mark: AthGeneration: number of compilation errors 0, warnings 0
    :white_check_mark: AnalysisBase: number of compilation errors 0, warnings 0
    :white_check_mark: AthAnalysis: number of compilation errors 0, warnings 0
    :white_check_mark: DetCommon: number of compilation errors 0, warnings 0
    :pencil: For experts only: Jenkins output [CI-MERGE-REQUEST-CC7 62590]

  • Vakhtang Tsulaia mentioned in merge request !58779 (merged)

    mentioned in merge request !58779 (merged)

  • mentioned in commit 51d1f6a6

  • Hey coming back to this with a minor comment

    it seems :

    SortIncludes (SortIncludesOptions) clang-format 3.8

    Controls if and how clang-format will sort #includes. If Never, includes are never sorted. If CaseInsensitive, includes are sorted in an ASCIIbetical or case insensitive fashion. If CaseSensitive, includes are sorted in an alphabetical or case sensitive fashion. Possible values:

    This is fine if the include "blocks" are separated with //

    but one needs to be careful vis a vis

    rdering of #include statements. [include-ordering]

    #include directives should generally be listed according to dependency ordering, with the files that have the most dependencies coming first. This implies that the first #include in a .cxx'' file should be the corresponding .h'' file, followed by other #include directives from the same package. These would then be followed by #include directives for other packages, again ordered from most to least dependent. Finally, system #include directives should come last.

    From the style guide.

    and in places where include "ordering" might matter ...

    Edited by Christos Anastopoulos
  • mentioned in merge request !58948 (merged)

Please register or sign in to reply
Loading