blob: 5ce44a0f3ea7970ac8b8d771658c2b0661340db3 [file] [log] [blame]
Martin Probstc4a0dd42016-05-20 11:24:24 +00001//===--- TokenAnalyzer.h - Analyze Token Streams ----------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Martin Probstc4a0dd42016-05-20 11:24:24 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file declares an abstract TokenAnalyzer, and associated helper
Martin Probstc4a0dd42016-05-20 11:24:24 +000011/// 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"
26#include "clang/Basic/DiagnosticOptions.h"
27#include "clang/Basic/FileManager.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/Format/Format.h"
30#include "llvm/ADT/STLExtras.h"
31#include "llvm/Support/Debug.h"
32
Martin Probstc4a0dd42016-05-20 11:24:24 +000033namespace clang {
34namespace format {
35
36class Environment {
37public:
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000038 // 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.
Eric Liu2e538082018-05-09 21:35:52 +000043 Environment(StringRef Code, StringRef FileName,
44 ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn = 0,
45 unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
Martin Probstc4a0dd42016-05-20 11:24:24 +000046
47 FileID getFileID() const { return ID; }
48
Martin Probstc4a0dd42016-05-20 11:24:24 +000049 const SourceManager &getSourceManager() const { return SM; }
50
Eric Liu2e538082018-05-09 21:35:52 +000051 ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
52
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000053 // 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
Martin Probstc4a0dd42016-05-20 11:24:24 +000065private:
Eric Liu2e538082018-05-09 21:35:52 +000066 // 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.
Martin Probstc4a0dd42016-05-20 11:24:24 +000071 SourceManager &SM;
Eric Liu2e538082018-05-09 21:35:52 +000072 FileID ID;
73
74 SmallVector<CharSourceRange, 8> CharRanges;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000075 unsigned FirstStartColumn;
76 unsigned NextStartColumn;
77 unsigned LastStartColumn;
Martin Probstc4a0dd42016-05-20 11:24:24 +000078};
79
80class TokenAnalyzer : public UnwrappedLineConsumer {
81public:
82 TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
83
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000084 std::pair<tooling::Replacements, unsigned> process();
Martin Probstc4a0dd42016-05-20 11:24:24 +000085
86protected:
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000087 virtual std::pair<tooling::Replacements, unsigned>
Martin Probstc4a0dd42016-05-20 11:24:24 +000088 analyze(TokenAnnotator &Annotator,
89 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
Martin Probsta9855af2016-09-02 14:29:48 +000090 FormatTokenLexer &Tokens) = 0;
Martin Probstc4a0dd42016-05-20 11:24:24 +000091
92 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
93
94 void finishRun() override;
95
96 FormatStyle Style;
97 // Stores Style, FileID and SourceManager etc.
98 const Environment &Env;
99 // AffectedRangeMgr stores ranges to be fixed.
100 AffectedRangeManager AffectedRangeMgr;
101 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
102 encoding::Encoding Encoding;
103};
104
105} // end namespace format
106} // end namespace clang
107
108#endif