Commit Graph

1064 Commits

Author SHA1 Message Date
Björn Schäpers b786a4aefe [clang-format] Extend SpaceBeforeParens for requires
We can now configure the space between requires and the following paren,
seperate for clauses and expressions.

Differential Revision: https://reviews.llvm.org/D113369
2022-02-15 21:37:36 +01:00
Marek Kurdej e21db15be8 [clang-format] Honour PointerAlignment in statements with initializers.
Fixes https://github.com/llvm/llvm-project/issues/53843.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D119814
2022-02-15 18:06:32 +01:00
Marek Kurdej e01f624adb [clang-format] Fix PointerAlignment within lambdas in a multi-variable declaration statement.
Fixes https://github.com/llvm/llvm-project/issues/43115.

Also, handle while loops with initializers (C++20) the same way as for loops.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D119648
2022-02-14 09:41:24 +01:00
Marek Kurdej 25282bd6c4 [clang-format] Handle PointerAlignment in `if` and `switch` statements with initializers (C++17) the same way as in `for` loops.
Reviewed By: MyDeveloperDay, owenpan

Differential Revision: https://reviews.llvm.org/D119650
2022-02-13 21:36:58 +01:00
Marek Kurdej 7d5062c6ac [clang-format] Remove unnecessary parentheses in return statements. NFC. 2022-02-12 21:25:52 +01:00
Björn Schäpers 9aab0db13f [clang-format] Improve require and concept handling
- Added an option where to put the requires clauses.
- Renamed IndentRequires to IndentRequiresClause.
- Changed BreakBeforeConceptDeclaration from bool to an enum.

Fixes https://llvm.org/PR32165, and https://llvm.org/PR52401.

Differential Revision: https://reviews.llvm.org/D113319
2022-02-11 22:42:37 +01:00
Marek Kurdej 23f27850b1 [clang-format] Avoid multiple calls to FormatToken::getNextNonComment(). NFC. 2022-02-11 15:20:11 +01:00
Marek Kurdej 326cb51b14 [clang-format] Simplify conditions in spaceRequiredBetween. NFC. 2022-02-11 12:01:24 +01:00
Marek Kurdej a77c67f939 [clang-format] Fix formatting of the array form of delete.
Fixes https://github.com/llvm/llvm-project/issues/53576.

There was an inconsistency in formatting of delete expressions.

Before:
```
delete (void*)a;
delete[](void*) a;
```

After this patch:
```
delete (void*)a;
delete[] (void*)a;
```

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D119117
2022-02-09 22:36:13 +01:00
Marek Kurdej d079995dd0 [clang-format] Elide unnecessary braces. NFC. 2022-02-02 15:28:53 +01:00
Marek Kurdej 630c736047 [clang-format] Elide unnecessary braces. NFC. 2022-02-02 14:36:01 +01:00
Marek Kurdej 36622c4e1a [clang-format] Fix AllowShortFunctionsOnASingleLine: InlineOnly with wrapping after record.
Fixes https://github.com/llvm/llvm-project/issues/53430.

Initially, I had a quick and dirty approach, but it led to a myriad of special cases handling comments (that may add unwrapped lines).
So I added TT_RecordLBrace type annotations and it seems like a much nicer solution.
I think that in the future it will allow us to clean up some convoluted code that detects records.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D118337
2022-01-27 18:06:31 +01:00
Marek Kurdej 50999e82e8 [clang-format] Space between attribute closing parenthesis and qualified type colon.
Fixes https://github.com/llvm/llvm-project/issues/35711.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D117894
2022-01-24 18:09:20 +01:00
ksyx 5e5efd8a91 [clang-format] Fix SeparateDefinitionBlocks issues
- Fixes https://github.com/llvm/llvm-project/issues/53227 that wrongly
  indents multiline comments
- Fixes wrong detection of single-line opening braces when used along
  with those only opening scopes, causing crashes due to duplicated
  replacements on the same token:
    void foo()
    {
      {
        int x;
      }
    }
