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