Commit Graph

2859 Commits

Author SHA1 Message Date
Marek Kurdej 0104f5efed [clang-format] Mark FormatToken::getNextNonComment() nodiscard. 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 6c7e6fc7b6 [clang-format] Do not remove required spaces when aligning tokens.
Fixes https://github.com/llvm/llvm-project/issues/44292.
Fixes https://github.com/llvm/llvm-project/issues/45874.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D119419
2022-02-10 19:15:27 +01:00
Marek Kurdej 4efde1e554 [clang-format] Move FormatToken::opensBlockOrBlockTypeList to source file. NFC. 2022-02-10 10:51:03 +01:00
Marek Kurdej a7b5e5b413 [clang-format] Fix formatting of macro definitions with a leading comment.
Fixes https://github.com/llvm/llvm-project/issues/43206.

Reviewed By: HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D118924
2022-02-09 22:39:59 +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 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
Marek Kurdej 06e42590a5 [clang-format] Comment unused parameters. NFC. 2022-02-08 09:33:55 +01:00
Marek Kurdej 7d6397348e [clang-format] Fix typo. NFC. 2022-02-08 09:33:54 +01:00
ksyx a70549ae43 [clang-format] Fix DefSeparator empty line issues
- Add or remove empty lines surrounding union blocks.
- Fixes https://github.com/llvm/llvm-project/issues/53229, in which
  keywords like class and struct in a line ending with left brace or
  whose next line is left brace only, will be falsely recognized as
  definition line, causing extra empty lines inserted surrounding blocks
  with no need to be formatted.

Reviewed By: MyDeveloperDay, curdeius, HazardyKnusperkeks, owenpan
Differential Revision: https://reviews.llvm.org/D119067
2022-02-07 14:23:21 +00:00
Owen Pan 35f7dd601d [clang-format][NFC] Fix a bug in setting type FunctionLBrace
The l_brace token in a macro definition should not be set to
TT_FunctionLBrace.

This patch could have fixed #42087.

Differential Revision: https://reviews.llvm.org/D118969
2022-02-04 11:36:30 -08:00
Sam McCall acc3ce945c [Format] Don't derive pointers right based on space before method ref-qualifiers
The second space in `void foo() &` is always produced by clang-format,
and isn't evidence of any particular style.

Before this patch, it was considered evidence of PAS_Right, because
there is a space before a pointerlike ampersand.

This caused the following code to have "unstable" pointer alignment:
  void a() &;
  void b() &;
  int *x;
PAS_Left, Derive=false would produce 'int* x' with other lines unchanged.
But subsequent formatting with Derive=true would produce 'int *x' again.

