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

Move clang includes to cpp file #78

Merged
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ build/
*.dSYM

## Generated files
src/lc/ast.h
src/lc/parser/tokenizer.cpp
src/lc/parser/parser.output
src/lc/parser/parser.tab.cc
src/lc/parser/parser.tab.hh
src/libasr/asr.h
src/libasr/wasm_visitor.h
src/libasr/config.h
Expand Down
113 changes: 3 additions & 110 deletions src/bin/lc.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#define CLI11_HAS_FILESYSTEM 0
#include <bin/CLI11.hpp>

#include <clang/AST/ASTConsumer.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendAction.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Tooling/CompilationDatabasePluginRegistry.h>
#include <clang/Tooling/Tooling.h>

#include <libasr/alloc.h>
#include <libasr/asr_scopes.h>
#include <libasr/asr.h>
Expand All @@ -31,27 +22,8 @@

#include <iostream>

using namespace clang::driver;
using namespace clang::tooling;
using namespace llvm;

// Constructs an empty compilation database
// and thus helps avoid compilation database not found errors.
class LCCompilationDatabase : public CompilationDatabasePlugin {
public:
~LCCompilationDatabase() {}

std::unique_ptr<CompilationDatabase>
loadFromDirectory(StringRef Directory, std::string &ErrorMessage) {
return std::make_unique<FixedCompilationDatabase>(".", std::vector<std::string>());
}
};

static CompilationDatabasePluginRegistry::Add<LCCompilationDatabase>
LCCompilationDatabasePlugin("lc-compilation-database",
"Constructs an empty compilation database.");


enum class Backend {
llvm, wasm, c, cpp, x86, fortran
};
Expand Down Expand Up @@ -90,67 +62,6 @@ std::string construct_outfile(std::string &arg_file, std::string &ArgO) {
return outfile;
}

class ClangCheckActionFactory {

public:

std::string infile, ast_dump_file, ast_dump_filter;
bool ast_list, ast_print, show_clang_ast;

ClangCheckActionFactory(std::string infile_, std::string ast_dump_file,
std::string ast_dump_filter, bool ast_list,
bool ast_print, bool show_clang_ast): infile(infile_),
ast_dump_file(ast_dump_file), ast_dump_filter(ast_dump_filter),
ast_list(ast_list), ast_print(ast_print), show_clang_ast(show_clang_ast) {}

std::unique_ptr<clang::ASTConsumer> newASTConsumer() {
if (ast_list) {
return clang::CreateASTDeclNodeLister();
} else if ( show_clang_ast ) {
llvm::raw_fd_ostream* llvm_fd_ostream = nullptr;
if ( ast_dump_file.size() > 0 ) {
std::error_code errorCode;
llvm_fd_ostream = new llvm::raw_fd_ostream(ast_dump_file, errorCode);
}
std::unique_ptr<llvm::raw_ostream> llvm_ostream(llvm_fd_ostream);
return clang::CreateASTDumper(std::move(llvm_ostream), ast_dump_filter,
/*DumpDecls=*/true,
/*Deserialize=*/false,
/*DumpLookups=*/false,
/*DumpDeclTypes=*/false,
clang::ADOF_Default);
} else if (ast_print) {
return clang::CreateASTPrinter(nullptr, ast_dump_filter);
}
return std::make_unique<clang::ASTConsumer>();
}
};

namespace LCompilers {

template <typename T>
class LCompilersFrontendActionFactory : public FrontendActionFactory {

public:

Allocator& al;
ASR::asr_t*& tu;

LCompilersFrontendActionFactory(Allocator& al_, ASR::asr_t*& tu_):
al(al_), tu(tu_) {}

std::unique_ptr<clang::FrontendAction> create() override {
return std::make_unique<T>(al, tu);
}
};

template <typename T>
FrontendActionFactory* newFrontendActionLCompilersFactory(Allocator& al_, ASR::asr_t*& tu_) {
return new LCompilersFrontendActionFactory<T>(al_, tu_);
}

}

#define DeclareLCompilersUtilVars \
LCompilers::CompilerOptions compiler_options; \
LCompilers::diag::Diagnostics diagnostics; \
Expand Down Expand Up @@ -745,36 +656,18 @@ int mainApp(int argc, const char **argv) {
return 0;
}

static cl::OptionCategory ClangCheckCategory("clang-check options");
static const opt::OptTable &Options = getDriverOptTable();
int clang_argc = 2;
const char *clang_argv[] = {"lc", arg_files[0].c_str()};
auto ExpectedParser = CommonOptionsParser::create(clang_argc, clang_argv, ClangCheckCategory);
if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError();
return 1;
}

CommonOptionsParser &OptionsParser = ExpectedParser.get();
std::vector<std::string> sourcePaths = OptionsParser.getSourcePathList();
ClangTool Tool(OptionsParser.getCompilations(), sourcePaths);

std::string infile = sourcePaths[0];
std::string infile = arg_files[0];

// Handle Clang related options in the following
if (show_clang_ast || ast_list || ast_print) {
ClangCheckActionFactory CheckFactory(infile, ast_dump_file,
return LCompilers::LC::dump_clang_ast(infile, ast_dump_file,
ast_dump_filter, ast_list, ast_print, show_clang_ast);
int status = Tool.run(newFrontendActionFactory(&CheckFactory).get());
return status;
}

// Handle LC related options
Allocator al(4*1024);
LCompilers::ASR::asr_t* tu = nullptr;
FrontendActionFactory* FrontendFactory;
FrontendFactory = LCompilers::newFrontendActionLCompilersFactory<LCompilers::FindNamedClassAction>(al, tu);
int status = Tool.run(FrontendFactory);
int status = LCompilers::LC::clang_ast_to_asr(al, infile, tu);
if (status != 0) {
return status;
}
Expand Down
1 change: 1 addition & 0 deletions src/lc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(SRC
parser/parser.cpp
parser/parser.tab.cc

clang_ast_to_asr.cpp
utils.cpp
)

Expand Down
Loading
Loading