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>
22 #include <queue>
23 #include <string>
24 
25 namespace clang {
26 namespace format {
27 
28 class ContinuationIndenter;
29 class WhitespaceManager;
30 
31 class UnwrappedLineFormatter {
32 public:
UnwrappedLineFormatter(ContinuationIndenter * Indenter,WhitespaceManager * Whitespaces,const FormatStyle & Style,const AdditionalKeywords & Keywords,bool * IncompleteFormat)33   UnwrappedLineFormatter(ContinuationIndenter *Indenter,
34                          WhitespaceManager *Whitespaces,
35                          const FormatStyle &Style,
36                          const AdditionalKeywords &Keywords,
37                          bool *IncompleteFormat)
38       : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
39         Keywords(Keywords), IncompleteFormat(IncompleteFormat) {}
40 
41   /// \brief Format the current block and return the penalty.
42   unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines,
43                   bool DryRun = false, int AdditionalIndent = 0,
44                   bool FixBadIndentation = false);
45 
46 private:
47   /// \brief Add a new line and the required indent before the first Token
48   /// of the \c UnwrappedLine if there was no structural parsing error.
49   void formatFirstToken(FormatToken &RootToken,
50                         const AnnotatedLine *PreviousLine, unsigned IndentLevel,
51                         unsigned Indent, bool InPPDirective);
52 
53   /// \brief Returns the column limit for a line, taking into account whether we
54   /// need an escaped newline due to a continued preprocessor directive.
55   unsigned getColumnLimit(bool InPPDirective,
56                           const AnnotatedLine *NextLine) const;
57 
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>,
62            unsigned> PenaltyCache;
63 
64   ContinuationIndenter *Indenter;
65   WhitespaceManager *Whitespaces;
66   const FormatStyle &Style;
67   const AdditionalKeywords &Keywords;
68   bool *IncompleteFormat;
69 };
70 } // end namespace format
71 } // end namespace clang
72 
73 #endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
74