Skip to content

Commit eef2bc4

Browse files
authored
Enforce Linux kernel coding style (#88)
The only exception is to indent with four spaces rather than tabs for sticking to compact layout of source listing. Close #87
1 parent 6110f8e commit eef2bc4

26 files changed

+275
-220
lines changed

examples/.clang-format

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,94 @@
1-
BasedOnStyle: Chromium
21
Language: Cpp
3-
MaxEmptyLinesToKeep: 3
4-
IndentCaseLabels: false
5-
AllowShortIfStatementsOnASingleLine: false
2+
3+
AccessModifierOffset: -4
4+
AlignAfterOpenBracket: Align
5+
AlignConsecutiveAssignments: false
6+
AlignConsecutiveDeclarations: false
7+
AlignOperands: true
8+
AlignTrailingComments: false
9+
AllowAllParametersOfDeclarationOnNextLine: false
10+
AllowShortBlocksOnASingleLine: false
611
AllowShortCaseLabelsOnASingleLine: false
12+
AllowShortFunctionsOnASingleLine: None
13+
AllowShortIfStatementsOnASingleLine: false
714
AllowShortLoopsOnASingleLine: false
15+
AlwaysBreakAfterDefinitionReturnType: None
16+
AlwaysBreakAfterReturnType: None
17+
AlwaysBreakBeforeMultilineStrings: false
18+
AlwaysBreakTemplateDeclarations: false
19+
BinPackArguments: true
20+
BinPackParameters: true
21+
22+
BraceWrapping:
23+
AfterClass: false
24+
AfterControlStatement: false
25+
AfterEnum: false
26+
AfterFunction: true
27+
AfterNamespace: true
28+
AfterObjCDeclaration: false
29+
AfterStruct: false
30+
AfterUnion: false
31+
AfterExternBlock: false
32+
BeforeCatch: false
33+
BeforeElse: false
34+
IndentBraces: false
35+
SplitEmptyFunction: true
36+
SplitEmptyRecord: true
37+
SplitEmptyNamespace: true
38+
39+
BreakBeforeBinaryOperators: None
40+
BreakBeforeBraces: Custom
41+
BreakBeforeInheritanceComma: false
42+
BreakBeforeTernaryOperators: false
43+
BreakConstructorInitializersBeforeComma: false
44+
BreakConstructorInitializers: BeforeComma
45+
BreakAfterJavaFieldAnnotations: false
46+
BreakStringLiterals: false
47+
ColumnLimit: 80
48+
CommentPragmas: '^ IWYU pragma:'
49+
CompactNamespaces: false
50+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
51+
ConstructorInitializerIndentWidth: 4
52+
ContinuationIndentWidth: 4
53+
Cpp11BracedListStyle: false
854
DerivePointerAlignment: false
55+
DisableFormat: false
56+
ExperimentalAutoDetectBinPacking: false
57+
FixNamespaceComments: false
58+
59+
IncludeBlocks: Preserve
60+
IncludeCategories:
61+
- Regex: '.*'
62+
Priority: 1
63+
IncludeIsMainRegex: '(Test)?$'
64+
IndentCaseLabels: false
65+
IndentPPDirectives: None
66+
IndentWidth: 4
67+
IndentWrappedFunctionNames: false
68+
KeepEmptyLinesAtTheStartOfBlocks: false
69+
MacroBlockBegin: ''
70+
MacroBlockEnd: ''
71+
MaxEmptyLinesToKeep: 1
72+
NamespaceIndentation: None
73+
974
PointerAlignment: Right
10-
SpaceAfterCStyleCast: true
75+
ReflowComments: false
76+
SortIncludes: false
77+
SortUsingDeclarations: false
78+
SpaceAfterCStyleCast: false
79+
SpaceAfterTemplateKeyword: true
80+
SpaceBeforeAssignmentOperators: true
81+
SpaceBeforeCtorInitializerColon: true
82+
SpaceBeforeInheritanceColon: true
83+
SpaceBeforeParens: ControlStatements
84+
SpaceBeforeRangeBasedForLoopColon: true
85+
SpaceInEmptyParentheses: false
86+
SpacesBeforeTrailingComments: 1
87+
SpacesInAngles: false
88+
SpacesInContainerLiterals: false
89+
SpacesInCStyleCastParentheses: false
90+
SpacesInParentheses: false
91+
SpacesInSquareBrackets: false
92+
Standard: Cpp03
1193
TabWidth: 4
12-
UseTab: Never
13-
IndentWidth: 4
14-
BreakBeforeBraces: Linux
15-
AccessModifierOffset: -4
94+
UseTab: Never

examples/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ all:
3636
clean:
3737
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
3838
$(RM) other/cat_noblock *.plist
39+
40+
indent:
41+
clang-format -i *[.ch]

examples/bottomhalf.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@
2121
#define DECLARE_TASKLET_OLD(arg1, arg2) DECLARE_TASKLET(arg1, arg2, 0L)
2222
#endif
2323

24-
static int button_irqs[] = {-1, -1};
24+
static int button_irqs[] = { -1, -1 };
2525

2626
/* Define GPIOs for LEDs.
2727
* TODO: Change the numbers for the GPIO on your board.
2828
*/
29-
static struct gpio leds[] = {{4, GPIOF_OUT_INIT_LOW, "LED 1"}};
29+
static struct gpio leds[] = { { 4, GPIOF_OUT_INIT_LOW, "LED 1" } };
3030

3131
/* Define GPIOs for BUTTONS
3232
* TODO: Change the numbers for the GPIO on your board.
3333
*/
3434
static struct gpio buttons[] = {
35-
{17, GPIOF_IN, "LED 1 ON BUTTON"},
36-
{18, GPIOF_IN, "LED 1 OFF BUTTON"},
35+
{ 17, GPIOF_IN, "LED 1 ON BUTTON" },
36+
{ 18, GPIOF_IN, "LED 1 OFF BUTTON" },
3737
};
3838

3939
/* Tasklet containing some non-trivial amount of processing */
@@ -106,7 +106,6 @@ static int __init bottomhalf_init(void)
106106
goto fail2;
107107
}
108108

