From aa3d406faf4265c526cb4d9923e849e8264f5204 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Mon, 10 Oct 2011 14:37:41 +0200 Subject: [PATCH] C++ indenter: Accept attributes in access specifiers. Change-Id: Ie85b8264c9579e8c8312e30018280fb11f95edda Reviewed-on: http://codereview.qt-project.org/6328 Sanity-Review: Qt Sanity Bot Reviewed-by: Erik Verbruggen Sanity-Review: Erik Verbruggen --- src/plugins/cpptools/cppcodeformatter.cpp | 18 +++++++++++++++++- src/plugins/cpptools/cppcodeformatter.h | 2 ++ .../codeformatter/tst_codeformatter.cpp | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp index 203e73057eb..6ac322cad6d 100644 --- a/src/plugins/cpptools/cppcodeformatter.cpp +++ b/src/plugins/cpptools/cppcodeformatter.cpp @@ -137,6 +137,11 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) case T_RBRACE: leave(); continue; // always nested in class_start } break; + case access_specifier_start: + switch (kind) { + case T_COLON: leave(); break; + } break; + case enum_start: switch (kind) { case T_SEMICOLON: leave(); break; @@ -817,6 +822,16 @@ bool CodeFormatter::tryDeclaration() enter(using_start); return true; + case T_PUBLIC: + case T_PRIVATE: + case T_PROTECTED: + case T_Q_SIGNALS: + if (m_currentState.top().type == class_open) { + enter(access_specifier_start); + return true; + } + return false; + default: return false; } @@ -1473,7 +1488,8 @@ void QtStyleCodeFormatter::adjustIndent(const QList &tokens, i case T_Q_SIGNALS: if (m_styleSettings.indentDeclarationsRelativeToAccessSpecifiers && topState.type == class_open) { - if (tokenAt(1).is(T_COLON) || tokenAt(2).is(T_COLON)) { + if (tokenAt(1).is(T_COLON) || tokenAt(2).is(T_COLON) + || (tokenAt(tokenCount() - 1).is(T_COLON) && tokenAt(1).is(T___ATTRIBUTE__))) { *indentDepth = topState.savedIndentDepth; if (m_styleSettings.indentAccessSpecifiers) *indentDepth += m_tabSettings.m_indentSize; diff --git a/src/plugins/cpptools/cppcodeformatter.h b/src/plugins/cpptools/cppcodeformatter.h index dae19a2e249..38ae9cf45bf 100644 --- a/src/plugins/cpptools/cppcodeformatter.h +++ b/src/plugins/cpptools/cppcodeformatter.h @@ -121,6 +121,8 @@ public: // must be public to make Q_GADGET introspection work class_start, // after the 'class' token class_open, // Brace that opens a class definition. + access_specifier_start, // after 'private', 'protected' etc. + member_init_open, // After ':' that starts a member initialization list. member_init, // At the start and after every ',' in member_init_open member_init_paren_open, // After '(' in member_init. diff --git a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp index 1b882e16145..c142f7cf3d0 100644 --- a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp +++ b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp @@ -125,6 +125,7 @@ private Q_SLOTS: void caseBody6(); void blockBraces1(); void functionDefaultArgument(); + void attributeInAccessSpecifier(); }; struct Line { @@ -1990,6 +1991,20 @@ void tst_CodeFormatter::functionDefaultArgument() checkIndent(data); } +void tst_CodeFormatter::attributeInAccessSpecifier() +{ + QList data; + data << Line("class C {") + << Line("public __attribute__((annotate(\"foo\"))):") + << Line(" int a;") + << Line("private __attribute__((annotate(\"foo\"))):") + << Line(" int a;") + << Line("};") + << Line("int b;") + ; + checkIndent(data); +} + QTEST_APPLESS_MAIN(tst_CodeFormatter) #include "tst_codeformatter.moc"