From 972c4148b3c099991c57958e342f063473f1933e Mon Sep 17 00:00:00 2001 From: Brent Pappas Date: Thu, 15 Aug 2024 17:04:01 -0400 Subject: [PATCH 1/4] cg: Add invalid meta generator. Add a meta generator that only emits invalid locations. --- include/vast/CodeGen/InvalidMetaGenerator.hpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 include/vast/CodeGen/InvalidMetaGenerator.hpp 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 From 9567732fecd37fb30440fdce9fdbee47e88e4e85 Mon Sep 17 00:00:00 2001 From: Brent Pappas Date: Thu, 15 Aug 2024 17:06:27 -0400 Subject: [PATCH 2/4] cg: Add metagen to unsupported visitor. Parameterize the unsupported visitor with a metagenerator so that clients can choose what locations to emit for unsupported operations. --- include/vast/CodeGen/UnsupportedVisitor.hpp | 23 +++++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) 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; }; From 7b6beac2416ae60c740222d087d62c937c5b1b57 Mon Sep 17 00:00:00 2001 From: Brent Pappas Date: Thu, 15 Aug 2024 17:13:21 -0400 Subject: [PATCH 3/4] cg: Pass invalid metagen to unsupported visitor. Create an invalid metagen and pass it to the unsupported visitor so that vast's default visitor stack emits invalid locations for unsupported operations by default. --- lib/vast/CodeGen/CodeGenDriver.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/vast/CodeGen/CodeGenDriver.cpp b/lib/vast/CodeGen/CodeGenDriver.cpp index 74ee5ae0fa..e69c2cdaef 100644 --- a/lib/vast/CodeGen/CodeGenDriver.cpp +++ b/lib/vast/CodeGen/CodeGenDriver.cpp @@ -3,6 +3,7 @@ #include "vast/CodeGen/CodeGenDriver.hpp" #include "vast/CodeGen/CodeGenPolicy.hpp" #include "vast/CodeGen/DefaultCodeGenPolicy.hpp" +#include "vast/CodeGen/InvalidMetaGenerator.hpp" VAST_RELAX_WARNINGS #include @@ -16,6 +17,8 @@ VAST_UNRELAX_WARNINGS #include "vast/CodeGen/AttrVisitorProxy.hpp" #include "vast/CodeGen/DataLayout.hpp" #include "vast/CodeGen/DefaultVisitor.hpp" +#include "vast/CodeGen/CodeGenDriver.hpp" +#include "vast/CodeGen/InvalidMetaGenerator.hpp" #include "vast/CodeGen/TypeCachingProxy.hpp" #include "vast/CodeGen/UnreachableVisitor.hpp" #include "vast/CodeGen/UnsupportedVisitor.hpp" @@ -106,6 +109,10 @@ namespace vast::cg { 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()); } @@ -123,6 +130,7 @@ namespace vast::cg { 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 +140,7 @@ 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 From ae63da2a05ad1a1cdb8f4b92cbea2e5f2459087a Mon Sep 17 00:00:00 2001 From: xlauko Date: Fri, 30 Aug 2024 09:41:02 +0200 Subject: [PATCH 4/4] cg: Reformat with clang-format. --- lib/vast/CodeGen/CodeGenDriver.cpp | 41 ++++++++++++++---------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/lib/vast/CodeGen/CodeGenDriver.cpp b/lib/vast/CodeGen/CodeGenDriver.cpp index e69c2cdaef..f9246a37a7 100644 --- a/lib/vast/CodeGen/CodeGenDriver.cpp +++ b/lib/vast/CodeGen/CodeGenDriver.cpp @@ -1,9 +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/CodeGen/InvalidMetaGenerator.hpp" +#include "vast/Util/Warnings.hpp" VAST_RELAX_WARNINGS #include @@ -12,25 +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/CodeGenDriver.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); } @@ -93,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); @@ -104,8 +97,9 @@ 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); } @@ -124,7 +118,7 @@ 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); @@ -140,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, std::move(invalid_mg))) + | optional(enable_unsupported, + as_node_with_list_ref< unsup_visitor >( + mctx, *bld, std::move(invalid_mg) + ) + ) | as_node< unreach_visitor >(); // setup driver @@ -168,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()) {