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 28 class UnwrappedLineFormatter { 29 public: 30 UnwrappedLineFormatter(ContinuationIndenter *Indenter, 31 WhitespaceManager *Whitespaces, 32 const FormatStyle &Style, 33 const AdditionalKeywords &Keywords, 34 const SourceManager &SourceMgr, 35 FormattingAttemptStatus *Status) 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, 43 unsigned FirstStartColumn = 0, 44 unsigned NextStartColumn = 0, 45 unsigned LastStartColumn = 0); 46 47 private: 48 /// Add a new line and the required indent before the first Token 49 /// of the \c UnwrappedLine if there was no structural parsing error. 50 void formatFirstToken(const AnnotatedLine &Line, 51 const AnnotatedLine *PreviousLine, 52 const SmallVectorImpl<AnnotatedLine *> &Lines, 53 unsigned Indent, unsigned NewlineIndent); 54 55 /// Returns the column limit for a line, taking into account whether we 56 /// need an escaped newline due to a continued preprocessor directive. 57 unsigned getColumnLimit(bool InPPDirective, 58 const AnnotatedLine *NextLine) const; 59 60 // Cache to store the penalty of formatting a vector of AnnotatedLines 61 // starting from a specific additional offset. Improves performance if there 62 // are many nested blocks. 63 std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>, 64 unsigned> 65 PenaltyCache; 66 67 ContinuationIndenter *Indenter; 68 WhitespaceManager *Whitespaces; 69 const FormatStyle &Style; 70 const AdditionalKeywords &Keywords; 71 const SourceManager &SourceMgr; 72 FormattingAttemptStatus *Status; 73 }; 74 } // end namespace format 75 } // end namespace clang 76 77 #endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H 78