Skip to content

Commit 3664b4e

Browse files
authored
[clang-format] Remove special handling of C++ access specifiers in C (#129983)
This effectively reverts d1aed48 because of #129426.
1 parent 95767a9 commit 3664b4e

File tree

3 files changed

+33
-110
lines changed

3 files changed

+33
-110
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,18 @@ class LevelIndentTracker {
116116
Style.isCSharp()) {
117117
return 0;
118118
}
119-
120-
auto IsAccessModifier = [&](const FormatToken &RootToken) {
121-
if (Line.Type == LT_AccessModifier || RootToken.isObjCAccessSpecifier())
122-
return true;
123-
124-
const auto *Next = RootToken.Next;
125-
126-
// Handle Qt signals.
127-
if (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
128-
Next && Next->is(tok::colon)) {
129-
return true;
130-
}
131-
132-
if (Next && Next->isOneOf(Keywords.kw_slots, Keywords.kw_qslots) &&
133-
Next->Next && Next->Next->is(tok::colon)) {
134-
return true;
135-
}
136-
137-
// Handle malformed access specifier e.g. 'private' without trailing ':'.
138-
return !Next && RootToken.isAccessSpecifier(false);
139-
};
140-
141-
if (IsAccessModifier(*Line.First)) {
119+
const auto &RootToken = *Line.First;
120+
if (Line.Type == LT_AccessModifier ||
121+
RootToken.isAccessSpecifier(/*ColonRequired=*/false) ||
122+
RootToken.isObjCAccessSpecifier() ||
123+
(RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
124+
RootToken.Next && RootToken.Next->is(tok::colon))) {
142125
// The AccessModifierOffset may be overridden by IndentAccessModifiers,
143126
// in which case we take a negative value of the IndentWidth to simulate
144127
// the upper indent level.
145128
return Style.IndentAccessModifiers ? -Style.IndentWidth
146129
: Style.AccessModifierOffset;
147130
}
148-
149131
return 0;
150132
}
151133

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,75 +3386,15 @@ void UnwrappedLineParser::parseSwitch(bool IsExpr) {
33863386
NestedTooDeep.pop_back();
33873387
}
33883388

3389-
// Operators that can follow a C variable.
3390-
static bool isCOperatorFollowingVar(tok::TokenKind Kind) {
3391-
switch (Kind) {
3392-
case tok::ampamp:
3393-
case tok::ampequal:
3394-
case tok::arrow:
3395-
case tok::caret:
3396-
case tok::caretequal:
3397-
case tok::comma:
3398-
case tok::ellipsis:
3399-
case tok::equal:
3400-
case tok::equalequal:
3401-
case tok::exclaim:
3402-
case tok::exclaimequal:
3403-
case tok::greater:
3404-
case tok::greaterequal:
3405-
case tok::greatergreater:
3406-
case tok::greatergreaterequal:
3407-
case tok::l_paren:
3408-
case tok::l_square:
3409-
case tok::less:
3410-
case tok::lessequal:
3411-
case tok::lessless:
3412-
case tok::lesslessequal:
3413-
case tok::minus:
3414-
case tok::minusequal:
3415-
case tok::minusminus:
3416-
case tok::percent:
3417-
case tok::percentequal:
3418-
case tok::period:
3419-
case tok::pipe:
3420-
case tok::pipeequal:
3421-
case tok::pipepipe:
3422-
case tok::plus:
3423-
case tok::plusequal:
3424-
case tok::plusplus:
3425-
case tok::question:
3426-
case tok::r_brace:
3427-
case tok::r_paren:
3428-
case tok::r_square:
3429-
case tok::semi:
3430-
case tok::slash:
3431-
case tok::slashequal:
3432-
case tok::star:
3433-
case tok::starequal:
3434-
return true;
3435-
default:
3436-
return false;
3437-
}
3438-
}
3439-
34403389
void UnwrappedLineParser::parseAccessSpecifier() {
3441-
FormatToken *AccessSpecifierCandidate = FormatTok;
34423390
nextToken();
34433391
// Understand Qt's slots.
34443392
if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
34453393
nextToken();
34463394
// Otherwise, we don't know what it is, and we'd better keep the next token.
3447-
if (FormatTok->is(tok::colon)) {
3395+
if (FormatTok->is(tok::colon))
34483396
nextToken();
3449-
addUnwrappedLine();
3450-
} else if (FormatTok->isNot(tok::coloncolon) &&
3451-
!isCOperatorFollowingVar(FormatTok->Tok.getKind())) {
3452-
// Not a variable name nor namespace name.
3453-
addUnwrappedLine();
3454-
} else if (AccessSpecifierCandidate) {
3455-
// Consider the access specifier to be a C identifier.
3456-
AccessSpecifierCandidate->Tok.setKind(tok::identifier);
3457-
}
3397+
addUnwrappedLine();
34583398
}
34593399

34603400
/// \brief Parses a requires, decides if it is a clause or an expression.

clang/unittests/Format/FormatTest.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,46 +3501,47 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
35013501
"label:\n"
35023502
" signals.baz();\n"
35033503
"}");
3504-
verifyFormat("private[1];");
3504+
3505+
const auto Style = getLLVMStyle(FormatStyle::LK_C);
3506+
verifyFormat("private[1];", Style);
35053507
verifyFormat("testArray[public] = 1;");
3506-
verifyFormat("public();");
3508+
verifyFormat("public();", Style);
35073509
verifyFormat("myFunc(public);");
35083510
verifyFormat("std::vector<int> testVec = {private};");
3509-
verifyFormat("private.p = 1;");
3511+
verifyFormat("private.p = 1;", Style);
35103512
verifyFormat("void function(private...) {};");
35113513
verifyFormat("if (private && public)");
3512-
verifyFormat("private &= true;");
3514+
verifyFormat("private &= true;", Style);
35133515
verifyFormat("int x = private * public;");
3514-
verifyFormat("public *= private;");
3516+
verifyFormat("public *= private;", Style);
35153517
verifyFormat("int x = public + private;");
3516-
verifyFormat("private++;");
3518+
verifyFormat("private++;", Style);
35173519
verifyFormat("++private;");
3518-
verifyFormat("public += private;");
3519-
verifyFormat("public = public - private;");
3520-
verifyFormat("public->foo();");
3521-
verifyFormat("private--;");
3520+
verifyFormat("public += private;", Style);
3521+
verifyFormat("public = public - private;", Style);
3522+
verifyFormat("public->foo();", Style);
3523+
verifyFormat("private--;", Style);
35223524
verifyFormat("--private;");
3523-
verifyFormat("public -= 1;");
3525+
verifyFormat("public -= 1;", Style);
35243526
verifyFormat("if (!private && !public)");
3525-
verifyFormat("public != private;");
3527+
verifyFormat("public != private;", Style);
35263528
verifyFormat("int x = public / private;");
3527-
verifyFormat("public /= 2;");
3528-
verifyFormat("public = public % 2;");
3529-
verifyFormat("public %= 2;");
3529+
verifyFormat("public /= 2;", Style);
3530+
verifyFormat("public = public % 2;", Style);
3531+
verifyFormat("public %= 2;", Style);
35303532
verifyFormat("if (public < private)");
3531-
verifyFormat("public << private;");
3532-
verifyFormat("public <<= private;");
3533+
verifyFormat("public << private;", Style);
3534+
verifyFormat("public <<= private;", Style);
35333535
verifyFormat("if (public > private)");
3534-
verifyFormat("public >> private;");
3535-
verifyFormat("public >>= private;");
3536-
verifyFormat("public ^ private;");
3537-
verifyFormat("public ^= private;");
3538-
verifyFormat("public | private;");
3539-
verifyFormat("public |= private;");
3536+
verifyFormat("public >> private;", Style);
3537+
verifyFormat("public >>= private;", Style);
3538+
verifyFormat("public ^ private;", Style);
3539+
verifyFormat("public ^= private;", Style);
3540+
verifyFormat("public | private;", Style);
3541+
verifyFormat("public |= private;", Style);
35403542
verifyFormat("auto x = private ? 1 : 2;");
35413543
verifyFormat("if (public == private)");
35423544
verifyFormat("void foo(public, private)");
3543-
verifyFormat("public::foo();");
35443545

35453546
verifyFormat("class A {\n"
35463547
"public:\n"

0 commit comments

Comments
 (0)