Skip to content

Commit 007ac77

Browse files
Add a new IsConvertableGlobalVarDecl function to avoid empty thread_locals
1 parent 5cf9f0f commit 007ac77

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,17 @@ void Converter::ConvertVaListVarDecl(clang::VarDecl *decl) {
457457
StrCat(keyword_mut_, GetNamedDeclAsString(decl), token::kColon, "VaList");
458458
}
459459

460+
bool Converter::IsConvertableGlobalVarDecl(clang::VarDecl *decl) {
461+
if (!decl->isFileVarDecl()) {
462+
return true;
463+
}
464+
if (decl->isThisDeclarationADefinition() == clang::VarDecl::DeclarationOnly &&
465+
!decl->hasInit()) {
466+
return false;
467+
}
468+
return !globals_.contains(GetNamedDeclAsString(decl));
469+
}
470+
460471
bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
461472
auto qual_type = decl->getType();
462473
auto name = GetNamedDeclAsString(decl);
@@ -467,12 +478,10 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
467478
}
468479

469480
if (decl->isFileVarDecl()) {
470-
if ((decl->isThisDeclarationADefinition() ==
471-
clang::VarDecl::DeclarationOnly &&
472-
!decl->hasInit()) ||
473-
!globals_.insert(name).second) {
481+
if (!IsConvertableGlobalVarDecl(decl)) {
474482
return false;
475483
}
484+
globals_.insert(name);
476485
StrCat(AccessSpecifierAsString(decl->getAccess()), keyword::kStatic,
477486
keyword_mut_);
478487
ENSURE(decl_ids_.insert(GetID(decl)).second);

‎cpp2rust/converter/converter.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
100100

101101
virtual void ConvertGlobalVarDecl(clang::VarDecl *decl);
102102

103+
bool IsConvertableGlobalVarDecl(clang::VarDecl *decl);
104+
103105
virtual void ConvertVaListVarDecl(clang::VarDecl *decl);
104106

105107
virtual bool ConvertVarDeclSkipInit(clang::VarDecl *decl);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,9 @@ void ConverterRefCount::EmitHoistedInArmAssignment(clang::VarDecl *decl) {
740740
}
741741

742742
void ConverterRefCount::ConvertGlobalVarDecl(clang::VarDecl *decl) {
743+
if (!IsConvertableGlobalVarDecl(decl)) {
744+
return;
745+
}
743746
StrCat("thread_local!");
744747
{
745748
PushParen paren(*this);

0 commit comments

Comments
 (0)