Commit Graph

722 Commits

Author SHA1 Message Date
Daniel Jasper 1dbc2105dc clang-format: Fix post-commit review comment of r299204, use Style.isCpp().
Also, while at it, s/IsCpp/isCpp/ so that it follows LLVM style.

llvm-svn: 299214
2017-03-31 13:30:24 +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 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 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
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
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
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
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 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 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
Nico Weber 2108880660 clang-format: don't break code using __has_include, PR31908
llvm-svn: 294772
2017-02-10 19:36:52 +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
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
Daniel Jasper da910e0a8b clang-format: [Proto] Also supports implicit string literal concatenation
llvm-svn: 293995
2017-02-03 08:29:02 +00:00
Daniel Jasper 24de6fbfdb clang-format: [JS] Properly set scopes inside template strings.
Before:
  var f = `aaaaaaaaaaaaa:${aaaaaaa
              .aaaaa} aaaaaaaa
           aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;

After:
  var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa
           aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;

llvm-svn: 293622
2017-01-31 13:03:07 +00:00
Daniel Jasper f201929010 clang-format: [JS] Fix incorrect line break in template strings.
Before:
  var f = `aaaa ${a ? 'a' : 'b'
                            }`;

After:
  var f = `aaaa ${a ? 'a' : 'b'}`;

llvm-svn: 293618
2017-01-31 12:07:35 +00:00
Daniel Jasper 7d42f3f746 [clang-format] Refactor WhitespaceManager and friends
The main motivation behind this is to cleanup the WhitespaceManager and
make it more extensible for future alignment etc. features.
Specifically, WhitespaceManager has started to copy more and more code
that is already present in FormatToken. Instead, I think it makes more
sense to actually store a reference to each FormatToken for each change.

This has as a consequence led to a change in the calculation of indent
levels. Now, we actually compute them for each Token ahead of time,
which should be more efficient as it removes an unsigned value for the
ParenState, which is used during the combinatorial exploration of the
solution space.

No functional changes intended.

Review: https://reviews.llvm.org/D29300
llvm-svn: 293616
2017-01-31 11:25:01 +00:00
Krasimir Georgiev 91834227a3 [clang-format] Implement comment reflowing.
Summary:
This presents a version of the comment reflowing with less mutable state inside
the comment breakable token subclasses. The state has been pushed into the
driving breakProtrudingToken method. For this, the API of BreakableToken is enriched
by the methods getSplitBefore and getLineLengthAfterSplitBefore.

Reviewers: klimek

Reviewed By: klimek

Subscribers: djasper, klimek, mgorny, cfe-commits, ioeric

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

llvm-svn: 293055
2017-01-25 13:58:58 +00:00
Daniel Jasper c941e7d21b clang-format: Improve support for override/final as variable names.
Before:
  bool a = f() &&override.f();
  bool a = f() &&final.f();
  void f(const MyOverride & override);
  void f(const MyFinal & final);

After:
  bool a = f() && override.f();
  bool a = f() && final.f();
  void f(const MyOverride &override);
  void f(const MyFinal &final);

llvm-svn: 291434
2017-01-09 11:04:07 +00:00
Daniel Jasper 2388861f09 clang-format: Less eagerly try to keep label-value pairs on a line.
Before:
  string v =
      StrCat("aaaaaaaaaaaaaaaaaaaaaaaaaaa: ", SomeFunction(aaaaaaaaaaaa,
                                                           aaaaaaaaaaaaaaa),
             bbbbbbbbbbbbbbbbbbbbbbb);

After:
  string v = StrCat("aaaaaaaaaaaaaaaaaaaaaaaaaaa: ",
                    SomeFunction(aaaaaaaaaaaa, aaaaaaaaaaaaaaa),
                    bbbbbbbbbbbbbbbbbbbbbbb);

llvm-svn: 290337
2016-12-22 12:37:06 +00:00
Daniel Jasper f789f05ee2 clang-format: Fix bug in understanding string-label&value analysis.
While for <<-operators often used in log statments, a single key value
pair is always on the second operator, e.g.

  llvm::errs() << "aaaaa=" << aaaaa;

It is on the first operator for plus- or comma-concatenated strings:

  string s = "aaaaaaaaaa: " + aaaaaaaa;

(the "=" not counting because that's a different operator precedence)

llvm-svn: 290177
2016-12-20 15:27:46 +00:00
Daniel Jasper 7aacf468c0 clang-format: Slightly tweak the behavior of <<-wrapping.
Before:
  SomeLongLoggingStatementOrMacro() << "Some long text "
                                    << some_variable << "\n";

Before:
  SomeLongLoggingStatementOrMacro()
      << "Some long text " << some_variable << "\n";

Short logging statements are already special cased in a different part
of the code.

llvm-svn: 290094
2016-12-19 11:14:23 +00:00
Daniel Jasper 7209bb9d4e clang-format: Keep string-literal-label + value pairs on a line.
We have previously done that for <<-operators. This patch also adds
this logic for "," and "+".

Before:
  string v = "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa + "aaaaaaaaaaaaaaaa: " +
             aaaaaaaaaaaaaaaa + "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa;
  string v = StrCat("aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa, "aaaaaaaaaaaaaaaa: ",
                    aaaaaaaaaaaaaaaa, "aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa);

After:
  string v = "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa +
	     "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa +
	     "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa;
  string v = StrCat("aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa,
		    "aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa,
		    "aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa);

llvm-svn: 289531
2016-12-13 11:16:42 +00:00
Daniel Jasper 03a04fe95f clang-format: Separate out a language kind for ObjC.
While C(++) and ObjC are generally formatted the same way and can be
mixed, people might want to choose different styles based on the
language. This patch recognizes .m and .mm files as ObjC and also
implements a very crude detection of whether or not a .h file contains
ObjC code. This can be improved over time.

Also move most of the ObjC tests into their own test file to keep file
size maintainable.

llvm-svn: 289428
2016-12-12 12:42:29 +00:00
Daniel Jasper e98e58111b clang-format: [JS] Properly format dict literals that skip labels.
llvm-svn: 288121
2016-11-29 09:40:36 +00:00
Daniel Jasper fd36f0b504 clang-format: Support ObjC selectors with unnamed parameters.
This fixes llvm.org/PR28063.

llvm-svn: 286715
2016-11-12 07:38:22 +00:00
Nico Weber b2673a1e48 [clang-format] Fix PR30527: Regression when clang-format insert spaces in [] when in template
Actual regression was introduced in r272668. This revision fixes JS script, but
also regress Cpp case. It manifests with spaces added when template is followed
with array. Bug 30527 mentions case of array as a nested template type
(foo<bar<baz>[]>). Fix is to detect such case and to prevent treating it as
array initialization, but as a subscript case. However, before r272668, this
case was treated simple because we were detecting it as a StartsObjCMethodExpr.
Same was true for other similar case - array of templates (foo<int>[]). This
patch tries to address two problems: 1) fixing regression 2) making sure both
cases (array as a nested type, array of templates) which were entering
StartsObjCMethodExpr branch are handled now appropriately.

https://reviews.llvm.org/D26163
Patch from Branko Kokanovic <branko@kokanovic.org>!

llvm-svn: 286507
2016-11-10 21:49:25 +00:00
Martin Probst e19acaa361 drop kw_module from ASI protection block
llvm-svn: 286469
2016-11-10 16:21:04 +00:00
Martin Probst 72fd75a0d1 clang-format: [JS] do not break after declare namespace.
See TypeScript grammar for tokens following 'declare':
https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#A.10

llvm-svn: 286467
2016-11-10 16:20:58 +00:00
Daniel Jasper 8315ea1613 clang-format: [TypeScript] Fix bug in handling of non-null operator.
Before:
  var i = x!-1;

After:
  var i = x! - 1;

llvm-svn: 286367
2016-11-09 14:12:55 +00:00
Daniel Jasper a432208bca clang-format: Better support for CUDA's triple brackets.
Before:
  aaaaaaaaaaaaaaa<
      aaaaaaaaa, aaaaaaaaaa,
      aaaaaaaaaaaaaa><<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();

After:
  aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>
      <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();

llvm-svn: 286041
2016-11-05 17:43:16 +00:00
Daniel Jasper 28b4d5133c clang-format: Fix bug in function reference qualifier detection.
Before:
  template <typename T>
      void F(T) &&
      = delete;

After:
  template <typename T>
  void F(T) && = delete;

llvm-svn: 285674
2016-11-01 06:23:19 +00:00
Daniel Jasper d0d27aa59b clang-format: Fix incorrect pointer detection.
Before:
  void f() { f(float{1}, a *a); }

After:
  void f() { f(float{1}, a * a); }

llvm-svn: 285673
2016-11-01 06:23:14 +00:00
Daniel Jasper b559b43802 clang-format: [JS] Fix incorrect space when "as" is used as identifier.
Before:
  aaaaa.as ();

After:
  aaaaa.as();

llvm-svn: 285672
2016-11-01 06:23:10 +00:00
Daniel Jasper 3ade3be2a1 clang-format: Fix incorrect binary operator detection.
Before:
  int x = f(* + [] {});

After:
  int x = f(*+[] {});

llvm-svn: 285671
2016-11-01 06:23:05 +00:00
Daniel Jasper 71e50af675 clang-format: [JS] Fix formatting of generator functions.
Before:
  var x = {
    a: function*
	() {
	  //
	}
  }

After:
  var x = {
    a: function*() {
      //
    }
  }

llvm-svn: 285670
2016-11-01 06:22:59 +00:00
Daniel Jasper 4d67dd77a1 clang-format: [JS] Fix space when for is used as regular identifier.
Before:
  x.for () = 1;

After:
  x.for() = 1;

llvm-svn: 285669
2016-11-01 06:22:54 +00:00
Daniel Jasper fda47cd873 Skip over AnnotatedLines with >50 levels of nesting; don't format them.
Reasoning:
- ExpressionParser uses a lot of stack for these, bad in some environments.
- Our formatting algorithm is N^3 and gets really slow.
- The resulting formatting is unlikely to be any good.
- This is probably generated code we're formatting by accident.

We treat these as unparseable, and signal incomplete formatting. 50 is
an arbitrary number, I've only seen real problems from ~150 levels.

Patch by Sam McCall. Thank you.

llvm-svn: 285570
2016-10-31 13:23:00 +00:00
Daniel Jasper eb886635d9 clang-format: [JS] Fix missing space after 'yield'.
Before:
  class X {
    delete(val) {
      return null;
    }
    * gen() {
      yield[1, 2];
    }
    * gen() {
      yield{a: 1};
    }
  };

After:
  class X {
    delete(val) {
      return null;
    }
    * gen() {
      yield [1, 2];
    }
    * gen() {
      yield {a: 1};
    }
  };

llvm-svn: 285569
2016-10-31 13:18:25 +00:00
Malcolm Parsons 5d8cdb83db [Format] Cleanup after replacing constructor body with = default
Summary:
Remove colon and commas after replacing constructor body with = default.
Fix annotation of TT_CtorInitializerColon when preceded by a comment.

Reviewers: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 284732
2016-10-20 14:58:45 +00:00
Daniel Jasper 85d1f0b610 clang-format: Fix bad multi-variable for-loop formatting.
Before:
  for (int*p, *q; p != q; p = p->next) {

After:
  for (int *p, *q; p != q; p = p->next) {

llvm-svn: 283246
2016-10-04 20:18:25 +00:00
Daniel Jasper e31388aee9 [clang-format] Don't allow newline after uppercase Obj-C block return types
Fixes the following:
  BOOL (^aaa)(void) = ^BOOL {
  };

The first BOOL's token was getting set to TT_FunctionAnnotationRParen
incorrectly, which was causing an unexpected newline after (^aaa). This
was introduced in r245846.

Patch by Kent Sutherland, thank you!

llvm-svn: 282448
2016-09-26 22:19:08 +00:00