[clang] [clang-format] Handle AttributeMacro before access modifiers (PR #95503)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 13 21:28:20 PDT 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/95503
Closes #95094.
>From 6684ed759ce118bb28e9da22be51bcfece2a1909 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 13 Jun 2024 21:25:08 -0700
Subject: [PATCH] [clang-format] Handle AttributeMacro before access modifiers
Closes #95094.
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 5 ++++-
clang/unittests/Format/FormatTest.cpp | 15 +++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 4d53361aaf333..80ab9827b050c 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -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)
+ 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())
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index fb57333858529..2ca85c7b70e65 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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;
@@ -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) {
More information about the cfe-commits
mailing list