Path: blob/main/contrib/llvm-project/clang/lib/Format/TokenAnalyzer.h
35233 views
//===--- TokenAnalyzer.h - Analyze Token Streams ----------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// This file declares an abstract TokenAnalyzer, and associated helper10/// classes. TokenAnalyzer can be extended to generate replacements based on11/// an annotated and pre-processed token stream.12///13//===----------------------------------------------------------------------===//1415#ifndef LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H16#define LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H1718#include "AffectedRangeManager.h"19#include "FormatTokenLexer.h"20#include "TokenAnnotator.h"2122namespace clang {23namespace format {2425class Environment {26public:27// This sets up an virtual file system with file \p FileName containing the28// fragment \p Code. Assumes that \p Code starts at \p FirstStartColumn,29// that the next lines of \p Code should start at \p NextStartColumn, and30// that \p Code should end at \p LastStartColumn if it ends in newline.31// See also the documentation of clang::format::internal::reformat.32Environment(StringRef Code, StringRef FileName, unsigned FirstStartColumn = 0,33unsigned NextStartColumn = 0, unsigned LastStartColumn = 0);3435FileID getFileID() const { return ID; }3637SourceManager &getSourceManager() const { return SM; }3839ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }4041// Returns the column at which the fragment of code managed by this42// environment starts.43unsigned getFirstStartColumn() const { return FirstStartColumn; }4445// Returns the column at which subsequent lines of the fragment of code46// managed by this environment should start.47unsigned getNextStartColumn() const { return NextStartColumn; }4849// Returns the column at which the fragment of code managed by this50// environment should end if it ends in a newline.51unsigned getLastStartColumn() const { return LastStartColumn; }5253// Returns nullptr and prints a diagnostic to stderr if the environment54// can't be created.55static std::unique_ptr<Environment> make(StringRef Code, StringRef FileName,56ArrayRef<tooling::Range> Ranges,57unsigned FirstStartColumn = 0,58unsigned NextStartColumn = 0,59unsigned LastStartColumn = 0);6061private:62// This is only set if constructed from string.63std::unique_ptr<SourceManagerForFile> VirtualSM;6465// This refers to either a SourceManager provided by users or VirtualSM66// created for a single file.67SourceManager &SM;68FileID ID;6970SmallVector<CharSourceRange, 8> CharRanges;71unsigned FirstStartColumn;72unsigned NextStartColumn;73unsigned LastStartColumn;74};7576class TokenAnalyzer : public UnwrappedLineConsumer {77public:78TokenAnalyzer(const Environment &Env, const FormatStyle &Style);7980std::pair<tooling::Replacements, unsigned>81process(bool SkipAnnotation = false);8283protected:84virtual std::pair<tooling::Replacements, unsigned>85analyze(TokenAnnotator &Annotator,86SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,87FormatTokenLexer &Tokens) = 0;8889void consumeUnwrappedLine(const UnwrappedLine &TheLine) override;9091void finishRun() override;9293FormatStyle Style;94LangOptions LangOpts;95// Stores Style, FileID and SourceManager etc.96const Environment &Env;97// AffectedRangeMgr stores ranges to be fixed.98AffectedRangeManager AffectedRangeMgr;99SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;100encoding::Encoding Encoding;101};102103} // end namespace format104} // end namespace clang105106#endif107108109