diff options
author | Martin Probst <martin@probst.io> | 2016-05-20 11:24:24 +0000 |
---|---|---|
committer | Martin Probst <martin@probst.io> | 2016-05-20 11:24:24 +0000 |
commit | c4a0dd49a3906ef86520d352562e3841672464f9 (patch) | |
tree | 80fe456d449b7d853502b490408916e6f6f3e39e /clang/lib/Format/TokenAnalyzer.h | |
parent | 6c9472c57fe6377aeb8d52bca4b63044d1bc4662 (diff) | |
download | bcm5719-llvm-c4a0dd49a3906ef86520d352562e3841672464f9.tar.gz bcm5719-llvm-c4a0dd49a3906ef86520d352562e3841672464f9.zip |
clang-format: [JS] sort ES6 imports.
Summary:
This change automatically sorts ES6 imports and exports into four groups:
absolute imports, parent imports, relative imports, and then exports. Exports
are sorted in the same order, but not grouped further.
To keep JS import sorting out of Format.cpp, this required extracting the
TokenAnalyzer infrastructure to separate header and implementation files.
Reviewers: djasper
Subscribers: cfe-commits, klimek
Differential Revision: http://reviews.llvm.org/D20198
llvm-svn: 270203
Diffstat (limited to 'clang/lib/Format/TokenAnalyzer.h')
-rw-r--r-- | clang/lib/Format/TokenAnalyzer.h | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/clang/lib/Format/TokenAnalyzer.h b/clang/lib/Format/TokenAnalyzer.h new file mode 100644 index 00000000000..c1aa9c594fc --- /dev/null +++ b/clang/lib/Format/TokenAnalyzer.h @@ -0,0 +1,108 @@ +//===--- TokenAnalyzer.h - Analyze Token Streams ----------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief This file declares an abstract TokenAnalyzer, and associated helper +/// classes. TokenAnalyzer can be extended to generate replacements based on +/// an annotated and pre-processed token stream. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H +#define LLVM_CLANG_LIB_FORMAT_TOKENANALYZER_H + +#include "AffectedRangeManager.h" +#include "Encoding.h" +#include "FormatToken.h" +#include "FormatTokenLexer.h" +#include "TokenAnnotator.h" +#include "UnwrappedLineParser.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticOptions.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Format/Format.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Debug.h" + +#define DEBUG_TYPE "format-formatter" + +namespace clang { +namespace format { + +class Environment { +public: + Environment(SourceManager &SM, FileID ID, ArrayRef<CharSourceRange> Ranges) + : ID(ID), CharRanges(Ranges.begin(), Ranges.end()), SM(SM) {} + + Environment(FileID ID, std::unique_ptr<FileManager> FileMgr, + std::unique_ptr<SourceManager> VirtualSM, + std::unique_ptr<DiagnosticsEngine> Diagnostics, + const std::vector<CharSourceRange> &CharRanges) + : ID(ID), CharRanges(CharRanges.begin(), CharRanges.end()), + SM(*VirtualSM), FileMgr(std::move(FileMgr)), + VirtualSM(std::move(VirtualSM)), Diagnostics(std::move(Diagnostics)) {} + + // This sets up an virtual file system with file \p FileName containing \p + // Code. + static std::unique_ptr<Environment> + CreateVirtualEnvironment(StringRef Code, StringRef FileName, + ArrayRef<tooling::Range> Ranges); + + FileID getFileID() const { return ID; } + + StringRef getFileName() const { return FileName; } + + ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; } + + const SourceManager &getSourceManager() const { return SM; } + +private: + FileID ID; + StringRef FileName; + SmallVector<CharSourceRange, 8> CharRanges; + SourceManager &SM; + + // The order of these fields are important - they should be in the same order + // as they are created in `CreateVirtualEnvironment` so that they can be + // deleted in the reverse order as they are created. + std::unique_ptr<FileManager> FileMgr; + std::unique_ptr<SourceManager> VirtualSM; + std::unique_ptr<DiagnosticsEngine> Diagnostics; +}; + +class TokenAnalyzer : public UnwrappedLineConsumer { +public: + TokenAnalyzer(const Environment &Env, const FormatStyle &Style); + + tooling::Replacements process(); + +protected: + virtual tooling::Replacements + analyze(TokenAnnotator &Annotator, + SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, + FormatTokenLexer &Tokens, tooling::Replacements &Result) = 0; + + void consumeUnwrappedLine(const UnwrappedLine &TheLine) override; + + void finishRun() override; + + FormatStyle Style; + // Stores Style, FileID and SourceManager etc. + const Environment &Env; + // AffectedRangeMgr stores ranges to be fixed. + AffectedRangeManager AffectedRangeMgr; + SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; + encoding::Encoding Encoding; +}; + +} // end namespace format +} // end namespace clang + +#endif |