Skip to content

Commit fcc0246

Browse files
committed
Keep stable name for external symbols
1 parent d9a9785 commit fcc0246

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

cpp2rust/converter/converter_lib.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,19 +332,24 @@ unsigned GetAnonIndex(const clang::NamedDecl *decl) {
332332
return 0;
333333
}
334334

335-
std::string GetID(const clang::Decl *decl) {
336-
assert(decl);
337-
const auto file_name = GetFileName(decl);
338-
const auto line_num = GetLineNumber(decl);
339-
const auto column_num = GetColumnNumber(decl);
335+
static std::string GetLocationID(const clang::Decl *decl) {
336+
return GetFileName(decl) + std::to_string(GetLineNumber(decl)) +
337+
std::to_string(GetColumnNumber(decl));
338+
}
339+
340+
static std::string GetParamSignature(const clang::Decl *decl) {
340341
std::string args;
341342
if (auto fdecl = clang::dyn_cast<clang::FunctionDecl>(decl)) {
342343
for (unsigned i = 0; i < fdecl->getNumParams(); ++i) {
343344
args += fdecl->getParamDecl(i)->getType().getAsString();
344345
}
345346
}
346-
return file_name + std::to_string(line_num) + std::to_string(column_num) +
347-
args;
347+
return args;
348+
}
349+
350+
std::string GetID(const clang::Decl *decl) {
351+
assert(decl);
352+
return GetLocationID(decl) + GetParamSignature(decl);
348353
}
349354

350355
static std::unordered_map<std::string, size_t> type_mapping;
@@ -368,7 +373,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
368373
if (!clang::isa<clang::CXXMethodDecl>(fn)) {
369374
auto mangled =
370375
clang::ASTNameGenerator(decl->getASTContext()).getName(decl) +
371-
GetID(decl);
376+
GetParamSignature(decl);
377+
if (fn->getFormalLinkage() == clang::Linkage::Internal) {
378+
mangled += GetLocationID(decl);
379+
}
372380
auto id =
373381
type_mapping.try_emplace(mangled, type_mapping.size()).first->second;
374382
name += '_';

0 commit comments

Comments
 (0)