blob: e43a860e46cf3c87f0fca607535563172addc2b7 [file] [log] [blame]
Martin Probstc4a0dd42016-05-20 11:24:24 +00001//===--- 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011/// This file declares an abstract TokenAnalyzer, and associated helper
Martin Probstc4a0dd42016-05-20 11:24:24 +000012/// 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
Martin Probstc4a0dd42016-05-20 11:24:24 +000034namespace clang {
35namespace format {
36
37class Environment {
38public:
39 Environment(SourceManager &SM, FileID ID, ArrayRef<CharSourceRange> Ranges)
Eric Liu2e538082018-05-09 21:35:52 +000040 : SM(SM), ID(ID), CharRanges(Ranges.begin(), Ranges.end()),
41 FirstStartColumn(0), NextStartColumn(0), LastStartColumn(0) {}
Martin Probstc4a0dd42016-05-20 11:24:24 +000042
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000043 // This sets up an virtual file system with file \p FileName containing the
44 // fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn,
45 // that the next lines of \p Code should start at \p NextStartColumn, and
46 // that \p Code should end at \p LastStartColumn if it ends in newline.
47 // See also the documentation of clang::format::internal::reformat.
Eric Liu2e538082018-05-09 21:35:52 +000048 Environment(StringRef Code, StringRef FileName,
49 ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn = 0,
50 unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
Martin Probstc4a0dd42016-05-20 11:24:24 +000051
52 FileID getFileID() const { return ID; }
53
Martin Probstc4a0dd42016-05-20 11:24:24 +000054 const SourceManager &getSourceManager() const { return SM; }
55
Eric Liu2e538082018-05-09 21:35:52 +000056 ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
57
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000058 // Returns the column at which the fragment of code managed by this
59 // environment starts.
60 unsigned getFirstStartColumn() const { return FirstStartColumn; }
61
62 // Returns the column at which subsequent lines of the fragment of code
63 // managed by this environment should start.
64 unsigned getNextStartColumn() const { return NextStartColumn; }
65
66 // Returns the column at which the fragment of code managed by this
67 // environment should end if it ends in a newline.
68 unsigned getLastStartColumn() const { return LastStartColumn; }
69
Martin Probstc4a0dd42016-05-20 11:24:24 +000070private:
Eric Liu2e538082018-05-09 21:35:52 +000071 // This is only set if constructed from string.
72 std::unique_ptr<SourceManagerForFile> VirtualSM;
73
74 // This refers to either a SourceManager provided by users or VirtualSM
75 // created for a single file.
Martin Probstc4a0dd42016-05-20 11:24:24 +000076 SourceManager &SM;
Eric Liu2e538082018-05-09 21:35:52 +000077 FileID ID;
78
79 SmallVector<CharSourceRange, 8> CharRanges;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000080 unsigned FirstStartColumn;
81 unsigned NextStartColumn;
82 unsigned LastStartColumn;
Martin Probstc4a0dd42016-05-20 11:24:24 +000083};
84
85class TokenAnalyzer : public UnwrappedLineConsumer {
86public:
87 TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
88
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000089 std::pair<tooling::Replacements, unsigned> process();
Martin Probstc4a0dd42016-05-20 11:24:24 +000090
91protected:
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000092 virtual std::pair<tooling::Replacements, unsigned>
Martin Probstc4a0dd42016-05-20 11:24:24 +000093 analyze(TokenAnnotator &Annotator,
94 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
Martin Probsta9855af2016-09-02 14:29:48 +000095 FormatTokenLexer &Tokens) = 0;
Martin Probstc4a0dd42016-05-20 11:24:24 +000096
97 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
98
99 void finishRun() override;
100
101 FormatStyle Style;
102 // Stores Style, FileID and SourceManager etc.
103 const Environment &Env;
104 // AffectedRangeMgr stores ranges to be fixed.
105 AffectedRangeManager AffectedRangeMgr;
106 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
107 encoding::Encoding Encoding;
108};
109
110} // end namespace format
111} // end namespace clang
112
113#endif