Skip to content

clang-format improvements - #1251

Merged
Carson Radtke (carsonRadtke) merged 12 commits into
microsoft:mainfrom
beinhaerter:clang-format
Jun 5, 2026
Merged

Carson Radtke (carsonRadtke) merged 12 commits into
microsoft:mainfrom
beinhaerter:clang-format

Conversation

@beinhaerter

@beinhaerter Werner Henze (beinhaerter) commented May 28, 2026 •

Copy link
Copy Markdown
Contributor
  • Add a clang-format linter check to the PR pipeline
  • Add a Windows bat file and a Linux sh script to apply formatting to all sources files, and on Linux also to all .sh shell scripts. Add a linter check to the pipeline for the .sh shell scripts (calling shfmt and shellcheck).
  • Run the formatting script(s) to apply clang-format and fix the clang-format linter errors
  • Remove CommentPragmas from .clang-format
  • Add WhitespaceSensitiveMacros: [GSL_SUPPRESS] to .clang-format
  • Remove all // clang-format off and // NO-FORMAT as they are not needed any more
  • Remove a commented out GSL_SUPPRESS

@beinhaerter Werner Henze (beinhaerter) changed the title clang-format clang-format improvements May 28, 2026
@beinhaerter
Werner Henze (beinhaerter) force-pushed the clang-format branch 2 times, most recently from 1bd5793 to 5b3e9be Compare May 28, 2026 13:25
@beinhaerter
Werner Henze (beinhaerter) marked this pull request as ready for review May 28, 2026 13:25
- Add a clang-format linter check to the PR pipeline
- Apply clang-format to files where the linter initially failed
- Remove `CommentPragmas` from `.clang-format`
- Remove all `// clang-format off` and `// NO-FORMAT` as they are not needed
- Remove a commented out `GSL_SUPPRESS`
@beinhaerter

Copy link
Copy Markdown
Contributor Author

I am not github pipeline expert. The clang-format.yml file is AI generated. I have no idea if it is OK or not.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for making these changes! The only blocker is the third-party actions dependency - if you wouldn't mind changing that to a shell command, that'd be appreciated.

Comment thread tests/utils_tests.cpp Resolved
Comment thread tests/span_tests.cpp Resolved
Comment thread .github/workflows/clang-format.yml Resolved
@beinhaerter

Copy link
Copy Markdown
Contributor Author

Thanks for making these changes! The only blocker is the third-party actions dependency - if you wouldn't mind changing that to a shell command, that'd be appreciated.

OK, I'll give it a try.

