Commit Graph

357 Commits

Author SHA1 Message Date
Manuel Klimek 9fa8d5578d Fix crashes in UnwrappedLineParser on missing parens.
llvm-svn: 172239
2013-01-11 19:23:05 +00:00
Manuel Klimek 2acb7b7bb1 Fix single-line optimization for ObjC.
Puts blocks always into multiple lines when they start with an ObjC
keyword or minus.

llvm-svn: 172238
2013-01-11 19:17:44 +00:00
Manuel Klimek adededff26 Fix crash on invalid.
if { foo; }
would previously crash clang-format.

llvm-svn: 172232
2013-01-11 18:28:36 +00:00
Manuel Klimek d5e5f8f2a4 Fix parsing of initializer lists with elaborated type specifier.
Now we correctly parse and format:
verifyFormat("struct foo a = { bar };
int n;

llvm-svn: 172229
2013-01-11 18:13:04 +00:00
Manuel Klimek f4ab9ef34c Implements pulling simple blocks into a single line.
void f() { return 42; }

The final change that implements the feature.

llvm-svn: 172225
2013-01-11 17:54:10 +00:00
Daniel Jasper d6a947f75f Correct spacing around new and delete.
This fixes llvm.org/PR14913.

Before:  A *a = new(placement) A;
After:   A *a = new (placement) A;
llvm-svn: 172212
2013-01-11 16:09:04 +00:00
Daniel Jasper fd8c4b1321 Improve handling of trailing declaration annotations.
Before:
void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) GUARDED_BY(
    aaaaaaaaaaaaa);

After:
void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
    GUARDED_BY(aaaaaaaaaaaaa);

Also did some formatting cleanups with clang-format on the way.

llvm-svn: 172200
2013-01-11 14:23:32 +00:00
Daniel Jasper 2408a8c755 Improved formatting of constructor initializers
Added option to put each constructor initializer on its own line
if not all initializers fit on a single line. Enabling this for
Google style now as the style guide (arguable) suggests it. Not
sure whether we also want it for LLVM.

llvm-svn: 172196
2013-01-11 11:37:55 +00:00
Nico Weber 71f95e05ec Formatter: Remove a fixme, Jordy says @synthesize is good as is.
llvm-svn: 172141
2013-01-10 23:12:29 +00:00
Nico Weber 9efe291fa8 Formatter: Put spaces in ObjC method decls in the right place for Google style.
Objective-C method declarations look like this:

- (returntype)name:(type)argname anothername:(type)arg2name;

In google style, there's no space after the leading '-' but one after
"(returntype)" instead (but none after the argument types), see
http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml#Method_Declarations_and_Definitions

Not inserting the space after '-' is easy, but to insert the space after the
return type, the formatter needs to know that a closing parenthesis ends the
return type. To do this, I tweaked the code in parse() to check for this, which
in turn required moving detection of TT_ObjCMethodSpecifier from annotate() to
parse(), because parse() runs before annotate().

(To keep things interesting, the return type is optional, but it's almost
always there in practice.)

http://llvm-reviews.chandlerc.com/D280

llvm-svn: 172140
2013-01-10 23:11:41 +00:00
Nico Weber a2a8495e19 Formatter: No spaces around '=' in @property lines.
Before:
@property(assign, getter = isEditable) BOOL editable;

Now:
@property(assign, getter=isEditable) BOOL editable;

It'd be nice if some Apple person could let me know if spaces are preferred
around '=' in @synthesize lines (see FIXME in the test).

llvm-svn: 172110
2013-01-10 21:30:42 +00:00
Nico Weber d6f962f151 Formatter: Add test for template and protocol parameters in ObjC method declarations.
llvm-svn: 172100
2013-01-10 20:18:33 +00:00
Nico Weber a608775ba2 Formatting: In @implementation etc lines, put a space before protocol lists.
Don't do this in Google style though:
http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml#Protocols

Most other places (function declarations, variable declarations) still get
this wrong, and since this looks very similiar to template instantiations to
the lexer (`id <MyProtocol> a = ...`), it's going to be hard to fix in some
places.

