diff options
-rw-r--r-- | SConstruct | 4 | ||||
-rw-r--r-- | etc/format_sample.cpp | 171 | ||||
-rw-r--r-- | src/mongo/.clang-format | 61 |
3 files changed, 236 insertions, 0 deletions
diff --git a/SConstruct b/SConstruct index 7775ea5bf6a..6b274017355 100644 --- a/SConstruct +++ b/SConstruct @@ -2230,6 +2230,10 @@ checkErrorCodes() # --- lint ---- def doLint( env , target , source ): + import buildscripts.clang_format + if not buildscripts.clang_format.lint(None, []): + raise Exception("clang-format lint errors") + import buildscripts.lint if not buildscripts.lint.run_lint( [ "src/mongo/" ] ): raise Exception( "lint errors" ) diff --git a/etc/format_sample.cpp b/etc/format_sample.cpp new file mode 100644 index 00000000000..b5b98b5ebc9 --- /dev/null +++ b/etc/format_sample.cpp @@ -0,0 +1,171 @@ +/** + * Sample file to demonstrate various clang-format messages + */ +#include <stdio.h> +#include <vector> +#include <string> + +// NamespaceIndentation: None +namespace foo { +// Contents of namespace are not indented. +int foo; + +namespace bar { +int bar; + +namespace { +int anony; +} // namespace anony + +} // namespace bar + +/** + * A Class comment + */ +class Example { + // AccessModiferOffset: -4 +public: + // BreakConstructorInitializersBeforeComma: false + // ConstructorInitializerAllOnOneLineOrOnePerLine: true + // + Example() : _aVariable(4), _bVariable(42) {} + Example(int a) + : _aVariable(a), + _bVariable(42), + _AReallyReallyLongVariableName(4), + _AnotherReallyReallyLongVariableNameToTriggerWrapping(42) { + printf("Hello "); + } + ~Example() {} + + /** + * A Function comment + * AllowShortFunctionsOnASingleLine: Empty + */ + int getOneLineFunction() { + return 0; + } + + /** A Incorrect Function comment + * AllowShortFunctionsOnASingleLine: Empty + */ + void doNothing() {} + + /** + * A Useful Function comment + */ + int manyVariableFunction(unsigned long long arg1, char arg2, unsigned long long arg3); + +private: + int _aVariable; + long _bVariable; + short _cVarianble = 49; + long _AReallyReallyLongVariableName; + long _AnotherReallyReallyLongVariableNameToTriggerWrapping; +}; + +int foo3() { + return 42; +} + +// AlwaysBreakTemplateDeclarations: true +template <typename T> +T myAdd(T x, T y) { + return x + y; +} + +// AlwaysBreakAfterDefinitionReturnType: false +// BinPackParameters: false +int Example::manyVariableFunction(unsigned long long argWithLongName, + char arg2, + unsigned long long argWithAnotherLongName) { + // 3.7 - AlignConsecutiveAssignments - false + // + int aaaa = 12; + int b = 23; + int ccc = 23; + // PointerAlignment: Left + const char* some_pointer = "Hello"; + + // SpacesInAngles: false + std::vector<std::pair<std::string, int>> list; + + // SpaceAfterCStyleCast: false + // SpacesInCStyleCastParentheses: false + char* some_nonconst_pointer = (char*)some_pointer; + + // Multi-line if + // SpaceBeforeParens: False + if (argWithLongName == 0) { // Comment: SpacesBeforeTrailingComments + // Do something + } else if (b % 7 = 3) { + } // some weird trailing else comment that clang-format does not touch + else { + // Notice the indent around else + } + + // AllowShortIfStatementsOnASingleLine: false + // Put statements on separate lines for short ifs + if (arg2 == 'a') + arg2 = 'b'; + + int bbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccc; + int aaaaaaaaaaaaaaaaaaaaaaaaaaaa = + bbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccccccc; + + // AlignOperands + int dddddddddddddddddddddddddddd = aaaaaaaaaaaaaaaaaaaaaaaaaaaa * 7 + 4 % 235124 > 275645 + ? bbbbbbbbbbbbbbbbbbbbbbbbbbbb + 897234 + : cccccccccccccccccccccccccccccc % 1293402; + + // AllowShortBlocksOnASingleLine: false + if (b) { + return 3; + } + + // AllowShortLoopsOnASingleLine: false + while (b < 5) + b++; + + // BreakBeforeBinaryOperators: None + if (b > 5 || b % 42 || cccccccccccccccccccccccccccccc % 873 || aaaa * 12312 % 23485 != 9873 || + some_pointer != 0) { + printf("Huh!\n"); + } + + // AlignAfterOpenBracket: false + // BinPackParameters: false + printf("A short function call %s %s %d - %ld\n", "", "tgz", 4, ULONG_MAX); + printf("A long function call %s %s %d - %ld\n", + "http://www.mongodbo.org/downloads", + "mongodb-latest.tgz", + 4, + ULONG_MAX); + printf("Thing1 %s\n", "Thing2"); + + // No spaces between parens and args + printf("%c\n", arg2); + + // A switch statement: TODO: Andy, what is the indent we want? Google style? + switch (arg2) { + // AllowShortCaseLabelsOnASingleLine: false + // IndentCaseLabels: true + case 'a': + return 2; + case 'y': + case 'z': + // Do something here + break; + default: + // The default` + break; + } + + do { + // Do a loop here + } while (0); + + return 1; +} + +} // namespace foo diff --git a/src/mongo/.clang-format b/src/mongo/.clang-format new file mode 100644 index 00000000000..d957d577f00 --- /dev/null +++ b/src/mongo/.clang-format @@ -0,0 +1,61 @@ +# BasedOnStyle: Google +AccessModifierOffset: -4 +AlignOperands: false +AlignAfterOpenBracket: true +AlignEscapedNewlinesLeft: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: false +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: true +BinPackArguments: false +BinPackParameters: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +ColumnLimit: 100 +CommentPragmas: '' +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +IndentCaseLabels: true +IndentFunctionDeclarationAfterType: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: false +Language: Cpp +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: None +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: false +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: true +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +TabWidth: 4 +UseTab: Never |