Commit Graph

136 Commits

Author SHA1 Message Date
Alexander Kornienko 3a33f0292b Implemented GNU-style formatting for compound statements.
Summary:
Added BraceBreakingStyle::BS_GNU. I'm not sure about the correctness of
static initializer formatting, but compound statements should be fine.

Reviewers: djasper

Reviewed By: djasper

CC: cfe-commits, klimek

Differential Revision: http://llvm-reviews.chandlerc.com/D2372

llvm-svn: 197138
2013-12-12 09:49:52 +00:00
Daniel Jasper 1556b59338 clang-format: Correctly handle Qt's Q_SLOTS.
This should fix llvm.org/PR17241. Maybe it sticks this time :-).

llvm-svn: 195953
2013-11-29 08:51:56 +00:00
Daniel Jasper 84c47a1074 clang-format: Support Qt's slot access specifiers.
This fixes llvm.org/PR17241.

llvm-svn: 195555
2013-11-23 17:53:41 +00:00
Daniel Jasper b631d5e6f3 clang-format: Recognize braced lists with trailing function call.
Before:
  int count = set<int> { f(), g(), h() }
      .size();

After:
  int count = set<int>{f(), g(), h()}.size();

llvm-svn: 195417
2013-11-22 07:26:53 +00:00
Alexander Kornienko 3cfa973978 Added an option to allow short function bodies be placed on a single line.
Summary:
The AllowShortFunctionsOnASingleLine option now controls short function
body placement on a single line independent of the BreakBeforeBraces option.
Updated tests using BreakBeforeBraces other than BS_Attach.

Addresses http://llvm.org/PR17888

Reviewers: klimek, djasper

Reviewed By: klimek

CC: cfe-commits, klimek

Differential Revision: http://llvm-reviews.chandlerc.com/D2230

llvm-svn: 195256
2013-11-20 16:33:05 +00:00
Manuel Klimek f6fd3d3fcf Remove incorrect assert.
If we run into the second preprocessor branch chain, the first branch
chain might have already set the maximum branch count on that level to
something > 0.

Fixes PR17645.

llvm-svn: 193153
2013-10-22 08:27:19 +00:00
Manuel Klimek b212f3baa1 Automatically munch semicolons after blocks.
While it is mostly a user error to have the extra semicolon,
formatting it graciously will correctly format in the cases
where we do not fully understand the code (macros).

llvm-svn: 192543
2013-10-12 22:46:56 +00:00
Daniel Jasper 53bd167c60 clang-format: Fix assertion on unterminated #ifs.
llvm-svn: 192535
2013-10-12 13:32:56 +00:00
Manuel Klimek 71814b4465 Support formatting of preprocessor branches.
We now correctly format:
  void SomeFunction(int param1,
  #ifdef X
                    NoTemplate param2,
  #else
                    template <
  #ifdef A
                        MyType<Some> >
  #else
                        Type1, Type2>
  #endif
                    param2,
  #endif
                    param3) {
    f();
  }

llvm-svn: 192503
2013-10-11 21:25:45 +00:00
Manuel Klimek 91e48586ae Fix incorrect detection of class definitions with alignas specification.
llvm-svn: 192094
2013-10-07 09:15:41 +00:00
Daniel Jasper f3d977979c clang-format: Detect braced lists in subscript expressions.
Before (even with Style.Cpp11BracedListStyle):
  f(MyMap[{ composite, key }]);

After:
  f(MyMap[{composite, key}]);

llvm-svn: 190678
2013-09-13 10:55:31 +00:00
Daniel Jasper 015ed028cf clang-format: Fix incorrect enum parsing / layouting.
Before:
  enum {
    Bar = Foo < int,
    int > ::value
  };

After:
  enum {
    Bar = Foo<int, int>::value
  };

llvm-svn: 190674
2013-09-13 09:20:45 +00:00
Daniel Jasper 786a550b9f clang-format: Format typed enums with nested names.
The explicit type specified for an enum can actually have a nested name
specifier.

This fixes llvm.org/PR17125.

llvm-svn: 190208
2013-09-06 21:32:35 +00:00
Daniel Jasper b4b9998713 clang-format: Don't confuse operator[] with lambdas.
Before:
  double &operator[](int i) { return 0; } int i;