llvm-svn: 172099
2013-01-10 20:12:55 +00:00
Nico Weber a1a5abd5b1 Formatter: Don't put a space in ObjC number literals like @+50
Before:
@ -4.5

Now:
@-4.5

llvm-svn: 172095
2013-01-10 19:36:35 +00:00
Nico Weber 2bb0074232 Formatter: Add space before '(' in @implemenation, @interface, @protocol lines
The first token in @implementation, @interface, and @protocol lines is now
marked TT_ObjCDecl, and lines starting with a TT_ObjCDecl token are now marked
LT_ObjCMethodDecl.

llvm-svn: 172093
2013-01-10 19:19:14 +00:00
Manuel Klimek 0ddd57a17b Fixes layout of right braces.
We now decide whether a newline should go before the closing brace
depending on whether a newline was inserted after the opening brace.

For example, we now insert a newline before '};' in:
static SomeClass WithALoooooooooooooooooooongName = {
  100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"
};
... while not inserting a newline here:
static SomeClass = { a, b, c, d, e, f, g, h, i, j,
                     looooooooooooooooooooooooooooooooooongname,
                     looooooooooooooooooooooooooooooong };

Also fixes the formating of (column limit 25):
int x = {
  avariable,
  b(alongervariable)
};

llvm-svn: 172076
2013-01-10 15:58:26 +00:00
Manuel Klimek 73a2fdf232 Fixes formatting of function calls etc inside an initializer list.
We're now formatting (column limit 25):
int x = {
  avariable,
  b(alongervariable) };

This also fixes:
Aaa({
  int i;
}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
                                    ccccccccccccccccc));
... where we would previously break after the '},'.

Putting the closing curly into an extra line when there's a break
directly after the first curly will be done in a subsequent patch.

Paired with djasper.

llvm-svn: 172070
2013-01-10 14:36:46 +00:00
Manuel Klimek e7d10a1f50 Do not add newline in empty blocks.
void f() {}
now gets formatted in one line.

llvm-svn: 172067
2013-01-10 13:24:24 +00:00
Daniel Jasper c1fa281325 Improvements to function type and ObjC block formatting.
Before: int (^myBlock) (int) = ^(int num) {}
        A<void ()>;
	int (*b)(int);

After:  int (^myBlock)(int) = ^(int num) {}
        A<void()>;
	int(*b)(int);

For function types and function pointer types, this patch only makes
the behavior consistent (for types that are keywords and other types).
For the latter function pointer type declarations, we'll probably
want to add a space after "int".

Also added LangOpts.Bool = 1, so we handle "A<bool()>" appropriately
Moved the LangOpts-settings to a public place for use by tests
and clang-format binary.

