Commit Graph

628 Commits

Author SHA1 Message Date
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
Craig Topper 411294d353 Use LLVM_DELETED_FUNCTION on unimplemented copy constructor and assignment operator.
llvm-svn: 185302
2013-07-01 04:07:34 +00:00
Craig Topper 318ed7c1a2 Put helper classes in an anonymous namespace.
llvm-svn: 185301
2013-07-01 04:03:19 +00:00
Craig Topper bfb5c40bee Use static for helper functions instead of an anonymous namespace per coding standards.
llvm-svn: 185300
2013-07-01 03:38:29 +00:00
Craig Topper 6862dcc6da Make string pointer const.
llvm-svn: 185296
2013-06-30 22:44:02 +00:00
Craig Topper af35e8521a Put helper classes into anonymous namespace.
llvm-svn: 185295
2013-06-30 22:29:28 +00:00
Alexander Kornienko 1e80887d63 Use lexing mode based on FormatStyle.Standard.
Summary:
Some valid pre-C++11 constructs change meaning when lexed in C++11
mode, e.g.
#define x(_a) printf("foo"_a);
(example from http://llvm.org/bugs/show_bug.cgi?id=16342). "foo"_a is treated as
a user-defined string literal when parsed in C++11 mode.
In order to deal with this correctly, we need to set lexing mode according to
which standard the code conforms to. We already have a configuration value for
this (FormatStyle.Standard), which seems to be appropriate to use in this case
as well.

Reviewers: klimek

CC: cfe-commits, gribozavr

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

llvm-svn: 185149
2013-06-28 12:51:24 +00:00
Nico Weber f579ab302a Fix a comment.
llvm-svn: 184905
2013-06-26 02:42:46 +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
Nico Weber 8084cffd0b Formatter: Don't put a space after parameter-naming block comments.
Before: f(a, b, /*doFoo=*/ false);
Now: f(a, b, /*doFoo=*/false);

This style is a lot more common:
$ ack -H '=\*\/\w' lib | wc -l 
    1281
$ ack -H '=\*\/ \w' lib | wc -l 
      70

llvm-svn: 184894
2013-06-26 00:15:19 +00:00
Nico Weber 0f987a6fb3 Fix a comment.
llvm-svn: 184873
2013-06-25 19:25:12 +00:00
Nico Weber ec9e41076b Formatter/ObjC: Correctly format casts in objc message send expressions.
llvm-svn: 184804
2013-06-25 00:55:57 +00:00
Manuel Klimek 836c2868f9 Add an option to not indent declarations when breaking after the type.
Make that option the default for LLVM style.

llvm-svn: 184563
2013-06-21 17:25:42 +00:00
Alexander Kornienko b93062e236 Use the same set of whitespace characters for all operations in BreakableToken.
Summary:
Fixes a problem where \t,\v or \f could lead to a crash when placed as
a first character in a line comment. The cause is that rtrim and ltrim handle
these characters, but our code didn't, so some invariants could be broken.

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 184425
2013-06-20 13:58:37 +00:00
Alexander Kornienko a3555e2416 Fixed long-standing issue with incorrect length calculation of multi-line comments.
Summary:
A trailing block comment having multiple lines would cause extremely
high penalties if the summary length of its lines is more than the column limit.
Fixed by always considering only the last line of a multi-line block comment.
Removed a long-standing FIXME from relevant tests and added a motivating test
modelled after problem cases from real code.

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 184340
2013-06-19 19:50:11 +00:00
Alexander Kornienko 7285207486 Split long strings on word boundaries.
Summary: Split strings at word boundaries, when there are no spaces and slashes.

Reviewers: klimek

CC: cfe-commits

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

llvm-svn: 184304
2013-06-19 14:22:47 +00:00
Alexander Kornienko afaa8f556b Fix a problem in ExpressionParser leading to trailing comments affecting indentation of an expression after a line break.
Summary:
E.g. the second line in 

return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
           b; //

is indented 4 characters more than in

return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
       b;

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 184078
2013-06-17 13:19:53 +00:00
Alexander Kornienko 4d26b6efef Fixes incorrect indentation of line comments after break and re-alignment.
Summary:
Selectively propagate the information about token kind in
WhitespaceManager::replaceWhitespaceInToken.For correct alignment of new
segments of line comments in order to align them correctly. Don't set
BreakBeforeParameter in breakProtrudingToken for line comments, as it introduces
a break after the _next_ parameter. Added tests for related functions.

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 184076
2013-06-17 12:59:44 +00:00
Alexander Kornienko be633908be Don't remove backslashes from block comments.
Summary:
Don't remove backslashes from block comments. Previously this
/* \    \ \ \ \ \
*/
would be turned to this:
/*
*/
which spoils some kinds of ASCII-art, people use in their comments. The behavior
was related to handling escaped newlines in block comments inside preprocessor
directives. This patch makes handling it in a more civilized way.

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 183978
2013-06-14 11:46:10 +00:00
Alexander Kornienko f370ad9055 Preserve newlines before block comments in static initializers.
Summary:
Basically, don't special-case line comments in this regard. And fixed
an incorrect test, that relied on the wrong behavior.

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 183851
2013-06-12 19:04:12 +00:00
Alexander Kornienko 555efc36d0 Insert a space at the start of a line comment in case it starts with an alphanumeric character.
Summary:
"//Test" becomes "// Test". This change is aimed to improve code
readability and conformance to certain coding styles. If a comment starts with a
non-alphanumeric character, the space isn't added, e.g. "//-*-c++-*-" stays
unchanged.

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 183750
2013-06-11 16:01:49 +00:00
Alexander Kornienko ee4ca9ba0e Improved handling of escaped newlines at the token start.
Summary: Remove them from the TokenText as well.

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 183536
2013-06-07 17:45:07 +00:00
Alexander Kornienko dd7ece53a2 Fixed calculation of penalty when breaking tokens.
Summary:
Introduced two new style parameters: PenaltyBreakComment and
PenaltyBreakString. Add penalty for each character of a breakable token beyond
the column limit (this relates mainly to comments, as they are broken only on
whitespace). Tuned PenaltyBreakComment to prefer comment breaking over breaking
inside most binary expressions.
Fixed a bug that prevented *, & and && from being considered TT_BinaryOperator
in the presense of adjacent comments.

Reviewers: klimek, djasper

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 183530
2013-06-07 16:02:52 +00:00
Daniel Jasper 61a40782df Fix incorrect line breaking before trailing block comments.
Before, clang-format would happily move a trailing block comment to a
new line, which normally changes the perceived binding of that comment.

E.g., it would move:
void f() { /* comment */
  ...
}
to:
void f() {
  /* comment */
  ...
}

llvm-svn: 183420
2013-06-06 16:08:57 +00:00
Daniel Jasper 6dcecb6b33 Fix clang-format's expression parser for leading }s.
The leading "}" in the construct "} else if (..) {" was confusing the
expression parser. Thus, no fake parentheses were generated and the
indentation was broken in some cases.

llvm-svn: 183393
2013-06-06 09:11:58 +00:00
Daniel Jasper 058663787e Improve c-style cast detection.
Before:
return (my_int) aaaa;
template <> void f<int>(int i)SOME_ANNOTATION;
f("aaaa" SOME_MACRO(aaaa)"aaaa");

After:
return (my_int)aaaa;
template <> void f<int>(int i) SOME_ANNOTATION;
f("aaaa" SOME_MACRO(aaaa) "aaaa");

llvm-svn: 183389
2013-06-06 08:20:20 +00:00
Alexander Kornienko ffcc010767 UTF-8 support for clang-format.
Summary:
Detect if the file is valid UTF-8, and if this is the case, count code
points instead of just using number of bytes in all (hopefully) places, where
number of columns is needed. In particular, use the new
FormatToken.CodePointCount instead of TokenLength where appropriate.
Changed BreakableToken implementations to respect utf-8 character boundaries
when in utf-8 mode.

Reviewers: klimek, djasper

Reviewed By: djasper

CC: cfe-commits, rsmith, gribozavr

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

llvm-svn: 183312
2013-06-05 14:09:10 +00:00
Alexander Kornienko 4b67207157 Moved FormatToken to a separate header.
llvm-svn: 183115
2013-06-03 16:45:03 +00:00
Daniel Jasper 1027c6e5dd Let clang-format remove empty lines before "}".
These lines almost never aid readability.

Before:
void f() {
  int i;  // some variable

}

After:
void f() {
  int i;  // some variable
}

llvm-svn: 183112
2013-06-03 16:16:41 +00:00
Daniel Jasper 8050395236 Improve detection preventing certain kind of formatting patterns.
An oversight in this detection made clang-format unable to format
the following nicely:
void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaa,
                         bbbbbbbbbbbbbbbbbbbbbbbbbb>(
    cccccccccccccccccccccccccccc);

llvm-svn: 183097
2013-06-03 09:54:46 +00:00
Daniel Jasper 68d888cfed Fix line-breaking problem caused by comment.
Before, clang-format would not find a solution for formatting:
if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||
     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa
    cccccc) {
}