109-
110109
ret = gpio_to_irq(buttons[1].gpio);
111110

112111
if (ret < 0) {

examples/chardev.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
2121

2222
#define SUCCESS 0
2323
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
24-
#define BUF_LEN 80 /* Max length of the message from the device */
24+
#define BUF_LEN 80 /* Max length of the message from the device */
2525

2626
/* Global variables are declared as static, so are global within the file. */
2727

28-
static int major; /* major number assigned to our device driver */
28+
static int major; /* major number assigned to our device driver */
2929
static int open_device_cnt = 0; /* Is device open?
3030
* Used to prevent multiple access to device */
31-
static char msg[BUF_LEN]; /* The msg the device will give when asked */
31+
static char msg[BUF_LEN]; /* The msg the device will give when asked */
3232
static char *msg_ptr;
3333

3434
static struct class *cls;
@@ -105,8 +105,8 @@ static int device_release(struct inode *inode, struct file *file)
105105
* read from it.
106106
*/
107107
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
108-
char *buffer, /* buffer to fill with data */
109-
size_t length, /* length of the buffer */
108+
char *buffer, /* buffer to fill with data */
109+
size_t length, /* length of the buffer */
110110
loff_t *offset)
111111
{
112112
/* Number of bytes actually written to the buffer */
@@ -134,9 +134,7 @@ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
134134
}
135135

