xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Format/AffectedRangeManager.h (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1*7330f729Sjoerg //===--- AffectedRangeManager.h - Format C++ code ---------------*- C++ -*-===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg ///
9*7330f729Sjoerg /// \file
10*7330f729Sjoerg /// AffectedRangeManager class manages affected ranges in the code.
11*7330f729Sjoerg ///
12*7330f729Sjoerg //===----------------------------------------------------------------------===//
13*7330f729Sjoerg 
14*7330f729Sjoerg #ifndef LLVM_CLANG_LIB_FORMAT_AFFECTEDRANGEMANAGER_H
15*7330f729Sjoerg #define LLVM_CLANG_LIB_FORMAT_AFFECTEDRANGEMANAGER_H
16*7330f729Sjoerg 
17*7330f729Sjoerg #include "clang/Basic/SourceManager.h"
18*7330f729Sjoerg 
19*7330f729Sjoerg namespace clang {
20*7330f729Sjoerg namespace format {
21*7330f729Sjoerg 
22*7330f729Sjoerg struct FormatToken;
23*7330f729Sjoerg class AnnotatedLine;
24*7330f729Sjoerg 
25*7330f729Sjoerg class AffectedRangeManager {
26*7330f729Sjoerg public:
AffectedRangeManager(const SourceManager & SourceMgr,const ArrayRef<CharSourceRange> Ranges)27*7330f729Sjoerg   AffectedRangeManager(const SourceManager &SourceMgr,
28*7330f729Sjoerg                        const ArrayRef<CharSourceRange> Ranges)
29*7330f729Sjoerg       : SourceMgr(SourceMgr), Ranges(Ranges.begin(), Ranges.end()) {}
30*7330f729Sjoerg 
31*7330f729Sjoerg   // Determines which lines are affected by the SourceRanges given as input.
32*7330f729Sjoerg   // Returns \c true if at least one line in \p Lines or one of their
33*7330f729Sjoerg   // children is affected.
34*7330f729Sjoerg   bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *> &Lines);
35*7330f729Sjoerg 
36*7330f729Sjoerg   // Returns true if 'Range' intersects with one of the input ranges.
37*7330f729Sjoerg   bool affectsCharSourceRange(const CharSourceRange &Range);
38*7330f729Sjoerg 
39*7330f729Sjoerg private:
40*7330f729Sjoerg   // Returns true if the range from 'First' to 'Last' intersects with one of the
41*7330f729Sjoerg   // input ranges.
42*7330f729Sjoerg   bool affectsTokenRange(const FormatToken &First, const FormatToken &Last,
43*7330f729Sjoerg                          bool IncludeLeadingNewlines);
44*7330f729Sjoerg 
45*7330f729Sjoerg   // Returns true if one of the input ranges intersect the leading empty lines
46*7330f729Sjoerg   // before 'Tok'.
47*7330f729Sjoerg   bool affectsLeadingEmptyLines(const FormatToken &Tok);
48*7330f729Sjoerg 
49*7330f729Sjoerg   // Marks all lines between I and E as well as all their children as affected.
50*7330f729Sjoerg   void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I,
51*7330f729Sjoerg                          SmallVectorImpl<AnnotatedLine *>::iterator E);
52*7330f729Sjoerg 
53*7330f729Sjoerg   // Determines whether 'Line' is affected by the SourceRanges given as input.
54*7330f729Sjoerg   // Returns \c true if line or one if its children is affected.
55*7330f729Sjoerg   bool nonPPLineAffected(AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
56*7330f729Sjoerg                          SmallVectorImpl<AnnotatedLine *> &Lines);
57*7330f729Sjoerg 
58*7330f729Sjoerg   const SourceManager &SourceMgr;
59*7330f729Sjoerg   const SmallVector<CharSourceRange, 8> Ranges;
60*7330f729Sjoerg };
61*7330f729Sjoerg 
62*7330f729Sjoerg } // namespace format
63*7330f729Sjoerg } // namespace clang
64*7330f729Sjoerg 
65*7330f729Sjoerg #endif // LLVM_CLANG_LIB_FORMAT_AFFECTEDRANGEMANAGER_H
66