2041 Commits

Author SHA1 Message Date
Owen Pan
a7166c3739 release/20.x: [clang-format] RemoveParentheses shouldn't remove empty parentheses (#138229)
Backport d3506ee573a2aa1403817642ef45f8c0305bb572
2025-05-09 13:45:23 -07:00
Gedare Bloom
e7ae5532bc [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

(cherry picked from commit b8734797a3f605c4aaa37fcb5007baa273565460)
2025-04-25 16:39:37 -07:00
Owen Pan
d05543ed07 [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

(cherry picked from commit f34483838937b1a01ee11ee22bdd6e13c81e9fff)
2025-04-11 11:33:19 -07:00
Owen Pan
f7a4e3a4d4 [clang-format] Don't remove parentheses separated from ellipsis by comma (#130471)
Also clean up `case tok::r_paren` in
`UnwrappedLineParser::parseParens()`.

Fix #130359

(cherry picked from commit 7d4d8509cbec7eecd8aaf2510015b54bc5c173e1)
2025-03-11 13:47:19 -07:00
Owen Pan
f88a294673 [clang-format] Fix a bug that changes keyword or to an identifier (#128410)
Backports ffc61dc393e4 0968df9c3a55 2d585ccecc45

Fixes #105482
2025-03-04 06:59:18 -08:00
Owen Pan
34f5f905f7 [clang-format] Fix a bug in annotating StartOfName (#127545)
Also ensure we can break before ClassHeadName like StartOfName.

Fixes #127470

(cherry picked from commit 13de15c9c49068db850368c45ffed8f7bbf07f20)
2025-02-18 15:12:11 -08:00
Owen Pan
a7f00c8ed8 [clang-format] Fix a crash on parsing requires clause (#125021)
Fixes #124921.

(cherry picked from commit 14178deab0334d9ce095ae7adce408868659faee)
2025-02-13 18:38:24 -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
Owen Pan
6330f1e052
[clang-format] Fix a regression in PointerAlignment: Left (#124085)
Don't insert a space between a type declaration r_paren and &/&&.

Fixes #124073.
2025-01-23 21:44:29 -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
Sirraide
106c483a10
[clang-format] Improve brace wrapping and add an option to control indentation of export { ... } (#110381)
`export { ... }` blocks can get a bit long, so I thought it would make
sense to have an option that makes it so their contents are not indented
(basically the same argument as for namespaces).

This is based on the `NamespaceIndentation` option, except that there is
no option to control the behaviour of `export` blocks when nested because
nesting them doesn’t really make sense.

Additionally, brace wrapping of short `export { ... }` blocks is now controlled by the
`AllowShortBlocksOnASingleLine` option. There is no separate option just for `export`
blocks because you can just write e.g. `export int x;` instead of `export { int x; }`.

This closes #121723.
2025-01-19 00:26:40 +01:00
Ander
e2402615a5
[clang-format] Fix option BreakBinaryOperations for operator >> (#122282)
Fixes #106228.
2025-01-17 19:45:10 -08:00
Owen Pan
a7bca1861b
[clang-format] Correctly annotate braces in macro definitions (#123279)
Fixes #123179.
2025-01-17 19:26:00 -08:00
Owen Pan
c1ea05eaf0
[clang-format] Don't break short macro call followed by l_paren (#121626)
Fixes #105658.
2025-01-04 16:24:41 -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
fe2685303b [clang-format] Fix a crash caused by commit f03bf8c45f43 2024-12-18 19:41:50 -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
Owen Pan
54ca1c4212
[clang-format] Fix idempotent format of hash in macro body (#118513)
Fixes #118334.
2024-12-10 16:47:21 -08:00
Gedare Bloom
46bf67d8fa
[clang-format] Reorder TokenAnnotator::canBreakBefore (#119044)
Move the checks related to breaking before right braces and right parens
earlier to avoid conflicting checks that prevent breaking based on the
left-hand token. This allows properly formatting declarations with
pointers and references at a minimum.
2024-12-09 21:05:28 -08:00
Owen Pan
cb61a5e420
[clang-format] Fix an assertion failure in RemoveSemicolon (#117472)
Fixes #117290.
2024-12-07 16:47:35 -08:00
Owen Pan
df232d46f5
[clang-format] Don't re-annotate CaseLabelColon as ConditionalExpr (#114639)
Fixes #114627.
2024-11-02 14:42:41 -07: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
1809d0fa1c
[clang-format] Insert a space between l_paren and ref-qualifier (#111465)
Fixes #111346.
2024-10-08 21:24:17 -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
Gedare Bloom
df935ff4ec
[clang-format] Fix regression with BlockIndent of Braced Initializers (#108717)
Fixes #73584.
2024-09-22 14:31:19 -07:00
Owen Pan
a8dd8f6302
[clang-format] Fix a bug in SpacesInParens InConditionalStatements (#108797)
Fixes #64416.
2024-09-17 21:17:30 -07:00
Owen Pan
7153a4bbf6
[clang-format] Reimplement InsertNewlineAtEOF (#108513)
Fixes #108333.
2024-09-17 21:16:20 -07:00
Owen Pan
fac7e87d66
[clang-format] Insert a space between new/delete and a C-style cast (#106175)
It doesn't make sense to remove the space between new/delete and a
C-style cast when SpaceBeforeParensOptions.AfterPlacementOperator is set
to false.

Fixes #105628.
2024-08-27 19:14:16 -07:00
Owen Pan
656d5aa958
[clang-format] Fix misalignments of pointers in angle brackets (#106013)
Fixes #105898.
2024-08-27 19:13:27 -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
Owen Pan
714033a6bf
[clang-format] Don't insert a space between :: and * (#105043)
Also, don't insert a space after ::* for method pointers.

See
https://github.com/llvm/llvm-project/pull/86253#issuecomment-2298404887.

Fixes #100841.
2024-08-22 20:02:48 -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
Owen Pan
9fa17fea3c
[clang-format] Handle parenthesized list in RemoveParentheses (#100852)
Also, reformat clang-format source to remove redundant parentheses
enclosing single list items.

Fixes #100768.
2024-08-02 01:29:05 -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
Owen Pan
0387cd052b
[clang-format] Fix a bug in annotating FunctionAnnotationRParen (#99802)
Fixes #37906.
2024-07-21 14:54:50 -07:00
Owen Pan
5bb3492892
[clang-format] Fix a bug in SpaceBeforeParensOptions (#98849)
Handle constructors/destructors for AfterFunctionDeclarationName and
AfterFunctionDefinitionName.

Fixes #98812.
Fixes #98820.
2024-07-15 19:17:36 -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
fa00e8bb6a
[clang-format] Correctly annotate l_brace after TypenameMacro (#96026)
Closes #95418.
2024-06-19 17:49:35 -07:00
Owen Pan
a106131a34
[clang-format] Handle AttributeMacro before access modifiers (#95634)
Closes #95094.
2024-06-16 13:50:59 -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
Owen Pan
798f2019a2
[clang-format] Don't format comments in SkipMacroDefinitionBody (#94425)
Fixes #94326.
2024-06-05 17:57:36 -07:00