Skip to content

Commit c2ce474

Browse files
Don't generate empty thread_locals
Co-authored-by: Francisco Gouveia <francisco.t.gouveia@tecnico.ulisboa.pt>
1 parent 2f5de0c commit c2ce474

4 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,12 @@ bool Converter::NeedsMut(const clang::VarDecl *decl, clang::QualType type,
499499

500500
bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
501501
auto qual_type = decl->getType();
502-
auto name = GetNamedDeclAsString(decl);
503-
504502
if (IsVaListType(qual_type) && decl->isLocalVarDecl()) {
505503
ConvertVaListVarDecl(decl);
506504
return true;
507505
}
508506

507+
auto name = GetNamedDeclAsString(decl);
509508
if (decl->isFileVarDecl()) {
510509
if ((decl->isThisDeclarationADefinition() ==
511510
clang::VarDecl::DeclarationOnly &&

‎cpp2rust/converter/converter.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,19 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
453453

454454
#define StrCat(...) _StrCat(__FUNCTION__, __LINE__, __VA_ARGS__)
455455

456+
inline bool is_empty(char c) { return false; }
457+
inline bool is_empty(const char* s) { return s == nullptr || *s == '\0'; }
458+
template <size_t N>
459+
inline bool is_empty(const char (&s)[N]) { return s[0] == '\0'; }
460+
template <typename T>
461+
inline bool is_empty(const T &s) { return s.empty(); }
462+
456463
template <typename... Ts>
457464
inline void _StrCat(const char *func, int line, const Ts &...vals) {
458465
log() << '[' << func << ':' << line << "] ";
459-
((log() << vals << '\n', *rs_code_ += vals, *rs_code_ += ' '), ...);
466+
((log() << vals << '\n',
467+
*rs_code_ += vals,
468+
(is_empty(vals) ? void() : void(*rs_code_ += ' '))), ...);
460469
}
461470

462471
class Buffer {

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,19 @@ void ConverterRefCount::EmitHoistedInArmAssignment(clang::VarDecl *decl) {
721721
}
722722

723723
void ConverterRefCount::ConvertGlobalVarDecl(clang::VarDecl *decl) {
724+
std::string str;
725+
{
726+
Buffer buf(*this);
727+
ConvertVarDecl(decl);
728+
str = std::move(buf).str();
729+
if (str.empty()) {
730+
return;
731+
}
732+
}
724733
StrCat("thread_local!");
725734
{
726735
PushParen paren(*this);
727-
ConvertVarDecl(decl);
736+
StrCat(str);
728737
}
729738
StrCat(token::kSemiColon);
730739
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ impl ByteRepr for Inner {
3232
}
3333
}
3434
}
35-
thread_local!();
3635
#[derive(Default)]
3736
pub struct Table {}
3837
impl Table {

0 commit comments

Comments
 (0)