summaryrefslogtreecommitdiff
path: root/unittests/Format/FormatTest.cpp
Commit message (Collapse)AuthorAgeFilesLines
...
* clang-format: Fix SpacesInParentheses with fully qualified names.Daniel Jasper2018-03-221-0/+1
| | | | | | | | | | | When SpacesInParentheses is set to true clang-format does not add a space before fully qualified names. For example: do_something(::globalVar ); Fix by Darby Payne. Thank you! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@328200 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Add a few more Core Graphics identifiers to ObjC heuristicBen Hamilton2018-03-221-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | Summary: We received reports of the Objective-C style guesser getting a false negative on header files like: CGSize SizeOfThing(MyThing thing); This adds more Core Graphics identifiers to the Objective-C style guesser. Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: jolesiak, djasper Reviewed By: jolesiak, djasper Subscribers: krasimir, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44632 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@328175 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Remove empty lines before }[;] // commentKrasimir Georgiev2018-03-191-2/+14
| | | | | | | | | | | | Summary: This addresses bug 36766 and a FIXME in tests about empty lines before `}[;] // comment` lines. Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44631 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@327861 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Improve detection of Objective-C block typesBen Hamilton2018-03-121-0/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, clang-format would detect the following as an Objective-C block type: FOO(^); when it actually must be a C or C++ macro dealing with an XOR statement or an XOR operator overload. According to the Clang Block Language Spec: https://clang.llvm.org/docs/BlockLanguageSpec.html block types are of the form: int (^)(char, float) and block variables of block type are of the form: void (^blockReturningVoidWithVoidArgument)(void); int (^blockReturningIntWithIntAndCharArguments)(int, char); void (^arrayOfTenBlocksReturningVoidWithIntArgument[10])(int); This tightens up the detection so we don't unnecessarily detect C macros which pass in the XOR operator. Depends On D43904 Test Plan: New tests added. Ran tests with: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: krasimir, jolesiak, djasper Reviewed By: djasper Subscribers: djasper, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D43906 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@327285 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Don't detect C++11 attribute specifiers as ObjCBen Hamilton2018-03-121-16/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, clang-format would detect C++11 and C++17 attribute specifiers like the following as Objective-C method invocations: [[noreturn]]; [[clang::fallthrough]]; [[noreturn, deprecated("so sorry")]]; [[using gsl: suppress("type")]]; To fix this, I ported part of the logic from tools/clang/lib/Parse/ParseTentative.cpp into TokenAnnotator.cpp so we can explicitly parse and identify C++11 attribute specifiers. This allows the guessLanguage() and getStyle() APIs to correctly guess files containing the C++11 attribute specifiers as C++, not Objective-C. Test Plan: New tests added. Ran tests with: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: krasimir, jolesiak, djasper Reviewed By: djasper Subscribers: aaron.ballman, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D43902 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@327284 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Improve detection of ObjC for-in statementsBen Hamilton2018-03-061-0/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, clang-format would detect the following as an Objective-C for-in statement: for (int x = in.value(); ...) {} because the logic only decided a for-loop was definitely *not* an Objective-C for-in loop after it saw a semicolon or a colon. To fix this, I delayed the decision of whether this was a for-in statement until after we found the matching right-paren, at which point we know if we've seen a semicolon or not. Test Plan: New tests added. Ran tests with: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: krasimir, jolesiak Reviewed By: jolesiak Subscribers: djasper, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D43904 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326815 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] fix handling of consecutive unary operatorsKrasimir Georgiev2018-03-061-0/+2
| | | | | | | | | | | | | | | | | | | | | | Summary: Code that used to be formatted as `if (! + object) {` is now formatted as `if (!+object) {` (we have a particular object in our codebase where unary `operator+` is overloaded to return the underlying value, which in this case is a `bool`) We still preserve the TypeScript behavior where `!` is a trailing non-null operator. (This is already tested by an existing unit test in `FormatTestJS.cpp`) It doesn't appear like handling of consecutive unary operators are tested in general, so I added another test for completeness Patch contributed by @kevinl! Reviewers: krasimir Reviewed By: krasimir Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D43312 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326792 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Add SpaceBeforeColon optionFrancois Ferrand2018-03-011-0/+111
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: When disabled, this option allows removing the space before colon, making it act more like the semi-colon. When enabled (default), the current behavior is not affected. This mostly affects C++11 loop, initializer list, inheritance list and container literals: class Foo: Bar {} Foo::Foo(): a(a) {} for (auto i: myList) {} f({a: 1, b: 2, c: 3}); Reviewers: krasimir, djasper Reviewed By: djasper Subscribers: xvallspl, teemperor, karies, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D32525 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326426 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Tidy up new API guessLanguage()Ben Hamilton2018-02-271-26/+8
| | | | | | | | | | | | | | | | | | Summary: This fixes a few issues djasper@ brought up in his review of D43522. Test Plan: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D43598 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326205 91177308-0d34-0410-b5e6-96231b3b80d8
* Resolve build bot problems in unittests/Format/FormatTest.cppBjorn Pettersson2018-02-261-1/+2
| | | | | | | | | | | | | | | | | | | | | | Summary: Make the new GetStyleWithEmptyFileName test case independent of the file system used when running the test. Since the test is supposed to use the fallback "Google" style we now use a InMemoryFileSystem to make sure that we do not accidentaly find a .clang-format file in the real file system. That could for example happen when having the build directory inside the llvm och clang repo (as there is a .clang-format file inside the repos). Reviewers: vsapsai, jolesiak, krasimir, benhamilton Reviewed By: krasimir Subscribers: uabelho, twoh, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D43732 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326086 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Fix regression when getStyle() called with empty filenameBen Hamilton2018-02-211-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | Summary: D43522 caused an assertion failure when getStyle() was called with an empty filename: P8065 This adds a test to reproduce the failure and fixes the issue by ensuring we never pass an empty filename to Environment::CreateVirtualEnvironment(). Test Plan: New test added. Ran test with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Before diff, test failed with P8065. Now, test passes. Reviewers: vsapsai, jolesiak, krasimir Reviewed By: vsapsai Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D43590 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325722 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] New API guessLanguage()Ben Hamilton2018-02-211-0/+28
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: For clients which don't have a filesystem, calling getStyle() doesn't make much sense (there's no .clang-format files to search for). In this diff, I hoist out the language-guessing logic from getStyle() and move it into a new API guessLanguage(). I also added support for guessing the language of files which have no extension (they could be C++ or ObjC). Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: jolesiak, krasimir Reviewed By: jolesiak, krasimir Subscribers: klimek, cfe-commits, sammccall Differential Revision: https://reviews.llvm.org/D43522 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325691 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Re-land: Fixup #include guard indents after parseFile()Mark Zeren2018-02-051-0/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: When a preprocessor indent closes after the last line of normal code we do not correctly fixup include guard indents. For example: #ifndef HEADER_H #define HEADER_H #if 1 int i; # define A 0 #endif #endif incorrectly reformats to: #ifndef HEADER_H #define HEADER_H #if 1 int i; # define A 0 # endif #endif To resolve this issue we must fixup levels after parseFile(). Delaying the fixup introduces a new state, so consolidate include guard search state into an enum. Reviewers: krasimir, klimek Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D42035 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@324246 91177308-0d34-0410-b5e6-96231b3b80d8
* Revert "[clang-format] Fixup #include guard indents after parseFile()"Mark Zeren2018-02-051-14/+0
| | | | | | | | This reverts r324238 | mzeren-vmw | 2018-02-05 06:35:54 -0800 (Mon, 05 Feb 2018) | 35 lines Incorrect version pushed upstream. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@324239 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Fixup #include guard indents after parseFile()Mark Zeren2018-02-051-0/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: When a preprocessor indent closes after the last line of normal code we do not correctly fixup include guard indents. For example: #ifndef HEADER_H #define HEADER_H #if 1 int i; # define A 0 #endif #endif incorrectly reformats to: #ifndef HEADER_H #define HEADER_H #if 1 int i; # define A 0 # endif #endif To resolve this issue we must fixup levels after parseFile(). Delaying the fixup introduces a new state, so consolidate include guard search state into an enum. Reviewers: krasimir, klimek Reviewed By: krasimir Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D42035 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@324238 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Align preprocessor comments with #Mark Zeren2018-01-311-15/+79
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: r312125, which introduced preprocessor indentation, shipped with a known issue where "indentation of comments immediately before indented preprocessor lines is toggled on each run". For example these two forms toggle: #ifndef HEADER_H #define HEADER_H #if 1 // comment # define A 0 #endif #endif #ifndef HEADER_H #define HEADER_H #if 1 // comment # define A 0 #endif #endif This happens because we check vertical alignment against the '#' yet indent to the level of the 'define'. This patch resolves this issue by aligning against the '#'. Reviewers: krasimir, klimek, djasper Reviewed By: krasimir Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D42408 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@323904 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Adds a canonical delimiter to raw string formattingKrasimir Georgiev2018-01-191-2/+5
| | | | | | | | | | | | | | | | Summary: This patch adds canonical delimiter support to the raw string formatting. This allows matching delimiters to be updated to the canonical one. Reviewers: bkramer Reviewed By: bkramer Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D42187 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@322956 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Fix shortening blocks in macros causing merged next lineKrasimir Georgiev2018-01-191-0/+17
| | | | | | | | | | | | | | | | Summary: This patch addresses bug 36002, where a combination of options causes the line following a short block in macro to be merged with that macro. Reviewers: bkramer Reviewed By: bkramer Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D42298 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@322954 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] adds enclosing function detection to raw string formattingKrasimir Georgiev2018-01-171-3/+17
| | | | | | | | | | | | | | Summary: This patch adds enclosing function detection to raw string formatting. Reviewers: bkramer Reviewed By: bkramer Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D42167 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@322678 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Reorganize RawStringFormat based on languageKrasimir Georgiev2018-01-171-7/+12
| | | | | | | | | | | | | | | | | | Summary: This patch changes the structure for raw string formatting options by making it language based (enumerate delimiters per language) as opposed to delimiter-based (specify the language for a delimiter). The raw string formatting now uses an appropriate style from the .clang-format file, if exists. Reviewers: bkramer Reviewed By: bkramer Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D42098 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@322634 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] [NFC] Remove commentMark Zeren2018-01-131-2/+0
| | | | | | | Remove inaccurate comment that came in with r312125. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@322448 91177308-0d34-0410-b5e6-96231b3b80d8
* [c++20] P0515R3: Parsing support and basic AST construction for operator <=>.Richard Smith2017-12-141-0/+8
| | | | | | | | | | | | | | | | Adding the new enumerator forced a bunch more changes into this patch than I would have liked. The -Wtautological-compare warning was extended to properly check the new comparison operator, clang-format needed updating because it uses precedence levels as weights for determining where to break lines (and several operators increased their precedence levels with this change), thread-safety analysis needed changes to build its own IL properly for the new operator. All "real" semantic checking for this operator has been deferred to a future patch. For now, we use the relational comparison rules and arbitrarily give the builtin form of the operator a return type of 'void'. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@320707 91177308-0d34-0410-b5e6-96231b3b80d8
* Better trade-off for excess characters vs. staying within the column limits.Manuel Klimek2017-12-011-6/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When we break a long line like: Column limit: 21 | // foo foo foo foo foo foo foo foo foo foo foo foo The local decision when to allow protruding vs. breaking can lead to this outcome (2 excess characters, 2 breaks): // foo foo foo foo foo // foo foo foo foo foo // foo foo While strictly staying within the column limit leads to this strictly better outcome (fully below the column limit, 2 breaks): // foo foo foo foo // foo foo foo foo // foo foo foo foo To get an optimal solution, we would need to consider all combinations of excess characters vs. breaking for all lines, but that would lead to a significant increase in the search space of the algorithm for little gain. Instead, we blindly try both approches and·select the one that leads to the overall lower penalty. Differential Revision: https://reviews.llvm.org/D40605 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319541 91177308-0d34-0410-b5e6-96231b3b80d8
* Restructure how we break tokens.Manuel Klimek2017-11-291-30/+29
| | | | | | | | | | | | | | | | | This fixes some bugs in the reflowing logic and splits out the concerns of reflowing from BreakableToken. Things to do after this patch: - Refactor the breakProtrudingToken function possibly into a class, so we can split it up into methods that operate on the common state. - Optimize whitespace compression when reflowing by using the next possible split point instead of the latest possible split point. - Retry different strategies for reflowing (strictly staying below the column limit vs. allowing excess characters if possible). Differential Revision: https://reviews.llvm.org/D40310 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319314 91177308-0d34-0410-b5e6-96231b3b80d8
* clang-format: remove trailing lines in lamdas and arrow functions.Martin Probst2017-11-171-3/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: clang-format already removes empty lines at the beginning & end of blocks: int x() { foo(); // lines before and after will be removed. } However because lamdas and arrow functions are parsed as expressions, the existing logic to remove empty lines in UnwrappedLineFormatter doesn't handle them. This change special cases arrow functions in ContinuationIndenter to remove empty lines: x = []() { foo(); // lines before and after will now be removed. }; Reviewers: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D40178 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318537 91177308-0d34-0410-b5e6-96231b3b80d8
* Implement more accurate penalty & trade-offs while breaking protruding tokens.Manuel Klimek2017-11-171-7/+108
| | | | | | | | For each line that we break in a protruding token, compute whether the penalty of breaking is actually larger than the penalty of the excess characters. Only break if that is the case. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318515 91177308-0d34-0410-b5e6-96231b3b80d8
* Refactor ContinuationIndenter's breakProtrudingToken logic.Manuel Klimek2017-11-141-1/+2
| | | | | | | | | | | Create more orthogonal pieces. The restructuring made it easy to try out several alternatives to D33589, and while none of the alternatives turned out to be the right solution, the underlying simplification of the structure is helpful. Differential Revision: https://reviews.llvm.org/D39900 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318141 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Handle unary operator overload with arguments and specifiersDaniel Jasper2017-11-061-0/+1
| | | | | | | | | | | | Before: int operator++(int)noexcept; After: int operator++(int) noexcept; Patch by Igor Sugak. Thank you! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@317473 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Handle CRLF correctly when formatting escaped newlinesKrasimir Georgiev2017-10-301-4/+30
| | | | | | | | | | Subscribers: klimek Differential Revision: https://reviews.llvm.org/D39420 Contributed by @peterbudai! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316910 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Format raw string literalsKrasimir Georgiev2017-10-301-0/+14
| | | | | | | | | | | | | | | Summary: This patch adds raw string literal formatting. Reviewers: djasper, klimek Reviewed By: klimek Subscribers: klimek, mgorny Differential Revision: https://reviews.llvm.org/D35943 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316903 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Fix regression about short functions after #elseKrasimir Georgiev2017-10-021-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This patch fixes a regression introduced in r312904, where the formatter confuses the `else` in `#else` with an `else` of an `if-else` statement. For example, formatting this code with google style ``` #ifdef A int f() {} #else int f() {} #endif ``` resulted in ``` #ifdef A int f() {} #else int f() { } #endif ``` Reviewers: sammccall Reviewed By: sammccall Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D37973 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@314683 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Fix FixNamespaceComments when BraceWrapping AfterNamespace is ↵Marek Kurdej2017-09-271-5/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | true. Summary: NamespaceEndCommentsFixer did not fix namespace comments when the brace opening the namespace was not on the same line as the "namespace" keyword. It occurs in Allman, GNU and Linux styles and whenever BraceWrapping.AfterNamespace is true. Before: ```lang=cpp namespace a { void f(); void g(); } ``` After: ```lang=cpp namespace a { void f(); void g(); } // namespace a ``` Reviewers: krasimir Reviewed By: krasimir Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D37904 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@314279 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Adjust space around &/&& of structured bindingsChih-Hung Hsieh2017-09-271-9/+44
| | | | | | | | | | | | | Keep space before or after the &/&& tokens, but not both. For example, auto [x,y] = a; auto &[xr, yr] = a; // LLVM style auto& [xr, yr] = a; // google style Differential Revision:https://reviews.llvm.org/D35743 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@314264 91177308-0d34-0410-b5e6-96231b3b80d8
* Fix clang-format's detection of structured bindings.Manuel Klimek2017-09-201-0/+5
| | | | | | | | | | | | | | | | | | | Correctly determine when [ is part of a structured binding instead of a lambda. To be able to reuse the implementation already available, this patch also: - sets the Previous link of FormatTokens in the UnwrappedLineParser - moves the isCppStructuredBinding function into FormatToken Before: auto const const &&[x, y] { A *i }; After: auto const const && [x, y]{A * i}; Fixing formatting of the type of the structured binding is still missing. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313742 91177308-0d34-0410-b5e6-96231b3b80d8
* Fix formatting of lambda introducers with initializers.Manuel Klimek2017-09-191-0/+2
| | | | | | | | | | | | | | | | | | | | | Most of the work was already done when we introduced a look-behind based lambda introducer detection. This patch finishes the transition by completely relying on the simple lambda introducer detection and simply recursing into normal brace-parsing code to parse until the end of the introducer. This fixes initializers in lambdas, including nested lambdas. Before: auto a = [b = [c = 42]{}]{}; auto b = [c = &i + 23]{}; After: auto a = [b = [c = 42] {}] {}; auto b = [c = &i + 23] {}; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313622 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] New flag - BraceWrapping.AfterExternBlockKrasimir Georgiev2017-09-151-1/+37
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: Bug: https://bugs.llvm.org/show_bug.cgi?id=34016 - **"extern C part"** **Problem:** Due to the lack of "brace wrapping extern" flag, clang format does parse the block after **extern** keyword moving the opening bracket to the header line always! **Patch description:** A new style added, new configuration flag - **BraceWrapping.AfterExternBlock** that allows us to decide whether we want a break before brace or not. Reviewers: djasper, krasimir Reviewed By: krasimir Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D37845 Contributed by @PriMee! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313354 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Fixed one-line if statementKrasimir Georgiev2017-09-111-0/+79
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: **Short overview:** Fixed bug: https://bugs.llvm.org/show_bug.cgi?id=34001 Clang-format bug resulting in a strange behavior of control statements short blocks. Different flags combinations do not guarantee expected result. Turned on option AllowShortBlocksOnASingleLine does not work as intended. **Description of the problem:** Cpp source file UnwrappedLineFormatter does not handle AllowShortBlocksOnASingleLine flag as it should. Putting a single-line control statement without any braces, clang-format works as expected (depending on AllowShortIfStatementOnASingleLine or AllowShortLoopsOnASingleLine value). Putting a single-line control statement in braces, we can observe strange and incorrect behavior. Our short block is intercepted by tryFitMultipleLinesInOne function. The function returns a number of lines to be merged. Unfortunately, our control statement block is not covered properly. There are several if-return statements, but none of them handles our block. A block is identified by the line first token and by left and right braces. A function block works as expected, there is such an if-return statement doing proper job. A control statement block, from the other hand, falls into strange conditional construct, which depends on BraceWrapping.AfterFunction flag (with condition that the line’s last token is left brace, what is possible in our case) or goes even further. That should definitely not happen. **Description of the patch:** By adding three different if statements, we guarantee that our short control statement block, however it looks like (different brace wrapping flags may be turned on), is handled properly and does not fall into wrong conditional construct. Depending on appropriate options we return either 0 (when something disturbs our merging attempt) or let another function (tryMergeSimpleBlock) take the responsibility of returned result (number of merged lines). Nevertheless, one more correction is required in mentioned tryMergeSimpleBlock function. The function, previously, returned either 0 or 2. The problem was that this did not handle the case when our block had the left brace in a separate line, not the header one. After change, after adding condition, we return the result compatible with block’s structure. In case of left brace in the header’s line we do everything as before the patch. In case of left brace in a separate line we do the job similar to the one we do in case of a “non-header left brace” function short block. To be precise, we try to merge the block ignoring the header line. Then, if success, we increment our returned result. **After fix:** **CONFIG:** ``` AllowShortBlocksOnASingleLine: true AllowShortIfStatementsOnASingleLine: true BreakBeforeBraces: Custom BraceWrapping: { AfterClass: true, AfterControlStatement: true, AfterEnum: true, AfterFunction: true, AfterNamespace: false, AfterStruct: true, AfterUnion: true, BeforeCatch: true, BeforeElse: true } ``` **BEFORE:** ``` if (statement) doSomething(); if (statement) { doSomething(); } if (statement) { doSomething(); } if (statement) { doSomething(); } if (statement) doSomething(); if (statement) { doSomething1(); doSomething2(); } ``` **AFTER:** ``` if (statement) doSomething(); if (statement) { doSomething(); } if (statement) { doSomething(); } if (statement) { doSomething(); } if (statement) doSomething(); if (statement) { doSomething1(); doSomething2(); } ``` Contributed by @PriMee! Reviewers: krasimir, djasper Reviewed By: krasimir Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D37140 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312904 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Add support for C++17 structured bindings.Marek Kurdej2017-09-071-0/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Before: ``` auto[a, b] = f(); ``` After: ``` auto [a, b] = f(); ``` or, if SpacesInSquareBrackets is true: ``` auto [ a, b ] = f(); ``` Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D37132 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312723 91177308-0d34-0410-b5e6-96231b3b80d8
* clang-format: Fix indentation of macros in include guards (after r312125).Daniel Jasper2017-09-041-1/+9
| | | | | | | | | | | | | | | | | | | | | | | | Before: #ifndef A_H #define A_H #define A() \ int i; \ int j; #endif // A_H After: #ifndef A_H #define A_H #define A() \ int i; \ int j; #endif // A_H git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312484 91177308-0d34-0410-b5e6-96231b3b80d8
* clang-format: Fix formatting of for loops with multiple increments.Daniel Jasper2017-09-031-0/+4
| | | | | | This fixes llvm.org/PR34366. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312437 91177308-0d34-0410-b5e6-96231b3b80d8
* Disable clang-format's MemoizationTest as it becomes prohibitive with ↵David Blaikie2017-08-311-0/+5
| | | | | | | | | | | | | | | | EXPENSIVE_CHECKS EXPENSIVE_CHECKS enables libstdc++'s library consistency checks, which includes checking the container passed to std::priority_queue for its well-formedness. This makes the clang-format memoization too expensive, so disable it. (it's a necessary feature of libstdc++'s consistency checks that it ruins the required scalability of C++ standard library features - so these workarounds are to be expected if a test ever tries to test scalability in some way, like this test does) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312268 91177308-0d34-0410-b5e6-96231b3b80d8
* clang-format: Add preprocessor directive indentationKrasimir Georgiev2017-08-301-2/+177
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This is an implementation for [bug 17362](https://bugs.llvm.org/attachment.cgi?bugid=17362) which adds support for indenting preprocessor statements inside if/ifdef/endif. This takes previous work from fmauch (https://github.com/fmauch/clang/tree/preprocessor_indent) and makes it into a full feature. The context of this patch is that I'm a VMware intern, and I implemented this because VMware needs the feature. As such, some decisions were made based on what VMware wants, and I would appreciate suggestions on expanding this if necessary to use-cases other people may want. This adds a new enum config option, `IndentPPDirectives`. Values are: * `PPDIS_None` (in config: `None`): ``` #if FOO #if BAR #include <foo> #endif #endif ``` * `PPDIS_AfterHash` (in config: `AfterHash`): ``` #if FOO # if BAR # include <foo> # endif #endif ``` This is meant to work whether spaces or tabs are used for indentation. Preprocessor indentation is independent of indentation for non-preprocessor lines. Preprocessor indentation also attempts to ignore include guards with the checks: 1. Include guards cover the entire file 2. Include guards don't have `#else` 3. Include guards begin with ``` #ifndef <var> #define <var> ``` This patch allows `UnwrappedLineParser::PPBranchLevel` to be decremented to -1 (the initial value is -1) so the variable can be used for indent tracking. Defects: * This patch does not handle the case where there's code between the `#ifndef` and `#define` but all other conditions hold. This is because when the #define line is parsed, `UnwrappedLineParser::Lines` doesn't hold the previous code line yet, so we can't detect it. This is out of the scope of this patch. * This patch does not handle cases where legitimate lines may be outside an include guard. Examples are `#pragma once` and `#pragma GCC diagnostic`, or anything else that does not change the meaning of the file if it's included multiple times. * This does not detect when there is a single non-preprocessor line in front of an include-guard-like structure where other conditions hold because `ScopedLineState` hides the line. * Preprocessor indentation throws off `TokenAnnotator::setCommentLineLevels` so the indentation of comments immediately before indented preprocessor lines is toggled on each run. Fixing this issue appears to be a major change and too much complexity for this patch. Contributed by @euhlmann! Reviewers: djasper, klimek, krasimir Reviewed By: djasper, krasimir Subscribers: krasimir, mzeren-vmw, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D35955 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312125 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Do not format likely xmlKrasimir Georgiev2017-08-291-0/+7
| | | | | | | | | | | | | | | | | Summary: This patch detects the leading '<' in likely xml files and stops formatting in that case. A recent use of a Qt xml file with a .ts extension triggered this: http://doc.qt.io/qt-4.8/linguist-ts-file-format.html Reviewers: djasper Reviewed By: djasper Subscribers: sammccall, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D37136 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311999 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] Fixed typedef enum brace wrappingKrasimir Georgiev2017-08-291-0/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Bug: https://bugs.llvm.org/show_bug.cgi?id=34016 - **Typedef enum part** **Problem:** Clang format does not allow the flag **BraceWrapping.AfterEnum** control the case when our **enum** is preceded by **typedef** keyword (what is common in C language). **Patch description:** Added case to the **"AfterEnum"** flag when our enum does not start a line - is preceded by **typedef** keyword. **After fix:** **CONFIG:** ``` BreakBeforeBraces: Custom BraceWrapping: { AfterClass: true, AfterControlStatement: true, AfterEnum: true, AfterFunction: true, AfterNamespace: false, AfterStruct: true, AfterUnion: true, BeforeCatch: true, BeforeElse: true } ``` **BEFORE:** ``` typedef enum { a, b, c } SomeEnum; ``` **AFTER:** ``` typedef enum { a, b, c } SomeEnum; ``` Contributed by @PriMee! Reviewers: krasimir, djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D37143 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311998 91177308-0d34-0410-b5e6-96231b3b80d8
* [Format] Invert nestingAndIndentLevel pair in WhitespaceManager used forDaniel Jasper2017-08-251-0/+10
| | | | | | | | | | | | | alignments Indent should be compared before nesting level to determine if a token is on the same scope as the one we align with. Because it was inverted, clang-format sometimes tried to align tokens with tokens from outer scopes, causing the assert(Shift >= 0) to fire. This fixes bug #33507. Patch by Beren Minor, thank you! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311792 91177308-0d34-0410-b5e6-96231b3b80d8
* clang-format: Fix left pointer alignment after delctype/typeofKrasimir Georgiev2017-08-141-3/+4
| | | | | | | | | | | | | Change 272124* introduced a regression in spaceRequiredBetween for left aligned pointers to decltype and typeof expressions. This fix adds logic to fix this. The test added is based on a related test in determineStarAmpUsage. Also add test cases for the regression. http://llvm.org/viewvc/llvm-project?view=revision&revision=272124 LLVM bug tracker: https://bugs.llvm.org/show_bug.cgi?id=30407 Differential revision: https://reviews.llvm.org/D35847 Fix contributed by euhlmann! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310831 91177308-0d34-0410-b5e6-96231b3b80d8
* [clang-format] let PointerAlignment dictate spacing of function ref qualifiersJacob Bandes-Storch2017-08-101-2/+4
| | | | | | | | | | | | | | Summary: The original changes for ref qualifiers in rL272537 and rL272548 allowed function const+ref qualifier spacing to diverge from the spacing used for variables. It seems more consistent for `T const& x;` to match `void foo() const&;`. Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D34324 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310544 91177308-0d34-0410-b5e6-96231b3b80d8
* clang-format: Fix bug with ENAS_DontAlign and empty linesJacob Bandes-Storch2017-08-101-0/+24
| | | | | | | | | | This fixes a bug in `ENAS_DontAlign` (introduced in D32733) where blank lines had an EscapedNewlineColumn of 0, causing a subtraction to overflow when converted back to unsigned and leading to runaway memory allocation. Differential Revision: https://reviews.llvm.org/D36019 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310539 91177308-0d34-0410-b5e6-96231b3b80d8
* clang-format: merge short case labels with trailing commentsFrancois Ferrand2017-07-281-0/+71
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: Allow merging short case labels when they actually end with a comment (like a comment after the ``break``) and when followed by switch-level comments (e.g. aligned with next case): switch(a) { case 0: break; // comment at end of case case 1: return value; // comment related to next case // comment related to next case case 2: } Reviewers: krasimir, djasper Reviewed By: krasimir Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D35557 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@309370 91177308-0d34-0410-b5e6-96231b3b80d8
* Fix dereference of pointers in throw statements.Manuel Klimek2017-07-171-0/+1
| | | | | | | | | | | | Before: throw * x; After: throw *x; Patch by Erik Uhlmann. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@308185 91177308-0d34-0410-b5e6-96231b3b80d8