Skip to content

clang-format: Add -disable-format option #137617

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DaanDeMeyer
Copy link
Contributor

When #27416 was fixed it became impossible to only use clang-format for include sorting. Let's introduce an option -disable-format to make it possible again to only sort includes with clang-format without doing any other formatting.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang-format labels Apr 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 28, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-format

Author: Daan De Meyer (DaanDeMeyer)

Changes

When #27416 was fixed it became impossible to only use clang-format for include sorting. Let's introduce an option -disable-format to make it possible again to only sort includes with clang-format without doing any other formatting.


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

2 Files Affected:

  • (modified) clang/docs/ClangFormat.rst (+2)
  • (modified) clang/tools/clang-format/ClangFormat.cpp (+9-2)
diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 92af06e5083d6..a0de662b5c93b 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -95,6 +95,8 @@ to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.
                                      determined by the QualifierAlignment style flag
     --sort-includes                - If set, overrides the include sorting behavior
                                      determined by the SortIncludes style flag
+    --disable-format               - If set, only sort includes if include sorting
+                                     is enabled
     --style=<string>               - Set coding style. <string> can be:
                                      1. A preset: LLVM, GNU, Google, Chromium, Microsoft,
                                         Mozilla, WebKit.
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index c45e3a2c28327..da3d7edf2649f 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -120,6 +120,12 @@ static cl::opt<bool>
                           "determined by the SortIncludes style flag"),
                  cl::cat(ClangFormatCategory));
 
+static cl::opt<bool>
+    DisableFormat("disable-format",
+                  cl::desc("If set, only sort includes if include sorting\n"
+                           "is enabled"),
+                  cl::cat(ClangFormatCategory));
+
 static cl::opt<std::string> QualifierAlignment(
     "qualifier-alignment",
     cl::desc("If set, overrides the qualifier alignment style\n"
@@ -506,8 +512,9 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
   // Get new affected ranges after sorting `#includes`.
   Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
   FormattingAttemptStatus Status;
-  Replacements FormatChanges =
-      reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
+  Replacements FormatChanges;
+  if (DisableFormat.getNumOccurrences() == 0 || !DisableFormat)
+    FormatChanges = reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
   Replaces = Replaces.merge(FormatChanges);
   if (DryRun) {
     return Replaces.size() > (IsJson ? 1u : 0u) &&

Copy link

github-actions bot commented Apr 28, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@DaanDeMeyer DaanDeMeyer force-pushed the disable-format branch 6 times, most recently from 5a2912a to e4d8ac4 Compare April 28, 2025 12:07
DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 28, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 applied on all .c
and .h files.

Our clang-format include sorting configuration will always put various
glibc headers that might conflict with linux headers first to avoid these
conflicts. This is a little overzealous as it will put these headers first
regardless of whether the conflicting Linux header is included or not but
since it's very hard to figure out whether the conflicting Linux header is
included (transitively) or not, being a bit overzealous shouldn't be a big
problem and might actually end up saving developers some pain down the road.
When llvm#27416 was fixed it
became impossible to only use clang-format for include sorting. Let's
introduce an option -disable-format to make it possible again to only
sort includes with clang-format without doing any other formatting.
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.

This looks confusing. I would expect -disable-format to mean what it says. (But then why run clang-format?)

If we allow turning on SortIncludes only, people will start asking for QualifierAlignment only, IntegerLiteralSeparator only, etc. If @mydeveloperday is not against this, then we should upgrade DisableFormat to a struct with the supported exceptions as members.

@DaanDeMeyer
Copy link
Contributor Author

I don't mind the exact interface too much tbh, I would just like to be able to run clang-format again to only sort includes without doing any other formatting. Happy to implement whatever makes folks happy.

@philnik777
Copy link
Contributor

Can't you just run the llvm-include-order clang-tidy check instead?

@DaanDeMeyer
Copy link
Contributor Author

Can't you just run the llvm-include-order clang-tidy check instead?

Does that take IncludeBlocks and IncludeCategories from .clang-format into account?

DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 29, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 applied on all .c
and .h files.

This commit fixes numerous cases where includes weren't sorted alphabetically
and moves all <linux/*.h> includes to the very bottom after our own includes
to avoid conflicts when a linux header is included before a glibc one such as
<netinet/in.h> or <net/if.h> which causes compilation failures.
DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 29, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 applied on all .c
and .h files.

This commit fixes numerous cases where includes weren't sorted alphabetically
and moves all <linux/*.h> includes to the very bottom after our own includes
to avoid conflicts when a linux header is included before a glibc one such as
<netinet/in.h> or <net/if.h> which causes compilation failures.
@philnik777
Copy link
Contributor

Can't you just run the llvm-include-order clang-tidy check instead?

Does that take IncludeBlocks and IncludeCategories from .clang-format into account?

I don't think so, but it shouldn't be impossible to teach clang-tidy.

DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 29, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 applied on all .c
and .h files.

This commit fixes numerous cases where includes weren't sorted alphabetically
and moves all <linux/*.h> includes to the very bottom after our own includes
to avoid conflicts when a linux header is included before a glibc one such as
<netinet/in.h> or <net/if.h> which causes compilation failures.
DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 29, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 applied on all .c
and .h files.
DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 29, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 and
llvm/llvm-project#137840 applied on all .c
and .h files.
DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 29, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 and
llvm/llvm-project#137840 applied on all .c
and .h files.
DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 29, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 and
llvm/llvm-project#137840 applied on all .c
and .h files.
DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 29, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 and
llvm/llvm-project#137840 applied on all .c
and .h files.
DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 30, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 and
llvm/llvm-project#137840 applied on all .c
and .h files.
DaanDeMeyer added a commit to DaanDeMeyer/systemd that referenced this pull request Apr 30, 2025
This was done by running a locally built clang-format with
llvm/llvm-project#137617 and
llvm/llvm-project#137840 applied on all .c
and .h files.
@owenca owenca requested a review from mydeveloperday May 1, 2025 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category clang-format
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants