Commit Graph

1657 Commits

Author SHA1 Message Date
Nico Weber 48c94a6164 [clang-format] Recognize Java logical shift assignment operator
At present, clang-format mangles Java containing logical right shift operators
('>>>=' or '>>>'), splitting them in two, resulting in invalid code:

 public class Minimal {
   public void func(String args) {
     int i = 42;
-    i >>>= 1;
+    i >> >= 1;
     return i;
   }
 }

This adds both forms of logical right shift to the FormatTokenLexer, so
clang-format won't attempt to split them and insert bogus whitespace.

https://reviews.llvm.org/D31652
Patch from Richard Bradfield <bradfier@fstab.me>!

llvm-svn: 299952
2017-04-11 15:50:04 +00:00
Alexander Kornienko d4fa2e6348 [clang-format] Handle NSString literals by merging tokens.
Summary:
This fixes a few outstanding bugs:
  * incorrect breaking of NSString literals containing double-width characters;
  * inconsistent formatting of ObjC dictionary literals containing NSString
    literals;
  * AlwaysBreakBeforeMultilineStrings ignoring implicitly-concatenated NSString
    literals.

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

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

llvm-svn: 299927
2017-04-11 09:55:00 +00:00
Nico Weber dc06518ff4 clang-format: Support formatting utf-8 character literals in C++11+ mode.
clang-format <<END
auto c1 = u8'a';
auto c2 = u'a';
END

Before:
  auto c1 = u8 'a';
  auto c2 = u'a';

Now:
  auto c1 = u8'a';
  auto c2 = u'a';

Patch from Denis Gladkikh <llvm@denis.gladkikh.email>!

llvm-svn: 299574
2017-04-05 18:10:42 +00:00
Martin Probst c4a3d081a9 clang-format: [JS] fix whitespace around "of" operator.
Summary:
Previously:
    import {of } from 'x';
    of (null);

Now:
    import {of} from 'x';
    of(null);

Reviewers: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 299533
2017-04-05 10:56:07 +00:00
Matthias Gehre ddae2516b8 [clang-format] fix crash in NamespaceEndCommentsFixer (PR32438)
Summary:
The new test case was crashing before. Now it passes
as expected.

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

llvm-svn: 299465
2017-04-04 20:11:13 +00:00
Daniel Jasper 72b3357f2d clang-format: [JavaScript] Ignore QT keywords.
llvm-svn: 299204
2017-03-31 12:04:37 +00:00
Nikola Smiljanic 92b397fb9d Fix issues in clang-format's AlignConsecutive modes.
Patch by Ben Harper.

llvm-svn: 298574
2017-03-23 02:51:25 +00:00
Martin Probst c9c51c4e41 [clang-format] disable adding extra space after MSVC '__super' keyword
clang-format treats MSVC `__super` keyword like all other keywords adding
a single space after. This change disables this behavior for `__super`.

Patch originally by jutocz (thanks!).

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

llvm-svn: 297936
2017-03-16 10:21:35 +00:00
Daniel Jasper 04bbda9923 clang-format: Fix bug in wrapping behavior of operators.
Before (even violating the column limit):
  auto Diag =
      diag()
      << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa);

After:
  auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,
                                         aaaaaaaaaaaaaaaaaaaaaaaaaa);

llvm-svn: 297931
2017-03-16 07:54:11 +00:00
Daniel Jasper c79e4d2d25 clang-format: Make it very slighly more expensive to wrap between "= {".
This prevents unwanted fallout from r296664. Specifically in proto formatting,
this changed:
  optional Aaaaaaaa aaaaaaaa = 12 [
    (aaa) = aaaa,
    (bbbbbbbbbbbbbbbbbbbbbbbbbb) = {
      aaaaaaaaaaaaaaaaa: true,
      aaaaaaaaaaaaaaaa: true
    }
  ];

Into:
  optional Aaaaaaaa aaaaaaaa = 12 [
    (aaa) = aaaa,
    (bbbbbbbbbbbbbbbbbbbbbbbbbb) =
        {aaaaaaaaaaaaaaaaa: true, aaaaaaaaaaaaaaaa: true}
  ];

Which is considered less readable. Generally, it seems preferable to
format such dict literals as blocks rather than contract them to one
line.

llvm-svn: 297696
2017-03-14 00:40:32 +00:00
Martin Probst b4cdb65e5f clang-format: [JS] do not wrap @see tags.
Summary:
@see is special among JSDoc tags in that it is commonly followed by URLs. The JSDoc spec suggests that users should wrap URLs in an additional {@link url...} tag (@see http://usejsdoc.org/tags-see.html), but this is very commonly violated, with @see being followed by a "naked" URL.

This change special cases all JSDoc lines that contain an @see not to be wrapped to account for that.

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

llvm-svn: 297607
2017-03-13 09:39:23 +00:00
Martin Probst 22b8d26924 clang-format: [JS] allow breaking after non-null assertions.
Summary:
Previously clang-format would not break after any !. However in TypeScript, ! can be used as a post fix operator for non-nullability:
    x.foo()!.bar()!;

With this change, clang-format will wrap after the ! if it is likely a post-fix non null operator.

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

llvm-svn: 297606
2017-03-13 09:14:23 +00:00
Martin Probst b98ab89ebb clang-format: [JS] do not wrap after interface and type.
Summary:
`interface` and `type` are pseudo keywords and cause automatic semicolon
insertion when followed by a line break:

    interface  // gets parsed as a long variable access to "interface"
        VeryLongInterfaceName {

    }

With this change, clang-format not longer wraps after `interface` or `type`.

Reviewers: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 297605
2017-03-13 07:10:18 +00:00
Andi-Bogdan Postelnicu 0ef8ee19f8 [clang-format] Add option to break before inheritance separation operator in class declaration.
Differential Revision: https://reviews.llvm.org/D30487

llvm-svn: 297467
2017-03-10 15:10:37 +00:00
Krasimir Georgiev bc05ebaa5a [clang-format] Look at NoLineBreak and NoLineBreakInOperand before breakProtrudingToken
Summary:
This patch makes ContinuationIndenter call breakProtrudingToken only if
NoLineBreak and NoLineBreakInOperand is false.

Previously, clang-format required two runs to converge on the following example with 24 columns:
Note that the second operand shouldn't be splitted according to NoLineBreakInOperand, but the
token breaker doesn't take that into account:
```
func(a, "long long long long", c);
```
After first run:
```
func(a, "long long "
        "long long",
         c);
```
After second run, where NoLineBreakInOperand is taken into account:
```
func(a,
     "long long "
     "long long",
     c);
```

With the patch, clang-format now obtains in one run:
```
func(a,
     "long long long"
     "long",
     c);
```
which is a better token split overall.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 297274
2017-03-08 12:54:50 +00:00
Daniel Jasper 628dd85b32 clang-format: Get slightly better at understanding */&.
Before:
  void f() { MACRO(A * const a); }

After:
  void f() { MACRO(A *const a); }

llvm-svn: 297268
2017-03-08 09:49:12 +00:00
Krasimir Georgiev 17725d8391 [clang-format] Enable comment reflowing in multiline comments containing pragmas
Summary:
This patch enables comment reflowing of lines not matching the comment pragma regex
in multiline comments containing comment pragma lines. Previously, these comments
were dumped without being reindented to the result.

Reviewers: djasper, mprobst

Reviewed By: mprobst

Subscribers: klimek, mprobst, cfe-commits

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

llvm-svn: 297261
2017-03-08 08:55:12 +00:00
Andi-Bogdan Postelnicu 4743e2d9b9 [clang-format] Followup of D30646 - unbreak the build
llvm-svn: 297148
2017-03-07 15:20:31 +00:00
Andi-Bogdan Postelnicu 6732989637 [clang-format] Fixed indent issue when adding a comment at the end of a return type in named function declaration.
Differential Revision: https://reviews.llvm.org/D30646

llvm-svn: 297143
2017-03-07 14:48:02 +00:00
Krasimir Georgiev eb62118e6d [clang-format] Support namespaces ending in semicolon
Summary:
This patch adds support for namespaces ending in semicolon to the namespace comment fixer.
source:
```
namespace A {
  int i;
  int j;
};
```
clang-format before:
```
namespace A {
  int i;
  int j;
} // namespace A;
```
clang-format after:
```
namespace A {
  int i;
  int j;
}; // namespace A
```

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 297140
2017-03-07 14:07:43 +00:00
Krasimir Georgiev 615353cf38 [clang-format] Add tests for ambiguous namespaces to the comment fixer
llvm-svn: 297034
2017-03-06 17:29:25 +00:00
Krasimir Georgiev bda77397db [clang-format] Make NamespaceEndCommentFixer add at most one comment
Summary:
Until now, NamespaceEndCommentFixer was adding missing comments for every run,
which results in multiple end comments for:
```
namespace {
  int i;
  int j;
}
#if A
  int a = 1;
#else
  int a = 2;
#endif
```
result before:

```
namespace {
  int i;
  int j;
}// namespace // namespace
#if A
  int a = 1;
#else
  int a = 2;
#endif
```
result after:
```
namespace {
  int i;
  int j;
}// namespace
#if A
  int a = 1;
#else
  int a = 2;
#endif
```

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

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

llvm-svn: 297028
2017-03-06 16:44:45 +00:00
Krasimir Georgiev 9163fe2aba [clang-format] Use number of unwrapped lines for short namespace
Summary:
This patch makes the namespace comment fixer use the number of unwrapped lines
that a namespace spans to detect it that namespace is short, thus not needing
end comments to be added.
This is needed to ensure clang-format is idempotent. Previously, a short namespace
was detected by the original source code lines. This has the effect of requiring two
runs for this example:
```
namespace { class A; }
```
after first run:
```
namespace {
class A;
}
```
after second run:
```
namespace {
class A;
} // namespace
```

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 296736
2017-03-02 09:54:44 +00:00
Daniel Jasper 893b8adca2 clang-format: [JS] Properly format object literals with shorthands.
Before:
  return {
    a,
    b: 'b', c,
  };

After:
  return {
    a,
    b: 'b',
    c,
  };

llvm-svn: 296664
2017-03-01 19:47:28 +00:00
Daniel Jasper 01b8783a05 clang-format: [JS/TS] Properly understand cast expressions.
Many things were wrong:
- We didn't always allow wrapping after "as", which can be necessary.
- We used to Undestand the identifier after "as" as a start of a name.
- We didn't properly parse the structure of the expression with "as"
  having the precedence of relational operators

llvm-svn: 296659
2017-03-01 19:26:12 +00:00
Krasimir Georgiev 85c3704c0d [clang-format] Don't add namespace end comments for unbalanced right braces after namespace end
llvm-svn: 296638
2017-03-01 16:38:08 +00:00
Krasimir Georgiev 32eaa864e3 [clang-format] Add a new flag FixNamespaceComments to FormatStyle
Summary:
This patch enables namespace end comments under a new flag FixNamespaceComments,
which is enabled for the LLVM and Google styles.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 296632
2017-03-01 15:35:39 +00:00
Daniel Jasper 62703eb8a7 Fix r296605 so that stuff in #ifndef SWIG blocks is still formatted.
llvm-svn: 296608
2017-03-01 11:10:11 +00:00
Daniel Jasper eab6cd474c 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.

llvm-svn: 296605
2017-03-01 10:47:52 +00:00
Daniel Jasper e154020dc5 clang-format: [Java] Fix bug in enum formatting.
Before:
  public enum VeryLongEnum {
    ENUM_WITH_MANY_PARAMETERS("aaaaaaaaaaaaaaaaaaaaaa",
                              "bbbbbbbbbbbbbbbbbbbbbbbbbbb",
                              "ccccccccccccccccccc")
    ,
    SECOND_ENUM("a", "b", "c");

    private VeryLongEnum(String a, String b, String c) {}
  }

After:
  public enum VeryLongEnum {
    ENUM_WITH_MANY_PARAMETERS("aaaaaaaaaaaaaaaaaaaaaa",
                              "bbbbbbbbbbbbbbbbbbbbbbbbbbb",
                              "ccccccccccccccccccc") ,
    SECOND_ENUM("a", "b", "c");

    private VeryLongEnum(String a, String b, String c) {}
  }

llvm-svn: 296499
2017-02-28 18:28:15 +00:00
Martin Probst bb46a7dd2a Blacklist arbitrary @\\w+ JSDoc tags from wrapping.
Summary:
Also limits the blacklisting to only apply when the tag is actually
followed by a parameter in curly braces.

    /** @mods {long.type.must.not.wrap} */

vs

    /** @const this is a long description that may wrap. */

Reviewers: djasper

Subscribers: klimek, krasimir, cfe-commits

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

llvm-svn: 296467
2017-02-28 11:08:24 +00:00
Krasimir Georgiev 7cb267af75 [clang-format] Add a NamespaceEndCommentsFixer
Summary:
This patch adds a NamespaceEndCommentsFixer TokenAnalyzer for clang-format,
which fixes end namespace comments.
It currently supports inserting and updating existing wrong comments.

Example source:
```
namespace A {
int i;
}

namespace B {
int j;
} // namespace A
```

after formatting:
```
namespace A {
int i;
} // namespace A

namespace B {
int j;
} // namespace B
```

Reviewers: klimek, djasper

Reviewed By: djasper

Subscribers: klimek, mgorny

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

llvm-svn: 296341
2017-02-27 13:28:36 +00:00
Martin Probst 20371c30ef clang-format: [JS] whitespace after async in arrow functions.
Summary:
Async arrow functions should be marked with a whitespace after the async keyword, before the parameter list:
    x = async () => foo();

Before:
    x = async() => foo();

This makes it easier to tell apart an async arrow function from a call to a function called async.

Reviewers: bkramer

Subscribers: cfe-commits, klimek

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

llvm-svn: 296330
2017-02-27 11:15:53 +00:00
Nico Weber d96ae86735 clang-format: Fix many Objective-C formatting regressions from r289428
r289428 added a separate language kind for Objective-C, but kept many
"Language == LK_Cpp" checks untouched.  This introduced a "IsCpp()"
method that returns true for both C++ and Objective-C++, and replaces
all comparisons of Language with LK_Cpp with calls to this new method.

Also add a lot more test coverge for formatting things in LK_ObjC mode,
by having FormatTest's verifyFormat() test for LK_ObjC everything that's
being tested for LK_Cpp at the moment.

Fixes PR32060 and many other things.

llvm-svn: 296160
2017-02-24 19:10:12 +00:00
Daniel Jasper 98e0b12d13 clang-format: [JS] Improve line-wrapping behavior of template strings.
Specifically, similar to other blocks, clang-format now wraps both
after "${" and before the corresponding "}", if the contained
expression spans multiple lines.

llvm-svn: 295663
2017-02-20 14:51:16 +00:00
Daniel Jasper d9b319e3e3 clang-format: Prevent weird line-wraps in complex lambda introducers
Before:
  aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
      [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]() -> ::std::
	  unordered_set<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {
	    //
	  });