Differential Revision: https://reviews.llvm.org/D118921
2022-02-04 12:13:58 +01:00
Björn Schäpers 3d0b619261 [clang-format][NFC] Code Tidies in UnwrappedLineFormatter
* Give I[1] and I[-1] a name:
  - Easier to understand
  - Easier to debug (since you don't go through operator[] everytime)
* TheLine->First != TheLine->Last follows since last is a l brace and
  first isn't.
* Factor the check for is(tok::l_brace) out.
* Drop else after return.

Differential Revision: https://reviews.llvm.org/D115060
2022-02-03 22:55:27 +01:00
ksyx 88e4e6be16 [clang-format] Use wider comment prefix space rule
This commit changes the condition of requiring comment to start with
alphanumeric characters to make no change only for a certain set of
characters, currently horizontal whitespace and punctuation characters,
to support wider set of leading characters unrelated to documentation
generation directives.

Reviewed By: HazardyKnusperkeks
Differential Revision: https://reviews.llvm.org/D118869
2022-02-03 21:49:10 +00:00
mydeveloperday 23fc20e06c [clang-format] regression from clang-format v13
https://github.com/llvm/llvm-project/issues/53567

The following source

```
namespace A {

template <int N> struct Foo<char[N]> {
  void foo() { std::cout << "Bar"; }
}; // namespace A
```

is incorrectly formatted as:

```
namespace A {

template <int N> struct Foo<char[N]>{void foo(){std::cout << "Bar";
}
}
; // namespace A
```

This looks to be caused by 5c2e7c9ca0

Reviewed By: curdeius

Differential Revision: https://reviews.llvm.org/D118911
2022-02-03 18:37:43 +00:00
Marek Kurdej ca0d97072e [clang-format] Avoid merging macro definitions.
Fixes https://github.com/llvm/llvm-project/issues/42087.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D118879
2022-02-03 18:54:46 +01:00
Marek Kurdej 529aa4b011 [clang-format] Avoid adding space after the name of a function-like macro when the name is a keyword.
Fixes https://github.com/llvm/llvm-project/issues/31086.

Before the code:
```
#define if(x)
```

was erroneously formatted to:
```
#define if (x)
```

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D118844
2022-02-03 18:45:51 +01:00
Sven van Haastregt 9694332b81 [clang-format] Add missing newline in -style help 2022-02-03 12:12:29 +00:00
Owen Pan eaef54f213 [clang-format] Revert a feature in RemoveBracesLLVM
Revert the handling of a single-statement block that gets wrapped.

See issue #53543.

Differential Revision: https://reviews.llvm.org/D118873
2022-02-03 02:56:09 -08:00
Marek Kurdej 768a6192df [clang-format] Reserve vectors when the number of items is known beforehand. NFC. 2022-02-03 10:38:23 +01:00
Marek Kurdej 7cc3e02042 [clang-format] Use back() instead of rbegin(). NFC. 2022-02-03 10:07:02 +01:00
Marek Kurdej bb1b53da6e [clang-format] Remove unnecessary non-null check and assert instead. NFC.
After a non-eof token, there is at least an eof token.
2022-02-03 09:50:36 +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 b3af2ef963 [clang-format] Factor out loop variable. NFC.
* Break on the size of the used variable Content instead of Lines (even though both should have the same size).
2022-02-02 14:36:00 +01:00
Marek Kurdej 10243d0dfd [clang-format] Simplify use of StringRef::substr(). NFC. 2022-02-02 14:36:00 +01:00
Marek Kurdej 574ad2a846 [clang-format] Use prefix operator--. NFC. 2022-02-02 14:01:48 +01:00
Marek Kurdej 61f09bcf11 [clang-format] Use llvm::seq instead of std::iota. NFC. 2022-02-02 14:00:50 +01:00
Marek Kurdej bc40b76b5b [clang-format] Correctly parse C99 digraphs: "<:", ":>", "<%", "%>", "%:", "%:%:".
Fixes https://github.com/llvm/llvm-project/issues/31592.

This commits enables lexing of digraphs in C++11 and onwards.
Enabling them in C++03 is error-prone, as it would unconditionally treat sequences like "<:" as digraphs, even if they are followed by a single colon, e.g. "<::" would be treated as "[:" instead of "<" followed by "::". Lexing in C++11 doesn't have this problem as it looks ahead the following token.
The relevant excerpt from Lexer::LexTokenInternal:
```
        // C++0x [lex.pptoken]p3:
        //  Otherwise, if the next three characters are <:: and the subsequent
        //  character is neither : nor >, the < is treated as a preprocessor
        //  token by itself and not as the first character of the alternative
        //  token <:.
```

Also, note that both clang and gcc turn on digraphs by default (-fdigraphs), so clang-format should match this behaviour.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D118706
2022-02-02 10:25:24 +01:00
Marek Kurdej af8f1dbb43 [clang-format] Use std::iota and reserve when sorting Java imports. NFC.
This way we have at most 1 allocation even if the number of includes is greater than the on-stack size of the small vector.
2022-02-01 14:29:31 +01:00
Marek Kurdej e75a3428a9 [clang-format] Use std::iota and reserve. NFC.
This way we have at most 1 allocation even if the number of includes is greater than the on-stack size of the small vector.
2022-02-01 14:24:01 +01:00
Marek Kurdej 34b4f00686 [clang-format] De-pessimize appending newlines. NFC.
* Avoid repeatedly calling std::string::append(char) in a loop.
* Reserve before calling std::string::append(const char *) in a loop.
2022-02-01 14:10:48 +01:00
Marek Kurdej 545317cb8e [clang-format] Use ranged for loops. NFC. 2022-02-01 14:10:48 +01:00
Marek Kurdej fd33cca762 [clang-format] Fix AlignConsecutiveAssignments breaking lambda formatting.
Fixes https://github.com/llvm/llvm-project/issues/52772.

This patch fixes the formatting of the code:
```
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = g([] {
  return;
});
```
which should be left as is, but before this patch was formatted to:
```
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = g([] {
  return;
                    });
```

Reviewed By: MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D115972
2022-02-01 09:17:59 +01:00
Marek Kurdej 95bf0a9ebd [clang-format] Don't break block comments when sorting includes.
Fixes https://github.com/llvm/llvm-project/issues/34626.

Before, the include sorter would break the code:
```
#include <stdio.h>
#include <stdint.h> /* long
                       comment */
```
and change it into:
```
#include <stdint.h> /* long
#include <stdio.h>
                       comment */
```

This commit handles only the most basic case of a single block comment on an include line, but does not try to handle all the possible edge cases with multiple comments.

Reviewed By: HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D118627
2022-02-01 08:51:10 +01:00
Philip Sigillito d1aed486ef [clang-format] Handle C variables with name that matches c++ access specifier
Reviewed By: MyDeveloperDay, curdeius, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D117416
2022-01-30 20:56:50 +01:00
Marek Kurdej 64df51624f [clang-format] Fix misaligned trailing comments in the presence of an empty block comment.
Fixes https://github.com/llvm/llvm-project/issues/53441.

Expected code:
```
/**/   //
int a; //
```

was before misformatted to:
```
/**/     //
int a; //
```

Because the "remaining length" (after the starting `/*`) of an empty block comment `/**/` was computed to be 0 instead of 2.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D118475
2022-01-28 22:28:48 +01:00
Martin Probst c267292515 clang-format: [JS] fix uninitialized memory.
SortJavaScriptImports attempts to set its currently parsed token to an
invalid token when it reaches the end of the line. However in doing so,
it used a `FormatToken`, which contains a `Token Tok`. `Token` does not
have a constructor, so its fields start out as uninitialized memory.

`Token::startToken()` initializes all fields. Calling it in
`JavaScriptImportSorter`'s constructor thus fixes the problem.

Differential Revision: https://reviews.llvm.org/D118448
2022-01-28 12:37:45 +01:00
Marek Kurdej f4d5195d2f [clang-format] Move irrelevant code from getRangeLength to getRemainingLength. NFC. 2022-01-28 12:01:02 +01:00
Martin Probst 03c59765b3 clang-format: [JS] sort import aliases. Users can define aliases for long symbols using import aliases:
import X = A.B.C;

Previously, these were unhandled and would terminate import sorting.
With this change, aliases sort as their own group, coming last after all
other imports.

Aliases are not sorted within their group, as they may reference each
other, so order is significant.

This reverts commit f750c3d95a. It fixes
the msan issue by not parsing past the end of the line when handling
import aliases.

Differential Revision: https://reviews.llvm.org/D118446
2022-01-28 11:51:28 +01:00
Marek Kurdej 249a21ab18 [clang-format] Remove useless npos parameter from substr. NFC. 2022-01-28 11:23:29 +01:00
Vitaly Buka f750c3d95a Revert "clang-format: [JS] sort import aliases."
Triggers MSAN report.

This reverts commit c6d5efb5d9.
2022-01-27 21:16:53 -08: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
Martin Probst c6d5efb5d9 clang-format: [JS] sort import aliases.
Users can define aliases for long symbols using import aliases:

    import X = A.B.C;

Previously, these were unhandled and would terminate import sorting.
With this change, aliases sort as their own group, coming last after all
other imports.

Aliases are not sorted within their group, as they may reference each
other, so order is significant.

Revision URI: https://reviews.llvm.org/D118361
2022-01-27 16:16:37 +01:00
Marek Kurdej 93948c5299 [clang-format] Correctly format lambdas with variadic template parameters.
Fixes https://github.com/llvm/llvm-project/issues/53405.

Reviewed By: MyDeveloperDay, owenpan

Differential Revision: https://reviews.llvm.org/D118220
2022-01-26 16:10:52 +01:00
Marek Kurdej 72e29caf03 [clang-format] Fix regression in parsing pointers to arrays.
Fixes https://github.com/llvm/llvm-project/issues/53293.

After commit 5c2e7c9, the code:
```
template <> struct S : Template<int (*)[]> {};
```
was misformatted as:
```
template <> struct S : Template<int (*)[]>{};
```

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D118106
2022-01-26 09:27:38 +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 81793bd276 [clang-format] Assert Line->First and State.NextToken->Previous. NFC.
Cf. scan-build reports:
* https://llvm.org/reports/scan-build/report-FormatToken.cpp-precomputeFormattingInfos-35-93e1e1.html#EndPath
* https://llvm.org/reports/scan-build/report-ContinuationIndenter.cpp-addTokenOnCurrentLine-15-dfdc6d.html#EndPath
2022-01-24 09:36:46 +01: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 ea2112ea15 [clang-format] Remove unused assignment. NFC.
Fixes scan-build reported warning: https://llvm.org/reports/scan-build/report-QualifierAlignmentFixer.cpp-analyzeRight-55-191910.html#EndPath.
2022-01-24 08:34:24 +01:00
Marek Kurdej 23a7bb541d [clang-format] Fix comment in spaceRequiredBefore. NFC. 2022-01-21 18:17:55 +01:00
Jan Svoboda 622354a522 [llvm][ADT] Implement `BitVector::{pop_,}back`
LLVM Programmer’s Manual strongly discourages the use of `std::vector<bool>` and suggests `llvm::BitVector` as a possible replacement.

Currently, some users of `std::vector<bool>` cannot switch to `llvm::BitVector` because it doesn't implement the `pop_back()` and `back()` functions.

To enable easy transition of `std::vector<bool>` users, this patch implements `llvm::BitVector::pop_back()` and `llvm::BitVector::back()`.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D117115
2022-01-21 14:50:53 +01:00
owenca c95afac89e [clang-format][NFC] Clean up tryMergeLessLess()
Differential Revision: https://reviews.llvm.org/D117759
2022-01-20 14:35:07 -08: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
Elliott Maguire 480a1fab72 [clang-format] Fix incorrect alignment of operator= overloads.
Fixes https://github.com/llvm/llvm-project/issues/31568.

Added a check for operator keyword tokens.

Reviewed By: MyDeveloperDay, curdeius, owenpan, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D117421
2022-01-19 10:18:47 +01:00
Marek Kurdej 1e512f022a [clang-format] Treat ForEachMacros as loops
TT_ForEachMacro should be considered in rules AllowShortBlocksOnASingleLine
and AllowShortLoopsOnASingleLine.
Fixes https://github.com/llvm/llvm-project/issues/45432.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D94955
2022-01-17 17:11:06 +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
Marek Kurdej 7af11989be [clang-format] Fix short functions being considered as inline inside an indented namespace.
Fixes https://github.com/llvm/llvm-project/issues/24784.

With config:
```
AllowShortFunctionsOnASingleLine: Inline
NamespaceIndentation: All
```

The code:
```
namespace Test
{
    void f()
    {
        return;
    }
}
```
was incorrectly formatted to:
```
namespace Test
{
    void f() { return; }
}
```

since the function `f` was considered being inside a class/struct/record.
That's because the check was simplistic and only checked for a non-zero indentation level of the line starting `f`.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D117142
2022-01-14 21:57:02 +01:00
Marek Kurdej 717cd16e85 [clang-format] Fix namespace end comments fixer with anonymous namespaces.
Previously, a strange trailing comment was produced:
```
namespace out { namespace {
}} // namespace out::
```
(mind the "out::").

Reviewed By: MyDeveloperDay, owenpan

Differential Revision: https://reviews.llvm.org/D117289
2022-01-14 11:41:14 +01:00
Armen Khachkinaev 47a9eb2117 [clang-format] Fix break being added to macro define with ColumnLimit: 0
Fix for #[[ https://github.com/llvm/llvm-project/issues/49164 | 49164 ]] issue.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, curdeius, owenpan

Differential Revision: https://reviews.llvm.org/D116859
2022-01-14 08:42:22 +01: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 6cbebfc7fb [clang-format] Fix comment. NFC. 2022-01-12 16:10:03 +01:00
ksyx ee25a327aa [clang-format] Fix SeparateDefinitionBlocks issues
Fixes https://github.com/llvm/llvm-project/issues/52976.

- Make no formatting for macros
- Attach comment with definition headers
- Make no change on use of empty lines at block start/end
- Fix misrecognition of keyword namespace

Differential Revision: https://reviews.llvm.org/D116663
Reviewed By: MyDeveloperDay, HazardyKnusperkeks, curdeius
2022-01-11 12:25:39 -05:00
mydeveloperday 5c2e7c9ca0 [clang-format] Ensure we can correctly parse lambda in the template argument list
https://github.com/llvm/llvm-project/issues/46505

The presence of a lambda in an argument template list ignored the [] as a lambda at all, this caused the contents of the <> to be incorrectly analyzed.

```
struct Y : X < [] {
  return 0;
} > {};
```
Fixes: #46505

Reviewed By: curdeius

Differential Revision: https://reviews.llvm.org/D116806
2022-01-10 08:29:35 +00:00
Kazu Hirata 40446663c7 [clang] Use true/false instead of 1/0 (NFC)
Identified with modernize-use-bool-literals.
2022-01-09 00:19:47 -08:00
Kazu Hirata d1b127b5b7 [clang] Remove unused forward declarations (NFC) 2022-01-08 11:56:40 -08:00
owenca d97025ad3a [clang-format][NFC] Fix a bug in getPreviousToken() in the parser
Differential Revision: https://reviews.llvm.org/D116318
2022-01-07 21:12:18 -08:00
Marek Kurdej 4681ae9353 [clang-format] Use range-for loops. NFC.
* Avoid if check on every element of the loop when printing symbols.
2022-01-07 16:06:11 +01:00
Marek Kurdej 359b4e6cdb [clang-format] Use prefix increment and decrement. NFC. 2022-01-07 11:19:53 +01:00
Marek Kurdej 91b9e6729c [clang-format] Fix `BraceWrapping: AfterFunction` affecting synchronized blocks in Java.
Fixes https://github.com/llvm/llvm-project/issues/32031.

Before this change, BraceWrapping: AfterFunction would affect synchronized blocks in Java, but they should be formatted w.r.t. BraceWrapping: AfterControlStatement.

Using the config:
```
BreakBeforeBraces: Custom
BraceWrapping:
  AfterControlStatement: false
  AfterFunction: true
```

would result in misformatted code like:
```
class Foo {
  void bar()
  {
    synchronized (this)
    {
      a();
      a();
    }
  }
}
```

instead of:
```
class Foo {
  void bar()
  {
    synchronized (this) {
      a();
      a();
    }
  }
}
```

Reviewed By: MyDeveloperDay, owenpan

Differential Revision: https://reviews.llvm.org/D116767
2022-01-07 10:06:49 +01:00
Marek Kurdej 01f355fe95 [clang-format] Use range-for loops. NFC.
Reviewed By: MyDeveloperDay, owenpan

Differential Revision: https://reviews.llvm.org/D116795
2022-01-07 10:01:09 +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 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
Jino Park 319971ca95 [clang-format][NFC] Fix typo in comment
Fix typo (leftt -> left)

Reviewed By: curdeius

Differential Revision: https://reviews.llvm.org/D116658
2022-01-05 17:33:16 +01:00
Marek Kurdej 5109737c92 [clang-format] Fix indentation for array variables with alignment of consecutive assignments and declarations.
Fixes https://github.com/llvm/llvm-project/issues/52914.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D116527
2022-01-05 13:52:45 +01:00
Marek Kurdej 46db030188 [clang-format] Simplify raw string regex. NFC.
Introduced in https://reviews.llvm.org/D115168.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D116647
2022-01-05 13:39:28 +01:00
Björn Schäpers 8f6af1d468 [clang-format][NFC] Put all state change into the for statement
Differential Revision: https://reviews.llvm.org/D116563
2022-01-05 12:31:36 +01: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
Björn Schäpers 35493b4560 [clang-format][NFC] Replace deque with vector
I think the deque was chosen because of a better push_front, but in
combination with llvm::reverse the push_back'ed vector should be the
better choice.

Differential Revision: https://reviews.llvm.org/D115064
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
Alexander Belyaev ca044f5369 Revert "[clang-format][NFC] Code Tidies in UnwrappedLineFormatter"
This reverts commit f014ab933f.

These tests are failing with asan:

clang/unittests:format_tests
clang/unittests:format_tests
clang-tools-extra/unittests:clang_move_tests
clang/unittests:tooling_tests
clang-tools-extra/test/clang-move:move-template-class.cpp.test
clang-tools-extra/test/clang-move:move-multiple-classes.cpp.test
clang-tools-extra/test/clang-move:move-used-helper-decls.cpp.test
clang-tools-extra/clangd/unittests:clangd_tests
clang/test/Format:access-modifiers.cpp.test
clang/unittests:rename_tests
clang/unittests:rename_tests
2022-01-04 12:10:52 +01:00
Marek Kurdej e2b6e21f19 [clang-format] Fix incorrect formatting of lambdas inside brace initialisation
Fixes https://github.com/llvm/llvm-project/issues/27146.
Fixes https://github.com/llvm/llvm-project/issues/52943.

Before:

```
namespace ns {

void foo() {
  std::variant<int, double> v;
  std::visit(overloaded{[](auto &&) -> int (*)[] { return nullptr; }}, v);
}

} // namespace ns

int break_me() {
  int x = 42;
  return int{[x = x]() {
    return x;
  }()};
}
```

got formatted as:
```
namespace ns {

void foo() {
  std::variant<int, double> v;
  std::visit(overloaded{[](auto &&) -> int (*)[] { return nullptr;
}
} // namespace ns
, v);
}

} // namespace ns

int break_me() {
  int x = 42;
  return int{[x = x](){return x;
}
()
}
;
}
```

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D116553
2022-01-04 08:28:12 +01:00
Björn Schäpers 1188f241ac Revert "[clang-format][NFC] Prefer pass by reference"
This reverts commit 25f637913f.

Differential Revision: https://reviews.llvm.org/D115061
2022-01-03 23:06:56 +01:00
Björn Schäpers d48d1f8ee8 [clang-format][NFC] Merge another two calls to isOneOf
Differential Revision: https://reviews.llvm.org/D115069
2022-01-03 23:06:55 +01:00
Björn Schäpers f014ab933f [clang-format][NFC] Code Tidies in UnwrappedLineFormatter
* Give I[1] and I[-1] a name:
  - Easier to understand
  - Easier to debug (since you don't go through operator[] everytime)
* TheLine->First != TheLine->Last follows since last is a l brace and
  first isn't.
* Factor the check for is(tok::l_brace) out.
* Drop else after return.

Differential Revision: https://reviews.llvm.org/D115060
2022-01-03 23:06:55 +01:00
ksyx 6f6f88ffda [clang-format] Style to separate definition blocks
This commit resolves GitHub issue #45895 (Bugzilla #46550), to
add or remove empty line between definition blocks including
namespaces, classes, structs, enums and functions.

Reviewed By: MyDeveloperDay, curdeius, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D116314
2022-01-03 15:47:39 -05: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
Michael Zimmermann 7972b2e422 [clang-format] respect AfterEnum for enums
There is some similar looking code in `TokenAnnotator.cpp` but given that I've
never worked on clang-format before I don't know what the purpose of that code
is and how it's related to `UnwrappedLineParser.cpp`.

Either way, it fixes clang-format with `BraceWrapping.AfterEnum=true` and
`AllowShortEnumsOnASingleLine=false` to behave like the documentation says.

Before this patch:
```
enum
{
  A,
  B
} myEnum;
```

After this patch:
```
enum {
  A,
  B
} myEnum;
```

According to the unittests which I had to modify this would change the LLVM
style. Please evaluate if you want to change the defaults or if you consider
the current style a bug.

Reviewed By: curdeius, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D106349
2022-01-03 20:01:10 +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
Gabriel Smith 694e6bcd52 [clang-format][NFC] Correct comment about checking merging of blocks
Reviewed By: HazardyKnusperkeks, MyDeveloperDay, owenpan

Differential Revision: https://reviews.llvm.org/D116189
2022-01-03 11:51:13 +01:00
Zhao Wei Liew b9e173fcd4 [clang-format] Add option to explicitly specify a config file
This diff extends the -style=file option to allow a config file to be specified explicitly. This is useful (for instance) when adding IDE commands to reformat code to a personal style.

Usage: `clang-format -style=file:<path/to/config/file> ...`

Reviewed By: HazardyKnusperkeks, curdeius, MyDeveloperDay, zwliew

Differential Revision: https://reviews.llvm.org/D72326
2022-01-03 11:43:25 +01:00
Zhao Wei Liew 0090cd4e7a [clang-format] Support inheriting from more than 1 parents in the fallback case
Currently, we are unable to inherit from a chain of parent configs where the outermost parent config has `BasedOnStyle: InheritParentConfig` set. This patch adds a test case for this scenario, and adds support for it.

To illustrate, suppose we have the following directory structure:
```
- e/
  |- .clang-format (BasedOnStyle: InheritParentConfig) <-- outermost config
  |- sub/
    |- .clang-format (BasedOnStyle: InheritParentConfig)
    |- sub/
      |- .clang-format (BasedOnStyle: InheritParentConfig)
      |- code.cpp
```
Now consider what happens when we run `clang-format --style=file /e/sub/sub/code.cpp`.

Without this patch, on a release build, only the innermost config will be applied. On a debug build, clang-format crashes due to an assertion failure.
With this patch, clang-format behaves as we'd expect, applying all 3 configs.

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D116371
2022-01-03 11:36:00 +01:00
Kazu Hirata 0542d15211 Remove redundant string initialization (NFC)
Identified with readability-redundant-string-init.
2021-12-26 09:39:26 -08:00
Kazu Hirata 2d303e6781 Remove redundant return and continue statements (NFC)
Identified with readability-redundant-control-flow.
2021-12-24 23:17:54 -08:00
Gabriel Smith 8ea64d5585 [clang-format] Fix short enums getting wrapped even when denied
Single-variant enums were still getting placed on a single line
even when AllowShortEnumsOnASingleLine was false. This fixes that
by checking that setting when looking to merge lines.

Differential Revision: https://reviews.llvm.org/D116188
2021-12-24 11:38:55 -08:00
Marek Kurdej f66d602c3f [clang-format] Fix wrong indentation after trailing requires clause.
Fixes https://github.com/llvm/llvm-project/issues/52834.

Before this patch, clang-format would wrongly parse top-level entities (e.g. namespaces) and format:
```
template<int I>
constexpr void foo requires(I == 42) {}
namespace ns {
void foo() {}
}  // namespace ns
```
into:
``````
template<int I>
constexpr void foo requires(I == 42) {}
namespace ns {
  void foo() {}
}  // namespace ns
```
with configuration:
```
NamespaceIndentation: None
````

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D116183
2021-12-23 08:22:12 +01: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
Marek Kurdej 07fe451305 [clang-format] Fix SplitEmptyRecord affecting SplitEmptyFunction.
Fixes https://github.com/llvm/llvm-project/issues/50051.

Given the style:
```
BraceWrapping
  AfterFunction: true
 SplitEmptyFunction: true
 SplitEmptyRecord: false
...
```

The code that should be like:
```
void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
       int bbbbbbbbbbbbbbbbbbbbbbbb)
{
}
```

gets the braces merged together:
```
void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
       int bbbbbbbbbbbbbbbbbbbbbbbb)
{}
```

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D116049
2021-12-21 16:54:19 +01: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
Marek Kurdej 960712ccc7 [clang-format] Fix wrong indentation of namespace identifiers after a concept declaration.
Before this patch, the code:
```
template <class T>
concept a_concept = X<>;
namespace B {
struct b_struct {};
} // namespace B
```
with config:
```
NamespaceIndentation: None
```

was wrongly indented inside namespace B, giving:
```
template <class T>
concept a_concept = X<>;
namespace B {
  struct b_struct {};
} // namespace B
```

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

Reviewed By: MyDeveloperDay, owenpan

Differential Revision: https://reviews.llvm.org/D116008
2021-12-20 09:13:32 +01:00
Krasimir Georgiev d96bf6ea46 Revert "[clang-format] Adjust braced list detection"
It appears that this regressed the formatting of initializer lists in some
cases, see comments on https://reviews.llvm.org/D114583. I'll follow-up
by adding regression tests for these.

This reverts commit c41b3b0fa0.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D116000
2021-12-19 16:04:42 +01:00
mydeveloperday 3362fa59ec [clang-format] extern with new line brace without indentation
https://github.com/llvm/llvm-project/issues/49804

Interaction between IndentExternBlock and AfterExternBlock means you cannot have AfterExternBlock = true and IndentExternBlock = NoIndent/Indent

This patch resolves that
```
BraceWrapping:
  AfterExternBlock: true
IndentExternBlock: AfterExternBlock
```
Fixes: #49804

Reviewed By: HazardyKnusperkeks, curdeius, owenpan

Differential Revision: https://reviews.llvm.org/D115879
2021-12-18 14:10:14 +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
Marek Kurdej 9cf4b7266b [clang-format] Refactor common handling of attributes. NFC.
Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D115968
2021-12-17 23:02:45 +01:00
Peter Stys 163c13fed9 [clang-format] Fix formatting of the code that follows C# Lambda Expressions
The alignment fix introduced by https://reviews.llvm.org/D104388 caused a regression whereby formatting of code that follows the lambda block is incorrect i.e. separate expressions are put on the same line.

Differential Revision: https://reviews.llvm.org/D115738
2021-12-17 10:42:15 -08: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 2b671c3fe0 [clang-format] add support for branch attribute macros
https://github.com/llvm/llvm-project/issues/49184

clang-format doesn't handle the use of AttributeMacros where `[[unlikely]]` / `[[likely]]` could be used in `if` statements

This was not covered in the original commit {{D80144}}

Fixes #49184

Reviewed By: curdeius, owenpan

Differential Revision: https://reviews.llvm.org/D115865
2021-12-16 20:36:25 +00:00
Marek Kurdej 27818f01fe [clang-format] Fix tabs when using BreakBeforeTernaryOperators=false.
Fixes https://github.com/llvm/llvm-project/issues/52724.

This is rather a workaround than a correct fix. To properly fix it, we'd need to find a better way to tell when not to decrease the StartOfTokenColumn.

Reviewed By: MyDeveloperDay, owenpan

Differential Revision: https://reviews.llvm.org/D115803
2021-12-16 09:28:00 +01:00
mydeveloperday a94aab12a4 [clang-format] put non-empty catch block on one line with AllowShortBlocksOnASingleLine: Empty
https://github.com/llvm/llvm-project/issues/52715

Fixes #52715

`AllowShortBlocksOnASingleLine` seems to never be checked for "Empty" as such if its used it will be considered "Always" as we only ever check `AllowShortBlocksOnASingleLine != Never`

This impacts C++ as well as C# hence the slightly duplicated test.

Reviewed By: curdeius, jbcoe

Differential Revision: https://reviews.llvm.org/D115794
2021-12-15 23:06:52 +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 6482383e50 [clang-format] FixNamespaceComments does not understand namespace aliases
https://github.com/llvm/llvm-project/issues/35876

Ensure a namespace alias doesn't get incorrectly identifier as a namespace

Reviewed By: HazardyKnusperkeks, curdeius, owenpan

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

Fixes: #35876
2021-12-14 14:53:04 +00:00
mydeveloperday 05bea533d1 [clang-format] [PR49298] Sort includes pass will sort inside raw strings
https://github.com/llvm/llvm-project/issues/48642

clang-format does not respect raw string literals when sorting includes

```
const char *RawStr = R"(
)";
```

Running clang-format over with SortIncludes enabled transforms this code to:

```
const char *RawStr = R"(
)";
```
The following code tries to minimize this impact during IncludeSorting, by treating R"( and )" as equivalent of // clang-format off/on

Reviewed By: HazardyKnusperkeks, curdeius

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

Fixes #48642
2021-12-12 17:00:43 +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
Tan S. B c41b3b0fa0 [clang-format] Adjust braced list detection
This avoids mishandling nested compound statements that are followed by another compound statement.

Fixes https://llvm.org/PR38314 and https://llvm.org/PR48305.

Differential Revision: https://reviews.llvm.org/D114583
2021-12-05 22:39:29 -08:00
Björn Schäpers 6e86789035 [clang-format][NFC] Use member directly
Instead of passing it as argument to the member function.

Differential Revision: https://reviews.llvm.org/D115072
2021-12-04 21:29:31 +01:00
Björn Schäpers 88fa4bfe1e [clang-format][NFC] Use range based for for fake l parens
Differential Revision: https://reviews.llvm.org/D115071
2021-12-04 21:29:30 +01:00
Björn Schäpers 4041f16bb4 [clang-format][NFC] Early return when nothing to do
Do not compute SkipFirstExtraIndent just to see that there are no fake l
parens.

Differential Revision: https://reviews.llvm.org/D115070
2021-12-04 21:29:30 +01:00
Björn Schäpers 8d1c85454d [clang-format][NFC] Move static variable in scope
Let only the JS/TS users pay for the initialistation.

Differential Revision: https://reviews.llvm.org/D115068
2021-12-04 21:29:30 +01: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
Björn Schäpers e7fdeda2c9 [clang-format][NFC] Rename variable so no shadowing happens
In the loop there is also a Node.

Differential Revision: https://reviews.llvm.org/D115063
2021-12-04 21:29:29 +01:00
Björn Schäpers 25f637913f [clang-format][NFC] Prefer pass by reference
Differential Revision: https://reviews.llvm.org/D115061
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
Manuel Klimek 4dcfae6a00 Fix a violated precondition in clang-format.
Make sure we do not try to change line comments that are non-regular, i.e. do
not start with "//" or "#". This can for example happen when "//" is
broken into two lines with an escaped newline.
2021-12-01 14:39:00 +01:00
Manuel Klimek d688b31628 Fix segfault in clang-format.
Fix bug where we'd read past the end of the tokens after merging _T
macro strings.
2021-12-01 11:57:41 +01:00
Manuel Klimek 5978842260 Fix clang-format bug when handling conflict markers.
Previously, clang-format would not correctly identify preprocessor
directives directly following a conflict marker, which would result in
violating the formatter's invariants.

The provided test fails in assert mode before this change.
2021-12-01 11:23:04 +01: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
Jesses Gott 813d486cbc [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks
Extend AllowShortBlocksOnASingleLine for else blocks. See https://bugs.llvm.org/show_bug.cgi?id=49722

Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D114320
2021-11-25 19:45:07 +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 72e4f4a2a1 [clang-format] [PR47936] AfterControlStatement: MultiLine breaks AllowShortFunctionsOnASingleLine
https://bugs.llvm.org/show_bug.cgi?id=47936

Using the MultiLine setting for BraceWrapping.AfterControlStatement appears to disable AllowShortFunctionsOnASingleLine, even in cases without any control statements

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D114521
2021-11-25 08:30:31 +00:00
Manuel Klimek 1b5a43ac3f Clean up clang-format tech debt.
Make all code go through FormatTokenSource instead of going around it, which
makes changes to TokenSource brittle.

Add LLVM_DEBUG in FormatTokenSource to be able to follow the token stream.
2021-11-24 12:58:35 +01: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 1cb3cfd932 [clang-format] [NFC] build clang-format with -Wall
When building clang-format with -Wall on Visual Studio 20119 we see the following, prevent this the only -Wall error
```
..FormatTokenLexer.cpp(45) : warning C4868: compiler may not enforce left-to-right evaluation order in braced initializer list
```
Reviewed By: HazardyKnusperkeks
Differential Revision: https://reviews.llvm.org/D113844
2021-11-23 10:43:27 +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
Zarko Todorovski d8e5a0c42b [clang][NFC] Inclusive terms: replace some uses of sanity in clang
Rewording of comments to avoid using `sanity test, sanity check`.

Reviewed By: aaron.ballman, Quuxplusone

Differential Revision: https://reviews.llvm.org/D114025
2021-11-19 14:58:35 -05:00
Manuel Klimek c2271926a4 Make clang-format fuzz through Lexing with asserts enabled.
Makes clang-format bail out if an in-memory source file with an
unsupported BOM is handed in instead of creating source locations that
are violating clang's assumptions.

In the future, we should add support to better transport error messages
like this through clang-format instead of printing to stderr and not
creating any changes.
2021-11-19 14:44:06 +01:00
owenca e852cc0d5a [clang-format][NFC] Add a default value to parseBlock()
Differential Revision: https://reviews.llvm.org/D114073
2021-11-17 13:48:53 -08:00
Kazu Hirata f1c159cc90 [Format, Sema] Use range-based for loops with llvm::reverse (NFC) 2021-11-17 08:52:35 -08:00