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