xref: /openbsd-src/gnu/llvm/clang/lib/Format/TokenAnalyzer.h (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===--- TokenAnalyzer.h - Analyze Token Streams ----------------*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick ///
9e5dd7070Spatrick /// \file
10e5dd7070Spatrick /// This file declares an abstract TokenAnalyzer, and associated helper
11e5dd7070Spatrick /// classes. TokenAnalyzer can be extended to generate replacements based on
12e5dd7070Spatrick /// an annotated and pre-processed token stream.
13e5dd7070Spatrick ///
14e5dd7070Spatrick //===----------------------------------------------------------------------===//
15e5dd7070Spatrick 
16e5dd7070Spatrick #ifndef LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H
17e5dd7070Spatrick #define LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H
18e5dd7070Spatrick 
19e5dd7070Spatrick #include "AffectedRangeManager.h"
20e5dd7070Spatrick #include "Encoding.h"
21e5dd7070Spatrick #include "FormatToken.h"
22e5dd7070Spatrick #include "FormatTokenLexer.h"
23e5dd7070Spatrick #include "TokenAnnotator.h"
24e5dd7070Spatrick #include "UnwrappedLineParser.h"
25e5dd7070Spatrick #include "clang/Basic/Diagnostic.h"
26e5dd7070Spatrick #include "clang/Basic/DiagnosticOptions.h"
27e5dd7070Spatrick #include "clang/Basic/FileManager.h"
28e5dd7070Spatrick #include "clang/Basic/SourceManager.h"
29e5dd7070Spatrick #include "clang/Format/Format.h"
30e5dd7070Spatrick #include "llvm/ADT/STLExtras.h"
31e5dd7070Spatrick #include "llvm/Support/Debug.h"
32*12c85518Srobert #include <memory>
33e5dd7070Spatrick 
34e5dd7070Spatrick namespace clang {
35e5dd7070Spatrick namespace format {
36e5dd7070Spatrick 
37e5dd7070Spatrick class Environment {
38e5dd7070Spatrick public:
39e5dd7070Spatrick   // This sets up an virtual file system with file \p FileName containing the
40e5dd7070Spatrick   // fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn,
41e5dd7070Spatrick   // that the next lines of \p Code should start at \p NextStartColumn, and
42e5dd7070Spatrick   // that \p Code should end at \p LastStartColumn if it ends in newline.
43e5dd7070Spatrick   // See also the documentation of clang::format::internal::reformat.
44*12c85518Srobert   Environment(StringRef Code, StringRef FileName, unsigned FirstStartColumn = 0,
45e5dd7070Spatrick               unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);
46e5dd7070Spatrick 
getFileID()47e5dd7070Spatrick   FileID getFileID() const { return ID; }
48e5dd7070Spatrick 
getSourceManager()49e5dd7070Spatrick   const SourceManager &getSourceManager() const { return SM; }
50e5dd7070Spatrick 
getCharRanges()51e5dd7070Spatrick   ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
52e5dd7070Spatrick 
53e5dd7070Spatrick   // Returns the column at which the fragment of code managed by this
54e5dd7070Spatrick   // environment starts.
getFirstStartColumn()55e5dd7070Spatrick   unsigned getFirstStartColumn() const { return FirstStartColumn; }
56e5dd7070Spatrick 
57e5dd7070Spatrick   // Returns the column at which subsequent lines of the fragment of code
58e5dd7070Spatrick   // managed by this environment should start.
getNextStartColumn()59e5dd7070Spatrick   unsigned getNextStartColumn() const { return NextStartColumn; }
60e5dd7070Spatrick 
61e5dd7070Spatrick   // Returns the column at which the fragment of code managed by this
62e5dd7070Spatrick   // environment should end if it ends in a newline.
getLastStartColumn()63e5dd7070Spatrick   unsigned getLastStartColumn() const { return LastStartColumn; }
64e5dd7070Spatrick 
65*12c85518Srobert   // Returns nullptr and prints a diagnostic to stderr if the environment
66*12c85518Srobert   // can't be created.
67*12c85518Srobert   static std::unique_ptr<Environment> make(StringRef Code, StringRef FileName,
68*12c85518Srobert                                            ArrayRef<tooling::Range> Ranges,
69*12c85518Srobert                                            unsigned FirstStartColumn = 0,
70*12c85518Srobert                                            unsigned NextStartColumn = 0,
71*12c85518Srobert                                            unsigned LastStartColumn = 0);
72*12c85518Srobert 
73e5dd7070Spatrick private:
74e5dd7070Spatrick   // This is only set if constructed from string.
75e5dd7070Spatrick   std::unique_ptr<SourceManagerForFile> VirtualSM;
76e5dd7070Spatrick 
77e5dd7070Spatrick   // This refers to either a SourceManager provided by users or VirtualSM
78e5dd7070Spatrick   // created for a single file.
79e5dd7070Spatrick   SourceManager &SM;
80e5dd7070Spatrick   FileID ID;
81e5dd7070Spatrick 
82e5dd7070Spatrick   SmallVector<CharSourceRange, 8> CharRanges;
83e5dd7070Spatrick   unsigned FirstStartColumn;
84e5dd7070Spatrick   unsigned NextStartColumn;
85e5dd7070Spatrick   unsigned LastStartColumn;
86e5dd7070Spatrick };
87e5dd7070Spatrick 
88e5dd7070Spatrick class TokenAnalyzer : public UnwrappedLineConsumer {
89e5dd7070Spatrick public:
90e5dd7070Spatrick   TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
91e5dd7070Spatrick 
92*12c85518Srobert   std::pair<tooling::Replacements, unsigned>
93*12c85518Srobert   process(bool SkipAnnotation = false);
94e5dd7070Spatrick 
95e5dd7070Spatrick protected:
96e5dd7070Spatrick   virtual std::pair<tooling::Replacements, unsigned>
97e5dd7070Spatrick   analyze(TokenAnnotator &Annotator,
98e5dd7070Spatrick           SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
99e5dd7070Spatrick           FormatTokenLexer &Tokens) = 0;
100e5dd7070Spatrick 
101e5dd7070Spatrick   void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;
102e5dd7070Spatrick 
103e5dd7070Spatrick   void finishRun() override;
104e5dd7070Spatrick 
105e5dd7070Spatrick   FormatStyle Style;
106e5dd7070Spatrick   // Stores Style, FileID and SourceManager etc.
107e5dd7070Spatrick   const Environment &Env;
108e5dd7070Spatrick   // AffectedRangeMgr stores ranges to be fixed.
109e5dd7070Spatrick   AffectedRangeManager AffectedRangeMgr;
110e5dd7070Spatrick   SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
111e5dd7070Spatrick   encoding::Encoding Encoding;
112e5dd7070Spatrick };
113e5dd7070Spatrick 
114e5dd7070Spatrick } // end namespace format
115e5dd7070Spatrick } // end namespace clang
116e5dd7070Spatrick 
117e5dd7070Spatrick #endif
118