Skip to content

Commit b05d9e2

Browse files
committed
Use same ASTNameGenerator for the duratoin of the Converter
1 parent fa5a463 commit b05d9e2

5 files changed

Lines changed: 43 additions & 15 deletions

File tree

‎cpp2rust/converter/converter.h‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
// Distributed under the MIT license that can be found in the LICENSE file.
55

66
#include <clang/AST/ASTContext.h>
7+
#include <clang/AST/Mangle.h>
78
#include <clang/AST/RecursiveASTVisitor.h>
89
#include <clang/Sema/Sema.h>
910

1011
#include <functional>
1112
#include <map>
13+
#include <memory>
1214
#include <optional>
1315
#include <string>
1416
#include <type_traits>
@@ -32,9 +34,18 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
3234
const char *keyword_unsafe = "unsafe",
3335
const char *keyword_mut = keyword::kMut)
3436
: rs_code_(&rs_code), ctx_(ctx), keyword_unsafe_(keyword_unsafe),
35-
keyword_mut_(keyword_mut) {}
37+
keyword_mut_(keyword_mut) {
38+
if (!GetNameGenerator()) {
39+
name_generator_ = std::make_unique<clang::ASTNameGenerator>(ctx);
40+
SetNameGenerator(name_generator_.get());
41+
}
42+
}
3643

37-
virtual ~Converter() = default;
44+
virtual ~Converter() {
45+
if (name_generator_) {
46+
SetNameGenerator(nullptr);
47+
}
48+
}
3849

3950
Converter(const Converter &) = delete;
4051
Converter &operator=(const Converter &) = delete;
@@ -784,6 +795,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
784795

785796
std::string *rs_code_;
786797
clang::ASTContext &ctx_;
798+
std::unique_ptr<clang::ASTNameGenerator> name_generator_;
787799
clang::FunctionDecl *curr_function_ = nullptr;
788800
bool in_function_formals_ = false;
789801
enum class MethodTarget : uint8_t {

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -840,12 +840,25 @@ static std::string GetParamSignature(const clang::Decl *decl) {
840840
return args;
841841
}
842842

843+
static clang::ASTNameGenerator *name_generator = nullptr;
844+
845+
void SetNameGenerator(clang::ASTNameGenerator *generator) {
846+
name_generator = generator;
847+
}
848+
849+
clang::ASTNameGenerator *GetNameGenerator() { return name_generator; }
850+
851+
static std::string GetMangledName(const clang::Decl *decl) {
852+
assert(name_generator);
853+
return name_generator->getName(decl);
854+
}
855+
843856
static std::string GetLexicalSpecializationID(const clang::Decl *decl) {
844857
auto &ctx = decl->getASTContext();
845858
std::string id;
846859
if (const auto *var =
847860
clang::dyn_cast<clang::VarTemplateSpecializationDecl>(decl)) {
848-
id += clang::ASTNameGenerator(var->getASTContext()).getName(var);
861+
id += GetMangledName(var);
849862
}
850863
if (const auto *self =
851864
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
@@ -864,7 +877,7 @@ static std::string GetLexicalSpecializationID(const clang::Decl *decl) {
864877
}
865878
if (const auto *fn = clang::dyn_cast<clang::FunctionDecl>(dc);
866879
fn && fn->getTemplateSpecializationArgs()) {
867-
id += clang::ASTNameGenerator(fn->getASTContext()).getName(fn);
880+
id += GetMangledName(fn);
868881
}
869882
}
870883
return id;
@@ -937,9 +950,7 @@ std::string DisambiguateAnonymousTag(const clang::TagDecl *tag) {
937950
static std::unordered_map<std::string, size_t> type_mapping;
938951

939952
static size_t GetDeclId(const clang::NamedDecl *decl, bool internal) {
940-
std::string key =
941-
clang::ASTNameGenerator(decl->getASTContext()).getName(decl) +
942-
GetParamSignature(decl);
953+
std::string key = GetMangledName(decl) + GetParamSignature(decl);
943954
if (internal) {
944955
key += GetLocationID(decl);
945956
}

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <clang/AST/Decl.h>
88
#include <clang/AST/DeclCXX.h>
99
#include <clang/AST/Expr.h>
10+
#include <clang/AST/Mangle.h>
1011
#include <clang/AST/StmtCXX.h>
1112
#include <clang/AST/Type.h>
1213
#include <llvm/ADT/APSInt.h>
@@ -181,6 +182,10 @@ unsigned GetColumnNumber(const clang::Decl *decl);
181182

182183
unsigned GetArraySize(clang::QualType array_type);
183184

185+
void SetNameGenerator(clang::ASTNameGenerator *generator);
186+
187+
clang::ASTNameGenerator *GetNameGenerator();
188+
184189
std::string GetID(const clang::Decl *decl);
185190
std::string GetMethodID(const clang::CXXMethodDecl *decl);
186191

‎tests/unit/out/refcount/lambda_to_fn_ptr.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ pub fn apply_0(mut x: i32, mut fn_: FnPtr<fn(i32) -> i32>) -> i32 {
1212
pub fn call_nttp_1(mut x: i32) -> i32 {
1313
return ({ (FnPtr::<fn(i32) -> i32>::new(int_operator___int__const::__invoke))(x) });
1414
}
15-
pub fn call_nttp_1(mut x: i32) -> i32 {
15+
pub fn call_nttp_2(mut x: i32) -> i32 {
1616
return ({ (FnPtr::<fn(i32) -> i32>::new(int_operator___int__const::__invoke))(x) });
1717
}
18-
pub fn call_nttp_2(mut x: i32) -> i32 {
18+
pub fn call_nttp_3(mut x: i32) -> i32 {
1919
return ({
2020
FnPtr::<fn(i32) -> i32>::new(|mut x: i32| -> i32 {
2121
{
@@ -55,8 +55,8 @@ fn main_0() -> i32 {
5555
});
5656
assert!((({ p.call(2,) }) == 6));
5757
assert!((({ call_nttp_1(5,) }) == 4));
58-
assert!((({ call_nttp_1(5,) }) == 50));
59-
assert!((({ call_nttp_2(5,) }) == 105));
58+
assert!((({ call_nttp_2(5,) }) == 50));
59+
assert!((({ call_nttp_3(5,) }) == 105));
6060
let sum: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::new(
6161
|n: i32| -> i32 {
6262
{

‎tests/unit/out/unsafe/lambda_to_fn_ptr.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ pub unsafe fn apply_0(mut x: i32, mut fn_: Option<unsafe fn(i32) -> i32>) -> i32
1212
pub unsafe fn call_nttp_1(mut x: i32) -> i32 {
1313
return (unsafe { (Some(int_operator___int__const::__invoke))(x) });
1414
}
15-
pub unsafe fn call_nttp_1(mut x: i32) -> i32 {
15+
pub unsafe fn call_nttp_2(mut x: i32) -> i32 {
1616
return (unsafe { (Some(int_operator___int__const::__invoke))(x) });
1717
}
18-
pub unsafe fn call_nttp_2(mut x: i32) -> i32 {
18+
pub unsafe fn call_nttp_3(mut x: i32) -> i32 {
1919
return (unsafe {
2020
FnPtr::<fn(i32) -> i32>::new(|mut x: i32| -> i32 {
2121
unsafe {
@@ -70,8 +70,8 @@ unsafe fn main_0() -> i32 {
7070
});
7171
assert!(((unsafe { (p).unwrap()(2,) }) == (6)));
7272
assert!(((unsafe { call_nttp_1(5,) }) == (4)));
73-
assert!(((unsafe { call_nttp_1(5,) }) == (50)));
74-
assert!(((unsafe { call_nttp_2(5,) }) == (105)));
73+
assert!(((unsafe { call_nttp_2(5,) }) == (50)));
74+
assert!(((unsafe { call_nttp_3(5,) }) == (105)));
7575
let mut sum: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(|mut n: i32| -> i32 {
7676
unsafe {
7777
let mut ap: *const VaArg = ::std::ptr::null();

0 commit comments

Comments
 (0)