After:
  double &operator[](int i) { return 0; }
  int i;

This fixes llvm.org/PR17134.

llvm-svn: 190207
2013-09-06 21:25:51 +00:00
Daniel Jasper 3b203a65c2 Add ATTRIBUTE_UNUSED to silence unused-function warning in release
builds.

llvm-svn: 190061
2013-09-05 16:05:56 +00:00
Manuel Klimek 9a53c7f40d Fixes PR 17106 (explicitly typed enums are formatted differently).
Before:
 enum X : int { A, B, C };

After:
 enum X : int {
   A,
   B,
   C
 };

llvm-svn: 190054
2013-09-05 15:34:55 +00:00
Daniel Jasper bf02b2c186 clang-format: Quickfix for braced init lists detected as lambdas.
Before:
  constexpr char hello [] { "hello" };

After:
  constexpr char hello[]{ "hello" };

llvm-svn: 190046
2013-09-05 11:49:39 +00:00
Daniel Jasper 9a8d48b5e1 clang-format: Fix parsing and indenting lambdas.
Before:
  void f() {
    other(x.begin(), x.end(), //
                         [&](int, int) { return 1; });
  }

After:
  void f() {
    other(x.begin(), x.end(), //
          [&](int, int) { return 1; });
  }

llvm-svn: 190039
2013-09-05 10:04:31 +00:00
Daniel Jasper 9fe0e8dacd clang-format: Enable formatting of nested blocks.
Among other things, this enables (better) formatting lambdas and
constructs like:
  MACRO({
          long_statement();
          long_statement_2();
        },
        {
          long_statement();
          long_statement_2();
        },
        { short_statement(); }, "");

This fixes llvm.org/PR15381.

llvm-svn: 190038
2013-09-05 09:29:45 +00:00
Manuel Klimek f017dc0156 Remove code duplication in unwrapped line parser.
llvm-svn: 189933
2013-09-04 13:34:14 +00:00
Manuel Klimek 516e054c05 Implement parsing of blocks (^{ ... }) in the unwrapped line parser.
This patch makes sure we produce the right number of unwrapped lines,
a follow-up patch will make the whitespace formatting consistent.

Before:
 void f() {
   int i = {[operation setCompletionBlock : ^{ [self onOperationDone];
 }]
 }
 ;
 }

After:
 void f() {
   int i = {[operation setCompletionBlock : ^{
     [self onOperationDone];
   }] };
 }

llvm-svn: 189932
2013-09-04 13:25:30 +00:00
Manuel Klimek bab25fdfa5 Fix layout of lambda captures.
Before:
 int c = [ &, &a, a]{
   [ =, c, &d]{
     return b++;
   }();
 }();

After:
 int c = [&, &a, a] {
   [=, c, &d] {
     return b++;
   }();
 }();

llvm-svn: 189924
2013-09-04 08:20:47 +00:00
Manuel Klimek ffdeb595ba First step towards correctly formatting lambdas.
Implements parsing of lambdas in the UnwrappedLineParser.
This introduces the correct line breaks; the formatting of
lambda captures are still incorrect, and the braces are also
still formatted as if they were braced init lists instead of
blocks.

llvm-svn: 189818
2013-09-03 15:10:01 +00:00
Daniel Jasper 7240762504 clang-format: Fix case-indentation in macros.
Before:
  #define OPERATION_CASE(name)           \
    case OP_name:                        \
    return operations::Operation##name

After:
  #define OPERATION_CASE(name)           \
    case OP_name:                        \
      return operations::Operation##name

