mirror of
https://github.com/github/codeql.git
synced 2025-12-17 09:13:20 +01:00
Swift: Extract files
This commit is contained in:
@@ -3,15 +3,58 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <swift/Basic/LLVMInitialize.h>
|
||||
#include <swift/FrontendTool/FrontendTool.h>
|
||||
|
||||
#include "Extractor.h"
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
class Observer : public swift::FrontendObserver {
|
||||
public:
|
||||
explicit Observer(const codeql::Configuration& config) : config{config} {}
|
||||
|
||||
void performedSemanticAnalysis(swift::CompilerInstance& instance) override {
|
||||
codeql::Extractor extractor{config, instance};
|
||||
extractor.extract();
|
||||
}
|
||||
|
||||
private:
|
||||
const codeql::Configuration& config;
|
||||
};
|
||||
|
||||
static std::string getenv_default(const char* envvar, const std::string& def) {
|
||||
if (const char* var = getenv(envvar)) {
|
||||
return var;
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
PROGRAM_START(argc, argv);
|
||||
if (auto trapDir = getenv("CODEQL_EXTRACTOR_SWIFT_TRAP_DIR")) {
|
||||
std::string file = trapDir;
|
||||
file += "/my_first.trap";
|
||||
if (std::ofstream out{file}) {
|
||||
out << "answer_to_life_the_universe_and_everything(42)\n";
|
||||
}
|
||||
if (argc == 1) {
|
||||
/// TODO: print usage
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
/// The frontend can be called in different modes, we are only interested
|
||||
/// in -frontend mode
|
||||
/// TODO: filter out at the tracer level
|
||||
if ("-frontend"s != argv[1]) {
|
||||
return 0;
|
||||
}
|
||||
PROGRAM_START(argc, argv);
|
||||
|
||||
codeql::Configuration configuration{};
|
||||
configuration.trapDir = getenv_default("CODEQL_EXTRACTOR_SWIFT_TRAP_DIR", ".");
|
||||
configuration.sourceArchiveDir = getenv_default("CODEQL_EXTRACTOR_SWIFT_SOURCE_ARCHIVE_DIR", ".");
|
||||
std::vector<const char*> args;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if ("-frontend"s == argv[i]) {
|
||||
continue;
|
||||
}
|
||||
args.push_back(argv[i]);
|
||||
}
|
||||
configuration.frontendOptions = args;
|
||||
Observer observer(configuration);
|
||||
int frontend_rc = swift::performFrontend(args, "swift-extractor", (void*)main, &observer);
|
||||
llvm::llvm_shutdown();
|
||||
return frontend_rc;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user