summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/Format.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Unified token breaking logic: support for line comments.Alexander Kornienko2013-04-171-22/+15
| | | | | | | | | | | | | | | | | | | 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
* Break after multiline parameters.Daniel Jasper2013-04-151-1/+3
| | | | | | | | | | | | | | | | | 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
* Fix unused variable warning with assertions disabled.Alexander Kornienko2013-04-151-3/+4
| | | | llvm-svn: 179531
* Unified token breaking logic for strings and block comments.Alexander Kornienko2013-04-151-466/+71
| | | | | | | | | | | | | | | | | | | | | | 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
* Revamps structural error detection / handling.Manuel Klimek2013-04-121-9/+10
| | | | | | | | | | | | | | | | | | | | | 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
* Change clang-format's affinity for breaking after return types.Daniel Jasper2013-04-111-3/+3
| | | | | | | | | | | | | 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
* Fix labels with trailing comments and cleanup.Daniel Jasper2013-04-101-14/+9
| | | | | | | | | | | | | | | | | | 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
* Fix comments before labels.Daniel Jasper2013-04-091-6/+13
| | | | | | | | | | | | | | | | | | | | | | Before: switch (...) { // a // b // c case first: break; } After: switch (...) { // a // b // c case first: break; } llvm-svn: 179107
* Revamp indentation behavior for complex binary expressions.Daniel Jasper2013-04-081-23/+36
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* Revert accidental commit r179015.Daniel Jasper2013-04-081-12/+14
| | | | llvm-svn: 179016
* xDaniel Jasper2013-04-081-14/+12
| | | | llvm-svn: 179015
* Improve formatting of multi-variable DeclStmts.Daniel Jasper2013-04-051-3/+12
| | | | | | | | | | | | | | | | | | | | This fixed llvm.org/PR15670 Before: aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb, c = cccccccccccccccccccc, d = dddddddddddddddddddd; aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb, *c = ccccccccccccccccccc, *d = ddddddddddddddddddd; After: aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb, c = cccccccccccccccccccc, d = dddddddddddddddddddd; aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb, *c = ccccccccccccccccccc, *d = ddddddddddddddddddd; llvm-svn: 178860
* Fix bug discovered with address sanitizer.Daniel Jasper2013-04-041-2/+2
| | | | | | Now, this works again with an empty stack. llvm-svn: 178779
* Improve formatting of for loops and multi-variable DeclStmts.Daniel Jasper2013-04-031-17/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | This combines several related changes: a) Don't break before after the variable types in for loops with a single variable. b) Better indent DeclStmts defining multiple variables. Before: bool aaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa), bbbbbbbbbbbbbbbbbbbbbbbbb = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb); for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa; aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) { } After: bool aaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa), bbbbbbbbbbbbbbbbbbbbbbbbb = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb); for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa; aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) { } llvm-svn: 178641
* Cleanup, add comments and address review comments.Daniel Jasper2013-04-031-9/+18
| | | | | | No functional changes. llvm-svn: 178626
* Fix some inconsistent use of indentation.Daniel Jasper2013-04-021-15/+39
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Basically we have always special-cased the top-level statement of an unwrapped line (the one with ParenLevel == 0) and that lead to several inconsistencies. All added tests were formatted in a strange way, for example: Before: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(); if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) { } After: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(); if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) { } llvm-svn: 178542
* Improve formatting of function types.Daniel Jasper2013-04-011-1/+0
| | | | | | Before: void * (*a)(int *, SomeType *); After: void *(*a)(int *, SomeType *); llvm-svn: 178474
* Insert extra new line before access specifiers.Alexander Kornienko2013-03-271-18/+15
| | | | | | | | | | | | | | Summary: Insert extra new line before access specifiers. Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D581 llvm-svn: 178149
* Split line commentsAlexander Kornienko2013-03-271-33/+86
| | | | | | | | | | | | | | | | Summary: Split line comments that exceed column limit + fixed leading whitespace handling when splitting block comments. Reviewers: djasper, klimek Reviewed By: djasper CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D577 llvm-svn: 178133
* Align comments to surrounding unformatted comments.Daniel Jasper2013-03-221-3/+18
| | | | | | | | | | | | | | | Before: int a; // not formatted // formatting this line only After: int a; // not formatted // formatting this line only This makes clang-format stable independent of whether the whole file or single lines are formatted in most cases. llvm-svn: 177739
* Make clang-format understand more line comments.Daniel Jasper2013-03-221-0/+1
| | | | | | | | | | | Apparently one needs to set LangOptions.LineComment. Before "//* */" got reformatted to "/ /* */" as the lexer was returning the token sequence (slash, comment). This could also lead to weird other stuff, e.g. for people that like to using comments like: //**************** llvm-svn: 177720
* Better block comment formatting.Alexander Kornienko2013-03-211-29/+35
| | | | | | | | | | | | | | | | | Summary: 1. When splitting one-line block comment, use indentation and *s. 2. Remove trailing whitespace from all lines of a comment, not only the ones being splitted. 3. Add backslashes for all lines if a comment is used insed a preprocessor directive. Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D557 llvm-svn: 177635
* Remove assertion that can be triggered on bad input.Daniel Jasper2013-03-201-2/+1
| | | | | | | clang-format can't do anything useful, so it should leave the remainder of the line unchanged, but it should not assert/segfault. llvm-svn: 177530
* Do not consider comments when adjusting to local indent style.Daniel Jasper2013-03-201-1/+2
| | | | | | | | | | | | | | | | Before (when only reformatting "int b"): int a; // comment // comment int b; After: int a; // comment // comment int b; This also fixes llvm.org/PR15433. llvm-svn: 177524
* Reduce penalty for breaks after "(" for functions with parameters.Daniel Jasper2013-03-201-2/+2
| | | | | | | | | | | | | Before: aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); After: aaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); llvm-svn: 177521
* Add extra indentation for multiline comparisons.Daniel Jasper2013-03-201-11/+16
| | | | | | | | | | | | | | | | This seems to be generally more desired. Before: if (aaaaaaaa && bbbbbbbb > cccccccc) {} After: if (aaaaaaaa && bbbbbbbb > cccccccc) {} Also: Some formatting cleanup on clang-format's files. llvm-svn: 177514
* Split long lines in multi-line comments.Alexander Kornienko2013-03-191-57/+147
| | | | | | | | | | | | | | Summary: This is implementation for /* */ comments only. Reviewers: djasper, klimek Reviewed By: djasper CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D547 llvm-svn: 177415
* Improve formatting of chained calls.Daniel Jasper2013-03-151-8/+16
| | | | | | | | | | | | | | | | clang-format already prevented sequences like: ... SomeParameter).someFunction( ... as those are quite confusing. This failed on: ... SomeParameter).someFunction(otherFunction( ... Fixed in this patch. llvm-svn: 177157
* Indent all lines in a multi-line comment by the same amount.Alexander Kornienko2013-03-151-19/+17
| | | | | | | | | | | | | | | | Summary: Do this to avoid spoling nicely formatted multi-line comments (e.g. with code examples or similar stuff). Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D544 llvm-svn: 177153
* Multi-line comment alignmentAlexander Kornienko2013-03-141-12/+45
| | | | | | | | | | | | | | | | | | | | | | | | Summary: Aligns continuation lines of multi-line comments to the base indentation level +1: class A { /* * test */ void f() {} }; The first revision is work in progress. The implementation is not yet complete. Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D541 llvm-svn: 177080
* Basic support for formatting asm() statments.Daniel Jasper2013-03-141-2/+2
| | | | llvm-svn: 177073
* Fix comment indentation before labels.Daniel Jasper2013-03-131-0/+7
| | | | | | | | | | | | | | | | | | Before: switch (x) { // if 1, do f() case 1: f(); } After: switch (x) { // if 1, do f() case 1: f(); } llvm-svn: 176953
* Added AnnotatedToken::isOneOf + a few other refactoringsAlexander Kornienko2013-03-131-77/+65
| | | | | | | | | | | | | | Summary: <subj> Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D536 llvm-svn: 176951
* Fix formatting of new arrays of pointers.Daniel Jasper2013-03-131-6/+6
| | | | | | | | | | | | | | Before: A = new SomeType * [Length]; A = new SomeType *[Length](); After: A = new SomeType *[Length]; A = new SomeType *[Length](); Small formatting cleanups with clang-format. llvm-svn: 176936
* Fixes breaking of string literals.Manuel Klimek2013-03-081-2/+56
| | | | | | | | | 1. We now ignore all non-default string literals, including raw literals. 2. We do not break inside escape sequences any more. FIXME: We still break in trigraphs. llvm-svn: 176710
* Remove unncessary whitespace when triggered on empty line.Daniel Jasper2013-03-071-10/+22
| | | | | | | | | | | | | | | | With the cursor located at "I", clang-format would not do anything to: int a; I int b; With this patch, it reduces the number of empty lines as necessary, and removes unnecessary whitespace. It does not change/reformat "int a;" or "int b;". llvm-svn: 176650
* Make sure to not split string literals at the first character.Manuel Klimek2013-03-041-2/+2
| | | | llvm-svn: 176447
* Format a line if a range in its leading whitespace was selected.Daniel Jasper2013-03-041-1/+5
| | | | | | | | | | | With [] marking the selected range, clang-format invoked on [ ] int a; Would so far not reformat anything. With this patch, it formats a line if its leading whitespace is touched. llvm-svn: 176435
* Remove whitespace at end of file.Daniel Jasper2013-03-011-1/+9
| | | | | | This fixes the rest of llvm.org/PR15062. llvm-svn: 176361
* Normal indent for last element of builder-type call.Daniel Jasper2013-03-011-5/+13
| | | | | | | | | | | | | | | | | | | | | | In builder type call, we indent to the laster function calls. However, for the last element of such a call, we don't need to do so, as that normally just wastes space and does not increase readability. Before: aaaaaa->aaaaaa->aaaaaa( // break aaaaaa); aaaaaaaaaaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaa ->aaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); After: aaaaaa->aaaaaa->aaaaaa( // break aaaaaa); aaaaaaaaaaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); llvm-svn: 176352
* Remove trailing whitespace of line comments.Daniel Jasper2013-03-011-14/+29
| | | | | | This fixed llvm.org/PR15378. llvm-svn: 176351
* Implements breaking string literals at slashes.Manuel Klimek2013-03-011-6/+11
| | | | | | | | | We now break at a slash if we do not find a space to break on. Also fixes a bug where we would go over the limit when breaking the second line. llvm-svn: 176350
* Implement fallback split point for string literals.Manuel Klimek2013-03-011-2/+7
| | | | | | | | If we don't find a natural split point (currently space) in a string literal protruding over the line, we just split at the last possible point. llvm-svn: 176349
* Improve formatting of #defines.Daniel Jasper2013-02-281-5/+10
| | | | | | | | | | | Two improvements: 1) Always leave at least one space before "\". Otherwise is can look bad and there is a risk of unwillingly joining to characters to a different token. 2) Use the full column limit for single-line #defines. Fixes llvm.org/PR15148 llvm-svn: 176245
* Fix bug when formatting "A<A<A>>".Daniel Jasper2013-02-281-0/+1
| | | | | | | | | | | Before: A<A<A>> ReadKansas(int aaaaaaaaaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaaaaa); Before: A<A<A>> ReadKansas(int aaaaaaaaaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaaaaa); llvm-svn: 176244
* Fix incorrect recognition of bin-packing.Daniel Jasper2013-02-281-1/+2
| | | | | | | | | | | | | | | | | Before (in Google style): Constructor() : aaaaa(aaaaaa), aaaaa(aaaaaa), aaaaa(aaaaaa), aaaaa(aaaaaa), aaaaa( aaaaaa) {} After: Constructor() : aaaaa(aaaaaa), aaaaa(aaaaaa), aaaaa(aaaaaa), aaaaa(aaaaaa), aaaaa(aaaaaa) {} llvm-svn: 176242
* Enable bin-packing in Google style.Daniel Jasper2013-02-271-1/+7
| | | | | | | | | | | | | | After some discussions, it seems that this is the better path in the long run. Does not change Chromium style, as there, bin packing is forbidden by the style guide. Also fix two minor bugs wrt. formatting: 1. If a call parameter is a function call itself and is split before the "." or "->", split before the next parameter. 2. If a call parameter is string literal that has to be split onto two lines, split before the next parameter. llvm-svn: 176177
* Only keep empty lines in unwrapped lines if they preceed a line comment.Daniel Jasper2013-02-261-3/+5
| | | | | | | | | | | | | | | | | | Empty lines followed by line comments are often used to highlight the comment. Empty lines somewhere else are usually left over from manual or automatic formatting and should probably be removed. Before (clang-format would keep): S s = { a, b }; After: S s = { a, b }; llvm-svn: 176086
* Only break string literals as a last resort.Daniel Jasper2013-02-261-1/+1
| | | | | | | | | | | | | | | | | We might want to move towards doing this if the formatting can be significantly improved, but we need to carefully evaluate the different situations first. Before (the string literal was split by clang-format here): aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaa("aaa aaaaa aaa aaa aaaaa aaa " "aaaaa aaa aaa aaaaaa")); After: aaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaa, aaaaaa("aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa")); llvm-svn: 176084
* Re-add hack that caused regression.Daniel Jasper2013-02-251-1/+3
| | | | | | | | | | | | | | | | | | | This fixes llvm.org/PR15350. Before: Constructor(int Parameter = 0) : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa), aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {} After: Constructor(int Parameter = 0) : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa), aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {} I think the correct solution is to put the VariablePos into ParenState, not LineState. Added FIXME. llvm-svn: 176027
OpenPOWER on IntegriCloud