3240 Commits

Author SHA1 Message Date
Owen Pan
0468fa07f8
[clang-format] Don't align ctors and dtors with other functions (#67618)
Fixed #67604.
2023-09-29 02:09:36 -07:00
Jon Phillips
82001e0d01
[clang-format] Disable OuterScope lambda indentation behaviour for constructor initializers (#66755)
By default, OuterScope aligns lambdas to the beginning of the current
line. This makes sense for most types of statements within code blocks
but leads to unappealing and misleading indentation for lambdas within
constructor initializers.
2023-09-28 21:04:42 +02:00
Emilia Kond
3e87829a8a
[clang-format] Fix requires misannotation with comma (#65908)
clang-format uses a heuristic to determine if a requires() is either a
requires clause or requires expression, based on what is in the
parentheses. Part of this heuristic assumed that a requires clause can
never contain a comma, however this is not the case if said comma is in
the template argument of a type.

This patch allows commas to appear in a requires clause if an angle
bracket `<` has been opened.

Fixes https://github.com/llvm/llvm-project/issues/65904
2023-09-28 01:34:30 +03:00
Owen Pan
67b99fa8ba
[clang-format] Correctly annotate keyword operator function name (#66904)
Fixes #66890.
2023-09-27 13:12:55 -07:00
Owen Pan
cef9f40cd4
[clang-format] Split TT_AttributeParen (#67396)
Replaced TT_AttributeParen with TT_AttributeLParen and
TT_AttributeRParen.
2023-09-26 20:27:15 -07:00
Owen Pan
f2c97ffe46
[clang-format] Fix a bug in NamespaceEndCommentsFixer (#67422)
Fixed #67407.
2023-09-26 14:22:29 -07:00
Owen Pan
6e608dc44b
[clang-format] Correctly annotate return type of function pointer (#66893)
Fixes #66857.
2023-09-26 14:17:49 -07:00
Owen Pan
38dd67c8b3 [clang-format][NFC] Minor cleanup of the parser and annotator 2023-09-25 22:20:02 -07:00
Owen Pan
d3f8c88abe
[clang-format] Fix a bug in aligning trailing comments (#67221)
Fixes #67116.
2023-09-24 20:07:23 -07:00
Owen Pan
7db211bda7 [clang-format][NFC] Remove the unused separateDefinitionBlocks()
Also, fixed a formatting error introduced in 84e3fdfc6523.
2023-09-24 19:25:23 -07:00
Owen Pan
ff7e854022 [clang-format] Fix an assertion failure in Whitespace Manager 2023-09-24 17:53:05 -07:00
Owen Pan
d1c643a016
[clang-format][NFC] Clean up alignTrailingComments() (#67218) 2023-09-24 15:00:57 -07:00
Arthur Eubanks
e0388e0e06
[clang-format] Don't split "DPI"/"DPI-C" in Verilog imports (#66951)
The spec doesn't allow splitting these strings and we're seeing compile
issues with splitting it.

String splitting was enabled for Verilog in
https://reviews.llvm.org/D154093.
2023-09-21 11:21:20 -07:00
Owen Pan
a2046ca9af
[clang-format][NFC] Clean up signatures of some parser functions (#66569)
Removed TT_CompoundRequirementLBrace and parameters
CanContainBracedList and NextLBracesType.
2023-09-20 01:13:05 -07:00
sstwcw
00e794b4dd
[clang-format] Properly indent lines inside Verilog case structure (#65861)
When a statement following a case label had to be broken into multiple
lines, the continuation parts were not indented correctly.

Old:

```Verilog
case (data)
  16'd0:
    result = // break here
    10'b0111111111;
endcase
```

New:

```Verilog
case (data)
  16'd0:
    result = // break here
        10'b0111111111;
endcase
```

Verilog case labels and the following statements are on the same
unwrapped line due to the difficulty of identifying them. So there was a
rule in `getNewLineColumn` to add a level of indentation to the part
following the case label. However, in case the line had to be broken
again, the code at the end of the function would see that the line was
already broken with the continuation part indented, so it would not
indent it more. Now `State.FirstIndent` is changed as well for the part
following the case label, so the logic for determining when to add a
continuation indentation works.
2023-09-16 14:30:19 +00:00
Owen Pan
5ed94c6e3d
[clang-format] Fix a bug in ContinuationIndenter (#66354)
See https://reviews.llvm.org/D136154#3890747 for context.

Fixes part of #58592.
2023-09-15 14:20:41 -07:00
sstwcw
ae90f689a5
[clang-format] Disable string breaking in JS for now (#66372)
See the discussion

[here](https://github.com/llvm/llvm-project/pull/66168#issuecomment-1719038797).

The functionality is not mature enough.
2023-09-15 12:36:09 +00:00
sstwcw
cb479e7d7d [clang-format] More on unbreakable strings in TypeScript (#66321)
Now. string literals in lines beginning with `export type` will not be
broken.

The case was missed in 5db201fb75e6.  I don't know TypeScript.  And
merging GitHub pull requests seems to be a little too easy.  So it got
committed before the reviewers had a chance to find edge cases.
2023-09-14 12:41:21 +00:00
Owen Pan
0b2c88eea7
[clang-format] Fix a bug in annotating && followed by * or & (#65933)
Fixes #65877.
2023-09-13 23:57:34 -07:00
sstwcw
5db201fb75
[clang-format] Stop breaking unbreakable strings in JS (#66168)
Dictionary literal keys and strings in TypeScript type declarations can
not be broken.

The problem was pointed out by @alexfh and @e-kud here:

https://reviews.llvm.org/D154093#4644512
2023-09-13 17:04:04 +02:00
Owen Pan
b908123c3c [clang-format][NFC] Minor cleanup of token annotator and test cases 2023-09-11 00:18:57 -07:00
Björn Schäpers
84e3fdfc65
[clang-format] BreakBeforeNoexceptSpecifier option added (#65808)
It really bugs me that it breaks to
``` c++
...) noexcept(
    noexcept(condition)...
```

This is a fix for people like me.
2023-09-09 22:31:58 +02:00
Jon Phillips
210e7b3ca7 [clang-format] Improve line-breaking in LambdaBodyIndentation: OuterScope
Avoid unnecessarily aggressive line-breaking when using
"LambdaBodyIndentation: OuterScope" with argument bin-packing.

Differential Revision: https://reviews.llvm.org/D148131
2023-09-08 14:34:00 -07:00
Emilia Kond
e9ed1aa9cd
[clang-format] Correctly annotate designated initializer with PP if (#65409)
When encountering braces, such as those of a designated initializer,
clang-format scans ahead to see what is contained within the braces. If
it found a statement, like an if-statement of for-loop, it would deem
the braces as not an initializer, but as a block instead.

However, this heuristic incorrectly included a preprocessor `#if` line
as an if-statement. This manifested in strange results and discrepancies
between `#ifdef` and `#if defined`.

With this patch, `if` is now ignored if it is preceeded by `#`.

Fixes most of https://github.com/llvm/llvm-project/issues/56685
2023-09-07 22:23:05 +03:00
Björn Schäpers
4f7086ce33
[clang-format] Fix misannotation of && before noexcept (#65526)
When we are in an expression, it has to be a binary operator and not
pointer or reference.
2023-09-07 12:44:26 +02:00
Owen Pan
c22c0c4769 [clang-format][NFC] Change conjunction of isNot() with one !isOneOf() 2023-09-07 00:20:02 -07:00
Arkadiy Yudintsev
f465a482ca [clang-format] Fix segmentation fault when formatting nested namespaces
Fixing the clang-format crash with the segmentation fault error when
formatting code with nested namespaces.

Fixes #64701.

Differential Revision: https://reviews.llvm.org/D158363
2023-09-05 11:12:14 -07:00
sstwcw
ddc80637cc [clang-format] Break long string literals in C#, etc.
Now strings that are too long for one line in C#, Java, JavaScript, and
Verilog get broken into several lines.  C# and JavaScript interpolated
strings are not broken.

A new subclass BreakableStringLiteralUsingOperators is used to handle
the logic for adding plus signs and commas.  The updateAfterBroken
method was added because now parentheses or braces may be required after
the parentheses or commas are added.  In order to decide whether the
added plus sign should be unindented in the BreakableToken object, the
logic for it is taken out into a separate function
shouldUnindentNextOperator.

The logic for finding the continuation indentation when the option
AlignAfterOpenBracket is set to DontAlign is not implemented yet.  So in
that case the new line may have the wrong indentation, and the parts may
have the wrong length if the string needs to be broken more than once
because finding where to break the string depends on where the string
starts.

The preambles for the C# and Java unit tests are changed to the newer
style in order to allow the 3-argument verifyFormat macro.  Some cases
are changed from verifyFormat to verifyImcompleteFormat because those
use incomplete code and the new verifyFormat function checks that the
code is complete.

The line in the doc was changed to being indented by 4 spaces, that is,
the default continuation indentation.  It has always been the case.  It
was probably a mistake that the doc showed 2 spaces previously.

This commit was fist committed as 16ccba51072b.  The tests caused
assertion failures.  Then it was reverted in 547bce36132a.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D154093
2023-09-05 03:19:49 +00:00
Galen Elias
58c67e724f [clang-format] Fix AlignArrayOfStructures + Cpp11BracedListStyle=false
Currently AlignArrayOfStructures=Left is hard coding setting Spaces to
0 for the token following the initial opening brace, but not touching
Spaces for the subsequent lines, which leads to the array being
misaligned. Additionally, it's not adding a space before the trailing
} which is generally done when Cpp11BracedListStyle=false.

I'm not exactly sure why this function needs to override the Spaces as
it seems to generally already be set to either 0 or 1 according to
the other formatting settings, but I'm going with an explicit fix where
I just force the padding to 1 when Cpp11BracedListStyle=false.

AlignArrayOfStructures=Right doesn't have any alignment problems, but
isn't adding the expected padding around the braces either, so I'm
giving that the same treatment.

Fixes #57611.

Differential Revision: https://reviews.llvm.org/D158795
2023-08-31 14:21:19 -07:00
Owen Pan
77b2fc8ad9 [clang-format][NFC] Reformat and fix file mode
Fix file mode errors introduced in 2c9372e78d7c and format errors in
https://reviews.llvm.org/D155239.
2023-08-30 18:35:24 -07:00
Owen Pan
0863051208 Reland "[clang-format] Annotate constructor/destructor names"
(0e63f1aacc00 was reverted by 7590b7657004 due to a crash.)

Annotate constructor/destructor names as FunctionDeclarationName.

Fixes #63046.

Differential Revision: https://reviews.llvm.org/D157963
2023-08-29 13:14:52 -07:00
Björn Schäpers
ca0aa53365 [clang-format] Handle Template Arguments with AlignConsecutiveXX
This fixes https://github.com/llvm/llvm-project/issues/64928.

Differential-Revision: https://reviews.llvm.org/D158945
2023-08-29 21:59:44 +02:00
Kadir Cetinkaya
7590b76570
Revert "[clang-format] Annotate constructor/destructor names"
This reverts commit 0e63f1aacc0040e9a16fa2fab15a140b1686e2ab.

clang-format started to crash with contents like:
a.h:
```
```
$ clang-format a.h
```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ../llvm/build/bin/clang-format a.h
 #0 0x0000560b689fe177 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Unix/Signals.inc:723:13
 #1 0x0000560b689fbfbe llvm::sys::RunSignalHandlers() /usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Signals.cpp:106:18
 #2 0x0000560b689feaca SignalHandler(int) /usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Unix/Signals.inc:413:1
 #3 0x00007f030405a540 (/lib/x86_64-linux-gnu/libc.so.6+0x3c540)
 #4 0x0000560b68a9a980 is /usr/local/google/home/kadircet/repos/llvm/clang/include/clang/Lex/Token.h:98:44
 #5 0x0000560b68a9a980 is /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/FormatToken.h:562:51
 #6 0x0000560b68a9a980 startsSequenceInternal<clang::tok::TokenKind, clang::tok::TokenKind> /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/FormatToken.h:831:9
 #7 0x0000560b68a9a980 startsSequence<clang::tok::TokenKind, clang::tok::TokenKind> /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/FormatToken.h:600:12
 #8 0x0000560b68a9a980 getFunctionName /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/TokenAnnotator.cpp:3131:17
 #9 0x0000560b68a9a980 clang::format::TokenAnnotator::annotate(clang::format::AnnotatedLine&) /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/TokenAnnotator.cpp:3191:17
Segmentation fault
```
2023-08-28 10:31:07 +02:00
MyDeveloperDay
2c9372e78d
[clang-format] Rename qualifier file comments (#64354)
* [clang-format] rename the file comments to match the file name

* [clang-format] rename the file comments to match the file name

* Remove extraneous space

* [clang-format] NFC remove EOF

---------

Co-authored-by: paul_hoad <paul_hoad@amat.com>
2023-08-26 16:48:52 +01:00
Owen Pan
0e63f1aacc [clang-format] Annotate constructor/destructor names
Annotate constructor/destructor names as FunctionDeclarationName.

Fixes #63046.

Differential Revision: https://reviews.llvm.org/D157963
2023-08-24 01:32:52 -07:00
Owen Pan
91c4db0061 [clang-format][NFC] Replace !is() with isNot()
Differential Revision: https://reviews.llvm.org/D158571
2023-08-24 01:27:24 -07:00
David Spickett
547bce3613 Revert "[clang-format] Break long string literals in C#, etc."
This reverts commit 16ccba51072bbc5ff4c66f91f939163dc91e5d96.

This is failing across Linaro's bots e.g.:
https://lab.llvm.org/buildbot/#/builders/188/builds/34393
2023-08-24 08:15:17 +00:00
Björn Schäpers
6e77ba6c1e [clang-format] Fix weird handling of AfterColon
Fixes #64895.

Differential Revision: https://reviews.llvm.org/D158505
2023-08-24 08:34:22 +02:00
sstwcw
16ccba5107 [clang-format] Break long string literals in C#, etc.
Now strings that are too long for one line in C#, Java, JavaScript, and
Verilog get broken into several lines.  C# and JavaScript interpolated
strings are not broken.

A new subclass BreakableStringLiteralUsingOperators is used to handle
the logic for adding plus signs and commas.  The updateAfterBroken
method was added because now parentheses or braces may be required after
the parentheses or commas are added.  In order to decide whether the
added plus sign should be unindented in the BreakableToken object, the
logic for it is taken out into a separate function
shouldUnindentNextOperator.

The logic for finding the continuation indentation when the option
AlignAfterOpenBracket is set to DontAlign is not implemented yet.  So in
that case the new line may have the wrong indentation, and the parts may
have the wrong length if the string needs to be broken more than once
because finding where to break the string depends on where the string
starts.

The preambles for the C# and Java unit tests are changed to the newer
style in order to allow the 3-argument verifyFormat macro.  Some cases
are changed from verifyFormat to verifyImcompleteFormat because those
use incomplete code and the new verifyFormat function checks that the
code is complete.

The line in the doc was changed to being indented by 4 spaces, that is,
the default continuation indentation.  It has always been the case.  It
was probably a mistake that the doc showed 2 spaces previously.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D154093
2023-08-24 03:16:31 +00:00
Jon Phillips
d06b923915 [clang-format] Fix a bug that wraps before function arguments
Fixes a long-standing bug that erroneously placed function arguments on a
new line despite all arguments being able to fit on the same line.

The original diff that introduced the bug implemented behaviour that pushed
the first argument to a function onto a new line under certain circumstances
relating passing lambdas as arguments.

This behaviour was implemented in TokenAnnotator::mustBreakBefore() which
meant the code lacked the necessary context to figure out whether subsequent
arguments might be able to all fit on one line. As such, I've moved the
implementation to ContinuationIndenter and, instead of forcing a line break
at the first argument in all cases, we now allow the OptimizingLineFormatter
to consider placing the first argument on the same line as the function call
but don't allow further line breaks in this case.

The end result is that either the first argument must go on a new line (as
before) or all arguments must be put on the current line.

Closes #44486.

Differential Revision: https://reviews.llvm.org/D156259
2023-08-23 11:18:23 -07:00
Owen Pan
07181e289e [clang-format][NFC] Simplify getFirstNonComment() in the annotator
Differential Revision: https://reviews.llvm.org/D158104
2023-08-18 13:23:47 -07:00
Owen Pan
e3a79503a3 [clang-format] Exclude kw_decltype in RemoveParentheses
From https://en.cppreference.com/w/cpp/language/decltype:
Note that if the name of an object is parenthesized, it is treated as an
ordinary lvalue expression, thus decltype(x) and decltype((x)) are often
different types.

Fixes #64786.

Differential Revision: https://reviews.llvm.org/D158155
2023-08-17 16:04:44 -07:00
Owen Pan
063c42e919 [clang-format] Handle NamespaceMacro string arg for FixNamespaceComments
Fixes #63795.

Differential Revision: https://reviews.llvm.org/D157568
2023-08-16 10:45:54 -07:00
Owen Pan
2b8542ce8e [clang-format] Correctly count annoated lines of a namespace body
Fixes #63882.

Differential Revision: https://reviews.llvm.org/D157244
2023-08-10 15:55:01 -07:00
Owen Pan
a0b1c235af [clang-format] Currectly handle PCIS_CurrentLine with no column limit
Fixes #63519.

Differential Revision: https://reviews.llvm.org/D157179
2023-08-06 12:14:23 -07:00
Owen Pan
58a71c66db [clang-format] Handle "// clang-format on" for SeparateDefinitionBlocks
Fixes 63393.

Differential Revision: https://reviews.llvm.org/D156971
2023-08-05 12:31:27 -07:00
Owen Pan
70d7ea0ceb [clang-format] Handle goto labels preceded by C++11 attributes
Fixes #64229.

Differential Revision: https://reviews.llvm.org/D156655
2023-08-02 22:48:48 -07:00
Björn Schäpers
f274ffc303 [clang-format][NFC] Resort IO Mapping 2023-08-02 11:50:14 +02:00
Björn Schäpers
2c3b12b541 [clang-format][NFCish] Obey debug settings
Differential Revision: https://reviews.llvm.org/D156228
2023-08-02 11:50:14 +02:00
Björn Schäpers
77a38f43b1 [clang-format] Supress aligning of trailing namespace comments
Fixes https://github.com/llvm/llvm-project/issues/57504.

Differential Revision: https://reviews.llvm.org/D138263
2023-08-02 11:50:14 +02:00