136136
/* Called when a process writes to dev file: echo "hi" > /dev/hello */
137-
static ssize_t device_write(struct file *filp,
138-
const char *buff,
139-
size_t len,
137+
static ssize_t device_write(struct file *filp, const char *buff, size_t len,
140138
loff_t *off)
141139
{
142140
pr_alert("Sorry, this operation is not supported.\n");

examples/chardev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
/* Get the n'th byte of the message */
4141
#define IOCTL_GET_NTH_BYTE _IOWR(MAJOR_NUM, 2, int)
4242
/* The IOCTL is used for both input and output. It receives from the user
43-
* a number, n, and returns Message[n].
43+
* a number, n, and returns message[n].
4444
*/
4545

4646
/* The name of the device file */

examples/chardev2.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
/* Is the device open right now? Used to prevent concurrent access into
2121
* the same device
2222
*/
23-
static int Device_Open = 0;
23+
static int open_device_cnt = 0;
2424

2525
/* The message the device will give when asked */
26-
static char Message[BUF_LEN];
26+
static char message[BUF_LEN];
2727

2828
/* How far did the process reading the message get? Useful if the message
2929
* is larger than the size of the buffer we get to fill in device_read.
3030
*/
31-
static char *Message_Ptr;
31+
static char *message_ptr;
3232

3333
static struct class *cls;
3434

@@ -38,12 +38,12 @@ static int device_open(struct inode *inode, struct file *file)
3838
pr_info("device_open(%p)\n", file);
3939

4040
/* We don't want to talk to two processes at the same time. */
41-
if (Device_Open)
41+
if (open_device_cnt)
4242
return -EBUSY;
4343

44-
Device_Open++;
44+
open_device_cnt++;
4545
/* Initialize the message */
46-
Message_Ptr = Message;
46+
message_ptr = message;
4747
try_module_get(THIS_MODULE);
4848
return SUCCESS;
4949
}
@@ -53,7 +53,7 @@ static int device_release(struct inode *inode, struct file *file)
5353
pr_info("device_release(%p,%p)\n", inode, file);
5454

5555
/* We're now ready for our next caller */
56-
Device_Open--;
56+
open_device_cnt--;
5757

5858
module_put(THIS_MODULE);
5959
return SUCCESS;
@@ -62,9 +62,9 @@ static int device_release(struct inode *inode, struct file *file)
6262
/* This function is called whenever a process which has already opened the
6363
* device file attempts to read from it.
6464
*/
65-
static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
65+
static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
6666
char __user *buffer, /* buffer to be filled */
67-
size_t length, /* length of the buffer */
67+
size_t length, /* length of the buffer */
6868
loff_t *offset)
6969
{
7070
/* Number of bytes actually written to the buffer */
@@ -73,17 +73,17 @@ static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
7373
pr_info("device_read(%p,%p,%ld)\n", file, buffer, length);
7474

7575
/* If at the end of message, return 0 (which signifies end of file). */
76-
if (*Message_Ptr == 0)
76+
if (*message_ptr == 0)
7777
return 0;
7878

7979
/* Actually put the data into the buffer */
80-
while (length && *Message_Ptr) {
80+
while (length && *message_ptr) {
8181
/* Because the buffer is in the user data segment, not the kernel
8282
* data segment, assignment would not work. Instead, we have to
8383
* use put_user which copies data from the kernel data segment to
8484
* the user data segment.
8585
*/
86-
put_user(*(Message_Ptr++), buffer++);
86+
put_user(*(message_ptr++), buffer++);
8787
length--;
8888
bytes_read++;
8989
}
@@ -97,19 +97,17 @@ static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
9797
}
9898

9999
/* called when somebody tries to write into our device file. */
100-
static ssize_t device_write(struct file *file,
101-
const char __user *buffer,
102-
size_t length,
103-
loff_t *offset)
100+
static ssize_t device_write(struct file *file, const char __user *buffer,
101+
size_t length, loff_t *offset)
104102
{
105103
int i;
106104

107105
pr_info("device_write(%p,%s,%ld)", file, buffer, length);
108106

109107
for (i = 0; i < length && i < BUF_LEN; i++)
110-
get_user(Message[i], buffer + i);
108+
get_user(message[i], buffer + i);
111109

112-
Message_Ptr = Message;
110+
message_ptr = message;
113111

114112
/* Again, return the number of input characters used. */
115113
return i;
@@ -123,7 +121,7 @@ static ssize_t device_write(struct file *file,
123121
* If the ioctl is write or read/write (meaning output is returned to the
124122
* calling process), the ioctl call returns the output of this function.
125123
*/
126-
long device_ioctl(struct file *file, /* ditto */
124+
long device_ioctl(struct file *file, /* ditto */
127125
unsigned int ioctl_num, /* number and param for ioctl */
128126
unsigned long ioctl_param)
129127
{
@@ -138,33 +136,33 @@ long device_ioctl(struct file *file, /* ditto */
138136
* be the device's message. Get the parameter given to ioctl by
139137
* the process.
140138
*/
141-
temp = (char *) ioctl_param;
139+
temp = (char *)ioctl_param;
142140

143141
/* Find the length of the message */
144142
get_user(ch, temp);
145143
for (i = 0; ch && i < BUF_LEN; i++, temp++)
146144
get_user(ch, temp);
147145

148-
device_write(file, (char *) ioctl_param, i, 0);
146+
device_write(file, (char *)ioctl_param, i, 0);
149147
break;
150148

151149
case IOCTL_GET_MSG:
152150
/* Give the current message to the calling process - the parameter
153151
* we got is a pointer, fill it.
154152
*/
155-
i = device_read(file, (char *) ioctl_param, 99, 0);
153+
i = device_read(file, (char *)ioctl_param, 99, 0);
156154

157155
/* Put a zero at the end of the buffer, so it will be properly
158156
* terminated.
159157
*/
160-
put_user('\0', (char *) ioctl_param + i);
158+
put_user('\0', (char *)ioctl_param + i);
161159
break;
162160

163161
case IOCTL_GET_NTH_BYTE:
164162
/* This ioctl is both input (ioctl_param) and output (the return
165163
* value of this function).
166164
*/
167-
return Message[ioctl_param];
165+
return message[ioctl_param];
168166
break;
169167
}
170168

@@ -178,7 +176,7 @@ long device_ioctl(struct file *file, /* ditto */
178176
* is kept in the devices table, it can't be local to init_module. NULL is
179177
* for unimplemented functions.
180178
*/
181-
struct file_operations Fops = {
179+
struct file_operations fops = {
182180
.read = device_read,
183181
.write = device_write,
184182
.unlocked_ioctl = device_ioctl,
@@ -190,7 +188,7 @@ struct file_operations Fops = {
190188
static int __init chardev2_init(void)
191189
{
192190
/* Register the character device (atleast try) */
193-
int ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &Fops);
191+
int ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &fops);
194192

195193
/* Negative values signify an error */
196194
if (ret_val < 0) {

examples/cryptosha256.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static void show_hash_result(char *plaintext, char *hash_sha256)
1313

1414
pr_info("sha256 test for string: \"%s\"\n", plaintext);
1515
for (i = 0; i < SHA256_LENGTH; i++)
16-
sprintf(&str[i * 2], "%02x", (unsigned char) hash_sha256[i]);
16+
sprintf(&str[i * 2], "%02x", (unsigned char)hash_sha256[i]);
1717
str[i * 2] = 0;
1818
pr_info("%s\n", str);
1919
}
@@ -53,7 +53,9 @@ int cryptosha256_init(void)
5353
return 0;
5454
}
5555

56-
void cryptosha256_exit(void) {}
56+
void cryptosha256_exit(void)
57+
{
58+
}
5759

5860
module_init(cryptosha256_init);
5961
module_exit(cryptosha256_exit);

0 commit comments

Comments
 (0)