Skip to content

Commit ac8caa8

Browse files
committed
Less stray semis in Cfront mode.
1 parent 8bba69e commit ac8caa8

30 files changed

Lines changed: 64 additions & 100 deletions

CodeGenerator.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ void CodeGenerator::StartLifetimeScope()
11231123

11241124
void CodeGenerator::EndLifetimeScope()
11251125
{
1126-
mLifeTimeTracker.EndScope(mOutputFormatHelper, false);
1126+
mSkipSemi = mLifeTimeTracker.EndScope(mOutputFormatHelper, false) or mSkipSemi;
11271127
}
11281128
//-----------------------------------------------------------------------------
11291129

@@ -2292,10 +2292,13 @@ void CodeGenerator::InsertArg(const CompoundStmt* stmt)
22922292

22932293
if(requiresImplicitReturnZero) {
22942294
InsertArg(Return(Int32(0)));
2295-
InsertArg(mkNullStmt());
2295+
2296+
if(not mSkipSemi) {
2297+
InsertArg(mkNullStmt());
2298+
}
22962299
}
22972300

2298-
mLifeTimeTracker.EndScope(mOutputFormatHelper, mLastStmt and isa<ReturnStmt>(mLastStmt));
2301+
mSkipSemi = mLifeTimeTracker.EndScope(mOutputFormatHelper, mLastStmt and isa<ReturnStmt>(mLastStmt));
22992302

23002303
mOutputFormatHelper.CloseScope(OutputFormatHelper::NoNewLineBefore::Yes);
23012304
}
@@ -2327,9 +2330,11 @@ void CodeGenerator::HandleCompoundStmt(const CompoundStmt* stmt)
23272330
SwitchStmt,
23282331
CXXTryStmt,
23292332
CppInsightsCommentStmt>(item) and
2330-
InsertSemi() and not skipSemiForLambda) {
2333+
InsertSemi() and not skipSemiForLambda and not mSkipSemi) {
23312334
mOutputFormatHelper.AppendSemiNewLine();
23322335
}
2336+
2337+
mSkipSemi = false;
23332338
}
23342339
}
23352340
//-----------------------------------------------------------------------------
@@ -2853,7 +2858,7 @@ void CodeGenerator::InsertArg(const ExprWithCleanups* stmt)
28532858
mOutputFormatHelper.AppendSemiNewLine();
28542859
}
28552860

2856-
mLifeTimeTracker.EndScope(mOutputFormatHelper, false);
2861+
mSkipSemi = mLifeTimeTracker.EndScope(mOutputFormatHelper, false);
28572862
}
28582863
//-----------------------------------------------------------------------------
28592864

@@ -4134,7 +4139,7 @@ void CodeGenerator::InsertArg(const ReturnStmt* stmt)
41344139
}
41354140
}
41364141

4137-
mLifeTimeTracker.Return(mOutputFormatHelper);
4142+
mSkipSemi = mLifeTimeTracker.Return(mOutputFormatHelper);
41384143

41394144
// the InsertArg above changes the start
41404145
mLastStmt = stmt;
@@ -4146,6 +4151,7 @@ void CodeGenerator::InsertArg(const ReturnStmt* stmt)
41464151
void CodeGenerator::InsertArg(const NullStmt* /*stmt*/)
41474152
{
41484153
mOutputFormatHelper.AppendSemiNewLine();
4154+
mSkipSemi = true;
41494155
}
41504156
//-----------------------------------------------------------------------------
41514157

CodeGenerator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class LifetimeTracker
7575

7676
void removeTop();
7777
void StartScope(bool funcStart);
78-
void Return(OutputFormatHelper& ofm);
79-
void EndScope(OutputFormatHelper& ofm, bool clear);
78+
bool Return(OutputFormatHelper& ofm);
79+
bool EndScope(OutputFormatHelper& ofm, bool clear);
8080
};
8181

