3749 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
owenca
a21d17f1d7
[clang-format] Fix a bug in breaking before FunctionDeclarationName (#153924)
Fixes #153891
2025-08-17 12:54:48 -07:00
owenca
5e57a10f50
[clang-format] Allow breaking before bit-field colons (#153529)
Fixes #153448
2025-08-17 12:54:23 -07:00
owenca
9a692e0f94
[clang-format] Don't annotate class property specifiers as StartOfName (#153525)
Fixes #153443
2025-08-17 12:53:57 -07:00
owenca
3d38a92c8a
[clang-format] Add functionality of getting info about numeric literals (#152878) 2025-08-11 09:24:28 -07:00
Owen Pan
d696f8104e
[clang-format][NFC] Maximize usage of isOneOf() in TokenAnnotator (#151658)
Also make a StringRef literal `static constexpr`.
2025-08-02 12:18:04 -07:00
Owen Pan
5fc482cfc0
[clang-format] Disable IntegerLiteralSeparator for C++ before c++14 (#151273)
Fixes #151102
2025-07-30 09:43:46 -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
Eric Li
531cf8298b
[clang-format] Stop ctor initializer from being inlined (#150361)
The colon in a constructor's initializer list triggers the inlining of a
nested block as if it was a conditional operator expression. This
prevents line breaks under certain circumstances when the initializer
list contains braced initializers, which in turn prevents the line
formatter from finding a solution.

In this commit we exclude colons that are a constructor initializer
colon from consideration of nested block inlining.

Fixes #97242.
Fixes #81822.
2025-07-24 04:06:03 -04:00
Owen Pan
4d48996ff0
[clang-format][NFC] Clean up around StringRef initializations (#149765)
Consistently use `constexpr StringRef Code("string literal");`.
2025-07-21 19:11:17 -07:00
sstwcw
12a3afe47d
[clang-format] Remove code related to trigraphs (#148640)
When reviewing #147156, the reviewers pointed out that we didn't need to
support the trigraph. The code never handled it right.

In the debug build, this kind of input caused the assertion in the
function `countLeadingWhitespace` to fail. The release build without
assertions outputted `?` `?` `/` separated by spaces.

```C
#define A ??/
  int i;
```

This is because the code in `countLeadingWhitespace` assumed that the
underlying lexer recognized the entire `??/` sequence as a single token.
In fact, the lexer recognized it as 3 separate tokens. The flag to make
the lexer recognize trigraphs was never enabled.

This patch enables the flag in the underlying lexer. This way, the
program now either turns the trigraph into a single `\` or removes it
altogether if the line is short enough. There are operators like the
`??=` in C#. So the flag is not enabled for all input languages. Instead
the check for the token size is moved from the assert line into the if
line.

The problem was introduced by my own patch 370bee480139 from about 3
years ago. I added code to count the number of characters in the escape
sequence probably just because the block of code used to have a comment
saying someone should add the feature. Maybe I forgot to enable
assertions when I ran the code. I found the problem because reviewing
pull request 145243 made me look at the code again.
2025-07-21 15:40:28 +00:00
Owen Pan
06ed4d2c92
[clang-format] Fix a bug in BreakBeforeBinaryOperators: All (#149695)
Fixes #149520
2025-07-20 19:01:46 -07:00
Owen Pan
a8f5e9ed6b
[clang-format] Fix a regression of annotating PointerOrReference (#149039)
Fixes #149010
2025-07-17 21:43: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
b79c763e91
[clang-format][NFC] Simplify some logic in BreakableLineCommentSection (#148324) 2025-07-13 20:35:45 -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
Owen Pan
cb52efb893
[clang-format] Split line comments separated by backslashes (#147648)
Fixes #147341
2025-07-10 18:14:45 -07:00
Owen Pan
7704f817d0
[clang-format][NFC] Replace a function with StringRef::contains (#146245) 2025-07-10 18:07:17 -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
a5af874503 [clang-format][NFC] Use empty() instead of comparing size() to 0 or 1 2025-07-06 23:54:00 -07:00
Owen Pan
5ccbea9f48
[clang-format][NFC] Replace size() with empty() (#147164) 2025-07-06 14:19:30 -07:00
Eric Li
878ce210e3
[clang-format] Propagate LeadingEmptyLinesAffected when joining lines (#146761)
Before this commit, when `LineJoiner` joins a line with affected leading
whitespace, it would drop the knowledge of this entirely. However, when
the `AffectedRangeManager` is computing the affected lines, the leading
empty whitespace lines are potentially considered for non-first tokens
in the `AnnotatedLine`. This causes a discrepancy in behavior when an
`AnnotatedLine` is put together from joining multiple lines versus when
it is not.

We change `LineJoiner::join` to follow `AffectedRangeManager`'s logic,
considering the leading whitespace when determining `Affected` for a
token.


a63f572628/clang/lib/Format/AffectedRangeManager.cpp (L111-L130)

Fixes #138942.
2025-07-05 17:06:59 -04:00
Owen Pan
fa058647fe
[clang-format] Make EndsInComma in ContinuationIndenter consistent (#146256) 2025-06-29 12:41:34 -07:00
Owen Pan
f771d08a24
[clang-format] Fix a bug in ReflowComments: Always (#146202)
Fixes #39150
2025-06-28 15:23:00 -07:00
Owen Pan
f2f17e563d
[clang-format][NFC] Remove \brief from comments (#145853)
This was done before in https://reviews.llvm.org/D46320
2025-06-26 19:14:34 -07:00
Owen Pan
117ed65c07 [clang-format][NFC] Remove a redundant check for null
Missed by #145686
2025-06-25 20:10:53 -07:00
Naveen Seth Hanig
dd47b845a6
[clang-format] Handle Trailing Whitespace After Line Continuation (P2223R2) (#145243)
Fixes #145226.

Implement
[P2223R2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2223r2.pdf)
in clang-format to correctly handle cases where a backslash '\\' is
followed by trailing whitespace before the newline.
Previously, `clang-format` failed to properly detect and handle such
cases, leading to misformatted code.

With this, `clang-format` matches the behavior already implemented in
Clang's lexer and `DependencyDirectivesScanner.cpp`, which allow
trailing whitespace after a line continuation in any C++ standard.
2025-06-25 18:13:00 +02:00
Aaron Ballman
01174ff2ba
Remove unneeded checks for null; NFC (#145686)
At the start of the case for `tok::colon`, we do an early return if
`Prev` is null. Nothing else within that case modifies the value of
`Prev`, so the checks for null are unnecessary and were confusing
static analysis tools.
2025-06-25 09:04:36 -04:00
Owen Pan
01b288fe6a
[clang-format] Improve QualifierAlignment in guessing macros (#145468)
Fixes #145388
2025-06-25 00:01:06 -07:00
Owen Pan
1506ba95d7
[clang-format][NFC] Clean up DisallowLineBreaks lambda (#144255)
See also
https://github.com/llvm/llvm-project/pull/141576/files#r2141808121
2025-06-15 18:28:06 -07:00
Owen Pan
2e7fbb94bc
[clang-format] Fix a bug in annotating braces (#144095)
Stop looking for function decls after hitting a BK_BracedInit brace.

Fixes #144057.
2025-06-14 00:21:39 -07:00
Kazu Hirata
e266d6a5da
[Format] Use llvm::min_element (NFC) (#143725)
llvm::min_elements allows us to pass a range.
2025-06-11 12:50:11 -07:00
Owen Pan
9a894ae794
[clang-format] Parse JSON outermost l_brace as braced list brace (#143327)
See
https://github.com/llvm/llvm-project/issues/65400#issuecomment-2922181979.
2025-06-09 20:34:12 -07:00
Owen Pan
0c495ce9c4
[clang-format] Handle function decls with MS calling conventions (#143083) 2025-06-06 20:43:35 -07:00
Owen Pan
eb71fdde57
[clang-format] More consumeToken() cleanup (#143063)
Similar to #142104
2025-06-05 23:36:43 -07:00
Owen Pan
05c12b2284
[clang-format] Handle requires clause following a pointer type (#142893)
Fix #142818
2025-06-05 19:55:37 -07:00
Owen Pan
7bf5862dbf
[clang-format] Correctly annotate token-pasted function decl names (#142337)
Fix #142178
2025-06-02 13:35:27 -07:00
Owen Pan
c5cce4861c Revert "[clang-format] Handle token-pasted function decl names (#142251)"
This reverts commit 29f79ea3c59649f7686a09845665660c25ca3f9b which caused a regression.

See https://github.com/llvm/llvm-project/pull/142251#issuecomment-2928718530.
2025-06-01 23:32:00 -07:00
Kazu Hirata
dfaa66281a
[Format] Remove unused includes (NFC) (#142296)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-05-31 19:00:34 -07:00
Owen Pan
29f79ea3c5
[clang-format] Handle token-pasted function decl names (#142251)
Fix #142178
2025-05-31 11:11:00 -07:00
Owen Pan
4851886693
[clang-format] Handle bit-field colon of non-numeric-constant size (#142110)
Fix #142050
2025-05-30 18:51:01 -07:00
Owen Pan
3f0a5305b4
[clang-format][NFC] Clean up AnnotatingParser::consumeToken() (#142104) 2025-05-30 01:51:23 -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
Kazu Hirata
23c2f8827b
[Format] Use llvm::count_if (NFC) (#141518) 2025-05-26 19:43:28 -07:00
Owen Pan
b7f5950bb3
[clang-format] Handle Java text blocks (#141334)
Fix #61954
2025-05-25 15:40:45 -07:00
Owen Pan
eb341f0b04
[clang-format][NFC] FormatTokenLexer.cpp cleanup (#141202) 2025-05-23 13:49:28 -07:00