78 Commits

Author SHA1 Message Date
Owen Pan
97c953406d
[clang-format] Add AfterNot to SpaceBeforeParensOptions (#150367)
Closes #149971
2025-07-24 09:38:43 -07:00
Daan De Meyer
1653a093de
[clang-format] Add IgnoreExtension to SortIncludes (#137840)
Sorting by stem gives nicer results when various header file names are
substrings of other header file names. For example, a CLI application
with a main header named analyze.h and an analyze-xxx.h header for each
subcommand currently will always put analyze.h last after all the
analyze-xxx.h headers, but putting analyze.h first instead is arguably
nicer to read.

TLDR; Instead of
```
#include "analyze-blame.h"
#include "analyze.h"
```
You'd get
```
#include "analyze.h"
#include "analyze-blame.h"
```

Let's allow sorting by stem instead of full path by adding
IgnoreExtension to SortIncludes.
2025-07-17 00:36:52 -07:00
Owen Pan
c384ec431d
[clang-format] Add MacrosSkippedByRemoveParentheses option (#148345)
This allows RemoveParentheses to skip the invocations of function-like
macros.

Fixes #68354.
Fixes #147780.
2025-07-13 14:29:51 -07:00
Owen Pan
7e1a88b9d1
[clang-format] Handle .h files for LK_C and LK_ObjC (#141714)
Fix #137792
2025-05-28 19:10:20 -07:00
Owen Pan
5ddcd765db
[clang-format][NFC] Upgrade SortIncludes option to a struct (#140497)
This allows adding other suboptions e.g. IgnoreExtension in #137840.
2025-05-19 01:32:17 -07:00
Filip Milosevic
5df01abe19
[clang-format] Add SpaceAfterOperatorKeyword option (#137610)
Add SpaceAfterOperatorKeyword option to clang-format
2025-05-07 22:47:56 -07:00
Owen Pan
8effc8da29 Reland [clang-format] Add OneLineFormatOffRegex option (#137577) 2025-04-30 19:58:59 -07:00
Owen Pan
7752e0a10b Revert "[clang-format] Add OneLineFormatOffRegex option (#137577)"
This reverts commit b8bb1ccb4f9126d1bc9817be24e17f186a75a08b which triggered
an assertion failure in CodeGenTest.TestNonAlterTest.
2025-04-30 00:12:41 -07:00
Owen Pan
b8bb1ccb4f
[clang-format] Add OneLineFormatOffRegex option (#137577)
Close #54334
2025-04-29 19:22:53 -07:00
Owen Pan
e5fcbfa2aa
[clang-format] Add an option for editing enum trailing commas (#133576)
Also refactor the code that removes/replaces a token.
2025-03-30 16:02:49 -07:00
Owen Pan
05fb8408de
[clang-format] Allow Language: Cpp for C files (#133033)
Fix #132832
2025-03-27 01:00:02 -07:00
Jan Voung
467ad6a035
[clang-format] Add support for absl nullability macros (#130346)
Add support for formatting w/ absl nullability macros
(c52afac4f8/absl/base/nullability.h (L237)).
Example at https://godbolt.org/z/PYv19M1Gj
input:
```
std::vector<int* _Nonnull> x;
std::vector<int* absl_nonnull> y;
```

orig output:
```
std::vector<int* _Nonnull> x;
std::vector<int * absl_nonnull> y;
```

new output:
```
std::vector<int* _Nonnull> x;
std::vector<int* absl_nonnull> y;
```
credit to @ymand for the original patch
2025-03-14 00:01:35 -07:00
Owen Pan
80f34e2716
[clang-format] Change BracedInitializerIndentWidth to int (#128988)
Fixes #108526
2025-02-27 20:18:02 -08:00
Gedare Bloom
e0a21e23a7
[clang-format] Add BinPackLongBracedList style option (#112482)
The use of Cpp11BracedListStyle with BinPackArguments=False avoids bin
packing until reaching a hard-coded limit of 20 items. This is an
arbitrary choice. Introduce a new style option to allow disabling this
limit.
2025-02-07 22:10:35 -08:00
leijurv
d2b45ce100
[clang-format] Add BreakBeforeTemplateCloser option (#118046)
In clang-format, multiline templates have the `>` on the same line as
the last parameter:

```c++
template <
    typename Foo,
    typename Bar>
void foo() {
```

I would like to add an option to put the `>` on the next line, like
this:

```c++
template <
    typename Foo,
    typename Bar
>
void foo() {
```

An example of a large project that uses this style is NVIDIA's CUTLASS,
here is an example:


https://github.com/NVIDIA/cutlass/blob/main/include/cutlass/epilogue/dispatch_policy.hpp#L149-L156

My reasoning is that it reminds me of this style of braces:

```c++
if (foo()) {
    bar();
    baz();}
```

Most people agree this is better:

```c++
if (foo()) {
    bar();
    baz();
}
```

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-02-06 01:15:47 -08:00
Owen Pan
b28e879a83
[clang-format] Simplify ConfigParseTest for int/unsigned options (#124704)
Also add a number of missing tests for unsigned options.
2025-01-28 21:04:41 -08:00
Gedare Bloom
d50ebd47ae
[clang-format] Add style option PenaltyBreakBeforeMemberAccess (#118409)
The penalty for breaking before a member access is hard-coded to 150.
Add a configuration option to allow setting it.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-01-27 21:40:17 -08:00
Sirraide
33656932b0
[clang-format] Rename ExportBlockIndentation -> IndentExportBlock (#123493)
This renames the `ExportBlockIndentation` option and adds a config parse
test, as requested in #110381.
2025-01-21 14:13:13 +01:00
Owen Pan
66f16e682f
[clang-format][NFC] Add missing config tests for List of Strings (#121451)
Also, simplify the existing test for NamespaceMacros. Like the options
tested by the added tests, it's also a list of arbitrary strings and
initialized to an empty list. (The other existing tests for list of
strings either are initialized to a list of one or more strings or
require specific strings.)
2025-01-04 16:14:25 -08:00
dmasloff
1c997feff1
[clang-format] Add option WrapNamespaceBodyWithNewlines (#106145)
It wraps the body of namespace with additional newlines, turning this code:
```
namespace N {
int function();
}
```
into the following:
```
namespace N {

int function();

}
```

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-01-02 21:52:01 -08:00
Galen Elias
486ec4bd74
[clang-format] Add AllowShortNamespacesOnASingleLine option (#105597)
This fixes #101363 which is a resurrection of a previously opened but
never completed review: https://reviews.llvm.org/D11851

The feature is to allow code like the following not to be broken across
multiple lines:

```
namespace foo { class bar; }
namespace foo { namespace bar { class baz; } }
```

Code like this is commonly used for forward declarations, which are
ideally kept compact. This is also apparently the format that
include-what-you-use will insert for forward declarations.

Also, fix an off-by-one error in `CompactNamespaces` code. For nested
namespaces with 3 or more namespaces, it was incorrectly compacting
lines which were 1 or two spaces over the `ColumnLimit`, leading to
incorrect formatting results.
2024-12-30 01:28:03 -08:00
Owen Pan
786db636b9
[clang-format] Add KeepFormFeed option (#113268)
Closes #113170.
2024-10-23 19:55:32 -07:00
Owen Pan
d989c2410e
[clang-format] Add RemoveEmptyLinesInUnwrappedLines option (#112325)
Fixes #111340.
2024-10-17 20:21:42 -07:00
Iuri Chaer
0fba8381d2
[clang-format] Introduce "ReflowComments: IndentOnly" to re-indent comments without breaking internal structure (think Doxygen). (#96804)
* Convert `ReflowComments` from boolean into a new `enum` which can take
on the value `RCS_Never`, `RCS_IndentOnly`, or `RCS_Always`. The first
one is equivalent to the old `false`, the third one is `true`, and the
middle one means that multiline comments should only have their
indentation corrected, which is what Doxygen users will want.
* Preserve backward compatibility while parsing `ReflowComments`.
2024-10-11 19:14:09 -07:00
Owen Pan
65688274b1
[clang-format][NFC] Clean up AlignConsecutiveStyle (#111285)
- Add a `CHECK_PARSE` for `AcrossComments`.
- Add a `CHECK_PARSE_NESTED_BOOL` for `AlignFunctionPointers`.
- Remove redundant statements.
- Clean up documentation.
2024-10-07 19:12:47 -07:00
Brad House
f0bd62d870
[clang-format] Add AlignFunctionDeclarations to AlignConsecutiveDeclarations (#108241)
Enabling AlignConsecutiveDeclarations also aligns function prototypes
or declarations.  This is often unexpected as typically function
prototypes, especially in public headers, don't use any padding.

Setting AlignFunctionDeclarations to false will skip this alignment.
It is by default set to true to keep compatibility with prior
versions to not make unexpected changes.

Fixes #74320
2024-10-06 17:46:43 -07:00
Tom
7c3237d778
[clang-format] Change BinPackParameters to enum and add AlwaysOnePerLine (#101882)
Related issues that have requested this feature:
#51833
#23796 
#53190 Partially solves - this issue requests is for both arguments and
parameters
2024-08-22 21:42:22 -07:00
Ameer J
c5a4291fb7
[clang-format] Add BreakBinaryOperations configuration (#95013)
By default, clang-format packs binary operations, but it may be
desirable to have compound operations be on individual lines instead of
being packed.

This PR adds the option `BreakBinaryOperations` to break up large
compound binary operations to be on one line each.

This applies to all logical and arithmetic/bitwise binary operations

Maybe partially addresses #79487 ?
Closes #58014 
Closes #57280
2024-08-10 13:28:33 -07:00
Gedare Bloom
54f040fff3
[clang-format] Add SpacesInParensOption for filtering repeated parens (#77522)
The __attribute((specifier-list)) currently is formatted based on the
SpacesInParensOptions.Other (previously, SpacesInParentheses). This
change allows finer control over addition of spaces between the
consecutive parens, and between the inner parens and the list of
attribute specifiers.

Differential Revision: https://reviews.llvm.org/D155529

This is migrated from Phabricator, see more discussion there.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-07-02 00:00:14 -07:00
sstwcw
2853a838d2 [clang-format] Add option to remove leading blank lines (#91221)
The options regarding which blank lines are kept are also aggregated.
The new option is `KeepEmptyLines`.

This patch was initially part of 9267f8f19a2e502e.  I neglected to check
the server builds before I added it.  It broke clangd.  Jie Fu fixed the
problem in 4c91b49bab0728d4.  I was unaware of it.  I thought the main
branch was still broken.  I reverted the first patch in
70cfece24d6cbb57.  It broke his fix.  He reverted it in
c69ea04fb9738db2.  Now the feature is added again including the fix.
2024-06-30 01:20:20 +00:00
sstwcw
70cfece24d Revert "[clang-format] Add option to remove leading blank lines (#91221)"
This reverts commit 9267f8f19a2e502ef5a216c0d52b352b3699d399.

I changed a formatter option.  I forgot to update other components that
depend on the formatter when the option name changed.
2024-06-25 13:12:32 +00:00
sstwcw
9267f8f19a [clang-format] Add option to remove leading blank lines (#91221)
The options regarding which blank lines are kept are also aggregated.
The new option is `KeepEmptyLines`.
2024-06-25 03:50:11 +00:00
Owen Pan
527e732860 [clang-format][NFC] Suppress diagnostic noise in GetStyleOfFile test 2024-06-16 14:56:02 -07:00
pointhex
fe9aef05c2
[clang-format] Add DiagHandler parameter to format::getStyle() (#91317)
It allows to control of error output for the function.

Closes #94205.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-06-16 13:58:26 -07:00
Owen Pan
dba2aa265c
[clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (#93402)
Closes #92999.
2024-05-27 15:20:58 -07:00
Owen Pan
236b3e1aad
[clang-format] Handle Java switch expressions (#91112)
Also adds AllowShortCaseExpressionOnASingleLine option and
AlignCaseArrows suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
2024-05-06 19:55:55 -07:00
rmarker
d821650e13
[clang-format][NFC] Drop "Always" in "AlwaysBreakAfterReturnType". (#81591)
Complete the switch from "AlwaysBreakAfterReturnType" to
"BreakAfterReturnType".
2024-02-14 20:10:56 -08:00
rmarker
91dcf53abd
[clang-format] Rename option AlwaysBreakAfterReturnType. (#80827)
Changes the option to BreakAfterReturnType option, with a more relevant
name, deprecating and replacing AlwaysBreakAfterReturnType.
Following up on #78010.
2024-02-12 20:28:33 -08:00
Owen Pan
7664ddf881 [clang-format][NFC] Drop "Always" in "AlwaysBreakTemplateDeclarations" 2024-02-09 20:18:24 -08:00
Owen Pan
c5cbfc5689
[clang-format] Rename option AlwaysBreakTemplateDeclarations (#81093)
Drop the "Always" prefix to remove the self-contradiction.
2024-02-09 19:53:04 -08:00
Owen Pan
d033799050
[clang-format] Add Leave to AlwaysBreakTemplateDeclarations (#80569)
Closes #78067.
2024-02-07 20:24:44 -08:00
rmarker
2562007321
[clang-format] Add Automatic and ExceptShortType options for AlwaysBreakAfterReturnType. (#78011)
The RTBS_None option in Clang-format avoids breaking after a short
return type.
However, there was an issue with the behaviour in that it wouldn't take
the leading indentation of the line into account.
This meant that the behaviour wasn't applying when intended.

In order to address this situation without breaking the existing
formatting, RTBS_None has been deprecated.
In its place are two new options for AlwaysBreakAfterReturnType.
The option RTBS_Automatic will break after the return type based on
PenaltyReturnTypeOnItsOwnLine.
The option RTBS_ExceptShortType will take the leading indentation into
account and prevent breaking after short return types.

This allows the inconsistent behaviour of RTBS_None to be avoided and
users to decide whether they want to allow breaking after short return
types or not.

Resolves #78010
2024-02-04 12:26:32 -08:00
Owen Pan
908fd09a13
[clang-format] Simplify the AfterPlacementOperator option (#79796)
Change AfterPlacementOperator to a boolean and deprecate SBPO_Never,
which meant never inserting a space except when after new/delete.

Fixes #78892.
2024-01-31 20:20:26 -08:00
Owen Pan
a7d7da6e45
[clang-format] Add SkipMacroDefinitionBody option (#78682)
Closes #67991.

See also: #70338

Co-authored-by: @tomekpaszek
2024-01-20 00:08:23 -08:00
serbanu
59a265311a
[clang-format] Add parse tests for SeparateDefinitionBlocks option (#78256)
Add config parse tests for SeparateDefinitionBlocks option.
2024-01-16 21:22:04 +01:00
rmarker
60a9874f54
[clang-format] Add PenaltyBreakScopeResolution option. (#78015)
Resolves #78014
2024-01-14 20:55:44 +01:00
Gedare Bloom
b2c0c6f3f2
[clang-format]: Split alignment of declarations around assignment (#69340)
Function pointers are detected as a type of declaration using
FunctionTypeLParen. They are aligned based on rules for
AlignConsecutiveDeclarations. When a function pointer is on the
right-hand side of an assignment, the alignment of the function pointer
can result in excessive whitespace padding due to the ordering of
alignment, as the alignment processes a line from left-to-right and
first aligns the declarations before and after the assignment operator,
and then aligns the assignment operator. Injection of whitespace by
alignment of declarations after the equal sign followed by alignment of
the equal sign results in the excessive whitespace.

Fixes #68079.
2024-01-10 19:35:03 -08:00
Owen Pan
39faf13dde
[clang-format] Add BreakAdjacentStringLiterals option (#73432)
Closes #70451.
2023-11-27 13:01:16 -08:00
Owen Pan
88934a82dc [clang-format][NFC] Remove extraneous newlines in some unit test files 2023-10-26 21:43:27 -07:00
Backl1ght
69209e30a7 [clang-format] AllowShortCompoundRequirementOnASingleLine
clang-format brace wrapping did not take requires into consideration,
compound requirements will be affected by BraceWrapping.AfterFunction.

Closes #59412.

Differential Revision: https://reviews.llvm.org/D139834
2023-10-25 05:13:50 -07:00