Граф коммитов

32 Коммитов

Автор SHA1 Сообщение Дата
Martin Probst 112ff305be clang-format: fix a crash in comment wraps.
Summary:
Previously, clang-format would crash if it tried to wrap an overlong
single line comment, because two parts of the code inserted a break in
the same location.

    /** heregoesalongcommentwithnospace */

This wasn't previously noticed as it could only trigger for an overlong
single line comment that did have no breaking opportunities except for a
whitespace at the very beginning.

This also introduces a check for JavaScript to not ever wrap a comment
before an opening curly brace:

    /** @mods {donotbreakbeforethecurly} */

This is because some machinery parsing these tags sometimes supports
breaks before a possible `{`, but in some other cases does not.
Previously clang-format was careful never to wrap a line with certain
tags on it. The better solution is to specifically disable wrapping
before the problematic token: this allows wrapping and aligning comments
but still avoids the problem.

Reviewers: krasimir

Subscribers: cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@338706 91177308-0d34-0410-b5e6-96231b3b80d8
2018-08-02 11:52:08 +00:00
Krasimir Georgiev 323c6c5e52 [clang-format] Indent after breaking Javadoc annotated line
Summary:
This patch makes clang-format indent the subsequent lines created by breaking a
long javadoc annotated line.

Reviewers: mprobst

Reviewed By: mprobst

