From 1072d573f5552aed32f434585a69393b77e526ed Mon Sep 17 00:00:00 2001 From: Weng Xuetian Date: Tue, 4 Mar 2025 07:38:59 -0800 Subject: [PATCH] Add github actions --- .clang-format | 58 ++++++++++++++++++++++++++++++ .github/workflows/check.yml | 46 ++++++++++++++++++++++++ src/main.cpp | 71 +++++++++++++++++++------------------ src/parser.cpp | 66 ++++++++++++++++++++-------------- src/parser.h | 15 ++++---- src/protocol.cpp | 14 ++++---- src/protocol.h | 12 +++---- src/utils.h | 2 +- 8 files changed, 199 insertions(+), 85 deletions(-) create mode 100644 .clang-format create mode 100644 .github/workflows/check.yml diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..f93f79b --- /dev/null +++ b/.clang-format @@ -0,0 +1,58 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -4 +ConstructorInitializerIndentWidth: 4 +AlignEscapedNewlinesLeft: false +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AlwaysBreakTemplateDeclarations: true +AlwaysBreakBeforeMultilineStrings: false +BreakBeforeBinaryOperators: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BinPackParameters: true +ColumnLimit: 80 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +DerivePointerAlignment: false +ExperimentalAutoDetectBinPacking: false +IndentCaseLabels: false +IndentWrappedFunctionNames: false +IndentFunctionDeclarationAfterType: false +MaxEmptyLinesToKeep: 1 +KeepEmptyLinesAtTheStartOfBlocks: true +NamespaceIndentation: None +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakString: 1000 +PenaltyBreakFirstLessLess: 120 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +SpacesBeforeTrailingComments: 1 +Cpp11BracedListStyle: true +Standard: Cpp11 +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +BreakBeforeBraces: Attach +SpacesInParentheses: false +SpacesInAngles: false +SpaceInEmptyParentheses: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: true +SpaceBeforeAssignmentOperators: true +ContinuationIndentWidth: 4 +CommentPragmas: '^ IWYU pragma:' +ForEachMacros: [ Q_FOREACH, BOOST_FOREACH ] +SpaceBeforeParens: ControlStatements +DisableFormat: false +SortIncludes: true +... + diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..7e68ef1 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,46 @@ +name: CI +on: + push: + branches: + - main + pull_request: + branches: + - main +jobs: + clang-format: + name: Check clang-format + runs-on: ubuntu-latest + container: archlinux:latest + steps: + - name: Install dependencies + run: | + pacman -Syu --noconfirm git clang diffutils + git config --global --add safe.directory $GITHUB_WORKSPACE + - uses: actions/checkout@v4 + - uses: fcitx/github-actions@clang-format + check: + name: Build and test + needs: clang-format + runs-on: ubuntu-latest + container: archlinux:latest + strategy: + fail-fast: false + matrix: + compiler: [gcc, clang] + include: + - compiler: gcc + cxx_compiler: g++ + - compiler: clang + cxx_compiler: clang++ + env: + CC: ${{ matrix.compiler }} + CXX: ${{ matrix.cxx_compiler }} + steps: + - name: Install dependencies + run: | + pacman -Syu --noconfirm base-devel clang cmake ninja extra-cmake-modules qt6-base ktexttemplate + - uses: actions/checkout@v4 + - name: Build and Install fcitx5-configtool + uses: fcitx/github-actions@cmake + with: + path: . diff --git a/src/main.cpp b/src/main.cpp index a567ea9..6862ed6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,62 +5,63 @@ * */ -#include +#include "protocol.h" #include #include #include #include #include #include +#include #include #include -#include "protocol.h" Q_LOGGING_CATEGORY(generator, "generator"); QtMessageHandler defaultHandler = nullptr; void logHandler(const QtMsgType type, const QMessageLogContext &context, - const QString &msg) { - defaultHandler(type, context, msg); - if (context.category == QLatin1String("generator") && type == QtCriticalMsg) { - ::exit(1); - } + const QString &msg) { + defaultHandler(type, context, msg); + if (context.category == QLatin1String("generator") && + type == QtCriticalMsg) { + ::exit(1); + } } int main(int argc, char *argv[]) { - defaultHandler = qInstallMessageHandler(logHandler); - QCoreApplication app(argc, argv); - QCommandLineParser parser; + defaultHandler = qInstallMessageHandler(logHandler); + QCoreApplication app(argc, argv); + QCommandLineParser parser; - parser.addOption(QCommandLineOption( - "includes", "Comma-separated list of extra includes.", "includes")); - parser.addOption(QCommandLineOption( - "namespace", "Namespace for the generated code.", "namespace")); - parser.addOption( - QCommandLineOption("directory", "Output directory.", "directory", ".")); - parser.addHelpOption(); - parser.addPositionalArgument("files", "Input files.", - "[deps_protocol_file] main_protocol_file"); - parser.process(app); + parser.addOption(QCommandLineOption( + "includes", "Comma-separated list of extra includes.", "includes")); + parser.addOption(QCommandLineOption( + "namespace", "Namespace for the generated code.", "namespace")); + parser.addOption( + QCommandLineOption("directory", "Output directory.", "directory", ".")); + parser.addHelpOption(); + parser.addPositionalArgument("files", "Input files.", + "[deps_protocol_file] main_protocol_file"); + parser.process(app); - EmitOptions options; - if (parser.isSet("includes")) { - options.extraIncludes_ = parser.values("includes"); - } - if (parser.isSet("namespace")) { - options.namespaces_ = parser.value("namespace").split("::"); - } - options.directory_ = parser.value("directory"); + EmitOptions options; + if (parser.isSet("includes")) { + options.extraIncludes_ = parser.values("includes"); + } + if (parser.isSet("namespace")) { + options.namespaces_ = parser.value("namespace").split("::"); + } + options.directory_ = parser.value("directory"); - const auto filenames = parser.positionalArguments(); + const auto filenames = parser.positionalArguments(); - KTextTemplate::registerMetaType(); - KTextTemplate::registerMetaType(); - KTextTemplate::registerMetaType(); + KTextTemplate::registerMetaType(); + KTextTemplate::registerMetaType(); + KTextTemplate::registerMetaType(); - Protocol protocol(filenames); - protocol.generate(options); + Protocol protocol(filenames); + protocol.generate(options); - return 0; + return 0; } diff --git a/src/parser.cpp b/src/parser.cpp index 14d0215..dfa1450 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -18,18 +18,18 @@ */ #include "parser.h" -#include -#include -#include +#include "protocol.h" #include #include +#include +#include #include #include +#include +#include #include #include -#include -#include -#include "protocol.h" +#include Q_DECLARE_LOGGING_CATEGORY(generator); @@ -78,7 +78,8 @@ void ParseContext::startElement(QStringView element_name, bool ok = false; version = att.value().toUInt(&ok); if (!ok) { - qCCritical(generator) << "wrong version (" << att.value() << ")"; + qCCritical(generator) + << "wrong version (" << att.value() << ")"; } } if (att.name() == "type") { @@ -151,8 +152,9 @@ void ParseContext::startElement(QStringView element_name, if (!ok) { qCCritical(generator) << "invalid integer (" << since << ")"; } else if (version > interface_->version_) { - qCCritical(generator) << "since (" << version << ") larger than version (" - << interface_->version_ << ")"; + qCCritical(generator) + << "since (" << version << ") larger than version (" + << interface_->version_ << ")"; } } else { version = 1; @@ -165,7 +167,8 @@ void ParseContext::startElement(QStringView element_name, message_->since_ = version; if (name == "destroy" && !message_->destructor_) { - qCCritical(generator) << "destroy request should be destructor type"; + qCCritical(generator) + << "destroy request should be destructor type"; } } else if (element_name == "arg") { if (name.isEmpty()) { @@ -187,7 +190,8 @@ void ParseContext::startElement(QStringView element_name, break; default: if (!interface_name.isEmpty()) { - qCCritical(generator) << "interface attribute not allowed for type " << type; + qCCritical(generator) + << "interface attribute not allowed for type " << type; } break; } @@ -198,13 +202,15 @@ void ParseContext::startElement(QStringView element_name, } else if (allow_null == "false") { arg->nullable_ = false; } else { - qCCritical(generator) << "invalid value for allow-null attribute (" - << allow_null << ")"; + qCCritical(generator) + << "invalid value for allow-null attribute (" << allow_null + << ")"; } if (!arg->isNullableType()) { - qCCritical(generator) << "allow-null is only valid for objects, strings, " - "and arrays"; + qCCritical(generator) + << "allow-null is only valid for objects, strings, " + "and arrays"; } } @@ -223,9 +229,10 @@ void ParseContext::startElement(QStringView element_name, } else if (bitfield == "true") { enumeration_->bitfield_ = true; } else { - qCCritical(generator) << "invalid value (" << bitfield - << ") for bitfield attribute (only true/false " - "are accepted)"; + qCCritical(generator) + << "invalid value (" << bitfield + << ") for bitfield attribute (only true/false " + "are accepted)"; } } else if (element_name == "entry") { if (name.isEmpty()) { @@ -239,9 +246,9 @@ void ParseContext::startElement(QStringView element_name, } } -void ParseContext::verifyArguments(Interface *interface, - std::list *messages, - std::list * /*enumerations*/) const { +void ParseContext::verifyArguments( + Interface *interface, std::list *messages, + std::list * /*enumerations*/) const { for (auto &m : *messages) { for (auto &a : m.argList_) { @@ -249,23 +256,27 @@ void ParseContext::verifyArguments(Interface *interface, continue; } - const auto *e = protocol_->findEnumeration(interface, a.enumerationName_); + const auto *e = + protocol_->findEnumeration(interface, a.enumerationName_); if (!e) { - qCCritical(generator) << "could not find enumeration " << a.enumerationName_; + qCCritical(generator) + << "could not find enumeration " << a.enumerationName_; } switch (a.type_) { case INT: if (e->bitfield_) { - qCCritical(generator) << "bitfield-style enum must only be referenced " - "by uint"; + qCCritical(generator) + << "bitfield-style enum must only be referenced " + "by uint"; } break; case UNSIGNED: break; default: - qCCritical(generator) << "enumeration-style argument has wrong type"; + qCCritical(generator) + << "enumeration-style argument has wrong type"; } } } @@ -290,7 +301,8 @@ void ParseContext::endElement(QStringView name) { message_ = nullptr; } else if (name == "enum") { if (enumeration_->entryList_.empty()) { - qCCritical(generator) << "enumeration " << enumeration_->name_ << " was empty"; + qCCritical(generator) + << "enumeration " << enumeration_->name_ << " was empty"; } enumeration_ = nullptr; } else if (name == "protocol") { diff --git a/src/parser.h b/src/parser.h index 20bf99d..ffa1841 100644 --- a/src/parser.h +++ b/src/parser.h @@ -7,12 +7,12 @@ #ifndef _GENERATOR_PARSER_H_ #define _GENERATOR_PARSER_H_ -#include #include "protocol.h" #include "utils.h" -#include #include #include +#include +#include struct Protocol; struct Message; @@ -27,19 +27,16 @@ struct ParseContext { void parse(); - void verifyArguments(Interface *interface, std::list *messages, std::list *enumerations) const; + void verifyArguments(Interface *interface, std::list *messages, + std::list *enumerations) const; void startElement(QStringView name, const QXmlStreamAttributes &atts); void endElement(QStringView name); void characterData(QStringView text); - void setLineNumber(int lineNumber) { - loc_.lineNumber_ = lineNumber; - } + void setLineNumber(int lineNumber) { loc_.lineNumber_ = lineNumber; } - const Location &location() const { - return loc_; - } + const Location &location() const { return loc_; } bool main_; QXmlStreamReader reader_; diff --git a/src/protocol.cpp b/src/protocol.cpp index 0f4b4d5..0e69be2 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -6,11 +6,8 @@ */ #include "protocol.h" -#include -#include -#include -#include -#include +#include "parser.h" +#include "utils.h" #include #include #include @@ -26,9 +23,12 @@ #include #include #include +#include +#include +#include #include -#include "parser.h" -#include "utils.h" +#include +#include using namespace Qt::Literals::StringLiterals; diff --git a/src/protocol.h b/src/protocol.h index 9b2932d..ace04f1 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -7,18 +7,18 @@ #ifndef _GENERATOR_PROTOCOL_H_ #define _GENERATOR_PROTOCOL_H_ -#include -#include -#include -#include -#include +#include "utils.h" #include #include #include #include #include #include -#include "utils.h" +#include +#include +#include +#include +#include struct Message; struct Enumeration; diff --git a/src/utils.h b/src/utils.h index e141410..d762be4 100644 --- a/src/utils.h +++ b/src/utils.h @@ -7,10 +7,10 @@ #ifndef _GENERATOR_UTILS_H_ #define _GENERATOR_UTILS_H_ +#include #include #include #include -#include #include struct Location {