blob: 7bcead9d25e1acc8e400676ed2af1c7664985f1d [file] [log] [blame]
Daniel Jasper0df50932014-12-10 19:00:42 +00001//===--- 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 Jasper0df50932014-12-10 19:00:42 +000022
23namespace clang {
24namespace format {
25
26class ContinuationIndenter;
27class WhitespaceManager;
28
29class UnwrappedLineFormatter {
30public:
31 UnwrappedLineFormatter(ContinuationIndenter *Indenter,
32 WhitespaceManager *Whitespaces,
Nico Weberfac23712015-02-04 15:26:27 +000033 const FormatStyle &Style,
Manuel Klimekec5c3db2015-05-07 12:26:30 +000034 const AdditionalKeywords &Keywords,
35 bool *IncompleteFormat)
Nico Weberfac23712015-02-04 15:26:27 +000036 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
Manuel Klimekec5c3db2015-05-07 12:26:30 +000037 Keywords(Keywords), IncompleteFormat(IncompleteFormat) {}
Daniel Jasper0df50932014-12-10 19:00:42 +000038
Manuel Klimekd3585db2015-05-11 08:21:35 +000039 /// \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 Jasper289afc02015-04-23 09:23:17 +000043
Daniel Jasper0df50932014-12-10 19:00:42 +000044private:
Daniel Jasper0df50932014-12-10 19:00:42 +000045 /// \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.
47 void formatFirstToken(FormatToken &RootToken,
48 const AnnotatedLine *PreviousLine, unsigned IndentLevel,
49 unsigned Indent, bool InPPDirective);
50
Manuel Klimek3d3ea842015-05-12 09:23:57 +000051 /// \brief Returns the column limit for a line, taking into account whether we
52 /// need an escaped newline due to a continued preprocessor directive.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +000053 unsigned getColumnLimit(bool InPPDirective,
54 const AnnotatedLine *NextLine) const;
Daniel Jasper0df50932014-12-10 19:00:42 +000055
Daniel Jasper0df50932014-12-10 19:00:42 +000056 // Cache to store the penalty of formatting a vector of AnnotatedLines
57 // starting from a specific additional offset. Improves performance if there
58 // are many nested blocks.
59 std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>,
60 unsigned> PenaltyCache;
Manuel Klimekec5c3db2015-05-07 12:26:30 +000061
Manuel Klimekd3585db2015-05-11 08:21:35 +000062 ContinuationIndenter *Indenter;
63 WhitespaceManager *Whitespaces;
64 const FormatStyle &Style;
65 const AdditionalKeywords &Keywords;
Manuel Klimekec5c3db2015-05-07 12:26:30 +000066 bool *IncompleteFormat;
Daniel Jasper0df50932014-12-10 19:00:42 +000067};
68} // end namespace format
69} // end namespace clang
70
71#endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H