Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce coding style #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be annoying as if sorts the included headers alphabetically. You usually want more flexibility on order of the included files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also specify include categories, which will provide some general rules on how the included headers will be sorted.

example:
IncludeCategories:
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
    Priority:        3
  - Regex:           '.*'
    Priority:        1

SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ matrix:
env:
- OS_TYPE=doxygen
addons: { apt: { packages: ["cmake", "graphviz"] } }
- os: linux
env:
- OS_TYPE=clangformat
addons: { apt: { packages: ["clang-format-6.0"], sources: ["llvm-toolchain-xenial-6.0"] } }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curios: would this work? addons: { apt: { packages: ["clang-format-6.0"], sources: ["llvm-toolchain-${TRAVIS_DIST}-6.0"] } }

also, is ther a generic llvm-toolchain-6.0 addon?
i would not add xenial if possible; otherwise, it's probably better to add dist: xenial back to make sure things stay consistent (sorry about that, if it happens)


before_install:

Expand Down
4 changes: 4 additions & 0 deletions CI/travis/before_install_linux
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ handle_doxygen() {
cd ..
}

handle_clangformat() {
echo "Clang-format version: " "$(/usr/bin/clang-format-6.0 --version)"
}

OS_TYPE=${1:-default}
OS_VERSION=${2}

Expand Down
45 changes: 45 additions & 0 deletions CI/travis/clangformat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

echo_red() { printf "\033[1;31m$*\033[m\n"; }
echo_green() { printf "\033[1;32m$*\033[m\n"; }

############################################################################
# Check if the file given as input has .h or .cpp extension
############################################################################
is_source_file() {
local file="$1"

EXTENSIONS=".hpp .cpp"

for extension in $EXTENSIONS; do
[[ "${file: -2}" == "$extension" || "${file: -4}" == "$extension" ]] && return 0
done;

return 1
}


############################################################################
# Check if the files modified in the current commit / commit range respect
# the coding style defined in the .clang-format file
############################################################################
COMMIT_RANGE=$TRAVIS_COMMIT_RANGE

if [ -z "$TRAVIS_PULL_REQUEST_SHA" ]
then
COMMIT_RANGE=HEAD~1
fi

git diff --name-only --diff-filter=d $COMMIT_RANGE | while read -r file; do
if is_source_file "$file"
then
/usr/bin/clang-format-6.0 -i "$file"
fi
done;

git diff --exit-code || {
echo_red "The code is not properly formatted!"
exit 1
}

echo_green "The code is properly formatted!"
4 changes: 4 additions & 0 deletions CI/travis/make_linux
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ handle_doxygen() {
./CI/travis/doxygen.sh
}

handle_clangformat() {
./CI/travis/clangformat.sh
}

LIBNAME="$1"
OS_TYPE=${2:-default}
OS_VERSION="$3"
Expand Down
3 changes: 3 additions & 0 deletions src/digital/m2kdigital.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ using namespace std;
M2kDigital::M2kDigital(struct iio_context *ctx, std::string logic_dev, bool sync) :
m_pimpl(std::unique_ptr<M2kDigitalImpl>(new M2kDigitalImpl(ctx, logic_dev, sync)))
{


// test format should fail
}

M2kDigital::~M2kDigital()
Expand Down