Skip to content

[clang-format] Add cmake target clang-format-style-options for updating ClangFormatStyleOptions.rst #111513

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

Merged
merged 9 commits into from
Dec 5, 2024

Conversation

ichaer
Copy link
Contributor

@ichaer ichaer commented Oct 8, 2024

  • Create a new clang-format-style-options build target which re-generates ClangFormatStyleOptions.rst from its source header files.

As discussed in #96804 (comment)

ichaer added 5 commits October 8, 2024 10:52
Add a change to clang/include/clang/Format/Format.h so we can see the new CI check working.
…rmat-style

* llvm-trunk/main: (5932 commits)
  [AArch64] NFC: rename subreg zsub to qsub (llvm#111154)
  [libomp][AIX] Use SO version "1" for AIX libomp (llvm#111384)
  [clang][Sema] Bad register variable type error should point to the type (llvm#110239)
  [AMDGPU] Include WWM register spill into BB Prolog (llvm#111496)
  [lldb][test] Fix typo in TestSharedLibStrippedSymbols
  [RISCV] Add cost tests for more interleave factors. NFC
  [NFC][EarlyIfConverter] Rename SSAIfConv::runOnMachineFunction to SSAIfConv::init (llvm#111500)
  [Clang] [NFC] Remove trailing whitespace in release notes (llvm#111506)
  [Clang][AArch64] Fix checkArmStreamingBuiltin for 'sve-b16b16' (llvm#109420)
  [llvm][docs] Improve the formatting of the Common Problems section (llvm#108522)
  [lldb][test] Remove xfails from static lib tests on Windows
  [lldb][test] Fix unexpected pass of TestBSDArchives on Windows
  Fix comment typo in ExpandFCOPYSIGN (llvm#111489)
  Revert "[NFC][EarlyIfConverter] Turn SSAIfConv into a local variable (llvm#107390)" (llvm#111385)
  Recommit "[RISCV][FMV] Support target_version" (llvm#111096)" (llvm#111333)
  [mlir][SCF][NFC] `scf.for`/`scf.while`: rename builder args (llvm#111493)
  [libc++][CI] Replace LLDB test targets with libc++ test category (llvm#110856)
  [Clang] [Sema] Effects: Correctly detect `(x ? a : b)` as nonblocking when a and b are (llvm#111224)
  [ConstantFPRange] Implement `ConstantFPRange::makeExactFCmpRegion` (llvm#111490)
  [AArch64][GlobalISel] Scalarize i128/fp128 vector loads/stores.
  ...
@ichaer
Copy link
Contributor Author

ichaer commented Oct 8, 2024

@ichaer
Copy link
Contributor Author

ichaer commented Oct 8, 2024

@owenca, see what you think.

@ichaer ichaer marked this pull request as ready for review October 8, 2024 14:06
@llvmbot llvmbot added clang Clang issues not falling into any other category github:workflow labels Oct 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 8, 2024

@llvm/pr-subscribers-clang-format
@llvm/pr-subscribers-github-workflow

@llvm/pr-subscribers-clang

Author: Iuri Chaer (ichaer)

Changes
  • Create a new clang-format-style-options build target which re-generates ClangFormatStyleOptions.rst from its source header files.
  • Add CI check confirming that the repository's ClangFormatStyleOptions.rst is up-to-date, so that it doesn't have to be done by reviewers.

As discussed in #96804 (comment)


Full diff: https://github.com/llvm/llvm-project/pull/111513.diff

2 Files Affected:

  • (modified) .github/workflows/docs.yml (+13)
  • (modified) clang/docs/CMakeLists.txt (+6)
diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml
index 800e9291573533..f559fbd237a99d 100644
--- a/.github/workflows/docs.yml
+++ b/.github/workflows/docs.yml
@@ -75,6 +75,9 @@ jobs:
               - 'clang/include/clang/Basic/AttrDocs.td'
               - 'clang/include/clang/Driver/ClangOptionDocs.td'
               - 'clang/include/clang/Basic/DiagnosticDocs.td'
+            clang-format_style-headers:
+              - 'clang/include/clang/Format/Format.h'
+              - 'clang/include/clang/Tooling/Inclusions/IncludeStyle.h'
             clang-tools-extra:
               - 'clang-tools-extra/docs/**'
             lldb:
@@ -122,6 +125,16 @@ jobs:
         run: |
           cmake -B clang-build -GNinja -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_SPHINX=ON ./llvm
           TZ=UTC ninja -C clang-build docs-clang-html docs-clang-man
+      - name: Confirm ClangFormatStyleOptions.rst is up-to-date
+        if: steps.docs-changed-subprojects.outputs.clang-format_style-headers_any_changed == 'true'
+        run: |
+          cmake -B clang-build -GNinja -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_SPHINX=ON ./llvm
+          TZ=UTC ninja -C clang-build clang-format-style-options
+          GIT_STATUS="$(git status --porcelain=v1 clang/docs/ClangFormatStyleOptions.rst)"
+          if [ ! -z "${GIT_STATUS}" ]; then
+            echo "Error: you must build the 'clang-format-style-options' target and commit any changes to 'clang/docs/ClangFormatStyleOptions.rst'"
+            exit 1
+          fi
       - name: Build clang-tools-extra docs
         if: steps.docs-changed-subprojects.outputs.clang-tools-extra_any_changed == 'true'
         run: |
diff --git a/clang/docs/CMakeLists.txt b/clang/docs/CMakeLists.txt
index 4fecc007f59954..93a4356c1d53bb 100644
--- a/clang/docs/CMakeLists.txt
+++ b/clang/docs/CMakeLists.txt
@@ -1,3 +1,9 @@
+add_custom_target(clang-format-style-options
+    COMMAND "${Python3_EXECUTABLE}" dump_format_style.py
+    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/tools"
+    DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../include/clang/Format/Format.h"
+            "${CMAKE_CURRENT_SOURCE_DIR}/../include/clang/Tooling/Inclusions/IncludeStyle.h")
+add_dependencies(clang-format clang-format-style-options)
 
 if (DOXYGEN_FOUND)
 if (LLVM_ENABLE_DOXYGEN)

Copy link
Contributor

@boomanaiden154 boomanaiden154 left a comment

Choose a reason for hiding this comment

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

The documentation build workflow is not really the correct place to put this.

Also, why can't we just run the python script as part of building the docs instead of committing the result of the script to the repository?

@ichaer
Copy link
Contributor Author

ichaer commented Oct 8, 2024

The documentation build workflow is not really the correct place to put this.

Yeah, I agree, but I couldn't find a place which looked correct 😅. This thing of adding built artifacts to the repo always introduces sadness... the documentation build workflow looked like the least bad place. I'll gladly take input on this!

Also, why can't we just run the python script as part of building the docs instead of committing the result of the script to the repository?

That's an excellent question, maybe @owenca will know! I should have asked in #96804 (comment); what I did instead was try to reason out an explanation based on what I could see, and I came up with two theories. The first one is that we want for ClangFormatStyleOptions.rst to be available to anyone browsing the repo... it's not very convincing, but it doesn't sound absurd. I could definitely get behind my second theory, though: we want to force everyone to review ClangFormatStyleOptions.rst before merging changes to the header files from which it is produced, and putting it in the repo is a good way to inject that into the code review process.

Anyhow, I'm proposing this because in #96804 (comment) I thought that it was an unreasonable burden for project maintainers having to manually gate on changes which may affect ClangFormatStyleOptions.rst, and also an unnecessary source of friction during code reviews, so I volunteered to automate that part.

@boomanaiden154
Copy link
Contributor

That's an excellent question, maybe @owenca will know! I should have asked in #96804 (comment); what I did instead was try to reason out an explanation based on what I could see, and I came up with two theories. The first one is that we want for ClangFormatStyleOptions.rst to be available to anyone browsing the repo... it's not very convincing, but it doesn't sound absurd. I could definitely get behind my second theory, though: we want to force everyone to review ClangFormatStyleOptions.rst before merging changes to the header files from which it is produced, and putting it in the repo is a good way to inject that into the code review process.

Based on the commit history (d278e0e, https://reviews.llvm.org/D1597), it seems like the real answer might be that it just never got implemented. That looks like it was from the time where there were two build systems that both needed to be maintained at the same time, so maybe somewhat understandable it wasn't implemented that way. There is precedent for generating docs from other files (several clang docs pages are generated through tablegen. So I'd prefer to see a patch doing that rather than just asserting that it is the same. I don't think there should be issues with the Python requirement either. Most importantly, we already require a relatively recent Python in CMake, so its guaranteed to be available, and secondly building clang-format as part of a bootstrapping process doesn't make much sense.

That is my read on it, although I'm not a clang-format maintainer, so they might have more context there that I am not aware of.

Anyhow, I'm proposing this because in #96804 (comment) I thought that it was an unreasonable burden for project maintainers having to manually gate on changes which may affect ClangFormatStyleOptions.rst, and also an unnecessary source of friction during code reviews, so I volunteered to automate that part.

Yeah, I saw the thread. Thanks for looking into process improvements here.

@ichaer
Copy link
Contributor Author

ichaer commented Oct 9, 2024

So I'd prefer to see a patch doing that rather than just asserting that it is the same.

@boomanaiden154, I'm willing to do that, but could we confirm with a clang-format maintainer first? In the course of that code review I referenced I did introduce an error in the documentation through a valid change to the header files, and @mydeveloperday was able to catch it by looking at the file generated. The error I introduced would have been hard to catch through automation. I suppose the same could be said for all of the human-consumable artifacts, all the docs, but my impression is that this is a special case, user-facing documentation produced from comments in the source code.

My instinct is to agree with you and not version-control artifacts, I just want to make sure that the folks who are dealing with that part of the codebase more frequently agree that change would be net positive.

@owenca
Copy link
Contributor

owenca commented Oct 10, 2024

Also, why can't we just run the python script as part of building the docs instead of committing the result of the script to the repository?

Because only part of the doc is generated by dump_format_style.py. See

This file is automatically generated, in part. Do not edit the style options
.

@owenca
Copy link
Contributor

owenca commented Oct 10, 2024

@owenca, see what you think.

IMO it would be more helpful to add the build target to lib/Format/CMakeLists.txt instead. For example,

$ ninja FormatTests
[1/1] cd /Users/Owen/llvm-project/clang....12/bin/python3.12 dump_format_style.p
$ 

I'm ok with not adding the CI check though.

@ichaer
Copy link
Contributor Author

ichaer commented Oct 11, 2024

IMO it would be more helpful to add the build target to lib/Format/CMakeLists.txt instead. For example,

$ ninja FormatTests

Moving the target from clang/docs/CMakeLists.txt to lib/Format/CMakeLists.txt sounds great to me, but the testing part of the thing relies on us being in a git clone, it's a very CI-oriented thing, my impression is that it's more appropriate for it to live in the workflow yaml files. I'm saying this because you suggested FormatTests as the name... I'm not sure if clang-format-style-options is a good name, but I think it's closer to what we can do there. What do you think?

I'm ok with not adding the CI check though.

IMO that would be sad, I would really like to free maintainers up from having to remember this dependency themselves. Is this because you feel .github/workflows/docs.yml is the wrong place for that validation? I see that step more as a pre-build validation for docs than as a test, that's why docs.yml looks like a good home for it from my point of view, but we could move it elsewhere if you're uncomfortable with that.

@owenca
Copy link
Contributor

owenca commented Oct 12, 2024

IMO it would be more helpful to add the build target to lib/Format/CMakeLists.txt instead. For example,

$ ninja FormatTests

Moving the target from clang/docs/CMakeLists.txt to lib/Format/CMakeLists.txt sounds great to me, but the testing part of the thing relies on us being in a git clone, it's a very CI-oriented thing, my impression is that it's more appropriate for it to live in the workflow yaml files. I'm saying this because you suggested FormatTests as the name... I'm not sure if clang-format-style-options is a good name, but I think it's closer to what we can do there. What do you think?

I wasn't suggesting a different name as FormatTests is an existing build target. See

clang_target_link_libraries(FormatTests

Actually, I like the clang-format-style-options name.

Anyway, my current workflow is:

  1. Edit files in clang/lib/Format and clang/unittests/Format.
  2. Edit clang/include/clang/Format/Format.h if needed.
  3. Run ninja FormatTests.
  4. Run cd clang/docs/tools && dump_format_style.py if Step 2 above wasn't skipped.
  5. Run the unit tests.
  6. Run git commit -a.

It would be nice if I didn't have to run step 4 manually.

I'm ok with not adding the CI check though.

IMO that would be sad, I would really like to free maintainers up from having to remember this dependency themselves. Is this because you feel .github/workflows/docs.yml is the wrong place for that validation? I see that step more as a pre-build validation for docs than as a test, that's why docs.yml looks like a good home for it from my point of view, but we could move it elsewhere if you're uncomfortable with that.

I'm not opposed to adding the CI check either.

… gets picked up by `FormatTests` as well.
@ichaer
Copy link
Contributor Author

ichaer commented Oct 15, 2024

It would be nice if I didn't have to run step 4 manually.

Got it! So ideally we'd move clang-format-style-options lower down the stack, to a target shared between clang-format and FormatTests.

I've made it a dependency of the clangFormat library, let me know what you think :).

@boomanaiden154
Copy link
Contributor

Because only part of the doc is generated by dump_format_style.py.

It should still be possible to only include the non-generated parts in the repo and then modify the script to add all the generated components on afterwards as a build step? Not sure if I'm missing something here.

@owenca
Copy link
Contributor

owenca commented Oct 21, 2024

Because only part of the doc is generated by dump_format_style.py.

It should still be possible to only include the non-generated parts in the repo and then modify the script to add all the generated components on afterwards as a build step? Not sure if I'm missing something here.

Sometimes we need to edit the non-generated parts, and it would be easier to have the entire contexts. Anyway, don't we need the generated RST file in the repo so that the corresponding HTML file (e.g. https://clang.llvm.org/docs/ClangFormatStyleOptions.html) will be readily available?

Copy link
Contributor

@owenca owenca left a comment

Choose a reason for hiding this comment

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

@ichaer I suggest that you split the workflow part to another patch so that we can get the cmake part approved.

@boomanaiden154
Copy link
Contributor

#113739 should fix this problem by just adding the options as part of the build and omitting them from the documentation source.

@ichaer
Copy link
Contributor Author

ichaer commented Nov 9, 2024

@ichaer I suggest that you split the workflow part to another patch so that we can get the cmake part approved.

It's a bit sad, but I get your point, let's make it more focused.

@ichaer ichaer changed the title [clang-format] Add CI check confirming ClangFormatStyleOptions.rst is up-to-date. [clang-format] Add new cmake target, clang-format-style-options, for updating ClangFormatStyleOptions.rst. Nov 9, 2024
@owenca owenca changed the title [clang-format] Add new cmake target, clang-format-style-options, for updating ClangFormatStyleOptions.rst. [clang-format] Add cmake target clang-format-style-options for updating ClangFormatStyleOptions.rst Dec 5, 2024
@owenca owenca merged commit f7560ee into llvm:main Dec 5, 2024
8 checks passed
@Andarwinux
Copy link
Contributor

If CLANG_INCLUDE_DOCS=OFF then this should be skipped, otherwise the build will fail if llvm/clang/docs doesn't exist.

nikic added a commit that referenced this pull request Dec 5, 2024
…r updating ClangFormatStyleOptions.rst (#111513)"

Breaks the build when docs are not enabled.

This reverts commit f7560ee.
This reverts commit 6bec180.
@nikic
Copy link
Contributor

nikic commented Dec 5, 2024

Reverted this because it breaks the build. I initially assumed this is due to disabled docs per the previous comment, but I think in my case the problem is actually that this seems to be trying to write to the source directory, which is forbidden. If you need to write out files, that has to happen in the build directory.

FAILED: /var/llvm-compile-time-tracker/llvm-project/clang/docs/ClangFormatStyleOptions.rst 
cd /var/llvm-compile-time-tracker/llvm-project/clang/docs/tools && /usr/bin/python3.10 dump_format_style.py
Traceback (most recent call last):
  File "/var/llvm-compile-time-tracker/llvm-project/clang/docs/tools/dump_format_style.py", line 23, in <module>
    with open(PLURALS_FILE, "a+") as f:
PermissionError: [Errno 13] Permission denied: '/var/llvm-compile-time-tracker/llvm-project/clang/docs/tools/plurals.txt'

@nikic
Copy link
Contributor

nikic commented Dec 5, 2024

Reading back here, the general thing you are trying to do here is not allowed. At best you can add this as a manually invoked target that is not implicitly called by anything else. Though the actually correct way to handle this is what was proposed in #113739.

@owenca
Copy link
Contributor

owenca commented Dec 6, 2024

FAILED: /var/llvm-compile-time-tracker/llvm-project/clang/docs/ClangFormatStyleOptions.rst 
cd /var/llvm-compile-time-tracker/llvm-project/clang/docs/tools && /usr/bin/python3.10 dump_format_style.py
Traceback (most recent call last):
  File "/var/llvm-compile-time-tracker/llvm-project/clang/docs/tools/dump_format_style.py", line 23, in <module>
    with open(PLURALS_FILE, "a+") as f:
PermissionError: [Errno 13] Permission denied: '/var/llvm-compile-time-tracker/llvm-project/clang/docs/tools/plurals.txt'

Changed the mode to read-only in 74d29c6.

@owenca
Copy link
Contributor

owenca commented Dec 6, 2024

If CLANG_INCLUDE_DOCS=OFF then this should be skipped, otherwise the build will fail if llvm/clang/docs doesn't exist.

This was fixed in f0b09df, which also addressed an issue that ClangFormatStyleOptions.rst would be deleted by ninja clean.

@owenca owenca removed the clang Clang issues not falling into any other category label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants