summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/Format.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Initial support for formatting ObjC method declarations/calls.Daniel Jasper2013-02-051-1/+31
| | | | | | | | | | | | | | | | We can now format stuff like: - (void)doSomethingWith:(GTMFoo *)theFoo rect:(NSRect)theRect interval:(float)theInterval { [myObject doFooWith:arg1 // name:arg2 error:arg3]; } This seems to fix everything mentioned in llvm.org/PR14939. llvm-svn: 174364
* Fix some linebreak decisions in Google format.Daniel Jasper2013-02-051-8/+10
| | | | | | | | | | | | | | | | | | Before: f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa); aaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaa, aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)); After: f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa); aaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaa, aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)); llvm-svn: 174363
* Improve handling of trailing block commentsDaniel Jasper2013-02-041-3/+7
| | | | | | This is a follow up to r174309 to actually make it work. llvm-svn: 174314
* Improve handling of trailing block comments.Daniel Jasper2013-02-041-1/+2
| | | | | | | | | | We can now (even in non-bin-packing modes) format: someFunction(1, /* comment 1 */ 2, /* comment 2 */ 3, /* comment 3 */ aaa); llvm-svn: 174309
* Fix an error in formatting of for-loops.Daniel Jasper2013-02-041-2/+4
| | | | | | | | | | | | | | | | Two minor changes: * Slight penalty for breaking at "," as opposed to ";". * Don't apply bin-packing rules to for-loops. Before: for (int aaaaaa = aaaaaaaaaa; aaaaaa < bbbbbbbb; ++aaaaaa, ++ccccccccccccccc) {} After: for (int aaaaaa = aaaaaaaaaa; aaaaaa < bbbbbbbb; ++aaaaaa, ++ccccccccccccccc) {} llvm-svn: 174308
* Restructuring of token annotation for formatting.Daniel Jasper2013-02-041-15/+15
| | | | | | | | | | This combines several changes: * Calculation token type (e.g. for * and &) in the AnnotatingParser. * Calculate the scope binding strength in the AnnotatingParser. * Let <> and [] scopes bind stronger than () and {} scopes. * Add minimal debugging output. llvm-svn: 174307
* Fix bug in formatting of nested initializers.Daniel Jasper2013-02-031-1/+0
| | | | | | | | | | | | | We can now format: SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } }, { { 111111111111111111111111111111, 222222222222222222222222222222, 333333333333333333333333333333 } }, { { 1, 2, 3 } }, { { 1, 2, 3 } } }; Before, we did strange things there. llvm-svn: 174291
* Hopefully fix windows build due to non-standard pair implementation.Daniel Jasper2013-02-011-1/+1
| | | | llvm-svn: 174169
* Revamp of the basic layouting algorithm in clang-format.Daniel Jasper2013-02-011-117/+122
| | | | | | | | | | | | | | | | | | | | In order to end up with good solutions, clang-format needs to try "all" combinations of line breaks, evaluate them and select the best one. Before, we have done this using a DFS with memoization and cut-off conditions. However, this approach is very limited as shown by the huge static initializer in the attachment of llvm.org/PR14959. Instead, this new implementation uses a variant of Dijkstra's algorithm to do a prioritized BFS over the solution space. Some numbers: lib/Format/TokenAnnotator.cpp: 1.5s -> 0.15s Attachment of PR14959: 10min+ (didn't finish) -> 10s No functional changes intended. llvm-svn: 174166
* Several improvements to the formatting of static initializers.Daniel Jasper2013-01-311-11/+39
| | | | | | | | | | | | | 1. Never avoid bin packing in static initializers as this can lead to terrible results. 2. If an element has to be broken over multiple lines, break after the following comma. This should be a step forward, but there are still many cases especially with nested static initializers that we handle badly. More patches will follow. llvm-svn: 174061
* Don't generate no-op replacements.Daniel Jasper2013-01-301-0/+5
| | | | | | No functional changes. llvm-svn: 173916
* Move the token annotator into separate files.Daniel Jasper2013-01-291-1030/+1
| | | | | | | No functional changes. Also removed experimental-warning from all of clang-format's files, as it is no longer accurate. llvm-svn: 173830
* Improve formatting of code with comments.Daniel Jasper2013-01-291-6/+5
| | | | | | | | | | | | | | | | | | | | Before: aaaaaaa(aaaaaa( // comment aaaaaaa)); <big mess> After: aaaaaaa(aaaaaa( // comment aaaaaaaa)); function(/* parameter 1 */ aaaaaaa, /* parameter 2 */ aaaaaaa, /* parameter 3 */ aaaaaaa, /* parameter 4 */ aaaaaaa); (the latter example was only wrong in the one-arg-per-line mode, e.g. in Google style). llvm-svn: 173821
* Allow all parameters on next line for function calls in Chrome.Daniel Jasper2013-01-291-5/+6
| | | | | | | | | | | | The style guide only forbids this for function declarations. So, now someFunction( aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaa); Is allowed in Chromium mode. llvm-svn: 173806
* Split ">>" in "A<B<C> >" in Chromium style.Daniel Jasper2013-01-291-0/+1
| | | | | | It needs to be compatible with C++03. llvm-svn: 173805
* Fix uninitialized error caused by r173801.Daniel Jasper2013-01-291-2/+3
| | | | llvm-svn: 173803
* Calculate the split penalty upfront.Daniel Jasper2013-01-291-55/+60
| | | | | | | | | | This is a) More efficient. b) Important as we move forward with further metrics for penalty. No functional changes intended. llvm-svn: 173801
* Fix a comment and reformat clang-format with clang-formatDaniel Jasper2013-01-291-43/+44
| | | | | | Not all changes might be ideal, but IMO all are acceptable. llvm-svn: 173793
* Initial support for multiple variable declarations.Daniel Jasper2013-01-291-15/+15
| | | | | | | | | | | Before: SomeType aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(), aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa(); After: SomeType aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(), aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa(); llvm-svn: 173792
* Reverting changes from r173785 (removing empty lines before "}").Daniel Jasper2013-01-291-2/+0
| | | | | | | This needs some more thinking, e.g. for namespaces, chains of if-else if, ... llvm-svn: 173787
* Remove empty lines before "}".Daniel Jasper2013-01-291-0/+2
| | | | | | | | | | | | | | | | | | Those empty lines waste vertical whitespace and almost never increase readability. Before: void f() { DoSomething(); } After: void f() { DoSomething(); } llvm-svn: 173785
* Fix a bug that would lead to bad line break decisions in for loops.Daniel Jasper2013-01-281-6/+6
| | | | | | | | | | | | | | Before: for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaa; aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {} After: for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaa; aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {} llvm-svn: 173695
* Don't put a function's return type on its own line in Google style.Daniel Jasper2013-01-281-1/+3
| | | | | | | | | | This would be against the style guide: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Function_Declarations_and_Definitions#Function_Declarations_and_Definitions Not sure what to do as a last resort if the function signature does not fit onto a single line in Google style .. llvm-svn: 173690
* Let clang-format break after a function's return type.Daniel Jasper2013-01-281-7/+33
| | | | | | | | | | | | | | | | Before: TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, IdentifierInfo *II, Type *T) {} After: TypeSpecDecl * TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, IdentifierInfo *II, Type *T) {} This fixes llvm.org/PR14717. llvm-svn: 173688
* Make continuations in constructor initializers consistent.Daniel Jasper2013-01-281-1/+2
| | | | | | | | | | | | | | Before: Constructor : LooooooooooooooooooooongVariable( LooooooooooooooooooooongValue) {} After: Constructor : LooooooooooooooooooooongVariable( LooooooooooooooooooooongValue) {} llvm-svn: 173685
* Initial support for formatting range-based for-loops.Daniel Jasper2013-01-281-3/+19
| | | | | | | | | | | | | | | | Before (in good cases): for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} for (auto aaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa, aaaa)) {} After: for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} for (auto aaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa, aaaa)) {} llvm-svn: 173684
* Improve formatting of conditional expressions.Daniel Jasper2013-01-281-10/+22
| | | | | | | | | | | | | | | | | | | Before we did not really systematically format those. Now, we format the different cases as: - 1 Line: a ? b : c; - 2 Lines: short ? loooooooooong : loooooooooong - 2 Lines: loooooooooooooooong ? short : short - 3 Lines: loooooooooooooooong ? loooooooooooooong : loooooooooooooong Not sure whether "?" and ":" should go on the new line, but it seems to be the most consistent approach. llvm-svn: 173683
* Several small changes in formatting decisions.Daniel Jasper2013-01-281-26/+15
| | | | | | | | | | | | | | | | | 1. Use a hanging ident for function calls nested in binary expressions. E.g.: int aaaaa = aaaaaaaaa && aaaaaaaaaa( aaaaaaaaaa); 2. Slightly improve heuristic for builder type expressions and reduce penalty for breaking before "." and "->" in those. 3. Remove mostly obsolete metric of decreasing indent level. This fixes: llvm.org/PR14931. Changes #1 and #2 were necessary to keep tests passing after #3. llvm-svn: 173680
* Improve indentation after breaking at nested name specifiers.Daniel Jasper2013-01-281-3/+4
| | | | | | | | | | | | | | These always represent a continuation and we should increase the ident. Before: aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa:: aaaaaaaaaaaaaaaaaaaa); After: aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa:: aaaaaaaaaaaaaaaaaaaa); llvm-svn: 173675
* Avoid confusing identations for multi-parameter functions.Daniel Jasper2013-01-281-3/+30
| | | | | | | | | | | | | | Before: aaaaaaaa(aaaaaaaaa( aaaaaaaaaa(), aaaaaaaaa); After: aaaaaaaa(aaaaaaaaa( aaaaaaaaaa(), aaaaaaaaa); llvm-svn: 173673
* Fix some alignment and line break decisions.Daniel Jasper2013-01-251-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | This combines two small changes: 1) Put a penalty on breaking after "<" 2) Only produce a hanging indent when parameters are separated by commas. Before: aaaaaaaaaaaaaaaaaaaaaaaa< aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)); After: aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)); This changes one ObjC test, but AFAICT this is not according to any style guide (neither before nor after). We probably should be aligning on the ":" there according to: http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml?showone=Method_Invocations#Method_Invocations llvm-svn: 173457
* Allow breaking after "::" if absolutely necessary.Daniel Jasper2013-01-251-4/+7
| | | | | | | | | | Otherwise, really long nested name specifiers can easily lead to a violation of the column limit. Not sure about the rules for indentation in those cases, so input is appreciated (see tests.). llvm-svn: 173438
* Add extra indent for nested calls inside if's.Daniel Jasper2013-01-231-3/+7
| | | | | | | | | | | | Before: if (aaaaaaaaaa( aaaaaaaaaa)) {} After: if (aaaaaaaaaa( aaaaaaaaaa)) {} llvm-svn: 173290
* Don't try to align builder-type continuations on assignments.Daniel Jasper2013-01-231-4/+12
| | | | | | | | | | | | | Before: int aaaa = aaaaa().aaaaa() // force break .aaaaa(); After: int aaaa = aaaaa().aaaaa() // force break .aaaaa(); The other indent is just wrong and confusing. llvm-svn: 173273
* Don't try to do a hanging ident after assignments.Daniel Jasper2013-01-231-5/+2
| | | | | | | | | | | | | | | | | | | | | | | Before: bool aaaa = aaaaaaaaaaa( aaaaaaaaaaaaaaaaa); After: bool aaaa = aaaaaaaaaaa( aaaaaaaaaaaaaaaaa); The other indentation was a nice attempt but doesn't work in many cases. Not sure what the right long term solution is as the "After: " is still not nice. We either need to figure out what to do in the cases where it "doesn't work" or come up with a third solution, e.g. falling back to: bool aaaa = aaaaaaaaaaa( aaaaaaaaaaaaaaaaa); which should always work and nicely highlight the structure. llvm-svn: 173268
* Fixes layouting regression and invalid-read.Manuel Klimek2013-01-231-1/+3
| | | | | | | | | | Layouting would prevent breaking before + in a[b + c] = d; Regression detected by code review. Also fixes an invalid-read found by the valgrind bot. llvm-svn: 173262
* Fix the formatting of pointer/reference types in range-based for loops.Daniel Jasper2013-01-231-1/+2
| | | | | | Before: for (int & a : Values) {} After: for (int &a : Values) {} llvm-svn: 173259
* Removing the penalty for breaking after "=".Daniel Jasper2013-01-231-5/+0
| | | | | | | | | | | | | | | | Having seen more cases, this actually was not a good thing to do in the first place. We can still improve on what we do now, but breaking after the "=" is good in many cases. Before: aaaaaaaaaaaaa = aa->aaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa)); After: aaaaaaaaaaaaa = aa->aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa)); llvm-svn: 173257
* Fix another regression for pointer types.Daniel Jasper2013-01-231-11/+19
| | | | | | | | | | Before: if (int * a = &b) ... After: if (int *a = &b) ... Also changed all the existing tests to test the expressions in question both in a declaration and in an expression context. llvm-svn: 173256
* Fix regression in formatting pointer types.Daniel Jasper2013-01-231-1/+3
| | | | | | | | | We will need a more principled solution, but we should not leave this unfixed until we come up with one. Before: void f() { int * a; } After: void f() { int *a; } llvm-svn: 173252
* Fix segfaults in the formatter.Manuel Klimek2013-01-231-2/+2
| | | | | Also: expletive deleted. llvm-svn: 173247
* Add option to allow putting all parameters onto the next line.Daniel Jasper2013-01-231-18/+17
| | | | | | | | | | | | | | | | | This only affects styles where BinPackParameters is false. With AllowAllParametersOnNextLine: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaa); Without it: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaa); llvm-svn: 173246
* Allow us to better guess the context of an unwrapped line.Manuel Klimek2013-01-231-3/+6
| | | | | | | | | | | This gives us the ability to guess better defaults for whether a * between identifiers is a pointer dereference or binary operator. Now correctly formats: void f(a *b); void f() { f(a * b); } llvm-svn: 173243
* Implements more principled comment parsing.Manuel Klimek2013-01-221-1/+3
| | | | | | | | | | | | | | | | | | Changing nextToken() in the UnwrappedLineParser to get the next non-comment token. This allows us to correctly layout a whole class of snippets, like: if /* */(/* */ a /* */) /* */ f() /* */; /* */ else /* */ g(); Fixes a bug in the formatter where we would assume there is a previous non-comment token. Also adds the indent level of an unwrapped line to the debug output in the parser. llvm-svn: 173168
* Let the formatter be more restrictive for breaking around . and ->Daniel Jasper2013-01-221-1/+11
| | | | | | | | | | | | Before: aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa) .aaaaaaaaaaaaaaaaaa(); After: aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa(); llvm-svn: 173160
* Fix "*" formatting when creating arrays of pointers.Daniel Jasper2013-01-221-0/+10
| | | | | | Before: A = new int * [10](); After: A = new int *[10](); llvm-svn: 173150
* Remove "incorrect" aligning of trailing comments.Daniel Jasper2013-01-211-4/+11
| | | | | | | | | | | | | | | We used to align trailing comments belong to different things. Before: void f() { // some function.. } int a; // some variable.. After: void f() { // some function.. } int a; // some variable.. llvm-svn: 173100
* Formatter: Set MatchingParen for [], to match <>, (), {}. No functionality ↵Nico Weber2013-01-211-0/+2
| | | | | | change. llvm-svn: 173078
* Formatter: Rename LSquare to Left to make parseSquare() more consistent with ↵Nico Weber2013-01-211-8/+6
| | | | | | the other paren parsing methods. llvm-svn: 173077
* Fixes formatting of empty blocks.Manuel Klimek2013-01-211-33/+34
| | | | | | | | | | | | | | | | We now only put empty blocks into a single line, if all of: - all tokens of the structural element fit into a single line - we're not in a control flow statement Note that we usually don't put record definitions into a single line, as there's usually at least one more token (the semicolon) after the closing brace. This doesn't hold when we are in a context where there is no semicolon, like "enum E {}". There were some missing tests around joining lines around the corner cases of the allowed number of columns, so this patch adds some. llvm-svn: 173055
OpenPOWER on IntegriCloud