After:
  aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
      [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()
	  -> ::std::unordered_set<
	      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {
	//
      });

llvm-svn: 295659
2017-02-20 12:43:48 +00:00
Daniel Jasper 3d934d366e clang-format: [JS/TS] Improve detection for array subscripts in types.
Before:
  var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[
                  ]).someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

After:
  var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])
                      .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

llvm-svn: 295658
2017-02-20 12:43:41 +00:00
Daniel Jasper 23c2b5ae7e clang-format: Don't remove existing spaces between identifier and ::.
This can lead to bad behavior with macros that are used to annotate
functions (e.g. ALWAYS_INLINE).

Before, this:
  ALWAYS_INLINE ::std::string getName() ...

was turned into:
  ALWAYS_INLINE::std::string getName() ...

If it turns out that clang-format is failing to clean up a lot of the
existing spaces now, we can add more analyses of the identifier. It
should not currently. Cases where clang-format breaks nested name
specifiers should be fine as clang-format wraps after the "::". Thus, a
line getting longer and then shorter again should lead to the same
original code.

llvm-svn: 295437
2017-02-17 10:44:07 +00:00
Krasimir Georgiev bb99a36dc0 [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

llvm-svn: 295312
2017-02-16 12:39:31 +00:00
Krasimir Georgiev 93a8e9df44 [clang-format] Remove dead code in FormatTestComments, NFC
llvm-svn: 295044
2017-02-14 10:35:42 +00:00
Nico Weber 2108880660 clang-format: don't break code using __has_include, PR31908
llvm-svn: 294772
2017-02-10 19:36:52 +00:00
Krasimir Georgiev fd0dda765e [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

llvm-svn: 294439
2017-02-08 12:53:18 +00:00
Krasimir Georgiev f62f958a58 [clang-format] Break before a sequence of line comments aligned with the next line.
Summary:
Make the comment alignment respect sections of line comments originally alinged
with the next token. Until now the decision how to break a continuous sequence
of line comments into sections was taken without reference to the next token.

source:
```
class A {
public: // comment about public
  // comment about a
  int a;
}
```

format before:
```
class A {
public: // comment about public
        // comment about a
  int a;
}
```

format after:
```
class A {
public: // comment about public
  // comment about a
  int a;
}
```

Reviewers: djasper, klimek

Reviewed By: klimek

Subscribers: cfe-commits, klimek

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

llvm-svn: 294435
2017-02-08 10:30:44 +00:00
Daniel Jasper 697a8ec6cd clang-format: Fix bad variable declaration detection.
Before:
  LooooooooooooooooongType
  variable(nullptr, [](A *a) {});

After:
  LooooooooooooooooongType
      variable(nullptr, [](A *a) {});

llvm-svn: 294358
2017-02-07 21:38:16 +00:00
Martin Probst 8e3eba0373 clang-format: [JS] correcly format object literal methods.
Summary:
In JavaScript, object literals can contain methods:

   var x = {
     a() { return 1; },
   };

Previously, clang-format always parsed nested {} inside a braced list as
further braced lists. Special case this logic for JavaScript to try
parsing as a braced list, but fall back to parsing as a child block.

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

llvm-svn: 294315
2017-02-07 16:33:13 +00:00
Martin Probst 16282993b7 clang-format: [JS] exclaim preceding regex literals.
Summary:
Regex detection would incorrectly classify a trailing `!` operator
(nullability cast) followed by a `/` as the start of a regular
expression literal. This fixes code such as:

    var foo = x()! / 10;

Which would previously parse a regexp all the way to the end of the
source file (or next `/`).

Reviewers: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 294304
2017-02-07 14:08:03 +00:00
Martin Probst 1027fb8a06 clang-format: [JS] handle parenthesized class expressions.
Summary:
In JavaScript, classes are expressions, so they can appear e.g. in
argument lists.

    var C = foo(class {
      bar() {
        return 1;
      }
    };

Reviewers: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 294302
2017-02-07 14:05:30 +00:00
Daniel Jasper 2db1b4a177 clang-format: Fix bug with conflicting BreakBeforeBinaryOperations and AlignAfterOpenBracket
Fix for the formatting options combination of
BreakBeforeBinaryOperators: All, AlignAfterOpenBracket: AlwaysBreak not
handling long templates correctly. This patch allows a break after an
opening left parenthesis, TemplateOpener, or bracket when both options
are enabled.

Patch by Daphne Pfister, thank you!

Fixes llvm.org/PR30304.

llvm-svn: 294179
2017-02-06 10:55:49 +00:00
Daniel Jasper c06f6da34e clang-format: [JS] Fix bugs in parsing and aligning template strings.
llvm-svn: 294009
2017-02-03 14:32:38 +00:00
Krasimir Georgiev d105b72df6 [clang-format] Re-align broken comment lines where appropriate.
Summary:
The comment aligner was skipping over newly broken comment lines. This patch fixes that.

source:
```
int ab; // line
int a; // long long
```

format with column limit 15 before:
```
int ab; // line
int a;  // long
       // long
```

format with column limit 15 after:
```
int ab; // line
int a;  // long
        // long
```

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 293997
2017-02-03 10:18:25 +00:00