Skip to content

Commit 3ceac1d

Browse files
Heejong Leemeta-codesync[bot]
authored andcommitted
Add specify_implicit_func_param_id codemod
Summary: Add specify_implicit_func_param_id codemod Reviewed By: aristidisp Differential Revision: D90121162 fbshipit-source-id: 5cdfd2dda03cb27c164356ed1a9419d2ddf683f9
1 parent 5d8eb83 commit 3ceac1d

2 files changed

Lines changed: 166 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <fmt/core.h>
18+
19+
#include <thrift/compiler/ast/ast_visitor.h>
20+
#include <thrift/compiler/codemod/codemod.h>
21+
#include <thrift/compiler/codemod/file_manager.h>
22+
23+
using apache::thrift::compiler::basic_ast_visitor;
24+
using apache::thrift::compiler::run_codemod;
25+
using apache::thrift::compiler::source_manager;
26+
using apache::thrift::compiler::t_field;
27+
using apache::thrift::compiler::t_program;
28+
using apache::thrift::compiler::t_program_bundle;
29+
using apache::thrift::compiler::codemod::file_manager;
30+
31+
namespace {
32+
33+
void specify_implicit_func_param_id(file_manager& fm, const t_field& f) {
34+
if (f.explicit_id()) {
35+
return;
36+
}
37+
auto loc = f.type().src_range().begin.offset();
38+
fm.add({loc, loc, fmt::format("{}: ", f.id())});
39+
}
40+
41+
} // namespace
42+
43+
int main(int argc, char** argv) {
44+
return apache::thrift::compiler::run_codemod(
45+
argc, argv, [](source_manager& sm, t_program_bundle& pb) {
46+
t_program& program = *pb.root_program();
47+
basic_ast_visitor<true, file_manager&> visitor;
48+
visitor.add_function_param_visitor(specify_implicit_func_param_id);
49+
file_manager fm(sm, program);
50+
visitor(fm, program);
51+
fm.apply_replacements();
52+
});
53+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# pyre-unsafe
16+
17+
import os
18+
import shutil
19+
import tempfile
20+
import textwrap
21+
import unittest
22+
23+
import pkg_resources
24+
25+
from xplat.thrift.compiler.codemod.test_utils import read_file, run_binary, write_file
26+
27+
28+
class SpecifyImplicitFuncParamIdTest(unittest.TestCase):
29+
def setUp(self):
30+
tmp = tempfile.mkdtemp()
31+
self.addCleanup(shutil.rmtree, tmp, True)
32+
self.tmp = tmp
33+
self.addCleanup(os.chdir, os.getcwd())
34+
os.chdir(self.tmp)
35+
self.maxDiff = None
36+
37+
def test_basic_replace(self):
38+
write_file(
39+
"foo.thrift",
40+
textwrap.dedent(
41+
"""\
42+
struct Annotation {}
43+
44+
struct A {
45+
string foo;
46+
}
47+
48+
service B1 {
49+
i32 bar(i32 a, i32 b);
50+
}
51+
52+
service B2 {
53+
i32 bar(1: i32 a, i32 b);
54+
}
55+
56+
service B3 {
57+
i32 bar(1: i32 a, i32 b, 2: i32 c, i32 d, 3: i32 e);
58+
}
59+
60+
interaction C1 {
61+
void baz(i32 x);
62+
}
63+
64+
interaction C2 {
65+
void baz(
66+
1: i32 x,
67+
2: i32 y,
68+
i32 z
69+
);
70+
}
71+
"""
72+
),
73+
)
74+
75+
binary = pkg_resources.resource_filename(__name__, "codemod")
76+
run_binary(binary, "foo.thrift")
77+
78+
self.assertEqual(
79+
read_file("foo.thrift"),
80+
textwrap.dedent(
81+
"""\
82+
struct Annotation {}
83+
84+
struct A {
85+
string foo;
86+
}
87+
88+
service B1 {
89+
i32 bar(-1: i32 a, -2: i32 b);
90+
}
91+
92+
service B2 {
93+
i32 bar(1: i32 a, -1: i32 b);
94+
}
95+
96+
service B3 {
97+
i32 bar(1: i32 a, -1: i32 b, 2: i32 c, -2: i32 d, 3: i32 e);
98+
}
99+
100+
interaction C1 {
101+
void baz(-1: i32 x);
102+
}
103+
104+
interaction C2 {
105+
void baz(
106+
1: i32 x,
107+
2: i32 y,
108+
-1: i32 z
109+
);
110+
}
111+
"""
112+
),
113+
)

0 commit comments

Comments
 (0)