summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format
Commit message (Collapse)AuthorAgeFilesLines
...
* Use the same set of whitespace characters for all operations in BreakableToken.Alexander Kornienko2013-06-201-11/+24
| | | | | | | | | | | | | | | | | 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
* Fixed long-standing issue with incorrect length calculation of multi-line ↵Alexander Kornienko2013-06-191-5/+5
| | | | | | | | | | | | | | | | | | | | | 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
* Split long strings on word boundaries.Alexander Kornienko2013-06-191-0/+5
| | | | | | | | | | | | 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
* Fix a problem in ExpressionParser leading to trailing comments affecting ↵Alexander Kornienko2013-06-171-7/+4
| | | | | | | | | | | | | | | | | | | | | | | | | 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
* Fixes incorrect indentation of line comments after break and re-alignment.Alexander Kornienko2013-06-172-6/+15
| | | | | | | | | | | | | | | | | | | 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
* Don't remove backslashes from block comments.Alexander Kornienko2013-06-143-50/+51
| | | | | | | | | | | | | | | | | | | | | | | 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
* Preserve newlines before block comments in static initializers.Alexander Kornienko2013-06-122-2/+2
| | | | | | | | | | | | | | | | 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
* Insert a space at the start of a line comment in case it starts with an ↵Alexander Kornienko2013-06-114-44/+84
| | | | | | | | | | | | | | | | | | | | 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
* Improved handling of escaped newlines at the token start.Alexander Kornienko2013-06-072-44/+21
| | | | | | | | | | | | | | 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
* Fixed calculation of penalty when breaking tokens.Alexander Kornienko2013-06-074-29/+48
| | | | | | | | | | | | | | | | | | | | | 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
* Fix incorrect line breaking before trailing block comments.Daniel Jasper2013-06-061-1/+1
| | | | | | | | | | | | | | | | | 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
* Fix clang-format's expression parser for leading }s.Daniel Jasper2013-06-061-1/+5
| | | | | | | | 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
* Improve c-style cast detection.Daniel Jasper2013-06-061-1/+4
| | | | | | | | | | | | | | 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
* UTF-8 support for clang-format.Alexander Kornienko2013-06-057-167/+282
| | | | | | | | | | | | | | | | | | | | 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
* Moved FormatToken to a separate header.Alexander Kornienko2013-06-032-255/+282
| | | | llvm-svn: 183115
* Let clang-format remove empty lines before "}".Daniel Jasper2013-06-032-1/+6
| | | | | | | | | | | | | | | | | These lines almost never aid readability. Before: void f() { int i; // some variable } After: void f() { int i; // some variable } llvm-svn: 183112
* Improve detection preventing certain kind of formatting patterns.Daniel Jasper2013-06-031-14/+13
| | | | | | | | | | An oversight in this detection made clang-format unable to format the following nicely: void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb>( cccccccccccccccccccccccccccc); llvm-svn: 183097
* Fix line-breaking problem caused by comment.Daniel Jasper2013-06-031-0/+1
| | | | | | | | | | Before, clang-format would not find a solution for formatting: if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa cccccc) { } llvm-svn: 183096
* Improve recognition of template parameters.Daniel Jasper2013-06-011-1/+8
| | | | | | Before: return a<b &&c> d; After: return a < b && c > d; llvm-svn: 183077
* Improve clang-format's c-style cast detection.Daniel Jasper2013-05-311-11/+27
| | | | | | | | | | | | | | | | 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
* Improve clang-format's error recovery.Daniel Jasper2013-05-311-13/+16
| | | | | | | | | | | | | 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
* Make formatting of empty blocks more consistent.Daniel Jasper2013-05-311-4/+5
| | | | | | | | | | | | | | | | | | | 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
* Properly format nested conditional operators.Daniel Jasper2013-05-311-5/+27
| | | | | | | | | | | | | | | | | | Before: bool aaaaaa = aaaaaaaaaaaaa // ? aaaaaaaaaaaaaaa : bbbbbbbbbbbbbbb // ? ccccccccccccccc : ddddddddddddddd; After: bool aaaaaa = aaaaaaaaaaaaa // ? aaaaaaaaaaaaaaa : bbbbbbbbbbbbbbb // ? ccccccccccccccc : ddddddddddddddd; llvm-svn: 183007
* Fix detection/formatting of braced lists in ternary expressions.Daniel Jasper2013-05-311-2/+2
| | | | | | | | | | | | | | | | 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
* More fixes for clang-format's multiline comment breaking.Daniel Jasper2013-05-301-3/+1
| | | | llvm-svn: 182940
* Fix another clang-format crasher related to multi-line comments.Daniel Jasper2013-05-301-0/+3
| | | | | | | | | | This fixes: /* * * something long going over the column limit. */ llvm-svn: 182932
* Add asserts to guard against regressions.Manuel Klimek2013-05-301-0/+2
| | | | llvm-svn: 182916
* Fix crasher when formatting certain block comments.Daniel Jasper2013-05-301-0/+5
| | | | | | | | | Smallest reproduction: /* ** */ llvm-svn: 182913
* Fixes error when splitting block comments.Manuel Klimek2013-05-291-2/+10
| | | | | | | | 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
* Use a non-recursive implementation to reconstruct line breaks.Manuel Klimek2013-05-291-16/+17
| | | | | | | | | 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
* The second step in the token refactoring.Manuel Klimek2013-05-296-582/+513
| | | | | | | | | | 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
* Add return missing in r182855.Daniel Jasper2013-05-291-0/+1
| | | | llvm-svn: 182856
* Leave some macros on their own lineDaniel Jasper2013-05-291-1/+7
| | | | | | | | | | | | | | | | | | | 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
* Add option to always break template declarations.Daniel Jasper2013-05-292-0/+7
| | | | | | | | | | | | | | 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
* Remove obsolete variable as discovered in post-commit review.Daniel Jasper2013-05-281-4/+0
| | | | llvm-svn: 182796
* Support uniform inits in braced lists.Daniel Jasper2013-05-281-5/+0
| | | | | | | | | | | | | 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
* Fix formatting regression regarding pointers to arrays.Daniel Jasper2013-05-281-1/+1
| | | | | | | | | Before: f( (*PointerToArray)[10]); After: f((*PointerToArray)[10]); This fixes llvm.org/PR16163 llvm-svn: 182777
* Make UnwrappedLines and AnnotatedToken contain pointers to FormatToken.Manuel Klimek2013-05-286-109/+123
| | | | | | The FormatToken is now not copyable any more. llvm-svn: 182772
* A first step towards giving format tokens pointer identity.Manuel Klimek2013-05-283-190/+199
| | | | | | | | | | | 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
* Initial support for designated initializers.Daniel Jasper2013-05-283-11/+22
| | | | llvm-svn: 182767
* Disable tab expansion when counting the columns in block comments.Manuel Klimek2013-05-281-10/+8
| | | | | | | | | | | | | | | | | 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
* Fixes indentation of empty lines in block comments.Manuel Klimek2013-05-281-2/+11
| | | | | | | | | | | 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
* Clean up formatting of function types.Daniel Jasper2013-05-282-12/+11
| | | | | | | | | | | | 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
* Fix formatting of expressions containing ">>".Daniel Jasper2013-05-281-0/+1
| | | | | | | | | | | | | 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
* Remove unreachable returnDavid Blaikie2013-05-271-1/+0
| | | | llvm-svn: 182742
* Improve formatting of templates.Daniel Jasper2013-05-271-1/+2
| | | | | | | | | Before: A < int&& > a; After: A<int &&> a; Also remove obsolete FIXMEs. llvm-svn: 182741
* Major refactoring of BreakableToken.Manuel Klimek2013-05-276-344/+445
| | | | | | | | | | | | | | | | | | | 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
* Improve indentation of assignments.Daniel Jasper2013-05-271-1/+4
| | | | | | | | | | | | | | | Before: unsigned OriginalStartColumn = SourceMgr.getSpellingColumnNumber( Current.FormatTok.getStartOfNonWhitespace()) - 1; After: unsigned OriginalStartColumn = SourceMgr.getSpellingColumnNumber( Current.FormatTok.getStartOfNonWhitespace()) - 1; llvm-svn: 182733
* Address post-review comment from dblakie.Manuel Klimek2013-05-271-1/+1
| | | | llvm-svn: 182732
* Fix hacky way of preventing a certain type of line break.Daniel Jasper2013-05-271-10/+17
| | | | | | | | | | | | | | | | | | | | | | | | 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
OpenPOWER on IntegriCloud