summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/UnwrappedLineParser.h
Commit message (Collapse)AuthorAgeFilesLines
* [clang-format] Add new style option IndentGotoLabelsPaul Hoad2019-09-121-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This option determines whether goto labels are indented according to scope. Setting this option to false causes goto labels to be flushed to the left. This is mostly copied from [[ http://lists.llvm.org/pipermail/cfe-dev/2015-September/045014.html | this patch ]] submitted by Christian Neukirchen that didn't make its way into trunk. ``` true: false: int f() { vs. int f() { if (foo()) { if (foo()) { label1: label1: bar(); bar(); } } label2: label2: return 1; return 1; } } ``` Reviewers: klimek, MyDeveloperDay Reviewed By: MyDeveloperDay Subscribers: cfe-commits Tags: #clang, #clang-tools-extra Patch by: tetsuo-cpp Differential Revision: https://reviews.llvm.org/D67037 llvm-svn: 371719
* [clang-format] [NFC] clang-format the Format libraryPaul Hoad2019-03-011-2/+1
| | | | | | | | | | | Previously revisions commited non-clang-formatted changes to the Format library, this means submitting any revision e.g. {D55170} can cause additional whitespace changes to potentially be included in a revision. Commit a non functional change using latest build Windows clang-format r351376 with no other changes, to remove these differences All FormatTests pass [==========] 652 tests from 20 test cases ran. llvm-svn: 355182
* Update the file headers across all of the LLVM projects in the monorepoChandler Carruth2019-01-191-4/+3
| | | | | | | | | | | | | | | | | to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
* clang-format: better handle statement macrosFrancois Ferrand2018-10-021-0/+1
| | | | | | | | | | | | | | | | | | | | | | Summary: Some macros are used in the body of function, and actually contain the trailing semicolon: they should thus be automatically followed by a new line, and not get merged with the next line. This is for example the case with Qt's Q_UNUSED macro: void foo(int a, int b) { Q_UNUSED(a) return b; } This patch deals with these cases by introducing a new option to specify list of statement macros. This re-uses the system already in place for foreach macros, to ensure there is no impact on performance. Reviewers: krasimir, djasper, klimek Reviewed By: krasimir Subscribers: acoomans, mgrang, alexfh, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D33440 llvm-svn: 343602
* [clang-format/ObjC] Correctly parse Objective-C methods with 'class' in nameBen Hamilton2018-05-301-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Please take a close look at this CL. I haven't touched much of `UnwrappedLineParser` before, so I may have gotten things wrong. Previously, clang-format would incorrectly format the following: ``` @implementation Foo - (Class)class { } - (void)foo { } @end ``` as: ``` @implementation Foo - (Class)class { } - (void)foo { } @end ``` The problem is whenever `UnwrappedLineParser::parseStructuralElement()` sees any of the keywords `class`, `struct`, or `enum`, it calls `parseRecord()` to parse them as a C/C++ record. This causes subsequent lines to be parsed incorrectly, which causes them to be indented incorrectly. In Objective-C/Objective-C++, these keywords are valid selector components. This diff fixes the issue by explicitly handling `+` and `-` lines inside `@implementation` / `@interface` / `@protocol` blocks and parsing them as Objective-C methods. Test Plan: New tests added. Ran tests with: make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: jolesiak, klimek Reviewed By: jolesiak, klimek Subscribers: klimek, cfe-commits, Wizard Differential Revision: https://reviews.llvm.org/D47095 llvm-svn: 333553
* Remove \brief commands from doxygen comments.Adrian Prantl2018-05-091-7/+7
| | | | | | | | | | | | | | | | | | | 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
* Format closing braces when reformatting the line containing the opening brace.Manuel Klimek2018-04-231-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* 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] Re-land: Fixup #include guard indents after parseFile()Mark Zeren2018-02-051-4/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 llvm-svn: 324246
* Revert "[clang-format] Fixup #include guard indents after parseFile()"Mark Zeren2018-02-051-17/+4
| | | | | | | | This reverts r324238 | mzeren-vmw | 2018-02-05 06:35:54 -0800 (Mon, 05 Feb 2018) | 35 lines Incorrect version pushed upstream. llvm-svn: 324239
* [clang-format] Fixup #include guard indents after parseFile()Mark Zeren2018-02-051-4/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 llvm-svn: 324238
* clang-format: Support macros in front of @interface / @protocol for ObjC code.Nico Weber2018-01-231-1/+1
| | | | llvm-svn: 323226
* [clang-format] Format raw string literalsKrasimir Georgiev2017-10-301-0/+7
| | | | | | | | | | | | | | | Summary: This patch adds raw string literal formatting. Reviewers: djasper, klimek Reviewed By: klimek Subscribers: klimek, mgorny Differential Revision: https://reviews.llvm.org/D35943 llvm-svn: 316903
* clang-format clang-format.Manuel Klimek2017-09-201-2/+3
| | | | llvm-svn: 313744
* Fix clang-format's detection of structured bindings.Manuel Klimek2017-09-201-1/+0
| | | | | | | | | | | | | | | | | | | 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. llvm-svn: 313742
* Fix formatting of lambda introducers with initializers.Manuel Klimek2017-09-191-1/+1
| | | | | | | | | | | | | | | | | | | | | 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] {}; llvm-svn: 313622
* clang-format: Add preprocessor directive indentationKrasimir Georgiev2017-08-301-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 llvm-svn: 312125
* clang-format: fix block OpeningLineIndex around preprocessorFrancois Ferrand2017-07-281-2/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: The current code would return an incorrect value when a preprocessor directive is present immediately after the opening brace: this causes the nanespace end comment fixer to break in some places, for exemple it would not add the comment in this case: namespace a { #define FOO } Fixing the computation is simple enough, but it was breaking a feature, as it would cause comments to be added also when the namespace declaration was dependant on conditional compilation. To fix this, a hash of the current preprocessor stack/branches is computed at the beginning of parseBlock(), so that we explicitely do not store the OpeningLineIndex when the beginning and end of the block are not in the same preprocessor conditions. Tthe hash is computed based on the line, but this could propbably be improved by using the actual condition, so that clang-format would be able to match multiple identical #ifdef blocks. Reviewers: krasimir, djasper Reviewed By: krasimir Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D35483 llvm-svn: 309369
* [clang-format] Fix comment levels between '} else {' and PPDirective.Krasimir Georgiev2017-07-241-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This fixes a regression exposed by r307795 and rL308725 in which the level of a comment line between '} else {' and a preprocessor directive is incorrectly set as the level of the '} else {' line. For example, this : ``` int f(int i) { if (i) { ++i; } else { // comment #ifdef A --i; #endif } } ``` was formatted as: ``` int f(int i) { if (i) { ++i; } else { // comment #ifdef A --i; #endif } } ``` Reviewers: djasper, klimek Reviewed By: klimek Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D35794 llvm-svn: 308882
* [clang-format] Support <>-style proto message fieldsKrasimir Georgiev2017-06-271-1/+2
| | | | | | | | | | | | | | | | | Summary: This patch adds support for <>-style proto message fields inside proto options. Previously these were wrongly treated as binary operators and as such were working only by chance for a limited number of cases. Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D34621 llvm-svn: 306406
* [clang-format] Add a NamespaceEndCommentsFixerKrasimir Georgiev2017-02-271-2/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This patch adds a NamespaceEndCommentsFixer TokenAnalyzer for clang-format, which fixes end namespace comments. It currently supports inserting and updating existing wrong comments. Example source: ``` namespace A { int i; } namespace B { int j; } // namespace A ``` after formatting: ``` namespace A { int i; } // namespace A namespace B { int j; } // namespace B ``` Reviewers: klimek, djasper Reviewed By: djasper Subscribers: klimek, mgorny Differential Revision: https://reviews.llvm.org/D30269 llvm-svn: 296341
* [clang-format] Break before a sequence of line comments aligned with the ↵Krasimir Georgiev2017-02-081-0/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | next line. Summary: Make the comment alignment respect sections of line comments originally alinged with the next token. Until now the decision how to break a continuous sequence of line comments into sections was taken without reference to the next token. source: ``` class A { public: // comment about public // comment about a int a; } ``` format before: ``` class A { public: // comment about public // comment about a int a; } ``` format after: ``` class A { public: // comment about public // comment about a int a; } ``` Reviewers: djasper, klimek Reviewed By: klimek Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D29626 llvm-svn: 294435
* clang-format: [JS] handle parenthesized class expressions.Martin Probst2017-02-071-2/+5
| | | | | | | | | | | | | | | | | | | | Summary: In JavaScript, classes are expressions, so they can appear e.g. in argument lists. var C = foo(class { bar() { return 1; } }; Reviewers: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D29635 llvm-svn: 294302
* [clang-format] Don't reflow across comment pragmas.Krasimir Georgiev2017-02-021-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: The comment reflower wasn't taking comment pragmas as reflow stoppers. This patch fixes that. source: ``` // long long long long // IWYU pragma: ``` format with column limit = 20 before: ``` // long long long // long IWYU pragma: ``` format with column limit = 20 after: ``` // long long long // long // IWYU pragma: ``` Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D29450 llvm-svn: 293898
* clang-format: [JS] Handle certain cases of ASI.Daniel Jasper2016-03-141-0/+1
| | | | | | | | | | | | | | Automatic Semicolon Insertion can only be properly handled by parsing source code. However conservatively catching just a few, common situations prevents breaking code during development, which greatly improves usability. JS code should still use semicolons, and ASI code should be flagged by a compiler or linter. Patch by Martin Probst. Thank you. llvm-svn: 263470
* clang-format: [JS] Support semicolons in TypeScript's TypeMemberLists.Daniel Jasper2016-01-091-0/+1
| | | | | | | | | | | | | | Before: var x: { a: string; b: number; } = {}; After: var x: {a: string; b: number;} = {}; llvm-svn: 257255
* clang-format: [JS/TypeScript] Support "enum" as property name.Daniel Jasper2015-12-291-1/+1
| | | | | | | | | | | Before: enum: string []; After: enum: string[]; llvm-svn: 256546
* Roll-back r250822.Angel Garcia Gomez2015-10-201-1/+1
| | | | | | | | | | Summary: It breaks the build for the ASTMatchers Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D13893 llvm-svn: 250827
* Apply modernize-use-default to clang.Angel Garcia Gomez2015-10-201-1/+1
| | | | | | | | | | | | Summary: Replace empty bodies of default constructors and destructors with '= default'. Reviewers: bkramer, klimek Subscribers: klimek, alexfh, cfe-commits Differential Revision: http://reviews.llvm.org/D13890 llvm-svn: 250822
* clang-format: NFC. Cleanup after r237895.Daniel Jasper2015-05-311-5/+0
| | | | | | | | | | | Specifically adhere to LLVM Coding Standards (no 'else' after return/break/continue) and remove yet another implementation of paren counting. We already have enough of those in the UnwrappedLineParser. No functional changes intended. llvm-svn: 238672
* clang-format: [JS] Better support for fat arrows.Manuel Klimek2015-05-211-0/+5
| | | | | | | | | | | | Assigns a token type (TT_JsFatArrow) to => tokens, and uses that to more easily recognize and format fat arrow functions. Improves function parsing to better recognize formal parameter lists and return type declarations. Recognizes arrow functions and parse function bodies as child blocks. Patch by Martin Probst. llvm-svn: 237895
* clang-format: Fix another regression caused by r237565.Daniel Jasper2015-05-181-2/+2
| | | | | | | | | | | | | | | | | | | | Before: class C : test { class D : test{void f(){int i{2}; } } ; } ; After: class C : test { class D : test { void f() { int i{2}; } }; }; llvm-svn: 237569
* clang-format: Allow braced initializers in template arguments of classDaniel Jasper2015-05-181-2/+2
| | | | | | | | | | | | | | specializations. Before: template <class T> struct S < std::is_arithmetic<T> { } > {}; After: template <class T> struct S<std::is_arithmetic<T>{}> {}; llvm-svn: 237565
* Remove all computation of structural errors in clang-format's line parser.Manuel Klimek2015-05-061-6/+1
| | | | | | We were already ignoring those already. llvm-svn: 236591
* clang-format: [Java] Support anonymous classes after = and return.Daniel Jasper2015-03-121-0/+1
| | | | | | | | | | | | | | | | | | | Before: A a = new A(){public String toString(){return "NotReallyA"; } } ; After: A a = return new A() { public String toString() { return "NotReallyA"; } }; This fixes llvm.org/PR22878. llvm-svn: 232042
* clang-format: [js] Support ES6 module exports.Daniel Jasper2015-02-191-1/+1
| | | | | | Patch by Martin Probst, thank you! llvm-svn: 229865
* clang-format: [js] Support ES6 module imports.Daniel Jasper2015-02-191-0/+1
| | | | | | Patch by Martin Probst. llvm-svn: 229863
* clang-format: [Java] Support Java enums.Daniel Jasper2014-11-131-0/+1
| | | | | | | In Java, enums can contain a class body and enum constants can have arguments as well as class bodies. Support most of that. llvm-svn: 221895
* clang-format: Use identifier table for keywords in other languages.Daniel Jasper2014-11-041-1/+5
| | | | | | | | | Slightly easier to write, more efficient and prevents bugs by misspelling them. No functional changes intended. llvm-svn: 221259
* Sink a #include while we're here. NFC.Hans Wennborg2014-10-291-0/+1
| | | | | | Format.h doesn't use stack. llvm-svn: 220879
* Header guard canonicalization, clang part.Benjamin Kramer2014-08-131-3/+3
| | | | | | Modifications made by clang-tidy with minor tweaks. llvm-svn: 215557
* clang-format: Fix bug introduced by r208392.Daniel Jasper2014-05-091-1/+1
| | | | | | Also run clang-format over clang-format's files. llvm-svn: 208409
* [C++11] Use 'nullptr'.Craig Topper2014-05-091-1/+1
| | | | llvm-svn: 208392
* clang-format: Initial support for try-catch.Daniel Jasper2014-05-081-0/+1
| | | | | | | | | | Most of this patch was created by Alexander Rojas in http://reviews.llvm.org/D2555 Thank you! Synced and addressed review comments. llvm-svn: 208302
* clang-format: [JS] support closures in container literals.Daniel Jasper2014-05-081-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | Before: return {body: {setAttribute: function(key, val) {this[key] = val; } , getAttribute : function(key) { return this[key]; } , style : { direction: '' } } } ; After: return { body: { setAttribute: function(key, val) { this[key] = val; }, getAttribute: function(key) { return this[key]; }, style: {direction: ''} } }; llvm-svn: 208292
* Format code around VCS conflict markers.Manuel Klimek2014-04-141-1/+10
| | | | | | | | | | | | | | | | | | | | | Now correctly formats: { int a; void f() { callme(some(parameter1, <<<<<<< text by the vcs parameter2), ||||||| text by the vcs parameter2), parameter3, ======= text by the vcs parameter2, parameter3), >>>>>>> text by the vcs otherparameter); } } llvm-svn: 206157
* Correctly handle escaped newlines when the next token starts without a space.Manuel Klimek2014-04-111-0/+1
| | | | | | | We will need this to correctly handle conflict markers inside macro definitions. llvm-svn: 206029
* Replace OwningPtr with std::unique_ptr.Ahmed Charles2014-03-071-1/+1
| | | | | | This compiles cleanly with lldb/lld/clang-tools-extra/llvm. llvm-svn: 203279
* Get rid of special parsing for return statements.Manuel Klimek2014-01-241-1/+0
| | | | | | | | This was done when we were not able to parse lambdas to handle some edge cases for block formatting different in return statements, but is not necessary any more. llvm-svn: 199982
* Sort all the #include lines with LLVM's utils/sort_includes.py whichChandler Carruth2014-01-071-1/+1
| | | | | | | encodes the canonical rules for LLVM's style. I noticed this had drifted quite a bit when cleaning up LLVM, so wanted to clean up Clang as well. llvm-svn: 198686
OpenPOWER on IntegriCloud