summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/Format.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Improve detection for preventing certain kind of formatting patterns.Daniel Jasper2013-07-051-14/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a better implementation of r183097. The main purpose is to prevent certain constructs to be formatted "like a block of text". Before: aaaaaaaaaaaaa< aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa< aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb); aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[ dddddddddddddddddddddddddddddddddddddddddddddddddddddddd]; After: aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>( bbbbbbbbbbbbbbbbbbbbbbbb); aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[ dddddddddddddddddddddddddddddddddddddddddddddddddddddddd]; llvm-svn: 185687
* Fixed typo: NoneComment -> NonComment, no other changes.Alexander Kornienko2013-07-041-10/+10
| | | | llvm-svn: 185640
* Added AlwaysBreakBeforeMultilineStrings option.Alexander Kornienko2013-07-041-0/+4
| | | | | | | | | | | | | | | | | Summary: Always breaking before multiline strings can help format complex expressions containing multiline strings more consistently, and avoid consuming too much horizontal space. Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D1097 llvm-svn: 185622
* Don't insert confusing line breaks in comparisons.Daniel Jasper2013-07-031-2/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | In general, clang-format breaks after an operator if the LHS spans multiple lines. Otherwise, this can lead to confusing effects and effectively hide the operator precendence, e.g. in if (aaaaaaaaaaaaaa == bbbbbbbbbbbbbb && c) { ... This patch removes this rule for comparisons, if the LHS is not a binary expression itself as many users were wondering why clang-format inserts an unnecessary linebreak. Before: if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) { ... After: if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) { ... In the long run, we might: - Want to do this for other binary expressions as well. - Do this only if the RHS is short or even only if it is a literal. llvm-svn: 185530
* Avoid column limit violation in block comments in certain cases.Alexander Kornienko2013-07-011-4/+10
| | | | | | | | | | | | | | | | | Summary: Add penalty when an excessively long line in a block comment can not be broken on a leading whitespace. Lack of this addition can lead to severe column width violations when they can be easily avoided. Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D1071 llvm-svn: 185337
* Put helper classes into anonymous namespace.Craig Topper2013-06-301-0/+4
| | | | llvm-svn: 185295
* Use lexing mode based on FormatStyle.Standard.Alexander Kornienko2013-06-281-3/+4
| | | | | | | | | | | | | | | | | | | | | Summary: Some valid pre-C++11 constructs change meaning when lexed in C++11 mode, e.g. #define x(_a) printf("foo"_a); (example from http://llvm.org/bugs/show_bug.cgi?id=16342). "foo"_a is treated as a user-defined string literal when parsed in C++11 mode. In order to deal with this correctly, we need to set lexing mode according to which standard the code conforms to. We already have a configuration value for this (FormatStyle.Standard), which seems to be appropriate to use in this case as well. Reviewers: klimek CC: cfe-commits, gribozavr Differential Revision: http://llvm-reviews.chandlerc.com/D1028 llvm-svn: 185149
* Fix a comment.Nico Weber2013-06-261-1/+1
| | | | llvm-svn: 184905
* Run clang-format on lib/Format code after r184894. No other changes.Nico Weber2013-06-261-5/+5
| | | | llvm-svn: 184896
* Add an option to not indent declarations when breaking after the type.Manuel Klimek2013-06-211-1/+6
| | | | | | Make that option the default for LLVM style. llvm-svn: 184563
* 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
* Fixes incorrect indentation of line comments after break and re-alignment.Alexander Kornienko2013-06-171-2/+8
| | | | | | | | | | | | | | | | | | | 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-141-19/+13
| | | | | | | | | | | | | | | | | | | | | | | 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-121-1/+1
| | | | | | | | | | | | | | | | 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
* Improved handling of escaped newlines at the token start.Alexander Kornienko2013-06-071-33/+16
| | | | | | | | | | | | | | 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-071-4/+18
| | | | | | | | | | | | | | | | | | | | | 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
* UTF-8 support for clang-format.Alexander Kornienko2013-06-051-37/+56
| | | | | | | | | | | | | | | | | | | | 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
* Let clang-format remove empty lines before "}".Daniel Jasper2013-06-031-0/+5
| | | | | | | | | | | | | | | | | 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
* 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
* 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-291-156/+137
| | | | | | | | | | 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 option to always break template declarations.Daniel Jasper2013-05-291-0/+4
| | | | | | | | | | | | | | 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
* Make UnwrappedLines and AnnotatedToken contain pointers to FormatToken.Manuel Klimek2013-05-281-57/+65
| | | | | | The FormatToken is now not copyable any more. llvm-svn: 182772
* A first step towards giving format tokens pointer identity.Manuel Klimek2013-05-281-42/+55
| | | | | | | | | | | 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-281-3/+7
| | | | llvm-svn: 182767
* 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
* Major refactoring of BreakableToken.Manuel Klimek2013-05-271-19/+26
| | | | | | | | | | | | | | | | | | | 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
* 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
* Improve formatting of braced lists.Daniel Jasper2013-05-231-3/+2
| | | | | | Before: vector<int> v{ -1}; After: vector<int> v{-1}; llvm-svn: 182597
* Use a SourceRange for the whitespace location in FormatToken.Manuel Klimek2013-05-231-18/+35
| | | | | | | | | | | Replaces the use of WhitespaceStart + WhitspaceLength. This made a bug in the formatter obvous where we would incorrectly calculate the next column. FIXME: There's a similar bug left regarding TokenLength. We should probably also move to have a TokenRange instead. llvm-svn: 182572
* Improve formatting of braced lists.Daniel Jasper2013-05-231-0/+4
| | | | | | | | | | | | | Before: vector<int> x { 1, 2, 3 }; After: vector<int> x{ 1, 2, 3 }; Also add a style option to remove the spaces inside braced lists, so that the above becomes: std::vector<int> v{1, 2, 3}; llvm-svn: 182570
* Makes whitespace management more consistent.Manuel Klimek2013-05-221-73/+51
| | | | | | | | | | | | | | | | | | | | | | | | | Instead of selectively storing some changes and directly generating replacements for others, we now notify the WhitespaceManager of the whitespace before every token (and optionally with more changes inside tokens). Then, we run over all whitespace in the very end in original source order, where we have all information available to correctly align comments and escaped newlines. The future direction is to pull more of the comment alignment implementation that is now in the BreakableToken into the WhitespaceManager. This fixes a bug when aligning comments or escaped newlines in unwrapped lines that are handled out of order: #define A \ f({ \ g(); \ }); ... now gets correctly layouted. llvm-svn: 182467
* Fix function declaration behavior.Daniel Jasper2013-05-221-1/+1
| | | | | | | | | | | | | | | | | | This only affects styles that prevent bin packing. There, a break after a template declaration also forced a line break after the function name. Before: template <class SomeType, class SomeOtherType> SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {} After: template <class SomeType, class SomeOtherType> SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {} This fixes llvm.org/PR16072. llvm-svn: 182457
* Cut-off clang-format analysis.Daniel Jasper2013-05-221-12/+23
| | | | | | | | | | | | | | | | If clang-format is confronted with long and deeply nested lines (e.g. complex static initializers or function calls), it can currently try too hard to find the optimal solution and never finish. The reason is that the memoization does not work effectively for deeply nested lines. This patch removes an earlier workaround and instead opts for accepting a non-optimal solution in rare cases. However, it only does so only in cases where it would have to analyze an excessive number of states (currently set to 10000 - the most complex line in Format.cpp requires ~800 states) so this should not change the behavior in a relevant way. llvm-svn: 182449
* Minor fix: don't crash on empty configuration file, consider empty ↵Alexander Kornienko2013-05-201-0/+2
| | | | | | configuration files invalid. llvm-svn: 182290
* Clang-format: allow -style="{yaml/json}" on command lineAlexander Kornienko2013-05-191-13/+19
| | | | | | | | | | | | | | Summary: + improved handling of default style and predefined styles. Reviewers: djasper, klimek Reviewed By: klimek CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D813 llvm-svn: 182205
* Slightly modify the formatting rules for braced lists.Daniel Jasper2013-05-171-2/+10
| | | | | | | | | | | | | Basically, the new rule is: The opening "{" always has to be on the same line as the first element if the braced list is nested (e.g. in another braced list or in a function). The solution that clang-format produces almost always adheres to this rule anyway and this makes clang-format significantly faster for larger lists. Added a test cases for the only exception I could find (which doesn't seem to be very important at first sight). llvm-svn: 182082
* Don't insert a break into include lines with trailing comments.Daniel Jasper2013-05-161-1/+2
| | | | llvm-svn: 182003
* Add option to put short loops on a single line.Daniel Jasper2013-05-161-9/+19
| | | | | | | | | | This enables things like: for (int &v : vec) v *= 2; Enabled for Google style. llvm-svn: 182000
* Add a more convenient interface to use clang-format.Daniel Jasper2013-05-161-0/+26
| | | | | | | | | | | | It turns out that several implementations go through the trouble of setting up a SourceManager and Lexer and abstracting this into a function makes usage easier. Also abstracts SourceManager-independent ranges out of tooling::Refactoring and provides a convenience function to create them from line ranges. llvm-svn: 181997
* Comments should not prevent single-line functions.Daniel Jasper2013-05-161-2/+2
| | | | | | | | | | | | | Before: void f() {} void g() { } // comment After: void f() {} void g() {} // comment llvm-svn: 181996
* Add back accidentally deleted line and add test for it.Daniel Jasper2013-05-161-0/+1
| | | | | | | | | | | Before: f("a", "b" "c"); After: f("a", "b" "c"); llvm-svn: 181980
* Don't put short namespace on a single line.Daniel Jasper2013-05-151-3/+6
| | | | | | | | | | | | | | | | | | | | | | | | Before: namespace abc { class SomeClass; } namespace def { void someFunction() {} } After: namespace abc { class Def; } namespace def { void someFunction() {} } Rationale: a) Having anything other than forward declaration on the same line as a namespace looks confusing. b) Formatting namespace-forward-declaration-combinations different from other stuff is inconsistent. c) Wasting vertical space close to such forward declarations really does not affect readability. llvm-svn: 181887
* Break function declarations after multi-line return types.Daniel Jasper2013-05-151-0/+7
| | | | | | | | | | | | | | | Before: template <typename A> SomeLoooooooooooooooooooooongType< typename some_namespace::SomeOtherType<A>::Type> Function() {} After: template <typename A> SomeLoooooooooooooooooooooongType< typename some_namespace::SomeOtherType<A>::Type> Function() {} llvm-svn: 181877
* Don't merge one-line functions in weird brace styles.Daniel Jasper2013-05-151-1/+2
| | | | llvm-svn: 181872
* Remove diagnostics from clang-format.Daniel Jasper2013-05-151-21/+5
| | | | | | | We only ever implemented one and that one is not actually all that helpful (e.g. gets incorrectly triggered by macros). llvm-svn: 181871
* Fix expression breaking for one-parameter-per-line styles.Daniel Jasper2013-05-141-1/+2
| | | | | | | | | | | | | | Before: if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} After: if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} llvm-svn: 181828
OpenPOWER on IntegriCloud