llvm-svn: 172065
2013-01-10 13:08:12 +00:00
Manuel Klimek 8e07a1b64b Fix layout of blocks inside statements.
Previously, we would not indent:
SOME_MACRO({
  int i;
});
correctly. This is fixed by adding the trailing }); to the unwrapped
line starting with SOME_MACRO({, so the formatter can correctly match
the braces and indent accordingly.

Also fixes incorrect parsing of initializer lists, like:
int a[] = { 1 };

llvm-svn: 172058
2013-01-10 11:52:21 +00:00
Daniel Jasper 7194e1818e Improve clang-format's understanding of casts.
This fixes llvm.org/PR14684.

Before: int *pa = (int *) & a;
After:  int *pa = (int *)&a;

We still don't understand all kinds of casts. I added a FIXME to
address that.

llvm-svn: 172056
2013-01-10 11:14:08 +00:00
Daniel Jasper 83a54d23d3 Do more error checking for '{}'.
This fixes llvm.org/PR14883, where clang-format would run into an
assertion on:

void f() { return } 42

llvm-svn: 172049
2013-01-10 09:26:47 +00:00
Nico Weber bbe28b3d80 Formatter: Remove unused @-formatting code.
@optional @property is put on two different unwrapped lines now, so this is no
longer necessary.

llvm-svn: 172024
2013-01-10 00:42:07 +00:00
Nico Weber 51306d232e Formatter: @optional and @required go on their own line.
Previously:
@protocol myProtocol
- (void)mandatoryWithInt:(int)i;
@optional - (void) optional;
@required - (void) required;
@end

Now:
@protocol myProtocol
- (void)mandatoryWithInt:(int)i;
@optional
- (void)optional;
@required
- (void)required;
@end

llvm-svn: 172023
2013-01-10 00:25:19 +00:00
Nico Weber 2ce0ac5a8c Formatter: Add support for @implementation.
Just reuse the @interface code for this. It accepts slightly more than
necessary (@implementation cannot have protocol lists), but that's ok.

llvm-svn: 172019
2013-01-09 23:25:37 +00:00
Nico Weber d8ffe75ad1 Formatter: Make parseObjCUntilAtEnd() actually work.
llvm-svn: 172003
2013-01-09 21:42:32 +00:00
Nico Weber 8696a8d9e3 Formatting: Add support for @protocol.
Pull pieces of the @interface code into reusable methods.

llvm-svn: 172001
2013-01-09 21:15:03 +00:00
Nico Weber 7eecf4b6e3 Formatter: Add support for @interface.
Previously:
@interface Foo + (id)init; @end

Now:
@interface Foo
+ (id)init;
@end

Some tweaking remains, but this is a good first step.

llvm-svn: 171995
2013-01-09 20:25:35 +00:00
Manuel Klimek 52b1515405 Enables layouting unwrapped lines around preprocessor directives.
Previously, we'd always start at indent level 0 after a preprocessor
directive, now we layout the following snippet (column limit 69) as
follows:

functionCallTo(someOtherFunction(
    withSomeParameters, whichInSequence,
    areLongerThanALine(andAnotherCall,
  B
                       withMoreParamters,
                       whichStronglyInfluenceTheLayout),
    andMoreParameters),
               trailing);

Note that the different jumping indent is a different issue that will be
addressed separately.

This is the first step towards handling #ifdef->#else->#endif chains
correctly.

llvm-svn: 171974
2013-01-09 15:25:02 +00:00
Daniel Jasper bcab430fee Only align after assignments on the top level.
This fixes llvm.org/PR14870 and we no longer mess up:
template <typename T1, typename T2 = char, typename T3 = char,
          typename T4 = char>
void f();

It removes the nice aligment for assignments inside other expressions,
but I am not sure those are actually practically relevant. If so, we can
fix those later.

llvm-svn: 171966
2013-01-09 10:40:23 +00:00
Daniel Jasper 2df93311da Don't simply give up when exceeding 80cols, choose an "ok" option.
This addresses llvm.org/PR14847.

We can now format something like:
int aaaaaaaaaaaaaaaaaaaaaaaaaaa =
    // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;

clang-format unavoidably exceeds the column limit, but does not just
flush everything into a single line. Moreover, it tries to minimize the
number of characters beyond the column limit.

llvm-svn: 171964
2013-01-09 10:16:05 +00:00
Daniel Jasper 890589414b Correctly format wrapped function call parameters in templated functions.
This fixes llvm.org/PR14846.

Before:
template <typename T>
void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
        int aaaaaaaaaaaaaaaaa);

After:
template <typename T>
void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
    int aaaaaaaaaaaaaaaaa);

llvm-svn: 171963
2013-01-09 09:50:48 +00:00
Daniel Jasper d8bb2db242 Allow comments in the middle of statements to be on their own line.
This fixes llvm.org/PR14860.

Before, we messed up the format of:
if (DeclaratorInfo.isFunctionDeclarator() &&
    //getDeclSpecContextFromDeclaratorContext(Context) == DSC_top_level &&
    Tok.is(tok::semi) && NextToken().is(tok::l_brace)) {
}

llvm-svn: 171961
2013-01-09 09:33:39 +00:00
Daniel Jasper fb3f2482e5 Fix ObjC block declarations.
Before: int ( ^ Block1) (int, int) = ^ (int i, int j)
After:  int (^Block1) (int, int) = ^(int i, int j)
llvm-svn: 171959
2013-01-09 08:36:49 +00:00
Daniel Jasper 399d24bc7d Improve formatting of conditional operators.
This addresses llvm.org/PR14864.

