Make include order follow Google/LLVM/Lakos guidelines#110
Make include order follow Google/LLVM/Lakos guidelines#110hazelnusse wants to merge 1 commit intoTheLartians:masterfrom
Conversation
|
Upon closer reading of the Cpp Core Guidelines, I think the recommendation there is to Again, feel free to take it or leave it as this is something clients could easily modify to their tastes. |
|
Yeah, in my understanding, Not too sure how I feel about preserving include groups, as that require the user to pay attention to the order and block that the includes are defined. Tbh I prefer less degrees of freedom as a starting point and allowing users to modify the template if they have more specific needs. |
|
The most compelling reason to make the include of the corresponding header be the very first line in a cpp library implementation file is to ensure that the header file isn't missing includes, which I think is summarized nicely by the LLVM docs:
I think this can be partially addressed by using ICWU but the above practice is a simple and reliable method that requires no extra tooling. |
|
Yeah, that does make sense, however it's in no way enforced by clang-format, right? It would then require discipline by the implementor and code reviewer to not end up with a bunch of meaningless include blocks after a while. |
|
Correct, it is not enforced by clang-format, but with the change to .clang-format I made, it at least allows users to follow this practice if they want to without suggesting/making formating changes via the format/fix-format targets. If I'm not mistaken, Google's cpplint.py does in fact check this: However, following this convention is desirable even if you are not following the rest of the google style guide which is why I figured it might be worth including here. |
if using the IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^<(Poco|folly|gsl|asio|doctest|zmqpp|boost|fmt|json|spdlog|openssl)/'
Priority: 3
- Regex: '<[_[:alnum:]./]+>'
Priority: 4
# all other headers first!
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(_test)?$'see too https://clang.llvm.org/docs/ClangFormatStyleOptions.html |
|
@TheLartians Any plan to merge this? |
@TheLartians Feel free to take this or leave it. LLVM, Google, and John Lakos recommend this order:
https://llvm.org/docs/CodingStandards.html#include-style
https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes
However, you may prefer to instead use "" instead of <>, as recommend by Cpp Core Guidelines:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf12-prefer-the-quoted-form-of-include-for-files-relative-to-the-including-file-and-the-angle-bracket-form-everywhere-else
If you use "" instead of <>, you could keep
IncludeBlocks: Regroupbut with the <> it seems clang-format wants to re-order.Some places prefer/recommend <> exclusively, so YMMV.