Werner Henze added 2 commits May 29, 2026 14:29
to prevent formatting of include/CMakeLists.txt
In a VS2026 developer command prompt I ran `clang-format -i include\gsl\* --assume-filename x.cpp`. This was necessary because VS GUI does not format files without an extension :(. Please note that `--assume-filename` is necessary here, otherwise the files will not be formatted. Surprisingly the behaviour for the formatter differs from the behaviour of `lang-format(-20) --dry-run --Werror include/gsl/*` where clang-format recognizes that the files is C++.
@carsonRadtke

Copy link
Copy Markdown
Member

RE: installed version is 18 which replaces "GSL_SUPPRESS(bounds.1)" with "GSL_SUPPRESS(bounds .1)"

Consider adding WhitespaceSensitiveMacros: [GSL_SUPPRESS] to .clang-format so this doesn't happen?

Comment on lines +24 to +27
# Install the exact clang-format binary
- name: Install clang-format
run: |
sudo apt install clang-format-${{ env.CLANG_VERSION }}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd rather use the pre-installed version, but I'm not going to block the PR on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The pre-installed version needs the WhitespaceSensitiveMacros change, clang-format-20 does not.

Regarding the burden on the authors: I think it might be a good idea to provide an apply-clang-format.bat and an apply-clang-format.sh. Of course the bat file would only work in the developer command prompt and could only use the clang-format version shipped with Visual Studio. What do you think about it? Shall I add those files to make formatting easier?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shall I add those files to make formatting easier?

That sounds good to me. Can you add it to a <repo-root>/scripts folder?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Alternatively, we could add it as a copilot skill so [at]Copilot can make the changes automatically?

This way we don't have to worry about what clang-format version is installed on the user's machine when they run the script. Copilot will automatically have whatever we specify in https://github.com/microsoft/GSL/blob/main/.github/workflows/copilot-setup-steps.yml.

Either way is fine by me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Regarding the copilot-setup-steps.yml I have no idea how that all works together, what the best place is to do changes. I either need your guidance or you do your refinements after this PR is merged. Whatever you think is better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The Copilot stuff is a "nice to have"; that's something I may follow-up on in the future.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the changes! LGTM

I'll merge it on Monday in case you want to address my non-blocking comments.

@beinhaerter

Werner Henze (beinhaerter) commented May 29, 2026 •

Copy link
Copy Markdown
Contributor Author

RE: installed version is 18 which replaces "GSL_SUPPRESS(bounds.1)" with "GSL_SUPPRESS(bounds .1)"

Consider adding WhitespaceSensitiveMacros: [GSL_SUPPRESS] to .clang-format so this doesn't happen?

Here is a patch for .clang-format together with the changes it does to the files. I understand that it fixes the whitespace-in-suppress-attributes-problem, but I am not sure if this looks better. Which version do you prefer? Shall I apply the patch or leave it as it is?

For example:

 template <class Cont>
-GSL_SUPPRESS(bounds.4)
-GSL_SUPPRESS(bounds.2) constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()])
+GSL_SUPPRESS(bounds.4) GSL_SUPPRESS(bounds.2) constexpr auto at(Cont& cont, const index i)
+    -> decltype(cont[cont.size()])
 {

@carsonRadtke

Copy link
Copy Markdown
Member

but I am not sure if this looks better

Which version do you prefer? Shall I apply the patch or leave it as it is?

I don't really have a preference on what "looks better", but I do think that having WhitespaceSensitiveMacros: [GSL_SUPPRESS] is correct and should be kept. My vote is "apply the patch".

Werner Henze added 3 commits June 1, 2026 22:38
for linux with linter (shfmt and shellcheck)
Currently not clear to me:
On my personal computer at home, when I start "Developer Command Prompt for VS18", I can run `clang-format` without providing the path.
On my managed (domain) company computer, when I start "Developer Command Prompt for VS18", I can NOT run `clang-format` without providing the path. I need to call `"%VCINSTALLDIR%Tools\Llvm\bin\clang-format"`.
I have no idea what the difference is. At least the version `"%VCINSTALLDIR%Tools\Llvm\bin\clang-format"` works on both computers, so I add the path.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Thanks for the contribution!

@carsonRadtke
Carson Radtke (carsonRadtke) merged commit b2f6bec into microsoft:main Jun 5, 2026
89 checks passed
@beinhaerter
Werner Henze (beinhaerter) deleted the clang-format branch June 5, 2026 15:50
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
cpp-gsl 5.0.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* Update return by value threshold in gsl::not_null by @ozaktash in microsoft/GSL#1205
* copilot: Provide more project context for the Copilot coding agent by @carsonRadtke in microsoft/GSL#1207
* Document differences between hardened std::span and gsl::span by @carsonRadtke with @Copilot in microsoft/GSL#1206
* Add constexpr to not_null comparison operators by @carsonRadtke with @Copilot in microsoft/GSL#1208
* fix: all pipeline failures by @carsonRadtke in microsoft/GSL#1221
* export proper syntax for GSL_SUPPRESS for new VS by @carsonRadtke in microsoft/GSL#1213
* Revert unintended change to GSL_SUPPRESS by @beinhaerter in microsoft/GSL#1226
* use cmake presets instead of cmake settings by @carsonRadtke in microsoft/GSL#1219
* Fix std::hash compilation error for gsl::not_null<std::shared_ptr<T>> by @carsonRadtke with @Copilot in microsoft/GSL#1210
* infra: individual test executables by @carsonRadtke in microsoft/GSL#1212
* doc: Update URL in pointers to F.16 C++ Core Guidelines by @maflcko in microsoft/GSL#1232
* doc: Fix to use lower-case anchors in links to C++ Core Guidelines by @maflcko in microsoft/GSL#1234
* gsl::not_null: C.85: Make swap noexcept by @maflcko in microsoft/GSL#1235
* gsl::not_null: C.89: Make a hash noexcept by @maflcko in microsoft/GSL#1236
* ci: Bump to actions/checkout@v6 by @maflcko in microsoft/GSL#1237
* ci: Use microsoft/setup-msbuild@v3 by @maflcko in microsoft/GSL#1240
* narrow_cast instead of static_cast by @beinhaerter in microsoft/GSL#1250
* ci: Bump actions/setup-java@v5 by @maflcko in microsoft/GSL#1249
* remove space in GSL_SUPPRESS by @beinhaerter in microsoft/GSL#1248
* clang-format improvements by @beinhaerter in microsoft/GSL#1251
* feat: implementation of gsl::dyn_array by @carsonRadtke in microsoft/GSL#1228
* ci: fix failing actions by @carsonRadtke in microsoft/GSL#1255
* docs: fix false statements in gsl::dyn_array docs by @carsonRadtke in microsoft/GSL#1254
* dyn_array: fix include tree by @carsonRadtke in microsoft/GSL#1261
* Bump GSL to v5.0.0 by @carsonRadtke in microsoft/GSL#1262
* docs: fix http:// to https:// in CppCoreGuidelines links by @Rajkaran-122 in microsoft/GSL#1267
* Fix floating-to-integral narrow undefined behavior by @carsonRadtke in microsoft/GSL#1266
* Ensure iterators convert to const iterators by @carsonRadtke in microsoft/GSL#1264
* Prevent external construction of checked iterators by @carsonRadtke in microsoft/GSL#1263

## New Contributors
* @ozaktash made their first contribution in microsoft/GSL#1205
* @maflcko made their first contribution in microsoft/GSL#1232
* @Rajkaran-122 made their first contribution in microsoft/GSL#1267

**Full Changelog**: https://github.com/microsoft/GSL/compare/v4.2.2...v5.0.0</pre>
  <p>View the full release notes at <a href="https://github.com/microsoft/GSL/releases/tag/v5.0.0">https://github.com/microsoft/GSL/releases/tag/v5.0.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!17783
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.

2 participants