mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[swift-parse-test] Add -skip-bodies option
This is no-op for SwiftParser. Also add a sanity check test file.
This commit is contained in:
@@ -23,35 +23,44 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <numeric>
|
||||
|
||||
using namespace swift;
|
||||
|
||||
namespace {
|
||||
|
||||
struct SwiftParseTestOptions {
|
||||
llvm::cl::opt<bool> SwiftParser =
|
||||
llvm::cl::opt<bool>("swift-parser", llvm::cl::desc("Use SwiftParser"));
|
||||
enum class Executor {
|
||||
SwiftParser,
|
||||
LibParse,
|
||||
};
|
||||
|
||||
llvm::cl::opt<bool> LibParse =
|
||||
llvm::cl::opt<bool>("lib-parse", llvm::cl::desc("Use libParse"));
|
||||
enum class ExecuteOptionFlag {
|
||||
/// Enable body skipping
|
||||
SkipBodies = 1 << 0,
|
||||
};
|
||||
using ExecuteOptions = OptionSet<ExecuteOptionFlag>;
|
||||
|
||||
struct SwiftParseTestOptions {
|
||||
llvm::cl::list<Executor> Executors = llvm::cl::list<Executor>(
|
||||
llvm::cl::desc("Available parsers"),
|
||||
llvm::cl::values(
|
||||
clEnumValN(Executor::SwiftParser, "swift-parser", "SwiftParser"),
|
||||
clEnumValN(Executor::LibParse, "lib-parse", "libParse")));
|
||||
|
||||
llvm::cl::opt<unsigned int> Iteration = llvm::cl::opt<unsigned int>(
|
||||
"n", llvm::cl::desc("iteration"), llvm::cl::init(1));
|
||||
|
||||
llvm::cl::opt<bool> SkipBodies = llvm::cl::opt<bool>(
|
||||
"skip-bodies",
|
||||
llvm::cl::desc("skip function bodies and type members if possible"));
|
||||
|
||||
llvm::cl::list<std::string> InputPaths = llvm::cl::list<std::string>(
|
||||
llvm::cl::Positional, llvm::cl::desc("input paths"));
|
||||
};
|
||||
|
||||
enum class ParseMode {
|
||||
SwiftParser,
|
||||
LibParse,
|
||||
};
|
||||
|
||||
struct LibParseExecutor {
|
||||
constexpr static StringRef name = "libParse";
|
||||
|
||||
static void performParse(llvm::MemoryBufferRef buffer) {
|
||||
static void performParse(llvm::MemoryBufferRef buffer, ExecuteOptions opts) {
|
||||
SourceManager SM;
|
||||
unsigned bufferID =
|
||||
SM.addNewSourceBuffer(llvm::MemoryBuffer::getMemBuffer(buffer));
|
||||
@@ -67,11 +76,9 @@ struct LibParseExecutor {
|
||||
clangOpts, symbolOpts, SM, diagEngine));
|
||||
|
||||
SourceFile::ParsingOptions parseOpts;
|
||||
// Always disable body skipping because SwiftParser currently don't have it.
|
||||
// When SwiftParser implements delayed parsing, this should be a command
|
||||
// line option.
|
||||
parseOpts |= SourceFile::ParsingFlags::DisableDelayedBodies;
|
||||
parseOpts |= SourceFile::ParsingFlags::DisablePoundIfEvaluation;
|
||||
if (!opts.contains(ExecuteOptionFlag::SkipBodies))
|
||||
parseOpts |= SourceFile::ParsingFlags::DisableDelayedBodies;
|
||||
|
||||
ModuleDecl *M = ModuleDecl::create(Identifier(), *ctx);
|
||||
SourceFile *SF =
|
||||
@@ -86,11 +93,14 @@ struct LibParseExecutor {
|
||||
struct SwiftParserExecutor {
|
||||
constexpr static StringRef name = "SwiftParser";
|
||||
|
||||
static void performParse(llvm::MemoryBufferRef buffer) {
|
||||
static void performParse(llvm::MemoryBufferRef buffer, ExecuteOptions opts) {
|
||||
#if SWIFT_BUILD_SWIFT_SYNTAX
|
||||
// TODO: Implement 'ExecuteOptionFlag::SkipBodies'
|
||||
auto sourceFile = swift_ASTGen_parseSourceFile(
|
||||
buffer.getBufferStart(), buffer.getBufferSize(), "",
|
||||
buffer.getBufferIdentifier().data(), nullptr);
|
||||
buffer.getBufferStart(), buffer.getBufferSize(), /*moduleName=*/"",
|
||||
buffer.getBufferIdentifier().data(), /*ASTContext=*/nullptr);
|
||||
swift_ASTGen_destroySourceFile(sourceFile);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
@@ -144,7 +154,8 @@ static std::pair<Duration, Duration> measure(llvm::function_ref<void()> body) {
|
||||
template <typename Executor>
|
||||
static void
|
||||
perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
|
||||
unsigned iteration, uintmax_t totalBytes, uintmax_t totalLines) {
|
||||
ExecuteOptions opts, unsigned iteration, uintmax_t totalBytes,
|
||||
uintmax_t totalLines) {
|
||||
|
||||
llvm::outs() << "----\n";
|
||||
llvm::outs() << "parser: " << Executor::name << "\n";
|
||||
@@ -156,7 +167,7 @@ perform(const SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &buffers,
|
||||
for (unsigned i = 0; i < iteration; i++) {
|
||||
for (auto &buffer : buffers) {
|
||||
std::pair<duration_t, duration_t> elapsed = measure<duration_t>(
|
||||
[&] { Executor::performParse(buffer->getMemBufferRef()); });
|
||||
[&] { Executor::performParse(buffer->getMemBufferRef(), opts); });
|
||||
tDuration += elapsed.first;
|
||||
cDuration += elapsed.second;
|
||||
}
|
||||
@@ -185,6 +196,9 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
|
||||
"Swift parse test\n");
|
||||
|
||||
unsigned iteration = options.Iteration;
|
||||
ExecuteOptions execOptions;
|
||||
if (options.SkipBodies)
|
||||
execOptions |= ExecuteOptionFlag::SkipBodies;
|
||||
|
||||
SmallVector<std::unique_ptr<llvm::MemoryBuffer>> buffers;
|
||||
loadSources(options.InputPaths, buffers);
|
||||
@@ -199,11 +213,23 @@ int swift_parse_test_main(ArrayRef<const char *> args, const char *argv0,
|
||||
<< llvm::format("total bytes: %8d\n", byteCount)
|
||||
<< llvm::format("total lines: %8d\n", lineCount)
|
||||
<< llvm::format("iterations: %8d\n", iteration);
|
||||
|
||||
if (options.SwiftParser)
|
||||
perform<SwiftParserExecutor>(buffers, iteration, byteCount, lineCount);
|
||||
if (options.LibParse)
|
||||
perform<LibParseExecutor>(buffers, iteration, byteCount, lineCount);
|
||||
for (auto mode : options.Executors) {
|
||||
switch (mode) {
|
||||
case Executor::SwiftParser:
|
||||
#if SWIFT_BUILD_SWIFT_SYNTAX
|
||||
perform<SwiftParserExecutor>(buffers, execOptions, iteration, byteCount,
|
||||
lineCount);
|
||||
break;
|
||||
#else
|
||||
llvm::errs() << "error: SwiftParser is not enabled\n";
|
||||
return 1;
|
||||
#endif
|
||||
case Executor::LibParse:
|
||||
perform<LibParseExecutor>(buffers, execOptions, iteration, byteCount,
|
||||
lineCount);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user