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"
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       : 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,
54                            ArrayRef<tooling::Range> Ranges);
55 
56   FileID getFileID() const { return ID; }
57 
58   StringRef getFileName() const { return FileName; }
59 
60   ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
61 
62   const SourceManager &getSourceManager() const { return SM; }
63 
64 private:
65   FileID ID;
66   StringRef FileName;
67   SmallVector<CharSourceRange, 8> CharRanges;
68   SourceManager &SM;
69 
70   // The order of these fields are important - they should be in the same order
71   // as they are created in `CreateVirtualEnvironment` so that they can be
72   // deleted in the reverse order as they are created.
73   std::unique_ptr<FileManager> FileMgr;
74   std::unique_ptr<SourceManager> VirtualSM;
75   std::unique_ptr<DiagnosticsEngine> Diagnostics;
76 };
77 
78 class TokenAnalyzer : public UnwrappedLineConsumer {
79 public:
80   TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
81 
82   tooling::Replacements process();
83 
84 protected:
85   virtual tooling::Replacements
86   analyze(TokenAnnotator &Annotator,
87           SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
88           FormatTokenLexer &Tokens) = 0;
89 
90   void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
91 
92   void finishRun() override;
93 
94   FormatStyle Style;
95   // Stores Style, FileID and SourceManager etc.
96   const Environment &Env;
97   // AffectedRangeMgr stores ranges to be fixed.
98   AffectedRangeManager AffectedRangeMgr;
99   SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
100   encoding::Encoding Encoding;
101 };
102 
103 } // end namespace format
104 } // end namespace clang
105 
106 #endif
107