diff --git a/thrift/compiler/codemod/codemod.cc b/thrift/compiler/codemod/codemod.cc index 495befc695b..6ca62936d81 100644 --- a/thrift/compiler/codemod/codemod.cc +++ b/thrift/compiler/codemod/codemod.cc @@ -17,6 +17,8 @@ #include #include #include +#include +#include namespace apache::thrift::compiler { @@ -29,12 +31,27 @@ int run_codemod( return 1; } + // Parse command-line arguments. + auto parsing_params = compiler::parsing_params(); + auto sema_params = compiler::sema_params(); + std::optional filename = detail::parse_command_line_args( + {argv, argv + argc}, parsing_params, sema_params); + if (!filename) { + return 1; + } + parsing_params.allow_missing_includes = true; + sema_params.skip_lowering_annotations = true; + + // Parse the Thrift file. auto source_mgr = source_manager(); - auto program_bundle = parse_and_get_program( - source_mgr, std::vector(argv, argv + argc)); + auto diags = make_diagnostics_printer(source_mgr); + auto program_bundle = + parse_ast(source_mgr, diags, *filename, parsing_params, &sema_params); if (!program_bundle) { return 1; } + + // Run the codemod. codemod(source_mgr, *program_bundle->root_program()); return 0; } diff --git a/thrift/compiler/codemod/test/file_manager_test.cc b/thrift/compiler/codemod/test/file_manager_test.cc index e8c67f81e63..47a8490543e 100644 --- a/thrift/compiler/codemod/test/file_manager_test.cc +++ b/thrift/compiler/codemod/test/file_manager_test.cc @@ -19,22 +19,19 @@ #include #include #include -#include +#include #include -namespace apache::thrift::compiler::codemod { - -namespace { - -using ::testing::Eq; -using ::testing::IsTrue; using ::testing::NotNull; using ::testing::StrEq; using std::literals::string_view_literals::operator""sv; -static const std::string kVirtualFileName("virtual/path/file1.thrift"); +namespace apache::thrift::compiler::codemod { +namespace { + +const std::string test_file_name = "virtual/path/file1.thrift"; -static const std::string kVirtualFileContents(R"( +const std::string test_file_contents = R"( package "test.module" namespace java test.module @@ -42,16 +39,16 @@ namespace java test.module struct Foo { 1: optional i32 bar; } -)"); +)"; } // namespace class FileManagerTest : public ::testing::Test { protected: void SetUp() override { - source_ = source_manager_.add_virtual_file( - kVirtualFileName, kVirtualFileContents); - program_bundle_ = parse_and_get_program( - source_manager_, {"", kVirtualFileName}); + source_ = + source_manager_.add_virtual_file(test_file_name, test_file_contents); + auto diags = make_diagnostics_printer(source_manager_); + program_bundle_ = parse_ast(source_manager_, diags, test_file_name, {}); ASSERT_THAT(program_bundle_, NotNull()); file_manager_ = std::make_unique( @@ -65,9 +62,9 @@ class FileManagerTest : public ::testing::Test { }; TEST_F(FileManagerTest, old_content) { - // NOTE: comparison is done via underlying data() using STREQ because the text + // NOTE: comparison is done via underlying data() using StrEq because the text // in the source manager is explicitly null-terminated. - EXPECT_THAT(file_manager_->old_content().data(), StrEq(kVirtualFileContents)); + EXPECT_THAT(file_manager_->old_content().data(), StrEq(test_file_contents)); } TEST_F(FileManagerTest, add_include) { diff --git a/thrift/compiler/compiler.cc b/thrift/compiler/compiler.cc index b15f02c253c..7c4e3b9a8ad 100644 --- a/thrift/compiler/compiler.cc +++ b/thrift/compiler/compiler.cc @@ -839,6 +839,19 @@ std::unique_ptr parse_and_mutate( } // namespace +std::optional detail::parse_command_line_args( + const std::vector& args, + parsing_params& parsing_params, + sema_params& sema_params) { + gen_params gen_params = {}; + gen_params.targets.emplace_back(); // Avoid the need to pass --gen. + diagnostic_params diag_params = {}; + auto filename = + parse_args(args, parsing_params, gen_params, diag_params, sema_params); + return filename.empty() ? std::nullopt + : std::make_optional(std::move(filename)); +} + std::pair, diagnostic_results> parse_and_mutate_program( source_manager& sm, diff --git a/thrift/compiler/compiler.h b/thrift/compiler/compiler.h index 4a38bc190f2..f454d138b22 100644 --- a/thrift/compiler/compiler.h +++ b/thrift/compiler/compiler.h @@ -17,6 +17,7 @@ #pragma once #include +#include #include #include @@ -24,6 +25,16 @@ #include // parsing_params namespace apache::thrift::compiler { +namespace detail { + +// Parses command-line arguments and returns the input file name if successful; +// otherwise returns an empty optional. +[[nodiscard]] std::optional parse_command_line_args( + const std::vector& args, + parsing_params& parsing_params, + sema_params& sema_params); + +} // namespace detail class t_program_bundle;