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 support for retaining comments in clang-AST #109

Merged
merged 4 commits into from
Mar 13, 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
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ jobs:
run: |
export CPATH=$CONDA_PREFIX/include:$CPATH
lc --show-clang-ast tests/test.cpp
lc --show-clang-ast --parse-all-comments tests/parse_comments_01.cpp > parse_comments_01.stdout
grep "TextComment" parse_comments_01.stdout
rm parse_comments_01.stdout
lc examples/expr2.c --show-asr
./run_tests.py

Expand Down
6 changes: 4 additions & 2 deletions src/bin/lc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ int mainApp(int argc, const char **argv) {
std::string arg_backend = "";
bool print_rtl_header_dir = false;
bool print_rtl_dir = false;
bool parse_all_comments = false;

LCompilers::CompilerOptions co;
co.po.runtime_library_dir = LCompilers::LC::get_runtime_library_dir();
Expand All @@ -638,6 +639,7 @@ int mainApp(int argc, const char **argv) {
app.add_option("--backend", arg_backend, "Select a backend (llvm, c, cpp, x86, wasm, fortran)")->capture_default_str();
app.add_flag("--get-rtl-header-dir", print_rtl_header_dir, "Print the path to the runtime library header file");
app.add_flag("--get-rtl-dir", print_rtl_dir, "Print the path to the runtime library file");
app.add_flag("--parse-all-comments", parse_all_comments, "Parse all comments in the input code");

app.get_formatter()->column_width(25);
app.require_subcommand(0, 1);
Expand All @@ -661,13 +663,13 @@ int mainApp(int argc, const char **argv) {
// Handle Clang related options in the following
if (show_clang_ast || ast_list || ast_print) {
return LCompilers::LC::dump_clang_ast(infile, ast_dump_file,
ast_dump_filter, ast_list, ast_print, show_clang_ast);
ast_dump_filter, ast_list, ast_print, show_clang_ast, parse_all_comments);
}

// Handle LC related options
Allocator al(4*1024);
LCompilers::ASR::asr_t* tu = nullptr;
int status = LCompilers::LC::clang_ast_to_asr(al, infile, tu);
int status = LCompilers::LC::clang_ast_to_asr(al, infile, tu, parse_all_comments);
if (status != 0) {
return status;
}
Expand Down
23 changes: 15 additions & 8 deletions src/lc/clang_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2748,17 +2748,24 @@ clang::tooling::FrontendActionFactory* newFrontendActionLCompilersFactory(Alloca

namespace LC {

static llvm::Expected<clang::tooling::CommonOptionsParser> get_parser(std::string infile) {
static llvm::Expected<clang::tooling::CommonOptionsParser> get_parser(std::string infile, bool parse_all_comments) {
static llvm::cl::OptionCategory ClangCheckCategory("clang-check options");
static const llvm::opt::OptTable &Options = clang::driver::getDriverOptTable();
int clang_argc = 3;
const char *clang_argv[] = {"lc", infile.c_str(), "--"};
return clang::tooling::CommonOptionsParser::create(clang_argc, clang_argv, ClangCheckCategory);
if( parse_all_comments ) {
int clang_argc = 4;
const char *clang_argv[] = {"lc", infile.c_str(), "--extra-arg=-fparse-all-comments", "--"};
return clang::tooling::CommonOptionsParser::create(clang_argc, clang_argv, ClangCheckCategory);
} else {
int clang_argc = 3;
const char *clang_argv[] = {"lc", infile.c_str(), "--"};
return clang::tooling::CommonOptionsParser::create(clang_argc, clang_argv, ClangCheckCategory);
}
}

int dump_clang_ast(std::string infile, std::string ast_dump_file,
std::string ast_dump_filter, bool ast_list, bool ast_print, bool show_clang_ast) {
auto ExpectedParser = get_parser(infile);
std::string ast_dump_filter, bool ast_list, bool ast_print,
bool show_clang_ast, bool parse_all_comments) {
auto ExpectedParser = get_parser(infile, parse_all_comments);
if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError();
return 1;
Expand All @@ -2772,8 +2779,8 @@ int dump_clang_ast(std::string infile, std::string ast_dump_file,
return Tool.run(clang::tooling::newFrontendActionFactory(&CheckFactory).get());
}

int clang_ast_to_asr(Allocator &al, std::string infile, ASR::asr_t*& tu) {
auto ExpectedParser = get_parser(infile);
int clang_ast_to_asr(Allocator &al, std::string infile, ASR::asr_t*& tu, bool parse_all_comments) {
auto ExpectedParser = get_parser(infile, parse_all_comments);
if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError();
return 1;
Expand Down
6 changes: 4 additions & 2 deletions src/lc/clang_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

namespace LCompilers::LC {
int dump_clang_ast(std::string infile, std::string ast_dump_file,
std::string ast_dump_filter, bool ast_list, bool ast_print, bool show_clang_ast);
std::string ast_dump_filter, bool ast_list, bool ast_print,
bool show_clang_ast, bool parse_all_comments);

int clang_ast_to_asr(Allocator &al, std::string infile, LCompilers::ASR::asr_t*& tu);
int clang_ast_to_asr(Allocator &al, std::string infile,
LCompilers::ASR::asr_t*& tu, bool parse_all_comments);
} // namespace LCompilers::LC

#endif
12 changes: 12 additions & 0 deletions tests/parse_comments_01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
int main()
{
// #pragma clang diagnostic push
// #pragma clang diagnostic ignored "-Wunused-variable"

int a;

// #pragma clang diagnostic pop


return 0;
}
Loading