diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000000..7102f63f270 --- /dev/null +++ b/.clang-format @@ -0,0 +1,194 @@ +# Also see codestyle/cpp/README.md +# For documentation on options see: https://releases.llvm.org/8.0.0/tools/clang/docs/ClangFormatStyleOptions.html +# "Kt N/A" means that this C/C++/ObjC construct have no corresponding Kotlin construct to refer to +# "Kt U" means that formatting is unspecified in Kotlin formatting guide +# "Kt " means that this is specified in Kotlin formatting guide with url pointing to it +--- +DisableFormat: false +Standard: Cpp11 + +# Kt N/A. Different from 0 to make modifiers stand out. An alternative is -2, but it introduces another level of indentation. +AccessModifierOffset: -4 +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#method-call-formatting +AlignAfterOpenBracket: AlwaysBreak +# Kt U. Not touching +AlignConsecutiveAssignments: false +# Kt U. Not touching +AlignConsecutiveDeclarations: false +# Kt N/A. Not touching +AlignEscapedNewlines: DontAlign +# Kt U. Not touching +AlignOperands: false +# Kt U. Not touching +AlignTrailingComments: false +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#function-formatting +AllowAllParametersOfDeclarationOnNextLine: true +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting-control-flow-statements +AllowShortBlocksOnASingleLine: false +# Kt U. The closest is https://kotlinlang.org/docs/reference/coding-conventions.html#formatting-control-flow-statements +# Using false, because case statements usually contain at least 2 statements: doing something + break, which makes them multiline +AllowShortCaseLabelsOnASingleLine: false +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#function-formatting Inline is somewhat close to "single expression" functions +AllowShortFunctionsOnASingleLine: Inline +# Kt U. The closest is https://kotlinlang.org/docs/reference/coding-conventions.html#using-conditional-statements however it mostly refers to the ternary operator. +AllowShortIfStatementsOnASingleLine: true +# Kt U. Same as previous +AllowShortLoopsOnASingleLine: true +# Kt N/A. In Kotlin return type is in a trailing position. +AlwaysBreakAfterReturnType: None +# Kt U. Not touching. +AlwaysBreakBeforeMultilineStrings: false +# Kt N/A. In Kotlin type parameters are declared inline in a much more compact way. +# Using Yes to make it easy to detect function name +AlwaysBreakTemplateDeclarations: Yes +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#method-call-formatting +BinPackArguments: true +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#function-formatting +BinPackParameters: false +BraceWrapping: + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + AfterClass: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + AfterControlStatement: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + AfterEnum: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + AfterFunction: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + AfterNamespace: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + AfterObjCDeclaration: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + AfterStruct: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + AfterUnion: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + AfterExternBlock: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting-control-flow-statements + BeforeCatch: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting-control-flow-statements + BeforeElse: false + # Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting + IndentBraces: false + # Only used when AfterFunction is true + SplitEmptyFunction: true + # Only used when AfterRecord is true + SplitEmptyRecord: true + # Only used if AfterNamespace is true + SplitEmptyNamespace: true +# Kt U. Break after operators +BreakBeforeBinaryOperators: None +# Configured by BraceWrapping +BreakBeforeBraces: Custom +# Kt U. Using true because it looks more like an if-expression with "then" and "else" branches marked "?" and ":" +BreakBeforeTernaryOperators: true +# Kt N/A. The closest is https://kotlinlang.org/docs/reference/coding-conventions.html#class-header-formatting AfterColon looks the most consistent +BreakConstructorInitializers: AfterColon +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#class-header-formatting +BreakInheritanceList: AfterColon +# Kt U. Choosing to break long strings to fit into line length +BreakStringLiterals: true +# Kt U. IDEA displays a vertical guide at 120. Choosing it to avoid very long lines. +ColumnLimit: 140 +# Kt N/A. Probably should use nested namespaces instead. +CompactNamespaces: false +# Kt N/A. The closest is https://kotlinlang.org/docs/reference/coding-conventions.html#class-header-formatting +ConstructorInitializerAllOnOneLineOrOnePerLine: true +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting +ConstructorInitializerIndentWidth: 4 +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting IDEA seems to indent continuations with 8 +ContinuationIndentWidth: 8 +# Kt N/A. The closest is https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace +# Braced initalizer braces are close to parens in function call or to brackets in array initialier. And styleguide asks not to put spaces in them. +Cpp11BracedListStyle: true +# Configured by PointerAlignment +DerivePointerAlignment: false +# Confgured by BinPack* +ExperimentalAutoDetectBinPacking: false +# Kt N/A. Setting to true because it helps to see when anonymous namespace ends +FixNamespaceComments: true +# Kt U. Not touching manually created including blocks +IncludeBlocks: Preserve +# Kt U. Sorting: main header (priority 0) > system headers > project headers +IncludeCategories: + - Regex: '^<.*' + Priority: 1 + - Regex: '.*' + Priority: 2 +# Kt N/A. Main header must match current filename (modulo extension) exactly. +IncludeIsMainRegex: '$' +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting-control-flow-statements +IndentCaseLabels: true +# Kt N/A. Do not indent macro code +IndentPPDirectives: None +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting +IndentWidth: 4 +# Kt U. IDEA seems to indent +IndentWrappedFunctionNames: true +# Kt U. Do not touch. +KeepEmptyLinesAtTheStartOfBlocks: false +# Kt U. IDEA keeps 1 empty line by default. +MaxEmptyLinesToKeep: 1 +# Kt N/A. +NamespaceIndentation: None +# Kt N/A. The closest is https://kotlinlang.org/docs/reference/coding-conventions.html#class-header-formatting +ObjCBinPackProtocolList: Never +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting +ObjCBlockIndentWidth: 4 +# Kt N/A. Following Google and LLVM styleguides. +ObjCSpaceAfterProperty: false +# Kt N/A. The closest is class inheritance list. Following Google and LLVM styleguides. +ObjCSpaceBeforeProtocolList: true +# Kt N/A. +PointerAlignment: Left +# Kt U. Reflow comments to fit into line width +ReflowComments: true +# Kt U. IDEA does not sort by default, but allows this option. Do not touch. +SortIncludes: false +# Kt U. Like SortIncludes. +SortUsingDeclarations: false +# Kt N/A. +SpaceAfterCStyleCast: false +# Kt N/A. IDEA puts space in `fun ` +SpaceAfterTemplateKeyword: true +# Kt U. https://kotlinlang.org/docs/reference/coding-conventions.html puts spaces +SpaceBeforeAssignmentOperators: true +# Kt N/A. The closest is https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace which does not put space before parens and brackets. +SpaceBeforeCpp11BracedList: false +# Kt N/A. The closest is https://kotlinlang.org/docs/reference/coding-conventions.html#colon +SpaceBeforeCtorInitializerColon: true +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#colon +SpaceBeforeInheritanceColon: true +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace +SpaceBeforeParens: ControlStatements +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace +SpaceBeforeRangeBasedForLoopColon: true +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace +SpaceInEmptyParentheses: false +# Kt U. https://kotlinlang.org/docs/reference/coding-conventions.html uses 1 +SpacesBeforeTrailingComments: 1 +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace +SpacesInAngles: false +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace +SpacesInContainerLiterals: false +# Kt N/A. +SpacesInCStyleCastParentheses: false +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace +SpacesInParentheses: false +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace +SpacesInSquareBrackets: false +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting +TabWidth: 4 +# Kt https://kotlinlang.org/docs/reference/coding-conventions.html#formatting +UseTab: Never + +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +... + diff --git a/codestyle/cpp/CLionFormat.xml b/codestyle/cpp/CLionFormat.xml new file mode 100644 index 00000000000..92e72963b74 --- /dev/null +++ b/codestyle/cpp/CLionFormat.xml @@ -0,0 +1,56 @@ + + \ No newline at end of file diff --git a/codestyle/cpp/README.md b/codestyle/cpp/README.md new file mode 100644 index 00000000000..0a078aa2a1b --- /dev/null +++ b/codestyle/cpp/README.md @@ -0,0 +1,39 @@ +# Code style for C++ + +**TODO**: Expand beyond naming and formatting + +## Headers + +* Headers should live in the same folder with it's implementation counterpart (if there's one) +* Headers should use header guards + +## Naming + +* Types should use `PascalCase` +* Local variables and function parameters should use `camelCase` +* Global variables should use `camelCase` +* Constants should use `kPascalCase` (with prefix `k`) +* Private functions (not visible outside a compilation unit) should use `camelCase` +* Exported functions (declared in headers or shared with Kotlin) should use `PascalCase` +* Member fields should use `camelCase`. Private member fields should add `_` suffix: `camelCase_` +* Member functions should use `camelCase` +* Macros should use `SCREAMING_SNAKE_CASE` +* namespaces should use `snake_case` +* `enum` and `enum class` members should use `kPascalCase` + +If API is designed to mimic C++ stdlib (e.g. stubbing `` for platforms that do not support it), its allowed +to follow stdlib naming conventions. + +## Formatting + +For automated formatting you can use [config for CLion](codestyle/cpp/CLionFormat.xml) or `clang-format` (see [config](.clang-format) at the repo's root). Note, that CLion uses `clang-format` by default; this can be turned off if you prefer to use rules from `CLionFormat.xml`. + +Formatting rules are designed to closely mirror [Kotlin rules](https://kotlinlang.org/docs/reference/coding-conventions.html). + +* Use spaces instead of tabs. Indentation width is 4 spaces. Continuation width is 8 spaces +* Do not indent namespaces +* Visibility modifiers are placed without indentation +* All operators should be wrapped with a space or a line break +* In pointer and reference definitions `*` and `&` should be placed on a type instead of a variable +* In pointer to functions prefer not to use `*` at all. +* Add a space between `template` and `<`