2060 Commits

Author SHA1 Message Date
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
Björn Schäpers
6e77ba6c1e [clang-format] Fix weird handling of AfterColon
Fixes #64895.

Differential Revision: https://reviews.llvm.org/D158505
2023-08-24 08:34:22 +02:00
Jon Phillips
d06b923915 [clang-format] Fix a bug that wraps before function arguments
Fixes a long-standing bug that erroneously placed function arguments on a
new line despite all arguments being able to fit on the same line.

The original diff that introduced the bug implemented behaviour that pushed
the first argument to a function onto a new line under certain circumstances
relating passing lambdas as arguments.

This behaviour was implemented in TokenAnnotator::mustBreakBefore() which
meant the code lacked the necessary context to figure out whether subsequent
arguments might be able to all fit on one line. As such, I've moved the
implementation to ContinuationIndenter and, instead of forcing a line break
at the first argument in all cases, we now allow the OptimizingLineFormatter
to consider placing the first argument on the same line as the function call
but don't allow further line breaks in this case.

The end result is that either the first argument must go on a new line (as
before) or all arguments must be put on the current line.

Closes #44486.

Differential Revision: https://reviews.llvm.org/D156259
2023-08-23 11:18:23 -07:00
Owen Pan
e3a79503a3 [clang-format] Exclude kw_decltype in RemoveParentheses
From https://en.cppreference.com/w/cpp/language/decltype:
Note that if the name of an object is parenthesized, it is treated as an
ordinary lvalue expression, thus decltype(x) and decltype((x)) are often
different types.

Fixes #64786.

Differential Revision: https://reviews.llvm.org/D158155
2023-08-17 16:04:44 -07:00
Owen Pan
a0b1c235af [clang-format] Currectly handle PCIS_CurrentLine with no column limit
Fixes #63519.

Differential Revision: https://reviews.llvm.org/D157179
2023-08-06 12:14:23 -07:00
Owen Pan
70d7ea0ceb [clang-format] Handle goto labels preceded by C++11 attributes
Fixes #64229.

Differential Revision: https://reviews.llvm.org/D156655
2023-08-02 22:48:48 -07:00
Björn Schäpers
77a38f43b1 [clang-format] Supress aligning of trailing namespace comments
Fixes https://github.com/llvm/llvm-project/issues/57504.

Differential Revision: https://reviews.llvm.org/D138263
2023-08-02 11:50:14 +02:00
Galen Elias
400da115c5 [clang-format] Fix braced initializer with templated base class
Fixes #64134.

Differential Revision: https://reviews.llvm.org/D156705
2023-08-01 13:53:22 -07:00
Gedare Bloom
c669541c96 [clang-format] Add SpacesInParens with SpacesInParensOptions
This is a refactoring of:
- SpacesInConditionalStatement
- SpacesInCStyleCastParentheses
- SpaceInEmptyParentheses
- SpacesInParentheses

These are now options under the new Style Option: SpacesInParens. The
existing options are maintained for backward compatibility.

Within SpacesInParens, there are currently options for:
- Never
- Custom

The currently available options for Custom are:
- InConditionalStatements
- InCStyleCasts
- InEmptyParentheses
- Other

Setting InConditionalStatements and Other to true enables the same space
additions as SpacesInParentheses.

This refactoring does not add or remove any existing features, but it makes
it possible to more easily extend and maintain the addition of spaces within
parentheses.

Related to #55428.

Differential Revision: https://reviews.llvm.org/D155239
2023-07-24 19:27:23 -07:00
Galen Elias
74720d1b33 [clang-format] Add AlignConsecutiveShortCaseStatements
This adds a new AlignConsecutiveShortCaseStatements option in line with the
existing AlignConsecutive* options , which when
AllowShortCaseLabelsOnASingleLine is enabled will align the tokens after the
case statement's colon. This also adds a AlignCaseColons option to allow
aligning the case label colon itself rather than the token after it.

Fixes #55475.

Differential Revision: https://reviews.llvm.org/D151761
2023-07-24 18:39:25 -07:00
Owen Pan
87ad34ffdc Revert "[clang-format] Add AlignConsecutiveShortCaseStatements"
This reverts commit 4ba00844174db562d2bd8b15dab6aac605eddf57.
2023-07-24 18:37:42 -07:00
Owen Pan
ac6e55146f Revert "Revert "[clang-format] Add AlignConsecutiveShortCaseStatements""
This reverts commit 4f093b31669a4f8e417259583141159586a05b28.
2023-07-24 18:24:30 -07:00
Owen Pan
4f093b3166 Revert "[clang-format] Add AlignConsecutiveShortCaseStatements"
This reverts commit 4ba00844174db562d2bd8b15dab6aac605eddf57 due to missing
authorship.
2023-07-24 18:19:38 -07:00
Owen Pan
4ba0084417 [clang-format] Add AlignConsecutiveShortCaseStatements
This adds a new AlignConsecutiveShortCaseStatements option in line with the
existing AlignConsecutive* options , which when
AllowShortCaseLabelsOnASingleLine is enabled will align the tokens after the
case statement's colon. This also adds a AlignCaseColons option to allow
aligning the case label colon itself rather than the token after it.

Fixes #55475.

Differential Revision: https://reviews.llvm.org/D151761
2023-07-24 18:16:21 -07:00
Jared Grubb
63d6659a04 [clang-format] Fix support for ObjC blocks with pointer return types
The ObjC-block detection code only supports a single token as the return type. Add support to detect pointers, too (ObjC has lots of object-pointers).

For example, using `BasedOnStyle: WebKit`, the following is stable output:

```
int* p = ^int*(void)
{ //
    return nullptr;
}
();
```

After the patch, this is stable:

```
int* p = ^int*(void) { //
    return nullptr;
}();
```

Differential Review: https://reviews.llvm.org/D146434
2023-07-17 14:47:49 +01:00
Owen Pan
3a6a0702c2 [clang-format] Add an option to remove redundant parentheses
Differential Revision: https://reviews.llvm.org/D154484
2023-07-11 16:33:19 -07:00
Gedare Bloom
413a7cb6e0 [clang-format] Support block indenting array/struct list initializers
C89 and C99 list initializers are treated differently than Cpp11 braced
initializers. This patch identifies the C array/struct initializer lists by
finding the preceding equal sign before a left brace, and applies formatting
rules for BracketAlignmentStyle.BlockIndent to those list initializers.

Fixes #57878.

Differential Revision: https://reviews.llvm.org/D153205
2023-07-06 13:42:11 -07:00
Gedare Bloom
a84e0b4bdc [clang-format] Fix align consecutive declarations over function pointers
Fixes a bug that prevents alignment from proceeding through a function
pointer in a list of declarations.

Fixes #63451.

Differential Revision: https://reviews.llvm.org/D153585
2023-07-06 02:17:32 -07:00
Gedare Bloom
6b8a9ff7e7 [clang-format] Fix RAS reference alignment when PAS is left or middle
Fixes a bug with the handling of right aligned references with left/middle
alignment pointers.

Fixes #63452.

Differential Revision: https://reviews.llvm.org/D153579
2023-07-06 01:41:16 -07:00
Owen Pan
3f3620e5c9 [clang-format] Correctly annotate */&/&& in operator function calls
Reverts 4986f3f2f220 (but keeps its unit tests) and fixes #49973
differently.

Also fixes bugs that incorrectly annotate the operator keyword as
TT_FunctionDeclarationName in function calls and as TT_Unknown in function
declarations and definitions.

Differential Revision: https://reviews.llvm.org/D154184
2023-07-03 17:49:10 -07:00
Emilia Kond
4986f3f2f2
[clang-format] Correctly annotate operator free function call
The annotator correctly annotates an overloaded operator call when
called as a member function, like `x.operator+(y)`, however, when called
as a free function, like `operator+(x, y)`, the annotator assumed it was
an overloaded operator function *declaration*, instead of a call.

This patch allows for a free function call to correctly be annotated as
a call, but only if the current like cannot be a declaration, usually
within the bodies of a function.

Fixes https://github.com/llvm/llvm-project/issues/49973

Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay, Nuullll

Differential Revision: https://reviews.llvm.org/D153798
2023-06-29 19:51:27 +03:00
Emilia Kond
6e39b0ddf0
[clang-format][NFC] Use correct test method for new tests
7a38b3bfeb56 landed after 20b4df1ed611, which refactored how tests are
structured in FormatTest. This quick fix-up unifies the tests added in
7a38b3bfeb56 to comply with this new format.
2023-06-26 12:27:57 +03:00
Emilia Kond
7a38b3bfeb
[clang-format] Respect ColumnLimit 0 line breaks in inline asm
Previously, using ColumnLimit: 0 with extended inline asm with the
BreakBeforeInlineASMColon: OnlyMultiline option (the default style),
the formatter would act as if in Always mode, meaning a line break was
added before every colon in an extended inline assembly block.

This patch respects the already existing line breaks, and doesn't add
any new ones, if in ColumnLimit 0 mode.

Behaviour with Always stays as expected, with a break before every colon
regardless of any existing line breaks.

Behaviour with Never was broken before, and remains broken with this patch,
it is just never respected in ColumnLimit 0 mode.

Fixes https://github.com/llvm/llvm-project/issues/62754

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D150848
2023-06-23 17:30:24 +03:00
Owen Pan
8e85739a5f [clang-format] Don't finalize #if, #else, #endif, etc.
Don't finalize a preprocessor branch directive if it's the first
token of an annotated line. See the rationale at
https://reviews.llvm.org/D150057#inline-1449546.

Fixes #63379

Differential Revision: https://reviews.llvm.org/D153243
2023-06-21 15:15:57 -07:00
Owen Pan
8411bdb0ac [clang-format][NFC] Remove redundant getLLVMStyle() in unit tests 2023-06-17 15:25:59 -07:00
Owen Pan
441108ccba Reland [clang-format] Fix overlapping whitespace replacements before PPDirective
If the first token of an annotated line already has a computed Newlines,
reuse it to avoid potential overlapping whitespace replacements before
preprocessor branching directives.

Fixes #62892.

Differential Revision: https://reviews.llvm.org/D151954
2023-06-16 17:00:12 -07:00
Owen Pan
926e1faabc [clang-format][NFC] Use verifyGoogleFormat in FormatTest.cpp
Replaces verifyFormat(..., getGoogleStyle()) with
verifyGoogleFormat(...) in FormatTest.cpp.
2023-06-16 16:44:06 -07:00
Owen Pan
20b4df1ed6 [clang-format][NFC] Clean up unit tests
This patch adds a verifyNoChange macro to verify code that won't
change after being formatted. (The code will not be messed up before
being formatted.) It then replaces EXPECT_EQ with verifyFormat
wherever applicable so that the code will be messed up before being
formatted. When the replacement fails the unit test, verifyFormat is
replaced with verifyNoChange.

Differential Revision: https://reviews.llvm.org/D153109
2023-06-16 16:03:19 -07:00
Owen Pan
829ed96b77 Reland [clang-format] Add the KeepEmptyLinesAtEOF option 2023-06-13 10:02:10 -07:00
Paul Kirth
b6a718016c [clang-format] Add test case for issue 63170
After https://reviews.llvm.org/D151954 we've noticed some issues w/
clang-format behavior, as outlined in
https://github.com/llvm/llvm-project/issues/63170.

Valid C/C++ files, that were previously accepted, are now rejected by
clang-format, emitting the message:

"The new replacement overlaps with an existing replacement."

This reverts commit 4b9764959dc4b8783e18747c1742ab164e4bc4ee and
d2627cf88d2553a4c2e850430bdb908a4b7d2e52, which depends on it.

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D152473
2023-06-09 20:10:19 +00:00
Leonard Chan
9f00eb9975 Revert "Revert "[clang-format] Fix overlapping replacements before PPDirectives""
This reverts commit 94e75469597f197f9c4b45baa6c8a576c78dbd02.

Apparently I broke some builders with the original revert: http://45.33.8.238/linux/109159/step_7.txt
2023-06-08 00:15:30 +00:00
Leonard Chan
94e7546959 Revert "[clang-format] Fix overlapping replacements before PPDirectives"
This reverts commit 4b9764959dc4b8783e18747c1742ab164e4bc4ee.

Reverting since this causes clang-formtat to incorrectly fall into an
error path yet return a zero exit code despite not formatting the file
at all.

See https://github.com/llvm/llvm-project/issues/63170
2023-06-07 23:44:32 +00:00