Commit Graph

2859 Commits

Author SHA1 Message Date
owenca c9e7eec7bc [clang-format][NFC] Use isComment() in setCommentLineLevels()
Also replace an unnecessary check with assert() in the unwrapped
line parser.

Differential Revision: https://reviews.llvm.org/D124215
2022-04-22 09:21:28 -07:00
owenca 221c2b68dd [clang-format] Fix a crash on AllowShortFunctionsOnASingleLine
Fixes #55008.

Differential Revision: https://reviews.llvm.org/D124152
2022-04-21 14:56:30 -07:00
owenca 7343f768d1 [clang-format][NFC] Clean up code in token annotator
Differential Revision: https://reviews.llvm.org/D123741
2022-04-21 14:17:38 -07:00
Arthur Eubanks 19884d62c4 [clang-format] Don't skip PP lines if original line was a PP line when trying to merge lines
Fixes a crash introduced in D123737 where LastNonComment would be null.

Reviewed By: curdeius

Differential Revision: https://reviews.llvm.org/D124036
2022-04-20 08:42:30 -07:00
Konrad Kleine d46fa023ca [clang-format] SortIncludes should support "@import" lines in Objective-C
Fixes [[ https://github.com/llvm/llvm-project/issues/38995 | #38995 ]]

This is an attempt to modify the regular expression to identify
`@import` and `import` alongside the regular `#include`. The challenging
part was not to support `@` in addition to `#` but how to handle
everything that comes after the `include|import` keywords. Previously
everything that wasn't `"` or `<` was consumed. But as you can see in
this example from the issue #38995, there is no `"` or `<` following the
keyword:

```
@import Foundation;
```

I experimented with a lot of fancy and useful expressions in [this
online regex tool](https://regex101.com) only to find out that some
things are simply not supported by the regex implementation in LLVM.

 * For example the beginning `[\t\ ]*` should be replacable by the
   horizontal whitespace character `\h*` but this will break the
   `SortIncludesTest.LeadingWhitespace` test.

That's why I've chosen to come back to the basic building blocks.

The essential change in this patch is the change from this regular
expression:

```
^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">])
        ~                              ~~~~~~~~~~~~~~
        ^                              ^
        |                              |
        only support # prefix not @    |
                                       only support "" and <> as
delimiters
                                       no support for C++ modules and ;
                                       ending. Also this allows for ">
                                       or <" or "" or <> which all seems
                                       either off or wrong.
```

to this:

```
^[\t\ ]*[@#][\t\ ]*(import|include)([^"]*("[^"]+")|[^<]*(<[^>]+>)|[\t\
]*([^;]+;))
        ~~~~                        ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
        ^                                 ^           ^       ^       ^
        |                                 |           |       |       |
        Now support @ and #.            Clearly support "" and <> as
well as an
                                        include name without enclosing
characters.
                                        Allows for no mixture of "> or
<" or
                                        empty include names.

```

Here is how I've tested this patch:

```
ninja clang-Format
ninja FormatTests
./tools/clang/unittests/Format/FormatTests
--gtest_filter=SortIncludesTest*
```

And if that worked I doubled checked that nothing else broke by running
all format checks:

```
./tools/clang/unittests/Format/FormatTests
```

One side effect of this change is it should partially support
[C++20 Module](https://en.cppreference.com/w/cpp/language/modules)
`import` lines without the optional `export` in front. Adding
this can be a change on its own that shouldn't be too hard. I say
partially because the `@` or `#` are currently *NOT* optional in the
regular expression.

I see an opportunity to optimized the matching to exclude `@include` for
example. But eventually these should be caught by the compiler, so...

With my change, the matching group is not at a fixed position any
longer. I decided to
choose the last match (group) that is not empty.

Reviewed By: HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D121370
2022-04-20 07:03:35 +00:00
Arthur Eubanks f14ebe91c5 [clang-format] Skip preprocessor lines when finding the record lbrace
With D117142, we would now format

```
struct A {
#define A
  void f() { a(); }
#endif
};
```

into

```
struct A {
#ifdef A
  void f() {
    a();
  }
#endif
};
```

because we were looking for the record lbrace without skipping preprocess lines.

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

Reviewed By: curdeius, owenpan

Differential Revision: https://reviews.llvm.org/D123737
2022-04-14 09:31:15 -07:00
Marek Kurdej b58616c2cd [clang-format] Fix SeparateDefinitionBlocks breaking up function-try-block.
Fixes https://github.com/llvm/llvm-project/issues/54536.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D122468
2022-04-13 16:44:04 +02:00
owenca 0cb9c6ea83 [clang-format] Allow empty .clang-format file
Differential Revision: https://reviews.llvm.org/D123535
2022-04-12 21:20:54 -07:00
owenca 0cde8bdb0b Revert "[clang-format] Allow empty .clang-format file"
This reverts commit 4e814a6f2d.
2022-04-12 16:05:39 -07:00
owenca 4e814a6f2d [clang-format] Allow empty .clang-format file
Differential Revision: https://reviews.llvm.org/D123535
2022-04-12 15:45:22 -07:00
owenca c80eaa919f Revert "[clang-format] Allow empty .clang-format file"
This reverts commit 6eafda0ef0.
2022-04-12 14:28:02 -07:00
owenca 6eafda0ef0 [clang-format] Allow empty .clang-format file
Differential Revision: https://reviews.llvm.org/D123535
2022-04-12 13:54:12 -07:00
Owen Pan 492cb7bf91 [clang-format] Fix a crash in qualifier alignment
Related to #54513.
2022-04-01 17:30:59 -07:00
sstwcw f6740fe483 [clang-format] Indent import statements in JavaScript.
[clang-format] Indent import statements in JavaScript.

Take for example this piece of code found at
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import>.

```
for (const link of document.querySelectorAll("nav > a")) {
  link.addEventListener("click", e => {
    e.preventDefault();

    import('/modules/my-module.js')
        .then(module => {
          module.loadPageInto(main);
        })
        .catch(err => {
          main.textContent = err.message;
        });
  });
}
```

Previously the import line would be unindented, looking like this.

```
for (const link of document.querySelectorAll("nav > a")) {
  link.addEventListener("click", e => {
    e.preventDefault();

import('/modules/my-module.js')
        .then(module => {
          module.loadPageInto(main);
        })
        .catch(err => {
          main.textContent = err.message;
        });
  });
}
```

Actually we were going to fix this along with fixing Verilog import
statements.  But the patch got too big.

Reviewed By: MyDeveloperDay, curdeius

Differential Revision: https://reviews.llvm.org/D121906
2022-03-30 23:17:27 +00:00
owenca eee536dd31 [clang-format] Don't format qualifiers in PPDirective
Fixes #54513

Differential Revision: https://reviews.llvm.org/D122548
2022-03-28 14:28:14 -07:00
Krasimir Georgiev be5c3ca7fb Revert "[clang-format] Correctly recognize arrays in template parameter list."
This reverts commit 126b37a713.

Regressed some ObjC patterns, see comments on https://reviews.llvm.org/D121584.
2022-03-24 10:14:13 +01:00
Dávid Bolvanský 1c13bbdde6 [NFCI] Fix set-but-unused warning in UnwrappedLineParser.cpp 2022-03-24 08:13:29 +01:00
owenca f74413d163 [clang-format] Fix invalid code generation with comments in lambda
Fixes #51234 and #54496

Differential Revision: https://reviews.llvm.org/D122301
2022-03-23 19:40:24 -07:00
Marek Kurdej 4e88cb6825 [clang-format] Handle attributes before case label. Relanded.
Fixes https://github.com/llvm/llvm-project/issues/53110.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

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

Relanding as the original patch provoked an infinite loop in JavaScript/TypeScript.
A reproducer test case was added and the issue fixed.
2022-03-23 16:24:24 +01:00
Krasimir Georgiev eb35e0ecbe [clang-format] don't break up #-style comment sections
Follow-up from 36d13d3f8a; https://reviews.llvm.org/D121451.

Restore the old behavior in situations where we use # as comments and long strings of #'s for comment sections.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D122230
2022-03-22 15:29:02 +01:00
sstwcw 8c31b68f48 [clang-format] Use an enum for context types. NFC
We currently have all those fields in AnnotatingParser::Context.  They
are not inherited from the Context object for the parent scope.  They
are exclusive.  Now they are replaced with an enum.

`InCpp11AttributeSpecifier` and `InCSharpAttributeSpecifier` are not
handled like the rest in ContextType because they are not exclusive.

Reviewed By: curdeius, MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D121907
2022-03-21 21:58:17 +00:00
Jorge Gorbe Moya 5b81158675 Revert "[clang-format] Handle attributes before case label."
This reverts commit 596fa2d900.
2022-03-21 14:39:14 -07:00
Marek Kurdej 9dad527fc0 [clang-format] Use range-for loop with drop_end. NFC. 2022-03-21 10:05:06 +01:00
Marek Kurdej c59c2b6bd1 [clang-format] Refactor ShouldBreakBeforeBrace to use switch. NFC. 2022-03-18 15:16:01 +01:00
Marek Kurdej acc7a7f9a1 [clang-format] Use range-for loop. NFC. 2022-03-18 15:01:41 +01:00
Marek Kurdej b6baab673a [clang-format] Refactor BreakableBlockComment constructor. NFC. 2022-03-18 15:01:40 +01:00
Marek Kurdej dc142ea184 [clang-format] Correctly recognize binary operators in template arguments with parenthesized literals.
Fixes https://github.com/llvm/llvm-project/issues/24602.

Before, code like `foo<b & 1>` was formatted correctly but `foo<b & (1)>` wasn't.
This patch fixes this inconsistency.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D121846
2022-03-17 09:36:25 +01:00
Marek Kurdej 34ce42fe4d [clang-format] Reformat. NFC. 2022-03-17 09:27:31 +01:00
Owen Pan 7fb2d9f9b5 [clang-format] Fix crashes due to missing l_paren
Fixes #54384.

Differential Revision: https://reviews.llvm.org/D121682
2022-03-16 01:45:20 -07:00
Björn Schäpers 1db8112311 [clang-format] Fix crash with ObjC Blocks
Fixes https://github.com/llvm/llvm-project/issues/54367
Fixes https://github.com/llvm/llvm-project/issues/54368

Differential Revision: https://reviews.llvm.org/D121596
2022-03-15 21:41:57 +01:00
Marek Kurdej 3227aa3aa8 [clang-format] Correctly format variable templates.
Fixes https://github.com/llvm/llvm-project/issues/54257.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D121456
2022-03-15 13:16:56 +01:00
Marek Kurdej 126b37a713 [clang-format] Correctly recognize arrays in template parameter list.
Fixes https://github.com/llvm/llvm-project/issues/54245.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D121584
2022-03-15 11:33:13 +01:00
Owen Pan 0a0cc3c58a [clang-format] Don't unwrap lines preceded by line comments
Fixes #53495

Differential Revision: https://reviews.llvm.org/D121576
2022-03-14 19:16:29 -07:00
Marek Kurdej a6b2f50fb4 Revert "[clang-format] Correctly format variable templates."
This reverts commit a140b7104f.

It provoked the bug https://github.com/llvm/llvm-project/issues/54374.
2022-03-14 16:04:09 +01:00
Björn Schäpers 35abbf166d [clang-format] Fix crash on asm block with label
Fixes https://github.com/llvm/llvm-project/issues/54349

Differential Revision: https://reviews.llvm.org/D121559
2022-03-14 12:44:48 +01:00
Björn Schäpers b7494a1d72 [clang-format][NFC] Left renamed to OpeningBrace...
in TokenAnnotator::parseBrace. Left is misleading, because we have a
loop and Left does not move.

Also return early.

Differential Revision: https://reviews.llvm.org/D121558
2022-03-14 12:44:47 +01:00
Björn Schäpers acd17a2be8 [clang-format] Fix crash on invalid requires expression
Fixes https://github.com/llvm/llvm-project/issues/54350

Differential Revision: https://reviews.llvm.org/D121550
2022-03-14 12:44:46 +01:00
Björn Schäpers 2d8e907016 [clang-format][NFC] Rename Left to OpeningParen...
in TokenAnnotator::parseParens(). Left is misleading since we have a
loop and Left is not adjusted.

Differential Revision: https://reviews.llvm.org/D121557
2022-03-14 12:44:45 +01:00
Marek Kurdej 2507e0a257 [clang-format] Clean up UnwrappedLineParser::parseRecord. NFC. 2022-03-14 11:59:52 +01:00
sstwcw c24b3db45c [clang-format] Add option to align compound assignments like `+=`
Reviewed By: curdeius, HazardyKnusperkeks, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D119599
2022-03-14 04:41:40 +00:00
Marek Kurdej 0570af1758 [clang-format] Fix incorrect assertion on macro definitions with keyword class.
Fixes https://github.com/llvm/llvm-project/issues/54348.
2022-03-13 22:17:48 +01:00
Marek Kurdej a140b7104f [clang-format] Correctly format variable templates.
Fixes https://github.com/llvm/llvm-project/issues/54257.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D121456
2022-03-13 22:00:17 +01:00
Marek Kurdej 36d13d3f8a [clang-format] Add space to comments starting with '#'.
Fixes https://github.com/llvm/llvm-project/issues/35116.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D121451
2022-03-13 21:56:22 +01:00
Marek Kurdej 596fa2d900 [clang-format] Handle attributes before case label.
Fixes https://github.com/llvm/llvm-project/issues/53110.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D121450
2022-03-13 21:53:40 +01:00
Björn Schäpers 8b4d68bf65 [clang-format] Handle builtins in constraint expression
Fixes https://github.com/llvm/llvm-project/issues/54106

Differential Revision: https://reviews.llvm.org/D120774
2022-03-12 22:44:24 +01:00
mydeveloperday adfe58b09d [clang-format] Minimize the damage caused by AlignArrayOfStructures when working on non square arrays
I have lost count of the number of times this has been reported, but it fundamentally comes down to the fact that the "AlignArrayLeft/Right" function is fundamentally broken for non-square arrays.

As a result, a pointer can end up running off the end of the array structure, I've spent the last 2 weekends trying to rewrite this algorithm but I've struggled to get it aligned correctly.

This is an interim fix, that ignores all array that are non-square and leaves them alone. I think this can allow us to close out most of the crashes (if not all).

I think this can help reduce the number of bugs coming in that are duplicates.

https://github.com/llvm/llvm-project/issues/53748
https://github.com/llvm/llvm-project/issues/51767
https://github.com/llvm/llvm-project/issues/51277

Reviewed By: curdeius, HazardyKnusperkeks, feg208

Differential Revision: https://reviews.llvm.org/D121069
2022-03-12 17:22:31 +00:00
owenca 9f616a467f [clang-format][NFC] Group QualifierAlignment with other C++ passes
Also increases the initial size of Passes to 8 and move the
definition of BracesInserter up.

Differential Revision: https://reviews.llvm.org/D121434
2022-03-11 14:18:00 -08:00
Marek Kurdej b44eb207e9 [clang-format] Refactor condition in AnnotatingParser::modifyContext(). NFC. 2022-03-11 13:12:43 +01:00
Zequan Wu d54c4df314 [clang-format] Fix namespace format when the name is followed by a macro
Example:
```
$ cat a.cpp
namespace my_namespace::yeah API_AVAILABLE(macos(10.15)) {
void test() {}
}

$ clang-format a.cpp
namespace my_namespace::yeah API_AVAILABLE(macos(10.15)) {
void test() {}
}// namespace my_namespace::yeahAPI_AVAILABLE(macos(10.15))
```
After:
```
$ clang-format a.cpp
namespace my_namespace::yeah API_AVAILABLE(macos(10.15)) {
void test() {}
}// namespace my_namespace::yeah
```

Reviewed By: MyDeveloperDay, owenpan, curdeius

Differential Revision: https://reviews.llvm.org/D121269
2022-03-10 15:00:32 -08:00
owenca 0be56c8701 [clang-format][NFC] Group all C++ passes under isCpp()
Also removes a not very helpful comment.
2022-03-10 14:30:30 -08:00