Commit Graph

1487 Commits

Author SHA1 Message Date
Eric Liu 3528832930 [clang-format] make header guard identification stricter (with Lexer).
Summary: make header guard identification stricter with Lexer.

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D20959

llvm-svn: 271883
2016-06-06 11:00:13 +00:00
Eric Liu 303baf53b1 [clang-format] skip empty lines and comments in the top of the code when inserting new headers.
Summary:
[clang-format] skip empty lines and comments in the top of the code when inserting new headers.

Pair-programmed with @hokein

Reviewers: djasper

Subscribers: ioeric, cfe-commits, hokein, klimek

Differential Revision: http://reviews.llvm.org/D20898

llvm-svn: 271664
2016-06-03 12:52:59 +00:00
Martin Probst 48622090c7 clang-format: [JS] no ASI on `import {x as\n y}`.
Summary: ASI did not handle the ES6 `as` operator correctly.

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D20817

llvm-svn: 271401
2016-06-01 15:22:47 +00:00
Martin Probst 081f176a62 clang-format: [JS] Sort imported symbols.
Summary: E.g. sort `import {b, a} from 'x';` into `import {a, b} from 'x';`.

Reviewers: djasper

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D20798

llvm-svn: 271400
2016-06-01 15:19:53 +00:00
Eric Liu 659afd55fa [clang-format] insert new #includes into correct blocks when cleaning up Replacement with cleanupAroundReplacements().
Summary:
When a replacement's offset is set to UINT_MAX or -1U, it is treated as
a header insertion replacement by cleanupAroundReplacements(). The new #include
directive is then inserted into the correct block.

Reviewers: klimek, djasper

Subscribers: klimek, cfe-commits, bkramer

Differential Revision: http://reviews.llvm.org/D20734

llvm-svn: 271276
2016-05-31 13:34:20 +00:00
Daniel Jasper e77f19c0af clang-format: Fix segfault introduced by allowing wraps after comments.
llvm-svn: 271191
2016-05-29 22:07:22 +00:00
Martin Probst 7ea9b6d783 clang-format: [JS] Support shebang lines on the very first line.
Summary:
Shebang lines (`#!/bin/blah`) can be used in JavaScript scripts to indicate
they should be run using e.g. node. This change treats # lines on the first line
as line comments.

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D20632

llvm-svn: 271185
2016-05-29 14:41:36 +00:00
Martin Probst 409697ecb9 clang-format: [JS] fix async parsing.
Summary:
Only treat the sequence `async function` as the start of a function expression,
as opposed to every occurrence of the token `async` (whoops).

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D20737

llvm-svn: 271184
2016-05-29 14:41:07 +00:00
Daniel Jasper 99302edd1c clang-format: Allow splitting the line after /**/-comments.
While it might change the meaning of the comment in rare circumstances,
it is better than violating the column limit.

llvm-svn: 270975
2016-05-27 08:59:34 +00:00
Eric Liu 1ef68456cf [clang-format] moved unit tests related to replacements cleaner from FormatTest.cpp to CleanUpTest.cpp.
llvm-svn: 270971
2016-05-27 08:20:02 +00:00
Martin Probst c4a0dd49a3 clang-format: [JS] sort ES6 imports.
Summary:
This change automatically sorts ES6 imports and exports into four groups:
absolute imports, parent imports, relative imports, and then exports. Exports
are sorted in the same order, but not grouped further.

To keep JS import sorting out of Format.cpp, this required extracting the
TokenAnalyzer infrastructure to separate header and implementation files.

Reviewers: djasper

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D20198

llvm-svn: 270203
2016-05-20 11:24:24 +00:00
Daniel Jasper 997cf2fea8 clang-format: [JS] Treat "for" as a reserved word after a ".".
Otherwise, clang-format can get confused with statements like:

  x.for = 1;

llvm-svn: 270188
2016-05-20 06:16:01 +00:00
Daniel Jasper 5dbcd3bd07 clang-format: [JS] Fix spacing in destructuring assignments.
Before:
  const[a, b, c] = [1, 2, 3];

After:
  const [a, b, c] = [1, 2, 3];

