41 Commits

Author SHA1 Message Date
Owen Pan
01b288fe6a
[clang-format] Improve QualifierAlignment in guessing macros (#145468)
Fixes #145388
2025-06-25 00:01:06 -07:00
Owen Pan
eb6ca1242c
[clang-format] Hanlde qualified type name for QualifierAlignment (#125327)
Fixes #125178.
2025-02-04 01:33:44 -08:00
Owen Pan
3bd8b02aa0
[clang-format] Handle leading C++11 attribute in QualifierAlignment (#123690)
Fixes #123573.
2025-01-21 21:22:13 -08:00
Owen Pan
40acaa394f
[clang-format] Fix a crash in QualifierAlignment: Right (#120821)
Fixes #120793.
2024-12-21 17:16:29 -08:00
Owen Pan
714033a6bf
[clang-format] Don't insert a space between :: and * (#105043)
Also, don't insert a space after ::* for method pointers.

See
https://github.com/llvm/llvm-project/pull/86253#issuecomment-2298404887.

Fixes #100841.
2024-08-22 20:02:48 -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
e20800c16f [clang-format][NFC] Test IsQualifier only needs to call the lexer 2024-05-13 19:45:11 -07:00
Owen Pan
c72e94382c
[clang-format][NFC] Move LeftRightQualifierAlignmentFixer::is...() (#91930)
Move static member functions LeftRightQualifierAlignmentFixer::is...()
out of the class so that #91712 can reland.
2024-05-13 19:19:15 -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
Owen Pan
de641e2892 [clang-format] Fix buildbot failures
Fix the following buildbot failures by making LangOpts in the unit test
static:
https://lab.llvm.org/buildbot/#/builders/236/builds/11223
https://lab.llvm.org/buildbot/#/builders/239/builds/6968
2024-05-12 18:56:18 -07:00
Owen Pan
626025ac77 Revert "[clang-format] Fix buildbot failures"
This reverts commit 0869204cff22831d0bb19a82c99bf85e4deb4ae3, which caused a
buildbot failure:
https://lab.llvm.org/buildbot/#/builders/5/builds/43322
2024-05-12 18:35:35 -07:00
Owen Pan
0869204cff [clang-format] Fix buildbot failures
This effectively reverts 5cd280433e8e and changes to QualifierFixerTest.cpp
from e62ce1f8842c.

Failed buidbots:
https://lab.llvm.org/buildbot/#/builders/236/builds/11223
https://lab.llvm.org/buildbot/#/builders/239/builds/6968
2024-05-11 15:23:35 -07:00
Owen Pan
5cd280433e [clang-format] Move QualifierFixerTest::LangOpts to IsQualifierType test
This should fix buildbot failures in:
https://lab.llvm.org/buildbot/#/builders/5/builds/43303
https://lab.llvm.org/buildbot/#/builders/168/builds/20347
2024-05-10 23:14:58 -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
579dc7f844
[clang-forma] Support PointerAlignment for pointers to members (#86253)
Fixes #85761.
2024-03-23 02:03:45 -07:00
Owen Pan
ea16a3bb4c
[clang-format] Don't sort qualifiers across preprocessor directives (#81958)
Fixes #80579.
2024-02-16 23:19:31 -08:00
Owen Pan
02362b1ad1 [clang-format] Check token size in QualifierFixerTest.cpp 2024-02-09 02:14:50 -08:00
Owen Pan
245d7727d5 Revert "[clang-format] Fix an out-of-bounds bug uncovered by 763139afc19d"
This reverts commit 173e674ba55eb93e8af43f2eece7feffe9954b34.

Actually, NotTokens[5] in QualifierFixerTest.cpp is not out of bounds.
2024-02-09 01:53:47 -08:00
Owen Pan
173e674ba5 [clang-format] Fix an out-of-bounds bug uncovered by 763139afc19d 2024-02-09 00:21:09 -08:00
Owen Pan
f0ad9ea36a
[clang-format] Handle lambdas in QualifierAlignment (#72456)
Fixed #62780.
2023-11-16 15:00:09 -08:00
Owen Pan
b04664be6f
[clang-format] Handle constrained auto in QualifierAlignment (#72251)
Fixed #69610.
2023-11-15 14:28:37 -08:00
Owen Pan
6c3bc91058 [clang-format][NFC] Remove more extraneous newlines in unit tests
Also removed a duplicate test case.
2023-10-27 02:22:36 -07:00
Owen Pan
77b2fc8ad9 [clang-format][NFC] Reformat and fix file mode
Fix file mode errors introduced in 2c9372e78d7c and format errors in
https://reviews.llvm.org/D155239.
2023-08-30 18:35:24 -07:00
Sedenion
899c867794 [clang-format] Fixed bad performance with enabled qualifier fixer.
This fixes github issue #57117: If the "QualifierAlignment"
option of clang-format is set to anything else but "Leave", the
"QualifierAlignmentFixer" pass gets enabled. This pass scales
quadratically with the number of preprocessor branches, i.e.
with the number of elements in TokenAnalyzer::UnwrappedLines.
The reason is that QualifierAlignmentFixer::process() generates
the UnwrappedLines, but then QualifierAlignmentFixer::analyze()
calls LeftRightQualifierAlignmentFixer::process() several times
(once for each qualifier) which again each time generates the
UnwrappedLines.

This commit gets rid of this double loop by registering the
individual LeftRightQualifierAlignmentFixer passes directly in
the top most container of passes (local variable "Passes" in
reformat()).
With this change, the original example in the github issue #57117
now takes only around 3s instead of >300s to format.

Since QualifierAlignmentFixer::analyze() got deleted, we also
no longer have the code with the NonNoOpFixes. This causes
replacements that end up not changing anything to appear in the
list of final replacements. There is a unit test to check that
this does not happen: QualifierFixerTest.NoOpQualifierReplacements.
However, it got broken at some point in time. So this commit
fixes the test. To keep the behavior that no no-op replacements
should appear from the qualifier fixer, the corresponding code
from QualifierAlignmentFixer::analyze() was moved to the top
reformat() function. Thus, is now done for **every** replacement
of every formatting pass. If no-op replacements are a problem
for the qualifier fixer, then it seems to be a good idea to
filter them out always.

See
https://github.com/llvm/llvm-project/issues/57117#issuecomment-1546716934
for some more details.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D153228
2023-07-03 11:54:33 +01:00
Owen Pan
20b4df1ed6 [clang-format][NFC] Clean up unit tests
This patch adds a verifyNoChange macro to verify code that won't
change after being formatted. (The code will not be messed up before
being formatted.) It then replaces EXPECT_EQ with verifyFormat
wherever applicable so that the code will be messed up before being
formatted. When the replacement fails the unit test, verifyFormat is
replaced with verifyNoChange.

Differential Revision: https://reviews.llvm.org/D153109
2023-06-16 16:03:19 -07:00
Colin Ogilvie
f3dcd3ad99 [clang-format] Correctly limit formatted ranges when specifying qualifier alignment
The qualifier alignment fixer appeared to ignore any ranges specified for limiting formatting.
This change ensures that it only formats affected lines to avoid unexpected changes.

Fixes #54888.

Differential Revision: https://reviews.llvm.org/D149643
2023-05-04 02:59:05 -07:00
Alexander Hederstaf
cd7ab4b5c1 [clang-format] Improve QualifierAlignment
Qualifiers were not moved for non-pointer non-simple types.
Add additional support for many special cases such as templates,
requires clauses, long qualified names.

Fixes https://github.com/llvm/llvm-project/issues/57154 and
https://github.com/llvm/llvm-project/issues/60898

Reviewed By: MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D144709
2023-03-27 15:18:29 +01:00
Manuel Klimek
f8d10d5ac9 [clang-format][NFC] Refactor formatting unit tests.
Pull out common base class for formatting unit tests, removing duplicate
code that accumulated over the years.

Pull out macro expansion test into its own test file.
2023-03-01 12:36:07 +00:00
Emilia Dreamer
393e197cd6
[clang-format] Don't move qualifiers past pointers-to-member
Previously, given a pointer-to-member type such as `Foo const Bar::*`,
clang-format would see the `const Bar::` part as a regular type name
with scope resolution operators, and with `QualifierAlignment: Right` it
would attempt to "fix" it, resulting in `Foo Bar::const *`, a syntax
error.

This patch no longer allows qualifiers to be moved across `::*`.

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

Reviewed By: owenpan, MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D144537
2023-02-25 12:19:20 +02:00
Micah Weston
4cafc3727b [clang-format] Add 'friend' to QualifierOrder
For cases of defining friend functions, qualifier ordering can
allow multiple positions for the 'friend' token.

Closes #59450.

Differential Revision: https://reviews.llvm.org/D139801
2022-12-22 02:02:09 -08:00
Marek Kurdej
d4d28f2ace [clang-format] Fix QualifierAlignment with global namespace qualified types.
Fixes https://github.com/llvm/llvm-project/issues/55610.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D126096
2022-05-26 15:02:33 +02:00
Owen Pan
492cb7bf91 [clang-format] Fix a crash in qualifier alignment
Related to #54513.
2022-04-01 17:30:59 -07:00
owenca
eee536dd31 [clang-format] Don't format qualifiers in PPDirective
Fixes #54513

Differential Revision: https://reviews.llvm.org/D122548
2022-03-28 14:28:14 -07:00
mydeveloperday
28bb040ded [clang-format] QualifierOrder does not reorder template arguments
https://github.com/llvm/llvm-project/issues/53981

Reorder the qualifiers inside the template argument. This should handle the simple cases of

```
<const T>
<T const>
```

But only by relaxing that single letter capital variables are not possible macros

Fixes: #53981

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D120710
2022-03-05 18:08:25 +00:00
Marek Kurdej
46f6c834d9 [clang-format] Fix QualifierOrder breaking the code with requires clause.
Fixes https://github.com/llvm/llvm-project/issues/53962.

Given the config:
```
BasedOnStyle: LLVM
QualifierAlignment: Custom
QualifierOrder: ['constexpr', 'type']
```

The code:
```
template <typename F>
  requires std::invocable<F>
constexpr constructor();
```
was incorrectly formatted to:
```
template <typename F>
  requires
constexpr std::invocable<F> constructor();
```
because we considered `std::invocable<F> constexpr` as a type, not recognising the requires clause.

This patch avoids moving the qualifier across the boundary of the requires clause (checking `ClosesRequiresClause`).

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D120309
2022-02-24 10:16:10 +01:00
Marek Kurdej
e329b5866f [clang-format] Honour "// clang-format off" when using QualifierOrder.
Fixes https://github.com/llvm/llvm-project/issues/53643.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D119218
2022-02-09 22:15:20 +01:00
mydeveloperday
031d3ece3f [clang-format] Fix a crash (assertion) in qualifier alignment when matching template closer is null
https://github.com/llvm/llvm-project/issues/53008

```
template <class Id> using A = quantity /**/<kind<Id>, 1>;
```

the presence of the comment between identifier and template opener seems to be causing the qualifier alignment to fail

Reviewed By: curdeius

Fixes: #53008

Differential Revision: https://reviews.llvm.org/D116726
2022-01-06 19:40:39 +00:00
mydeveloperday
c2ec5dd209 [clang-format] Left/Right alignment fixer can cause false positive replacements when they don't actually change anything
Earlier during the development of {D69764} I felt it was no longer necessary to
ensure we were not trying to change code which didn't need to change
and we felt this could be removed, however I'd like to bring this back for now
as I am seeing some false positives in terms of the "replacements"

What I see is the generation of a replacement which is a "No Op" on the original
code, I think this comes about because of the merging of replacements:

```
static const a;
->
const static a;
->
static const a;
```

The replacements don't really merge, in such a way as to identify when we have gone
back to the original

Also remove the Penalty as I'm not using it (and it became marked as set and no used,
I'd rather get rid of it if it means nothing)

I think we need to do this step for now, as many people use the --output-replacements-xml
to identify that the file "needs a clang-format"

The same can be seen with the -n or --dry-run option as this uses the replacements
to drive the error/warning output.

Reviewed By: HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D110392
2021-09-25 17:35:41 +01:00
Nemanja Ivanovic
76d845cb16 [clang-format] Fix unittest failures with -Werror
Commit a44ab1702539 added a unit test that fails to build with
-Werror which causes build bot breaks on bots that include that
option in their build. This patch just adds the necessary casts to
silence the warnings.
2021-09-23 18:24:39 -05:00
mydeveloperday
a44ab17025 [clang-format] Add Left/Right Const fixer capability
Developers these days seem to argue over east vs west const like they used to argue over tabs vs whitespace or the various bracing style. These previous arguments were mainly eliminated with tools like `clang-format` that allowed those rules to become part of your style guide. Anyone who has been using clang-format in a large team over the last couple of years knows that we don't have those religious arguments any more, and code reviews are more productive.

https://www.youtube.com/watch?v=fv--IKZFVO8
https://mariusbancila.ro/blog/2018/11/23/join-the-east-const-revolution/
https://www.youtube.com/watch?v=z6s6bacI424

The purpose of this revision is to try to do the same for the East/West const discussion. Move the debate into the style guide and leave it there!

In addition to the new `ConstStyle: Right` or `ConstStyle: Left` there is an additional command-line argument `--const-style=left/right` which would allow an individual developer to switch the source back and forth to their own style for editing, and back to the committed style before commit. (you could imagine an IDE might offer such a switch)

The revision works by implementing a separate pass of the Annotated lines much like the SortIncludes and then create replacements for constant type declarations.

Differential Revision: https://reviews.llvm.org/D69764
2021-09-23 20:00:33 +01:00