Skip to content

[clang-format] Handle AttributeMacro before access modifiers #95503

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class LevelIndentTracker {
/// Update the indent state given that \p Line is going to be formatted
/// next.
void nextLine(const AnnotatedLine &Line) {
Offset = getIndentOffset(*Line.First);
const auto *Tok = Line.First;
if (Tok->is(TT_AttributeMacro) && Tok->Next)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of it but decided to not bother because it would be rare and looping through all AttributeMacro prefixes would still leave comments and function-like macros unsupported. See a better solution in #95634.

Tok = Tok->Next;
Offset = getIndentOffset(*Tok);
// Update the indent level cache size so that we can rely on it
// having the right size in adjustToUnmodifiedline.
if (Line.Level >= IndentForLevel.size())
Expand Down
15 changes: 15 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12912,6 +12912,15 @@ TEST_F(FormatTest, FormatsAccessModifiers) {
" int j;\n"
"};",
Style);
Style.AttributeMacros.push_back("FOO");
Style.AttributeMacros.push_back("BAR");
verifyFormat("struct foo {\n"
"FOO private:\n"
" int i;\n"
"BAR private:\n"
" int j;\n"
"};",
Style);

FormatStyle NoEmptyLines = getLLVMStyle();
NoEmptyLines.MaxEmptyLinesToKeep = 0;
Expand Down Expand Up @@ -26130,6 +26139,12 @@ TEST_F(FormatTest, IndentAccessModifiers) {
" int i;\n"
"};",
Style);
Style.AttributeMacros.push_back("FOO");
verifyFormat("class C {\n"
" FOO public:\n"
" int i;\n"
"};",
Style);
}

TEST_F(FormatTest, LimitlessStringsAndComments) {
Expand Down
Loading