diff --git a/include/vast/CodeGen/InvalidMetaGenerator.hpp b/include/vast/CodeGen/InvalidMetaGenerator.hpp new file mode 100644 index 0000000000..d3efb78d9c --- /dev/null +++ b/include/vast/CodeGen/InvalidMetaGenerator.hpp @@ -0,0 +1,35 @@ +// Copyright (c) 2022-present, Trail of Bits, Inc. + +#pragma once + +#include "vast/Util/Common.hpp" + +VAST_RELAX_WARNINGS +#include "mlir/IR/Location.h" +VAST_UNRELAX_WARNINGS + +#include "vast/CodeGen/CodeGenMetaGenerator.hpp" +#include "vast/CodeGen/Common.hpp" + +namespace vast::cg { + struct invalid_meta_gen final : meta_generator + { + invalid_meta_gen(mcontext_t *mctx) : mctx(mctx) {} + + loc_t location(const clang_decl *decl) const override { + return mlir::UnknownLoc::get(mctx); + } + + loc_t location(const clang_stmt *stmt) const override { + return mlir::UnknownLoc::get(mctx); + } + + loc_t location(const clang_expr *expr) const override { + return mlir::UnknownLoc::get(mctx); + } + + private: + mcontext_t *mctx; + }; + +} // namespace vast::cg diff --git a/include/vast/CodeGen/UnsupportedVisitor.hpp b/include/vast/CodeGen/UnsupportedVisitor.hpp index 0ebcee679a..7ccefcec5e 100644 --- a/include/vast/CodeGen/UnsupportedVisitor.hpp +++ b/include/vast/CodeGen/UnsupportedVisitor.hpp @@ -2,9 +2,11 @@ #pragma once +#include "vast/CodeGen/CodeGenMetaGenerator.hpp" #include "vast/Util/Warnings.hpp" #include +#include #include "vast/CodeGen/CodeGenVisitorBase.hpp" @@ -107,9 +109,11 @@ namespace vast::cg , unsup_type_visitor< unsup_visitor > , unsup_attr_visitor< unsup_visitor > { - unsup_visitor(visitor_base &head, mcontext_t &mctx, codegen_builder &bld) - : mctx(mctx), bld(bld), visitors_head(head) - {} + unsup_visitor( + visitor_base &head, mcontext_t &mctx, codegen_builder &bld, + std::shared_ptr< meta_generator > mg + ) + : mctx(mctx), bld(bld), mg(std::move(mg)), visitors_head(head) {} operation visit(const clang_decl *decl, scope_context &scope) override { return unsup_decl_visitor::visit(decl, scope); @@ -135,16 +139,16 @@ namespace vast::cg return unsup_decl_visitor::visit(decl, scope); } - std::optional< loc_t > location(const clang_decl *) override { - return mlir::UnknownLoc::get(&mctx); + std::optional< loc_t > location(const clang_decl *decl) override { + return mg->location(decl); } - std::optional< loc_t > location(const clang_stmt *) override { - return mlir::UnknownLoc::get(&mctx); + std::optional< loc_t > location(const clang_stmt *stmt) override { + return mg->location(stmt); } - std::optional< loc_t > location(const clang_expr *) override { - return mlir::UnknownLoc::get(&mctx); + std::optional< loc_t > location(const clang_expr *expr) override { + return mg->location(expr); } std::optional< symbol_name > symbol(clang_global decl) override { @@ -163,6 +167,7 @@ namespace vast::cg protected: mcontext_t &mctx; codegen_builder &bld; + std::shared_ptr< meta_generator > mg; visitor_view visitors_head; }; diff --git a/lib/vast/CodeGen/CodeGenDriver.cpp b/lib/vast/CodeGen/CodeGenDriver.cpp index 74ee5ae0fa..f9246a37a7 100644 --- a/lib/vast/CodeGen/CodeGenDriver.cpp +++ b/lib/vast/CodeGen/CodeGenDriver.cpp @@ -1,8 +1,6 @@ // Copyright (c) 2022-present, Trail of Bits, Inc. -#include "vast/CodeGen/CodeGenDriver.hpp" -#include "vast/CodeGen/CodeGenPolicy.hpp" -#include "vast/CodeGen/DefaultCodeGenPolicy.hpp" +#include "vast/Util/Warnings.hpp" VAST_RELAX_WARNINGS #include @@ -11,23 +9,23 @@ VAST_RELAX_WARNINGS #include VAST_UNRELAX_WARNINGS -#include "vast/Frontend/Options.hpp" - #include "vast/CodeGen/AttrVisitorProxy.hpp" +#include "vast/CodeGen/CodeGenDriver.hpp" +#include "vast/CodeGen/CodeGenFunction.hpp" +#include "vast/CodeGen/CodeGenModule.hpp" +#include "vast/CodeGen/CodeGenVisitorList.hpp" #include "vast/CodeGen/DataLayout.hpp" +#include "vast/CodeGen/DefaultCodeGenPolicy.hpp" +#include "vast/CodeGen/DefaultMetaGenerator.hpp" #include "vast/CodeGen/DefaultVisitor.hpp" +#include "vast/CodeGen/IdMetaGenerator.hpp" +#include "vast/CodeGen/InvalidMetaGenerator.hpp" #include "vast/CodeGen/TypeCachingProxy.hpp" #include "vast/CodeGen/UnreachableVisitor.hpp" #include "vast/CodeGen/UnsupportedVisitor.hpp" -#include "vast/CodeGen/CodeGenVisitorList.hpp" - -#include "vast/CodeGen/DefaultMetaGenerator.hpp" -#include "vast/CodeGen/IdMetaGenerator.hpp" #include "vast/Dialect/Core/CoreOps.hpp" -#include "vast/CodeGen/CodeGenModule.hpp" - namespace vast::cg { void driver::emit(clang::DeclGroupRef decls) { generator.emit(decls); } @@ -90,9 +88,7 @@ namespace vast::cg { } } - bool driver::verify() { - return mlir::verify(mod).succeeded(); - } + bool driver::verify() { return mlir::verify(mod).succeeded(); } std::unique_ptr< codegen_builder > mk_codegen_builder(mcontext_t &mctx) { return std::make_unique< codegen_builder >(&mctx); @@ -101,11 +97,16 @@ namespace vast::cg { std::shared_ptr< meta_generator > mk_meta_generator( acontext_t *actx, mcontext_t *mctx, const cc::vast_args &vargs ) { - if (vargs.has_option(cc::opt::locs_as_meta_ids)) + if (vargs.has_option(cc::opt::locs_as_meta_ids)) { return std::make_shared< id_meta_gen >(actx, mctx); + } return std::make_shared< default_meta_gen >(actx, mctx); } + std::shared_ptr< meta_generator > mk_invalid_meta_generator(mcontext_t *mctx) { + return std::make_shared< invalid_meta_gen >(mctx); + } + std::shared_ptr< symbol_generator > mk_symbol_generator(acontext_t &actx) { return std::make_shared< default_symbol_generator >(actx.createMangleContext()); } @@ -117,12 +118,13 @@ namespace vast::cg { std::unique_ptr< driver > mk_default_driver( cc::action_options &opts, const cc::vast_args &vargs, acontext_t &actx, mcontext_t &mctx ) { - auto bld = mk_codegen_builder(mctx); + auto bld = mk_codegen_builder(mctx); // setup visitor list const bool enable_unsupported = !vargs.has_option(cc::opt::disable_unsupported); auto mg = mk_meta_generator(&actx, &mctx, vargs); + auto invalid_mg = mk_invalid_meta_generator(&mctx); auto sg = mk_symbol_generator(actx); auto policy = mk_codegen_policy(opts); @@ -132,7 +134,11 @@ namespace vast::cg { | as_node_with_list_ref< default_visitor >( mctx, actx, *bld, std::move(mg), std::move(sg), std::move(policy) ) - | optional(enable_unsupported, as_node_with_list_ref< unsup_visitor >(mctx, *bld)) + | optional(enable_unsupported, + as_node_with_list_ref< unsup_visitor >( + mctx, *bld, std::move(invalid_mg) + ) + ) | as_node< unreach_visitor >(); // setup driver @@ -160,13 +166,12 @@ namespace vast::cg { // Set the module name to be the name of the main file. TranslationUnitDecl // often contains invalid source locations and isn't a reliable source for the // module location. - auto main_file_id = actx.getSourceManager().getMainFileID(); + auto main_file_id = actx.getSourceManager().getMainFileID(); const auto &main_file = *actx.getSourceManager().getFileEntryForID(main_file_id); return main_file.tryGetRealPathName(); } - namespace detail - { + namespace detail { std::pair< loc_t, std::string > module_loc_name(mcontext_t &mctx, acontext_t &actx) { // TODO use meta generator if (auto path = get_path_to_source(actx); !path.empty()) {