]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Format/TokenAnalyzer.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Format / TokenAnalyzer.h
1 //===--- TokenAnalyzer.h - Analyze Token Streams ----------------*- 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 /// This file declares an abstract TokenAnalyzer, and associated helper
12 /// classes. TokenAnalyzer can be extended to generate replacements based on
13 /// an annotated and pre-processed token stream.
14 ///
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H
18 #define LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H
19
20 #include "AffectedRangeManager.h"
21 #include "Encoding.h"
22 #include "FormatToken.h"
23 #include "FormatTokenLexer.h"
24 #include "TokenAnnotator.h"
25 #include "UnwrappedLineParser.h"
26 #include "clang/Basic/Diagnostic.h"
27 #include "clang/Basic/DiagnosticOptions.h"
28 #include "clang/Basic/FileManager.h"
29 #include "clang/Basic/SourceManager.h"
30 #include "clang/Format/Format.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/Support/Debug.h"
33
34 namespace clang {
35 namespace format {
36
37 class Environment {
38 public:
39   Environment(SourceManager &SM, FileID ID, ArrayRef<CharSourceRange> Ranges)
40       : SM(SM), ID(ID), CharRanges(Ranges.begin(), Ranges.end()),
41         FirstStartColumn(0), NextStartColumn(0), LastStartColumn(0) {}
42
43   // This sets up an virtual file system with file \p FileName containing the
44   // fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn,
45   // that the next lines of \p Code should start at \p NextStartColumn, and
46   // that \p Code should end at \p LastStartColumn if it ends in newline.
47   // See also the documentation of clang::format::internal::reformat.
48   Environment(StringRef Code, StringRef FileName,
49               ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn = 0,
50               unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
51
52   FileID getFileID() const { return ID; }
53
54   const SourceManager &getSourceManager() const { return SM; }
55
56   ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
57
58   // Returns the column at which the fragment of code managed by this
59   // environment starts.
60   unsigned getFirstStartColumn() const { return FirstStartColumn; }
61
62   // Returns the column at which subsequent lines of the fragment of code
63   // managed by this environment should start.
64   unsigned getNextStartColumn() const { return NextStartColumn; }
65
66   // Returns the column at which the fragment of code managed by this
67   // environment should end if it ends in a newline.
68   unsigned getLastStartColumn() const { return LastStartColumn; }
69
70 private:
71   // This is only set if constructed from string.
72   std::unique_ptr<SourceManagerForFile> VirtualSM;
73
74   // This refers to either a SourceManager provided by users or VirtualSM
75   // created for a single file.
76   SourceManager &SM;
77   FileID ID;
78
79   SmallVector<CharSourceRange, 8> CharRanges;
80   unsigned FirstStartColumn;
81   unsigned NextStartColumn;
82   unsigned LastStartColumn;
83 };
84
85 class TokenAnalyzer : public UnwrappedLineConsumer {
86 public:
87   TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
88
89   std::pair<tooling::Replacements, unsigned> process();
90
91 protected:
92   virtual std::pair<tooling::Replacements, unsigned>
93   analyze(TokenAnnotator &Annotator,
94           SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
95           FormatTokenLexer &Tokens) = 0;
96
97   void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
98
99   void finishRun() override;
100
101   FormatStyle Style;
102   // Stores Style, FileID and SourceManager etc.
103   const Environment &Env;
104   // AffectedRangeMgr stores ranges to be fixed.
105   AffectedRangeManager AffectedRangeMgr;
106   SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
107   encoding::Encoding Encoding;
108 };
109
110 } // end namespace format
111 } // end namespace clang
112
113 #endif