llvm-svn: 183096
2013-06-03 08:42:05 +00:00
Daniel Jasper d589391d07 Improve recognition of template parameters.
Before: return a<b &&c> d;
After:  return a < b && c > d;
llvm-svn: 183077
2013-06-01 18:56:00 +00:00
Daniel Jasper da6f225ef9 Improve clang-format's c-style cast detection.
Before:
  x[(uint8) y];
  x = (uint8) y;
  void f() { x = (uint8) y; }
  #define AA(X) sizeof(((X *) NULL)->a)

After:
  x[(uint8)y];
  x = (uint8)y;
  void f() { x = (uint8)y; }
  #define AA(X) sizeof(((X *)NULL)->a)

llvm-svn: 183014
2013-05-31 16:14:28 +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 a9eb2aafa1 Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.

The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
  inserted while still working on the code. If they are on two lines,
  inserting code is easier. Also, overlooking the "{}" of an
  "if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
  them on one line saves vertical space at no loss of readability.

llvm-svn: 183008
2013-05-31 14:56:20 +00:00
Daniel Jasper 2c611c0341 Properly format nested conditional operators.
Before:
bool aaaaaa = aaaaaaaaaaaaa //
                  ? aaaaaaaaaaaaaaa
                  : bbbbbbbbbbbbbbb //
                  ? ccccccccccccccc
                  : ddddddddddddddd;

After:
bool aaaaaa = aaaaaaaaaaaaa //
                  ? aaaaaaaaaaaaaaa
                  : bbbbbbbbbbbbbbb //
                        ? ccccccccccccccc
                        : ddddddddddddddd;

llvm-svn: 183007
2013-05-31 14:56:12 +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 ce257f296b More fixes for clang-format's multiline comment breaking.
llvm-svn: 182940
2013-05-30 17:27:48 +00:00
Daniel Jasper 58dd2f0652 Fix another clang-format crasher related to multi-line comments.
This fixes:
/*
*
* something long going over the column limit.
*/

llvm-svn: 182932
2013-05-30 15:20:29 +00:00
Manuel Klimek 8910d192d0 Add asserts to guard against regressions.
llvm-svn: 182916
2013-05-30 07:45:53 +00:00
Daniel Jasper 51fb2b2151 Fix crasher when formatting certain block comments.
Smallest reproduction:
/*
**
*/

llvm-svn: 182913
2013-05-30 06:40:07 +00:00
Manuel Klimek ae1fbfb740 Fixes error when splitting block comments.
When trying to fall back to search from the end onwards, we
would still find leading whitespace if the leading whitespace
went on after the end of the line.

llvm-svn: 182886
2013-05-29 22:06:18 +00:00
Manuel Klimek 4c5c28bb36 Use a non-recursive implementation to reconstruct line breaks.
Now that the TokenAnnotator does not require stack space anymore,
reconstructing the lines has become the limiting factor. This patch
fixes that problem, allowing large files with multiple megabytes of
single unwrapped lines to be formatted.

llvm-svn: 182861
2013-05-29 15:10:11 +00:00
Manuel Klimek 6e6310ec84 The second step in the token refactoring.
Gets rid of AnnotatedToken, putting everything into FormatToken.
FormatTokens are created once, and only referenced by pointer.  This
enables multiple future features, like having tokens shared between
multiple UnwrappedLines (while there's still work to do to fully enable
that).

llvm-svn: 182859
2013-05-29 14:47:47 +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 61e6bbf850 Add option to always break template declarations.
With option enabled (e.g. in Google-style):
template <typename T>
void f() {}

With option disabled:
template <typename T> void f() {}

Enabling this for Google-style and Chromium-style, not sure which other
styles would prefer that.

llvm-svn: 182849
2013-05-29 12:07:31 +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
Daniel Jasper 4d03d3b327 Fix formatting regression regarding pointers to arrays.
Before: f( (*PointerToArray)[10]);
After:  f((*PointerToArray)[10]);

This fixes llvm.org/PR16163

llvm-svn: 182777
2013-05-28 15:27:10 +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
Daniel Jasper bca4bbe30a Initial support for designated initializers.
llvm-svn: 182767
2013-05-28 11:30:49 +00:00
Manuel Klimek 34d15151c4 Disable tab expansion when counting the columns in block comments.
To fully support this, we also need to expand tabs in the text before
the block comment. This patch breaks indentation when there was a
non-standard mixture of spaces and tabs used for indentation, but
fixes a regression in the simple case:
{
  /*
   * Comment.
   */
  int i;
}
Is now formatted correctly, if there were tabs used for indentation
before.

llvm-svn: 182760
2013-05-28 10:01:59 +00:00
Manuel Klimek 281dcbe026 Fixes indentation of empty lines in block comments.
Block comment indentation of empty lines regressed, as we did not
have a test for it.
 /* Comment with...
  *
  * empty line. */
is now formatted correctly again.

llvm-svn: 182757
2013-05-28 08:55:01 +00:00
Daniel Jasper 3719428c06 Clean up formatting of function types.
Before:
int (*func)(void*);
void f() { int(*func)(void*); }

After (consistent space after "int"):
int (*func)(void*);
void f() { int (*func)(void*); }

llvm-svn: 182756
2013-05-28 08:33:00 +00:00
Daniel Jasper 9f82df295e Fix formatting of expressions containing ">>".
This gets turned into two ">" operators at the beginning in order to
simplify template parameter handling. Thus, we need a special case to
handle those two binary operators correctly.

With this patch, clang-format can now correctly handle cases like:
aaaaaa = aaaaaaa(aaaaaaa, // break
                 aaaaaa) >>
         bbbbbb;

llvm-svn: 182754
2013-05-28 07:42:44 +00:00
David Blaikie 8f6a2972ce Remove unreachable return
llvm-svn: 182742
2013-05-27 20:43:54 +00:00
Daniel Jasper 1eff9080af Improve formatting of templates.
Before: A < int&& > a;
After:  A<int &&> a;

Also remove obsolete FIXMEs.

llvm-svn: 182741
2013-05-27 16:36:33 +00:00
Manuel Klimek 9043c74f49 Major refactoring of BreakableToken.
Unify handling of whitespace when breaking protruding tokens with other
whitespace replacements.

As a side effect, the BreakableToken structure changed significantly:
- have a common base class for single-line breakable tokens, as they are
  much more similar
- revamp handling of multi-line comments; we now calculate the
  information about lines in multi-line comments similar to normal
  tokens, and always issue replacements

As a result, we were able to get rid of special casing of trailing
whitespace deletion for comments in the whitespace manager and the
BreakableToken and fixed bugs related to tab handling and escaped
newlines.

llvm-svn: 182738
2013-05-27 15:23:34 +00:00
Daniel Jasper 7b27a10b1e Improve indentation of assignments.
Before:
unsigned OriginalStartColumn = SourceMgr.getSpellingColumnNumber(
    Current.FormatTok.getStartOfNonWhitespace()) -
                               1;

After:
unsigned OriginalStartColumn =
    SourceMgr.getSpellingColumnNumber(
        Current.FormatTok.getStartOfNonWhitespace()) -
    1;

llvm-svn: 182733
2013-05-27 12:45:09 +00:00
Manuel Klimek 75081b5cf8 Address post-review comment from dblakie.
llvm-svn: 182732
2013-05-27 12:36:28 +00:00
Daniel Jasper 32a796bc5b Fix hacky way of preventing a certain type of line break.
In general, we like to avoid line breaks like:

  ...
  SomeParameter, OtherParameter).DoSomething(
  ...

as they tend to make code really hard to read (how would you even indent the
next line?). Previously we have implemented this in a hacky way, which has now
shown to lead to problems. This fixes a few weird looking formattings, such as:

Before:
aaaaa(
    aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
        .aaaaa(aaaaa),
    aaaaaaaaaaaaaaaaaaaaa);
After:
aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),
      aaaaaaaaaaaaaaaaaaaaa);

llvm-svn: 182731
2013-05-27 11:50:16 +00:00
Nico Weber ced7d41b51 Formatter/ObjC: In dictionary literals, break after ':', not before it.
Before:
  @{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
     : regularFont, };

Now:
  @{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :
     regularFont, };

':'s in dictionary literals (and the corresponding {}s) are now marked as
TT_ObjCDictLiteral too, which makes further improvements to dict literal
layout possible.

llvm-svn: 182716
2013-05-26 05:39:26 +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 f632f69284 More tests and a fix for braced init lists.
Before: f(new vector<int> { 1, 2, 3 });
After:  f(new vector<int>{ 1, 2, 3 });
llvm-svn: 182614
2013-05-23 21:35:49 +00:00
Manuel Klimek 75ef31f607 Fix aligning of comments.
Previously we started sequences to align for single line comments when
the previous line had a trailing comment, but the sequence was broken
for other reasons.

Now we re-format:
// a
 // b
f(); // c
to:
// a
// b
f(); // c

llvm-svn: 182608
2013-05-23 20:46:07 +00:00
Manuel Klimek b27375fcd7 Fix aligning of comments that are at the start of the line.
Now correctly leaves:
f(); // comment
// comment
g(); // comment
... alone if the middle comment was aligned with g() before formatting.

llvm-svn: 182605
2013-05-23 19:54:43 +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
Daniel Jasper 5bd0b9e53b Improve formatting of braced lists.
Before: vector<int> v{ -1};
After:  vector<int> v{-1};
llvm-svn: 182597
2013-05-23 18:05:18 +00:00
Manuel Klimek c573080d45 Stop aligning trailing comments which are aligned with the next line.
Previously we would align:
f(); // comment
     // other comment
g();

Even if // other comment was at the start of the line. Now we do not
align trailing comments if they have been already aligned correctly
with the next line.

Thus,
f(); // comment
// other comment
g();
will not be changed, while:
f(); // comment
  // other commment
g();
will lead to the two trailing comments being aligned.

llvm-svn: 182577
2013-05-23 11:42:52 +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
Daniel Jasper e5777d25d6 Improve formatting of braced lists.
Before:
vector<int> x { 1, 2, 3 };
After:
vector<int> x{ 1, 2, 3 };

Also add a style option to remove the spaces inside braced lists,
so that the above becomes:
std::vector<int> v{1, 2, 3};

llvm-svn: 182570
2013-05-23 10:15:45 +00:00
Manuel Klimek 6734592c12 Fix no-assert compiles.
llvm-svn: 182569
2013-05-23 10:02:51 +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
Manuel Klimek 05c6789b00 Fix uninitialized access error found by valgrind.
llvm-svn: 182476
2013-05-22 14:01:08 +00:00
Manuel Klimek 4fe43002f8 Makes whitespace management more consistent.
Instead of selectively storing some changes and directly generating
replacements for others, we now notify the WhitespaceManager of the
whitespace before every token (and optionally with more changes inside
tokens).

Then, we run over all whitespace in the very end in original source
order, where we have all information available to correctly align
comments and escaped newlines.

The future direction is to pull more of the comment alignment
implementation that is now in the BreakableToken into the
WhitespaceManager.

This fixes a bug when aligning comments or escaped newlines in unwrapped
lines that are handled out of order:
  #define A \
    f({     \
      g();  \
    });
... now gets correctly layouted.

llvm-svn: 182467
2013-05-22 12:51:29 +00:00
Daniel Jasper a4c571cd1c Improve clang-format's stream formatting.
clang-format was a bit too aggressive when trying to keep labels and
values on the same line.

Before:
    llvm::outs()
         << "aaaaaaaaaaaaaaaaaaa: " << aaaaaaaaaaaaa(
                                           aaaaaaaaaaaaaaaaaaaaaaaaaaaa);
After:
  llvm::outs() << "aaaaaaaaaaaaaaaaaaa: "
               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);

llvm-svn: 182458
2013-05-22 09:13:26 +00:00
Daniel Jasper 53e8d854fd Fix function declaration behavior.
This only affects styles that prevent bin packing. There, a break after
a template declaration also forced a line break after the function name.

Before:
template <class SomeType, class SomeOtherType>
SomeType
SomeFunction(SomeType Type, SomeOtherType OtherType) {}

After:
template <class SomeType, class SomeOtherType>
SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}

This fixes llvm.org/PR16072.

llvm-svn: 182457
2013-05-22 08:55:55 +00:00
Daniel Jasper 13c37b3483 Improve handling of trailing 'const'.
Reduce the preference for breaking before a trailing 'const' according
to review comments on r182362.

llvm-svn: 182455
2013-05-22 08:28:26 +00:00
Daniel Jasper f8114cf621 Cut-off clang-format analysis.
If clang-format is confronted with long and deeply nested lines (e.g.
complex static initializers or function calls), it can currently try too
hard to find the optimal solution and never finish. The reason is that
the memoization does not work effectively for deeply nested lines.

This patch removes an earlier workaround and instead opts for
accepting a non-optimal solution in rare cases. However, it only does
so only in cases where it would have to analyze an excessive number of
states (currently set to 10000 - the most complex line in Format.cpp
requires ~800 states) so this should not change the behavior in a
relevant way.

llvm-svn: 182449
2013-05-22 05:27:42 +00:00
Daniel Jasper 5be31f728a Allow breaking before a trailing const.
Before:
void someLongFunction(
    int someLongParameter) const;

After:
void someLongFunction(int someLongParameter)
    const;

Also slightly cleanup tests.

llvm-svn: 182362
2013-05-21 09:16:31 +00:00
Alexander Kornienko 06e0033427 Minor fix: don't crash on empty configuration file, consider empty configuration files invalid.
llvm-svn: 182290
2013-05-20 15:18:01 +00:00
Alexander Kornienko 006b5c89ce Clang-format: allow -style="{yaml/json}" on command line
Summary: + improved handling of default style and predefined styles.

Reviewers: djasper, klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 182205
2013-05-19 00:53:30 +00:00
Daniel Jasper 473c62c485 Slightly modify the formatting rules for braced lists.
Basically, the new rule is: The opening "{" always has to be on the
same line as the first element if the braced list is nested
(e.g. in another braced list or in a function).

The solution that clang-format produces almost always adheres to this
rule anyway and this makes clang-format significantly faster for larger
lists. Added a test cases for the only exception I could find
(which doesn't seem to be very important at first sight).

llvm-svn: 182082
2013-05-17 09:35:01 +00:00
Daniel Jasper 8bb99e8911 Don't insert a break into include lines with trailing comments.
llvm-svn: 182003
2013-05-16 12:59:13 +00:00
Daniel Jasper 3a685df7e0 Add option to put short loops on a single line.
This enables things like:

for (int &v : vec) v *= 2;

Enabled for Google style.

llvm-svn: 182000
2013-05-16 12:12:21 +00:00
Daniel Jasper ec04c0dba0 Add a more convenient interface to use clang-format.
It turns out that several implementations go through the trouble of
setting up a SourceManager and Lexer and abstracting this into a
function makes usage easier.

Also abstracts SourceManager-independent ranges out of
tooling::Refactoring and provides a convenience function to create them
from line ranges.

llvm-svn: 181997
2013-05-16 10:40:07 +00:00
Daniel Jasper f9eb9b1883 Comments should not prevent single-line functions.
Before:
void f() {}
void g() {
} // comment

After:
void f() {}
void g() {} // comment

llvm-svn: 181996
2013-05-16 10:17:39 +00:00
Daniel Jasper 7dd22c51b1 Add back accidentally deleted line and add test for it.
Before:
f("a", "b"
  "c");
After:
f("a", "b"
       "c");

llvm-svn: 181980
2013-05-16 04:26:02 +00:00
Daniel Jasper abca58c9ee Don't put short namespace on a single line.
Before:
namespace abc { class SomeClass; }
namespace def { void someFunction() {} }

After:
namespace abc {
class Def;
}
namespace def {
void someFunction() {}
}

Rationale:
a) Having anything other than forward declaration on the same line
   as a namespace looks confusing.
b) Formatting namespace-forward-declaration-combinations different
   from other stuff is inconsistent.
c) Wasting vertical space close to such forward declarations really
   does not affect readability.

llvm-svn: 181887
2013-05-15 14:09:55 +00:00
Daniel Jasper 6f05e59f23 Improve recognition of template definitions.
In the long run, this will probably be better fixed by a proper
expression parser..

Before:
  template <typename F>
  Matcher(const Matcher<F> & Other,
          typename enable_if_c < is_base_of<F, T>::value &&
              !is_same<F, T>::value > ::type * = 0)
      : Implementation(new ImplicitCastMatcher<F>(Other)) {}

After:
  template <typename F>
  Matcher(const Matcher<F> & Other,
          typename enable_if_c<is_base_of<F, T>::value &&
                               !is_same<F, T>::value>::type * = 0)
      : Implementation(new ImplicitCastMatcher<F>(Other)) {}

llvm-svn: 181884
2013-05-15 13:46:48 +00:00
Daniel Jasper c6fbc2192c Break function declarations after multi-line return types.
Before:
template <typename A>
SomeLoooooooooooooooooooooongType<
    typename some_namespace::SomeOtherType<A>::Type> Function() {}

After:
template <typename A>
SomeLoooooooooooooooooooooongType<
    typename some_namespace::SomeOtherType<A>::Type>
Function() {}

llvm-svn: 181877
2013-05-15 09:35:08 +00:00
Daniel Jasper 00aca707d5 Don't merge one-line functions in weird brace styles.
llvm-svn: 181872
2013-05-15 08:30:06 +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
Daniel Jasper 5dad58e9df Improve formatting of function types.
The function type detection in r181438 and r181764 detected function
types too eagerly. This led to inconsistent formatting of inline
assembly and (together with r181687) to an incorrect formatting of calls
in macros.

Before: #define DEREF_AND_CALL_F(parameter) f (*parameter)
After:  #define DEREF_AND_CALL_F(parameter) f(*parameter)
llvm-svn: 181870
2013-05-15 07:51:51 +00:00
Daniel Jasper 571f1af0bb Fix expression breaking for one-parameter-per-line styles.
Before:
  if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||
      aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||
      aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||
      aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
After:
  if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||
      aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||
      aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}

llvm-svn: 181828
2013-05-14 20:39:56 +00:00
Daniel Jasper 8187c76db5 Fix uninitialized value bug found by valgrind.
llvm-svn: 181779
2013-05-14 10:44:17 +00:00
Daniel Jasper cdd0662b4e Correctly determine ranges for clang-format.
We have been assuming that CharSourceRange::getTokenRange() by itself
expands a range until the end of a token, but in fact it only sets
IsTokenRange to true. Thus, we have so far only considered the first
character of the last token to belong to an unwrapped line. This
did not really manifest in symptoms as all edit integrations
expand ranges to fully lines.

llvm-svn: 181778
2013-05-14 10:31:09 +00:00
Daniel Jasper 12f9d8ef43 Fix clang-format bug in unwrapped-line merging.
Before (in styles that allow it), clang-format would not merge an
if statement onto a single line, if only the second line was format
(e.g. in an editor integration):

if (a)
  return; // clang-format invoked on this line.

With this patch, this gets properly merged to:

if (a) return; // ...

llvm-svn: 181770
2013-05-14 09:30:02 +00:00
Manuel Klimek 5ecb5fd7b2 Implement string literal breaking on unbreakable token sequences.
This fixes indentation where there are for example multiple closing
parentheses after a string literal, and where those parentheses
run over the end of the line.

During testing this revealed a bug in the implementation of
breakProtrudingToken: we don't want to change the state if we didn't
actually do anything.

llvm-svn: 181767
2013-05-14 09:04:24 +00:00
Daniel Jasper abc342148f Don't format sizeof/alignof as function types.
Before: A<sizeof (*x)> a;
After:  A<sizeof(*x)> a;
llvm-svn: 181764
2013-05-14 08:34:47 +00:00
Daniel Jasper 47a04442f9 Align a multiline string literal with the first part.
Before:
  #define A(X)          \
    "aaaaa" #X "bbbbbb" \
               "ccccc"

After:
  #define A(X)          \
    "aaaaa" #X "bbbbbb" \
    "ccccc"

llvm-svn: 181732
2013-05-13 20:50:15 +00:00
Alexander Kornienko 9a38ec2658 A better version of r181699: use raw_string_ostream.str() instead of manually calling .flush().
llvm-svn: 181702
2013-05-13 12:56:35 +00:00
Manuel Klimek c1d83f472e Fix style according to post-commit review comments.
llvm-svn: 181701
2013-05-13 12:53:04 +00:00
Manuel Klimek a8eb914978 Implements brace breaking styles.
We now support "Linux" and "Stroustrup" brace breaking styles, which
gets us one step closer to support formatting WebKit, KDE & Linux code.

Linux brace breaking style:
namespace a
{
class A
{
  void f()
  {
    if (x) {
      f();
    } else {
      g();
    }
  }
}
}

Stroustrup brace breaking style:
namespace a {
class A {
  void f()
  {
    if (x) {
      f();
    } else {
      g();
    }
  }
}
}

llvm-svn: 181700
2013-05-13 12:51:40 +00:00
Alexander Kornienko 31f75e3f0a Fixes [Bug 15960] YAMLTraits doesn't roundtrip on Windows.
Thanks to Kim Gräsman for help!

llvm-svn: 181699
2013-05-13 12:41:08 +00:00
Manuel Klimek b9eae4c1d7 Implements UseTab for clang-format.
This is required for kernel linux kernel style formatting.

llvm-svn: 181693
2013-05-13 09:22:11 +00:00
Daniel Jasper cc3044cf46 Further improve optimization for nested calls.
Fake parentheses (i.e. emulated parentheses used to correctly handle
binary expressions) used to prevent the optimization implemented in
r180264.

llvm-svn: 181692
2013-05-13 09:19:24 +00:00
Manuel Klimek 13b97d8b82 Implements IndentWidth.
This is required for various styles that are for example based on
8-indent.

llvm-svn: 181690
2013-05-13 08:42:42 +00:00
Daniel Jasper b910bbb87a Assume macros to contain declarations.
This seems to be the vastly more common case. If we find enough
examples to the contrary, we can make it smarter.

Before: #define MACRO void f(int * a)
After:  #define MACRO void f(int *a)
llvm-svn: 181687
2013-05-13 07:14:40 +00:00
Daniel Jasper 66e4f83c07 When breaking at function calls, indent from function name.
Otherwise (when indenting from the wrapped -> or .), this looks
like a confusing indent.

Before:
aaaaaaa        //
    .aaaaaaa( //
        aaaaaaa);
After:
aaaaaaa        //
    .aaaaaaa( //
         aaaaaaa);

llvm-svn: 181595
2013-05-10 13:37:16 +00:00
Daniel Jasper 1cb530f1e0 Always format entire macro definitions.
Thereby, the macro is consistently formatted (including the trailing
escaped newlines) even if clang-format is invoked only on single lines
of the macro.

llvm-svn: 181590
2013-05-10 13:00:49 +00:00
Alexander Kornienko 49149677d9 Config file support for clang-format, part 2.
Summary:
Adds actual config file reading to the clang-format utility.
Configuration file name is .clang-format. It is looked up for each input file
in its parent directories starting from immediate one. First found .clang-format
file is used. When using standard input, .clang-format is searched starting from
the current directory.
Added -dump-config option to easily create configuration files.

Reviewers: djasper, klimek

Reviewed By: klimek

CC: cfe-commits, jordan_rose, kimgr

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

llvm-svn: 181589
2013-05-10 11:56:10 +00:00
Daniel Jasper 8f9624b3bc Fix bug when formatting overloaded operators.
Before, the actual operator of an overloaded operator declaration was
handled as a binary operator an thus, clang-format could not find valid
formattings for many examples, e.g.:

template <typename AAAAAAA, typename BBBBBBB>
AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);

llvm-svn: 181585
2013-05-10 07:59:58 +00:00
Daniel Jasper 2f34cacc3b Further fix to pointer to member formatting.
With style where the *s go with the type:
Before: typedef bool* (Class:: *Member)() const;
After:  typedef bool* (Class::*Member)() const;

llvm-svn: 181439
2013-05-08 15:06:58 +00:00
Daniel Jasper cfda517ea8 Fix formatting of pointers to members.
Before: int(S::*func)(void *);
After:  int (S::*func)(void *);
llvm-svn: 181438
2013-05-08 14:58:20 +00:00
Daniel Jasper d69fc77b9e Improve line breaking in binary expressions.
If the LHS of a binary expression is broken, clang-format should also
break after the operator as otherwise:
- The RHS can be easy to miss
- It can look as if clang-format doesn't understand operator precedence

Before:
bool aaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=
                                 bbbbbbbbbbbbbbbbbb && ccccccccc == ddddddddddd;
After:
bool aaaaaaaaaaaaaaaaaaaaa =
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&
    ccccccccc == ddddddddddd;

As an additional note, clang-format would also be ok with the following
formatting, it just has a higher penalty (IMO correctly so).
bool aaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=
                                 bbbbbbbbbbbbbbbbbb &&
                             ccccccccc == ddddddddddd;

llvm-svn: 181430
2013-05-08 14:12:04 +00:00
Daniel Jasper 0f0234e16e Change indentation of multi-line nested name specifiers.
Before:
aaaaaaaa::
    aaaaaaaa::
    aaaaaaaa();
After:
aaaaaaaa::
    aaaaaaaa::
        aaaaaaaa();

The reason for the change is that:
a) we are not sure which is better
b) it is a really rare edge case
c) it simplifies the code
d) it currently causes problems with memoization

llvm-svn: 181421
2013-05-08 10:00:18 +00:00
Alexander Kornienko d6538338fd Config file support for clang-format, part 1.
Summary:
Added parseConfiguration method, which reads FormatStyle from YAML
string. This supports all FormatStyle fields and an additional BasedOnStyle
field, which can be used to specify base style.

Reviewers: djasper, klimek

Reviewed By: djasper

CC: cfe-commits

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

llvm-svn: 181326
2013-05-07 15:32:14 +00:00
Daniel Jasper 990ff9729b Correctly recognize dereference after 'delete'.
With certain styles:
Before: delete* x;
After:  delete *x;

llvm-svn: 181318
2013-05-07 14:17:18 +00:00
Alexander Kornienko c860266e0f Added Mozilla style, cleaned get*Style methods.
Summary: Patch based on a patch by Ehsan Akhgari.

Reviewers: djasper

Reviewed By: djasper

CC: cfe-commits, klimek

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

llvm-svn: 181196
2013-05-06 14:11:27 +00:00
Daniel Jasper 4a4be01818 Don't break comments after includes.
LLVM/Clang basically don't use such comments and for Google-style,
include-lines are explicitly exempt from the column limit. Also, for
most cases, where the column limit is violated, the "better" solution
would be to move the comment to before the include, which clang-format
cannot do (yet).

llvm-svn: 181191
2013-05-06 10:24:51 +00:00
Daniel Jasper 8e35769b24 Change indentation when breaking after a type.
clang-format did not indent any declarations/definitions when breaking
after the type. With this change, it indents for all declarations but
does not indent for function definitions, i.e.:

Before:
const SomeLongTypeName&
some_long_variable_name;
typedef SomeLongTypeName
SomeLongTypeAlias;
const SomeLongReturnType*
SomeLongFunctionName();
const SomeLongReturnType*
SomeLongFunctionName() { ... }

After:
const SomeLongTypeName&
    some_long_variable_name;
typedef SomeLongTypeName
    SomeLongTypeAlias;
const SomeLongReturnType*
    SomeLongFunctionName();
const SomeLongReturnType*
SomeLongFunctionName() { ... }

While it might seem inconsistent to indent function declarations, but
not definitions, there are two reasons for that:
- Function declarations are very similar to declarations of function
type variables, so there is another side to consistency to consider.
- There can be many function declarations on subsequent lines and not
indenting can make them harder to identify. Function definitions
are already separated by their body and not indenting
makes the function name slighly easier to find.

llvm-svn: 181187
2013-05-06 08:27:33 +00:00
Daniel Jasper a61aefb367 Break the class-inheritance ":" to the new line.
This seems to be more common in LLVM, Google and Chromium.

Before:
class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA :
    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,
    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {
};

After:
class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,
      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {
};

llvm-svn: 181183
2013-05-06 06:45:09 +00:00
Daniel Jasper 10cd581f95 Don't put a space before ellipsis.
Before: template <class ... Ts> void Foo(Ts ... ts) { Foo(ts ...); }
After:  template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }
llvm-svn: 181182
2013-05-06 06:35:44 +00:00
Daniel Jasper 72463d32e0 Add space between ; and (.
Before: for (int i = 0;(i < 10); ++i) {}
After:  for (int i = 0; (i < 10); ++i) {}
llvm-svn: 181020
2013-05-03 14:50:50 +00:00
Daniel Jasper c37de30219 Fix expression recognition in for-loops.
Before: for (; a&& b;) {}
After:  for (; a && b;) {}
llvm-svn: 181017
2013-05-03 14:41:24 +00:00
Daniel Jasper e3c0e0144e Improve clang-format's memoization behavior.
Deeply nested expressions basically break clang-format's memoization.
This patch slightly improves the situations and makes expressions like

  aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(
      aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(
          aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(
              aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(
                  aaaaa(aaaaa())))))))))))))))))))))))))))))))))))))));

work.

llvm-svn: 180264
2013-04-25 13:31:51 +00:00
Daniel Jasper 6fe2f009df Add option to align escaped newlines left.
This enables formattings like:

  #define A   \
    int aaaa; \
    int b;    \
    int ccc;  \
    int dddddddddd;

Enabling this for Google/Chromium styles only as I don't know whether it
is desired for Clang/LLVM.

llvm-svn: 180253
2013-04-25 08:56:26 +00:00
Daniel Jasper 085a2edaea Flip flag to merge short if-statements into one line for Google style.
This now allows clang-format to do:
if (a) return;

llvm-svn: 180187
2013-04-24 13:46:00 +00:00
Daniel Jasper 770eb7c5f9 Fix comment alignment behavior.
In the following snippet, clang-format incorrectly aligned the
trailing comment, when only the last line was formatted:

  int aaaaaa; // comment
  int b;
  int c; // Formatting only this line moved this comment.

llvm-svn: 180173
2013-04-24 06:33:59 +00:00
Daniel Jasper 4431aa99ab Fix formatting of complex #if expressions.
Before:
  #if !defined(AAAAAAAAAAAAAAAA) && (defined CCCCCCCC ||                         \
                                     defined DDDDDDDD) && defined(BBBBBBBB)

After:
  #if !defined(AAAAAAAAAAAAAAAA) && (defined CCCCCCCC || defined DDDDDDDD) &&    \
      defined(BBBBBBBB)

This fixes llvm.org/PR15828.

llvm-svn: 180105
2013-04-23 13:54:04 +00:00
Daniel Jasper cc960fa645 Fix bin-packing behavior of constructor initialziers.
In Google style, constructor initializers need to be all on one line or
one initializer per line if that does not fit. Without this patch, this
non-bin-packing-behavior incorrectly extends to the parameters of the
initializers.

Before:
Constructor()
    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa,
            aaaaaaaaaaaaaaaaaaaaaa,
            aaaaaaaaaaaaaaaaaaaaaa) {}

After:
Constructor()
    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,
            aaaaaaaaaaaaaaaaaaaaaa) {}

llvm-svn: 180001
2013-04-22 07:59:53 +00:00
Alexander Kornienko 9e90b62e01 Unified token breaking logic: support for line comments.
Summary:
Added BreakableLineComment, moved common code from
BreakableBlockComment to newly added BreakableComment. As a side-effect of the
rewrite, found another problem with escaped newlines and had to change
code which removes trailing whitespace from line comments not to break after
this patch.

Reviewers: klimek, djasper

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 179693
2013-04-17 17:34:05 +00:00
Daniel Jasper 1b8e76f14e Break after multiline parameters.
We do this in general, but missed a few cases.

Before:
void aaaaaaaaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbb bbbb);

After:
void aaaaaaaaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
    bbbb bbbb);

llvm-svn: 179570
2013-04-15 22:36:37 +00:00
Alexander Kornienko c3c8affc06 Fix unused variable warning with assertions disabled.
llvm-svn: 179531
2013-04-15 15:47:34 +00:00
Alexander Kornienko cb45bc1861 Unified token breaking logic for strings and block comments.
Summary:
Both strings and block comments are broken into lines in
breakProtrudingToken. Logic specific for strings or block comments is abstracted
in implementations of the BreakToken interface. Among other goodness, this
change fixes placement of backslashes after a block comment inside a
preprocessor directive (see removed FIXMEs in unit tests).

The code is far from being polished, and some parts of it will be changed for
line comments support.

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 179526
2013-04-15 14:28:00 +00:00
Manuel Klimek 1a18c40468 Revamps structural error detection / handling.
Previously we'd only detect structural errors on the very first level.
This leads to incorrectly balanced braces not being discovered, and thus
incorrect indentation.

This change fixes the problem by:
- changing the parser to use an error state that can be detected
  anywhere inside the productions, for example if we get an eof on
  SOME_MACRO({ some block <eof>
- previously we'd never break lines when we discovered a structural
  error; now we break even in the case of a structural error if there
  are two unwrapped lines within the same line; thus,
  void f() { while (true) { g(); y(); } }
  will still be re-formatted, even if there's missing braces somewhere
  in the file
- still exclude macro definitions from generating structural error;
  macro definitions are inbalanced snippets

llvm-svn: 179379
2013-04-12 14:13:36 +00:00
Daniel Jasper 6728fc11bc Change clang-format's affinity for breaking after return types.
Function declarations are now broken with the following preferences:
1) break amongst arguments.
2) break after return type.
3) break after (.
4) break before after nested name specifiers.

Options #2 or #3 are preferred over #1 only if a substantial number of
lines can be saved by that.

llvm-svn: 179287
2013-04-11 14:29:13 +00:00
Daniel Jasper 6e8f4edf2d Fix formatting of overloaded assignment operators.
Before: SomeType &operator=(const SomeType & S);
After:  SomeType &operator=(const SomeType &S);
llvm-svn: 179270
2013-04-11 08:48:20 +00:00
Manuel Klimek a3ff45ebed Fixes recovering from errors when parsing braced init lists.
Before we would build huge unwrapped lines which take a long time
to optimze.

llvm-svn: 179168
2013-04-10 09:52:05 +00:00
Daniel Jasper c04baae34a Fix labels with trailing comments and cleanup.
Before:
class A {
public : // test
};

After:
class A {
public: // test
};

Also remove duplicate methods calculating properties of AnnotatedTokens
and make them members of AnnotatedTokens so that they are in a common
place.

llvm-svn: 179167
2013-04-10 09:49:49 +00:00
Daniel Jasper b67cc423eb Fix comments before labels.
Before:
switch (...) {
  // a
  // b
// c
case first:
  break;
}

After:
switch (...) {
// a
// b
// c
case first:
  break;
}

llvm-svn: 179107
2013-04-09 17:46:55 +00:00
Alexander Kornienko a04e5e213b Again macros without trailing semicolons: don't care about declaration context.
Summary:
Some codebases use these kinds of macros in functions, e.g. Chromium's
IPC_BEGIN_MESSAGE_MAP, IPC_BEGIN_MESSAGE_HANDLER, etc.

Reviewers: djasper, klimek

Reviewed By: klimek

CC: cfe-commits

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

llvm-svn: 179099
2013-04-09 16:15:19 +00:00
Alexander Kornienko de64427ac6 Recognize function-like macro usages without semicolon in declaration context.
Summary:
Preserve line breaks after function-like macro usages without
semicolon, e.g.:

QQQ(xxx)
class X {
};

Reviewers: djasper, klimek

Reviewed By: djasper

CC: cfe-commits

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

llvm-svn: 179064
2013-04-08 22:16:06 +00:00
Daniel Jasper 6bee682fae Revamp indentation behavior for complex binary expressions.
The idea is to indent according to operator precedence and pretty much
identical to how stuff would be indented with parenthesis.

Before:
bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==
             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *
             bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +
             bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&
             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *
             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >
             ccccccccccccccccccccccccccccccccccccccccc;

After:
  bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==
                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *
                           bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +
                       bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&
               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *
                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >
                   ccccccccccccccccccccccccccccccccccccccccc;

llvm-svn: 179049
2013-04-08 20:33:42 +00:00
Daniel Jasper e7de2a3013 Revert accidental commit r179015.
llvm-svn: 179016
2013-04-08 10:45:44 +00:00
Daniel Jasper bd16eea59f x
llvm-svn: 179015
2013-04-08 10:36:32 +00:00