1//===--- UnwrappedLineFormatter.h - Format C++ code -------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Implements a combinatorial exploration of all the different
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
20namespace clang {
21namespace format {
22
23class ContinuationIndenter;
24class WhitespaceManager;
25
26class UnwrappedLineFormatter {
27public:
28 UnwrappedLineFormatter(ContinuationIndenter *Indenter,
29 WhitespaceManager *Whitespaces,
30 const FormatStyle &Style,
31 const AdditionalKeywords &Keywords,
32 const SourceManager &SourceMgr,
33 FormattingAttemptStatus *Status)
34 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
35 Keywords(Keywords), SourceMgr(SourceMgr), Status(Status) {}
36
37 /// Format the current block and return the penalty.
38 unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines,
39 bool DryRun = false, int AdditionalIndent = 0,
40 bool FixBadIndentation = false, unsigned FirstStartColumn = 0,
41 unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
42
43private:
44 /// Add a new line and the required indent before the first Token
45 /// of the \c UnwrappedLine if there was no structural parsing error.
46 void formatFirstToken(const AnnotatedLine &Line,
47 const AnnotatedLine *PreviousLine,
48 const AnnotatedLine *PrevPrevLine,
49 const SmallVectorImpl<AnnotatedLine *> &Lines,
50 unsigned Indent, unsigned NewlineIndent);
51
52 /// Returns the column limit for a line, taking into account whether we
53 /// need an escaped newline due to a continued preprocessor directive.
54 unsigned getColumnLimit(bool InPPDirective,
55 const AnnotatedLine *NextLine) const;
56
57 // Cache to store the penalty of formatting a vector of AnnotatedLines
58 // starting from a specific additional offset. Improves performance if there
59 // are many nested blocks.
60 std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>,
61 unsigned>
62 PenaltyCache;
63
64 ContinuationIndenter *Indenter;
65 WhitespaceManager *Whitespaces;
66 const FormatStyle &Style;
67 const AdditionalKeywords &Keywords;
68 const SourceManager &SourceMgr;
69 FormattingAttemptStatus *Status;
70};
71} // end namespace format
72} // end namespace clang
73
74#endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
75