Skip to content

Commit 9e16e89

Browse files
committed
Add a warning and refactor the code that removes/replaces a token
1 parent 3b35212 commit 9e16e89

File tree

3 files changed

+39
-57
lines changed

3 files changed

+39
-57
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3982,6 +3982,13 @@ the configuration (without a prefix: ``Auto``).
39823982
Insert a comma (if missing) or remove the comma at the end of an ``enum``
39833983
enumerator list.
39843984

3985+
.. warning::
3986+
3987+
Setting this option to any value other than ``Leave`` could lead to
3988+
incorrect code formatting due to clang-format's lack of complete semantic
3989+
information. As such, extra care should be taken to review code changes
3990+
made by this option.
3991+
39853992
Possible values:
39863993

39873994
* ``ETC_Leave`` (in configuration: ``Leave``)

clang/include/clang/Format/Format.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,6 +2728,12 @@ struct FormatStyle {
27282728

27292729
/// Insert a comma (if missing) or remove the comma at the end of an ``enum``
27302730
/// enumerator list.
2731+
/// \warning
2732+
/// Setting this option to any value other than ``Leave`` could lead to
2733+
/// incorrect code formatting due to clang-format's lack of complete semantic
2734+
/// information. As such, extra care should be taken to review code changes
2735+
/// made by this option.
2736+
/// \endwarning
27312737
/// \version 21
27322738
EnumTrailingCommaStyle EnumTrailingComma;
27332739

clang/lib/Format/Format.cpp

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,21 @@ FormatStyle::GetLanguageStyle(FormatStyle::LanguageKind Language) const {
22142214

22152215
namespace {
22162216

2217+
void replaceToken(const FormatToken &Token, FormatToken *Next,
2218+
const SourceManager &SourceMgr, tooling::Replacements &Result,
2219+
const char *Text = "") {
2220+
const auto &Tok = Token.Tok;
2221+
SourceLocation Start;
2222+
if (Next && Next->NewlinesBefore == 0 && Next->isNot(tok::eof)) {
2223+
Start = Tok.getLocation();
2224+
Next->WhitespaceRange = Token.WhitespaceRange;
2225+
} else {
2226+
Start = Token.WhitespaceRange.getBegin();
2227+
}
2228+
const auto &Range = CharSourceRange::getCharRange(Start, Tok.getEndLoc());
2229+
cantFail(Result.add(tooling::Replacement(SourceMgr, Range, Text)));
2230+
}
2231+
22172232
class ParensRemover : public TokenAnalyzer {
22182233
public:
22192234
ParensRemover(const Environment &Env, const FormatStyle &Style)
@@ -2240,20 +2255,8 @@ class ParensRemover : public TokenAnalyzer {
22402255
continue;
22412256
for (const auto *Token = Line->First; Token && !Token->Finalized;
22422257
Token = Token->Next) {
2243-
if (!Token->Optional || !Token->isOneOf(tok::l_paren, tok::r_paren))
2244-
continue;
2245-
auto *Next = Token->Next;
2246-
assert(Next && Next->isNot(tok::eof));
2247-
SourceLocation Start;
2248-
if (Next->NewlinesBefore == 0) {
2249-
Start = Token->Tok.getLocation();
2250-
Next->WhitespaceRange = Token->WhitespaceRange;
2251-
} else {
2252-
Start = Token->WhitespaceRange.getBegin();
2253-
}
2254-
const auto &Range =
2255-
CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
2256-
cantFail(Result.add(tooling::Replacement(SourceMgr, Range, " ")));
2258+
if (Token->Optional && Token->isOneOf(tok::l_paren, tok::r_paren))
2259+
replaceToken(*Token, Token->Next, SourceMgr, Result, " ");
22572260
}
22582261
}
22592262
}
@@ -2342,24 +2345,13 @@ class BracesRemover : public TokenAnalyzer {
23422345
const auto *NextLine = I + 1 == End ? nullptr : I[1];
23432346
for (const auto *Token = Line->First; Token && !Token->Finalized;
23442347
Token = Token->Next) {
2345-
if (!Token->Optional)
2346-
continue;
2347-
if (!Token->isOneOf(tok::l_brace, tok::r_brace))
2348+
if (!Token->Optional || !Token->isOneOf(tok::l_brace, tok::r_brace))
23482349
continue;
23492350
auto *Next = Token->Next;
23502351
assert(Next || Token == Line->Last);
23512352
if (!Next && NextLine)
23522353
Next = NextLine->First;
2353-
SourceLocation Start;
2354-
if (Next && Next->NewlinesBefore == 0 && Next->isNot(tok::eof)) {
2355-
Start = Token->Tok.getLocation();
2356-
Next->WhitespaceRange = Token->WhitespaceRange;
2357-
} else {
2358-
Start = Token->WhitespaceRange.getBegin();
2359-
}
2360-
const auto &Range =
2361-
CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
2362-
cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
2354+
replaceToken(*Token, Next, SourceMgr, Result);
23632355
}
23642356
}
23652357
}
@@ -2411,16 +2403,7 @@ class SemiRemover : public TokenAnalyzer {
24112403
assert(Next || Token == Line->Last);
24122404
if (!Next && NextLine)
24132405
Next = NextLine->First;
2414-
SourceLocation Start;
2415-
if (Next && Next->NewlinesBefore == 0 && Next->isNot(tok::eof)) {
2416-
Start = Token->Tok.getLocation();
2417-
Next->WhitespaceRange = Token->WhitespaceRange;
2418-
} else {
2419-
Start = Token->WhitespaceRange.getBegin();
2420-
}
2421-
const auto &Range =
2422-
CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
2423-
cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
2406+
replaceToken(*Token, Next, SourceMgr, Result);
24242407
}
24252408
}
24262409
}
@@ -2458,26 +2441,12 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
24582441
assert(BeforeRBrace);
24592442
if (BeforeRBrace->is(TT_EnumLBrace)) // Empty braces.
24602443
continue;
2461-
if (Style.EnumTrailingComma == FormatStyle::ETC_Insert) {
2462-
if (BeforeRBrace->isNot(tok::comma)) {
2463-
cantFail(Result.add(tooling::Replacement(
2464-
SourceMgr, BeforeRBrace->Tok.getEndLoc(), 0, ",")));
2465-
}
2466-
} else {
2467-
assert(Style.EnumTrailingComma == FormatStyle::ETC_Remove);
2468-
if (BeforeRBrace->isNot(tok::comma))
2469-
continue;
2470-
auto *Next = BeforeRBrace->Next;
2471-
SourceLocation Start;
2472-
if (Next->NewlinesBefore == 0) {
2473-
Start = BeforeRBrace->Tok.getLocation();
2474-
Next->WhitespaceRange = BeforeRBrace->WhitespaceRange;
2475-
} else {
2476-
Start = BeforeRBrace->WhitespaceRange.getBegin();
2477-
}
2478-
const auto &Range = CharSourceRange::getCharRange(
2479-
Start, BeforeRBrace->Tok.getEndLoc());
2480-
cantFail(Result.add(tooling::Replacement(SourceMgr, Range, " ")));
2444+
if (BeforeRBrace->is(tok::comma)) {
2445+
if (Style.EnumTrailingComma == FormatStyle::ETC_Remove)
2446+
replaceToken(*BeforeRBrace, BeforeRBrace->Next, SourceMgr, Result);
2447+
} else if (Style.EnumTrailingComma == FormatStyle::ETC_Insert) {
2448+
cantFail(Result.add(tooling::Replacement(
2449+
SourceMgr, BeforeRBrace->Tok.getEndLoc(), 0, ",")));
24812450
}
24822451
}
24832452
}

0 commit comments

Comments
 (0)