summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format
Commit message (Collapse)AuthorAgeFilesLines
...
* clang-format: tweak formatting of variable initialization blocksFrancois Ferrand2018-05-161-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This patch changes the behavior of PenaltyBreakBeforeFirstCallParameter so that is does not apply after a brace, when Cpp11BracedListStyle is false. This way, variable initialization is wrapped more like an initializer than like a function call, which is more consistent with user expectations for this braced list style. With PenaltyBreakBeforeFirstCallParameter=200, this gives the following code: (with Cpp11BracedListStyle=false) Before : const std::unordered_map<std::string, int> Something::MyHashTable = { { "aaaaaaaaaaaaaaaaaaaaa", 0 }, { "bbbbbbbbbbbbbbbbbbbbb", 1 }, { "ccccccccccccccccccccc", 2 } }; After : const std::unordered_set<std::string> Something::MyUnorderedSet = { { "aaaaaaaaaaaaaaaaaaaaa", 0 }, { "bbbbbbbbbbbbbbbbbbbbb", 1 }, { "ccccccccccccccccccccc", 2 } }; Reviewers: krasimir, djasper, klimek Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D43290 llvm-svn: 332434
* [clang] Update uses of DEBUG macro to LLVM_DEBUG.Nicola Zaghen2018-05-159-63/+70
| | | | | | | | | | | | | The DEBUG() macro is very generic so it might clash with other projects. The renaming was done as follows: - git grep -l 'DEBUG' | xargs sed -i 's/\bDEBUG\s\?(/LLVM_DEBUG(/g' - git diff -U0 master | ../clang/tools/clang-format/clang-format-diff.py -i -p1 -style LLVM Explicitly avoided changing the strings in the clang-format tests. Differential Revision: https://reviews.llvm.org/D44975 llvm-svn: 332350
* [Tooling] Pull #include manipulation code from clangFormat into libToolingCore.Eric Liu2018-05-141-393/+5
| | | | | | | | | | | | | | Summary: Also pull #include related style out of FormatStyle as tooling::IncludeStyle. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: klimek, mgorny, cfe-commits, djasper Differential Revision: https://reviews.llvm.org/D46496 llvm-svn: 332287
* [clang-format] Move #include related style to libToolingCoreEric Liu2018-05-141-34/+22
| | | | | | | | | | | | | | Summary: This will be shared by include insertion/deletion library. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: mgorny, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D46758 llvm-svn: 332284
* [clang-format] Continue after non-scope-closers in getLengthToMatchingParenKrasimir Georgiev2018-05-141-1/+3
| | | | | | | | | | | | | Summary: This fixes a regression introduced by `r331857` where we stop the search for the End token as soon as we hit a non-scope-closer, which prematurely stops before semicolons for example, which should otherwise be considered as part of the unbreakable tail. Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D46824 llvm-svn: 332225
* Add SourceManagerForFile helper which sets up SourceManager and dependencies ↵Eric Liu2018-05-094-99/+41
| | | | | | | | | | | | | | | | for a single file with code snippet Summary: This can be used to create a virtual environment (incl. VFS, source manager) for code snippets. Reviewers: sammccall, klimek Reviewed By: sammccall Subscribers: klimek, mgorny, cfe-commits Differential Revision: https://reviews.llvm.org/D46176 llvm-svn: 331923
* [clang-format] Respect BreakBeforeClosingBrace while calculating lengthKrasimir Georgiev2018-05-093-20/+87
| | | | | | | | | | | | | | | | | | Summary: This patch makes `getLengthToMatchingParen` respect the `BreakBeforeClosingBrace` ParenState for matching scope closers. In order to distinguish between paren states introduced by real vs. fake parens, I've added the token opening the ParensState to that struct. Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D46519 llvm-svn: 331857
* Remove \brief commands from doxygen comments.Adrian Prantl2018-05-0929-260/+260
| | | | | | | | | | | | | | | | | | | This is similar to the LLVM change https://reviews.llvm.org/D46290. We've been running doxygen with the autobrief option for a couple of years now. This makes the \brief markers into our comments redundant. Since they are a visual distraction and we don't want to encourage more \brief markers in new code either, this patch removes them all. Patch produced by for i in $(git grep -l '\@brief'); do perl -pi -e 's/\@brief //g' $i & done for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done Differential Revision: https://reviews.llvm.org/D46320 llvm-svn: 331834
* [clang-format] Refactor #include insertion/deletion functionality into a class.Eric Liu2018-05-041-102/+252
| | | | | | | | | | | | | | | | | | | | | | | | Summary: The class will be moved into libToolingCore as followup. The new behaviors in this patch: - New #include is inserted in the right position in a #include block to preserver sorted #includes. This is best effort - only works when the block is already sorted. - When inserting multiple #includes to the end of a file which doesn't end with a "\n" character, a "\n" will be prepended to each #include. This is a special and rare case that was previously handled. This is now relaxed to avoid complexity as it's rare in practice. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: klimek, cfe-commits, djasper Differential Revision: https://reviews.llvm.org/D46180 llvm-svn: 331544
* Implement P0482R2, support for char8_t type.Richard Smith2018-05-011-0/+1
| | | | | | | | | | | | This is not yet part of any C++ working draft, and so is controlled by the flag -fchar8_t rather than a -std= flag. (The GCC implementation is controlled by a flag with the same name.) This implementation is experimental, and will be removed or revised substantially to match the proposal as it makes its way through the C++ committee. llvm-svn: 331244
* [clang-format/ObjC] Use getIdentifierInfo() instead of tok::identifierBen Hamilton2018-04-271-4/+21
| | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, we checked tokens for `tok::identifier` to see if they were identifiers inside an Objective-C selector. However, this missed C++ keywords like `new` and `delete`. To fix this, this diff uses `getIdentifierInfo()` to find identifiers or keywords inside Objective-C selectors. Test Plan: New tests added. Ran tests with: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D46143 llvm-svn: 331067
* [clang-format] Start formatting cpp code in raw strings in google styleKrasimir Georgiev2018-04-251-12/+31
| | | | | | | | | | | | | | Summary: This adds some delimiters to detect cpp code in raw strings. Reviewers: klimek Reviewed By: klimek Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D46062 llvm-svn: 330832
* [clang-format] Fix clang-tidy readability problems, NFCIKrasimir Georgiev2018-04-233-6/+6
| | | | llvm-svn: 330574
* Format closing braces when reformatting the line containing the opening brace.Manuel Klimek2018-04-2310-29/+37
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This required a couple of yaks to be shaved: 1. MatchingOpeningBlockLineIndex was misused to also store the closing index; instead, use a second variable, as this doesn't work correctly for "} else {". 2. We needed to change the API of AffectedRangeManager to not use iterators; we always passed in begin / end for the whole container before, so there was no mismatch in generality. 3. We need an extra check to discontinue formatting at the top level, as we now sometimes change the indent of the closing brace, but want to bail out immediately afterwards, for example: void f() { if (a) { } void g(); Previously: void f() { if (a) { } void g(); Now: void f() { if (a) { } void g(); Differential Revision: https://reviews.llvm.org/D45726 llvm-svn: 330573
* Revert "[clang-format] Improve Incomplete detection for (text) protos"Krasimir Georgiev2018-04-231-18/+5
| | | | | | | | This reverts commit r330016. The incomplete detection has too many false positives, picking up typos for hard failures and refusing to format anything in that case. llvm-svn: 330569
* [clang-format] Don't remove empty lines before namespace endingsKrasimir Georgiev2018-04-194-11/+24
| | | | | | | | | | | | | | Summary: This implements an alternative to r327861, namely preserving empty lines before namespace endings. Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45373 llvm-svn: 330324
* [clang-format] Improve Incomplete detection for (text) protosKrasimir Georgiev2018-04-131-5/+18
| | | | | | | | | | | | | | | | | | Summary: This patch improves detection of incomplete code for protos and text protos. This is especially important for text protos in raw string literals, since they might be partial strings concatenated, and we'd like to disable formatting in these cases. Reviewers: sammccall Reviewed By: sammccall Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44203 llvm-svn: 330016
* [clang-format] Do not break after ObjC category open parenBen Hamilton2018-04-121-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, `clang-format` would break Objective-C category extensions after the opening parenthesis to avoid breaking the protocol list: ``` % echo "@interface ccccccccccccc (ccccccccccc) <ccccccccccccc> { }" | \ clang-format -assume-filename=foo.h -style="{BasedOnStyle: llvm, \ ColumnLimit: 40}" @interface ccccccccccccc ( ccccccccccc) <ccccccccccccc> { } ``` This looks fairly odd, as we could have kept the category extension on the previous line. Category extensions are a single item, so they are generally very short compared to protocol lists. We should prefer breaking after the opening `<` of the protocol list over breaking after the opening `(` of the category extension. With this diff, we now avoid breaking after the category extension's open paren, which causes us to break after the protocol list's open angle bracket: ``` % echo "@interface ccccccccccccc (ccccccccccc) <ccccccccccccc> { }" | \ ./bin/clang-format -assume-filename=foo.h -style="{BasedOnStyle: llvm, \ ColumnLimit: 40}" @interface ccccccccccccc (ccccccccccc) < ccccccccccccc> { } ``` Test Plan: New test added. Confirmed test failed before diff and passed after diff by running: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45526 llvm-svn: 329919
* [clang-format] Improve ObjC guessing heuristic by supporting all @keywordsBen Hamilton2018-04-121-4/+7
| | | | | | | | | | | | | | | | | | | | | | | Summary: This diff improves the Objective-C guessing heuristic by replacing the hard-coded list of a subset of Objective-C @keywords with a general check which supports all @keywords. I also added a few more Foundation keywords which were missing from the heuristic. Test Plan: Unit tests updated. Ran tests with: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45521 llvm-svn: 329918
* [clang-format] Don't insert space between ObjC class and lightweight genericBen Hamilton2018-04-121-3/+6
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: In D45185, I added clang-format parser support for Objective-C generics. However, I didn't touch the whitespace logic, so they got the same space logic as Objective-C protocol lists. In every example in the Apple SDK and in the documentation, there is no space between the class name and the opening `<` for the lightweight generic specification, so this diff removes the space and updates the tests. Test Plan: Tests updated. Ran tests with: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45498 llvm-svn: 329917
* [clang-format] Always indent wrapped Objective-C selector namesBen Hamilton2018-04-121-8/+10
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: Currently, indentation of Objective-C method names which are wrapped onto the next line due to a long return type is controlled by the style option `IndentWrappedFunctionNames`. This diff changes the behavior so we always indent wrapped Objective-C selector names. NOTE: I partially reverted https://github.com/llvm-mirror/clang/commit/6159c0fbd1876c7f5f984b4830c664cc78f16e2e / rL242484, as it was causing wrapped selectors to be double-indented. Its tests in FormatTestObjC.cpp still pass. Test Plan: Tests updated. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak, stephanemoore, thakis Reviewed By: djasper Subscribers: stephanemoore, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45004 llvm-svn: 329916
* Fix bugs around handling C++11 attributes.Manuel Klimek2018-04-111-2/+12
| | | | | | | | | | | | | | | | Previously, we would format: int a() { ... } [[unused]] int b() { ... } as... int a() {} [[unused] int b() {} Now we correctly format each on its own line. Similarly, we would detect: [[unused]] int b() { return 42; } As a lambda and leave it on a single line, even if that was disallowed by the format style. llvm-svn: 329816
* Fix typos in clangAlexander Kornienko2018-04-061-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Found via codespell -q 3 -I ../clang-whitelist.txt Where whitelist consists of: archtype cas classs checkk compres definit frome iff inteval ith lod methode nd optin ot pres statics te thru Patch by luzpaz! (This is a subset of D44188 that applies cleanly with a few files that have dubious fixes reverted.) Differential revision: https://reviews.llvm.org/D44188 llvm-svn: 329399
* [clang-format] Support lightweight Objective-C genericsBen Hamilton2018-04-051-3/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, `clang-format` didn't understand lightweight Objective-C generics, which have the form: ``` @interface Foo <KeyType, ValueTypeWithConstraint : Foo, AnotherValueTypeWithGenericConstraint: Bar<Baz>, ... > ... ``` The lightweight generic specifier list appears before the base class, if present, but because it starts with < like the protocol specifier list, `UnwrappedLineParser` was getting confused and failed to parse interfaces with both generics and protocol lists: ``` @interface Foo <KeyType> : NSObject <NSCopying> ``` Since the parsed line would be incomplete, the format result would be very confused (e.g., https://bugs.llvm.org/show_bug.cgi?id=24381). This fixes the issue by explicitly parsing the ObjC lightweight generic conformance list, so the line is fully parsed. Fixes: https://bugs.llvm.org/show_bug.cgi?id=24381 Test Plan: New tests added. Ran tests with: % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45185 llvm-svn: 329298
* [clang-format] Ensure ObjC selectors with 0 args are annotated correctlyBen Hamilton2018-04-051-0/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, clang-format would incorrectly annotate 0-argument Objective-C selector names as TT_TrailingAnnotation: ``` % echo "-(void)foo;" > /tmp/test.m % ./bin/clang-format -debug /tmp/test.m Language: Objective-C ---- Line(0, FSC=0): minus[T=68, OC=0] l_paren[T=68, OC=1] void[T=68, OC=2] r_paren[T=68, OC=6] identifier[T=68, OC=7] semi[T=68, OC=10] Line(0, FSC=0): eof[T=68, OC=0] Run 0... AnnotatedTokens(L=0): M=0 C=0 T=ObjCMethodSpecifier S=1 B=0 BK=0 P=0 Name=minus L=1 PPK=2 FakeLParens= FakeRParens=0 Text='-' M=0 C=1 T=Unknown S=1 B=0 BK=0 P=33 Name=l_paren L=3 PPK=2 FakeLParens= FakeRParens=0 Text='(' M=0 C=1 T=Unknown S=0 B=0 BK=0 P=140 Name=void L=7 PPK=2 FakeLParens= FakeRParens=0 Text='void' M=0 C=0 T=CastRParen S=0 B=0 BK=0 P=43 Name=r_paren L=8 PPK=2 FakeLParens= FakeRParens=0 Text=')' M=0 C=1 T=TrailingAnnotation S=0 B=0 BK=0 P=120 Name=identifier L=11 PPK=2 FakeLParens= FakeRParens=0 Text='foo' M=0 C=0 T=Unknown S=0 B=0 BK=0 P=23 Name=semi L=12 PPK=2 FakeLParens= FakeRParens=0 Text=';' ``` This caused us to incorrectly indent 0-argument wrapped selectors when Style.IndentWrappedFunctionNames was false, as we thought the 0-argument ObjC selector name was actually a trailing annotation (which is always indented). This diff fixes the issue and adds tests. Test Plan: New tests added. Confirmed tests failed before diff. After diff, tests passed. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak Reviewed By: djasper, jolesiak Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44996 llvm-svn: 329297
* [clang-format] Preserve spaces before a percent in (text) protosKrasimir Georgiev2018-04-051-0/+4
| | | | | | | This makes sure that we do not change the meaning of pieces of text with format specifiers. llvm-svn: 329263
* [clang-format/ObjC] Do not detect "[]" as ObjC method expressionBen Hamilton2018-04-031-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: The following C++ code was being detected by `guessLanguage()` as Objective-C: #define FOO(...) auto bar = [] __VA_ARGS__; This was because `[] __VA_ARGS__` is not currently detected as a C++ lambda expression (it has no parens or braces), so `TokenAnnotator::parseSquare()` incorrectly treats the opening square as an ObjC method expression. We have two options to fix this: 1. Parse `[] __VA_ARGS__` explicitly as a C++ lambda 2. Make it so `[]` is never parsed as an Objective-C method expression This diff implements option 2, which causes the `[` to be parsed as `TT_ArraySubscriptLSquare` instead of `TT_ObjCMethodExpr`. Note that when I fixed this, it caused one change in formatting behavior, where the following was implicitly relying on the `[` being parsed as `TT_ObjCMethodExpr`: A<int * []> a; becomes: A<int *[]> a; with `Style.PointerAlignment = Middle`. I don't really know what the desired format is for this syntax; the test was added by Janusz Sobczak and integrated by @djasper in https://github.com/llvm-mirror/clang/commit/b511fe9818829d7ece0cc0b2ce1fbe04a1f0739a . I went ahead and changed the test for now. Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Fixes: https://bugs.llvm.org/show_bug.cgi?id=36248 Reviewers: djasper, jolesiak Reviewed By: djasper Subscribers: klimek, cfe-commits, djasper Differential Revision: https://reviews.llvm.org/D45169 llvm-svn: 329070
* [clang-format/ObjC] Do not insert space after opening brace of ObjC dict literalBen Hamilton2018-04-031-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: D44816 attempted to fix a few cases where `clang-format` incorrectly inserted a space before the closing brace of an Objective-C dictionary literal. This revealed there were still a few cases where we inserted a space after the opening brace of an Objective-C dictionary literal. This fixes the formatting to be consistent and adds more tests. Test Plan: New tests added. Confirmed tests failed before diff and passed after diff. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak, krasimir Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45168 llvm-svn: 329069
* [clang-format] Ensure wrapped ObjC selectors with 1 arg obey ↵Ben Hamilton2018-03-301-6/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | IndentWrappedFunctionNames Summary: In D43121, @Typz introduced logic to avoid indenting 2-or-more argument ObjC selectors too far to the right if the first component of the selector was longer than the others. This had a small side effect of causing wrapped ObjC selectors with exactly 1 argument to not obey IndentWrappedFunctionNames: ``` - (aaaaaaaaaa) aaaaaaaaaa; ``` This diff fixes the issue by ensuring we align wrapped 1-argument ObjC selectors correctly: ``` - (aaaaaaaaaa) aaaaaaaaaa; ``` Test Plan: New tests added. Test failed before change, passed after change. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, klimek, Typz, jolesiak Reviewed By: djasper, jolesiak Subscribers: cfe-commits, Typz Differential Revision: https://reviews.llvm.org/D44994 llvm-svn: 328871
* [clang] Change std::sort to llvm::sort in response to r327219Mandeep Singh Grang2018-03-272-2/+2
| | | | | | | | | | | r327219 added wrappers to std::sort which randomly shuffle the container before sorting. This will help in uncovering non-determinism caused due to undefined sorting order of objects having the same key. To make use of that infrastructure we need to invoke llvm::sort instead of std::sort. llvm-svn: 328636
* [clang-format] Refine ObjC guesser to handle child lines of child linesBen Hamilton2018-03-271-10/+3
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: This fixes an issue brought up by djasper@ in his review of D44790. We handled top-level child lines, but if those child lines themselves had child lines, we didn't handle them. Rather than use recursion (which could blow out the stack), I use a DenseSet to hold the set of lines we haven't yet checked (since order doesn't matter), and update the set to add the children of each line as we check it. Test Plan: New tests added. Confirmed tests failed before fix and passed after fix. Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44831 llvm-svn: 328628
* [clang-format] Do not insert space before closing brace in ObjC dict literalBen Hamilton2018-03-271-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, `clang-format` would sometimes insert a space before the closing brace in an Objective-C dictionary literal. Unlike array literals (which obey `Style.SpacesInContainerLiterals` to add a space after `[` and before `]`), Objective-C dictionary literals currently are not meant to insert a space after `{` and before `}`, regardless of `Style.SpacesInContainerLiterals`. However, some constructs like `@{foo : @(bar)}` caused `clang-format` to insert a space between `)` and `}`. This fixes the issue and adds tests. (I understand the behavior is not consistent between array literals and dictionary literals, but that's existing behavior that's a much larger change.) Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak, Wizard Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44816 llvm-svn: 328627
* Revert "[clang-format] Remove empty lines before }[;] // comment"Krasimir Georgiev2018-03-271-5/+1
| | | | | | | | | This reverts commit r327861. The empty line before namespaces is desired in some places. We need a better approach to handle this. llvm-svn: 328621
* [clang-format] Fix ObjC style guesser to also iterate over child linesBen Hamilton2018-03-221-2/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: When I wrote `ObjCHeaderStyleGuesser`, I incorrectly assumed the correct way to iterate over all tokens in `AnnotatedLine` was to iterate over the linked list tokens starting with `AnnotatedLine::First`. However, `AnnotatedLine` also contains a vector `AnnotedLine::Children` with child `AnnotedLine`s which have their own tokens which we need to iterate over. Because I didn't iterate over the tokens in the children lines, the ObjC style guesser would fail on syntax like: #define FOO ({ NSString *s = ... }) as the statement(s) inside { ... } are child lines. This fixes the bug and adds a test. I confirmed the test failed before the fix, and passed after the fix. Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak, Wizard Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44790 llvm-svn: 328220
* clang-format: Narrow down raw string literal line break exception.Daniel Jasper2018-03-221-4/+4
| | | | | | | | | | | | For multiline raw string literals, we generally want to respect the author's choice of linebreak before the 'R"(' as the rest of the raw string might be aligned to it and we cannot (commonly) modify the content. For single-line raw string literals, this doesn't make any sense and so we should just treat them as regular string literals in this regard. llvm-svn: 328201
* clang-format: Fix SpacesInParentheses with fully qualified names.Daniel Jasper2018-03-221-1/+2
| | | | | | | | | | | 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! llvm-svn: 328200
* [clang-format] Add a few more Core Graphics identifiers to ObjC heuristicBen Hamilton2018-03-221-0/+15
| | | | | | | | | | | | | | | | | | | | | | | | 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 llvm-svn: 328175
* [clang-format] Don't insert space between r_paren and 'new' in ObjC declBen Hamilton2018-03-221-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Previously, clang-format would insert a space between the closing parenthesis and 'new' in the following valid Objective-C declaration: + (instancetype)new; This was because 'new' is treated as a keyword, not an identifier. TokenAnnotator::spaceRequiredBefore() already handled the case where r_paren came before an identifier, so this diff extends it to handle r_paren before 'new'. Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, jolesiak, stephanemoore Reviewed By: djasper, jolesiak, stephanemoore Subscribers: stephanemoore, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44692 llvm-svn: 328174
* [format] Eliminate global destructors.Benjamin Kramer2018-03-202-8/+7
| | | | llvm-svn: 328047
* [format] Initialize regex lazilyBenjamin Kramer2018-03-201-8/+8
| | | | | | | No need to pay for this on program startup, and also no need to destroy it on process end. llvm-svn: 328041
* [clang-format] Fix ObjC selectors with multiple params passed to macroBen Hamilton2018-03-201-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Objective-C selectors with arguments take the form of: foo: foo:bar: foo:bar:baz: These can be passed to a macro, like NS_SWIFT_NAME(): https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html and must never have spaces inserted around the colons. Previously, there was logic in TokenAnnotator's tok::colon parser to handle the single-argument case, but it failed for the multiple-argument cases. This diff fixes the bug and adds more tests. Test Plan: New tests added. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: jolesiak, djasper, Wizard Reviewed By: jolesiak, Wizard Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44638 llvm-svn: 327986
* [clang-format] Remove empty lines before }[;] // commentKrasimir Georgiev2018-03-191-1/+5
| | | | | | | | | | | | 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 llvm-svn: 327861
* [clang-format] Disallow breaks before ']' in text proto extensionsKrasimir Georgiev2018-03-161-0/+3
| | | | | | | | | | Summary: This disallows patterns like `[ext.name\n]` in text protos. Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44569 llvm-svn: 327716
* [clang-format] Fix raw string prefix penaltyKrasimir Georgiev2018-03-161-1/+7
| | | | | | | | | | Summary: We weren't penalizing cases where the raw string prefix goes over the column limit. Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44563 llvm-svn: 327708
* [clang-format] Improve detection of Objective-C block typesBen Hamilton2018-03-121-6/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 llvm-svn: 327285
* [clang-format] Don't detect C++11 attribute specifiers as ObjCBen Hamilton2018-03-123-13/+65
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 llvm-svn: 327284
* clang-format: Properly handle implicit string concatenation in text protosDaniel Jasper2018-03-123-2/+5
| | | | | | | | | | | | | Three issues to fix: - char_constants weren't properly treated as string literals - Prevening the break after "label: " does not make sense in concunction with AlwaysBreakBeforeMultilineStrings. It leads to situations where clang-format just cannot find a viable format (it must break and yet it must not break). - AlwaysBreakBeforeMultilineStrings should not be on for LK_TextProto in Google style. llvm-svn: 327255
* Don't re-format raw string literal contents when formatting is disableDaniel Jasper2018-03-121-1/+1
| | | | | | | Not entirely sure this is the best place to put this check, but it fixes the immediate issue. llvm-svn: 327253
* [clang-format] Use NestedBlockIndent as a 0 column in formatted raw stringsKrasimir Georgiev2018-03-081-4/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This makes the formatter of raw string literals use NestedBlockIndent for determining the 0 column of the content inside. This makes the formatting use less horizonal space and fixes a case where two newlines before and after the raw string prefix were selected instead of a single newline after it: Before: ``` aaaa = ffff( R"pb( key: value)pb"); ``` After: ``` aaaa = ffff(R"pb( key: value)pb"); ``` Reviewers: djasper, sammccall Reviewed By: sammccall Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44141 llvm-svn: 326996
* [clang-format] Break consecutive string literals in text protosKrasimir Georgiev2018-03-071-1/+2
| | | | | | | | | | | | | | | | Summary: This patch fixes a bug where consecutive string literals in text protos were put on the same line. Reviewers: alexfh Reviewed By: alexfh Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44204 llvm-svn: 326945
OpenPOWER on IntegriCloud