llvm-svn: 189743
2013-09-02 08:26:29 +00:00
Daniel Jasper b715087278 clang-format: Improve recovery from enums with errors.
Before:
  namespace n {
  enum Type {
    One,
    Two, // missing };
    int i;
  } void g() {
  }

After:
  namespace n {
  enum Type {
    One,
    Two, // missing };
    int i;
  }
  void g() {}

llvm-svn: 189662
2013-08-30 10:10:19 +00:00
Daniel Jasper a15da3068d clang-format: Fix corner case in ObjC interface definitions.
In
  @implementation ObjcClass
  - (void)method;
  {
  }
  @end
the ObjC compiler seems to accept the superfluous comma after "method",
but clang-format used to assert on the subsequent "{".

This fixes llvm.org/PR16604.

llvm-svn: 189453
2013-08-28 08:04:23 +00:00
Daniel Jasper 65b79829c3 clang-format: Improve braced init list detection:
Before:
  std::this_thread::sleep_for(std::chrono::nanoseconds{
    std::chrono::seconds { 1 }
  } /
                              5);

After:
  std::this_thread::sleep_for(
      std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);

This fixes llvm.org/PR16554.

llvm-svn: 189451
2013-08-28 07:50:37 +00:00
Daniel Jasper 2b41a82e61 clang-format: Format enum struct/class like enum.
Patch by Joe Hermaszewski. Thank you!

llvm-svn: 188794
2013-08-20 12:42:50 +00:00
Manuel Klimek a027f306a6 Fixes a couple of bugs with the Allman brace breaking.
In particular, left braces after an enum declaration now occur on their
own line.  Further, when short ifs/whiles are allowed these no longer
cause the left brace to be on the same line as the if/while when a
brace is included.

Patch by Thomas Gibson-Robinson.

llvm-svn: 187901
2013-08-07 19:20:45 +00:00
Manuel Klimek d3ed59ae15 Implement Allman style.
Patch by Frank Miller.

llvm-svn: 187678
2013-08-02 21:31:59 +00:00
Daniel Jasper 65ee347285 clang-format: Add more options to namespace indentation.
With this patch, clang-format can be configured to:
* not indent in namespace at all (former behavior).
* indent in namespace as in other blocks.
* indent only in inner namespaces (as required by WebKit style).

Also fix alignment of access specifiers in WebKit style.

Patch started by Marek Kurdej. Thank you!

llvm-svn: 187540
2013-07-31 23:16:02 +00:00
Daniel Jasper 516d7971b3 clang-format: Fix switch/case interaction with macros.
Before:
  #define OPERATION_CASE(name) \
    case OP_name:              \
    return operations::Operation##name

  switch (OpCode) {
      CASE(Add);
      CASE(Subtract);
    default:
      return operations::Unknown;
  }

After:
  #define OPERATION_CASE(name) \
    case OP_name:              \
      return operations::Operation##name;

  switch (OpCode) {
    CASE(Add);
    CASE(Subtract);
    default:
      return operations::Unknown;
  }

llvm-svn: 187118
2013-07-25 11:31:57 +00:00
Daniel Jasper b1f74a8152 Fix alignment of closing brace in braced initializers.
Before:
someFunction(OtherParam, BracedList{
                           // comment 1 (Forcing intersting break)
                           param1, param2,
                           // comment 2
                           param3, param4
             });
After:
someFunction(OtherParam, BracedList{
                           // comment 1 (Forcing intersting break)
                           param1, param2,
                           // comment 2
                           param3, param4
                         });

To do so, the UnwrappedLineParser now stores the information about the
kind of brace in the FormatToken.

llvm-svn: 185914
2013-07-09 09:06:29 +00:00
Daniel Jasper ca7bd720eb Fix incorrect token counting introduced by r185319.
This lead to weird formatting.
Before:
DoSomethingWithVector({ {} /* No data */ }, {
  { 1, 2 }
});
After:
DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });

llvm-svn: 185346
2013-07-01 16:43:38 +00:00
Daniel Jasper 7f5d53e55f Fix braced-list detection in lieu of trailing comments.
Before:
DoSomethingWithVector({
} /* No data */);
After:
DoSomethingWithVector({} /* No data */);

llvm-svn: 185319
2013-07-01 09:15:46 +00:00
Craig Topper 69665e112b Put helper classes in an anonymous namespace.
llvm-svn: 185303
2013-07-01 04:21:54 +00:00
Nico Weber 9096fc0dab Run clang-format on lib/Format code after r184894. No other changes.
llvm-svn: 184896
2013-06-26 00:30:14 +00:00
Daniel Jasper 393564fdfe Improve clang-format's error recovery.
If a "}" is found inside parenthesis, this is probably a case of
missing parenthesis. This enables continuing to format after stuff code
like:

class A {
  void f(
};
..

llvm-svn: 183009
2013-05-31 14:56:29 +00:00
Daniel Jasper 5648cb32d9 Fix detection/formatting of braced lists in ternary expressions.
Before:
foo = aaaaaaaaaaa ? vector<int> {
  aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, aaaaa
}
: vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb, bbbbbbbbbbbbbbbbbbbb, bbbbb };

After:
foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,
                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }
                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,
                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };

llvm-svn: 182992
2013-05-31 10:09:55 +00:00
Daniel Jasper 41a0f78d43 Add return missing in r182855.
llvm-svn: 182856
2013-05-29 14:09:17 +00:00
Daniel Jasper 40e1921f2a Leave some macros on their own line
If an identifier is on its own line and it is all upper case, it is highly
likely that this is a macro that is meant to stand on a line by itself.

Before:
class A : public QObject {
  Q_OBJECT A() {}
};

Ater:
class A : public QObject {
  Q_OBJECT

  A() {}
};

llvm-svn: 182855
2013-05-29 13:16:10 +00:00
Daniel Jasper 12eba0a699 Remove obsolete variable as discovered in post-commit review.
llvm-svn: 182796
2013-05-28 19:11:43 +00:00
Daniel Jasper 1ec31065e8 Support uniform inits in braced lists.
This made it necessary to remove an error detection which would let us
bail out of braced lists in certain situations of missing "}". However,
as we always entirely escape from the braced list on finding ";", this
should not be a big problem.

With this, we can no format braced lists with uniformat inits:

return { arg1, SomeType { parameter } };

llvm-svn: 182788
2013-05-28 18:50:02 +00:00
Manuel Klimek 591ab5a830 Make UnwrappedLines and AnnotatedToken contain pointers to FormatToken.
The FormatToken is now not copyable any more.

llvm-svn: 182772
2013-05-28 13:42:28 +00:00
Manuel Klimek 15dfe7ac40 A first step towards giving format tokens pointer identity.
With this patch, we create all tokens in one go before parsing and pass
an ArrayRef<FormatToken*> to the UnwrappedLineParser. The
UnwrappedLineParser is switched to use pointer-to-token internally.

The UnwrappedLineParser still copies the tokens into the UnwrappedLines.
This will be fixed in an upcoming patch.

llvm-svn: 182768
2013-05-28 11:55:06 +00:00
Alexander Kornienko f2e021233c Ignore contents of #if 0 blocks.
Summary:
Added stack of preprocessor branching directives, and ignore all tokens
inside #if 0 except for preprocessor directives.

Reviewers: klimek, djasper

Reviewed By: klimek

CC: cfe-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D855

llvm-svn: 182658
2013-05-24 18:24:24 +00:00
Daniel Jasper d443239493 Increase test coverage for braced init lists.
Also fix a minor bug for constructor initializers with braced init lists.

llvm-svn: 182601
2013-05-23 18:29:16 +00:00
Manuel Klimek 5c24cca0f0 Use a SourceRange for the whitespace location in FormatToken.
Replaces the use of WhitespaceStart + WhitspaceLength.
This made a bug in the formatter obvous where we would incorrectly
calculate the next column.

FIXME: There's a similar bug left regarding TokenLength. We should
probably also move to have a TokenRange instead.

llvm-svn: 182572
2013-05-23 10:56:37 +00:00
Manuel Klimek ab41991c07 Expand parsing of braced init lists.
Allows formatting of C++11 braced init list constructs, like:
vector<int> v { 1, 2, 3 };
f({ 1, 2 });

This involves some changes of how tokens are handled in the
UnwrappedLineFormatter. Note that we have a plan to evolve the
design of the token flow into one where we create all tokens
up-front and then annotate them in the various layers (as we
currently already have to create all tokens at once anyway, the
current abstraction does not help). Thus, this introduces
FIXMEs towards that goal.

llvm-svn: 182568
2013-05-23 09:41:43 +00:00
Daniel Jasper d2ae41a7c6 Remove diagnostics from clang-format.
We only ever implemented one and that one is not actually all that
helpful (e.g. gets incorrectly triggered by macros).

llvm-svn: 181871
2013-05-15 08:14:19 +00:00