Skip to content

Commit 63d67f8

Browse files
aristidispmeta-codesync[bot]
authored andcommitted
file_manager: ensure includes are added at the beginning of the next line
Summary: To avoid breaking a line between an existing include and trailing comments (See file_manager_test.cc) Reviewed By: iahs Differential Revision: D90194275 fbshipit-source-id: cb7d685c758eb992c54b38ce03eadc88a59a9b06
1 parent 4d187b0 commit 63d67f8

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

third-party/thrift/src/thrift/compiler/codemod/annotate_allow_legacy_missing_uris_test.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ def test_adds_annotation_existing_package_and_other_includes(self):
101101
""",
102102
)
103103

104+
def test_adds_annotation_existing_package_and_other_includes_with_commend(self):
105+
self._check(
106+
before="""\
107+
include "thrift/annotation/cpp.thrift" // foo
108+
109+
package;
110+
111+
struct S { }
112+
""",
113+
after="""\
114+
include "thrift/annotation/cpp.thrift" // foo
115+
include "thrift/annotation/thrift.thrift"
116+
117+
@thrift.AllowLegacyMissingUris
118+
package;
119+
120+
struct S { }
121+
""",
122+
)
123+
104124
def test_adds_annotation_existing_package_before_include(self):
105125
self._check(
106126
before="""\
@@ -160,6 +180,29 @@ def test_adds_annotation_with_namespaces_and_other_include(self):
160180
package;
161181
162182
183+
struct S { }
184+
""",
185+
)
186+
187+
def test_adds_annotation_with_namespaces_and_other_include_with_comment(self):
188+
self._check(
189+
before="""\
190+
namespace cpp2 foo
191+
192+
include "thrift/annotation/cpp.thrift" // foo
193+
194+
struct S { }
195+
""",
196+
after="""\
197+
namespace cpp2 foo
198+
199+
include "thrift/annotation/cpp.thrift" // foo
200+
include "thrift/annotation/thrift.thrift"
201+
202+
@thrift.AllowLegacyMissingUris
203+
package;
204+
205+
163206
struct S { }
164207
""",
165208
)

third-party/thrift/src/thrift/compiler/codemod/file_manager.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,15 @@ std::optional<size_t> file_manager::add_include(std::string thrift_include) {
157157
const size_t new_include_offset = [&]() -> size_t {
158158
// If there are includes, add new include after the last one.
159159
if (!program_->includes().empty()) {
160-
return to_offset(program_->includes().back()->src_range().end) + 1;
160+
size_t offset = to_offset(program_->includes().back()->src_range().end);
161+
// Move to the beginning of the next line to handle any trailing comments
162+
// on the include line (e.g., include "foo.thrift" // comment)
163+
size_t newline_pos = old_content_.find('\n', offset);
164+
if (newline_pos != std::string::npos) {
165+
return newline_pos + 1;
166+
}
167+
// If no newline found, place at end of content
168+
return old_content_.length();
161169
}
162170

163171
// Otherwise, add the include before the first of: package, namespace or

third-party/thrift/src/thrift/compiler/codemod/test/file_manager_test.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include <string>
18+
#include <glog/logging.h>
1819
#include <gmock/gmock.h>
1920
#include <gtest/gtest.h>
2021
#include <thrift/compiler/ast/t_program_bundle.h>
@@ -56,9 +57,10 @@ Fixture prepare_fixture(std::string_view contents) {
5657
diagnostics_engine diags = make_diagnostics_printer(*sourceManager);
5758
std::unique_ptr<t_program_bundle> programBundle =
5859
parse_ast(*sourceManager, diags, kTestFileName, {});
60+
CHECK_NOTNULL(programBundle.get());
5961

6062
auto fileManager = std::make_unique<file_manager>(
61-
*sourceManager, *programBundle->root_program());
63+
*sourceManager, *CHECK_NOTNULL(programBundle->root_program()));
6264

6365
return Fixture{
6466
.source_manager_ = std::move(sourceManager),
@@ -187,6 +189,19 @@ struct S {}
187189
)");
188190
}
189191

192+
TEST_F(FileManagerTest, add_include_trailing_comment) {
193+
Fixture fixture = prepare_fixture(R"(
194+
include "thrift/annotation/thrift.thrift" // foo
195+
)");
196+
197+
fixture.file_manager_->add_include("some/other/file.thrift");
198+
199+
EXPECT_EQ(fixture.file_manager_->get_new_content(), R"(
200+
include "thrift/annotation/thrift.thrift" // foo
201+
include "some/other/file.thrift"
202+
)");
203+
}
204+
190205
TEST_F(FileManagerTest, has_thrift_include) {
191206
Fixture fixture = prepare_fixture(R"(
192207
include "thrift/annotation/thrift.thrift"

0 commit comments

Comments
 (0)