clang  14.0.0git
UnwrappedLineFormatter.h
Go to the documentation of this file.
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 combinartorial 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 #include "clang/Format/Format.h"
20 #include <map>
21 
22 namespace clang {
23 namespace format {
24 
25 class ContinuationIndenter;
26 class WhitespaceManager;
27 
29 public:
31  WhitespaceManager *Whitespaces,
32  const FormatStyle &Style,
33  const AdditionalKeywords &Keywords,
34  const SourceManager &SourceMgr,
36  : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
37  Keywords(Keywords), SourceMgr(SourceMgr), Status(Status) {}
38 
39  /// 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, unsigned FirstStartColumn = 0,
43  unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
44 
45 private:
46  /// Add a new line and the required indent before the first Token
47  /// of the \c UnwrappedLine if there was no structural parsing error.
48  void formatFirstToken(const AnnotatedLine &Line,
49  const AnnotatedLine *PreviousLine,
50  const AnnotatedLine *PrevPrevLine,
52  unsigned Indent, unsigned NewlineIndent);
53 
54  /// Returns the column limit for a line, taking into account whether we
55  /// need an escaped newline due to a continued preprocessor directive.
56  unsigned getColumnLimit(bool InPPDirective,
57  const AnnotatedLine *NextLine) const;
58 
59  // Cache to store the penalty of formatting a vector of AnnotatedLines
60  // starting from a specific additional offset. Improves performance if there
61  // are many nested blocks.
62  std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>,
63  unsigned>
64  PenaltyCache;
65 
66  ContinuationIndenter *Indenter;
67  WhitespaceManager *Whitespaces;
68  const FormatStyle &Style;
69  const AdditionalKeywords &Keywords;
70  const SourceManager &SourceMgr;
72 };
73 } // end namespace format
74 } // end namespace clang
75 
76 #endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
clang::format::UnwrappedLineFormatter::format
unsigned format(const SmallVectorImpl< AnnotatedLine * > &Lines, bool DryRun=false, int AdditionalIndent=0, bool FixBadIndentation=false, unsigned FirstStartColumn=0, unsigned NextStartColumn=0, unsigned LastStartColumn=0)
Format the current block and return the penalty.
Definition: UnwrappedLineFormatter.cpp:1121
clang::format::FormatStyle
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:59
clang::format::AdditionalKeywords
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang's l...
Definition: FormatToken.h:841
Format.h
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::format::WhitespaceManager
Manages the whitespaces around tokens and their replacements.
Definition: WhitespaceManager.h:40
clang::format::AnnotatedLine
Definition: TokenAnnotator.h:38
clang::format::UnwrappedLineFormatter::UnwrappedLineFormatter
UnwrappedLineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces, const FormatStyle &Style, const AdditionalKeywords &Keywords, const SourceManager &SourceMgr, FormattingAttemptStatus *Status)
Definition: UnwrappedLineFormatter.h:30
clang::format::FormattingAttemptStatus
Represents the status of a formatting attempt.
Definition: Format.h:3888
clang::format::UnwrappedLineFormatter
Definition: UnwrappedLineFormatter.h:28
Line
const AnnotatedLine * Line
Definition: UsingDeclarationsSorter.cpp:68
clang::Indent
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Definition: JsonSupport.h:20
clang::format::ContinuationIndenter
Definition: ContinuationIndenter.h:50
clang
Definition: CalledOnceCheck.h:17
ContinuationIndenter.h
llvm::SmallVectorImpl
Definition: LLVM.h:39
Indenter
ContinuationIndenter * Indenter
Definition: UnwrappedLineFormatter.cpp:883