From 0ac8c3aa841212a193bcf027025521f7adf71255 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Mon, 26 Aug 2024 16:37:27 +0900 Subject: Draft base clang plugin --- goatc.cc | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 goatc.cc (limited to 'goatc.cc') diff --git a/goatc.cc b/goatc.cc new file mode 100644 index 0000000..354d377 --- /dev/null +++ b/goatc.cc @@ -0,0 +1,93 @@ +// Clang plugin implementation +// Copyright (C) 2024 Nguyễn Gia Phong +// +// This file is part of GoatC. +// +// GoatC is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// GoatC is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with GoatC. If not, see . + +#include "goatc.hh" + +#include +#include +#include +#include +#include + +bool +GoatCConsumer::HandleTopLevelDecl (clang::DeclGroupRef dg) +{ + if (this->noop) + return true; + + for (auto const& d : dg) + if (auto const& nd = llvm::dyn_cast (d)) + llvm::errs() << "top-level-decl: \"" << nd->getNameAsString() << "\"\n"; + return true; +} + +clang::PluginASTAction::ActionType +GoatCAction::getActionType () +{ + return AddBeforeMainAction; +} + +std::unique_ptr +GoatCAction::CreateASTConsumer (clang::CompilerInstance& ci, + llvm::StringRef in_file) +{ + llvm::errs () << in_file << '\n'; + return std::make_unique (ci, in_file != this->source_file); +} + +void +printHelp (llvm::raw_ostream& ros) +{ + ros << "Usage: goatcflags [OPTION]...\n\nOptions:\n"; + ros << " -h, --help show this help message and exit\n"; + ros << " -s, --source-file PATH path to the file to be chimera'd\n"; +} + +bool +GoatCAction::ParseArgs (const clang::CompilerInstance& ci, + const std::vector &args) +{ + auto& d = ci.getDiagnostics (); + for (unsigned i = 0, e = args.size(); i < e; ++i) + if (args[i] == "-h" || args[i] == "--help") + { + printHelp (llvm::errs ()); + // FIXME: exit with status 0 + return false; + } + else if (args[i] == "-s" || args[i] == "--source-file") + { + if (++i == e) + { + d.Report (d.getCustomDiagID (clang::DiagnosticsEngine::Error, + "missing --source-file argument")); + return false; + } + // TODO: check if file exists + this->source_file = args[i]; + } + + if (this->source_file.empty ()) + { + d.Report (d.getCustomDiagID (clang::DiagnosticsEngine::Error, + "unspecified --source-file")); + return false; + } + + return true; +} -- cgit 1.4.1