clang  10.0.0svn
TokenAnalyzer.h
Go to the documentation of this file.
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"
29 #include "clang/Format/Format.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/Support/Debug.h"
32 
33 namespace clang {
34 namespace format {
35 
36 class Environment {
37 public:
38  // This sets up an virtual file system with file \p FileName containing the
39  // fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn,
40  // that the next lines of \p Code should start at \p NextStartColumn, and
41  // that \p Code should end at \p LastStartColumn if it ends in newline.
42  // See also the documentation of clang::format::internal::reformat.
43  Environment(StringRef Code, StringRef FileName,
44  ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn = 0,
45  unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
46 
47  FileID getFileID() const { return ID; }
48 
49  const SourceManager &getSourceManager() const { return SM; }
50 
51  ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
52 
53  // Returns the column at which the fragment of code managed by this
54  // environment starts.
55  unsigned getFirstStartColumn() const { return FirstStartColumn; }
56 
57  // Returns the column at which subsequent lines of the fragment of code
58  // managed by this environment should start.
59  unsigned getNextStartColumn() const { return NextStartColumn; }
60 
61  // Returns the column at which the fragment of code managed by this
62  // environment should end if it ends in a newline.
63  unsigned getLastStartColumn() const { return LastStartColumn; }
64 
65 private:
66  // This is only set if constructed from string.
67  std::unique_ptr<SourceManagerForFile> VirtualSM;
68 
69  // This refers to either a SourceManager provided by users or VirtualSM
70  // created for a single file.
71  SourceManager &SM;
72  FileID ID;
73 
75  unsigned FirstStartColumn;
76  unsigned NextStartColumn;
77  unsigned LastStartColumn;
78 };
79 
81 public:
82  TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
83 
84  std::pair<tooling::Replacements, unsigned> process();
85 
86 protected:
87  virtual std::pair<tooling::Replacements, unsigned>
88  analyze(TokenAnnotator &Annotator,
89  SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
90  FormatTokenLexer &Tokens) = 0;
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.
AffectedRangeManager class manages affected ranges in the code.
ArrayRef< CharSourceRange > getCharRanges() const
Definition: TokenAnalyzer.h:51
AffectedRangeManager AffectedRangeMgr
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...
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:49
unsigned getLastStartColumn() const
Definition: TokenAnalyzer.h:63
Determines extra information about the tokens comprising an UnwrappedLine.
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:49
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:55
SmallVector< SmallVector< UnwrappedLine, 16 >, 2 > UnwrappedLines
Environment(StringRef Code, StringRef FileName, ArrayRef< tooling::Range > Ranges, unsigned FirstStartColumn=0, unsigned NextStartColumn=0, unsigned LastStartColumn=0)
This file contains the declaration of the FormatToken, a wrapper around Token with additional informa...
unsigned getNextStartColumn() const
Definition: TokenAnalyzer.h:59
This class handles loading and caching of source files into memory.
const FormatStyle & Style