clang  6.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 /// \brief 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  : ID(ID), CharRanges(Ranges.begin(), Ranges.end()), SM(SM) {}
41 
42  Environment(FileID ID, std::unique_ptr<FileManager> FileMgr,
43  std::unique_ptr<SourceManager> VirtualSM,
44  std::unique_ptr<DiagnosticsEngine> Diagnostics,
45  const std::vector<CharSourceRange> &CharRanges)
46  : ID(ID), CharRanges(CharRanges.begin(), CharRanges.end()),
47  SM(*VirtualSM), FileMgr(std::move(FileMgr)),
48  VirtualSM(std::move(VirtualSM)), Diagnostics(std::move(Diagnostics)) {}
49 
50  // This sets up an virtual file system with file \p FileName containing \p
51  // Code.
52  static std::unique_ptr<Environment>
53  CreateVirtualEnvironment(StringRef Code, StringRef FileName,
55 
56  FileID getFileID() const { return ID; }
57 
58  ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
59 
60  const SourceManager &getSourceManager() const { return SM; }
61 
62 private:
63  FileID ID;
65  SourceManager &SM;
66 
67  // The order of these fields are important - they should be in the same order
68  // as they are created in `CreateVirtualEnvironment` so that they can be
69  // deleted in the reverse order as they are created.
70  std::unique_ptr<FileManager> FileMgr;
71  std::unique_ptr<SourceManager> VirtualSM;
72  std::unique_ptr<DiagnosticsEngine> Diagnostics;
73 };
74 
76 public:
77  TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
78 
79  tooling::Replacements process();
80 
81 protected:
82  virtual tooling::Replacements
83  analyze(TokenAnnotator &Annotator,
84  SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
85  FormatTokenLexer &Tokens) = 0;
86 
87  void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
88 
89  void finishRun() override;
90 
92  // Stores Style, FileID and SourceManager etc.
93  const Environment &Env;
94  // AffectedRangeMgr stores ranges to be fixed.
98 };
99 
100 } // end namespace format
101 } // end namespace clang
102 
103 #endif
Defines the clang::FileManager interface and associated types.
Defines the SourceManager interface.
AffectedRangeManager class manages affected ranges in the code.
Maintains a set of replacements that are conflict-free.
Definition: Replacement.h:205
ArrayRef< CharSourceRange > getCharRanges() const
Definition: TokenAnalyzer.h:58
AffectedRangeManager AffectedRangeMgr
Definition: TokenAnalyzer.h:95
const Environment & Env
Definition: TokenAnalyzer.h:93
Contains functions for text encoding manipulation.
Definition: Format.h:1821
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:60
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:46
encoding::Encoding Encoding
Definition: TokenAnalyzer.h:97
static std::unique_ptr< Environment > CreateVirtualEnvironment(StringRef Code, StringRef FileName, ArrayRef< tooling::Range > Ranges)
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
Environment(FileID ID, std::unique_ptr< FileManager > FileMgr, std::unique_ptr< SourceManager > VirtualSM, std::unique_ptr< DiagnosticsEngine > Diagnostics, const std::vector< CharSourceRange > &CharRanges)
Definition: TokenAnalyzer.h:42
SmallVector< SmallVector< UnwrappedLine, 16 >, 2 > UnwrappedLines
Definition: TokenAnalyzer.h:96
This file contains the declaration of the FormatToken, a wrapper around Token with additional informa...
This class handles loading and caching of source files into memory.
const FormatStyle & Style