515 Commits

Author SHA1 Message Date
owenca
a21d17f1d7
[clang-format] Fix a bug in breaking before FunctionDeclarationName (#153924)
Fixes #153891
2025-08-17 12:54:48 -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
06ed4d2c92
[clang-format] Fix a bug in BreakBeforeBinaryOperators: All (#149695)
Fixes #149520
2025-07-20 19:01:46 -07:00
Owen Pan
fa058647fe
[clang-format] Make EndsInComma in ContinuationIndenter consistent (#146256) 2025-06-29 12:41:34 -07:00
Owen Pan
1506ba95d7
[clang-format][NFC] Clean up DisallowLineBreaks lambda (#144255)
See also
https://github.com/llvm/llvm-project/pull/141576/files#r2141808121
2025-06-15 18:28:06 -07:00
Owen Pan
9a894ae794
[clang-format] Parse JSON outermost l_brace as braced list brace (#143327)
See
https://github.com/llvm/llvm-project/issues/65400#issuecomment-2922181979.
2025-06-09 20:34:12 -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
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
a6ccda28f7 [clang-format][NFC] Use better names for a couple of data members 2025-03-05 21:45:01 -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
80f34e2716
[clang-format] Change BracedInitializerIndentWidth to int (#128988)
Fixes #108526
2025-02-27 20:18:02 -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
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
Gedare Bloom
b8734797a3
[clang-format] Fix mismatched break in BlockIndent (#124998)
Near the ColumnLimit a break could be inserted before a right parens
with BlockIndent without a break after the matching left parens. Avoid
these hanging right parens by disallowing breaks before right parens
unless there was a break after the left parens.

Fixes #103306
2025-01-30 19:38:43 -08:00
Ander
e2402615a5
[clang-format] Fix option BreakBinaryOperations for operator >> (#122282)
Fixes #106228.
2025-01-17 19:45:10 -08:00
Owen Pan
f03bf8c45f
[clang-format] Don't change breaking before CtorInitializerColon (#119522)
Don't change breaking before CtorInitializerColon with `ColumnLimit: 0`.

Fixes #119519.
2024-12-18 12:42:26 -08:00
Gedare Bloom
57c161a647
[clang-format] Detect nesting in template strings (#119989)
The helper to check if a token is in a template string scans too far
backward. It should stop if a different scope is found.

Fixes #107571
2024-12-17 08:05:01 -08:00
Owen Pan
0fe12a7db3 [clang-format][NFC] Remove a pointer in ContinuationIndenter 2024-11-25 00:35:50 -08: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
Gedare Bloom
df935ff4ec
[clang-format] Fix regression with BlockIndent of Braced Initializers (#108717)
Fixes #73584.
2024-09-22 14:31:19 -07:00
Gedare Bloom
c9aa9d53b6
[clang-format] Fix regression in BAS_AlwaysBreak for-await (#108634)
Fixes #108589.
2024-09-18 19:44:09 -07:00
Owen Pan
8168088f0a
[clang-format] Fix regressions in BAS_AlwaysBreak (#107506)
Fixes #107401.
Fixes #107574.
2024-09-11 19:34:54 -07:00
Owen Pan
438ad9f2bf
[clang-format] Revert "[clang-format][NFC] Delete TT_LambdaArrow (#70… (#105923)
…519)"

This reverts commit e00d32afb9d33a1eca48e2b041c9688436706c5b and adds a
test for lambda arrow SplitPenalty.

Fixes #105480.
2024-08-28 18:23:54 -07:00
kadir çetinkaya
e439fdf4ea
[clang-format] Treat new expressions as simple functions (#105168)
ccae7b461be339e717d02f99ac857cf0bc7d17f improved handling for nested
calls, but this resulted in a lot of changes near `new` expressions.

This patch tries to restore previous behavior around new expressions, by
treating them as simple functions, which seem to align with the concept.

Fixes https://github.com/llvm/llvm-project/issues/105133.
2024-08-24 03:04:30 +02: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
Nathan Sidwell
4cf9a42661
[clang-format] Adjust requires clause wrapping (#101550) (#102078)
Address #101550 by adding OwnLineWithBrace option for RequiresClausePosition. This permits placing a following '{' on the same line as the requires clause.

Thus, instead of:
```
bool Foo ()
  requires(true)
{
  return true;
}
```

we have:
```
bool Foo ()
  requires(true) {
  return true;
}
```

If the function body is empty, we'll get:
```
bool Foo ()
  requires(true) {}
```

I attempted to get a line break between the open and close braces, but
failed. Perhaps that's fine -- it's rare and only happens in the empty
body case.
2024-08-16 21:05:08 -04: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
ccae7b461b
[clang-format] Improve BlockIndent at ColumnLimit (#93140)
Fixes #55731

The reported formatting problems were related to ignoring deep nesting
of "simple" functions (causing #54808) and to allowing the trailing
annotation to become separated from the closing parens, which allowed a
break to occur between the closing parens and the trailing annotation.
The fix for the nesting of "simple" functions is to detect them more
carefully. "Simple" was defined in a comment as being a single
non-expression argument. I tried to stay as close to the original intent
of the implementation while fixing the various bad formatting reports.

In the process of fixing these bugs, some latent bugs were discovered
related to how JavaScript Template Strings are handled. Those are also
fixed here.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-07-24 19:47:11 -07:00
c8ef
d9593c1edd
[clang-format] Fix a bug in indenting lambda trailing arrows (#94560)
Closes #94181
2024-06-10 09:10:30 -07:00
Owen Pan
5e0fc93d01
[clang-format] Fix a bug in AlignAfterOpenBracket: DontAlign (#94561)
Fixes #94555.
2024-06-06 17:25:46 -07:00
Kazu Hirata
deffae5da1
[clang] Use StringRef::operator== instead of StringRef::equals (NFC) (#91844)
I'm planning to remove StringRef::equals in favor of
StringRef::operator==.

- StringRef::operator==/!= outnumber StringRef::equals by a factor of
  24 under clang/ in terms of their usage.

- The elimination of StringRef::equals brings StringRef closer to
  std::string_view, which has operator== but not equals.

- S == "foo" is more readable than S.equals("foo"), especially for
  !Long.Expression.equals("str") vs Long.Expression != "str".
2024-05-11 11:38:52 -07:00
Owen Pan
d61edecbfd
[clang-format] Fix a regression in ContinuationIndenter (#88414)
Commit d06b92391513 caused a regression that breaks after a block
comment adjacent to a function paramter that follows.

Fixes #86573.
2024-04-12 10:12:24 -07:00
Owen Pan
b2082a9817 Revert "[clang-format][NFC] Delete 100+ redundant #include lines in .cpp files"
This reverts commit b92d6dd704d789240685a336ad8b25a9f381b4cc. See
github.com/llvm/llvm-project/commit/b92d6dd704d7#commitcomment-139992444

We should use a tool like Visual Studio to clean up the headers.
2024-03-19 21:28:22 -07:00
Owen Pan
6f31cf51df Revert "[clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599)"
This reverts c3a1eb6207d8 (and the related commit f3c5278efa3b) which makes
cleanupAroundReplacements() no longer thread-safe.
2024-03-19 18:06:59 -07:00
Hirofumi Nakamura
0c423af59c
[clang-format] Add Options to break inside the TableGen DAGArg. (#83149)
Add two options to control the line break inside TableGen DAGArg.
- TableGenBreakInsideDAGArg
- TableGenBreakingDAGArgOperators
2024-03-19 00:13:59 +09:00
Owen Pan
b92d6dd704 [clang-format][NFC] Delete 100+ redundant #include lines in .cpp files 2024-03-16 22:24:11 -07:00
Owen Pan
c3a1eb6207 Reland [clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599)
Initialize IsCpp in LeftRightQualifierAlignmentFixer ctor.
2024-03-14 19:44:40 -07:00
Mehdi Amini
b0d1e32ca2
Revert "[clang-format][NFC] Eliminate the IsCpp parameter in all functions" (#85353)
Reverts llvm/llvm-project#84599

This broke the presubmit bot.
2024-03-14 19:33:11 -07:00
Owen Pan
0c07102927
[clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599) 2024-03-14 18:56:24 -07:00
rmarker
22fc2cb15d
[clang-format] Fix AllowShortLambdasOnASingleLine interfering with lambda brace wrapping. (#81848)
Fix an issue where the lambda body left brace could sometimes fail to be
wrapped when AllowShortLambdasOnASingleLine is enabled.

Now, when BraceWrapping.BeforeLambdaBody is enabled, if the brace is not
wrapped, we prevent breaks in the lambda body.

Resolves #81845
2024-02-19 14:32:06 -08:00
Hirofumi Nakamura
8c1c94e8c1
[clang-format] Support of TableGen basic format restrictions. (#81611)
- Allow/force to break the line or not.
- Allow/force to insert space or not.
2024-02-16 21:35:45 +09: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
Owen Pan
7664ddf881 [clang-format][NFC] Drop "Always" in "AlwaysBreakTemplateDeclarations" 2024-02-09 20:18:24 -08:00
Owen Pan
d033799050
[clang-format] Add Leave to AlwaysBreakTemplateDeclarations (#80569)
Closes #78067.
2024-02-07 20:24:44 -08:00
sstwcw
617602d4f2 [clang-format] Handle generic selections inside parentheses (#79785)
new

```C
while (_Generic(x, //
           long: x)(x) > x) {
}
while (_Generic(x, //
           long: x)(x)) {
}
```

old

```C
while (_Generic(x, //
       long: x)(x) > x) {
}
while (_Generic(x, //
    long: x)(x)) {
}
```

In the first case above, the second line previously aligned to the open
parenthesis.  The 4 spaces did not get added by the fallback line near
the end of getNewLineColumn because there was already some indentaton.
Now the spaces get added explicitly.

In the second case above, without the fake parentheses, the second line
did not respect the outer parentheses, because the LastSpace field did
not get set without the fake parentheses.  Now the indentation of the
outer level is used instead.
2024-02-06 05:02:12 +00: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
Gedare Bloom
4ef646eab3
[clang-format]: Fix formatting of if statements with BlockIndent (#77699)
A bug with BlockIndent prevents line breaks within if (and else if)
clauses.

While fixing this bug, it appears that AlignAfterOpenBracket is not
designed to work with loop and if statements, but AlwaysBreak works on
if clauses. The documentation and tests are not clear on whether or not
this behavior is intended.

This PR preserves the `AlwaysBreak` behavior on `if` clauses without
supporting `BlockIndent` on `if` clauses to avoid regressions while
fixing the bug.

It may be reasonable to create an explicit option for alignment of if
(and loop) clauses intentionally for both `AlwaysBreak` and
`BlockIndent`

Fixes #54663.

Migrated from Differential Revision: https://reviews.llvm.org/D154755
See more discussion there. Addressed last open comment from the rev
about refactoring the complex conditional logic involved with the
`AlignAfterOpenBracket` line break behavior.
2024-01-22 19:01:16 -08:00
Owen Pan
3b171cb968
[clang-format] Fix a bug in ContinuationIndenter (#78921)
Fixes #76991.
2024-01-22 18:59:08 -08:00
Emilia Kond
fa6025e25b
[clang-format] Don't confuse initializer equal signs in for loops (#77712)
clang-format has logic to align declarations of multiple variables of
the same type, aligning them at the equals sign. This logic is applied
in for loops as well. However, this alignment logic also erroneously
affected the equals signs of designated initializers.

This patch forbids alignment if the token 2 tokens back from the equals
sign is a designated initializer period.

Fixes https://github.com/llvm/llvm-project/issues/73902
2024-01-22 14:57:37 +02:00