diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..1d9e5d87 --- /dev/null +++ b/.clang-format @@ -0,0 +1,177 @@ +--- +Language: Cpp +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveMacros: None +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: None +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortEnumsOnASingleLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Inline +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: TopLevel +AlwaysBreakAfterReturnType: TopLevel +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: Yes +AttributeMacros: + - __capability +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterCaseLabel: false + AfterClass: true + AfterControlStatement: Never + AfterEnum: true + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: true + AfterUnion: true + AfterExternBlock: true + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: false + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: true +BreakBeforeBraces: Mozilla +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeComma +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeComma +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 2 +ContinuationIndentWidth: 2 +Cpp11BracedListStyle: false +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseLabels: true +IndentCaseBlocks: false +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentRequires: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: false +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PenaltyIndentedWhitespace: 0 +PointerAlignment: Left +PPIndentWidth: -1 +ReferenceAlignment: Pointer +ReflowComments: true +ShortNamespaceLines: 1 +SortIncludes: Never +SortJavaStaticImport: Before +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceAroundPointerQualifiers: Default +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +BitFieldColonSpacing: Both +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE + - NS_SWIFT_NAME + - CF_SWIFT_NAME +... + diff --git a/clang_formatter.py b/clang_formatter.py new file mode 100644 index 00000000..8d9230d3 --- /dev/null +++ b/clang_formatter.py @@ -0,0 +1,11 @@ +import subprocess + + +class ClangFormatter: + def fix_formatting(self, file_path): + """Check and modifies file if needs formatting""" + try: + subprocess.run(['clang-format', '-i', file_path], + capture_output=True, check=True) + except subprocess.CalledProcessError as e: + print(f"clang-format error for file {file_path}: {e}") diff --git a/cpp/__init__.py b/cpp/__init__.py index 373cd458..16a080be 100644 --- a/cpp/__init__.py +++ b/cpp/__init__.py @@ -180,7 +180,6 @@ def cpp_escape_keyword(value): "Schema": "serialization::pimpl::schema", "List_Schema": "std::vector", - "Version": "version", } _cpp_types_encode = { diff --git a/requirements.txt b/requirements.txt index 36158e3f..ed524966 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ PyYAML jsonschema -jinja2 \ No newline at end of file +jinja2 +clang-format \ No newline at end of file diff --git a/util.py b/util.py index 1172ba52..aac7c89e 100644 --- a/util.py +++ b/util.py @@ -15,6 +15,7 @@ from yaml import MarkedYAMLError from binary import FixSizedEntryListTypes, FixSizedTypes, FixSizedListTypes, FixSizedMapTypes +from clang_formatter import ClangFormatter from cpp import ( cpp_ignore_service_list, cpp_types_decode, @@ -205,13 +206,15 @@ def generate_codecs(services, custom_services, template, output_dir, lang, env): data_containing_requests = generate_data_containing_requests_lookup_table(services, custom_services) id_fmt = "0x%02x%02x%02x" + codecs_header_file = join(output_dir, "codecs.h") + codecs_source_file = join(output_dir, "codecs.cpp") if lang is SupportedLanguages.CPP: curr_dir = dirname(realpath(__file__)) cpp_dir = "%s/cpp" % curr_dir content = get_rendered_text("header_includes.j2", env) - save_file(join(output_dir, "codecs.h"), content) + save_file(codecs_header_file, content) content = get_rendered_text("source_header.j2", env) - save_file(join(output_dir, "codecs.cpp"), content) + save_file(codecs_source_file, content) for service in services: if ignore_service(service, lang): @@ -246,7 +249,7 @@ def generate_codecs(services, custom_services, template, output_dir, lang, env): method=method, contains_serialized_data_in_request=contains_serialized_data_in_request ) - save_file(join(output_dir, "codecs.h"), content, "a+") + save_file(codecs_header_file, content, "a+") codec_template = env.get_template("codec-template.cpp.j2") content = codec_template.render( @@ -254,7 +257,7 @@ def generate_codecs(services, custom_services, template, output_dir, lang, env): method=method, contains_serialized_data_in_request=contains_serialized_data_in_request ) - save_file(join(output_dir, "codecs.cpp"), content, "a+") + save_file(codecs_source_file, content, "a+") else: content = template.render( service_name=service_name, @@ -267,8 +270,11 @@ def generate_codecs(services, custom_services, template, output_dir, lang, env): if lang is SupportedLanguages.CPP: content = get_rendered_text("footer.j2", env) - save_file(join(output_dir, "codecs.h"), content, "a+") - save_file(join(output_dir, "codecs.cpp"), content, "a+") + save_file(codecs_header_file, content, "a+") + clang_formatter = ClangFormatter() + clang_formatter.fix_formatting(codecs_header_file) + save_file(codecs_source_file, content, "a+") + clang_formatter.fix_formatting(codecs_source_file) def generate_custom_codecs(services, template, output_dir, lang, env):