Files
swift-mirror/tools/swift-ast-script/swift-ast-script.cpp
John McCall d53a88d920 Stub out a tool that runs a script over a Swift AST.
The tool is currently hard-coded to find functions in the SwiftUI library that take parameters of type `(...) -> T` where `T: View` but where the parameter isn't annotated with `@ViewBuilder`.  The long-term vision here, of course, is that this reads and interprets a script file, but that's quite a bit more work (especially to generate a million bindings to the AST).  In the meantime, I think having a functional harness that people familiar with the C++ API can easily hack on to make their own tools is still pretty useful.

The harness does try to open a script file and lex the first token of it, because that's exactly as far as I got before deciding to hard-code the query I wanted.  Since this input is otherwise ignored, you can just point the tool at any old `.swift` file (or just an empty file) and it'll be fine.
2019-07-25 01:38:38 -04:00

94 lines
2.7 KiB
C++

//===--- swift-ast-script.cpp ---------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// This utility is a command line tool that searches Swift code for
/// declarations matching the given requirements.
///
//===----------------------------------------------------------------------===//
#include "swift/Frontend/Frontend.h"
#include "swift/FrontendTool/FrontendTool.h"
#include "swift/Basic/LLVMInitialize.h"
#include "ASTScript.h"
#include "ASTScriptConfiguration.h"
using namespace swift;
using namespace scripting;
namespace {
class Observer : public FrontendObserver {
ArrayRef<const char *> Args;
std::unique_ptr<ASTScriptConfiguration> Config;
std::unique_ptr<ASTScript> Script;
bool HadError = false;
public:
Observer(ArrayRef<const char *> args) : Args(args) {}
void configuredCompiler(CompilerInstance &instance) override {
Config = ASTScriptConfiguration::parse(instance, Args);
if (!Config) return flagError();
Script = ASTScript::parse(*Config);
if (!Script) return flagError();
}
void performedSemanticAnalysis(CompilerInstance &instance) override {
if (Script) {
if (Script->execute())
return flagError();
}
}
bool hadError() const {
return HadError;
}
private:
void flagError() {
HadError = true;
}
};
}
int main(int argc, const char *argv[]) {
PROGRAM_START(argc, argv);
// Look for the first "--" in the arguments.
auto argBegin = argv + 1;
auto argEnd = argv + argc;
auto dashDash = std::find(argBegin, argEnd, StringRef("--"));
if (dashDash == argEnd) {
llvm::errs() << "error: missing '--' in arguments to separate "
"script configuration from compiler arguments\n"
"usage:\n"
" swift-grep <script_file> -- <compiler flags>\n";
return 1;
}
Observer observer(llvm::makeArrayRef(argBegin, dashDash));
// Set up the frontend arguments.
unsigned numFrontendArgs = argEnd - (dashDash + 1);
SmallVector<const char *, 8> frontendArgs;
frontendArgs.reserve(numFrontendArgs + 1);
frontendArgs.append(dashDash + 1, argEnd);
frontendArgs.push_back("-typecheck");
int frontendResult =
performFrontend(frontendArgs, argv[0], (void*) &main, &observer);
return (observer.hadError() ? 1 : frontendResult);
}