3749 Commits

Author SHA1 Message Date
Mehdi Amini
b0d1e32ca2
Revert "[clang-format][NFC] Eliminate the IsCpp parameter in all functions" (#85353)
Reverts llvm/llvm-project#84599

This broke the presubmit bot.
2024-03-14 19:33:11 -07:00
Owen Pan
0c07102927
[clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599) 2024-03-14 18:56:24 -07:00
Jie Fu
83fe0b1382 [clang] Fix -Wunused-lambda-capture in TokenAnnotator.cpp (NFC)
llvm-project/clang/lib/Format/TokenAnnotator.cpp:2707:43:
error: lambda capture 'this' is not used [-Werror,-Wunused-lambda-capture]
    auto IsQualifiedPointerOrReference = [this](FormatToken *T) {
                                          ^~~~
1 error generated.
2024-03-09 20:26:10 +08:00
Owen Pan
0baef3b18c
[clang-format] Handle common C++ non-keyword types as such (#83709)
Fixes #83400.
2024-03-08 19:42:35 -08:00
Owen Pan
051e910b8b
[clang-format][NFC] Replace Style.isCpp() with IsCpp (#83533) 2024-03-02 12:57:24 -08:00
Hirofumi Nakamura
19cec9ca12
[clang-format] Add AlignConsecutiveTableGenDefinitions option. (#83008)
To align TableGen consecutive definitions.
2024-02-27 22:31:23 +09:00
Owen Pan
b2a4f64e19
[clang-format][NFC] Skip ObjCHeaderStyleGuesser for empty code (#82957) 2024-02-26 20:46:46 -08:00
Hirofumi Nakamura
046682ef88
[clang-format] Add AlignConsecutiveTableGenCondOperatorColons option. (#82878)
To align colons inside TableGen !cond operators.
2024-02-26 22:50:51 +09:00
Owen Pan
b0d2a52c87
[clang-format][NFC] Enable RemoveSemicolon for clang-format style (#82735)
Also insert separators for decimal integers longer than 4 digits.
2024-02-23 20:03:13 -08:00
r4nt
ddb4450a46
[ClangFormat] Fix indent in child lines within a macro argument. (#82523)
When reconstructing lines from a macro expansion, make sure that lines
at different levels in the expanded code get indented correctly as part
of the macro argument.
2024-02-23 13:18:00 +01:00
Emilia Kond
2e7cacfced
[clang-format] Fix crash in TokenAnnotator (#82349)
The while loop on line 3814 can cause a segmentation fault getting the
Next field on a nullptr. This is because further down, on line 3823,
there is another for loop, which assigns Tok to Tok->Next in its
initializer. This for loop has a condition to check if the result of
that isn't null. If it is, the loop is skipped and we drop back out to
the outer loop, except, now Tok is null, and we try to dereference it
without checking first.

This patch adds a defensive check that returns if Tok->Next is null
before we make it to the second for loop.

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

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-02-22 20:22:05 +02:00
Owen Pan
04fbc461e0
[clang-format] Fix RemoveSemicolon for empty functions (#82278)
Fixes #79833.
2024-02-20 21:51:51 -08:00
Owen Pan
119a72866f [clang-format][NFC] Remove redundant calls to guessIsObjC()
Running clang-format on the following input
```
int lambdas() {
  return [&] {
  return [&] {
  return [&] {
  return [&] {
  return [&] {
  return [&] {
  return [&] { return 3; } ();
  } (); } (); } (); } (); } (); } (); }
```
will finish instantly if you pass clang-format a .cpp input with this
content, but hang for tens of seconds if you pass the same via stdin
or a .h file.

Adding some debug statements showed that guessIsObjC was getting called
tens of millions of times in a manner that scales very rapidly with the
amount of nesting (if clang-format just takes a few seconds with that
input passed on stdin, try adding a couple more levels of nesting).

This change moves the recursive guessIsObjC call one level of nesting
out of an inner loop whose iterations don't affect the input to the
recursive call. This resolves the performance issue.

Authored-by: davidvc1 and Uran198

Differential Revision: https://reviews.llvm.org/D114837
Differential Revision: https://reviews.llvm.org/D47515
2024-02-20 00:28:26 -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
8de230093f
[clang-format] Correctly annotate braces of empty ctors/dtors (#82097)
Also reformat Polly.

Fixes #79834.
2024-02-19 12:41:22 -08:00
Owen Pan
ea16a3bb4c
[clang-format] Don't sort qualifiers across preprocessor directives (#81958)
Fixes #80579.
2024-02-16 23:19:31 -08:00
Hirofumi Nakamura
8c1c94e8c1
[clang-format] Support of TableGen basic format restrictions. (#81611)
- Allow/force to break the line or not.
- Allow/force to insert space or not.
2024-02-16 21:35:45 +09:00
Owen Pan
6087d7bc0a [clang-format][NFC] Sort options in Format.cpp 2024-02-14 21:40:07 -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
61c83e9491 Revert "[clang-format][NFC] Make LangOpts global in namespace Format"
This reverts commit 32e65b0b8a743678974c7ca7913c1d6c41bb0772.

It seems to break some PowerPC bots.

See https://github.com/llvm/llvm-project/pull/81390#issuecomment-1941964803.
2024-02-13 21:02:14 -08:00
rmarker
91dcf53abd
[clang-format] Rename option AlwaysBreakAfterReturnType. (#80827)
Changes the option to BreakAfterReturnType option, with a more relevant
name, deprecating and replacing AlwaysBreakAfterReturnType.
Following up on #78010.
2024-02-12 20:28:33 -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
Haojian Wu
c9afeaa643 [format] Remove an unused variable. 2024-02-12 16:19:42 +01:00
Hirofumi Nakamura
6a471611a4
[clang-format] Support of TableGen value annotations. (#80299)
This implements the annotation of the values in TableGen.
The main changes are,

- parseTableGenValue(), the simplified parser method for the syntax of
values.
- modified consumeToken() to parseTableGenValue in 'if', 'assert' and
after '='.
- modified parseParens() to call parseTableGenValue inside.
- modified parseSquare() to to call parseTableGenValue inside, with
skipping separator tokens.
- modified parseAngle() to call parseTableGenValue inside, with skipping
separator tokens.
2024-02-12 23:27:09 +09:00
Haojian Wu
50ed98f1cf Revert "Reland "[clang-format] Update FormatToken::isSimpleTypeSpecifier() (#80241)""
The change caused an asan crash when running the `QualifierFixerTest.IsQualifierType` unittest, see
details: https://github.com/llvm/llvm-project/pull/80241#issuecomment-1938749844

This reverts commit 7f40c5cc4e15a0a67b031f13370afc342a5dc14b.
2024-02-12 15:24:27 +01:00
Owen Pan
7f40c5cc4e Reland "[clang-format] Update FormatToken::isSimpleTypeSpecifier() (#80241)" 2024-02-12 00:10:24 -08:00
Owen Pan
33108fae90 Reverted due to wrong commit message
This reverts commit f5d71b79fe978f1e70f4a41adfd15f65dec6e210.
2024-02-12 00:07:47 -08:00
Owen Pan
f5d71b79fe Reland "[clang-format][NFC] Make LangOpts global in namespace Format (#81390)" 2024-02-12 00:04:34 -08:00
Owen Pan
32e65b0b8a Reland "[clang-format][NFC] Make LangOpts global in namespace Format (#81390)"
Restore getFormattingLangOpts().
2024-02-11 22:01:23 -08:00
Owen Pan
3dc8ef677d Revert "[clang-format][NFC] Make LangOpts global in namespace Format (#81390)"
This reverts commit 03f571995b4f0c260254955afd16ec44d0764794.

We can't hide getFormattingLangOpts() as it's used by other tools.
2024-02-11 13:08:28 -08:00
Owen Pan
03f571995b
[clang-format][NFC] Make LangOpts global in namespace Format (#81390) 2024-02-11 12:59:05 -08:00
Owen Pan
7664ddf881 [clang-format][NFC] Drop "Always" in "AlwaysBreakTemplateDeclarations" 2024-02-09 20:18:24 -08:00
Owen Pan
c5cbfc5689
[clang-format] Rename option AlwaysBreakTemplateDeclarations (#81093)
Drop the "Always" prefix to remove the self-contradiction.
2024-02-09 19:53:04 -08:00
Owen Pan
5609bd83c3 Revert "[clang-format] Update FormatToken::isSimpleTypeSpecifier() (#80241)"
This reverts commit 763139afc19ddf2e0f0265dc828ce8e5fbe92530.

It seems that LangOpts is not initialized before use.
2024-02-09 01:52:41 -08:00
Owen Pan
763139afc1
[clang-format] Update FormatToken::isSimpleTypeSpecifier() (#80241)
Now with a8279a8bc541, we can make the update.
2024-02-08 21:42:29 -08:00
Owen Pan
d033799050
[clang-format] Add Leave to AlwaysBreakTemplateDeclarations (#80569)
Closes #78067.
2024-02-07 20:24:44 -08:00
Fernando Tagawa
1b03cbc939
[clang-format] Handle doxygen commands starting with \ (#80381)
Fixes llvm/llvm-project#63241

Doxygen commands can start with `@` or `\`.
2024-02-06 21:23:12 -03:00
j-jorge
984dd15d4d
[clang-format] Add MainIncludeChar option. (#78752)
Resolves #27008, #39735, #53013, #63619.

Hello, this PR adds the MainIncludeChar option to clang-format, allowing
to select which include syntax must be considered when searching for the
main header: quotes (`#include "foo.hpp"`, the default), brackets
(`#include <foo.hpp>`), or both.

The lack of support for brackets has been reported many times, see the
linked issues, so I am pretty sure there is a need for it :)

A short note about why I did not implement a regex approach as discussed
in #53013: while a regex would have allowed many extra ways to describe
the main header, the bug descriptions listed above suggest a very simple
need: support brackets for the main header. This PR answers this needs
in a quite simple way, with a very simple style option. IMHO the feature
space covered by the regex (again, for which there is no demand :)) can
be implemented latter, in addition to the proposed option.

The PR also includes tests for the option with and without grouped
includes.
2024-02-06 01:06:33 -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
XDeme
7e7f118404
[clang-format] Handles Elaborated type specifier for enum in trailing return (#80085)
Fixes llvm/llvm-project#80062
2024-02-01 14:11:14 -03:00
Kazu Hirata
b67ce7e349 [clang] Use StringRef::starts_with (NFC) 2024-01-31 23:54:09 -08:00
Emilia Kond
9b68c095d6
[clang-format] Allow decltype in requires clause (#78847)
If clang-format is not sure whether a `requires` keyword starts a
requires clause or a requires expression, it looks ahead to see if any
token disqualifies it from being a requires clause. Among these tokens
was `decltype`, since it fell through the switch.

This patch allows decltype to exist in a require clause.

I'm not 100% sure this change won't have repercussions, but that just
means we need more test coverage!

Fixes https://github.com/llvm/llvm-project/issues/78645
2024-02-01 08:00:45 +02: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
Hirofumi Nakamura
0058263600
[clang-format] Support of TableGen tokens with unary operator like form, bang operators and numeric literals. (#78996)
Adds the support for tokens that have forms like unary operators.
- bang operators:  `!name`
- cond operator: `!cond`
- numeric literals: `+1`, `-1`
cond operator are one of bang operators but is distinguished because it has very specific syntax.
2024-01-31 00:30:37 +09:00
Owen Pan
179ade6a6d [clang-format] Fix an issue reported by static analyzer
Fixes #79685.
2024-01-27 21:21:01 -08:00
Owen Pan
f826f55b2a
[clang-format] Fix a bug in AnnotatingParser::rParenEndsCast() (#79549)
Fixes #78965.
2024-01-26 18:32:03 -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