10e2c34f92f00628d48968dfea096d36381f494cbStephen Hines//===--- UnwrappedLineFormatter.h - Format C++ code -------------*- C++ -*-===//
20e2c34f92f00628d48968dfea096d36381f494cbStephen Hines//
30e2c34f92f00628d48968dfea096d36381f494cbStephen Hines//                     The LLVM Compiler Infrastructure
40e2c34f92f00628d48968dfea096d36381f494cbStephen Hines//
50e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// This file is distributed under the University of Illinois Open Source
60e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// License. See LICENSE.TXT for details.
70e2c34f92f00628d48968dfea096d36381f494cbStephen Hines//
80e2c34f92f00628d48968dfea096d36381f494cbStephen Hines//===----------------------------------------------------------------------===//
90e2c34f92f00628d48968dfea096d36381f494cbStephen Hines///
100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// \file
110e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// \brief Implements a combinartorial exploration of all the different
120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// linebreaks unwrapped lines can be formatted in.
130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines///
140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines//===----------------------------------------------------------------------===//
150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
160e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
180e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#include "ContinuationIndenter.h"
200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#include "clang/Format/Format.h"
210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#include <map>
220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#include <queue>
230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#include <string>
240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
250e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesnamespace clang {
260e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesnamespace format {
270e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
280e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesclass ContinuationIndenter;
290e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesclass WhitespaceManager;
300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
310e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesclass UnwrappedLineFormatter {
320e2c34f92f00628d48968dfea096d36381f494cbStephen Hinespublic:
330e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  UnwrappedLineFormatter(ContinuationIndenter *Indenter,
340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                         WhitespaceManager *Whitespaces,
350e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                         const FormatStyle &Style,
36b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                         const AdditionalKeywords &Keywords,
37b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                         bool *IncompleteFormat)
380e2c34f92f00628d48968dfea096d36381f494cbStephen Hines      : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
39b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar        Keywords(Keywords), IncompleteFormat(IncompleteFormat) {}
400e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
41b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  /// \brief Format the current block and return the penalty.
42b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines,
43b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                  bool DryRun = false, int AdditionalIndent = 0,
44b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar                  bool FixBadIndentation = false);
450e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
460e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesprivate:
470e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// \brief Add a new line and the required indent before the first Token
480e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// of the \c UnwrappedLine if there was no structural parsing error.
490e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void formatFirstToken(FormatToken &RootToken,
500e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                        const AnnotatedLine *PreviousLine, unsigned IndentLevel,
510e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                        unsigned Indent, bool InPPDirective);
520e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
53b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  /// \brief Returns the column limit for a line, taking into account whether we
54b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  /// need an escaped newline due to a continued preprocessor directive.
5587d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar  unsigned getColumnLimit(bool InPPDirective,
5687d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar                          const AnnotatedLine *NextLine) const;
570e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
580e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // Cache to store the penalty of formatting a vector of AnnotatedLines
590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // starting from a specific additional offset. Improves performance if there
600e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // are many nested blocks.
610e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>,
620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines           unsigned> PenaltyCache;
63b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
64b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  ContinuationIndenter *Indenter;
65b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  WhitespaceManager *Whitespaces;
66b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  const FormatStyle &Style;
67b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  const AdditionalKeywords &Keywords;
68b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  bool *IncompleteFormat;
690e2c34f92f00628d48968dfea096d36381f494cbStephen Hines};
700e2c34f92f00628d48968dfea096d36381f494cbStephen Hines} // end namespace format
710e2c34f92f00628d48968dfea096d36381f494cbStephen Hines} // end namespace clang
720e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
730e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
74