Commit Graph

2761 Commits

Author SHA1 Message Date
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 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