clang  3.9.0
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 #define DEBUG_TYPE "format-formatter"
35 
36 namespace clang {
37 namespace format {
38 
39 class Environment {
40 public:
42  : ID(ID), CharRanges(Ranges.begin(), Ranges.end()), SM(SM) {}
43 
44  Environment(FileID ID, std::unique_ptr<FileManager> FileMgr,
45  std::unique_ptr<SourceManager> VirtualSM,
46  std::unique_ptr<DiagnosticsEngine> Diagnostics,
47  const std::vector<CharSourceRange> &CharRanges)
48  : ID(ID), CharRanges(CharRanges.begin(), CharRanges.end()),
49  SM(*VirtualSM), FileMgr(std::move(FileMgr)),
50  VirtualSM(std::move(VirtualSM)), Diagnostics(std::move(Diagnostics)) {}
51 
52  // This sets up an virtual file system with file \p FileName containing \p
53  // Code.
54  static std::unique_ptr<Environment>
55  CreateVirtualEnvironment(StringRef Code, StringRef FileName,
57 
58  FileID getFileID() const { return ID; }
59 
60  StringRef getFileName() const { return FileName; }
61 
62  ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
63 
64  const SourceManager &getSourceManager() const { return SM; }
65 
66 private:
67  FileID ID;
68  StringRef FileName;
70  SourceManager &SM;
71 
72  // The order of these fields are important - they should be in the same order
73  // as they are created in `CreateVirtualEnvironment` so that they can be
74  // deleted in the reverse order as they are created.
75  std::unique_ptr<FileManager> FileMgr;
76  std::unique_ptr<SourceManager> VirtualSM;
77  std::unique_ptr<DiagnosticsEngine> Diagnostics;
78 };
79 
81 public:
83 
85 
86 protected:
87  virtual tooling::Replacements
88  analyze(TokenAnnotator &Annotator,
89  SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
91 
92  void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
93 
94  void finishRun() override;
95 
97  // Stores Style, FileID and SourceManager etc.
98  const Environment &Env;
99  // AffectedRangeMgr stores ranges to be fixed.
103 };
104 
105 } // end namespace format
106 } // end namespace clang
107 
108 #endif
Defines the clang::FileManager interface and associated types.
Defines the SourceManager interface.
std::set< Replacement > Replacements
A set of Replacements.
Definition: Replacement.h:148
AffectedRangeManager class manages affected ranges in the code.
AffectedRangeManager AffectedRangeMgr
iterator begin() const
Definition: Type.h:4235
const Environment & Env
Definition: TokenAnalyzer.h:98
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...
TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
iterator end() const
An unwrapped line is a sequence of Token, that we would like to put on a single line if there was no ...
Determines extra information about the tokens comprising an UnwrappedLine.
void consumeUnwrappedLine(const UnwrappedLine &TheLine) override
FileID getFileID() const
Definition: TokenAnalyzer.h:58
The result type of a method or function.
tooling::Replacements process()
Environment(SourceManager &SM, FileID ID, ArrayRef< CharSourceRange > Ranges)
Definition: TokenAnalyzer.h:41
Various functions to configurably format source code.
ArrayRef< FormatToken * > Tokens
This file contains the declaration of the UnwrappedLineParser, which turns a stream of tokens into Un...
StringRef getFileName() const
Definition: TokenAnalyzer.h:60
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:46
encoding::Encoding Encoding
static std::unique_ptr< Environment > CreateVirtualEnvironment(StringRef Code, StringRef FileName, ArrayRef< tooling::Range > Ranges)
const SourceManager & getSourceManager() const
Definition: TokenAnalyzer.h:64
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Defines the Diagnostic-related interfaces.
virtual tooling::Replacements analyze(TokenAnnotator &Annotator, SmallVectorImpl< AnnotatedLine * > &AnnotatedLines, FormatTokenLexer &Tokens, tooling::Replacements &Result)=0
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:44
ArrayRef< CharSourceRange > getCharRanges() const
Definition: TokenAnalyzer.h:62
SmallVector< SmallVector< UnwrappedLine, 16 >, 2 > UnwrappedLines
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.