-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Reduce the amount of boilerplate in Thrift codemods by moving the top-level code needed to run a codemod to a separate function, imaginatively named `run_codemod`. It handles command-line arguments (which we don't actually use except for the file name and includes), parses the Thrift files and invokes the provided function that does the actual work on a parsed AST. Before: ``` int main(int argc, char** argv) { auto source_mgr = source_manager(); auto program_bundle = parse_and_get_program( source_mgr, std::vector<std::string>(argv, argv + argc)); if (!program_bundle) { return 1; } auto program = program_bundle->root_program(); add_package(source_mgr, *program).run(); return 0; } ``` After: ``` int main(int argc, char** argv) { return apache::thrift::compiler::run_codemod( argc, argv, [](source_manager& sm, t_program& p) { add_package(sm, p).run(); }); } ``` Fix a few minor issues such as wrong includes. Reviewed By: yukselakinci Differential Revision: D68466951 fbshipit-source-id: c8f79b1ac93a72197003350548c59bc8e6af41ce
- Loading branch information
1 parent
9f3f04f
commit db9ec35
Showing
10 changed files
with
161 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <fmt/core.h> | ||
#include <thrift/compiler/codemod/codemod.h> | ||
#include <thrift/compiler/compiler.h> | ||
|
||
namespace apache::thrift::compiler { | ||
|
||
int run_codemod( | ||
int argc, | ||
char** argv, | ||
std::function<void(source_manager&, t_program&)> codemod) { | ||
if (argc <= 1) { | ||
fmt::print(stderr, "Usage: {} <thrift-file>\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
auto source_mgr = source_manager(); | ||
auto program_bundle = parse_and_get_program( | ||
source_mgr, std::vector<std::string>(argv, argv + argc)); | ||
if (!program_bundle) { | ||
return 1; | ||
} | ||
codemod(source_mgr, *program_bundle->root_program()); | ||
return 0; | ||
} | ||
|
||
} // namespace apache::thrift::compiler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <thrift/compiler/ast/t_program.h> | ||
#include <thrift/compiler/source_location.h> | ||
|
||
namespace apache::thrift::compiler { | ||
|
||
// Parses a Thrift file specified in the command-line arguments and runs | ||
// `codemod`, a function implementing the codemod, on the AST representation of | ||
// this file. | ||
[[nodiscard]] int run_codemod( | ||
int argc, | ||
char** argv, | ||
std::function<void(source_manager&, t_program&)> codemod); | ||
|
||
} // namespace apache::thrift::compiler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.