Subscribers: acoomans, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@338232 91177308-0d34-0410-b5e6-96231b3b80d8
2018-07-30 08:45:45 +00:00
Krasimir Georgiev 6fc954314a [clang-format] Fix crash while reflowing backslash in comments
Summary:
The added test case was currently crashing with an assertion:
```
krasimir@krasimir> cat test.cc                                                                                                                                                              ~
// How to run:
// bbbbb run \
// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \
// <log_file> -- --output_directory="<output_directory>"
krasimir@krasimir> ~/work/llvm-build/bin/clang-format test.cc                                                                                                                               ~
clang-format: /usr/local/google/home/krasimir/work/llvm/tools/clang/lib/Format/WhitespaceManager.cpp:117: void clang::format::WhitespaceManager::calculateLineBreakInformation(): Assertion `PreviousOriginalWhitespaceEndOffset <= OriginalWhitespaceStartOffset' failed.
```
The root cause was that BreakableToken was not considering the case of a reflow between an unescaped newline in a line comment.

Subscribers: cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@334527 91177308-0d34-0410-b5e6-96231b3b80d8
2018-06-12 19:33:15 +00:00
Nicola Zaghen ff0626ea3c [clang] Update uses of DEBUG macro to LLVM_DEBUG.
The DEBUG() macro is very generic so it might clash with other projects.
The renaming was done as follows:
- git grep -l 'DEBUG' | xargs sed -i 's/\bDEBUG\s\?(/LLVM_DEBUG(/g'
- git diff -U0 master | ../clang/tools/clang-format/clang-format-diff.py -i -p1 -style LLVM

Explicitly avoided changing the strings in the clang-format tests.

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



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@332350 91177308-0d34-0410-b5e6-96231b3b80d8
2018-05-15 13:30:56 +00:00
Mark Zeren 893efa76e2 [clang-format] In tests, expected code should be format-stable
Summary: Extend various verifyFormat helper functions to check that the
expected text is "stable". This provides some protection against bugs
where formatting results are ocilating between two forms, or continually
change in some other way.

Testing Done:

* Ran unit tests.

* Reproduced a known instability in preprocessor indentation which was
  caught by this new check.

Reviewers: krasimir

Subscribers: cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@329231 91177308-0d34-0410-b5e6-96231b3b80d8
2018-04-04 21:09:00 +00:00
Manuel Klimek b779bc4dd1 Fix bug where we wouldn't break columns over the limit.
Before, we would not break:
  int a = foo(/* trailing */);
when the end of /* trailing */ was exactly the column limit; the reason
is that block comments can have an unbreakable tail length - in this case
2, for the trailing ");"; we would unconditionally account that when
calculating the column state at the end of the token, but not correctly
add it into the remaining column length before, as we do for string
literals.
The fix is to correctly account the trailing unbreakable sequence length
into our formatting decisions for block comments. Line comments cannot
have a trailing unbreakable sequence, so no change is needed for them.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319642 91177308-0d34-0410-b5e6-96231b3b80d8
2017-12-04 08:53:16 +00:00
Manuel Klimek 64d42a2fb8 Restructure how we break tokens.
This fixes some bugs in the reflowing logic and splits out the concerns
of reflowing from BreakableToken.

Things to do after this patch:
- Refactor the breakProtrudingToken function possibly into a class, so we
  can split it up into methods that operate on the common state.
- Optimize whitespace compression when reflowing by using the next possible
  split point instead of the latest possible split point.
- Retry different strategies for reflowing (strictly staying below the
  column limit vs. allowing excess characters if possible).

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319314 91177308-0d34-0410-b5e6-96231b3b80d8
2017-11-29 14:29:43 +00:00
Manuel Klimek 91f4dd295a Implement more accurate penalty & trade-offs while breaking protruding tokens.
For each line that we break in a protruding token, compute whether the
penalty of breaking is actually larger than the penalty of the excess
characters. Only break if that is the case.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318515 91177308-0d34-0410-b5e6-96231b3b80d8
2017-11-17 11:17:15 +00:00
Manuel Klimek bf29fe4665 Refactor ContinuationIndenter's breakProtrudingToken logic.
Create more orthogonal pieces. The restructuring made it easy to try out
several alternatives to D33589, and while none of the alternatives
turned out to be the right solution, the underlying simplification of
the structure is helpful.

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318141 91177308-0d34-0410-b5e6-96231b3b80d8
2017-11-14 09:19:53 +00:00
Krasimir Georgiev 4fd3ff53bc [clang-format] Support python-style comments in text protos
Summary: This patch adds support for python-style comments in text protos.

Reviewers: djasper

Reviewed By: djasper

Subscribers: bkramer, cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@317886 91177308-0d34-0410-b5e6-96231b3b80d8
2017-11-10 12:50:09 +00:00
Krasimir Georgiev e58dc968a1 [clang-format] Break non-trailing comments, try 2
Summary:
This patch enables `BreakableToken` to manage the formatting of non-trailing
block comments. It is a refinement of https://reviews.llvm.org/D37007.
We discovered that the optimizer outsmarts us on cases where breaking the comment
costs considerably less than breaking after the comment. This patch addresses
this by ensuring that a newline is inserted between a block comment and the next
token.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@315893 91177308-0d34-0410-b5e6-96231b3b80d8
2017-10-16 09:08:53 +00:00
Krasimir Georgiev 1b8529eb2f [clang-format] Emit absolute splits before lines for comments, try 2
Summary:
This recommits https://reviews.llvm.org/D36956 with an update to the added test
case to not use raw string literals, since this makes gcc unhappy.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311672 91177308-0d34-0410-b5e6-96231b3b80d8
2017-08-24 16:41:10 +00:00
Krasimir Georgiev deada28d59 Revert "[clang-format] Break non-trailing block comments"
This reverts commit r311457. It reveals some dormant bugs in comment
reflowing, like breaking a single line jsdoc type annotation before a
parameter into multiple lines.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311641 91177308-0d34-0410-b5e6-96231b3b80d8
2017-08-24 08:55:07 +00:00
Krasimir Georgiev 4bf0c5df66 Revert "[clang-format] Emit absolute splits before lines for comments"
This reverts commit r311559, which added a test containing raw string
literals in macros, which chokes gcc:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55971

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311566 91177308-0d34-0410-b5e6-96231b3b80d8
2017-08-23 15:58:10 +00:00
Krasimir Georgiev d2a520ee9e [clang-format] Emit absolute splits before lines for comments
Summary:
This patch makes the splits emitted for the beginning of comment lines during
reformatting absolute. Previously, they were relative to the start of the
non-whitespace content of the line, which messes up further TailOffset
calculations in breakProtrudingToken. This fixes an assertion failure reported
in bug 34236: https://bugs.llvm.org/show_bug.cgi?id=34236.

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311559 91177308-0d34-0410-b5e6-96231b3b80d8
2017-08-23 15:16:47 +00:00
Krasimir Georgiev 4d26b24b68 [clang-format] Align trailing comments if ColumnLimit is 0
Summary:
ColumnLimit = 0 means no limit, so comment should always be aligned if requested. This was broken with

  https://llvm.org/svn/llvm-project/cfe/trunk@304687

introduced via

  https://reviews.llvm.org/D33830

and is included in 5.0.0-rc2. This commit fixes it and adds a unittest for this property.

Should go into clang-5.0 IMHO.

Contributed by @pboettch!

Reviewers: djasper, krasimir

Reviewed By: djasper, krasimir

Subscribers: hans, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311532 91177308-0d34-0410-b5e6-96231b3b80d8
2017-08-23 07:18:36 +00:00
Krasimir Georgiev 6fb41bb447 [clang-format] Break non-trailing block comments
Summary:
This patch is an alternative to https://reviews.llvm.org/D36614, by resolving a
non-idempotency issue by breaking non-trailing comments:

Consider formatting the following code with column limit at `V`:
```
                    V
