-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format] Remove special handling of C++ access specifiers in C #129983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This effectively reverts d1aed48 because of
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesThis effectively reverts d1aed48 because of Full diff: https://github.com/llvm/llvm-project/pull/129983.diff 3 Files Affected:
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 14e984529d640..000a5105ca407 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -116,36 +116,18 @@ class LevelIndentTracker {
Style.isCSharp()) {
return 0;
}
-
- auto IsAccessModifier = [&](const FormatToken &RootToken) {
- if (Line.Type == LT_AccessModifier || RootToken.isObjCAccessSpecifier())
- return true;
-
- const auto *Next = RootToken.Next;
-
- // Handle Qt signals.
- if (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
- Next && Next->is(tok::colon)) {
- return true;
- }
-
- if (Next && Next->isOneOf(Keywords.kw_slots, Keywords.kw_qslots) &&
- Next->Next && Next->Next->is(tok::colon)) {
- return true;
- }
-
- // Handle malformed access specifier e.g. 'private' without trailing ':'.
- return !Next && RootToken.isAccessSpecifier(false);
- };
-
- if (IsAccessModifier(*Line.First)) {
+ const auto &RootToken = *Line.First;
+ if (Line.Type == LT_AccessModifier ||
+ RootToken.isAccessSpecifier(/*ColonRequired=*/false) ||
+ RootToken.isObjCAccessSpecifier() ||
+ (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
+ RootToken.Next && RootToken.Next->is(tok::colon))) {
// The AccessModifierOffset may be overridden by IndentAccessModifiers,
// in which case we take a negative value of the IndentWidth to simulate
// the upper indent level.
return Style.IndentAccessModifiers ? -Style.IndentWidth
: Style.AccessModifierOffset;
}
-
return 0;
}
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 2da0432816df7..3714d4e22a28a 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3386,75 +3386,15 @@ void UnwrappedLineParser::parseSwitch(bool IsExpr) {
NestedTooDeep.pop_back();
}
-// Operators that can follow a C variable.
-static bool isCOperatorFollowingVar(tok::TokenKind Kind) {
- switch (Kind) {
- case tok::ampamp:
- case tok::ampequal:
- case tok::arrow:
- case tok::caret:
- case tok::caretequal:
- case tok::comma:
- case tok::ellipsis:
- case tok::equal:
- case tok::equalequal:
- case tok::exclaim:
- case tok::exclaimequal:
- case tok::greater:
- case tok::greaterequal:
- case tok::greatergreater:
- case tok::greatergreaterequal:
- case tok::l_paren:
- case tok::l_square:
- case tok::less:
- case tok::lessequal:
- case tok::lessless:
- case tok::lesslessequal:
- case tok::minus:
- case tok::minusequal:
- case tok::minusminus:
- case tok::percent:
- case tok::percentequal:
- case tok::period:
- case tok::pipe:
- case tok::pipeequal:
- case tok::pipepipe:
- case tok::plus:
- case tok::plusequal:
- case tok::plusplus:
- case tok::question:
- case tok::r_brace:
- case tok::r_paren:
- case tok::r_square:
- case tok::semi:
- case tok::slash:
- case tok::slashequal:
- case tok::star:
- case tok::starequal:
- return true;
- default:
- return false;
- }
-}
-
void UnwrappedLineParser::parseAccessSpecifier() {
- FormatToken *AccessSpecifierCandidate = FormatTok;
nextToken();
// Understand Qt's slots.
if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
nextToken();
// Otherwise, we don't know what it is, and we'd better keep the next token.
- if (FormatTok->is(tok::colon)) {
+ if (FormatTok->is(tok::colon))
nextToken();
- addUnwrappedLine();
- } else if (FormatTok->isNot(tok::coloncolon) &&
- !isCOperatorFollowingVar(FormatTok->Tok.getKind())) {
- // Not a variable name nor namespace name.
- addUnwrappedLine();
- } else if (AccessSpecifierCandidate) {
- // Consider the access specifier to be a C identifier.
- AccessSpecifierCandidate->Tok.setKind(tok::identifier);
- }
+ addUnwrappedLine();
}
/// \brief Parses a requires, decides if it is a clause or an expression.
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ae2eaf70de1c2..bd335f4b6a21b 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3501,46 +3501,47 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
"label:\n"
" signals.baz();\n"
"}");
- verifyFormat("private[1];");
+
+ const auto Style = getLLVMStyle(FormatStyle::LK_C);
+ verifyFormat("private[1];", Style);
verifyFormat("testArray[public] = 1;");
- verifyFormat("public();");
+ verifyFormat("public();", Style);
verifyFormat("myFunc(public);");
verifyFormat("std::vector<int> testVec = {private};");
- verifyFormat("private.p = 1;");
+ verifyFormat("private.p = 1;", Style);
verifyFormat("void function(private...) {};");
verifyFormat("if (private && public)");
- verifyFormat("private &= true;");
+ verifyFormat("private &= true;", Style);
verifyFormat("int x = private * public;");
- verifyFormat("public *= private;");
+ verifyFormat("public *= private;", Style);
verifyFormat("int x = public + private;");
- verifyFormat("private++;");
+ verifyFormat("private++;", Style);
verifyFormat("++private;");
- verifyFormat("public += private;");
- verifyFormat("public = public - private;");
- verifyFormat("public->foo();");
- verifyFormat("private--;");
+ verifyFormat("public += private;", Style);
+ verifyFormat("public = public - private;", Style);
+ verifyFormat("public->foo();", Style);
+ verifyFormat("private--;", Style);
verifyFormat("--private;");
- verifyFormat("public -= 1;");
+ verifyFormat("public -= 1;", Style);
verifyFormat("if (!private && !public)");
- verifyFormat("public != private;");
+ verifyFormat("public != private;", Style);
verifyFormat("int x = public / private;");
- verifyFormat("public /= 2;");
- verifyFormat("public = public % 2;");
- verifyFormat("public %= 2;");
+ verifyFormat("public /= 2;", Style);
+ verifyFormat("public = public % 2;", Style);
+ verifyFormat("public %= 2;", Style);
verifyFormat("if (public < private)");
- verifyFormat("public << private;");
- verifyFormat("public <<= private;");
+ verifyFormat("public << private;", Style);
+ verifyFormat("public <<= private;", Style);
verifyFormat("if (public > private)");
- verifyFormat("public >> private;");
- verifyFormat("public >>= private;");
- verifyFormat("public ^ private;");
- verifyFormat("public ^= private;");
- verifyFormat("public | private;");
- verifyFormat("public |= private;");
+ verifyFormat("public >> private;", Style);
+ verifyFormat("public >>= private;", Style);
+ verifyFormat("public ^ private;", Style);
+ verifyFormat("public ^= private;", Style);
+ verifyFormat("public | private;", Style);
+ verifyFormat("public |= private;", Style);
verifyFormat("auto x = private ? 1 : 2;");
verifyFormat("if (public == private)");
verifyFormat("void foo(public, private)");
- verifyFormat("public::foo();");
verifyFormat("class A {\n"
"public:\n"
|
HazardyKnusperkeks
approved these changes
Mar 6, 2025
mydeveloperday
approved these changes
Mar 6, 2025
jph-13
pushed a commit
to jph-13/llvm-project
that referenced
this pull request
Mar 21, 2025
…lvm#129983) This effectively reverts d1aed48 because of llvm#129426.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This effectively reverts d1aed48 because of
#129426.