1189 Commits

Author SHA1 Message Date
owenca
6cfedea492
[clang-format] Add SpaceInEmptyBraces option (#153765)
Also set it to SIEB_Always for WebKit style.

Closes #85525.
Closes #93635.
2025-08-17 12:56:22 -07:00
Owen Pan
3d99446809
[clang-format] Fix a bug in DerivePointerAlignment: true (#150744)
This effectively reverts a4d4859dc70c046ad928805ddeaf8fa101793394 which
didn't fix the problem that `int*,` was not counted as "Left" alignment.

Fixes #150327
2025-07-28 00:44:12 -07:00
James Y Knight
9281797a57
[clang-format] Google Style: disable DerivePointerAlignment. (#149602)
The [Google C++ Style
Guide](https://google.github.io/styleguide/cppguide.html#Pointer_and_Reference_Expressions)
is being changed to specify that spaces should go after the
asterisk/ampersand, rather than permitting either before or after on a
file-by-file basis.

The new requirement is:
> When referring to a pointer or reference (variable declarations or
> definitions, arguments, return types, template parameters, etc.),
> you must not place a space before the asterisk/ampersand. Use a
> space to separate the type from the declared name (if present).

The [Google ObjC
style](https://google.github.io/styleguide/objcguide.html) is silent on
this matter, but the de-facto style is not being modified at this time.
So, keep DerivePointerAlignment enabled for ObjC language mode.
2025-07-25 11:55:50 -04:00
Owen Pan
a4d4859dc7
[clang-format] Fix a bug in DerivePointerAlignment: true (#150387)
Fixes #150327
2025-07-24 09:41:22 -07:00
Owen Pan
97c953406d
[clang-format] Add AfterNot to SpaceBeforeParensOptions (#150367)
Closes #149971
2025-07-24 09:38:43 -07:00
Daan De Meyer
1653a093de
[clang-format] Add IgnoreExtension to SortIncludes (#137840)
Sorting by stem gives nicer results when various header file names are
substrings of other header file names. For example, a CLI application
with a main header named analyze.h and an analyze-xxx.h header for each
subcommand currently will always put analyze.h last after all the
analyze-xxx.h headers, but putting analyze.h first instead is arguably
nicer to read.

TLDR; Instead of
```
#include "analyze-blame.h"
#include "analyze.h"
```
You'd get
```
#include "analyze.h"
#include "analyze-blame.h"
```

Let's allow sorting by stem instead of full path by adding
IgnoreExtension to SortIncludes.
2025-07-17 00:36:52 -07:00
Owen Pan
c384ec431d
[clang-format] Add MacrosSkippedByRemoveParentheses option (#148345)
This allows RemoveParentheses to skip the invocations of function-like
macros.

Fixes #68354.
Fixes #147780.
2025-07-13 14:29:51 -07:00
Owen Pan
2f1673eaa0 Follow up on #147623 2025-07-11 18:15:27 -07:00
Alex Sepkowski
7c16a31aa5
Address a handful of C4146 compiler warnings where literals can be replaced with std::numeric_limits (#147623)
This PR addresses instances of compiler warning C4146 that can be
replaced with std::numeric_limits. Specifically, these are cases where a
literal such as '-1ULL' was used to assign a value to a uint64_t
variable. The intent is much cleaner if we use the appropriate
std::numeric_limits value<Type>::max() for these cases.


Addresses #147439
2025-07-09 16:13:28 -07:00
Owen Pan
5ccbea9f48
[clang-format][NFC] Replace size() with empty() (#147164) 2025-07-06 14:19:30 -07:00
Owen Pan
7e1a88b9d1
[clang-format] Handle .h files for LK_C and LK_ObjC (#141714)
Fix #137792
2025-05-28 19:10:20 -07:00
Owen Pan
0dfdf7efbf
[clang-format] Handle raw string literals containing JSON code (#140666)
Fix #65400
2025-05-20 19:15:57 -07:00
Owen Pan
5ddcd765db
[clang-format][NFC] Upgrade SortIncludes option to a struct (#140497)
This allows adding other suboptions e.g. IgnoreExtension in #137840.
2025-05-19 01:32:17 -07:00
Kazu Hirata
4567c11d28
[clang] Use llvm::binary_search (NFC) (#140216) 2025-05-16 07:32:44 -07:00
Filip Milosevic
5df01abe19
[clang-format] Add SpaceAfterOperatorKeyword option (#137610)
Add SpaceAfterOperatorKeyword option to clang-format
2025-05-07 22:47:56 -07:00
Owen Pan
8effc8da29 Reland [clang-format] Add OneLineFormatOffRegex option (#137577) 2025-04-30 19:58:59 -07:00
Owen Pan
7752e0a10b Revert "[clang-format] Add OneLineFormatOffRegex option (#137577)"
This reverts commit b8bb1ccb4f9126d1bc9817be24e17f186a75a08b which triggered
an assertion failure in CodeGenTest.TestNonAlterTest.
2025-04-30 00:12:41 -07:00
Owen Pan
b8bb1ccb4f
[clang-format] Add OneLineFormatOffRegex option (#137577)
Close #54334
2025-04-29 19:22:53 -07:00
Kazu Hirata
f2ec5e40d9
[clang] Use llvm::unique (NFC) (#136469) 2025-04-19 20:33:53 -07:00
Owen Pan
c64f670255
[clang-format] Fix a crash in EnumTrailingComma (#135903)
Fix #135819
2025-04-18 18:11:56 -07:00
Owen Pan
5f744cc630
[clang-format] Wrap and indent lambda braces in GNU style (#135479)
Fix #133135
2025-04-12 15:06:21 -07:00
Owen Pan
09c8cfe219
[clang-format][NFC] Add isJava() and isTextProto() in FormatStyle (#135466)
Also remove redundant name qualifiers format::, FormatStyle::, and
LanguageKind::.
2025-04-12 15:04:29 -07:00
Tim Clephas
c893f1d18d
Treat ipynb as json (#135137)
Fixes #110727
2025-04-10 20:40:43 +02:00
Owen Pan
75cbb1f0fa
[clang-format][NFC] Add FormatToken::is(tok::ObjCKeywordKind) (#134973)
This allows simplification of code that checks if a token is an
Objective-C keyword.

Also, delete the following in
UnwrappedLineParser::parseStructuralElement():
- an else-after-break in the tok::at case
- the copypasted code in the tok::objc_autoreleasepool case
2025-04-09 17:49:26 -07:00
Owen Pan
d71ee7d230
[clang-format] Set C11 instead of C17 for LK_C (#134472)
Fix #134453
2025-04-05 13:35:45 -07:00
Owen Pan
e5fcbfa2aa
[clang-format] Add an option for editing enum trailing commas (#133576)
Also refactor the code that removes/replaces a token.
2025-03-30 16:02:49 -07:00
Owen Pan
05fb8408de
[clang-format] Allow Language: Cpp for C files (#133033)
Fix #132832
2025-03-27 01:00:02 -07:00
Jan Voung
467ad6a035
[clang-format] Add support for absl nullability macros (#130346)
Add support for formatting w/ absl nullability macros
(c52afac4f8/absl/base/nullability.h (L237)).
Example at https://godbolt.org/z/PYv19M1Gj
input:
```
std::vector<int* _Nonnull> x;
std::vector<int* absl_nonnull> y;
```

orig output:
```
std::vector<int* _Nonnull> x;
std::vector<int * absl_nonnull> y;
```

new output:
```
std::vector<int* _Nonnull> x;
std::vector<int* absl_nonnull> y;
```
credit to @ymand for the original patch
2025-03-14 00:01:35 -07:00
Owen Pan
136f2574dd
[clang-format] Lex C++ only keywords as identifiers in C (#129426)
Fix #128847
2025-03-03 17:40:28 -08:00
Owen Pan
0ed2945a59
[clang-format] Don't sort includes for C# (#129369)
Fixes #106194
2025-03-03 17:40:02 -08:00
Owen Pan
80f34e2716
[clang-format] Change BracedInitializerIndentWidth to int (#128988)
Fixes #108526
2025-02-27 20:18:02 -08:00
Owen Pan
0968df9c3a
[clang-format] Add the C language instead of treating it like C++ (#128287)
Closes #128120
2025-02-23 00:36:19 -08:00
Owen Pan
ffc61dc393
[clang-format] Allow specifying the language for .h files (#128122)
Closes #128119
2025-02-21 20:46:43 -08:00
Gedare Bloom
e0a21e23a7
[clang-format] Add BinPackLongBracedList style option (#112482)
The use of Cpp11BracedListStyle with BinPackArguments=False avoids bin
packing until reaching a hard-coded limit of 20 items. This is an
arbitrary choice. Introduce a new style option to allow disabling this
limit.
2025-02-07 22:10:35 -08:00
leijurv
d2b45ce100
[clang-format] Add BreakBeforeTemplateCloser option (#118046)
In clang-format, multiline templates have the `>` on the same line as
the last parameter:

```c++
template <
    typename Foo,
    typename Bar>
void foo() {
```

I would like to add an option to put the `>` on the next line, like
this:

```c++
template <
    typename Foo,
    typename Bar
>
void foo() {
```

An example of a large project that uses this style is NVIDIA's CUTLASS,
here is an example:


https://github.com/NVIDIA/cutlass/blob/main/include/cutlass/epilogue/dispatch_policy.hpp#L149-L156

My reasoning is that it reminds me of this style of braces:

```c++
if (foo()) {
    bar();
    baz();}
```

Most people agree this is better:

```c++
if (foo()) {
    bar();
    baz();
}
```

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-02-06 01:15:47 -08:00
Gedare Bloom
d50ebd47ae
[clang-format] Add style option PenaltyBreakBeforeMemberAccess (#118409)
The penalty for breaking before a member access is hard-coded to 150.
Add a configuration option to allow setting it.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-01-27 21:40:17 -08:00
Sirraide
33656932b0
[clang-format] Rename ExportBlockIndentation -> IndentExportBlock (#123493)
This renames the `ExportBlockIndentation` option and adds a config parse
test, as requested in #110381.
2025-01-21 14:13:13 +01:00
Sirraide
106c483a10
[clang-format] Improve brace wrapping and add an option to control indentation of export { ... } (#110381)
`export { ... }` blocks can get a bit long, so I thought it would make
sense to have an option that makes it so their contents are not indented
(basically the same argument as for namespaces).

This is based on the `NamespaceIndentation` option, except that there is
no option to control the behaviour of `export` blocks when nested because
nesting them doesn’t really make sense.

Additionally, brace wrapping of short `export { ... }` blocks is now controlled by the
`AllowShortBlocksOnASingleLine` option. There is no separate option just for `export`
blocks because you can just write e.g. `export int x;` instead of `export { int x; }`.

This closes #121723.
2025-01-19 00:26:40 +01:00
Owen Pan
04610b901f
[clang-format][NFC] Replace SmallVectorImpl with ArrayRef (#121621) 2025-01-04 16:19:46 -08:00
dmasloff
1c997feff1
[clang-format] Add option WrapNamespaceBodyWithNewlines (#106145)
It wraps the body of namespace with additional newlines, turning this code:
```
namespace N {
int function();
}
```
into the following:
```
namespace N {

int function();

}
```

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-01-02 21:52:01 -08:00
Owen Pan
1a0d0ae234
[clang-format] Add VariableTemplates option (#121318)
Closes #120148.
2025-01-01 18:24:56 -08:00
Galen Elias
486ec4bd74
[clang-format] Add AllowShortNamespacesOnASingleLine option (#105597)
This fixes #101363 which is a resurrection of a previously opened but
never completed review: https://reviews.llvm.org/D11851

The feature is to allow code like the following not to be broken across
multiple lines:

```
namespace foo { class bar; }
namespace foo { namespace bar { class baz; } }
```

Code like this is commonly used for forward declarations, which are
ideally kept compact. This is also apparently the format that
include-what-you-use will insert for forward declarations.

Also, fix an off-by-one error in `CompactNamespaces` code. For nested
namespaces with 3 or more namespaces, it was incorrectly compacting
lines which were 1 or two spaces over the `ColumnLimit`, leading to
incorrect formatting results.
2024-12-30 01:28:03 -08:00
Owen Pan
141c544c03
[clang-format] Skip line splices when sorting C++ includes (#120680)
Fixes #109864.
2024-12-24 21:47:53 -08:00
Jim B
56ab56c857
[clang-format] Add support for .cjs as JavaScript file extension (#118188)
Node uses `.cjs` as an extension for 'common javascript' modules.
2024-12-03 09:03:51 -08:00
Owen Pan
6e57186c0c [clang-format][NFC] Clean up RemoveBraces, RemoveSemi, etc. 2024-11-25 21:11:15 -08:00
Owen Pan
786db636b9
[clang-format] Add KeepFormFeed option (#113268)
Closes #113170.
2024-10-23 19:55:32 -07:00
Owen Pan
b69ac31a3b
[clang-format] Use RemoveEmptyLinesInUnwrappedLines in clang-format s… (#113260)
…tyle
2024-10-22 18:37:43 -07:00
Owen Pan
d989c2410e
[clang-format] Add RemoveEmptyLinesInUnwrappedLines option (#112325)
Fixes #111340.
2024-10-17 20:21:42 -07:00
Iuri Chaer
0fba8381d2
[clang-format] Introduce "ReflowComments: IndentOnly" to re-indent comments without breaking internal structure (think Doxygen). (#96804)
* Convert `ReflowComments` from boolean into a new `enum` which can take
on the value `RCS_Never`, `RCS_IndentOnly`, or `RCS_Always`. The first
one is equivalent to the old `false`, the third one is `true`, and the
middle one means that multiline comments should only have their
indentation corrected, which is what Doxygen users will want.
* Preserve backward compatibility while parsing `ReflowComments`.
2024-10-11 19:14:09 -07:00
Owen Pan
65688274b1
[clang-format][NFC] Clean up AlignConsecutiveStyle (#111285)
- Add a `CHECK_PARSE` for `AcrossComments`.
- Add a `CHECK_PARSE_NESTED_BOOL` for `AlignFunctionPointers`.
- Remove redundant statements.
- Clean up documentation.
2024-10-07 19:12:47 -07:00