const /* comment comment */ A = B;
```
The comment is not a trailing comment, breaking before it doesn't bring it under
the column limit. The formatter breaks after it, resulting in:

```
                    V
const /* comment comment */
    A = B;
```
For a next reformat, the formatter considers the comment as a trailing comment,
so it is free to break it further, resulting in:

```
                    V
const /* comment
         comment */
    A = B;
```
This patch improves the situation by directly producing the third case.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311457 91177308-0d34-0410-b5e6-96231b3b80d8
2017-08-22 14:40:05 +00:00
Krasimir Georgiev 6fc7e24c55 [clang-format] Fix comment levels between '} else {' and PPDirective.
Summary:
This fixes a regression exposed by r307795 and rL308725 in which the level of a
comment line between '} else {' and a preprocessor directive is incorrectly set
as the level of the '} else {' line. For example, this :
```
int f(int i) {
  if (i) {
    ++i;
  } else {
    // comment
#ifdef A
    --i;
#endif
  }
}
```
was formatted as:
```
int f(int i) {
  if (i) {
    ++i;
  } else {
  // comment
#ifdef A
    --i;
#endif
  }
}
```

Reviewers: djasper, klimek

Reviewed By: klimek

Subscribers: klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@308882 91177308-0d34-0410-b5e6-96231b3b80d8
2017-07-24 14:51:59 +00:00
Krasimir Georgiev 98d12e2907 [clang-format] Fix comment levels between '}' and PPDirective
Summary:
This fixes a regression exposed by r307795 in which the level of a comment line
between '}' and a preprocessor directive is incorrectly set as the level of the
line before the '}'. In effect, this:
```
int f(int i) {
  int j = i;
  return i + j;
}
// comment

#ifdef A
#endif
```
was formatted as:
```
int f(int i) {
  int j = i;
  return i + j;
}
  // comment

#ifdef A
#endif
```

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@308725 91177308-0d34-0410-b5e6-96231b3b80d8
2017-07-21 10:26:13 +00:00
Krasimir Georgiev 059cc9230c [clang-format] Keep level of comment before an empty line
Summary:
This patch fixes bug https://bugs.llvm.org/show_bug.cgi?id=3313: a comment line
was aligned with the next #ifdef even in the presence of an empty line between
them.

Reviewers: djasper, klimek

Reviewed By: djasper

Subscribers: klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@307795 91177308-0d34-0410-b5e6-96231b3b80d8
2017-07-12 15:21:43 +00:00
Krasimir Georgiev 942d6e44a9 [clang-format] Fix alignment of preprocessor trailing comments
Summary:
This patch is a follow-up of https://reviews.llvm.org/rL304687, which fixed an
overflow in the comment alignment code in clang-format. The token length of
trailing comments of preprocessor directives is calculated incorrectly by
including the text between consecutive directives. That causes them to not being
aligned.

For example, in this code with column limit 20
```
#if A
#else  // A
int iiii;
#endif // B
```
the length of the token `// A` was wrongly calculated as 14 = 5 (the size of `// A\n`) plus 9 (the size of `int iiii;`) and so `// A` wouldn't be aligned with `// B` and this was produced:
```
#if A
#else // A
int iiii;
#endif // B
```

This patch fixes this case.

Reviewers: alexfh

Reviewed By: alexfh

Subscribers: cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@304912 91177308-0d34-0410-b5e6-96231b3b80d8
2017-06-07 14:05:06 +00:00
Krasimir Georgiev 2241c1625a [clang-format] Don't align too long broken trailing comments
Summary:
This patch fixes a bug where clang-format will align newly broken trailing
comments even if this will make them exceed the line limit. The bug was caused
by a combination of unsigned arithmetic overflow and an imprecise computation
of the length of broken comment lines.

Reviewers: djasper, alexfh

Reviewed By: alexfh

Subscribers: klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@304687 91177308-0d34-0410-b5e6-96231b3b80d8
2017-06-04 19:27:02 +00:00
Francois Ferrand 6c88fb85c2 clang-format: do not reflow bullet lists
Summary:
This patch prevents reflowing bullet lists in block comments.

It handles all lists supported by doxygen and markdown, e.g. bullet
lists starting with '-', '*', '+', as well as numbered lists starting
with -# or a number followed by a dot.

Reviewers: krasimir

Reviewed By: krasimir