- Fixes wrong recognition of first line of definition when the line
  starts with block comment, causing crashes due to duplicated
  replacements on the same token for this leads toward skipping the line
  starting with inline block comment:
    /*
      Some descriptions about function
    */
    /*inline*/ void bar() {
    }
- Fixes wrong recognition of enum when used as a type name rather than
  starting definition block, causing crashes due to duplicated
  replacements on the same token since both actions for enum and for
  definition blocks were taken place:
    void foobar(const enum EnumType e) {
    }
- Change to use function keyword for JavaScript instead of comparing
  strings
- Resolves formatting conflict with options EmptyLineAfterAccessModifier
  and EmptyLineBeforeAccessModifier (prompts with --dry-run (-n) or
  --output-replacement-xml but no observable change)
- Recognize long (len>=5) uppercased name taking a single line as return
  type and fix the problem of adding newline below it, with adding new
  token type FunctionLikeOrFreestandingMacro and marking tokens in
  UnwrappedLineParser:
    void
    afunc(int x) {
      return;
    }
    TYPENAME
    func(int x, int y) {
      // ...
    }
- Remove redundant and repeated initialization
- Do no change to newlines before EOF

Reviewed By: MyDeveloperDay, curdeius, HazardyKnusperkeks
Differential Revision: https://reviews.llvm.org/D117520
2022-01-24 14:23:20 +00:00
Marek Kurdej 670a721de2 [clang-format] Assert Line->First. NFC.
Cf. scan-build reports:
* https://llvm.org/reports/scan-build/report-AffectedRangeManager.cpp-nonPPLineAffected-34-16c04b.html#EndPath
* https://llvm.org/reports/scan-build/report-SortJavaScriptImports.cpp-parseModuleReferences-34-96a7f8.html#EndPath
* https://llvm.org/reports/scan-build/report-TokenAnnotator.cpp-setCommentLineLevels-26-77bdba.html#EndPath
* https://llvm.org/reports/scan-build/report-AffectedRangeManager.cpp-nonPPLineAffected-31-714434.html#EndPath
* https://llvm.org/reports/scan-build/report-TokenAnnotator.cpp-setCommentLineLevels-16-bd39d0.html#EndPath
* https://llvm.org/reports/scan-build/report-UnwrappedLineFormatter.cpp-format-90-668b2d.html#EndPath
2022-01-24 08:54:55 +01:00
Marek Kurdej 23a7bb541d [clang-format] Fix comment in spaceRequiredBefore. NFC. 2022-01-21 18:17:55 +01:00
Marek Kurdej 82452be5cb [clang-format] Refactor: add FormatToken::hasWhitespaceBefore(). NFC.
This factors out a pattern that comes up from time to time.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D117769
2022-01-20 21:16:17 +01:00
Jino Park 560eb2277b [clang-format] Fix bug in parsing `operator<` with template
Fixes https://github.com/llvm/llvm-project/issues/44601.

This patch handles a bug when parsing a below example code :

```
template <class> class S;

template <class T> bool operator<(S<T> const &x, S<T> const &y) {
  return x.i < y.i;
}

template <class T> class S {
  int i = 42;
  friend bool operator< <>(S const &, S const &);
};

int main() { return S<int>{} < S<int>{}; }
```
which parse `< <>` as `<< >`, not `< <>` in terms of tokens as discussed in discord.

1. Add a condition in `tryMergeLessLess()` considering `operator` keyword and `>`
2. Force to leave a whitespace between `tok::less` and a template opener
3. Add unit test

Reviewed By: MyDeveloperDay, curdeius

Differential Revision: https://reviews.llvm.org/D117398
2022-01-20 08:59:04 +01:00
Cameron Mulhern 966f24e5a6 [clang-format] Add a BlockIndent option to AlignAfterOpenBracket
This style is similar to AlwaysBreak, but places closing brackets on new lines.

For example, if you have a multiline parameter list, clang-format currently only supports breaking per-parameter, but places the closing bracket on the line of the last parameter.

