2089 Commits

Author SHA1 Message Date
owenca
6cfedea492
[clang-format] Add SpaceInEmptyBraces option (#153765)
Also set it to SIEB_Always for WebKit style.

Closes #85525.
Closes #93635.
2025-08-17 12:56:22 -07:00
owenca
a21d17f1d7
[clang-format] Fix a bug in breaking before FunctionDeclarationName (#153924)
Fixes #153891
2025-08-17 12:54:48 -07:00
owenca
5e57a10f50
[clang-format] Allow breaking before bit-field colons (#153529)
Fixes #153448
2025-08-17 12:54:23 -07:00
Connector Switch
8b7f81f2de
[NFC] Fix assignment typo. (#151864) 2025-08-03 22:32:00 +08:00
Owen Pan
3d99446809
[clang-format] Fix a bug in DerivePointerAlignment: true (#150744)
This effectively reverts a4d4859dc70c046ad928805ddeaf8fa101793394 which
didn't fix the problem that `int*,` was not counted as "Left" alignment.

Fixes #150327
2025-07-28 00:44:12 -07:00
James Y Knight
9281797a57
[clang-format] Google Style: disable DerivePointerAlignment. (#149602)
The [Google C++ Style
Guide](https://google.github.io/styleguide/cppguide.html#Pointer_and_Reference_Expressions)
is being changed to specify that spaces should go after the
asterisk/ampersand, rather than permitting either before or after on a
file-by-file basis.

The new requirement is:
> When referring to a pointer or reference (variable declarations or
> definitions, arguments, return types, template parameters, etc.),
> you must not place a space before the asterisk/ampersand. Use a
> space to separate the type from the declared name (if present).

The [Google ObjC
style](https://google.github.io/styleguide/objcguide.html) is silent on
this matter, but the de-facto style is not being modified at this time.
So, keep DerivePointerAlignment enabled for ObjC language mode.
2025-07-25 11:55:50 -04:00
Owen Pan
a4d4859dc7
[clang-format] Fix a bug in DerivePointerAlignment: true (#150387)
Fixes #150327
2025-07-24 09:41:22 -07:00
Owen Pan
97c953406d
[clang-format] Add AfterNot to SpaceBeforeParensOptions (#150367)
Closes #149971
2025-07-24 09:38:43 -07:00
Eric Li
531cf8298b
[clang-format] Stop ctor initializer from being inlined (#150361)
The colon in a constructor's initializer list triggers the inlining of a
nested block as if it was a conditional operator expression. This
prevents line breaks under certain circumstances when the initializer
list contains braced initializers, which in turn prevents the line
formatter from finding a solution.

In this commit we exclude colons that are a constructor initializer
colon from consideration of nested block inlining.

Fixes #97242.
Fixes #81822.
2025-07-24 04:06:03 -04:00
Owen Pan
4d48996ff0
[clang-format][NFC] Clean up around StringRef initializations (#149765)
Consistently use `constexpr StringRef Code("string literal");`.
2025-07-21 19:11:17 -07:00
sstwcw
12a3afe47d
[clang-format] Remove code related to trigraphs (#148640)
When reviewing #147156, the reviewers pointed out that we didn't need to
support the trigraph. The code never handled it right.

In the debug build, this kind of input caused the assertion in the
function `countLeadingWhitespace` to fail. The release build without
assertions outputted `?` `?` `/` separated by spaces.

```C
#define A ??/
  int i;
```

This is because the code in `countLeadingWhitespace` assumed that the
underlying lexer recognized the entire `??/` sequence as a single token.
In fact, the lexer recognized it as 3 separate tokens. The flag to make
the lexer recognize trigraphs was never enabled.

This patch enables the flag in the underlying lexer. This way, the
program now either turns the trigraph into a single `\` or removes it
altogether if the line is short enough. There are operators like the
`??=` in C#. So the flag is not enabled for all input languages. Instead
the check for the token size is moved from the assert line into the if
line.

The problem was introduced by my own patch 370bee480139 from about 3
years ago. I added code to count the number of characters in the escape
sequence probably just because the block of code used to have a comment
saying someone should add the feature. Maybe I forgot to enable
assertions when I ran the code. I found the problem because reviewing
pull request 145243 made me look at the code again.
2025-07-21 15:40:28 +00:00
Owen Pan
f3a3270dbc [clang-format][NFC] Delete redundant type names after FormatStyle:: 2025-07-20 20:27:37 -07:00
Owen Pan
06ed4d2c92
[clang-format] Fix a bug in BreakBeforeBinaryOperators: All (#149695)
Fixes #149520
2025-07-20 19:01:46 -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
fa058647fe
[clang-format] Make EndsInComma in ContinuationIndenter consistent (#146256) 2025-06-29 12:41:34 -07:00
Naveen Seth Hanig
dd47b845a6
[clang-format] Handle Trailing Whitespace After Line Continuation (P2223R2) (#145243)
Fixes #145226.

Implement
[P2223R2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2223r2.pdf)
in clang-format to correctly handle cases where a backslash '\\' is
followed by trailing whitespace before the newline.
Previously, `clang-format` failed to properly detect and handle such
cases, leading to misformatted code.

With this, `clang-format` matches the behavior already implemented in
Clang's lexer and `DependencyDirectivesScanner.cpp`, which allow
trailing whitespace after a line continuation in any C++ standard.
2025-06-25 18:13:00 +02:00
Owen Pan
6da2acf8e9
[clang-format] Merge short inline function in macro definition body (#140366)
Fix #62356
2025-05-19 01:30:42 -07:00
Owen Pan
0cac25bcf5
[clang-format] Fix the indent of StartOfName after AttributeMacro (#140361)
Fix #139510
2025-05-19 01:29:16 -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
79210feb29
[clang-format] Fix a crash on formatting missing r_paren/r_brace (#138230)
Fix #138097
2025-05-02 19:20:42 -07:00
Owen Pan
d3506ee573
[clang-format] RemoveParentheses shouldn't remove empty parentheses (#138229)
Fix #138124
2025-05-02 19:19:39 -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
82f1f5e5d4
[clang-format] Correctly identify include guards (#137112)
Fix #136814
2025-04-24 20:17:07 -07:00
Owen Pan
da8f2d5242 Revert "[clang-format] Allow breaking before kw___attribute (#128623)"
This reverts commit 8fc8a84e23471fe56214e68706addc712b5a2949, which caused a
regression.

Fixes #136675.
2025-04-22 23:49:26 -07:00
Owen Pan
58a5c469d9
[clang-format] Fix a bug in BWACS_MultiLine (#136281)
Fixes #136266
2025-04-18 18:13:02 -07:00
Owen Pan
c64f670255
[clang-format] Fix a crash in EnumTrailingComma (#135903)
Fix #135819
2025-04-18 18:11:56 -07:00
Owen Pan
9bd0c8726a
[clang-format] Fix a bug in BWACS_MultiLine (#135906)
Fix #51940
2025-04-16 18:48:10 -07:00
Owen Pan
c41ef751f8
[clang-format] Treat lambda in braced init as inline (#135520)
Fix #125430
2025-04-13 09:02:46 -07:00
Owen Pan
5f11d64cfb
[clang-format] Fix a bug in AlignConsecutiveDeclarations (#135516)
Fix #109768
2025-04-13 09:01:37 -07:00
Owen Pan
5f744cc630
[clang-format] Wrap and indent lambda braces in GNU style (#135479)
Fix #133135
2025-04-12 15:06:21 -07:00
Owen Pan
09c8cfe219
[clang-format][NFC] Add isJava() and isTextProto() in FormatStyle (#135466)
Also remove redundant name qualifiers format::, FormatStyle::, and
LanguageKind::.
2025-04-12 15:04:29 -07:00
Owen Pan
f344838389
[clang-format] Keep the space between not and a unary operator (#135035)
Also keep the space between `not` and `::`.

Based on the
[documentation](https://releases.llvm.org/20.1.0/tools/clang/docs/ClangFormatStyleOptions.html#spaceafterlogicalnot),
it can be argued that SpaceAfterLogicalNot doesn't cover the alternative
operator `not`.

Closes #125465
2025-04-09 17:52:12 -07:00
Owen Pan
aaaeb86ace
[clang-format] Merge inline short functions for BS_Whitesmiths (#134473)
Fix #126747
2025-04-05 17:03:55 -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
da7f1564a8
[clang-format] Don't wrap before attributes in parameter lists (#132519)
Fix #132240
2025-03-24 19:18:13 -07:00
Owen Pan
91328dbae9
[clang-format] Correctly annotate user-defined conversion functions (#131434)
Also fix/delete existing invalid/redundant test cases.

Fix #130894
2025-03-16 16:11:39 -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
7d4d8509cb
[clang-format] Don't remove parentheses separated from ellipsis by comma (#130471)
Also clean up `case tok::r_paren` in
`UnwrappedLineParser::parseParens()`.

Fix #130359
2025-03-10 19:42:54 -07:00
Owen Pan
3664b4e2d5
[clang-format] Remove special handling of C++ access specifiers in C (#129983)
This effectively reverts d1aed486efc6d35a81ca4acbabb4203c4b91cda9
because of
#129426.
2025-03-06 21:08:25 -08:00
Owen Pan
024362f413
[clang-format] Insert a space after kw_new by default (#129634)
This effectively reverts dbc4d281bd6954362ccfc0747893ceaae842671b.

Fix #54703
2025-03-04 21:14:07 -08:00
Owen Pan
2ca0855059
[clang-format] Fix a bug in wrapping function return type (#129374)
Fixes #113766
2025-03-03 18:15:32 -08:00
Owen Pan
d29a1be94b
[clang-format] Don't break before *const (#128817)
Fixes #28919
2025-02-26 18:25:09 -08:00
Owen Pan
2d585ccecc
[clang-format] Fix a bug that changes keyword or to an identifier (#128410)
Fixes #105482
2025-02-26 18:14:26 -08:00
Owen Pan
8fc8a84e23
[clang-format] Allow breaking before kw___attribute (#128623)
Fixes #74784
2025-02-25 19:44:54 -08:00
Owen Pan
0968df9c3a
[clang-format] Add the C language instead of treating it like C++ (#128287)
Closes #128120
2025-02-23 00:36:19 -08:00
Owen Pan
ffc61dc393
[clang-format] Allow specifying the language for .h files (#128122)
Closes #128119
2025-02-21 20:46:43 -08:00
Owen Pan
204dcafec0
[clang-format] Fix a bug in BCIS_AfterColon and ColumnLimit: 0 (#127964)
Fixes #127622
2025-02-20 19:06:51 -08:00
Owen Pan
13de15c9c4
[clang-format] Fix a bug in annotating StartOfName (#127545)
Also ensure we can break before ClassHeadName like StartOfName.

Fixes #127470
2025-02-18 00:15:01 -08:00