8282
/// \brief More or less the heart of C++ Insights.
@@ -447,6 +447,7 @@ class CodeGenerator
447447
OutputFormatHelper* mOutputFormatHelperOutside{
448448
nullptr}; //!< Helper output buffer for std::initializer_list expansion.
449449
bool mRequiresImplicitReturnZero{}; //!< Track whether this is a function with an imlpicit return 0.
450+
bool mSkipSemi{};
450451
ProcessingPrimaryTemplate mProcessingPrimaryTemplate{};
451452
};
452453
//-----------------------------------------------------------------------------

InsightsUtility.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
}
2222
//-----------------------------------------------------------------------------
2323

24+
#define RETURN_FALSE_IF(cond) \
25+
if(cond) { \
26+
return false; \
27+
}
28+
//-----------------------------------------------------------------------------
29+
2430
using void_func_ref = llvm::function_ref<void()>;
2531
//-----------------------------------------------------------------------------
2632

LifetimeTracker.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,36 +62,34 @@ void LifetimeTracker::InsertDtorCall(const VarDecl* vd, OutputFormatHelper& ofm)
6262
return;
6363
}
6464

65-
auto* dtorDecl = type->getAsCXXRecordDecl()->getDestructor();
66-
auto* ic = CastLToRValue(vd);
65+
auto* dtorDecl = type->getAsCXXRecordDecl()->getDestructor();
66+
auto* ic = CastLToRValue(vd);
67+
CodeGeneratorVariant cg{ofm};
68+
69+
auto insertDtor = [&](Expr* member) {
70+
auto* mem = AccessMember(member, dtorDecl, vd->getType()->isPointerType());
71+
cg->InsertArg(CallMemberFun(mem, dtorDecl->getType()));
72+
ofm.AppendSemiNewLine();
73+
};
6774

6875
if(const auto* ar = dyn_cast_or_null<ConstantArrayType>(vd->getType()); ar and not GetInsightsOptions().UseShow2C) {
6976
// not nice but call the destructor for each array element
7077
for(const auto& i : NumberIterator{GetSize(ar)}) {
71-
auto* mem = AccessMember(ArraySubscript(ic, i, type), dtorDecl, vd->getType()->isPointerType());
72-
auto* callDtor = CallMemberFun(mem, dtorDecl->getType());
73-
74-
CodeGeneratorVariant cg{ofm};
75-
cg->InsertArg(callDtor);
76-
ofm.AppendSemiNewLine();
78+
insertDtor(ArraySubscript(ic, i, type));
7779
}
7880

7981
return;
8082
}
8183

82-
auto* mem = AccessMember(ic, dtorDecl, vd->getType()->isPointerType());
83-
auto* callDtor = CallMemberFun(mem, dtorDecl->getType());
84-
85-
CodeGeneratorVariant cg{ofm};
86-
cg->InsertArg(callDtor);
87-
88-
ofm.AppendSemiNewLine();
84+
insertDtor(ic);
8985
}
9086
//-----------------------------------------------------------------------------
9187

92-
void LifetimeTracker::Return(OutputFormatHelper& ofm)
88+
bool LifetimeTracker::Return(OutputFormatHelper& ofm)
9389
{
94-
RETURN_IF(not GetInsightsOptions().ShowLifetime or objects.empty())
90+
RETURN_FALSE_IF(not GetInsightsOptions().ShowLifetime or objects.empty())
91+
92+
bool ret{};
9593

9694
for(OnceTrue needsSemi{}; auto& e : llvm::reverse(objects)) {
9795
if(LifetimeEntry::FuncStart::Yes == e.funcStart) {
@@ -108,7 +106,10 @@ void LifetimeTracker::Return(OutputFormatHelper& ofm)
108106
}
109107

110108
InsertDtorCall(e.item, ofm);
109+
ret = true;
111110
}
111+
112+
return ret;
112113
}
113114
//-----------------------------------------------------------------------------
114115

@@ -123,9 +124,11 @@ void LifetimeTracker::removeTop()
123124
}
124125
//-----------------------------------------------------------------------------
125126

126-
void LifetimeTracker::EndScope(OutputFormatHelper& ofm, bool coveredByReturn)
127+
bool LifetimeTracker::EndScope(OutputFormatHelper& ofm, bool coveredByReturn)
127128
{
128-
RETURN_IF(not GetInsightsOptions().ShowLifetime or objects.empty())
129+
RETURN_FALSE_IF(not GetInsightsOptions().ShowLifetime or objects.empty())
130+
131+
bool ret{};
129132

130133
if(not coveredByReturn) {
131134
for(auto& e : llvm::reverse(objects)) {
@@ -138,10 +141,13 @@ void LifetimeTracker::EndScope(OutputFormatHelper& ofm, bool coveredByReturn)
138141
}
139142

140143
InsertDtorCall(e.item, ofm);
144+
ret = true;
141145
}
142146
}
143147

144148
removeTop();
149+
150+
return ret;
145151
}
146152
//-----------------------------------------------------------------------------
147153

tests/EduCfrontOverloadTest.expect

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ int __main(void)
4545
const int __temporary21_9 = 2;
4646
Fun_intr(&__temporary21_9);
4747
/* __temporary21_9 // lifetime ends here */
48-
;
4948
Fun_double(3.1400000000000001);
5049
Fun_intp(p);
5150
Fun_intpc(cp);
@@ -56,15 +55,13 @@ int __main(void)
5655
int __temporary32_9 = 4;
5756
Run_intR(&__temporary32_9);
5857
/* __temporary32_9 // lifetime ends here */
59-
;
6058
int i = 0;
6159
Run_intr(&i);
6260
Bun(4);
6361
return 0;
6462
/* i // lifetime ends here */
6563
/* ur // lifetime ends here */
6664
/* r // lifetime ends here */
67-
;
6865
}
6966

7067
int main(void)
@@ -74,7 +71,6 @@ int main(void)
7471
__cxa_atexit();
7572
return ret;
7673
/* ret // lifetime ends here */
77-
;
7874
}
7975

8076
void __cxa_start(void)

tests/EduCfrontReferencesTest.expect

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ int __main(void)
3535
/* a // lifetime ends here */
3636
/* f // lifetime ends here */
3737
/* i // lifetime ends here */
38-
;
3938
}
4039

4140
int main(void)
@@ -45,7 +44,6 @@ int main(void)
4544
__cxa_atexit();
4645
return ret;
4746
/* ret // lifetime ends here */
48-
;
4947
}
5048

5149
void __cxa_start(void)

tests/EduCfrontTest.expect

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ int __main(void)
8686
Destructor_Apple(&c);
8787
Destructor_Apple(&b);
8888
Destructor_Apple(&a);
89-
;
9089
}
9190

9291
int main(void)
@@ -96,7 +95,6 @@ int main(void)
9695
__cxa_atexit();
9796
return ret;
9897
/* ret // lifetime ends here */
99-
;
10098
}
10199

102100
void __cxa_start(void)

tests/EduCfrontTest11.expect

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ int main(void)
2424
__cxa_atexit();
2525
return ret;
2626
/* ret // lifetime ends here */
27-
;
2827
}
2928

3029
void __cxa_start(void)

tests/EduCfrontTest12.expect

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ int main(void)
3838
__cxa_atexit();
3939
return ret;
4040
/* ret // lifetime ends here */
41-
;
4241
}
4342

4443
void __cxa_start(void)

tests/EduCfrontTest13.expect

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ int __main(void)
9191
Destructor_E(&e);
9292
/* dvec // lifetime ends here */
9393
/* avec // lifetime ends here */
94-
;
9594
}
9695

9796
int main(void)
@@ -101,7 +100,6 @@ int main(void)
101100
__cxa_atexit();
102101
return ret;
103102
/* ret // lifetime ends here */
104-
;
105103
}
106104

107105
void __cxa_start(void)

0 commit comments

Comments
 (0)