-
Notifications
You must be signed in to change notification settings - Fork 12.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow including the "main" header with <
>
#53013
Comments
Don't you think this would be very messy for large projects Did I misunderstand why you wouldn't use |
Literally only because the C++ Core Guidelines say so (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-incform). To be clear, I'm not claiming it's a good guideline. It's like east vs west const (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rl-const). Do I like what the C++ Core Guidelines say? No. But I just follow it since, well... they are the C++ Core Guidelines. FWIW there is isocpp/CppCoreGuidelines#1666, which wants to move it even further to the " |
I would not expect a huge (long list?) IncludeDirectoryOwnHeaders. I would expect
With
(supposing component_A/component_B have their headers in its own subdirectory, it's not that unusual to have all the components having the headers directly in I certainly think it would be annoying. But, once again, the C++ Core Guidelines basically force me to do this. |
I thought you were trying to win me over! ;-) What bad things happen if we had an option to allow the main header to be either '"' or '<'? rather than having to maintain a whole list of "ownheaders"? |
I have not looked at the implementation. But I suppose only
src/thing.cpp
which one is the "main" one? |
Yes very likely... wouldn't be easy to differentiate. Assume we have
And we are in thing.cpp can we make any clear distension over who should win as to who is the main? a) In the presence of (1,2,3,4 & 5) 3 wins So from this I understand that you'd want to be able to say "component_b" is in my project so that we can determine who out of 2 and 5 should win. But ultimately that requires us to allow Ultimately if someone write
Personally I don't think we shouldn't assume that thing.h is the main header and hoist it out to be: do you?
|
This mostly comes down to this code in lib/Tooling/Inclusions
I personally think we could use another regex to solve this, which might be more powerful than just a list. |
To be fair, the user may have already said which one he prefers with IncludeCategories. If thing.cpp is in component_b, he may have '^<component_b/' with priority INT_MIN and so he may want only things in component_b considered as main headers.
Honestly, here I would suppose You could also argue that, in case of doubt/conflict, if any of the candidates is already the top header, it should be considered the main header. I mean, not only you have 5 different thing.h files includes in a single .cpp file... you also happen to have at the top At the end of the day, the "problem" is that clang-format is impossibly flexible when real projects are going to have some kind of more or less sane pattern (the issue being knowing which one). In a completely generic way, making at least any of 1, 3 or 5 the main header could potentially make sense.
No, I would never expect clang-format to do this to my code. |
What do you think about starting by making something like this an option?
|
Seems fine to me. |
Suggestions for a good name? not sure I like |
Not that I think it's the best name ever. But no, sorry, I don't have a great suggestion either. |
Reviewing this issue... has the core guideline changed? it seems to hold with the "" for a local header and <> for files elsewhere. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-incform Is this really still a requirement given that we seem to cover the guidelines and https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html |
I don't think it has changed. But it says "for files relative to the including file", not "local header" (I guess "local" here would be same project or component). So, according to the Core Guidelines, in
Notice the "note":
That seems to be the whole argument in the Core Guidelines: What if a header file later appears relative to the including/source file? Better to use <> to avoid the risk. The Core Guideline seem in direct contradiction to both the gcc manual and the C++ standard document. So an option is to try to change the Core Guideline, "the standard says something else" is a decent argument. |
Hi! This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
For more instructions on how to submit a patch to LLVM, see our documentation. If you have any further questions about this issue, don't hesitate to ask via a comment on this Github issue. @llvm/issue-subscribers-good-first-issue |
Resolves #27008, #39735, #53013, #63619. Hello, this PR adds the MainIncludeChar option to clang-format, allowing to select which include syntax must be considered when searching for the main header: quotes (`#include "foo.hpp"`, the default), brackets (`#include <foo.hpp>`), or both. The lack of support for brackets has been reported many times, see the linked issues, so I am pretty sure there is a need for it :) A short note about why I did not implement a regex approach as discussed in #53013: while a regex would have allowed many extra ways to describe the main header, the bug descriptions listed above suggest a very simple need: support brackets for the main header. This PR answers this needs in a quite simple way, with a very simple style option. IMHO the feature space covered by the regex (again, for which there is no demand :)) can be implemented latter, in addition to the proposed option. The PR also includes tests for the option with and without grouped includes.
IncludeCategories in https://clang.llvm.org/docs/ClangFormatStyleOptions.html explains that "The main header for a source file automatically gets category 0".
The first problem is that it fails to document it only considers a header "main" if it has been included with
"
.The second issue is that it fails to consider a header that has been included with
"
as the "main" header.This is not a problem for the LLVM project because it includes its own headers with
"
. This makes sense, the standard in "15.3 Source file inclusion" saysand https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html says "This variant is used for header files of your own program".
But unfortunately, the C++ Core Guidelines say the usage of
<
or"
should not be considering whether it's "outside the control of the implementation" or "header files of your own program" but considering whether the file is "relative to the including file" (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-incform).A common thing to do is
to have the headers defining the public API separated from others. And since thing.hpp is not relative to thing.cpp (I don't think
..
is defined in any standard, and it would be ugly anyway), the C++ Core Guidelines state that<
should be used and clang-format fails to see#include mylib/thing.hpp
is the main header of thing.cpp.One way to solve this would be adding an option to specify the directory prefix used by the project. If I can say
IncludeDirectoryOwnHeaders: mylib
then clang-format could consider mylib/thing.hpp as the main header without getting confused with otherlib/thing.hpp.The text was updated successfully, but these errors were encountered: