1893 Commits

Author SHA1 Message Date
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
Owen Pan
d2627cf88d [clang-format] Add the KeepEmptyLinesAtEOF option
Adds an option KeepEmptyLinesAtEOF to keep empty lines (up to
MaxEmptyLinesToKeep) before EOF. This remedies the probably unintentional
change in behavior introduced in 3d3ea84a4f8f, which started to always
remove empty lines before EOF.

Fixes #56054.
Fixes #63150.

Differential Revision: https://reviews.llvm.org/D152305
2023-06-07 14:17:58 -07:00
Owen Pan
4b9764959d [clang-format] Fix overlapping replacements before PPDirectives
If the first token of an annotated line is finalized, reuse its
NewlinesBefore value to avoid potential overlapping whitespace
replacements before preprocessor branching directives.

Fixes #62892.

Differential Revision: https://reviews.llvm.org/D151954
2023-06-03 12:33:03 -07:00
Galen Elias
d39929b925 [clang-format] Adjust braced list detection (reland 6dcde65)
This is a retry of https://reviews.llvm.org/D114583, which was backed
out for regressions.

Clang Format is detecting a nested scope followed by another open brace
as a braced initializer list due to incorrectly thinking it's matching a
braced initializer at the end of a constructor initializer list which is
followed by the body open brace.

Unfortunately, UnwrappedLineParser isn't doing a very detailed parse, so
it's not super straightforward to distinguish these cases given the
current structure of calculateBraceTypes. My current hypothesis is that
these can be disambiguated by looking at the token preceding the
l_brace, as initializer list parameters will be preceded by an
identifier, but a scope block generally will not (barring the MACRO
wildcard).

To this end, I am adding tracking of the previous token to the LBraceStack
to help scope this particular case.

TokenAnnotatorTests cherry picked from https://reviews.llvm.org/D150452.

Fixes #33891.
Fixes #52911.

Differential Revision: https://reviews.llvm.org/D150403
2023-05-23 18:50:41 -07:00
Owen Pan
dc4ab97085 [clang-format] Revert 6dcde65 due to missing commit message title
This reverts commit 6dcde658b2380d7ca1451ea5d1099af3e294ea16.
2023-05-23 18:48:52 -07:00
Galen Elias
6dcde658b2 This is a retry of https://reviews.llvm.org/D114583, which was backed
out for regressions.

Clang Format is detecting a nested scope followed by another open brace
as a braced initializer list due to incorrectly thinking it's matching a
braced initializer at the end of a constructor initializer list which is
followed by the body open brace.

Unfortunately, UnwrappedLineParser isn't doing a very detailed parse, so
it's not super straightforward to distinguish these cases given the
current structure of calculateBraceTypes. My current hypothesis is that
these can be disambiguated by looking at the token preceding the
l_brace, as initializer list parameters will be preceded by an
identifier, but a scope block generally will not (barring the MACRO
wildcard).

To this end, I am adding tracking of the previous token to the LBraceStack
to help scope this particular case.

TokenAnnotatorTests cherry picked from https://reviews.llvm.org/D150452.

Fixes #33891.
Fixes #52911.

Differential Revision: https://reviews.llvm.org/D150403
2023-05-22 20:25:55 -07:00
Emilia Kond
06763ea5d8
[clang-format] Ignore first token when finding MustBreak
When in ColumnLimit 0, the formatter looks for MustBreakBefore in the
line in order to check if a line is allowed to be merged onto one line.

However, since MustBreakBefore is really a property of the gap between
the token and the one previously, I belive the check is erroneous in
checking all the tokens in a line, since whether the previous line ended
with a forced line break should have no effect on whether the current
line is allowed to merge with the next one.

This patch changes the check to skip the first token in
`LineJoiner.containsMustBreak`.

This patch also changes a test, which is not ideal, but I believe the
test also suffered from this bug. The test case in question sets
AllowShortFunctionsOnASingleLine to "Empty", but the empty function in
said test case isn't merged to a single line, because of the very same
bug this patch fixes.

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

Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D150614
2023-05-18 05:50:26 +03:00
Emilia Kond
e4d3e88802
[clang-format] Don't allow template to be preceded by closing brace
This check is similar to the right paren check right below it, but it
doesn't need the overloaded operator check.

This patch prevents brace-initialized objects that are being compared
from being mis-annotated as template parameters.

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

Reviewed By: owenpan, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D150629
2023-05-17 01:37:19 +03:00
Owen Pan
a4c87f8cca [clang-format] Fix consecutive alignments in #else blocks
Since 3.8 or earlier, clang-format has been lumping all #else, #elif,
etc blocks together when doing whitespace replacements and causing
consecutive alignments across #else blocks.

Commit c077975 partially addressed the problem but also triggered
"regressions".

This patch fixes the root cause of the problem and "reverts" c077975
(except for the unit tests).

Fixes #36070.
Fixes #55265.
Fixes #60721.
Fixes #61498.

Differential Revision: https://reviews.llvm.org/D150057
2023-05-08 19:45:30 -07:00
sstwcw
82a90caa88 [clang-format] Correctly format goto labels followed by blocks
There doesn't seem to be an issue on GitHub.  But previously, a space
would be inserted before the goto colon in the code below.

    switch (x) {
    case 0:
    goto_0: {
      action();
      break;
    }
    }

Previously, the colon following a goto label would be annotated as
`TT_InheritanceColon`.  A goto label followed by an opening brace
wasn't recognized.  It is easy to add another line to have
`spaceRequiredBefore` function recognize the case, but I believed it
is more proper to avoid doing the same thing in `UnwrappedLineParser`
and `TokenAnnotator`.  So now the label colons would be labeled in
`UnwrappedLineParser`, and `spaceRequiredBefore` would rely on that.

Previously we had the type `TT_GotoLabelColon` intended for both goto
labels and case labels.  But since handling of goto labels and case
labels differ somewhat, I split it into separate types for goto and
case labels.

This patch doesn't change the behavior for case labels.  I added the
lines annotating case labels because they would previously be
mistakenly annotated as `TT_InheritanceColon` just like goto labels.
And since I added the annotations, the checks for the `case` and
`default` keywords in `spaceRequiredBefore` are not necessary anymore.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D148484
2023-04-30 22:25:48 +00:00
Jon Phillips
c12aa69a0b [clang-format] Add BracedInitializerIndentWidth option
The option allows users to specify how many columns to use to indent
the contents of initializer lists.

Closes #51070.

Differential Revision: https://reviews.llvm.org/D146101
2023-04-29 00:36:19 -07:00
Jorge Pinto Sousa
9db2a04548 [clang-format] Dont interpret variable named interface as keyword for C++
Fixes #53173.

Differential Revision: https://reviews.llvm.org/D148437
2023-04-16 03:23:53 -07:00
Jon Phillips
5c614bd88f [clang-format] Fix bugs with "LambdaBodyIndentation: OuterScope"
The previous implementation of the option corrupted the parenthesis
state stack. (See https://reviews.llvm.org/D102706.)

Fixes #55708.
Fixes #53212.
Fixes #52846.
Fixes #59954.

Differential Revision: https://reviews.llvm.org/D146042
2023-04-05 14:38:38 -07:00
Jon Phillips
1dd31ebe54 [clang-format][NFC] Refactor unit tests for "LambdaBodyIndentation: OuterScope"
Differential Revision: https://reviews.llvm.org/D146995
2023-04-01 19:35:49 -07:00
Owen Pan
767aee1de9 [clang-format] Don't annotate left brace of struct as FunctionLBrace
Related to a02c3af9f19d. Fixes #61700.

Differential Revision: https://reviews.llvm.org/D146895
2023-03-27 14:07:15 -07:00
Emilia Dreamer
a8d2bff290
[clang-format] Don't wrap struct return types as structs
When using BraceWrapping.AfterClass or BraceWrapping.AfterStruct, the
token annotator relies on the first token of the line to determine if
we're dealing with a struct or class, however, this check is faulty if
it's actually a function with an elaborated struct/class return type, as
is common in C.

This patch skips the check if the brace is already annotated as
FunctionLBrace, in which case we already know it's a function and should
be treated as such.

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

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D146281
2023-03-26 04:38:52 +03:00
Emilia Dreamer
c70e360b35
[clang-format] Allow trailing return types in macros
The trailing return type arrow checker verifies that a declaration is
being parsed, however, this isn't true when inside of macros.

It turns out the existence of the auto keyword is enough to make
sure that we're dealing with a trailing return type, and whether we're
in a declaration doesn't matter.

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

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D141811
2023-03-23 19:37:53 +02:00
Emilia Dreamer
ead9644684
[clang-format] Annotate noexcept, explicit specifiers as containing expressions
The noexcept specifier and explicit specifier can optionally include a
boolean expression to make these specifiers apply conditionally,
however, clang-format didn't set the context for the parenthesized
content of these specifiers, meaning they inherited the parent context,
which usually isn't an expressions, leading to misannotated binary
operators.

This patch applies expression context to the content of these
specifiers, making them similar to the static_assert keyword.

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

Reviewed By: owenpan, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D146284
2023-03-22 20:27:17 +02:00