UnwrappedLineFormatter.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #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 SmallVectorImpl<AnnotatedLine *> &Lines,
  44. unsigned Indent, unsigned NewlineIndent);
  45. /// 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>
  54. PenaltyCache;
  55. ContinuationIndenter *Indenter;
  56. WhitespaceManager *Whitespaces;
  57. const FormatStyle &Style;
  58. const AdditionalKeywords &Keywords;
  59. const SourceManager &SourceMgr;
  60. FormattingAttemptStatus *Status;
  61. };
  62. } // end namespace format
  63. } // end namespace clang
  64. #endif // LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEFORMATTER_H