Function(
    param1,
    param2,
    param3);

A style supported by other code styling tools (e.g. rustfmt) is to allow the closing brackets to be placed on their own line, aiding the user in being able to quickly infer the bounds of the block of code.

Function(
    param1,
    param2,
    param3
);

For prior work on a similar feature, see: https://reviews.llvm.org/D33029.

Note: This currently only supports block indentation for closing parentheses.

Differential Revision: https://reviews.llvm.org/D109557
2022-01-17 09:03:23 +01:00
Owen Pan 533fbae8d8 [clang-format] Add experimental option to remove LLVM braces
See the style examples at:
https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements

Differential Revision: https://reviews.llvm.org/D116316
2022-01-14 15:10:17 -08:00
mydeveloperday 7ee4236789 [clang-format] clang-format eats space in front of attributes for operator delete
https://github.com/llvm/llvm-project/issues/27037

Sorry its taken so long to get to this issue! (got it before it hit its 6th birthday!)

```
void operator delete(void *foo)ATTRIB;
```

(void *foo) is incorrectly determined to be a C-Style Cast resulting in the space being removed after the ) and before the attrib, due to the detection of

```
delete (A* )a;
```

The following was previously unaffected

```
void operator new(void *foo) ATTRIB;
```

Fixes #27037

Reviewed By: curdeius, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D116920
2022-01-13 07:57:45 +00:00
Marek Kurdej 359b4e6cdb [clang-format] Use prefix increment and decrement. NFC. 2022-01-07 11:19:53 +01:00
mydeveloperday 49d311874e [clang-format] Missing space after cast in a macro
https://github.com/llvm/llvm-project/issues/52979

Though SpaceAfterCStyleCast is set to true, clang-format 13 does not add a space after (void *) here:

```
```

This patch addresses that

Fixes: #52979

Reviewed By: curdeius, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D116592
2022-01-06 08:07:03 +00:00
Björn Schäpers 1da96f7449 [clang-format][NFC] Right.Previous is Left
Use that name. Also remove the one check for its existence, that is
given.

Differential Revision: https://reviews.llvm.org/D116562
2022-01-05 12:31:36 +01:00
Björn Schäpers 2ab5d29f55 [clang-format][NFC] Use Prev instead of Current->Previous
Differential Revision: https://reviews.llvm.org/D116561
2022-01-05 12:31:35 +01:00
Björn Schäpers 29d8535e2b [clang-format][NFC] TokenAnnotator: Use range based for
Differential Revision: https://reviews.llvm.org/D116560
2022-01-05 12:31:35 +01:00
Björn Schäpers 918c977dc1 [clang-format][NFC] Early return in TokenAnnotator::next
Differential Revision: https://reviews.llvm.org/D116559
2022-01-05 12:31:35 +01:00
Björn Schäpers a1db435390 [clang-format][NFC] Don't pass member by argument
And then use the argument and member.

Differential Revision: https://reviews.llvm.org/D116558
2022-01-05 12:31:34 +01:00
Rajat Bajpai da6b0d0b76 [clang-format] Add an option to add a space between operator overloading and opening parentheses
This change adds an option AfterOverloadedOperator in SpaceBeforeParensOptions to add a space between overloaded operator and opening parentheses in clang-format.

Reviewed By: MyDeveloperDay, curdeius, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D116283
2022-01-04 17:23:23 +01:00
G. Pery cfe3180742 [clang-format] Add penalty for breaking after '('
My team has a vendetta against lines ending with an open parenthesis, thought it might be useful for others too 😊

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D116170
2022-01-03 21:06:34 +01:00
mydeveloperday cd2b050fa4 [clang-format] spacesRequiredBetween is not honouring clang-format off/on
https://github.com/llvm/llvm-project/issues/52881

It seems that clang-format off/on is not being honoured in regard to adding spaces.

My understanding of clang-format off/on is that it marks the token as finalized based on whether formatting is currently enabled or disabled.

