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

Add clang-format support #913

Merged
merged 5 commits into from
Jul 9, 2024

Conversation

MathiasMagnus
Copy link
Contributor

This work if the continuation of #889.

Notes for the reviewers

Some comments from the original work addressed here, most notably:

  • Limit column size of all formatted sources to 100.

Aside from explicit comments:

  • A git-blame ignore file has been committed to the repo which instructs GitHub's blame functionality on the web interface to keep attribution of lines affected by formatting. Local clones may be configured told similarly.
  • The functional changes are all outside the singular "Apply initial formatting" commit.

Should other formatting changes need refining, discussions may continue here.

@MathiasMagnus MathiasMagnus force-pushed the clang-format-revisited branch 2 times, most recently from 90ae51d to fcdf9e0 Compare June 4, 2024 15:19
# SPDX-License-Identifier: Apache-2.0

# Apply initial formatting
4ceab17e94e73c918130a49ad1b21d5e4c14e7ec
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this ignoring just this single commit? What about future reformatting that takes place when commit(s) are pushed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reminding me, this commit hash needs update after rebases I done.

Ideally no further commits need to be added here, because now the entire repo is formatted and all formatting violations will trip CI, hence no pure formatting-related commits need to be made. Contributors are highly encouraged to configure their development environment to format on save. (eg. VS Code has editor.formatOnSave option and multiple C/C++ formatting extensions available, most notably ms-vscode.cpptools or llvm-vs-code-extensions.vscode-clangd)

Copy link
Collaborator

@MarkCallow MarkCallow Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document how to configure the development environment to configure on save. I have a personal interest in how to do it with Xcode and Vim.

Actually before you do that, let's discuss which is preferable and why: format on save from an editor or format on commit via a Git clean filter?

Did you update the commit hash?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been updated with the latest rebase.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I edited my original comment that said "please resolve" to add the request about documentation which you probably missed. Probably BUILDING.md is the best place for the documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented VS Code. I looked into (Neo)vim, but the phase space is too large.

@MathiasMagnus MathiasMagnus force-pushed the clang-format-revisited branch 2 times, most recently from 6cda98c to 44bb74a Compare June 7, 2024 10:38
@MathiasMagnus MathiasMagnus requested a review from MarkCallow June 7, 2024 15:29
@MathiasMagnus
Copy link
Contributor Author

I think this work is complete. The current CI failure doesn't seem to be related to the changes made here.

