diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index b5955ab..a542f1b 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -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 diff --git a/src/bin/lc.cpp b/src/bin/lc.cpp index ab14592..53c560c 100644 --- a/src/bin/lc.cpp +++ b/src/bin/lc.cpp @@ -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(); @@ -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); @@ -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; } diff --git a/src/lc/clang_ast_to_asr.cpp b/src/lc/clang_ast_to_asr.cpp index 8db3f2e..03c10a1 100644 --- a/src/lc/clang_ast_to_asr.cpp +++ b/src/lc/clang_ast_to_asr.cpp @@ -2748,17 +2748,24 @@ clang::tooling::FrontendActionFactory* newFrontendActionLCompilersFactory(Alloca namespace LC { -static llvm::Expected get_parser(std::string infile) { +static llvm::Expected 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; @@ -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; diff --git a/src/lc/clang_ast_to_asr.h b/src/lc/clang_ast_to_asr.h index 3698412..0565eb1 100644 --- a/src/lc/clang_ast_to_asr.h +++ b/src/lc/clang_ast_to_asr.h @@ -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 diff --git a/tests/parse_comments_01.cpp b/tests/parse_comments_01.cpp new file mode 100644 index 0000000..82844d7 --- /dev/null +++ b/tests/parse_comments_01.cpp @@ -0,0 +1,12 @@ +int main() +{ + // #pragma clang diagnostic push + // #pragma clang diagnostic ignored "-Wunused-variable" + + int a; + + // #pragma clang diagnostic pop + + + return 0; +}