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

clang-format #91

Closed
wants to merge 4 commits into from
Closed
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
143 changes: 143 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# SPDX-License-Identifier: GPL-2.0
#
# clang-format configuration file. Intended for clang-format >= 12.
#
# For more information, see:
#
# Documentation/dev-tools/clang-format.rst
# https://clang.llvm.org/docs/ClangFormat.html
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
#
---
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 90
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh noooo, I can no longer have two panes side-by-side :-P

Semi-joking aside (I do use ^^) - if we do this, the .editorconfig also needs an update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hope your pane is modern enough in 2024 to have horizontal scroll for the eventual lines going over 80 :). What I find annoying with auto format is that we don't have a good option for "prefer 80, but don't bother breaking it up if it's slightly over" - IMO we should be able to define soft and hard limits for autoformat tools. I think the Penalty* settings in clang get close, but I still see it doing some horrible changes because of column limit

Copy link
Collaborator

Choose a reason for hiding this comment

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

2x 80 (with a few extra symbols) just happens to be comfortably sized on my 13inch laptop. Any other device or single (tmux) pane is fine. Hope it was clear I was joking

Cannot agree more on the hard vs soft limits. If we keep different values in .clang-format vs .editorconfig, clang-format is likely to repeatedly re-format my patches ;-)

CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false

# Taken from:
# git grep -h '^#define [^[:space:]]*foreach[^[:space:]]*(' \
# | sed "s,^#define \([^[:space:]]*foreach[^[:space:]]*\)(.*$, - '\1'," \
# | LC_ALL=C sort -u
ForEachMacros:
- 'kmod_list_foreach'
- 'kmod_list_foreach_reverse'

# Taken from:
# git grep -h '^#define .*__attribute__' \
# | cut -d' ' -f2 \
# | sed -e 's/(.*//' -e 's/^/ - /' \
# | LC_ALL=C sort -u
AttributeMacros:
- KMOD_EXPORT
- TS_EXPORT
- __noreturn
- _always_inline_
- _cleanup_
- _must_check_
- _printf_format_
- _unused_
- noreturn
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note for another day: the above could use some cleanup.

Huge thanks for finding this trick o/

Copy link
Collaborator

Choose a reason for hiding this comment

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

The cleanup was incorporated in #114


IncludeBlocks: Preserve
IncludeCategories:
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 8
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 8
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true

# Taken from git's rules
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 100
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 10
PenaltyExcessCharacter: 50
PenaltyReturnTypeOnItsOwnLine: 110

PointerAlignment: Right
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatementsExceptForEachMacros
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp03
TabWidth: 8
UseTab: Always
...
22 changes: 12 additions & 10 deletions libkmod/libkmod-builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static struct kmod_builtin_iter *kmod_builtin_iter_new(struct kmod_ctx *ctx)

snprintf(path, PATH_MAX, "%s/%s", dirname, MODULES_BUILTIN_MODINFO);

file = open(path, O_RDONLY|O_CLOEXEC);
file = open(path, O_RDONLY | O_CLOEXEC);
if (file < 0) {
sv_errno = errno;
goto fail;
Expand Down Expand Up @@ -101,8 +101,8 @@ static void kmod_builtin_iter_free(struct kmod_builtin_iter *iter)
free(iter);
}

static off_t get_string(struct kmod_builtin_iter *iter, off_t offset,
char **line, size_t *size)
static off_t get_string(struct kmod_builtin_iter *iter, off_t offset, char **line,
size_t *size)
{
int sv_errno;
char buf[BUFSIZ];
Expand All @@ -122,9 +122,9 @@ static off_t get_string(struct kmod_builtin_iter *iter, off_t offset,
break;
}

nullp = memchr(buf, '\0', (size_t) sz);
nullp = memchr(buf, '\0', (size_t)sz);
partsz = (size_t)((nullp) ? (nullp - buf) + 1 : sz);
offset += (off_t) partsz;
offset += (off_t)partsz;

if (iter->bufsz < linesz + partsz) {
void *tmp;
Expand Down Expand Up @@ -162,7 +162,7 @@ static off_t get_string(struct kmod_builtin_iter *iter, off_t offset,

static bool kmod_builtin_iter_next(struct kmod_builtin_iter *iter)
{
char *line, *modname;
char *line, *modname;
size_t linesz;
off_t pos, offset, modlen;

Expand All @@ -185,7 +185,8 @@ static bool kmod_builtin_iter_next(struct kmod_builtin_iter *iter)

dot = strchr(line, '.');
if (!dot) {
ERR(iter->ctx, "kmod_builtin_iter_next: unexpected string without modname prefix\n");
ERR(iter->ctx,
"kmod_builtin_iter_next: unexpected string without modname prefix\n");
pos = iter->size;
break;
}
Expand All @@ -212,7 +213,7 @@ static bool kmod_builtin_iter_next(struct kmod_builtin_iter *iter)
}

static bool kmod_builtin_iter_get_modname(struct kmod_builtin_iter *iter,
char modname[static PATH_MAX])
char modname[static PATH_MAX])
{
int sv_errno;
char *line, *dot;
Expand All @@ -237,7 +238,8 @@ static bool kmod_builtin_iter_get_modname(struct kmod_builtin_iter *iter,
dot = strchr(line, '.');
if (!dot) {
sv_errno = EINVAL;
ERR(iter->ctx, "kmod_builtin_iter_get_modname: unexpected string without modname prefix\n");
ERR(iter->ctx,
"kmod_builtin_iter_get_modname: unexpected string without modname prefix\n");
goto fail;
}

Expand All @@ -259,7 +261,7 @@ static bool kmod_builtin_iter_get_modname(struct kmod_builtin_iter *iter,

/* array will be allocated with strings in a single malloc, just free *array */
ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname,
char ***modinfo)
char ***modinfo)
{
ssize_t count = 0;
char *s, *line = NULL;
Expand Down
Loading