Skip to content

Commit ac28b0f

Browse files
committed
Add single_file flag
1 parent 99c9dc2 commit ac28b0f

10 files changed

Lines changed: 32 additions & 19 deletions

File tree

cpp2rust/ast_consumer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
namespace cpp2rust {
99
void ASTConsumer::HandleTranslationUnit(clang::ASTContext &ctx) {
10-
auto converter = CreateConverter(rs_code_, ctx, model_, rules_dir_);
10+
auto converter =
11+
CreateConverter(rs_code_, ctx, model_, single_file_, rules_dir_);
1112
converter->SetSema(CI_.getSema());
1213
if (first_) {
1314
converter->EmitFilePreamble();

cpp2rust/ast_consumer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ namespace cpp2rust {
1515
class ASTConsumer : public clang::ASTConsumer {
1616
public:
1717
explicit ASTConsumer(std::string &rs_code, Model model, bool first,
18-
clang::CompilerInstance &CI,
18+
bool single_file, clang::CompilerInstance &CI,
1919
const std::string &rules_dir)
20-
: rs_code_(rs_code), model_(model), first_(first), CI_(CI),
21-
rules_dir_(rules_dir) {}
20+
: rs_code_(rs_code), model_(model), first_(first),
21+
single_file_(single_file), CI_(CI), rules_dir_(rules_dir) {}
2222

2323
void HandleTranslationUnit(clang::ASTContext &ctx) override;
2424

2525
private:
2626
std::string &rs_code_;
2727
Model model_;
2828
bool first_;
29+
bool single_file_;
2930
clang::CompilerInstance &CI_;
3031
const std::string &rules_dir_;
3132
};

cpp2rust/converter/converter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ bool Converter::VisitUsingType(clang::UsingType *type) {
275275
bool Converter::Convert(clang::Decl *decl) { return TraverseDecl(decl); }
276276

277277
bool Converter::VisitTranslationUnitDecl(clang::TranslationUnitDecl *decl) {
278-
StrCat("\n//", GetMainFileName(ctx_) + ".rs\n");
278+
if (!single_file_) {
279+
StrCat("\n//", GetMainFileName(ctx_) + ".rs\n");
280+
}
279281
for (auto *child : decl->decls()) {
280282
if (IsConvertibleDecl(child) &&
281283
(IsInMainFile(child) || !decl_ids_.contains(GetID(child)))) {

cpp2rust/converter/converter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
2424

2525
public:
2626
explicit Converter(std::string &rs_code, clang::ASTContext &ctx,
27+
bool single_file = false,
2728
const char *keyword_unsafe = "unsafe",
2829
const char *keyword_mut = "mut",
2930
const char *keyword_ptr_decay = ".as_mut_ptr()",
3031
const char *keyword_const_fn = keyword::kConst)
31-
: rs_code_(&rs_code), ctx_(ctx), keyword_unsafe_(keyword_unsafe),
32-
keyword_mut_(keyword_mut), keyword_ptr_decay_(keyword_ptr_decay),
32+
: rs_code_(&rs_code), ctx_(ctx), single_file_(single_file),
33+
keyword_unsafe_(keyword_unsafe), keyword_mut_(keyword_mut),
34+
keyword_ptr_decay_(keyword_ptr_decay),
3335
keyword_const_fn_(keyword_const_fn) {}
3436

3537
virtual ~Converter() = default;
@@ -525,6 +527,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
525527

526528
std::string *rs_code_;
527529
clang::ASTContext &ctx_;
530+
bool single_file_ = false;
528531
clang::FunctionDecl *curr_function_ = nullptr;
529532
bool in_function_formals_ = false;
530533
bool in_const_initializer_ = false;

cpp2rust/converter/factory.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ namespace cpp2rust {
1010

1111
std::unique_ptr<Converter> CreateConverter(std::string &rs_code,
1212
clang::ASTContext &ctx, Model model,
13+
bool single_file,
1314
const std::string &rules_dir) {
1415
Mapper::LoadTranslationRules(model, ctx, rules_dir);
1516
switch (model) {
1617
case Model::kUnsafe:
17-
return std::make_unique<Converter>(rs_code, ctx);
18+
return std::make_unique<Converter>(rs_code, ctx, single_file);
1819
case Model::kRefCount:
19-
return std::make_unique<ConverterRefCount>(rs_code, ctx);
20+
return std::make_unique<ConverterRefCount>(rs_code, ctx, single_file);
2021
}
2122
std::unreachable();
2223
}

cpp2rust/converter/factory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ class Converter;
1818

1919
std::unique_ptr<Converter> CreateConverter(std::string &rs_code,
2020
clang::ASTContext &ctx, Model model,
21+
bool single_file,
2122
const std::string &rules_dir);
2223
} // namespace cpp2rust

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
namespace cpp2rust {
1616
ConverterRefCount::ConverterRefCount(std::string &rs_code,
17-
clang::ASTContext &ctx)
18-
: Converter(rs_code, ctx, "", "", ".as_pointer()", ""),
17+
clang::ASTContext &ctx, bool single_file)
18+
: Converter(rs_code, ctx, single_file, "", "", ".as_pointer()", ""),
1919
conversion_kind_({ConversionKind::Unboxed}) {}
2020

2121
void ConverterRefCount::EmitFilePreamble() {

cpp2rust/converter/models/converter_refcount.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
namespace cpp2rust {
99
class ConverterRefCount final : public Converter {
1010
public:
11-
ConverterRefCount(std::string &rs_code, clang::ASTContext &ctx);
11+
ConverterRefCount(std::string &rs_code, clang::ASTContext &ctx,
12+
bool single_file = false);
1213

1314
void EmitFilePreamble() override;
1415

cpp2rust/cpp2rust_lib.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ std::string TranspileSrc(std::string_view cc_code, Model model,
2525

2626
std::string rs_code;
2727
clang::tooling::runToolOnCodeWithArgs(
28-
std::make_unique<FrontendAction>(rs_code, model, true, rules_dir),
28+
std::make_unique<FrontendAction>(rs_code, model, /*first=*/true,
29+
/*single_file=*/true, rules_dir),
2930
cc_code, tool_args, std::filesystem::path(filename).filename().string(),
3031
filename.ends_with(".c") ? CLANG_C_COMPILER : CLANG_CXX_COMPILER);
3132
return rs_code;

cpp2rust/frontend_action.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@ namespace cpp2rust {
1818
class FrontendAction : public clang::ASTFrontendAction {
1919
public:
2020
explicit FrontendAction(std::string &rs_code, Model model, bool first,
21-
const std::string &rules_dir)
22-
: rs_code_(rs_code), model_(model), first_(first), rules_dir_(rules_dir) {
23-
}
21+
bool single_file, const std::string &rules_dir)
22+
: rs_code_(rs_code), model_(model), first_(first),
23+
single_file_(single_file), rules_dir_(rules_dir) {}
2424

2525
std::unique_ptr<clang::ASTConsumer>
2626
CreateASTConsumer(clang::CompilerInstance &CI,
2727
llvm::StringRef InFile) override {
28-
return std::make_unique<ASTConsumer>(rs_code_, model_, first_, CI,
29-
rules_dir_);
28+
return std::make_unique<ASTConsumer>(rs_code_, model_, first_, single_file_,
29+
CI, rules_dir_);
3030
}
3131

3232
private:
3333
std::string &rs_code_;
3434
Model model_;
3535
bool first_;
36+
bool single_file_;
3637
const std::string &rules_dir_;
3738
};
3839

@@ -45,7 +46,8 @@ class FrontendActionFactory : public clang::tooling::FrontendActionFactory {
4546
std::unique_ptr<clang::FrontendAction> create() override {
4647
bool f = first_;
4748
first_ = false;
48-
return std::make_unique<FrontendAction>(rs_code_, model_, f, rules_dir_);
49+
return std::make_unique<FrontendAction>(rs_code_, model_, f,
50+
/*single_file=*/false, rules_dir_);
4951
}
5052

5153
private:

0 commit comments

Comments
 (0)