clang: lib/Format/TokenAnalyzer.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/Support/Debug.h"
31#include <type_traits>
32
33#define DEBUG_TYPE "format-formatter"
34
36namespace format {
37
38
39
41public:
45 Fatal = true;
48 llvm::errs() << Message << "\n";
49 }
50 }
51
53
54private:
55 bool Fatal = false;
56};
57
58std::unique_ptr
61 unsigned NextStartColumn, unsigned LastStartColumn) {
62 auto Env = std::make_unique(Code, FileName, FirstStartColumn,
63 NextStartColumn, LastStartColumn);
65 Env->SM.getDiagnostics().setClient(&Diags, false);
71 }
72
73 Env->SM.getBufferData(Env->ID);
75 return nullptr;
76 return Env;
77}
78
80 unsigned FirstStartColumn, unsigned NextStartColumn,
81 unsigned LastStartColumn)
83 ID(VirtualSM->get().getMainFileID()), FirstStartColumn(FirstStartColumn),
84 NextStartColumn(NextStartColumn), LastStartColumn(LastStartColumn) {}
85
88 AffectedRangeMgr(Env.getSourceManager(), Env.getCharRanges()),
89 UnwrappedLines(1),
90 Encoding(encoding::detectEncoding(
91 Env.getSourceManager().getBufferData(Env.getFileID()))) {
92 LLVM_DEBUG(
93 llvm::dbgs() << "File encoding: "
95 << "\n");
97 << "\n");
98}
99
100std::pair<tooling::Replacements, unsigned>
103 llvm::SpecificBumpPtrAllocator Allocator;
107 IdentTable);
112 Allocator, IdentTable);
115 unsigned Penalty = 0;
116 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; ++Run) {
118 LLVM_DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
120 AnnotatedLines.reserve(Lines.size());
121
125 if (!SkipAnnotation)
126 Annotator.annotate(*AnnotatedLines.back());
127 }
128
129 std::pair<tooling::Replacements, unsigned> RunResult =
130 analyze(Annotator, AnnotatedLines, Lex);
131
132 LLVM_DEBUG({
133 llvm::dbgs() << "Replacements for run " << Run << ":\n";
135 llvm::dbgs() << Fix.toString() << "\n";
136 });
139
140 Penalty += RunResult.second;
141 for (const auto &R : RunResult.first) {
142 auto Err = Result.add(R);
143
144
145 if (Err) {
146 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
148 }
149 }
150 }
151 return {Result, Penalty};
152}
153
157}
158
161}
162
163}
164}
AffectedRangeManager class manages affected ranges in the code.
Defines the Diagnostic-related interfaces.
Contains functions for text encoding manipulation.
Defines the clang::FileManager interface and associated types.
This file contains FormatTokenLexer, which tokenizes a source file into a token stream suitable for C...
This file contains the declaration of the FormatToken, a wrapper around Token with additional informa...
Various functions to configurably format source code.
Defines the SourceManager interface.
This file declares an abstract TokenAnalyzer, and associated helper classes.
This file implements a token annotator, i.e.
This file contains the declaration of the UnwrappedLineParser, which turns a stream of tokens into Un...
static CharSourceRange getCharRange(SourceRange R)
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine a...
void FormatDiagnostic(SmallVectorImpl< char > &OutStr) const
Format this diagnostic into a string, substituting the formal arguments into the %0 slots.
Level
The level of the diagnostic, after it has been through mapping.
Implements an efficient mapping from strings to IdentifierInfo nodes.
Parser - This implements a parser for the C family of languages.
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
SourceManager and necessary dependencies (e.g.
SourceManager & getSourceManager() const
Environment(StringRef Code, StringRef FileName, unsigned FirstStartColumn=0, unsigned NextStartColumn=0, unsigned LastStartColumn=0)
static std::unique_ptr< Environment > make(StringRef Code, StringRef FileName, ArrayRef< tooling::Range > Ranges, unsigned FirstStartColumn=0, unsigned NextStartColumn=0, unsigned LastStartColumn=0)
unsigned getFirstStartColumn() const
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
const AdditionalKeywords & getKeywords()
ArrayRef< FormatToken * > lex()
encoding::Encoding Encoding
virtual std::pair< tooling::Replacements, unsigned > analyze(TokenAnnotator &Annotator, SmallVectorImpl< AnnotatedLine * > &AnnotatedLines, FormatTokenLexer &Tokens)=0
SmallVector< SmallVector< UnwrappedLine, 16 >, 2 > UnwrappedLines
TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
std::pair< tooling::Replacements, unsigned > process(bool SkipAnnotation=false)
void consumeUnwrappedLine(const UnwrappedLine &TheLine) override
void finishRun() override
Determines extra information about the tokens comprising an UnwrappedLine.
void annotate(AnnotatedLine &Line)
LangOptions getFormattingLangOpts(const FormatStyle &Style=getLLVMStyle())
Returns the LangOpts that the formatter expects you to set.
StringRef getLanguageName(FormatStyle::LanguageKind Language)
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
The FormatStyle is used to configure the formatting to follow specific guidelines.
LanguageKind Language
Language, this format style is targeted at.
An unwrapped line is a sequence of Token, that we would like to put on a single line if there was no ...