mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2024-12-14 03:29:57 +00:00
01402831aa
Add configuration to specify macros. Macros will be expanded, and the code will be parsed and annotated in the expanded state. In a second step, the formatting decisions in the annotated expanded code will be reconstructed onto the original unexpanded macro call. Eventually, this will allow to remove special-case code for various macro options we accumulated over the years in favor of one principled mechanism. Differential Revision: https://reviews.llvm.org/D144170
123 lines
4.2 KiB
C++
123 lines
4.2 KiB
C++
//===--- TestLexer.h - Format C++ code --------------------------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
///
|
|
/// \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(SourceMgr.get(), Style, Lex.getKeywords(), 0,
|
|
Tokens, *this, Allocator, IdentTable);
|
|
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
|