1214 Commits

Author SHA1 Message Date
owenca
79042e701b
[clang-format] Add LeaveAll to the BreakAfterAttributes option (#187204)
Closes #163537
2026-03-18 23:14:38 -07:00
Alexander Kornienko
7e51783b0b
Revert "[Format] Configure ASSIGN_OR_RETURN macros for Google style" (#186445)
Reverts llvm/llvm-project#169037

The change breaks formatting of real code containing ASSIGN_OR_RETURN
macros nested into lambdas. See
https://github.com/llvm/llvm-project/pull/169037#issuecomment-4056423543
for the test case.
2026-03-16 20:16:28 +01:00
Tomáš Slanina
a7aebd809d
[clang-format] Add option AllowShortRecordOnASingleLine (#154580)
This patch supersedes PR #151970 by adding the option
``AllowShortRecordOnASingleLine`` that allows the following formatting:
```c++
  struct foo {};
  struct bar { int i; };
  struct baz
  {
    int i;
    int j;
    int k;
  };
```

---------

Co-authored-by: owenca <owenpiano@gmail.com>
2026-03-15 02:52:30 -07:00
Ivan
27e4ef689a
[clang-format] Upgrade ShortFunctionStyle to a struct (#134337)
The current clang-format configuration
option AllowShortFunctionsOnASingleLine uses a single enum
(ShortFunctionStyle) to control when short function definitions can be
merged onto a single line. This enum provides predefined combinations of
conditions
(e.g., None, Empty only, Inline only, Inline including Empty, All).

This approach has limitations:

1. **Lack of Granularity:** Users cannot specify arbitrary combinations
of conditions. For example, a user might want to allow merging
for both empty functions and short top-level functions, but not for
short functions defined within classes. This is not possible with the
current enum options except by choosing All, which might merge more than
desired.

2. **Inflexibility:** Adding new conditions for merging (e.g.,
distinguishing between member functions and constructors, handling
lambdas specifically) would require adding many new combined enum
values, leading to a combinatorial explosion and making the
configuration complex.

3. **Implicit Behavior:** Some options imply others
(e.g., Inline implies Empty), which might not always be intuitive or
desired.

The goal is to replace this single-choice enum with a more flexible
mechanism allowing users to specify a set of conditions that must be met
for a short function to be merged onto a single line.

---------

Co-authored-by: owenca <owenpiano@gmail.com>
2026-03-14 23:01:07 -07:00
nataliakokoromyti
7894e636f3
[clang-format] Ignore imports in comments for Java import sorting (#177326)
Java source files can contain apparent import statements inside block
comments (e.g., showing a code example). These can get mixed up with
real import statements when run through clang-format.

This patch tracks block comments (/* ... */) so that we skip lines that
are inside them.

Fixes #176771

---------

Co-authored-by: Natalia Kokoromyti <knatalia@yost-cm-01-imme.stanford.edu>
Co-authored-by: owenca <owenpiano@gmail.com>
2026-03-14 01:07:57 -07:00
Ezlanding1
8b5331f06f
[clang-format] Update goto label indentation styles (#180109)
Updates the goto label indentation styles based on the feature request. 

Resolves #24492.
2026-03-08 16:45:47 +01:00
Naveen Seth Hanig
4f50a725fa
[clang][clang-scan-deps] Add LangOptions::AllowLiteralDigitSeparator to fix #88896 (#184235)
Fixes #88896 by following the approach described in
https://github.com/llvm/llvm-project/pull/95798#discussion_r1649496882.
This adds `LangOptions::AllowLiteralDigitSeparator`, following the
pattern used for `RawStringLiterals` in #88265.

It is enabled by default for C++14 and C23, and the Scanner sets it
explicitly to always allow literals with digit separators in directives.

Originally authored by @tsfn (Yifan Fang, <gatsfn@gmail.com>) in
#158420.

Co-authored-by: Yifan Fang <gatsfn@gmail.com>
2026-03-02 22:57:51 +01:00
owenca
ab1d59e725
[clang-format] Allow InheritParentConfig to accept a directory (#182791)
Add support for `BasedOnStyle: InheritParentConfig=<directory-path>` in
config files to redirect inheritance to the `.clang-format` or
`_clang-format` file in the `<directory_path>` directory.

Closes #107808
2026-03-01 20:12:17 -08:00
Sergey Subbotin
62e55b410c
[clang-format] Add per-operator granularity for BreakBinaryOperations (#181051)
## Summary

Extend `BreakBinaryOperations` to accept a structured YAML configuration
with per-operator break rules and minimum chain length gating via
`PerOperator`.

- **Per-operator rules**: specify break style (`Never`, `OnePerLine`,
`RespectPrecedence`) for specific operator groups
- **Minimum chain length**: only trigger breaking when a chain has N or
more operators
- **Fully backward-compatible**: the simple scalar form
(`BreakBinaryOperations: OnePerLine`) behaves identically to the current
enum value

RFC discussion:
https://discourse.llvm.org/t/rfc-per-operator-granularity-for-breakbinaryoperations/89800

### New YAML syntax

```yaml
BreakBinaryOperations:
  Default: Never
  PerOperator:
    - Operators: ['&&', '||']
      Style: OnePerLine
      MinChainLength: 3
    - Operators: ['|']
      Style: OnePerLine
```

### Motivation

`BreakBinaryOperations` operates at the level of all binary operators
simultaneously. Enabling `OnePerLine` for `&&`/`||` condition chains
also forces it on `+`, `|`, and other operators, which may not be
desired. The only workaround today is `// clang-format off`.

## Test plan

- [x] All existing `BreakBinaryOperations` unit tests updated and
passing
- [x] New tests for per-operator rules (`&&`/`||` OnePerLine + default
Never)
- [x] New tests for multiple operator groups (`&&`/`||` + `|`)
- [x] New tests for `MinChainLength` gating (chain of 2 vs 3+)
- [x] Config parse tests for structured YAML form
- [x] RST documentation auto-generated via `dump_format_style.py`
- [x] Release notes updated
2026-02-21 23:08:18 +01:00
Ilya Biryukov
93c93ff199
[Format] Configure ASSIGN_OR_RETURN macros for Google style (#169037)
These macros are used by many of the Google projects, e.g.:
-
https://chromium.googlesource.com/chromium/src/+/133.0.6943.141/base/types/expected_macros.h#104
-
1477683618/src/google/protobuf/stubs/status_macros.h (L62)

---------

Co-authored-by: Daniel Jasper <djasper@google.com>
2026-02-17 11:01:05 +01:00
Perceval Anichini
cc67cb624d
[clang-format] Add AfterComma option for BreakConstructorInitializers (#180541)
This patch introduces a new `AfterComma` value for the
`BreakConstructorInitializers` style option. This style keeps the
constructor initializer colon on the same line as the constructor
declaration and breaks after each comma in the initializer list.

Example formatting:
```cpp
Constructor() : initializer1(),
                initializer2()
```

This style provides an alternative to the existing BeforeColon,
BeforeComma, and AfterColon options, allowing developers to maintain the
colon on the constructor line while placing each initializer on its own
line after commas.

---------

Co-authored-by: Perceval <perceval@trilogic.fr>
2026-02-16 20:26:02 +01:00
Vikas Patel
90cc5a4618
[clang-format] Add ObjCSpaceBeforeMethodDeclColon option to control space before Objective-C method return type (#170579)
[clang-format] Add ObjCSpaceBeforeMethodDeclColon option to control
space before Objective-C method return type

This patch introduces the ObjCSpaceBeforeMethodDeclColon style option,
allowing users to add or remove a space between the '-'/'+' and the
return type in Objective-C method declarations (e.g., '- (void)method'
vs '-(void)method').

Includes documentation and unit tests.
2026-01-27 19:55:20 +00:00
Björn Schäpers
3a25a4a687
[clang-format] Add xxxMaxDigitsNoSeparator (#164286)
This basically adds a Leave option for a specific range of literals.
2025-11-26 21:21:45 +01:00
Björn Schäpers
bcb1b773f6
[clang-format] Add option to separate comment alignment between ... (#165033)
normal lines and PP directives.

Handling PP directives differently can be desired, like in #161848.
Changing the default is not an option, there are tests for exactly the
current behaviour.
2025-11-07 15:12:30 +00:00
Gedare Bloom
8067b5cff7
[clang-format] Add BreakAfterOpenBracket* and BreakBeforeCloseBracket* (#108332)
Replace the `AlwaysBreak` and `BlockIndent` suboptions of
`AlignAfterOpenBracket` with new style options `BreakAfterOpenBracket*`
and `BreakBeforeCloseBracket*` for `*` in `BracedList` for braced list
initializers, `if` for if conditional statements, `Loop` for loop
control statements (for/while), `Switch` for switch statements, and
`Function` for function calls/declarations/definitions.

Deprecates `AlwaysBreak` and `BlockIndent`.
 
Fixes #67738 
Fixes #79176 
Fixes #80123 
Fixes #151844
2025-10-30 20:24:44 +01:00
Björn Schäpers
cf28a476fb
[clang-format] Remove special handling of comments after brace/paren (#71672)
Fixes http://llvm.org/PR55487 (#55487)

The code did not match the documentation about Cpp11BracedListStyle.
Changed handling of comments after opening braces, which are supposedly
function call like to behave exactly like their parenthesis counter
part.
2025-10-18 22:25:28 +00:00
owenca
059f2df748
[clang-format] Fix an assertion failure on comment-only config files (#163111) 2025-10-16 21:40:59 -07:00
sstwcw
2a05904288
[clang-format] Keep the ObjC selector name and @selector together (#160739)
Fixes #36459.

after

```Objective-C
- (void)test {
  if ([object
          respondsToSelector:@selector(
                                 selectorNameThatIsReallyLong:param1:param2:)])
    return;
}
```

before

```Objective-C
- (void)test {
  if ([object respondsToSelector:@selector
              (selectorNameThatIsReallyLong:param1:param2:)])
    return;
}
```

Before this patch, the `ObjCMethodExpr` type was assigned to many kinds
of tokens. The rule for allowing breaking the line before the colon on
line TokenAnnotator.cpp:6289 was intended for method declarations and
calls. It matched the parenthesis following `@selector` by mistake. To
fix the problem, this patch adds a new type for `@selector`. Most of the
special things in the code related to the old type is intended for other
constructs. So most of the code related to the old type is not changed
in this patch.
2025-10-03 20:40:48 +00:00
Björn Schäpers
847e1e1890
[clang-format][NFC] Introduce isNoneOf (#161021)
And apply throughout the code base.
2025-10-02 20:52:45 +02:00
owenca
d7921de802
[clang-format] Correctly handle backward compatibility of C headers (#159908)
This in effect reverts 05fb8408de23c3ccb6125b6886742177755bd757 and
7e1a88b9d1431e263258e3ff0f729c1fdce342d3, the latter of which
erroneously changed the behavior of formatting `ObjC` header files when
both the default and `ObjC` styles were absent. Now the previous
behavior of treating that as an error is restored.

Fixes #158704
2025-09-25 00:44:33 -07:00
owenca
583256d165
[clang-format] Add AllowBreakBeforeQtProperty option (#159909)
The test cases are adapted from #131605.
2025-09-22 00:07:14 -07:00
Owen Pan
43cfad6e26 [clang-format][NFC] Add is_sorted() assertion for binary_search() 2025-09-19 22:30:46 -07:00
owenca
30f4781eef
[clang-format] Handle C digit separators (#158418)
Fixes #158413
2025-09-14 14:20:59 -07:00
Gedare Bloom
7dd2f1cc10
[clang-format] Add IndentPPDirectives Leave option (#139750)
Allow an option to leave preprocessor directive indenting as-is. This
simplifies handling mixed styles of CPP directive indentation.

Fixes #38511
2025-09-14 13:47:06 -07:00
Andy MacGregor
220d705d21
[clang-format] Add an option to format numeric literal case (#151590)
Some languages have the flexibility to use upper or lower case
characters interchangeably in integer and float literal definitions.

I'd like to be able to enforce a consistent case style in one of my
projects, so I added this clang-format style option to control it.

With this .clang-format configuration:
```yaml
    NumericLiteralCaseStyle:
      UpperCasePrefix: Never
      UpperCaseHexDigit: Always
      UpperCaseSuffix: Never
```

This line of code:
```C
    unsigned long long  0XdEaDbEeFUll;
```

gets reformatted into this line of code:
```C
    unsigned long long 0xDEAFBEEFull;
```

-----

I'm new to this project, so please let me know if I missed something in
the process. I modeled this PR from
[IntegerLiteralSeparatorFixer](https://reviews.llvm.org/D140543)
2025-09-12 01:17:40 -07:00
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