AffectedRangeManager.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //===--- AffectedRangeManager.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. /// AffectedRangeManager class manages affected ranges in the code.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_LIB_FORMAT_AFFECTEDRANGEMANAGER_H
  14. #define LLVM_CLANG_LIB_FORMAT_AFFECTEDRANGEMANAGER_H
  15. #include "clang/Basic/SourceManager.h"
  16. namespace clang {
  17. namespace format {
  18. struct FormatToken;
  19. class AnnotatedLine;
  20. class AffectedRangeManager {
  21. public:
  22. AffectedRangeManager(const SourceManager &SourceMgr,
  23. const ArrayRef<CharSourceRange> Ranges)
  24. : SourceMgr(SourceMgr), Ranges(Ranges.begin(), Ranges.end()) {}
  25. // Determines which lines are affected by the SourceRanges given as input.
  26. // Returns \c true if at least one line in \p Lines or one of their
  27. // children is affected.
  28. bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *> &Lines);
  29. // Returns true if 'Range' intersects with one of the input ranges.
  30. bool affectsCharSourceRange(const CharSourceRange &Range);
  31. private:
  32. // Returns true if the range from 'First' to 'Last' intersects with one of the
  33. // input ranges.
  34. bool affectsTokenRange(const FormatToken &First, const FormatToken &Last,
  35. bool IncludeLeadingNewlines);
  36. // Returns true if one of the input ranges intersect the leading empty lines
  37. // before 'Tok'.
  38. bool affectsLeadingEmptyLines(const FormatToken &Tok);
  39. // Marks all lines between I and E as well as all their children as affected.
  40. void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I,
  41. SmallVectorImpl<AnnotatedLine *>::iterator E);
  42. // Determines whether 'Line' is affected by the SourceRanges given as input.
  43. // Returns \c true if line or one if its children is affected.
  44. bool nonPPLineAffected(AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
  45. SmallVectorImpl<AnnotatedLine *> &Lines);
  46. const SourceManager &SourceMgr;
  47. const SmallVector<CharSourceRange, 8> Ranges;
  48. };
  49. } // namespace format
  50. } // namespace clang
  51. #endif // LLVM_CLANG_LIB_FORMAT_AFFECTEDRANGEMANAGER_H