File: TestLexer.h

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (123 lines) | stat: -rw-r--r-- 4,207 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//===--- TestLexer.h - Format C++ code --------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains a TestLexer to create FormatTokens from strings.
///
//===----------------------------------------------------------------------===//

#ifndef CLANG_UNITTESTS_FORMAT_TESTLEXER_H
#define CLANG_UNITTESTS_FORMAT_TESTLEXER_H

#include "../../lib/Format/FormatTokenLexer.h"
#include "../../lib/Format/TokenAnalyzer.h"
#include "../../lib/Format/TokenAnnotator.h"
#include "../../lib/Format/UnwrappedLineParser.h"

#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"

#include <numeric>
#include <ostream>

namespace clang {
namespace format {

typedef llvm::SmallVector<FormatToken *, 8> TokenList;

inline std::ostream &operator<<(std::ostream &Stream, const FormatToken &Tok) {
  Stream << "(" << Tok.Tok.getName() << ", \"" << Tok.TokenText.str() << "\" , "
         << getTokenTypeName(Tok.getType()) << ")";
  return Stream;
}
inline std::ostream &operator<<(std::ostream &Stream, const TokenList &Tokens) {
  Stream << "{";
  for (size_t I = 0, E = Tokens.size(); I != E; ++I) {
    Stream << (I > 0 ? ", " : "") << *Tokens[I];
  }
  Stream << "} (" << Tokens.size() << " tokens)";
  return Stream;
}

inline TokenList uneof(const TokenList &Tokens) {
  assert(!Tokens.empty() && Tokens.back()->is(tok::eof));
  return TokenList(Tokens.begin(), std::prev(Tokens.end()));
}

inline std::string text(llvm::ArrayRef<FormatToken *> Tokens) {
  return std::accumulate(Tokens.begin(), Tokens.end(), std::string(),
                         [](const std::string &R, FormatToken *Tok) {
                           return (R + Tok->TokenText).str();
                         });
}

class TestLexer : public UnwrappedLineConsumer {
public:
  TestLexer(llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
            std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Buffers,
            FormatStyle Style = getLLVMStyle())
      : Allocator(Allocator), Buffers(Buffers), Style(Style),
        SourceMgr("test.cpp", ""), IdentTable(getFormattingLangOpts(Style)) {}

  TokenList lex(llvm::StringRef Code) {
    FormatTokenLexer Lex = getNewLexer(Code);
    ArrayRef<FormatToken *> Result = Lex.lex();
    return TokenList(Result.begin(), Result.end());
  }

  TokenList annotate(llvm::StringRef Code) {
    FormatTokenLexer Lex = getNewLexer(Code);
    auto Tokens = Lex.lex();
    UnwrappedLineParser Parser(Style, Lex.getKeywords(), 0, Tokens, *this);
    Parser.parse();
    TokenAnnotator Annotator(Style, Lex.getKeywords());
    for (auto &Line : UnwrappedLines) {
      AnnotatedLine Annotated(Line);
      Annotator.annotate(Annotated);
      Annotator.calculateFormattingInformation(Annotated);
    }
    UnwrappedLines.clear();
    return TokenList(Tokens.begin(), Tokens.end());
  }

  FormatToken *id(llvm::StringRef Code) {
    auto Result = uneof(lex(Code));
    assert(Result.size() == 1U && "Code must expand to 1 token.");
    return Result[0];
  }

protected:
  void consumeUnwrappedLine(const UnwrappedLine &TheLine) override {
    UnwrappedLines.push_back(TheLine);
  }
  void finishRun() override {}

  FormatTokenLexer getNewLexer(StringRef Code) {
    Buffers.push_back(
        llvm::MemoryBuffer::getMemBufferCopy(Code, "<scratch space>"));
    clang::FileID FID =
        SourceMgr.get().createFileID(Buffers.back()->getMemBufferRef());
    return FormatTokenLexer(SourceMgr.get(), FID, 0, Style, Encoding, Allocator,
                            IdentTable);
  }

public:
  llvm::SpecificBumpPtrAllocator<FormatToken>& Allocator;
  std::vector<std::unique_ptr<llvm::MemoryBuffer>>& Buffers;
  FormatStyle Style;
  encoding::Encoding Encoding = encoding::Encoding_UTF8;
  clang::SourceManagerForFile SourceMgr;
  IdentifierTable IdentTable;
  SmallVector<UnwrappedLine, 16> UnwrappedLines;
};

} // namespace format
} // namespace clang

#endif // LLVM_CLANG_UNITTESTS_FORMAT_TEST_LEXER_H