llvm-svn: 270029
2016-05-19 07:18:07 +00:00
Daniel Jasper 451544ab57 clang-format: Fix incorrect indentation in last line of macro definition
Before:
  #define MACRO(a) \
    if (a) {       \
      f();         \
    } else         \
    g()

After:
  #define MACRO(a) \
    if (a) {       \
      f();         \
    } else         \
      g()

llvm-svn: 270028
2016-05-19 06:30:48 +00:00
Daniel Jasper e2fab13313 clang-format: Fix enumerator case ranges.
Before:
  case a... b: break;

After:
  case a ... b: break;

llvm-svn: 270027
2016-05-19 06:19:17 +00:00
Eric Liu baf58c2309 [clang-format] Make formatReplacements() also sort #includes.
Summary: [clang-format] Make formatReplacements() also sort #includes.

Reviewers: bkramer, djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D20362

llvm-svn: 269924
2016-05-18 13:43:48 +00:00
Eric Liu ce5e4bc7ac Make clang-format cleaner remove redundant commas in list and redundant colon in constructor initializer.
Summary: Make clang-format cleaner remove redundant commas/colons in constructor initializer list.

Reviewers: klimek, djasper

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D19804

llvm-svn: 269888
2016-05-18 08:02:56 +00:00
Martin Probst e71b4cbdd1 clang-format: [JS] fix template string width counting.
Summary:
Simply looking at the final text greatly simplifies the algorithm and also
fixes a reported issue. This requires duplicating the "actual encoding width"
logic, but that seems cleaner than the column acrobatics before.

Reviewers: djasper, bkramer

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D20208

llvm-svn: 269747
2016-05-17 06:29:29 +00:00
Martin Probst a166979e45 clang-format: [JS] respect clang-format off when requoting strings.
Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D20200

llvm-svn: 269282
2016-05-12 11:20:32 +00:00
Daniel Jasper 2b2c967c1a clang-format: Fix space after argument comments.
Before:
  f(/*a=*/a, /*b=*/ ::b);

After:
  f(/*a=*/a, /*b=*/::b);

llvm-svn: 268879
2016-05-08 18:14:01 +00:00
Daniel Jasper a7900adf41 clang-format: Support enum type template arguments.
Before:
  template <enum E> class A { public : E *f(); };

After:
  template <enum E> class A {
  public:
    E *f();
  };

llvm-svn: 268878
2016-05-08 18:12:22 +00:00
Eric Liu 4cfb88a936 Added Fixer implementation and fix() interface in clang-format for removing redundant code.
Summary:
After applying replacements, redundant code like extra commas or empty namespaces
might be introduced. Fixer can detect and remove any redundant code introduced by replacements.
The current implementation only handles redundant commas.

Reviewers: djasper, klimek

Subscribers: ioeric, mprobst, klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D18551

llvm-svn: 267416
2016-04-25 15:09:22 +00:00
Martin Probst 5f8445b32a clang-format: [JS] generator and async functions.
For generators, see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_generators
async functions are not quite in the spec yet, but stage 3 and already widely used:
http://tc39.github.io/ecmascript-asyncawait/

Reviewers: djasper

Subscribers: klimek

Differential Revision: http://reviews.llvm.org/D19204

llvm-svn: 267368
2016-04-24 22:05:09 +00:00
Martin Probst 1e8261ea04 clang-format: [JS] support `interface` as a free standing identifier.
Summary:
`interface` can be used as a fee standing identifier in JavaScript/TypeScript.
This change uses the heuristic of whether it's followed by another identifier
as an indication.

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D19240

llvm-svn: 266789
2016-04-19 18:18:59 +00:00
Martin Probst 805c6167c2 Summary:
clang-format: [JS] unit tests for type aliases.

Also adds a test for "foo as bar" casts.

Spec:
https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.10

These are already handled correctly.

Reviewers: djasper

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D19206

llvm-svn: 266744
2016-04-19 14:59:16 +00:00
Martin Probst 053f1aa6d0 clang-format: [JS] simplify import/export.
Summary:
Change `import` and `export` parsing to special case the renaming
syntax (`import x, {y as bar} ...`, `export {x}`) and otherwise just
parse a regular structural element.

