2041 Commits

Author SHA1 Message Date
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
Jared Grubb
7f881a2abe [clang-format] Treat AttributeMacro more like __attribute__
There are two parts to this fix:
- Annotate the paren after an AttributeMacro as an AttributeLParen.
- Treat an AttributeMacro-without-paren the same as one with a paren.

I added a new test-case to differentiate a macro that is or is-not an
AttributeMacro; also handled whether ColumnLimit is set to infinite (0) or a
finite value, as part of this patch is in ContinuationIndenter.

Closes #68722.

Differential Revision: https://reviews.llvm.org/D145262
2023-10-15 15:58:24 -07:00
Owen Pan
7e856d1894 Reland "[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (#67955)"
Reland 6a621ed8e4cb which failed on Windows but not macOS.

The failures were caused by moving the annotation of the children from the
top to the bottom of TokenAnnotator::annotate(), resulting in invalid lines
including incomplete ones being skipped.
2023-10-03 22:26:48 -07:00
Owen Pan
d08fcc817e Revert "[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (#67955)"
This reverts commit 6a621ed8e4cb02bd55fe4a4a0254615576b70a55 as it caused
buildbots to fail.
2023-10-03 18:19:23 -07:00
Owen Pan
6a621ed8e4
[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (#67955)
After annotating constructors/destructors as FunctionDeclarationName in
commit 08630512088, we have seen several issues because ctors/dtors had
been treated differently than functions in aligning, wrapping, and
indenting.

This patch annotates ctors/dtors as CtorDtorDeclName instead and would
effectively revert commit 0468fa07f87f, which is obsolete now.

Fixed #67903.
Fixed #67907.
2023-10-03 18:02:09 -07:00
Björn Schäpers
1f01269028
[clang-format] Fix alignment in presence of template functions (#68029)
Fixes #68102.
2023-10-03 15:35:30 +02:00
Owen Pan
75441a6842
[clang-format] Fix a bug in RemoveParentheses: ReturnStatement (#67911)
Don't remove the outermost parentheses surrounding a return statement
expression when inside a function/lambda that has the decltype(auto)
return type.

Fixed #67892.
2023-10-02 09:15:49 +02:00
Owen Pan
0468fa07f8
[clang-format] Don't align ctors and dtors with other functions (#67618)
Fixed #67604.
2023-09-29 02:09:36 -07:00
Jon Phillips
82001e0d01
[clang-format] Disable OuterScope lambda indentation behaviour for constructor initializers (#66755)
By default, OuterScope aligns lambdas to the beginning of the current
line. This makes sense for most types of statements within code blocks
but leads to unappealing and misleading indentation for lambdas within
constructor initializers.
2023-09-28 21:04:42 +02:00
Owen Pan
5ed94c6e3d
[clang-format] Fix a bug in ContinuationIndenter (#66354)
See https://reviews.llvm.org/D136154#3890747 for context.

Fixes part of #58592.
2023-09-15 14:20:41 -07:00
Owen Pan
b908123c3c [clang-format][NFC] Minor cleanup of token annotator and test cases 2023-09-11 00:18:57 -07:00
Björn Schäpers
84e3fdfc65
[clang-format] BreakBeforeNoexceptSpecifier option added (#65808)
It really bugs me that it breaks to
``` c++
...) noexcept(
    noexcept(condition)...
```

This is a fix for people like me.
2023-09-09 22:31:58 +02:00
Jon Phillips
210e7b3ca7 [clang-format] Improve line-breaking in LambdaBodyIndentation: OuterScope
Avoid unnecessarily aggressive line-breaking when using
"LambdaBodyIndentation: OuterScope" with argument bin-packing.

Differential Revision: https://reviews.llvm.org/D148131
2023-09-08 14:34:00 -07:00
Arkadiy Yudintsev
f465a482ca [clang-format] Fix segmentation fault when formatting nested namespaces
Fixing the clang-format crash with the segmentation fault error when
formatting code with nested namespaces.

Fixes #64701.

Differential Revision: https://reviews.llvm.org/D158363
2023-09-05 11:12:14 -07:00
Galen Elias
58c67e724f [clang-format] Fix AlignArrayOfStructures + Cpp11BracedListStyle=false
Currently AlignArrayOfStructures=Left is hard coding setting Spaces to
0 for the token following the initial opening brace, but not touching
Spaces for the subsequent lines, which leads to the array being
misaligned. Additionally, it's not adding a space before the trailing
} which is generally done when Cpp11BracedListStyle=false.

I'm not exactly sure why this function needs to override the Spaces as
it seems to generally already be set to either 0 or 1 according to
the other formatting settings, but I'm going with an explicit fix where
I just force the padding to 1 when Cpp11BracedListStyle=false.

AlignArrayOfStructures=Right doesn't have any alignment problems, but
isn't adding the expected padding around the braces either, so I'm
giving that the same treatment.

Fixes #57611.

Differential Revision: https://reviews.llvm.org/D158795
2023-08-31 14:21:19 -07:00
Owen Pan
14feca5d14 [clang-format][NFC] Replace EXPECT_EQ() with verifyFormat()
Replaces the remaining ones not covered in 8b131d2e1480.
2023-08-30 14:55:26 -07:00
Owen Pan
8b131d2e14 [clang-format][NFC] Change EXPECT_EQ to verifyFormat or verifyNoChange
Replaces some 600 EXPECT_EQ() to verifyFormat() or verifyNoChange() in
FormatTest.cpp because the former neither checks stability of formatting
nor tests formatting C++ as Objective C.

Also marks dozens of unstable test cases with FIXME comments.

Differential Revision: https://reviews.llvm.org/D159051
2023-08-30 13:07:12 -07:00
Owen Pan
0863051208 Reland "[clang-format] Annotate constructor/destructor names"
(0e63f1aacc00 was reverted by 7590b7657004 due to a crash.)

Annotate constructor/destructor names as FunctionDeclarationName.

Fixes #63046.

Differential Revision: https://reviews.llvm.org/D157963
2023-08-29 13:14:52 -07:00
Björn Schäpers
ca0aa53365 [clang-format] Handle Template Arguments with AlignConsecutiveXX
This fixes https://github.com/llvm/llvm-project/issues/64928.

Differential-Revision: https://reviews.llvm.org/D158945
2023-08-29 21:59:44 +02:00
Kadir Cetinkaya
7590b76570
Revert "[clang-format] Annotate constructor/destructor names"
This reverts commit 0e63f1aacc0040e9a16fa2fab15a140b1686e2ab.

clang-format started to crash with contents like:
a.h:
```
```
$ clang-format a.h
```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ../llvm/build/bin/clang-format a.h
 #0 0x0000560b689fe177 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Unix/Signals.inc:723:13
 #1 0x0000560b689fbfbe llvm::sys::RunSignalHandlers() /usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Signals.cpp:106:18
 #2 0x0000560b689feaca SignalHandler(int) /usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Unix/Signals.inc:413:1
 #3 0x00007f030405a540 (/lib/x86_64-linux-gnu/libc.so.6+0x3c540)
 #4 0x0000560b68a9a980 is /usr/local/google/home/kadircet/repos/llvm/clang/include/clang/Lex/Token.h:98:44
 #5 0x0000560b68a9a980 is /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/FormatToken.h:562:51
 #6 0x0000560b68a9a980 startsSequenceInternal<clang::tok::TokenKind, clang::tok::TokenKind> /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/FormatToken.h:831:9
 #7 0x0000560b68a9a980 startsSequence<clang::tok::TokenKind, clang::tok::TokenKind> /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/FormatToken.h:600:12
 #8 0x0000560b68a9a980 getFunctionName /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/TokenAnnotator.cpp:3131:17
 #9 0x0000560b68a9a980 clang::format::TokenAnnotator::annotate(clang::format::AnnotatedLine&) /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/TokenAnnotator.cpp:3191:17
Segmentation fault
```
2023-08-28 10:31:07 +02:00
Owen Pan
19e3dfad58 [clang-format][NFC] Chang some verifyFormat() to verifyNoChange() 2023-08-26 19:44:05 -07:00
Owen Pan
f8838f85e1 [clang-format][NFC] Remove extraneous newlines at end of test cases
Removes some 500 newlines (\n and \r\n) at the end of test cases in
FormatTest.cpp.

Also changes a few verifyFormat() to verifyNoChange() in tests
InsertNewlineAtEOF and WhitespaceSensitiveMacros as messUp() removes
trailing newlines as well.

Differential Revision: https://reviews.llvm.org/D158805
2023-08-25 15:32:07 -07:00
Owen Pan
0e63f1aacc [clang-format] Annotate constructor/destructor names
Annotate constructor/destructor names as FunctionDeclarationName.

Fixes #63046.

Differential Revision: https://reviews.llvm.org/D157963
2023-08-24 01:32:52 -07:00