1e336b74cSManuel Klimek //===--- TestLexer.h - Format C++ code --------------------------*- C++ -*-===//
2e336b74cSManuel Klimek //
3c874dd53SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c874dd53SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
5c874dd53SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e336b74cSManuel Klimek //
7e336b74cSManuel Klimek //===----------------------------------------------------------------------===//
8e336b74cSManuel Klimek ///
9e336b74cSManuel Klimek /// \file
10e336b74cSManuel Klimek /// This file contains a TestLexer to create FormatTokens from strings.
11e336b74cSManuel Klimek ///
12e336b74cSManuel Klimek //===----------------------------------------------------------------------===//
13e336b74cSManuel Klimek
14e336b74cSManuel Klimek #ifndef CLANG_UNITTESTS_FORMAT_TESTLEXER_H
15e336b74cSManuel Klimek #define CLANG_UNITTESTS_FORMAT_TESTLEXER_H
16e336b74cSManuel Klimek
17e336b74cSManuel Klimek #include "../../lib/Format/FormatTokenLexer.h"
1885032534SAlex Richardson #include "../../lib/Format/TokenAnalyzer.h"
1985032534SAlex Richardson #include "../../lib/Format/TokenAnnotator.h"
2085032534SAlex Richardson #include "../../lib/Format/UnwrappedLineParser.h"
21e336b74cSManuel Klimek
22e336b74cSManuel Klimek #include "clang/Basic/FileManager.h"
23e336b74cSManuel Klimek #include "clang/Basic/SourceManager.h"
24e336b74cSManuel Klimek
25e336b74cSManuel Klimek #include <numeric>
26e336b74cSManuel Klimek #include <ostream>
27e336b74cSManuel Klimek
28e336b74cSManuel Klimek namespace clang {
29e336b74cSManuel Klimek namespace format {
30e336b74cSManuel Klimek
31*1c58208dSOwen Pan typedef SmallVector<FormatToken *, 8> TokenList;
32e336b74cSManuel Klimek
33e336b74cSManuel Klimek inline std::ostream &operator<<(std::ostream &Stream, const FormatToken &Tok) {
3485032534SAlex Richardson Stream << "(" << Tok.Tok.getName() << ", \"" << Tok.TokenText.str() << "\" , "
3585032534SAlex Richardson << getTokenTypeName(Tok.getType()) << ")";
36e336b74cSManuel Klimek return Stream;
37e336b74cSManuel Klimek }
38e336b74cSManuel Klimek inline std::ostream &operator<<(std::ostream &Stream, const TokenList &Tokens) {
39e336b74cSManuel Klimek Stream << "{";
4034ce42feSMarek Kurdej for (size_t I = 0, E = Tokens.size(); I != E; ++I)
41e336b74cSManuel Klimek Stream << (I > 0 ? ", " : "") << *Tokens[I];
4285032534SAlex Richardson Stream << "} (" << Tokens.size() << " tokens)";
43e336b74cSManuel Klimek return Stream;
44e336b74cSManuel Klimek }
45e336b74cSManuel Klimek
uneof(const TokenList & Tokens)46e336b74cSManuel Klimek inline TokenList uneof(const TokenList &Tokens) {
47e336b74cSManuel Klimek assert(!Tokens.empty() && Tokens.back()->is(tok::eof));
48e336b74cSManuel Klimek return TokenList(Tokens.begin(), std::prev(Tokens.end()));
49e336b74cSManuel Klimek }
50e336b74cSManuel Klimek
text(ArrayRef<FormatToken * > Tokens)51*1c58208dSOwen Pan inline std::string text(ArrayRef<FormatToken *> Tokens) {
52e336b74cSManuel Klimek return std::accumulate(Tokens.begin(), Tokens.end(), std::string(),
53e336b74cSManuel Klimek [](const std::string &R, FormatToken *Tok) {
54e336b74cSManuel Klimek return (R + Tok->TokenText).str();
55e336b74cSManuel Klimek });
56e336b74cSManuel Klimek }
57e336b74cSManuel Klimek
5885032534SAlex Richardson class TestLexer : public UnwrappedLineConsumer {
59e336b74cSManuel Klimek public:
6075a1790fSAlex Richardson TestLexer(llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
6175a1790fSAlex Richardson std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Buffers,
6275a1790fSAlex Richardson FormatStyle Style = getLLVMStyle())
Allocator(Allocator)6375a1790fSAlex Richardson : Allocator(Allocator), Buffers(Buffers), Style(Style),
6400918933SOwen Pan SourceMgr("test.cpp", ""), IdentTable(getFormattingLangOpts(Style)) {}
65e336b74cSManuel Klimek
lex(StringRef Code)66*1c58208dSOwen Pan TokenList lex(StringRef Code) {
6775a1790fSAlex Richardson FormatTokenLexer Lex = getNewLexer(Code);
6875a1790fSAlex Richardson ArrayRef<FormatToken *> Result = Lex.lex();
69e336b74cSManuel Klimek return TokenList(Result.begin(), Result.end());
70e336b74cSManuel Klimek }
71e336b74cSManuel Klimek
annotate(StringRef Code)72*1c58208dSOwen Pan TokenList annotate(StringRef Code) {
7385032534SAlex Richardson FormatTokenLexer Lex = getNewLexer(Code);
7485032534SAlex Richardson auto Tokens = Lex.lex();
7501402831SManuel Klimek UnwrappedLineParser Parser(SourceMgr.get(), Style, Lex.getKeywords(), 0,
7601402831SManuel Klimek Tokens, *this, Allocator, IdentTable);
7785032534SAlex Richardson Parser.parse();
7885032534SAlex Richardson TokenAnnotator Annotator(Style, Lex.getKeywords());
7985032534SAlex Richardson for (auto &Line : UnwrappedLines) {
8085032534SAlex Richardson AnnotatedLine Annotated(Line);
8185032534SAlex Richardson Annotator.annotate(Annotated);
8275a1790fSAlex Richardson Annotator.calculateFormattingInformation(Annotated);
8385032534SAlex Richardson }
8485032534SAlex Richardson UnwrappedLines.clear();
8585032534SAlex Richardson return TokenList(Tokens.begin(), Tokens.end());
8685032534SAlex Richardson }
8785032534SAlex Richardson
id(StringRef Code)88*1c58208dSOwen Pan FormatToken *id(StringRef Code) {
89e336b74cSManuel Klimek auto Result = uneof(lex(Code));
90e336b74cSManuel Klimek assert(Result.size() == 1U && "Code must expand to 1 token.");
91e336b74cSManuel Klimek return Result[0];
92e336b74cSManuel Klimek }
93e336b74cSManuel Klimek
9485032534SAlex Richardson protected:
consumeUnwrappedLine(const UnwrappedLine & TheLine)9585032534SAlex Richardson void consumeUnwrappedLine(const UnwrappedLine &TheLine) override {
9685032534SAlex Richardson UnwrappedLines.push_back(TheLine);
9785032534SAlex Richardson }
finishRun()9885032534SAlex Richardson void finishRun() override {}
9985032534SAlex Richardson
getNewLexer(StringRef Code)10085032534SAlex Richardson FormatTokenLexer getNewLexer(StringRef Code) {
10185032534SAlex Richardson Buffers.push_back(
10285032534SAlex Richardson llvm::MemoryBuffer::getMemBufferCopy(Code, "<scratch space>"));
103*1c58208dSOwen Pan FileID FID =
10485032534SAlex Richardson SourceMgr.get().createFileID(Buffers.back()->getMemBufferRef());
10585032534SAlex Richardson return FormatTokenLexer(SourceMgr.get(), FID, 0, Style, Encoding, Allocator,
10685032534SAlex Richardson IdentTable);
10785032534SAlex Richardson }
10885032534SAlex Richardson
10985032534SAlex Richardson public:
11075a1790fSAlex Richardson llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator;
11175a1790fSAlex Richardson std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Buffers;
112ff6e4441SAlex Richardson FormatStyle Style;
113e336b74cSManuel Klimek encoding::Encoding Encoding = encoding::Encoding_UTF8;
114*1c58208dSOwen Pan SourceManagerForFile SourceMgr;
115e336b74cSManuel Klimek IdentifierTable IdentTable;
11685032534SAlex Richardson SmallVector<UnwrappedLine, 16> UnwrappedLines;
117e336b74cSManuel Klimek };
118e336b74cSManuel Klimek
119e336b74cSManuel Klimek } // namespace format
120e336b74cSManuel Klimek } // namespace clang
121e336b74cSManuel Klimek
122e336b74cSManuel Klimek #endif // LLVM_CLANG_UNITTESTS_FORMAT_TEST_LEXER_H
123