clang  8.0.0svn
TokenAnalyzer.h
Go to the documentation of this file.
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"
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:
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 
80  unsigned FirstStartColumn;
81  unsigned NextStartColumn;
82  unsigned LastStartColumn;
83 };
84 
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 
102  // Stores Style, FileID and SourceManager etc.
103  const Environment &Env;
104  // AffectedRangeMgr stores ranges to be fixed.
108 };
109 
110 } // end namespace format
111 } // end namespace clang
112 
113 #endif
Defines the clang::FileManager interface and associated types.
Defines the SourceManager interface.
AffectedRangeManager class manages affected ranges in the code.
ArrayRef< CharSourceRange > getCharRanges() const
Definition: TokenAnalyzer.h:56
AffectedRangeManager AffectedRangeMgr
const Environment & Env
Contains functions for text encoding manipulation.
This file implements a token annotator, i.e.
This file contains FormatTokenLexer, which tokenizes a source file into a token stream suitable for C...
Defines the Diagnostic-related interfaces.
An unwrapped line is a sequence of Token, that we would like to put on a single line if there was no ...
const SourceManager & getSourceManager() const
Definition: TokenAnalyzer.h:54
unsigned getLastStartColumn() const
Definition: TokenAnalyzer.h:68
Determines extra information about the tokens comprising an UnwrappedLine.
Environment(SourceManager &SM, FileID ID, ArrayRef< CharSourceRange > Ranges)
Definition: TokenAnalyzer.h:39
Various functions to configurably format source code.
This file contains the declaration of the UnwrappedLineParser, which turns a stream of tokens into Un...
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:50
encoding::Encoding Encoding
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
unsigned getFirstStartColumn() const
Definition: TokenAnalyzer.h:60
SmallVector< SmallVector< UnwrappedLine, 16 >, 2 > UnwrappedLines
This file contains the declaration of the FormatToken, a wrapper around Token with additional informa...
unsigned getNextStartColumn() const
Definition: TokenAnalyzer.h:64
This class handles loading and caching of source files into memory.
const FormatStyle & Style