1963 Commits

Author SHA1 Message Date
Owen Pan
04fbc461e0
[clang-format] Fix RemoveSemicolon for empty functions (#82278)
Fixes #79833.
2024-02-20 21:51:51 -08: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
Owen Pan
92ef40874d [clang-format] Delete a redundant newline at the end of a test case
It slipped in from commit fa6025e25b57.
2024-02-16 01:47:49 -08: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
eafe98f937
[clang-format] Always add a space after #if and #elif (#81578)
Fixes #81569.
2024-02-13 22:35:52 -08:00
Owen Pan
4af24d4ab7
[clang-format] Don't remove parentheses in macro definitions (#81444)
Closes #81399.
2024-02-12 19:20:26 -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
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
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
XDeme
a464e05109
[clang-format] Handle templated elaborated type specifier in function… (#77013)
… return type.

The behavior now is consistent with the non template version.
Enabled and updated old test.
2024-01-20 14:47:58 -08:00
XDeme
2fc2ee136c
[clang-format] Fix poor spacing in AlignArrayOfStructures: Left (#77868)
Fixes llvm/llvm-project#62904

`AlignArrayOfStructures: Left` combined with `SpacesInParentheses: true`
causes the first cell of every row to have 1 additional space.
We were only setting the first cell of the first row to be against the
left brace, now every row will be against the left brace.
2024-01-20 13:34:37 -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
Ilya Biryukov
5723fce088
[Format] Fix isStartOfName to recognize attributes (#76804)
This addresses a problem with formatting attributes. Some context:
- eaff083035c8 changed `isStartOfName` to fix problems inside
`#pragma`s, but this behavior changed formatting of attribute macros in
an undesirable way.
- efeb546865c233dfa7706ee0316c676de9f69897 changed Google format style
to fix some widely used attributes.

Instead of changing the format style, this commit specializes behavior
introduced in eaff083035c8 to `#pragma`s. This seems to work well in
both cases.

Also update the test with two `GUARDED_BY` directives. While the
formatting after efeb546865c233dfa7706ee0316c676de9f69897 seems better,
this case is rare enough to not warrant the extra complexity. We are
reverting it back to the state it had before
efeb546865c233dfa7706ee0316c676de9f69897.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-01-15 14:57:05 +01:00
rmarker
60a9874f54
[clang-format] Add PenaltyBreakScopeResolution option. (#78015)
Resolves #78014
2024-01-14 20:55:44 +01:00
XDeme
97a9dbb649
[clang-format] Handle possible crash in getCells (#77723)
Done as requested in llvm/llvm-project#77045

I have changed the test a bit, because since the root problem was fixed,
the original test would possibly never crash.
2024-01-12 10:20:44 -08:00
XDeme
093e6bdd4b
[clang-format] Fix crash involving array designators (#77045)
Fixes llvm/llvm-project#76716
Fixes parsing of `[0]{}`. Before this patch it was begin parsed as a
lambda, now it is correctly parsed as a designator initializer.
2024-01-10 19:46:11 -08: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
James Grant
cc77e33271
[clang-format] Don't apply severe penalty if no possible column formats (#76675)
If there are possible column formats, but they weren't selected because
they don't fit within remaining characters for the current path then
applying severe penalty to induce column layout by selection of a
different path seems fair.

But if due to style configuration or what the input code is, there are
no possible column formats, different paths aren't going to have column
layouts. Seems wrong to apply the severe penalty to induce column
layouts if there are none available.

It just causes selection of sub-optimal paths, e.g. get bad formatting
when brace initializers are used inside lambda bodies.

Fixes #56350
2024-01-10 19:32:14 -08:00
kadir çetinkaya
27f547968c
[clang-format] Break after string literals with trailing line breaks (#76795)
This restores a subset of functionality that was forego in
d68826dfbd987377ef6771d40c1d984f09ee3b9e.

Streaming multiple string literals is rare enough in practice, hence
that change makes sense in general. But it seems people were
incidentally relying on this for having line breaks after string
literals that ended with `\n`.

This patch tries to restore that behavior to prevent regressions in the
upcoming LLVM release, until we can implement some configuration based
approach as proposed in https://github.com/llvm/llvm-project/pull/69859.
2024-01-08 11:11:02 +01:00
XDeme
41ef6fc54f
[clang-format] Fix bad indentation with attribute and templated type (#76336)
Fixes llvm/llvm-project#76314
2023-12-29 21:27:53 +01:00
XDeme
8097a5d37b
[clang-format] Fix operator overload inconsistency in BreakAfterAttributes: Always (#74943)
Fixes llvm/llvm-project#74901
2023-12-22 23:02:47 -08:00
Ilya Biryukov
efeb546865
[clang-format] Add common attribute macros to Google style (#76239)
We have found that 199fc973ced20016b04ba540cf63a1d4914fa513 regresses
formatting of our codebases because we do not properly configure the
names of attribute macros.

`GUARDED_BY` and `ABSL_GUARDED_BY` are very commoon in Google codebases
so it is reasonable to include them by default to avoid the need for
extra configuration in every Google repository.
2023-12-22 15:07:43 +01:00
Owen Pan
401f0396c3
[clang-format] Fix a bug in IndentExternBlock: NoIndent (#75731)
Fixes #36620.
Fixes #75719.
2023-12-17 15:07:11 -08:00
Owen Pan
e1a4b0032f
[clang-format] Handle merging functions containing only a block comment (#74651)
Fixed #41854.
2023-12-06 16:56:22 -08:00
Owen Pan
bbae59ae71
[clang-format] Finalize children after formatting them (#73753)
This would also fix the overlapping replacements below:
```
$ clang-format
 a(
 #else
 #endif
) = []() {      
)}
The new replacement overlaps with an existing replacement.
New replacement: <stdin>: 38:+7:"
"
Existing replacement: <stdin>: 38:+7:" "
```
Fixed #73487.
2023-11-29 12:56:05 -08:00
Owen Pan
39faf13dde
[clang-format] Add BreakAdjacentStringLiterals option (#73432)
Closes #70451.
2023-11-27 13:01:16 -08:00
Owen Pan
a369a5946f
[clang-format] Fix a bug in formating #define A x: (#73220)
Fixed #70789.
2023-11-26 16:20:19 -08:00
Owen Pan
edad025d1e
[clang-format] Correctly annotate braces of empty functions (#72733)
Also fixed some existing test cases.

Fixed #57305.
Fixed #58251.
2023-11-19 15:10:27 -08:00
Owen Pan
e16a8344d0
[clang-format][NFC] Skip alignArrayInitializers() for 1-row matrices (#72166) 2023-11-19 14:58:44 -08:00
Owen Pan
5679f5515b
[clang-format] Fix crashes in AlignArrayOfStructures (#72520)
Fixed #54815.
Fixed #55269.
Fixed #55493.
Fixed #68431.
2023-11-17 14:35:30 -08:00
Gedare Bloom
a852869398 [clang-format] Fix a bug in parsing function/variable names
Function and variable names are not detected when there is a
__attribute__((x)) preceding the name.

Fixes #64137.

Differential Revision: https://reviews.llvm.org/D156370
2023-11-13 19:35:28 -08:00
Owen Pan
fff993b7cf
[clang-format] Fix a bug in aligning comments in vector of structs (#72099)
Fixed #71825.
2023-11-13 13:41:01 -08:00
Owen Pan
40671bbdef
[clang-format] Handle control statements in BreakAfterAttributes (#71995)
This patch doesn't work for do-while loops.

Fixed #64474.
2023-11-12 01:08:27 -08:00
Owen Pan
199fc973ce
[clang-format] Handle variable declarations in BreakAfterAttributes (#71935)
Also fixed a bug in `isStartOfName()` and cleaned up some old test
cases.

Fixed #71563.

This is a rework of #71755.
2023-11-10 13:25:57 -08:00
Owen Pan
86ef9d3651 Revert "[clang-format] Handle variable declarations in BreakAfterAttributes (#71755)"
This reverts commit f7bbb58690910a8320b0927525dfba19df69a910 which caused
another formatting failure in polly.
2023-11-10 02:03:35 -08:00
Owen Pan
f7bbb58690 Reland "[clang-format] Handle variable declarations in BreakAfterAttributes (#71755)"
Also fixed another bug in isStartOfName().
2023-11-10 01:53:43 -08:00
Owen Pan
a0710e162d Revert "[clang-format] Handle variable declarations in BreakAfterAttributes (#71755)"
This reverts commit 5c36f4332d93c6f9bd9b01a34149a635e07a00d3 which caused a
formatting error in polly.
2023-11-09 23:34:06 -08:00
Owen Pan
5c36f4332d
[clang-format] Handle variable declarations in BreakAfterAttributes (#71755)
Also cleaned up some old test cases.

Fixes #71563.
2023-11-09 23:01:48 -08:00
Owen Pan
2c2fb8e10a
[clang-format] Treat empty for/while loops as short loops (#70768)
A for/while loop with only a semicolon as its body should be treated as
an empty loop.

Fixes #61708.
2023-11-02 14:09:00 -07:00
Vlad Serebrennikov
497b2ebb9e
[clang-format] Change LLVM style to BreakAfterAttributes == ABS_Leave (#70360)
This patch addresses some examples of bad formatting in Clang. The following commit contains only changes suggested by clang-format: 21689b56d1.
I believe it's a net negative on readability, with a couple of particularly bad cases. Highlights:
```diff
-    [[clang::preferred_type(bool)]]
-    mutable unsigned CachedLocalOrUnnamed : 1;
+    [[clang::preferred_type(bool)]] mutable unsigned CachedLocalOrUnnamed : 1;
```
```diff
-    [[clang::preferred_type(TypeDependence)]]
-    unsigned Dependence : llvm::BitWidth<TypeDependence>;
+    [[clang::preferred_type(TypeDependence)]] unsigned Dependence
+        : llvm::BitWidth<TypeDependence>;
```
```diff
-    [[clang::preferred_type(ExceptionSpecificationType)]]
-    unsigned ExceptionSpecType : 4;
+    [[clang::preferred_type(
+        ExceptionSpecificationType)]] unsigned ExceptionSpecType : 4;
```
Style guides doesn't appear to have an opinion on this matter.
2023-10-28 16:02:03 +04: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
Owen Pan
d68826dfbd
[clang-format] Don't break between string literal operands of << (#69871)
Fixes #44363.
2023-10-24 02:42:39 -07:00
Owen Pan
7bc1031c47 Revert "[clang-format] Fix align consecutive declarations over function pointers"
This reverts commit a84e0b4bdc9999872adbdaafbade8164b197784b.

Fixes #68079.
2023-10-24 02:15:44 -07:00
Owen Pan
853fb0a8ac
[clang-format] Don't alter SpacesRequiredBefore of finalized tokens (#69897)
Fixes #69237.
2023-10-23 13:12:51 -07:00
Omar Ahmed
7c15dd60ec [clang-format] Add space in placement new expression
Add AfterPlacementNew option to SpaceBeforeParensOptions to have more
control on placement new expressions.

Fixes #41501
Relates to #54703

Differential Revision: https://reviews.llvm.org/D127270
2023-10-20 03:16:28 -07:00