Skip to content
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

Prevent first value of header guards from being aligned (and more generally not be re-formatted) #52982

Open
Julien-Elie opened this issue Jan 4, 2022 · 8 comments
Labels
clang-format enhancement Improving things as opposed to bug fixing, e.g. new or missing feature good first issue https://github.com/llvm/llvm-project/contribute

Comments

@Julien-Elie
Copy link

Julien-Elie commented Jan 4, 2022

When AcrossEmptyLinesAndComments is used for AlignConsecutiveMacros, alignment is done like this:

#ifndef INN_PATHS_H
#define INN_PATHS_H                1

/* comment */

#define INN_PATH_NNRPD             "nnrpd"
#define INN_PATH_AUTHDIR_NOPASS    "resolv"

Couldn't the value of the first line of the header guard not be aligned?

@mydeveloperday
Copy link
Contributor

out of interest most header guards are written as

#ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
#define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H

why the need for 1?

@Julien-Elie
Copy link
Author

It is true that 1 is not necessary for header guards, that's a good point.

I've just been accustomed to always defining macros to something, which can then be tested in #if clauses. But you're right that I'm not going to test header guards later in the code.
That's a minor formatting nit though, so I'll consider removing the 1 in my header guards. But maybe I am not the only one to add 1 in similar definitions, and therefore it could be worthwhile preventing clang-format from reformatting it? (I'm of course OK if you believe it should still be.)

@mydeveloperday
Copy link
Contributor

To be honest I slightly feel that if we were going to do this we'd have to add yet another option, I'm not convinced this is worth it, I mean you set it to AcrossCommentsEmptyLines and that's what you got. Lets add it as an enhancement for now but as you have a workaround My feeling is it will just sit here not getting done (unless you'd like to contibute a fix yourself)

@mydeveloperday mydeveloperday added enhancement Improving things as opposed to bug fixing, e.g. new or missing feature and removed new issue labels Jan 4, 2022
@Julien-Elie
Copy link
Author

Why not just make header guards special and do not apply them any formatting? (like PPIndentWidth does not apply)

@mydeveloperday
Copy link
Contributor

If I understand correctly PPIndentWidth possibly does not apply because its at indent 0 because its first. (so its not really not applying it, its just not indenting)

I don't think to my knowledge that we assume anything about header guards, I've marked it as an enhancement, but honestly I can't really see it being a priority (at least for me). I just want to be clear of the expectation, That said you are free to submit patches if this is painful to you. (but it will need to be peer reviewed)

I can't see it just being something that is blanket everyone wouldn't want it to align. AlignConsecutiveMacros was a much sought after feature, its been in for a couple of years and this is the first time I've seen this raised. What ever you do it will have to be another option, (IgnoreHeaderGuardFormatting?) to ensure you don't break other people.

My advice honestly, is to change your style when it comes to header guards, I know that seems super harsh but there is so much code out there like the following that people WOULD want Aligned that I fear it would be way too easy for us to miss interpret an #ifndef as a header guard, I'm not sure I'd trust any code to be able to disambiguate between the two 100% of the time. (especially in the absence of real header guards)

#ifndef EAFNOSUPPORT
#define EAFNOSUPPORT 9901
#endif

@Julien-Elie
Copy link
Author

What is new is the AcrossEmptyLinesAndComments feature for AlignConsecutiveMacros. It is how I saw the formatting change in header guards. They are usually separated by an empty line with the rest of the code, so AlignConsecutiveMacros did not affect them before.

I think the example for EAFNOSUPPORT will not be considered by clang-format as a header guard because the #endif will not be at the end of the file. So it will be properly indented.

Header guards are explicitly excluded from indentation, and are handled by clang-format (I've for instance seen https://reviews.llvm.org/D42035).

@mydeveloperday
Copy link
Contributor

That's a very good point, definitely we should consider it and that's good evidence as to why we might. To be honest most changes like this need someone to champion them.

@mydeveloperday mydeveloperday added good first issue https://github.com/llvm/llvm-project/contribute beginner and removed good first issue https://github.com/llvm/llvm-project/contribute labels Jan 5, 2022
@Julien-Elie Julien-Elie changed the title Prevent header guards from being aligned? Prevent first value of header guards from being aligned (and more generally not be re-formatted) Jan 5, 2022
@Endilll Endilll added good first issue https://github.com/llvm/llvm-project/contribute and removed beginner labels Aug 15, 2023
@llvmbot
Copy link
Member

llvmbot commented Aug 15, 2023

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:

  1. Assign the issue to you.
  2. Fix the issue locally.
  3. Run the test suite locally.
    3.1) Remember that the subdirectories under test/ create fine-grained testing targets, so you can
    e.g. use make check-clang-ast to only run Clang's AST tests.
  4. Create a git commit
  5. Run git clang-format HEAD~1 to format your changes.
  6. Submit the patch to Phabricator.
    6.1) Detailed instructions can be found here

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-format enhancement Improving things as opposed to bug fixing, e.g. new or missing feature good first issue https://github.com/llvm/llvm-project/contribute
Projects
None yet
Development

No branches or pull requests

4 participants