We used to completely mess this up and now format as:
Diag(NewFD->getLocation(),
     getLangOpts().MicrosoftExt ? diag::ext_function_specialization_in_class :
         diag::err_function_specialization_in_class)
    << NewFD->getDeclName();

llvm-svn: 171957
2013-01-09 07:06:56 +00:00
Nico Weber 4cc9874ff9 Formatter: More tests for already-passing ObjC bits.
llvm-svn: 171892
2013-01-08 20:16:23 +00:00
Daniel Jasper 66dcb1ca86 Don't break after unary operators.
Before:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *
                                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

After:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

llvm-svn: 171890
2013-01-08 20:03:18 +00:00
Nico Weber 77aa25090a Formatter: Format @ literals better. Array and dictionary literals need more work.
llvm-svn: 171887
2013-01-08 19:40:21 +00:00
Nico Weber d8cdb53e1d Formatter: More ObjC tests.
llvm-svn: 171883
2013-01-08 19:15:23 +00:00
Nico Weber 7e6a7a19e3 Formatter: Add tests for some ObjC bits that happen to be formatted correctly.
llvm-svn: 171875
2013-01-08 17:56:31 +00:00
Daniel Jasper 4f397156c4 Don't put spaces around ##.
In Clang/LLVM this seems to be the more common formatting for ##s. There
might still be case that we miss, but we'll fix those as we go along.

Before:

  #define A(X)
    void function ## X();

After:

  #define A(X)
    void function##X();

llvm-svn: 171862
2013-01-08 16:17:54 +00:00
Daniel Jasper 7c85fde500 Change the data structure used in clang-format.
This is a first step towards supporting more complex structures such
as #ifs inside unwrapped lines. This patch mostly converts the array-based
UnwrappedLine into a linked-list-based UnwrappedLine. Future changes will
allow multiple children for each Token turning the UnwrappedLine into a
tree.

No functional changes intended.

llvm-svn: 171856
2013-01-08 14:56:18 +00:00
Nico Weber 04e9f1ab42 Formatter: Support @public/@protected/@package/@private.
@package is an Objective-C 2 feature, so turn on ObjC2 as well.

llvm-svn: 171766
2013-01-07 19:05:19 +00:00
Manuel Klimek 28cacc740d Fix parsing of variable declarations directly after a class / struct.
Previous indent:
class A {
}
a;
void f() {
};

With this patch:
class A {
} a;
void f() {
}
;

The patch introduces a production for classes and structs, and parses
the rest of the line to the semicolon after the class scope.
This allowed us to remove a long-standing wart in the parser that would
just much the semicolon after any block.
Due to this suboptimal formating some tests were broken.

Some unrelated formatting tests broke; those hit a bug in the ast
printing, and need to be fixed separately.

llvm-svn: 171761
2013-01-07 18:10:23 +00:00
Nico Weber d5650bdc1a Formatter: Add tests for try/catch. Let 'throw' start an expression.
Before:
  throw a *b;

Now:
  throw a * b;

llvm-svn: 171754
2013-01-07 16:36:17 +00:00
Nico Weber e89c42f409 Formatter: Don't put spaces betwen @ and objc keywords.
llvm-svn: 171753
2013-01-07 16:14:28 +00:00
Nico Weber 803d61d143 Test all @keywords. Turns out some of them are not formatted correctly yet.
llvm-svn: 171752
2013-01-07 16:07:07 +00:00
Nico Weber 12d5babba0 Slightly expand the @ test to cover whitespace between @ and keyword.
llvm-svn: 171751
2013-01-07 15:56:25 +00:00
Nico Weber a887650cf6 Add a test that checks that the formatter doesn't discard '@' with ObjC1 set.
Also set ObjC1 in the formatter tests.

The only effect of this flag in the lexer is that '@' now gets turned into
tok::at instead of tok::unknown.

llvm-svn: 171742
2013-01-07 15:17:23 +00:00
Daniel Jasper 8d1832e091 Reformat clang-formats source code.
All changes done by clang-format itself. No functional changes.

llvm-svn: 171732
2013-01-07 13:26:07 +00:00