Subscribers: djasper, klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@303556 91177308-0d34-0410-b5e6-96231b3b80d8
2017-05-22 14:47:17 +00:00
Krasimir Georgiev 51ee6cef43 [clang-format] Keep trailing preprocessor line comments separate from the following section comments
Summary:
r303415 changed the way a sequence of line comments following a preprocessor
macro is handled, which has the unfortunate effect of aligning a trailing
preprocessor line comment and following unrelated section comments, so:
```
#ifdef A // comment about A
// section comment
#endif
```
gets turned into:
```
#ifdef A // comment about A
         // section comment
#endif
```
This patch fixes this by additionally checking the original start columns of
the line comments.

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@303541 91177308-0d34-0410-b5e6-96231b3b80d8
2017-05-22 10:07:56 +00:00
Krasimir Georgiev 422116da28 clang-format: fix prefix for doxygen comments after member
Summary:
Doxygen supports putting documentation blocks after member, by adding
an additional < marker in the comment block. This patch makes sure
this marker is used in lines which are introduced by breaking the
comment.

  int foo; ///< Some very long comment.

becomes:

  int foo; ///< Some very long
           ///< comment.

Contributed by @Typz!

Reviewers: krasimir

Reviewed By: krasimir

Subscribers: djasper, klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@303330 91177308-0d34-0410-b5e6-96231b3b80d8
2017-05-18 07:36:21 +00:00
Krasimir Georgiev f47413706b [clang-format] Replace IncompleteFormat by a struct with Line
Summary: This patch replaces the boolean IncompleteFormat that is used to notify the client if an unrecoverable syntax error occurred by a struct that also contains a line number.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@300985 91177308-0d34-0410-b5e6-96231b3b80d8
2017-04-21 14:35:20 +00:00
Richard Smith 9ea711133f Fix mishandling of escaped newlines followed by newlines or nuls.
Previously, if an escaped newline was followed by a newline or a nul, we'd lex
the escaped newline as a bogus space character. This led to a bunch of
different broken corner cases:

For the pattern "\\\n\0#", we would then have a (horizontal) space whose
spelling ends in a newline, and would decide that the '#' is at the start of a
line, and incorrectly start preprocessing a directive in the middle of a
logical source line. If we were already in the middle of a directive, this
would result in our attempting to process multiple directives at the same time!
This resulted in crashes, asserts, and hangs on invalid input, as discovered by
fuzz-testing.

For the pattern "\\\n" at EOF (with an implicit following nul byte), we would
produce a bogus trailing space character with spelling "\\\n". This was mostly
harmless, but would lead to clang-format getting confused and misformatting in
rare cases. We now produce a trailing EOF token with spelling "\\\n",
consistent with our handling for other similar cases -- an escaped newline is
always part of the token containing the next character, if any.

For the pattern "\\\n\n", this was somewhat more benign, but would produce an
extraneous whitespace token to clients who care about preserving whitespace.
However, it turns out that our lexing for line comments was relying on this bug
due to an off-by-one error in its computation of the end of the comment, on the
slow path where the comment might contain escaped newlines.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@300515 91177308-0d34-0410-b5e6-96231b3b80d8
2017-04-17 23:44:51 +00:00
Daniel Jasper 59d31eb1ff Fix r296605 so that stuff in #ifndef SWIG blocks is still formatted.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@296608 91177308-0d34-0410-b5e6-96231b3b80d8
2017-03-01 11:10:11 +00:00
Daniel Jasper 948ee9b211 clang-format: Ignore contents of #ifdef SWIG .. #endif blocks.
Those blocks are used if C++ code is SWIG-wrapped (see swig.org) and
usually do not contain C++ code. Also cleanup the implementation of for #if 0
and #if false a bit.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@296605 91177308-0d34-0410-b5e6-96231b3b80d8
2017-03-01 10:47:52 +00:00
Krasimir Georgiev fd47979d4a [clang-format] Align block comment decorations
Summary:
This patch implements block comment decoration alignment.

source:
```
/* line 1
* line 2
*/
```

result before:
```
/* line 1
* line 2
*/
```

result after:
```
/* line 1
 * line 2
 */
```

Reviewers: djasper, bkramer, klimek

Reviewed By: klimek

Subscribers: mprobst, cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@295312 91177308-0d34-0410-b5e6-96231b3b80d8
2017-02-16 12:39:31 +00:00
Krasimir Georgiev cd78094cde [clang-format] Remove dead code in FormatTestComments, NFC
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@295044 91177308-0d34-0410-b5e6-96231b3b80d8
2017-02-14 10:35:42 +00:00
Krasimir Georgiev 1eed05ab3d [clang-format] Move comment tests to their own file.
Summary: With a growing suite of comment-related tests, it makes sense to take them out of the main test file. No functional changes.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek, mgorny

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@294439 91177308-0d34-0410-b5e6-96231b3b80d8
2017-02-08 12:53:18 +00:00