Skip to content

Commit bbaff70

Browse files
Merge pull request #717 from andreasfertig/fixIssue607
Fixed #607: Added support for initialization of variables with static storage duration in Cpp2C mode.
2 parents d96a283 + 6c767c9 commit bbaff70

File tree

3 files changed

+113
-5
lines changed

3 files changed

+113
-5
lines changed

CodeGenerator.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,13 +1386,24 @@ void CodeGenerator::InsertArg(const VarDecl* stmt)
13861386

13871387
// if(not ctorExpr->getConstructor()->isTrivial()) {
13881388
if(stmt->hasGlobalStorage()) {
1389-
// push to __cxx_global_var_init
1390-
auto* callExpr = CallConstructor(
1391-
stmt->getType(), stmt, ArgsToExprVector(ctorExpr), DoCast::No, AsReference::Yes);
1389+
if(ctorExpr->getConstructor()->isDefaultConstructor() and
1390+
ctorExpr->getConstructor()->getParent()->hasTrivialDefaultConstructor()) {
13921391

1393-
PushGlobalVariable(callExpr);
1392+
auto* callMemset = Call("memset"sv, {Ref(stmt), Int32(0), Sizeof(stmt->getType())});
13941393

1395-
PushGlobalVariableDtor(CallDestructor(stmt));
1394+
EnableGlobalInsert(GlobalInserts::FuncMemset);
1395+
PushGlobalVariable(callMemset);
1396+
1397+
} else {
1398+
1399+
// push to __cxx_global_var_init
1400+
auto* callExpr = CallConstructor(
1401+
stmt->getType(), stmt, ArgsToExprVector(ctorExpr), DoCast::No, AsReference::Yes);
1402+
1403+
PushGlobalVariable(callExpr);
1404+
1405+
PushGlobalVariableDtor(CallDestructor(stmt));
1406+
}
13961407

13971408
} else {
13981409
mOutputFormatHelper.AppendSemiNewLine();
@@ -1456,6 +1467,11 @@ void CodeGenerator::InsertArg(const VarDecl* stmt)
14561467
}
14571468
}
14581469
}
1470+
} else if(GetInsightsOptions().UseShow2C and stmt->hasGlobalStorage() and
1471+
(stmt->getStorageDuration() == SD_Static) and
1472+
(stmt->getDeclContext()->isNamespace() or
1473+
(is{stmt->getStorageClass()}.any_of(SC_Static, SC_Extern)))) {
1474+
PushGlobalVariable(Assign(stmt, Int32(0)));
14591475
}
14601476

14611477
if(stmt->isNRVOVariable()) {

tests/Issue607.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// cmdlineinsights:-edu-show-cfront
2+
namespace X
3+
{
4+
int initInNamespace;
5+
}
6+
7+
extern int initDueToExtern;
8+
9+
int unInitGlobal;
10+
int initGlobalWitValue = 3;
11+
12+
static int initGlobalStatic;
13+
14+
struct A{};
15+
16+
A initGlobalCtor;
17+
18+
namespace
19+
{
20+
int initInAnonNamespace;
21+
A initCtorInAnonNamespace;
22+
}
23+
24+
int main()
25+
{
26+
++X::initInNamespace;
27+
}

tests/Issue607.expect

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*************************************************************************************
2+
* NOTE: This an educational hand-rolled transformation. Things can be incorrect or *
3+
* buggy. *
4+
*************************************************************************************/
5+
void __cxa_start(void);
6+
void __cxa_atexit(void);
7+
extern "C" void* memset(void*, int, unsigned int);
8+
9+
namespace X
10+
{
11+
int initInNamespace;
12+
13+
}
14+
15+
extern int initDueToExtern;
16+
17+
int unInitGlobal;
18+
int initGlobalWitValue = 3;
19+
20+
static int initGlobalStatic;
21+
22+
typedef struct A
23+
{
24+
char __dummy;
25+
} A;
26+
27+
28+
A initGlobalCtor;
29+
30+
namespace
31+
{
32+
int initInAnonNamespace;
33+
A initCtorInAnonNamespace;
34+
35+
}
36+
37+
int __main(void)
38+
{
39+
++X::initInNamespace;
40+
return 0;
41+
}
42+
43+
int main(void)
44+
{
45+
__cxa_start();
46+
int ret = __main();
47+
__cxa_atexit();
48+
return ret;
49+
/* ret // lifetime ends here */
50+
}
51+
52+
void __cxa_start(void)
53+
{
54+
X::initInNamespace = 0;
55+
initDueToExtern = 0;
56+
initGlobalStatic = 0;
57+
memset(&initGlobalCtor, 0, sizeof(A));
58+
::initInAnonNamespace = 0;
59+
memset(&::initCtorInAnonNamespace, 0, sizeof(A));
60+
}
61+
62+
void __cxa_atexit(void)
63+
{
64+
}
65+

0 commit comments

Comments
 (0)