This simplifies the code a bit and should be more correct - it's easier
to recognise the specific import syntax than to recognise arbitrary
expressions and declarations.

Reviewers: djasper

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D19242

llvm-svn: 266743
2016-04-19 14:55:37 +00:00
Daniel Jasper c3ff0cd8a2 clang-format: Improve heuristics to detect function declarations/definitions.
Specifically understand ellipses in parameter lists and treat trailing
reference qualifiers and the "{" as signals.

llvm-svn: 266599
2016-04-18 11:31:21 +00:00
Marianne Mailhot-Sarrasin 03137c6538 clang-format: Last line in incomplete block is indented incorrectly
Indentation of the last line was reset to the initial indentation of the block when reaching EOF.

Patch by Maxime Beaulieu

Differential Revision: http://reviews.llvm.org/D19065

llvm-svn: 266321
2016-04-14 14:56:49 +00:00
Marianne Mailhot-Sarrasin 51fe279fbb clang-format: Implemented tab usage for continuation and indentation
Use tabs to fill whitespace at the start of a line.

Patch by Maxime Beaulieu

Differential Revision: http://reviews.llvm.org/D19028

llvm-svn: 266320
2016-04-14 14:52:26 +00:00
Martin Probst 716a533a7f clang-format: [JS] Test for parameter annotations.
Summary: Just to ensure no regressions, this already works fine.

Reviewers: djasper

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D18950

llvm-svn: 265922
2016-04-11 09:17:57 +00:00
Martin Probst bbffeac569 clang-format: [JS] do not insert semicolons after wrapped annotations.
Reviewers: djasper

Subscribers: klimek

Differential Revision: http://reviews.llvm.org/D18943

llvm-svn: 265916
2016-04-11 07:35:57 +00:00
Daniel Jasper 2cce7b728b clang-format: Fix label-in-if statement in macros where it is actually used.
Before:
  #define A \
    if (a)  \
    label:  \
    f()

After:
  #define A \
    if (a)  \
    label:  \
      f()

llvm-svn: 265557
2016-04-06 16:41:39 +00:00
Daniel Jasper 4060947289 clang-format: Support labels in brace-less ifs.
While I am not personally convinced about the usefulness of this
construct, we should break it.

Before:
  if (a) label:
  f();

After:
  if (a)
  label:
    f();

llvm-svn: 265545
2016-04-06 15:02:46 +00:00
Daniel Jasper 19bc1d007a clang-format: Fix incorrect function annotation detection.
Before:
  MACRO(
      abc).function() // wrap
      << abc;

After:
  MACRO(abc).function() // wrap
      << abc;

llvm-svn: 265540
2016-04-06 13:58:09 +00:00
Daniel Jasper 94b1bdf91a clang-format: Fix cast detection on "this".
Before:
  auto x = (X) this;

After:
  auto x = (X)this;

This fixes llvm.org/PR27198.

llvm-svn: 265385
2016-04-05 11:46:06 +00:00
Eric Liu 4c1ef97adb Added formatAndApplyAllReplacements that works on multiple files in libTooling.
Summary:
formatAndApplyAllReplacements takes a set of Replacements, applies them on a
Rewriter, and reformats the changed code.

Reviewers: klimek, djasper

Subscribers: ioeric, klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D17852

llvm-svn: 264745
2016-03-29 16:31:53 +00:00
Eric Liu c13ca6a950 Dsiable FormatStyle::GetStyleOfFile test case for mingw.
llvm-svn: 264289
2016-03-24 14:59:39 +00:00
Eric Liu 547d87912a Added support for different VFSs in format::getStyle. Disable platform-related test case for MS compilers to avoid breaking buildbot.
llvm-svn: 264277
2016-03-24 13:22:42 +00:00
Eric Liu 6b47faad53 Revert "Added support for different VFSs in format::getStyle."
This reverts commit r264253. It is breaking the buildbot http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast/builds/2203

