Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 1 | //===--- UnwrappedLineFormatter.h - Format C++ code -------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10 | /// Implements a combinartorial exploration of all the different |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 11 | /// linebreaks unwrapped lines can be formatted in. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H |
| 16 | #define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H |
| 17 | |
| 18 | #include "ContinuationIndenter.h" |
| 19 | #include "clang/Format/Format.h" |
| 20 | #include <map> |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 21 | |
| 22 | namespace clang { |
| 23 | namespace format { |
| 24 | |
| 25 | class ContinuationIndenter; |
| 26 | class WhitespaceManager; |
| 27 | |
| 28 | class UnwrappedLineFormatter { |
| 29 | public: |
| 30 | UnwrappedLineFormatter(ContinuationIndenter *Indenter, |
| 31 | WhitespaceManager *Whitespaces, |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 32 | const FormatStyle &Style, |
Manuel Klimek | ec5c3db | 2015-05-07 12:26:30 +0000 | [diff] [blame] | 33 | const AdditionalKeywords &Keywords, |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 34 | const SourceManager &SourceMgr, |
| 35 | FormattingAttemptStatus *Status) |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 36 | : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 37 | Keywords(Keywords), SourceMgr(SourceMgr), Status(Status) {} |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 38 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 39 | /// Format the current block and return the penalty. |
Manuel Klimek | d3585db | 2015-05-11 08:21:35 +0000 | [diff] [blame] | 40 | unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines, |
| 41 | bool DryRun = false, int AdditionalIndent = 0, |
Paul Hoad | 5bcf99b | 2019-03-01 09:09:54 +0000 | [diff] [blame] | 42 | bool FixBadIndentation = false, unsigned FirstStartColumn = 0, |
| 43 | unsigned NextStartColumn = 0, unsigned LastStartColumn = 0); |
Daniel Jasper | 289afc0 | 2015-04-23 09:23:17 +0000 | [diff] [blame] | 44 | |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 45 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 46 | /// Add a new line and the required indent before the first Token |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 47 | /// of the \c UnwrappedLine if there was no structural parsing error. |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 48 | void formatFirstToken(const AnnotatedLine &Line, |
Krasimir Georgiev | 6210305 | 2018-04-19 13:02:15 +0000 | [diff] [blame] | 49 | const AnnotatedLine *PreviousLine, |
| 50 | const SmallVectorImpl<AnnotatedLine *> &Lines, |
| 51 | unsigned Indent, unsigned NewlineIndent); |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 52 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 53 | /// Returns the column limit for a line, taking into account whether we |
Manuel Klimek | 3d3ea84 | 2015-05-12 09:23:57 +0000 | [diff] [blame] | 54 | /// need an escaped newline due to a continued preprocessor directive. |
Daniel Jasper | e6fcf7d | 2015-06-17 13:08:06 +0000 | [diff] [blame] | 55 | unsigned getColumnLimit(bool InPPDirective, |
| 56 | const AnnotatedLine *NextLine) const; |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 57 | |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 58 | // Cache to store the penalty of formatting a vector of AnnotatedLines |
| 59 | // starting from a specific additional offset. Improves performance if there |
| 60 | // are many nested blocks. |
| 61 | std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>, |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 62 | unsigned> |
| 63 | PenaltyCache; |
Manuel Klimek | ec5c3db | 2015-05-07 12:26:30 +0000 | [diff] [blame] | 64 | |
Manuel Klimek | d3585db | 2015-05-11 08:21:35 +0000 | [diff] [blame] | 65 | ContinuationIndenter *Indenter; |
| 66 | WhitespaceManager *Whitespaces; |
| 67 | const FormatStyle &Style; |
| 68 | const AdditionalKeywords &Keywords; |
Krasimir Georgiev | bcda54b | 2017-04-21 14:35:20 +0000 | [diff] [blame] | 69 | const SourceManager &SourceMgr; |
| 70 | FormattingAttemptStatus *Status; |
Daniel Jasper | 0df5093 | 2014-12-10 19:00:42 +0000 | [diff] [blame] | 71 | }; |
| 72 | } // end namespace format |
| 73 | } // end namespace clang |
| 74 | |
| 75 | #endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H |