clang  16.0.0git
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 #include <memory>
33 
34 namespace clang {
35 namespace format {
36 
37 class Environment {
38 public:
39  // This sets up an virtual file system with file \p FileName containing the
40  // fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn,
41  // that the next lines of \p Code should start at \p NextStartColumn, and
42  // that \p Code should end at \p LastStartColumn if it ends in newline.
43  // See also the documentation of clang::format::internal::reformat.
44  Environment(StringRef Code, StringRef FileName, 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  // Returns nullptr and prints a diagnostic to stderr if the environment
66  // can't be created.
67  static std::unique_ptr<Environment> make(StringRef Code, StringRef FileName,
69  unsigned FirstStartColumn = 0,
70  unsigned NextStartColumn = 0,
71  unsigned LastStartColumn = 0);
72 
73 private:
74  // This is only set if constructed from string.
75  std::unique_ptr<SourceManagerForFile> VirtualSM;
76 
77  // This refers to either a SourceManager provided by users or VirtualSM
78  // created for a single file.
79  SourceManager &SM;
80  FileID ID;
81 
83  unsigned FirstStartColumn;
84  unsigned NextStartColumn;
85  unsigned LastStartColumn;
86 };
87 
89 public:
91 
92  std::pair<tooling::Replacements, unsigned>
93  process(bool SkipAnnotation = false);
94 
95 protected:
96  virtual std::pair<tooling::Replacements, unsigned>
97  analyze(TokenAnnotator &Annotator,
98  SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
99  FormatTokenLexer &Tokens) = 0;
100 
101  void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
102 
103  void finishRun() override;
104 
106  // Stores Style, FileID and SourceManager etc.
107  const Environment &Env;
108  // AffectedRangeMgr stores ranges to be fixed.
112 };
113 
114 } // end namespace format
115 } // end namespace clang
116 
117 #endif
clang::format::TokenAnalyzer::analyze
virtual std::pair< tooling::Replacements, unsigned > analyze(TokenAnnotator &Annotator, SmallVectorImpl< AnnotatedLine * > &AnnotatedLines, FormatTokenLexer &Tokens)=0
clang::format::TokenAnalyzer::Env
const Environment & Env
Definition: TokenAnalyzer.h:107
llvm::SmallVector
Definition: LLVM.h:38
clang::format::FormatStyle
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:54
SourceManager.h
clang::format::UnwrappedLine
An unwrapped line is a sequence of Token, that we would like to put on a single line if there was no ...
Definition: UnwrappedLineParser.h:38
Format.h
clang::format::Environment::getLastStartColumn
unsigned getLastStartColumn() const
Definition: TokenAnalyzer.h:63
clang::format::Environment::getFileID
FileID getFileID() const
Definition: TokenAnalyzer.h:47
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:636
Encoding.h
clang::format::TokenAnalyzer::TokenAnalyzer
TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
Definition: TokenAnalyzer.cpp:86
TokenAnnotator.h
clang::format::encoding::Encoding
Encoding
Definition: Encoding.h:27
clang::format::UnwrappedLineConsumer
Definition: UnwrappedLineParser.h:79
clang::format::TokenAnalyzer::AffectedRangeMgr
AffectedRangeManager AffectedRangeMgr
Definition: TokenAnalyzer.h:109
clang::format::AffectedRangeManager
Definition: AffectedRangeManager.h:25
clang::format::TokenAnalyzer::consumeUnwrappedLine
void consumeUnwrappedLine(const UnwrappedLine &TheLine) override
Definition: TokenAnalyzer.cpp:154
Diagnostic.h
clang::format::TokenAnalyzer::UnwrappedLines
SmallVector< SmallVector< UnwrappedLine, 16 >, 2 > UnwrappedLines
Definition: TokenAnalyzer.h:110
clang::format::Environment::Environment
Environment(StringRef Code, StringRef FileName, unsigned FirstStartColumn=0, unsigned NextStartColumn=0, unsigned LastStartColumn=0)
Definition: TokenAnalyzer.cpp:79
clang::format::Environment
Definition: TokenAnalyzer.h:37
clang::format::Environment::getNextStartColumn
unsigned getNextStartColumn() const
Definition: TokenAnalyzer.h:59
clang::format::TokenAnnotator
Determines extra information about the tokens comprising an UnwrappedLine.
Definition: TokenAnnotator.h:168
clang::format::TokenAnalyzer::Encoding
encoding::Encoding Encoding
Definition: TokenAnalyzer.h:111
UnwrappedLineParser.h
clang::format::Environment::getSourceManager
const SourceManager & getSourceManager() const
Definition: TokenAnalyzer.h:49
clang::format::TokenAnalyzer::finishRun
void finishRun() override
Definition: TokenAnalyzer.cpp:159
llvm::ArrayRef
Definition: LLVM.h:34
clang::format::Environment::getCharRanges
ArrayRef< CharSourceRange > getCharRanges() const
Definition: TokenAnalyzer.h:51
FormatTokenLexer.h
clang
Definition: CalledOnceCheck.h:17
clang::format::TokenAnalyzer
Definition: TokenAnalyzer.h:88
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:38
clang::format::FormatTokenLexer
Definition: FormatTokenLexer.h:39
DiagnosticOptions.h
FormatToken.h
clang::format::TokenAnalyzer::process
std::pair< tooling::Replacements, unsigned > process(bool SkipAnnotation=false)
Definition: TokenAnalyzer.cpp:101
llvm::SmallVectorImpl
Definition: Randstruct.h:18
FileManager.h
clang::format::TokenAnalyzer::Style
FormatStyle Style
Definition: TokenAnalyzer.h:105
clang::format::Environment::getFirstStartColumn
unsigned getFirstStartColumn() const
Definition: TokenAnalyzer.h:55
AffectedRangeManager.h
clang::format::Environment::make
static std::unique_ptr< Environment > make(StringRef Code, StringRef FileName, ArrayRef< tooling::Range > Ranges, unsigned FirstStartColumn=0, unsigned NextStartColumn=0, unsigned LastStartColumn=0)
Definition: TokenAnalyzer.cpp:59