blob: a1ff16999589fcef757c207394419963c16070d4 [file] [log] [blame]
Daniel Jasper0df50932014-12-10 19:00:42 +00001//===--- UnwrappedLineFormatter.h - Format C++ code -------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Daniel Jasper0df50932014-12-10 19:00:42 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// Implements a combinartorial exploration of all the different
Daniel Jasper0df50932014-12-10 19:00:42 +000011/// 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#include "clang/Format/Format.h"
20#include <map>
Daniel Jasper0df50932014-12-10 19:00:42 +000021
22namespace clang {
23namespace format {
24
25class ContinuationIndenter;
26class WhitespaceManager;
27
28class UnwrappedLineFormatter {
29public:
30 UnwrappedLineFormatter(ContinuationIndenter *Indenter,
31 WhitespaceManager *Whitespaces,
Nico Weberfac23712015-02-04 15:26:27 +000032 const FormatStyle &Style,
Manuel Klimekec5c3db2015-05-07 12:26:30 +000033 const AdditionalKeywords &Keywords,
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000034 const SourceManager &SourceMgr,
35 FormattingAttemptStatus *Status)
Nico Weberfac23712015-02-04 15:26:27 +000036 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
Manuel Klimek89628f62017-09-20 09:51:03 +000037 Keywords(Keywords), SourceMgr(SourceMgr), Status(Status) {}
Daniel Jasper0df50932014-12-10 19:00:42 +000038
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000039 /// Format the current block and return the penalty.
Manuel Klimekd3585db2015-05-11 08:21:35 +000040 unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines,
41 bool DryRun = false, int AdditionalIndent = 0,
Paul Hoad5bcf99b2019-03-01 09:09:54 +000042 bool FixBadIndentation = false, unsigned FirstStartColumn = 0,
43 unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
Daniel Jasper289afc02015-04-23 09:23:17 +000044
Daniel Jasper0df50932014-12-10 19:00:42 +000045private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046 /// Add a new line and the required indent before the first Token
Daniel Jasper0df50932014-12-10 19:00:42 +000047 /// of the \c UnwrappedLine if there was no structural parsing error.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000048 void formatFirstToken(const AnnotatedLine &Line,
Krasimir Georgiev62103052018-04-19 13:02:15 +000049 const AnnotatedLine *PreviousLine,
50 const SmallVectorImpl<AnnotatedLine *> &Lines,
51 unsigned Indent, unsigned NewlineIndent);
Daniel Jasper0df50932014-12-10 19:00:42 +000052
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000053 /// Returns the column limit for a line, taking into account whether we
Manuel Klimek3d3ea842015-05-12 09:23:57 +000054 /// need an escaped newline due to a continued preprocessor directive.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +000055 unsigned getColumnLimit(bool InPPDirective,
56 const AnnotatedLine *NextLine) const;
Daniel Jasper0df50932014-12-10 19:00:42 +000057
Daniel Jasper0df50932014-12-10 19:00:42 +000058 // 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>,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000062 unsigned>
63 PenaltyCache;
Manuel Klimekec5c3db2015-05-07 12:26:30 +000064
Manuel Klimekd3585db2015-05-11 08:21:35 +000065 ContinuationIndenter *Indenter;
66 WhitespaceManager *Whitespaces;
67 const FormatStyle &Style;
68 const AdditionalKeywords &Keywords;
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000069 const SourceManager &SourceMgr;
70 FormattingAttemptStatus *Status;
Daniel Jasper0df50932014-12-10 19:00:42 +000071};
72} // end namespace format
73} // end namespace clang
74
75#endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H