aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
AgeCommit message (Collapse)Author
2013-02-04Restructuring of token annotation for formatting.Daniel Jasper
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174307 91177308-0d34-0410-b5e6-96231b3b80d8
2013-02-03Fix bug in formatting of nested initializers.Daniel Jasper
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174291 91177308-0d34-0410-b5e6-96231b3b80d8
2013-02-01Hopefully fix windows build due to non-standard pair implementation.Daniel Jasper
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174169 91177308-0d34-0410-b5e6-96231b3b80d8
2013-02-01Revamp of the basic layouting algorithm in clang-format.Daniel Jasper
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174166 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-31Several improvements to the formatting of static initializers.Daniel Jasper
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174061 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-30Don't generate no-op replacements.Daniel Jasper
No functional changes. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173916 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Move the token annotator into separate files.Daniel Jasper
No functional changes. Also removed experimental-warning from all of clang-format's files, as it is no longer accurate. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173830 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Improve formatting of code with comments.Daniel Jasper
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). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173821 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Allow all parameters on next line for function calls in Chrome.Daniel Jasper
The style guide only forbids this for function declarations. So, now someFunction( aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaa); Is allowed in Chromium mode. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173806 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Split ">>" in "A<B<C> >" in Chromium style.Daniel Jasper
It needs to be compatible with C++03. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173805 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Fix uninitialized error caused by r173801.Daniel Jasper
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173803 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Calculate the split penalty upfront.Daniel Jasper
This is a) More efficient. b) Important as we move forward with further metrics for penalty. No functional changes intended. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173801 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Fix a comment and reformat clang-format with clang-formatDaniel Jasper
Not all changes might be ideal, but IMO all are acceptable. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173793 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Initial support for multiple variable declarations.Daniel Jasper
Before: SomeType aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(), aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa(); After: SomeType aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(), aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa(); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173792 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Reverting changes from r173785 (removing empty lines before "}").Daniel Jasper
This needs some more thinking, e.g. for namespaces, chains of if-else if, ... git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173787 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-29Remove empty lines before "}".Daniel Jasper
Those empty lines waste vertical whitespace and almost never increase readability. Before: void f() { DoSomething(); } After: void f() { DoSomething(); } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173785 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-28Fix a bug that would lead to bad line break decisions in for loops.Daniel Jasper
Before: for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaa; aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {} After: for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaa; aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {} git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173695 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-28Don't put a function's return type on its own line in Google style.Daniel Jasper
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 .. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173690 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-28Let clang-format break after a function's return type.Daniel Jasper
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173688 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-28Make continuations in constructor initializers consistent.Daniel Jasper
Before: Constructor : LooooooooooooooooooooongVariable( LooooooooooooooooooooongValue) {} After: Constructor : LooooooooooooooooooooongVariable( LooooooooooooooooooooongValue) {} git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173685 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-28Initial support for formatting range-based for-loops.Daniel Jasper
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)) {} git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173684 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-28Improve formatting of conditional expressions.Daniel Jasper
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173683 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-28Several small changes in formatting decisions.Daniel Jasper
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173680 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-28Improve indentation after breaking at nested name specifiers.Daniel Jasper
These always represent a continuation and we should increase the ident. Before: aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa:: aaaaaaaaaaaaaaaaaaaa); After: aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa:: aaaaaaaaaaaaaaaaaaaa); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173675 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-28Avoid confusing identations for multi-parameter functions.Daniel Jasper
Before: aaaaaaaa(aaaaaaaaa( aaaaaaaaaa(), aaaaaaaaa); After: aaaaaaaa(aaaaaaaaa( aaaaaaaaaa(), aaaaaaaaa); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173673 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-25Fix some alignment and line break decisions.Daniel Jasper
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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173457 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-25Allow breaking after "::" if absolutely necessary.Daniel Jasper
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.). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173438 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Add extra indent for nested calls inside if's.Daniel Jasper
Before: if (aaaaaaaaaa( aaaaaaaaaa)) {} After: if (aaaaaaaaaa( aaaaaaaaaa)) {} git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173290 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Don't try to align builder-type continuations on assignments.Daniel Jasper
Before: int aaaa = aaaaa().aaaaa() // force break .aaaaa(); After: int aaaa = aaaaa().aaaaa() // force break .aaaaa(); The other indent is just wrong and confusing. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173273 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Don't try to do a hanging ident after assignments.Daniel Jasper
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173268 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Fixes layouting regression and invalid-read.Manuel Klimek
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173262 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Fix the formatting of pointer/reference types in range-based for loops.Daniel Jasper
Before: for (int & a : Values) {} After: for (int &a : Values) {} git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173259 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Removing the penalty for breaking after "=".Daniel Jasper
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)); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173257 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Fix another regression for pointer types.Daniel Jasper
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173256 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Fix regression in formatting pointer types.Daniel Jasper
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; } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173252 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Fix segfaults in the formatter.Manuel Klimek
Also: expletive deleted. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173247 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Add option to allow putting all parameters onto the next line.Daniel Jasper
This only affects styles where BinPackParameters is false. With AllowAllParametersOnNextLine: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaa); Without it: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaa); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173246 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-23Allow us to better guess the context of an unwrapped line.Manuel Klimek
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); } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173243 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-22Implements more principled comment parsing.Manuel Klimek
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173168 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-22Let the formatter be more restrictive for breaking around . and ->Daniel Jasper
Before: aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa) .aaaaaaaaaaaaaaaaaa(); After: aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa(); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173160 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-22Fix "*" formatting when creating arrays of pointers.Daniel Jasper
Before: A = new int * [10](); After: A = new int *[10](); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173150 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-21Remove "incorrect" aligning of trailing comments.Daniel Jasper
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.. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173100 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-21Formatter: Set MatchingParen for [], to match <>, (), {}. No functionality ↵Nico Weber
change. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173078 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-21Formatter: Rename LSquare to Left to make parseSquare() more consistent with ↵Nico Weber
the other paren parsing methods. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173077 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-21Fixes formatting of empty blocks.Manuel Klimek
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173055 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-21Fix parsing of templated declarations.Daniel Jasper
Before: template <template <typename T>, typename P > class X; After: template <template <typename T>, typename P> class X; More importantly, the token annotations for the second ">" are now computed correctly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173047 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-21Fix bug discovered by valgrind.Daniel Jasper
When trying to merge lines, we should not touch lines that are invalid, as we don't know how long they might be. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173043 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-18Fixes issues around pulling in the next line in simple if statements.Manuel Klimek
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172822 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-18Reduce penalty for splitting between ")" and ".".Daniel Jasper
').' is likely part of a builder pattern statement. This is based upon a patch developed by Nico Weber. Thank you! Before: int foo() { return llvm::StringSwitch<Reference::Kind>(name).StartsWith( ".eh_frame_hdr", ORDER_EH_FRAMEHDR).StartsWith( ".eh_frame", ORDER_EH_FRAME).StartsWith(".init", ORDER_INIT).StartsWith( ".fini", ORDER_FINI).StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT); } After: int foo() { return llvm::StringSwitch<Reference::Kind>(name) .StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR) .StartsWith(".eh_frame", ORDER_EH_FRAME) .StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI) .StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT); } Probably not ideal, but makes many cases much more readable. The changes to overriding-ftemplate-comments.cpp don't seem better or worse. We should address those soon. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172804 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-18Also align trailing line comments in include directives.Daniel Jasper
Before: #include <a> // for x #include <a/b/c> // for yz After: #include <a> // for x #include <a/b/c> // for yz git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172799 91177308-0d34-0410-b5e6-96231b3b80d8