This was causing a space to be added between the `<` and `<<`  in the Cuda kernel `foo<<<1, 1>>>();`

This if doesn't solve this actual issue but ensure that clang-format is at least honoured.

Reviewed By: curdeius, owenpan

Differential Revision: https://reviews.llvm.org/D116494
2022-01-03 11:19:02 +00:00
Owen Pan b9f6e09b85 [clang-format][NFC] Handle wrapping after => in mustBreakBefore()
Move the handling of brace wrapping after => from unwrapped line
parser to token annotator and clean up the parser.

Differential Revision: https://reviews.llvm.org/D115967
2021-12-21 16:42:52 -08:00
mydeveloperday 142e79b868 [clang-format] NFC use recently added Style.isJavaScript()
Improve the readability of these if(Style==FormatStyle::LK_JavsScript) clauses
2021-12-21 14:24:12 +00:00
mydeveloperday 6e28b86cc6 AlignConsecutiveDeclarations not working for 'const' keyword in JavsScript
https://github.com/llvm/llvm-project/issues/49846

Fixes #49846

AlignConsecutiveDeclarations  is not working for "let" and "const" in JavaScript

let letVariable     = 5;
const constVariable = 10;

Reviewed By: owenpan, HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D115990
2021-12-21 13:57:43 +00:00
mydeveloperday 936a67f089 [clang-format] Extra spaces surrounding arrow in templated member call in variable decl
https://github.com/llvm/llvm-project/issues/43196

Fixes #43196

-> is incorrectly interpreted as a TrailingReturnArrow if we've seen an auto

```
auto p = new A;
auto x = p -> foo<1>();
```

Reviewed By: curdeius

Differential Revision: https://reviews.llvm.org/D115903
2021-12-18 11:38:29 +00:00
mydeveloperday 62ead36547 [clang-format] Formatter does not handle c++11 string literal prefix with stringize #
https://github.com/llvm/llvm-project/issues/27740

Ensure
```
```
behave the same as
```
```

when formatted, ensure clang-format follows the conventions for `L` `u` `U` `u8`

https://docs.microsoft.com/en-us/cpp/cpp/string-and-character-literals-cpp?redirectedfrom=MSDN&view=msvc-170

Fixes #27740

Reviewed By: curdeius, owenpan

Differential Revision: https://reviews.llvm.org/D115938
2021-12-17 18:29:32 +00:00
mydeveloperday ebed0ca715 [clang-format] C# switch expression formatting differs from normal switch formatting
https://github.com/llvm/llvm-project/issues/52677

clang-format doesn't format C# switch expressions very well.

Start with this small use case and try and improve the output. I'll look for other examples to add as tests

Reviewed By: curdeius

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

Fixes  #52677
2021-12-15 19:47:29 +00:00
mydeveloperday 2a73a1ac57 [clang-format] PR48916 PointerAlignment not working when using C++20 init-statement in for loop
https://bugs.llvm.org/show_bug.cgi?id=48916

Left and Right Alignment inside a loop is misaligned.

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D115050
2021-12-09 10:37:02 +00:00
Björn Schäpers c25536e4fe [clang-format][NFC] Use range based for
That's much easier to read.

Differential Revision: https://reviews.llvm.org/D115067
2021-12-04 21:29:30 +01:00
Björn Schäpers 4483e9b527 [clang-format][NFC] Reorder conditions
Prefer to check the local variables first before dereferencing the
pointer.

Differential Revision: https://reviews.llvm.org/D115066
2021-12-04 21:29:29 +01:00
Björn Schäpers 5878ac7d2d [clang-format][NFC] Merge two calls of isOneOf
Differential Revision: https://reviews.llvm.org/D115065
2021-12-04 21:29:29 +01:00
mydeveloperday 57b95aed2a [clang-format] Add better support for co-routinues
Responding to a Discord call to help {D113977} and heavily inspired by the unlanded {D34225} add some support to help coroutinues from not being formatted from

```for co_await(auto elt : seq)```

to

```
for
co_await(auto elt : seq)
```