---
# Disable clang-format in this directory
DisableFormat: true
SortIncludes: false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed when formatting is disabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not strictly necessary.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the convention used in other Khronos repositories, but it should have no effects (at least on clang-format versions I've used).

if (transcoder_format == transcoder_texture_format::cTFPVRTC1_4_RGB ||
transcoder_format == transcoder_texture_format::cTFPVRTC1_4_RGBA) {
// For PVRTC1, Basis only writes (or requires) total_blocks * bytes_per_block. But GL
// requires extra padding for very small textures:
// https://www.khronos.org/registry/OpenGL/extensions/IMG/IMG_texture_compression_pvrtc.txt
// The transcoder will clear the extra bytes followed the used blocks to 0.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR but given the overhead of running a PR please fix the typo. s/followed/following/.

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

typedef HANDLE pthread_t;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you get it to indent this code up to the #endif?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can exclude lines of code from being formatted by surrounding them with:

// clang-format off
...
// clang-format on

There are examples of this in the generated files.

@@ -3,70 +3,47 @@
* SPDX-License-Identifier: Apache-2.0
Copy link
Collaborator

@MarkCallow MarkCallow Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either don't format this file or fix the formatting for it so the vertices remain separated in the cube face - the formatting has made the comments nonsensical plus it is much harder to see the vertices when they are all run together.

The extra spaces elsewhere are also very helpful for human parsing of the various elements.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no way to exclude individual files from formatting. That's why we had to move everything external that is not expected to be formatted by us into a separate subdirectory where we disabled formatting.

You can exclude lines of code from being formatted (or entire files) by surrounding them with:

// clang-format off
...
// clang-format on

There are examples of this in the generated files.

@@ -10,146 +10,92 @@
// Mesh and VertexFormat Data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also don't format this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

@@ -6,18 +6,6 @@
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

@@ -9,26 +9,10 @@
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

ktx_uint8_t CheckHeader1Test::ktxId[12] = {
0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
};
ktx_uint8_t CheckHeader1Test::ktxId[12] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31,
Copy link
Collaborator

@MarkCallow MarkCallow Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to fix the format of a few lines in a file? This represents the signature of a KTX v1 file so it much easier for humans to follow as originally written.

If there is, please make the same fix to CheckHeader2Test wherever it is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can exclude lines of code from being formatted by surrounding them with:

// clang-format off
...
// clang-format on

There are examples of this in the generated files.

@MarkCallow
Copy link
Collaborator

You need to rebase or merge main again to pick up the workaround for the build issue on GitHub Actions. The latest runner image is only partially fixed.

@MarkCallow
Copy link
Collaborator

@MathiasMagnus please address the outstanding issues and complete this work.

Also remove .clang-format from self-hosted external dep fmt, because it overrides the global disable under external/
The location and the name of the added file is GitHub-friendly, GitHub's blame annotation will also refer to the original author
@MathiasMagnus MathiasMagnus force-pushed the clang-format-revisited branch from 44bb74a to bc6a8af Compare July 3, 2024 08:38
@MathiasMagnus
Copy link
Contributor Author

@MarkCallow We created this PR out of courtesy, because this task did not fit in the original project timeline, so we are unable to address all the specific personal styling and tooling requests. I added documentation on the minimal requirement to reduce CI friction and noted how to configure VS Code to not trip CI. Vim is too deep a rabbit a hole and I don't have the time to give anything I document in XCode a test drive.

As for the rest of the outstanding formatting related inquiries: instead of whack-a-mole-ing the formatting issues one-by-one and aligning them to the desired format, I reverted the "Initial formatting" commit and just commit the machinery enabling the enforced formatting.

  • For now, CI job definition is there, but its invocation is commented out.
  • The GitHub-friendly git blame ignore file is there with a note but not hash.

Because clang-format can't run on an entire source tree, all the tools drive it file-by-file. Should you want to tweak the .clang-format files to perfection, I used the following one-liner on Windows:

# list folders excluding build and .venv | recurse into all | filter files matching regex | for each of the matches { run clang-format inplace using style files and full path to file }
gci -exc build,.venv | gci -rec | where -prop Name -match '^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu))$' | % { clang-format.exe -i --style=file $_.FullName }

I leave implementing similar logic in the shell of their choice to the end-user. (The regex I borrowed from the script run by the CI action.)

In summary, all the infrastructure for clang-format is there, please tweak it for your needs as you see fit.

@MarkCallow
Copy link
Collaborator

the understanding that due to the additional requests we ran out of time and budget to do the clang-format change

Were these "additional requests" related or unrelated to clang-format? If the former what where they?

There is no way to exclude individual files from formatting.

Because clang-format can't run on an entire source tree, all the tools drive it file-by-file.

These statements conflict. What am I missing?

@MarkCallow
Copy link
Collaborator

There is no way to exclude individual files from formatting.

What about .clang-format-ignore which I've just discovered at https://clang.llvm.org/docs/ClangFormat.html.

@aqnuep
Copy link
Collaborator

aqnuep commented Jul 4, 2024

the understanding that due to the additional requests we ran out of time and budget to do the clang-format change

Were these "additional requests" related or unrelated to clang-format? If the former what where they?

There is no way to exclude individual files from formatting.

Because clang-format can't run on an entire source tree, all the tools drive it file-by-file.

These statements conflict. What am I missing?

Edit: accidentally pressed the wrong button.

They do not conflict. clang-format, the CLI tool does run file-by-file (unless you run it with a script or indirectly through git clang-format, which is ran for all changed files), but the style sheets (the .clang-format files) apply to entire directories. You cannot change them file-by-file.

The only way to disable clang-format for a single file to either move it to a separate directory for which you disable the styling in the .clang-format file, or you modify the source to surround the content that is not supposed to be formatted with // clang-format off and // clang format on.

@aqnuep aqnuep closed this Jul 4, 2024
@aqnuep aqnuep reopened this Jul 4, 2024
@MarkCallow MarkCallow merged commit 110f049 into KhronosGroup:main Jul 9, 2024
32 checks passed
@KhronosGroup KhronosGroup deleted a comment from aqnuep Dec 12, 2024
@KhronosGroup KhronosGroup deleted a comment from aqnuep Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants