UnwrappedLineFormatter.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 combinatorial exploration of all the different
  11. /// linebreaks unwrapped lines can be formatted in.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
  15. #define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H
  16. #include "ContinuationIndenter.h"
  17. #include "clang/Format/Format.h"
  18. #include <map>
  19. namespace clang {
  20. namespace format {
  21. class ContinuationIndenter;
  22. class WhitespaceManager;
  23. class UnwrappedLineFormatter {
  24. public:
  25. UnwrappedLineFormatter(ContinuationIndenter *Indenter,
  26. WhitespaceManager *Whitespaces,
  27. const FormatStyle &Style,
  28. const AdditionalKeywords &Keywords,
  29. const SourceManager &SourceMgr,
  30. FormattingAttemptStatus *Status)
  31. : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
  32. Keywords(Keywords), SourceMgr(SourceMgr), Status(Status) {}
  33. /// Format the current block and return the penalty.
  34. unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines,
  35. bool DryRun = false, int AdditionalIndent = 0,
  36. bool FixBadIndentation = false, unsigned FirstStartColumn = 0,
  37. unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
  38. private:
  39. /// Add a new line and the required indent before the first Token
  40. /// of the \c UnwrappedLine if there was no structural parsing error.
  41. void formatFirstToken(const AnnotatedLine &Line,
  42. const AnnotatedLine *PreviousLine,
  43. const AnnotatedLine *PrevPrevLine,
  44. const SmallVectorImpl<AnnotatedLine *> &Lines,
  45. unsigned Indent, unsigned NewlineIndent);
  46. /// Returns the column limit for a line, taking into account whether we
  47. /// need an escaped newline due to a continued preprocessor directive.
  48. unsigned getColumnLimit(bool InPPDirective,
  49. const AnnotatedLine *NextLine) const;
  50. // Cache to store the penalty of formatting a vector of AnnotatedLines
  51. // starting from a specific additional offset. Improves performance if there
  52. // are many nested blocks.
  53. std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>,
  54. unsigned>
  55. PenaltyCache;
  56. ContinuationIndenter *Indenter;
  57. WhitespaceManager *Whitespaces;
  58. const FormatStyle &Style;
  59. const AdditionalKeywords &Keywords;
  60. const SourceManager &SourceMgr;
  61. FormattingAttemptStatus *Status;
  62. };
  63. } // end namespace format
  64. } // end namespace clang
  65. #endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H