UnwrappedLineFormatter.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //===--- UnwrappedLineFormatter.h - Format C++ code -------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// \brief Implements a combinartorial exploration of all the different
  12. /// linebreaks unwrapped lines can be formatted in.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
  16. #define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
  17. #include "ContinuationIndenter.h"
  18. #include "clang/Format/Format.h"
  19. #include <map>
  20. #include <queue>
  21. #include <string>
  22. namespace clang {
  23. namespace format {
  24. class ContinuationIndenter;
  25. class WhitespaceManager;
  26. class UnwrappedLineFormatter {
  27. public:
  28. UnwrappedLineFormatter(ContinuationIndenter *Indenter,
  29. WhitespaceManager *Whitespaces,
  30. const FormatStyle &Style,
  31. const AdditionalKeywords &Keywords,
  32. bool *IncompleteFormat)
  33. : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
  34. Keywords(Keywords), IncompleteFormat(IncompleteFormat) {}
  35. /// \brief Format the current block and return the penalty.
  36. unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines,
  37. bool DryRun = false, int AdditionalIndent = 0,
  38. bool FixBadIndentation = false);
  39. private:
  40. /// \brief Add a new line and the required indent before the first Token
  41. /// of the \c UnwrappedLine if there was no structural parsing error.
  42. void formatFirstToken(FormatToken &RootToken,
  43. const AnnotatedLine *PreviousLine, unsigned IndentLevel,
  44. unsigned Indent, bool InPPDirective);
  45. /// \brief Returns the column limit for a line, taking into account whether we
  46. /// need an escaped newline due to a continued preprocessor directive.
  47. unsigned getColumnLimit(bool InPPDirective,
  48. const AnnotatedLine *NextLine) const;
  49. // Cache to store the penalty of formatting a vector of AnnotatedLines
  50. // starting from a specific additional offset. Improves performance if there
  51. // are many nested blocks.
  52. std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>,
  53. unsigned> PenaltyCache;
  54. ContinuationIndenter *Indenter;
  55. WhitespaceManager *Whitespaces;
  56. const FormatStyle &Style;
  57. const AdditionalKeywords &Keywords;
  58. bool *IncompleteFormat;
  59. };
  60. } // end namespace format
  61. } // end namespace clang
  62. #endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H