Clang Project

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