Because of the dominance of clang-format in the C++ community, I don't think we should make it the blocker that prevents users from embracing the newer parts of the standard because we butcher the layout of some of the new constucts.

Reviewed By: HazardyKnusperkeks, Quuxplusone, ChuanqiXu

Differential Revision: https://reviews.llvm.org/D114859
2021-12-02 08:06:43 +00:00
mydeveloperday 814aabae37 [clang-format] regressed default behavior for operator parentheses
{D110833} regressed behavior of spaces before parentheses for operators, this revision reverts that so that operators are handled as they were before.

I think in hindsight it was a mistake to try and consume operator behaviour in with the function behaviour, I think Operators can be considered a special style. Its seems the code is getting confused as to if this is a function declaration or definition.

I think latterly we can consider adding an operator parentheses specific custom option but this should have been explicitly called out as it can impact projects.

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D114696
2021-11-29 14:27:16 +00:00
mydeveloperday c2fe2b5a63 [clang-format] [C++20] [Module] clang-format couldn't recognize partitions
https://bugs.llvm.org/show_bug.cgi?id=52517

clang-format is butchering modules, this could easily become a barrier to entry for modules given clang-formats wide spread use.

Prevent the following from adding spaces around the  `:`  (cf was considering the ':' as an InheritanceColon)

Reviewed By: HazardyKnusperkeks, owenpan, ChuanqiXu

Differential Revision: https://reviews.llvm.org/D114151
2021-11-25 11:51:21 +00:00
mydeveloperday d44f2a6db2 [clang-format]NFC improve the comment to match the code
Missing from {D114519}
2021-11-25 11:11:30 +00:00
mydeveloperday c94667a810 [clang-format] [PR52595] clang-format does not recognize rvalue references to array
https://bugs.llvm.org/show_bug.cgi?id=52595

missing space between `T(&&)` but not between `T (&` due to && being incorrectly thought of as `UnaryOperator`  rather than `PointerOrReference`

```
int operator()(T (&)[N]) { return 0; }
int operator()(T(&&)[N]) { return 1; }
```

Existing Unit tests are changed because actually I think they are originally incorrect, and are inconsistent with the (&) cases that are 4 or 5 lines above them.

Reviewed By: curdeius

Differential Revision: https://reviews.llvm.org/D114519
2021-11-25 11:05:46 +00:00
mydeveloperday 93fc91610f [clang-format] NFC - recent changes caused clang-format to no longer be clang-formatted.
The following 2 commits caused files in clang-format to no longer be clang-formatted.

we would lose our "clean" status https://releases.llvm.org/13.0.0/tools/clang/docs/ClangFormattedStatus.html

c2271926a4  - Make clang-format fuzz through Lexing with asserts enabled (https://github.com/llvm/llvm-project/commit/c2271926a4fc )

84bf5e3286 - Fix various problems found by fuzzing. (https://github.com/llvm/llvm-project/commit/84bf5e328664)

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D114430
2021-11-24 09:45:32 +00:00
mydeveloperday e7cb3283c8 [clang-format] [PR52527] can join * with /* to form an outside of comment error C4138
https://bugs.llvm.org/show_bug.cgi?id=52527

The follow patch ensures there is always a space between * and /* to prevent transforming
```
void foo(* /* comment */)(int bar);
```
into
```
void foo(*/* comment */)(int bar);
```

Differential Revision: https://reviews.llvm.org/D114142
2021-11-23 10:36:06 +00:00
Manuel Klimek 84bf5e3286 Fix various problems found by fuzzing.
1. IndexTokenSource::getNextToken cannot return nullptr; some code was
still written assuming it can; make getNextToken more resilient against
incorrect input and fix its call-sites.

2. Change various asserts that can happen due to user provided input to
conditionals in the code.
2021-11-22 11:08:38 +01:00
Kazu Hirata f1c159cc90 [Format, Sema] Use range-based for loops with llvm::reverse (NFC) 2021-11-17 08:52:35 -08:00