blob: 55f0d1cac689fd80314fd802d6d375edc380f287 [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,
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000035 const SourceManager &SourceMgr,
36 FormattingAttemptStatus *Status)
Nico Weberfac23712015-02-04 15:26:27 +000037 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000038 Keywords(Keywords), SourceMgr(SourceMgr),
39 Status(Status) {}
Daniel Jasper0df50932014-12-10 19:00:42 +000040
Manuel Klimekd3585db2015-05-11 08:21:35 +000041 /// \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);
Daniel Jasper289afc02015-04-23 09:23:17 +000045
Daniel Jasper0df50932014-12-10 19:00:42 +000046private:
Daniel Jasper0df50932014-12-10 19:00:42 +000047 /// \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.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000049 void formatFirstToken(const AnnotatedLine &Line,
50 const AnnotatedLine *PreviousLine, unsigned Indent);
Daniel Jasper0df50932014-12-10 19:00:42 +000051
Manuel Klimek3d3ea842015-05-12 09:23:57 +000052 /// \brief Returns the column limit for a line, taking into account whether we
53 /// need an escaped newline due to a continued preprocessor directive.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +000054 unsigned getColumnLimit(bool InPPDirective,
55 const AnnotatedLine *NextLine) const;
Daniel Jasper0df50932014-12-10 19:00:42 +000056
Daniel Jasper0df50932014-12-10 19:00:42 +000057 // 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>,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000061 unsigned>
62 PenaltyCache;
Manuel Klimekec5c3db2015-05-07 12:26:30 +000063
Manuel Klimekd3585db2015-05-11 08:21:35 +000064 ContinuationIndenter *Indenter;
65 WhitespaceManager *Whitespaces;
66 const FormatStyle &Style;
67 const AdditionalKeywords &Keywords;
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000068 const SourceManager &SourceMgr;
69 FormattingAttemptStatus *Status;
Daniel Jasper0df50932014-12-10 19:00:42 +000070};
71} // end namespace format
72} // end namespace clang
73
74#endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H