@@ -2261,27 +2261,36 @@ class SemiRemover : public TokenAnalyzer {
2261
2261
FormatTokenLexer &Tokens) override {
2262
2262
AffectedRangeMgr.computeAffectedLines (AnnotatedLines);
2263
2263
tooling::Replacements Result;
2264
- removeSemi (AnnotatedLines, Result);
2264
+ removeSemi (Annotator, AnnotatedLines, Result);
2265
2265
return {Result, 0 };
2266
2266
}
2267
2267
2268
2268
private:
2269
- void removeSemi (SmallVectorImpl<AnnotatedLine *> &Lines,
2269
+ void removeSemi (TokenAnnotator &Annotator,
2270
+ SmallVectorImpl<AnnotatedLine *> &Lines,
2270
2271
tooling::Replacements &Result) {
2272
+ auto PrecededByFunctionRBrace = [](const FormatToken &Tok) {
2273
+ const auto *Prev = Tok.Previous ;
2274
+ if (!Prev || Prev->isNot (tok::r_brace))
2275
+ return false ;
2276
+ const auto *LBrace = Prev->MatchingParen ;
2277
+ return LBrace && LBrace->is (TT_FunctionLBrace);
2278
+ };
2271
2279
const auto &SourceMgr = Env.getSourceManager ();
2272
2280
const auto End = Lines.end ();
2273
2281
for (auto I = Lines.begin (); I != End; ++I) {
2274
2282
const auto Line = *I;
2275
- removeSemi (Line->Children , Result);
2283
+ removeSemi (Annotator, Line->Children , Result);
2276
2284
if (!Line->Affected )
2277
2285
continue ;
2286
+ Annotator.calculateFormattingInformation (*Line);
2278
2287
const auto NextLine = I + 1 == End ? nullptr : I[1 ];
2279
2288
for (auto Token = Line->First ; Token && !Token->Finalized ;
2280
2289
Token = Token->Next ) {
2281
- if (!Token->Optional )
2282
- continue ;
2283
- if (Token->isNot (tok::semi))
2290
+ if (Token->isNot (tok::semi) ||
2291
+ (!Token->Optional && !PrecededByFunctionRBrace (*Token))) {
2284
2292
continue ;
2293
+ }
2285
2294
auto Next = Token->Next ;
2286
2295
assert (Next || Token == Line->Last );
2287
2296
if (!Next && NextLine)
@@ -3677,7 +3686,7 @@ reformat(const FormatStyle &Style, StringRef Code,
3677
3686
FormatStyle S = Expanded;
3678
3687
S.RemoveSemicolon = true ;
3679
3688
Passes.emplace_back ([&, S = std::move (S)](const Environment &Env) {
3680
- return SemiRemover (Env, S).process (/* SkipAnnotation= */ true );
3689
+ return SemiRemover (Env, S).process ();
3681
3690
});
3682
3691
}
3683
3692
0 commit comments