Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metagen to unsupported visitor #703

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions include/vast/CodeGen/InvalidMetaGenerator.hpp
Original file line number Diff line number Diff line change
@@ -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
23 changes: 14 additions & 9 deletions include/vast/CodeGen/UnsupportedVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#pragma once

#include "vast/CodeGen/CodeGenMetaGenerator.hpp"
#include "vast/Util/Warnings.hpp"

#include <gap/core/crtp.hpp>
#include <memory>

#include "vast/CodeGen/CodeGenVisitorBase.hpp"

Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -163,6 +167,7 @@ namespace vast::cg
protected:
mcontext_t &mctx;
codegen_builder &bld;
std::shared_ptr< meta_generator > mg;
visitor_view visitors_head;
};

Expand Down
45 changes: 25 additions & 20 deletions lib/vast/CodeGen/CodeGenDriver.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) 2022-present, Trail of Bits, Inc.

Check notice on line 1 in lib/vast/CodeGen/CodeGenDriver.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter (18, 22.04)

Run clang-format on lib/vast/CodeGen/CodeGenDriver.cpp

File lib/vast/CodeGen/CodeGenDriver.cpp does not conform to Custom style guidelines. (lines 137, 138, 139)

#include "vast/CodeGen/CodeGenDriver.hpp"
#include "vast/CodeGen/CodeGenPolicy.hpp"
#include "vast/CodeGen/DefaultCodeGenPolicy.hpp"
#include "vast/Util/Warnings.hpp"

VAST_RELAX_WARNINGS
#include <clang/AST/GlobalDecl.h>
Expand All @@ -11,23 +9,23 @@
#include <mlir/IR/Verifier.h>
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); }
Expand Down Expand Up @@ -90,9 +88,7 @@
}
}

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);
Expand All @@ -101,11 +97,16 @@
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());
}
Expand All @@ -117,12 +118,13 @@
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);

Expand All @@ -132,7 +134,11 @@
| 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
Expand Down Expand Up @@ -160,13 +166,12 @@
// 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()) {
Expand Down