llvm-svn: 264257
2016-03-24 11:25:28 +00:00
Eric Liu b72f6098df Added support for different VFSs in format::getStyle.
Summary:
Previously, format::getStyle assumes that the given file resides in
the real file system, which prevents the use of virtual file system in testing etc.
This patch adds a parameter in format::getStyle interface so that users can specify
the right file system. By default, the file system is the real file system.

Reviewers: djasper, klimek

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D18399

llvm-svn: 264253
2016-03-24 10:50:17 +00:00
Daniel Jasper 8fc7a1e992 clang-format: [JS] do not wrap ES6 imports/exports.
"import ... from '...';" and "export ... from '...';" should be treated
the same as goog.require/provide/module/forwardDeclare calls.

Patch by Martin Probst.

llvm-svn: 264055
2016-03-22 14:32:20 +00:00
Daniel Jasper 91b1d1ab6c clang-format: [JS] no space in union and intersection types.
The operators | and & in types, as opposed to the bitwise operators,
should not have whitespace around them (e.g. `Foo<Bar|Baz>`).

Patch by Martin Probst. Thank you.

llvm-svn: 263961
2016-03-21 17:57:31 +00:00
Daniel Jasper 9c8ff3551a clang-format: Make include sorting's main include detection configurable.
This patch adds a regular expression to configure suffixes of an
included file to check whether it is the "main" include of the current
file. Previously, clang-format has allowed arbitrary suffixes on the
formatted file, which is still the case when no IncludeMainRegex is
specified.

llvm-svn: 263943
2016-03-21 14:11:27 +00:00
Daniel Jasper 97439926e4 clang-format: [JS] Make requoting of JavaScript string literals only
change affected ranges.

llvm-svn: 263713
2016-03-17 13:03:41 +00:00
Daniel Jasper a4607e1b88 clang-format: [JS] Fix incorrect spacing around contextual keywords.
Before:
  x.of ();

After:
  x.of();

llvm-svn: 263710
2016-03-17 12:17:59 +00:00
Daniel Jasper 710f8493c8 clang-format: Slightly weaken AlignAfterOpenBracket=AlwaysBreak.
If a call takes a single argument, using AlwaysBreak can lead to lots
of wasted lines and additional indentation without improving the
readability in a significant way.

Before:
  caaaaaaaaaaaall(
      caaaaaaaaaaaall(
          caaaaaaaaaaaall(
              caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));

After:
  caaaaaaaaaaaall(caaaaaaaaaaaall(caaaaaaaaaaaall(
      caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));

llvm-svn: 263709
2016-03-17 12:00:22 +00:00
Daniel Jasper 1dcbbcfc5c clang-format: [JS] Handle certain cases of ASI.
Automatic Semicolon Insertion can only be properly handled by parsing
source code. However conservatively catching just a few, common
situations prevents breaking code during development, which greatly
improves usability.

JS code should still use semicolons, and ASI code should be flagged by
a compiler or linter.

Patch by Martin Probst. Thank you.

llvm-svn: 263470
2016-03-14 19:21:36 +00:00
Daniel Jasper acffeb8b63 clang-format: [JS] Support destructuring assignments in for loops.
Before:
  for (let { a, b } of x) {
  }

After:
  for (let {a, b} of x) {
  }

llvm-svn: 262776
2016-03-05 18:34:26 +00:00
Daniel Jasper 94a96fcebe clang-format: Use stable_sort when sorting #includes.
Otherwise, clang-format can output useless replacements in the presence
of identical #includes

llvm-svn: 262630
2016-03-03 17:34:14 +00:00
Daniel Jasper abd1f57453 clang-format: [JS] Optionally re-quote string literals.
Turns "foo" into 'foo' (or vice versa, depending on configuration).
This makes it more convenient to follow the Google JavaScript style
guide:
https://google.github.io/styleguide/javascriptguide.xml?showone=Strings#Strings

This functionality is behind the option "JavaScriptQuotes", which can be:

  * "leave" (no re-quoting)
  * "single" (change to single quotes)
  * "double" (change to double quotes)

This also changes single quoted JavaScript string literals to be treated
as tok::string_literal, not tok::char_literal, which fixes two unrelated
tests.

Patch by Martin Probst. Thank you.

llvm-svn: 262534
2016-03-02 22:44:03 +00:00