1139 Commits

Author SHA1 Message Date
Brad House
f0bd62d870
[clang-format] Add AlignFunctionDeclarations to AlignConsecutiveDeclarations (#108241)
Enabling AlignConsecutiveDeclarations also aligns function prototypes
or declarations.  This is often unexpected as typically function
prototypes, especially in public headers, don't use any padding.

Setting AlignFunctionDeclarations to false will skip this alignment.
It is by default set to true to keep compatibility with prior
versions to not make unexpected changes.

Fixes #74320
2024-10-06 17:46:43 -07:00
Owen Pan
688bc958bd
[clang-format] Add TemplateNames option to help parse C++ angles (#109916)
Closes #109912.
2024-10-02 18:10:56 -07:00
Tom
7c3237d778
[clang-format] Change BinPackParameters to enum and add AlwaysOnePerLine (#101882)
Related issues that have requested this feature:
#51833
#23796 
#53190 Partially solves - this issue requests is for both arguments and
parameters
2024-08-22 21:42:22 -07:00
Owen Pan
3496245ed3
[clang-format] Change GNU style language standard to LS_Latest (#104669)
Fixes #104655.
2024-08-18 12:02:15 -07:00
Nathan Sidwell
4cf9a42661
[clang-format] Adjust requires clause wrapping (#101550) (#102078)
Address #101550 by adding OwnLineWithBrace option for RequiresClausePosition. This permits placing a following '{' on the same line as the requires clause.

Thus, instead of:
```
bool Foo ()
  requires(true)
{
  return true;
}
```

we have:
```
bool Foo ()
  requires(true) {
  return true;
}
```

If the function body is empty, we'll get:
```
bool Foo ()
  requires(true) {}
```

I attempted to get a line break between the open and close braces, but
failed. Perhaps that's fine -- it's rare and only happens in the empty
body case.
2024-08-16 21:05:08 -04:00
Ameer J
c5a4291fb7
[clang-format] Add BreakBinaryOperations configuration (#95013)
By default, clang-format packs binary operations, but it may be
desirable to have compound operations be on individual lines instead of
being packed.

This PR adds the option `BreakBinaryOperations` to break up large
compound binary operations to be on one line each.

This applies to all logical and arithmetic/bitwise binary operations

Maybe partially addresses #79487 ?
Closes #58014 
Closes #57280
2024-08-10 13:28:33 -07:00
Owen Pan
ceade83ad5
[clang-format] Skip block commented out includes when sorting them (#97787)
Fixes #97539.
2024-07-05 15:28:16 -07:00
Gedare Bloom
54f040fff3
[clang-format] Add SpacesInParensOption for filtering repeated parens (#77522)
The __attribute((specifier-list)) currently is formatted based on the
SpacesInParensOptions.Other (previously, SpacesInParentheses). This
change allows finer control over addition of spaces between the
consecutive parens, and between the inner parens and the list of
attribute specifiers.

Differential Revision: https://reviews.llvm.org/D155529

This is migrated from Phabricator, see more discussion there.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-07-02 00:00:14 -07:00
sstwcw
2853a838d2 [clang-format] Add option to remove leading blank lines (#91221)
The options regarding which blank lines are kept are also aggregated.
The new option is `KeepEmptyLines`.

This patch was initially part of 9267f8f19a2e502e.  I neglected to check
the server builds before I added it.  It broke clangd.  Jie Fu fixed the
problem in 4c91b49bab0728d4.  I was unaware of it.  I thought the main
branch was still broken.  I reverted the first patch in
70cfece24d6cbb57.  It broke his fix.  He reverted it in
c69ea04fb9738db2.  Now the feature is added again including the fix.
2024-06-30 01:20:20 +00:00
sstwcw
70cfece24d Revert "[clang-format] Add option to remove leading blank lines (#91221)"
This reverts commit 9267f8f19a2e502ef5a216c0d52b352b3699d399.

I changed a formatter option.  I forgot to update other components that
depend on the formatter when the option name changed.
2024-06-25 13:12:32 +00:00
sstwcw
9267f8f19a [clang-format] Add option to remove leading blank lines (#91221)
The options regarding which blank lines are kept are also aggregated.
The new option is `KeepEmptyLines`.
2024-06-25 03:50:11 +00:00
pointhex
fe9aef05c2
[clang-format] Add DiagHandler parameter to format::getStyle() (#91317)
It allows to control of error output for the function.

Closes #94205.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-06-16 13:58:26 -07:00
Owen Pan
dba2aa265c
[clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (#93402)
Closes #92999.
2024-05-27 15:20:58 -07:00
Owen Pan
1c58208d89 [clang-format][NFC] Remove redundnat llvm::, clang::, etc. 2024-05-20 20:32:18 -07:00
Owen Pan
364f988d3f Reland "[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)"
Remove FormatToken::isSimpleTypeSpecifier() and call
Token::isSimpleTypeSpecifier(LangOpts) instead.
2024-05-13 21:54:23 -07:00
Owen Pan
1fadb2b0c8 Revert "[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)"
This reverts commits e62ce1f8842c, 5cd280433e8e, and de641e289269 due to
buildbot failures.
2024-05-12 23:15:35 -07:00
Kazu Hirata
deffae5da1
[clang] Use StringRef::operator== instead of StringRef::equals (NFC) (#91844)
I'm planning to remove StringRef::equals in favor of
StringRef::operator==.

- StringRef::operator==/!= outnumber StringRef::equals by a factor of
  24 under clang/ in terms of their usage.

- The elimination of StringRef::equals brings StringRef closer to
  std::string_view, which has operator== but not equals.

- S == "foo" is more readable than S.equals("foo"), especially for
  !Long.Expression.equals("str") vs Long.Expression != "str".
2024-05-11 11:38:52 -07:00
Owen Pan
e62ce1f884
[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)
Remove FormatToken::isSimpleTypeSpecifier() and call
Token::isSimpleTypeSpecifier(LangOpts) instead.
2024-05-10 19:27:02 -07:00
Owen Pan
236b3e1aad
[clang-format] Handle Java switch expressions (#91112)
Also adds AllowShortCaseExpressionOnASingleLine option and
AlignCaseArrows suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
2024-05-06 19:55:55 -07:00
NorthBlue333
2de0bedfeb
[clang-format] Do not update cursor pos if no includes replacement (#77456)
Fix https://github.com/llvm/llvm-project/issues/77450.

---------

Signed-off-by: NorthBlue333 <north333@free.fr>
Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-04-25 20:34:49 -07:00
Jannik Silvanus
2c5d7a8885
[clang-format] Remove YAML hack to emit a BasedOnStyle comment (#89228)
When serializing a formatting style to YAML, we were emitting a comment
`# BasedOnStyle: <style>` if the serialized formatting style matches one
of the known styles. This is useful, but mis-uses the YAML API.

An upcoming change to fix keys with special characters by quoting them
breaks this,
and will emit a non-comment **key** `'# BasedOnStyle': <style>` instead.
(https://github.com/llvm/llvm-project/pull/88763)

Thus, remove this hack. There doesn't seem to be a specific use for it,
and it is not tested.

If we want the comment back, we should add comment support to the YAML writer,
and use that instead.
2024-04-25 12:47:43 +02:00
Jing Wang
6dbd4bb3b4
Add txtpb to the list of supported TextProto extensions (#88355)
According to
https://protobuf.dev/reference/protobuf/textformat-spec/#text-format-files,
txtpb is the canonical extension

Co-authored-by: Jing Wang <99jingw@gmail.com>
2024-04-12 21:41:06 +02:00
Owen Pan
72e2e4f7dc
[clang-format] Lambda parameter should be passed by const reference (#87306)
Closes #87254.
2024-04-03 09:00:23 -07:00
Ameer J
13be0d4a34
[clang-format] Add BreakFunctionDefinitionParameters option (#84988)
This adds an option to break function definition parameters, putting
them on the next line after the function's opening paren.

This was a missing step towards allowing styles which require all
function definition parameters be on their own lines.

Closes #62963
2024-04-02 21:00:51 +02:00
Hirofumi Nakamura
e54af60816
[clang-format] Added AlignConsecutiveTableGenBreakingDAGArgColons option. (#86150)
The option to specify the style of alignment of the colons inside TableGen's DAGArg.
2024-03-22 23:11:36 +09:00
Owen Pan
6f31cf51df Revert "[clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599)"
This reverts c3a1eb6207d8 (and the related commit f3c5278efa3b) which makes
cleanupAroundReplacements() no longer thread-safe.
2024-03-19 18:06:59 -07:00
Hirofumi Nakamura
0c423af59c
[clang-format] Add Options to break inside the TableGen DAGArg. (#83149)
Add two options to control the line break inside TableGen DAGArg.
- TableGenBreakInsideDAGArg
- TableGenBreakingDAGArgOperators
2024-03-19 00:13:59 +09:00
Owen Pan
f3c5278efa [clang-format][NFC] Don't export IsCpp in Format.h 2024-03-17 00:36:12 -07:00
Owen Pan
426e694589 [clang-format][NFC] Delete redundant and extraneous #include lines 2024-03-15 20:51:57 -07:00
Owen Pan
c3a1eb6207 Reland [clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599)
Initialize IsCpp in LeftRightQualifierAlignmentFixer ctor.
2024-03-14 19:44:40 -07:00
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
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
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
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
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
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
d033799050
[clang-format] Add Leave to AlwaysBreakTemplateDeclarations (#80569)
Closes #78067.
2024-02-07 20:24:44 -08: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
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
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