xref: /openbsd-src/gnu/llvm/clang/lib/Format/UnwrappedLineFormatter.h (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===--- UnwrappedLineFormatter.h - Format C++ code -------------*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick ///
9e5dd7070Spatrick /// \file
10*12c85518Srobert /// Implements a combinatorial exploration of all the different
11e5dd7070Spatrick /// linebreaks unwrapped lines can be formatted in.
12e5dd7070Spatrick ///
13e5dd7070Spatrick //===----------------------------------------------------------------------===//
14e5dd7070Spatrick 
15e5dd7070Spatrick #ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
16e5dd7070Spatrick #define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
17e5dd7070Spatrick 
18e5dd7070Spatrick #include "ContinuationIndenter.h"
19e5dd7070Spatrick #include "clang/Format/Format.h"
20e5dd7070Spatrick #include <map>
21e5dd7070Spatrick 
22e5dd7070Spatrick namespace clang {
23e5dd7070Spatrick namespace format {
24e5dd7070Spatrick 
25e5dd7070Spatrick class ContinuationIndenter;
26e5dd7070Spatrick class WhitespaceManager;
27e5dd7070Spatrick 
28e5dd7070Spatrick class UnwrappedLineFormatter {
29e5dd7070Spatrick public:
UnwrappedLineFormatter(ContinuationIndenter * Indenter,WhitespaceManager * Whitespaces,const FormatStyle & Style,const AdditionalKeywords & Keywords,const SourceManager & SourceMgr,FormattingAttemptStatus * Status)30e5dd7070Spatrick   UnwrappedLineFormatter(ContinuationIndenter *Indenter,
31e5dd7070Spatrick                          WhitespaceManager *Whitespaces,
32e5dd7070Spatrick                          const FormatStyle &Style,
33e5dd7070Spatrick                          const AdditionalKeywords &Keywords,
34e5dd7070Spatrick                          const SourceManager &SourceMgr,
35e5dd7070Spatrick                          FormattingAttemptStatus *Status)
36e5dd7070Spatrick       : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
37e5dd7070Spatrick         Keywords(Keywords), SourceMgr(SourceMgr), Status(Status) {}
38e5dd7070Spatrick 
39e5dd7070Spatrick   /// Format the current block and return the penalty.
40e5dd7070Spatrick   unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines,
41e5dd7070Spatrick                   bool DryRun = false, int AdditionalIndent = 0,
42e5dd7070Spatrick                   bool FixBadIndentation = false, unsigned FirstStartColumn = 0,
43e5dd7070Spatrick                   unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
44e5dd7070Spatrick 
45e5dd7070Spatrick private:
46e5dd7070Spatrick   /// Add a new line and the required indent before the first Token
47e5dd7070Spatrick   /// of the \c UnwrappedLine if there was no structural parsing error.
48e5dd7070Spatrick   void formatFirstToken(const AnnotatedLine &Line,
49e5dd7070Spatrick                         const AnnotatedLine *PreviousLine,
50a9ac8606Spatrick                         const AnnotatedLine *PrevPrevLine,
51e5dd7070Spatrick                         const SmallVectorImpl<AnnotatedLine *> &Lines,
52e5dd7070Spatrick                         unsigned Indent, unsigned NewlineIndent);
53e5dd7070Spatrick 
54e5dd7070Spatrick   /// Returns the column limit for a line, taking into account whether we
55e5dd7070Spatrick   /// need an escaped newline due to a continued preprocessor directive.
56e5dd7070Spatrick   unsigned getColumnLimit(bool InPPDirective,
57e5dd7070Spatrick                           const AnnotatedLine *NextLine) const;
58e5dd7070Spatrick 
59e5dd7070Spatrick   // Cache to store the penalty of formatting a vector of AnnotatedLines
60e5dd7070Spatrick   // starting from a specific additional offset. Improves performance if there
61e5dd7070Spatrick   // are many nested blocks.
62e5dd7070Spatrick   std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>,
63e5dd7070Spatrick            unsigned>
64e5dd7070Spatrick       PenaltyCache;
65e5dd7070Spatrick 
66e5dd7070Spatrick   ContinuationIndenter *Indenter;
67e5dd7070Spatrick   WhitespaceManager *Whitespaces;
68e5dd7070Spatrick   const FormatStyle &Style;
69e5dd7070Spatrick   const AdditionalKeywords &Keywords;
70e5dd7070Spatrick   const SourceManager &SourceMgr;
71e5dd7070Spatrick   FormattingAttemptStatus *Status;
72e5dd7070Spatrick };
73e5dd7070Spatrick } // end namespace format
74e5dd7070Spatrick } // end namespace clang
75e5dd7070Spatrick 
76e5dd7070Spatrick #endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
77