Skip to content

Commit 3210fb2

Browse files
author
newnix
committed
Massive Cleanup
* .clang-format - Started work on configuring clang-format options * .clang-tidy - Started work on configuring static analysis options * compile_commands.json - Hand-written compilation database to help with the clang tooling This is a first-pass refactor primarily targeting statements in branches or loops that weren't enclosed in braces to clearly delineate their scope. Additionally, some minor whitespace and naming changes were made, as well as normalizing the format of function definitions. There's still a LOT of work to be done for this project, but this is a good step towards making it easier to automatically catch and fix certain types of maintenance burdens and potential issues not covered by typical compiler warnings.
1 parent 39e4295 commit 3210fb2

39 files changed

+2787
-903
lines changed

.clang-format

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# vim: ft=yaml
2+
---
3+
## Supposedly supports C too, but there's no documented
4+
## support for setting a C standard like C11, so
5+
## the utility for C codebases may be somewhat limited.
6+
Language: Cpp
7+
BasedOnStyle: None
8+
IndentWidth: 2
9+
AccessModifierOffset: 0
10+
AlignAfterOpenBracket: Align
11+
AlignConsecutiveMacros: true
12+
AlignEscapedNewlines: Left
13+
AlignOperands: Align
14+
AllowAllArgumentsOnNextLine: false
15+
AllowAllParametersOfDeclarationOnNextLine: false
16+
AllowShortBlocksOnASingleLine: Never
17+
AllowShortCaseLabelsOnASingleLine: false
18+
AllowShortEnumsOnASingleLine: true
19+
AllowShortFunctionsOnASingleLine: None
20+
AllowShortIfStatementsOnASingleLine: Never
21+
AllowShortLoopsOnASingleLine: false
22+
AlwaysBreakAfterReturnType: AllDefinitions
23+
AlwaysBreakBeforeMultilineStrings: false
24+
BinPackArguments: true
25+
BinPackParameters: true
26+
BreakBeforeBraces: Attach
27+
BreakBeforeTernaryOperators: true
28+
BreakStringLiterals: true
29+
ColumnLimit: 120
30+
DisableFormat: false
31+
IncludeBlocks: Regroup
32+
IncludeCategories:
33+
- Regex: '^<(sys)/'
34+
Priority: -3
35+
- Regex: '^<([:alnum:]+/)'
36+
Priority: -2
37+
- Regex: '^<'
38+
Priority: -1
39+
- Regex: '^"'
40+
Priority: 0
41+
IncludeIsMainRegex: '([_-]test)?$'

.clang-tidy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# vim: ft=yaml
2+
## NOTE: Documentation is available online here:
3+
# https://clang.llvm.org/extra/clang-tidy/checks/list.html
4+
---
5+
Checks: -*,
6+
bugprone-*,
7+
clang-analyzer-*,
8+
readability-*,
9+
cert-*,
10+
performance-*,
11+
portability-*,
12+
modernize-*,
13+
concurrency-*,
14+
llvm-libc-*,
15+
misc-*,
16+
-readability-avoid-const-params-in-decls,
17+
-readability-isolate-declaration
18+
WarningsAsErrors: -*,
19+
clang-analyzer-*,
20+
readability-*,
21+
bugprone-*,
22+
modernize-*,
23+
-readability-redundant-control-flow
24+
-readability-else-after-return
25+
CheckOptions:
26+
## XXX: This is the default, but I want to be explicit with this check
27+
- key: 'readability-braces-around-statements.ShortStatementLines'
28+
value: 0

access.c

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#define OTHER 0
1616

1717
/* ingroupset -- determine whether gid lies in the user's set of groups */
18-
static Boolean ingroupset(int gid) {
18+
static Boolean
19+
ingroupset(int gid) {
1920
#ifdef NGROUPS
2021
int i;
2122
static int ngroups;
@@ -25,19 +26,23 @@ static Boolean ingroupset(int gid) {
2526
initialized = TRUE;
2627
ngroups = getgroups(NGROUPS, gidset);
2728
}
28-
for (i = 0; i < ngroups; i++)
29-
if (gid == gidset[i])
29+
for (i = 0; i < ngroups; i++) {
30+
if (gid == gidset[i]) {
3031
return TRUE;
32+
}
33+
}
3134
#endif
3235
return FALSE;
3336
}
3437

35-
static int testperm(struct stat *stat, int perm) {
38+
static int
39+
testperm(struct stat *stat, int perm) {
3640
int mask;
37-
static int uid, gid;
41+
static uid_t uid, gid;
3842
static Boolean initialized = FALSE;
39-
if (perm == 0)
43+
if (perm == 0) {
4044
return 0;
45+
}
4146
if (!initialized) {
4247
initialized = TRUE;
4348
uid = geteuid();
@@ -54,31 +59,38 @@ static int testperm(struct stat *stat, int perm) {
5459
return (stat->st_mode & mask) ? 0 : EACCES;
5560
}
5661

57-
static int testfile(char *path, int perm, int type) {
62+
static int
63+
testfile(char *path, int perm, int type) {
5864
struct stat st;
5965
#ifdef S_IFLNK
6066
if (type == S_IFLNK) {
61-
if (lstat(path, &st) == -1)
67+
if (lstat(path, &st) == -1) {
6268
return errno;
69+
}
6370
} else
6471
#endif
65-
if (stat(path, &st) == -1)
72+
if (stat(path, &st) == -1) {
6673
return errno;
67-
if (type != 0 && (st.st_mode & S_IFMT) != type)
74+
}
75+
if (type != 0 && (st.st_mode & S_IFMT) != type) {
6876
return EACCES; /* what is an appropriate return value? */
77+
}
6978
return testperm(&st, perm);
7079
}
7180

72-
static char *pathcat(char *prefix, char *suffix) {
81+
static char
82+
*pathcat(char *prefix, char *suffix) {
7383
char *s;
7484
size_t plen, slen, len;
7585
static char *pathbuf = NULL;
7686
static size_t pathlen = 0;
7787

78-
if (*prefix == '\0')
88+
if (*prefix == '\0') {
7989
return suffix;
80-
if (*suffix == '\0')
90+
}
91+
if (*suffix == '\0') {
8192
return prefix;
93+
}
8294

8395
plen = strlen(prefix);
8496
slen = strlen(suffix);
@@ -90,8 +102,9 @@ static char *pathcat(char *prefix, char *suffix) {
90102

91103
memcpy(pathbuf, prefix, plen);
92104
s = pathbuf + plen;
93-
if (s[-1] != '/')
105+
if (s[-1] != '/') {
94106
*s++ = '/';
107+
}
95108
memcpy(s, suffix, slen + 1);
96109
return pathbuf;
97110
}
@@ -106,7 +119,7 @@ static List
106119

107120
gcdisable();
108121
esoptbegin(list, "$&access", usage);
109-
while ((c = esopt("bcdefln:prswx1")) != EOF)
122+
while ((c = esopt("bcdefln:prswx1")) != EOF) {
110123
switch (c) {
111124
case 'n': suffix = getstr(esoptarg()); break;
112125
case '1': first = TRUE; break;
@@ -131,15 +144,17 @@ static List
131144
esoptend();
132145
fail("$&access", "access -%c is not supported on this system", c);
133146
}
147+
}
134148
list = esoptend();
135149

136150
for (lp = NULL; list != NULL; list = list->next) {
137151
int error;
138152
char *name;
139153

140154
name = getstr(list->term);
141-
if (suffix != NULL)
155+
if (suffix != NULL) {
142156
name = pathcat(name, suffix);
157+
}
143158
error = testfile(name, perm, type);
144159

145160
if (first) {
@@ -151,32 +166,39 @@ static List
151166
NULL));
152167
gcenable();
153168
RefReturn(result);
154-
} else if (error != ENOENT)
155-
estatus = error;
156-
} else
169+
} else {
170+
if (error != ENOENT) {
171+
estatus = error;
172+
}
173+
}
174+
} else {
157175
lp = mklist(mkstr(error == 0 ? "0" : esstrerror(error)),
158176
lp);
177+
}
159178
}
160179

161180
if (first && exception) {
162181
gcenable();
163-
if (suffix)
182+
if (suffix) {
164183
fail("$&access", "%s: %s", suffix, esstrerror(estatus));
165-
else
184+
} else {
166185
fail("$&access", "%s", esstrerror(estatus));
186+
}
167187
}
168188

169189
Ref(List *, result, reverse(lp));
170190
gcenable();
171191
RefReturn(result);
172192
}
173193

174-
extern Dict *initprims_access(Dict *primdict) {
194+
extern Dict
195+
*initprims_access(Dict *primdict) {
175196
primdict = dictput(primdict, STRING(access), (void *)prim_access);
176197
return primdict;
177198
}
178199

179-
extern char *checkexecutable(char *file) {
200+
extern char
201+
*checkexecutable(char *file) {
180202
int err = testfile(file, EXEC, S_IFREG);
181203
return err == 0 ? NULL : esstrerror(err);
182204
}

closure.c

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
DefineTag(Closure, static);
1111

12-
extern Closure *mkclosure(Tree *tree, Binding *binding) {
12+
extern Closure
13+
*mkclosure(Tree *tree, Binding *binding) {
1314
gcdisable();
1415
Ref(Closure *, closure, gcnew(Closure));
1516
closure->tree = tree;
@@ -18,24 +19,28 @@ extern Closure *mkclosure(Tree *tree, Binding *binding) {
1819
RefReturn(closure);
1920
}
2021

21-
static void *ClosureCopy(void *op) {
22+
static void
23+
*ClosureCopy(void *op) {
2224
void *np = gcnew(Closure);
2325
memcpy(np, op, sizeof (Closure));
2426
return np;
2527
}
2628

27-
static size_t ClosureScan(void *p) {
29+
static size_t
30+
ClosureScan(void *p) {
2831
Closure *closure = p;
2932
closure->tree = forward(closure->tree);
3033
closure->binding = forward(closure->binding);
3134
return sizeof (Closure);
3235
}
3336

3437
/* revtree -- destructively reverse a list stored in a tree */
35-
static Tree *revtree(Tree *tree) {
38+
static Tree
39+
*revtree(Tree *tree) {
3640
Tree *prev, *next;
37-
if (tree == NULL)
41+
if (tree == NULL) {
3842
return NULL;
43+
}
3944
prev = NULL;
4045
do {
4146
assert(tree->kind == nList);
@@ -54,10 +59,11 @@ typedef struct Chain {
5459
/* Internal initialization of the chain */
5560
static Chain *chain = NULL;
5661

57-
static Binding *extract(Tree *tree, Binding *bindings) {
62+
static Binding
63+
*extract(Tree *tree, Binding *bindings) {
5864
assert(gcisblocked());
5965

60-
/*
66+
/*
6167
* Tree layout:
6268
* Nodekind kind;
6369
* union { Tree *p; char *s; int i; } u[2];
@@ -94,16 +100,18 @@ static Binding *extract(Tree *tree, Binding *bindings) {
94100
fail("$&parse", "bad count in $&nestedbinding: %d", count);
95101
NOTREACHED;
96102
}
97-
if (i == count)
103+
if (i == count) {
98104
break;
105+
}
99106
}
100107
term = mkterm(NULL, cp->closure);
101108
} else {
102109
fail("$&parse", "bad unquoted primitive in %%closure: $&%s", prim);
103110
NOTREACHED;
104111
}
105-
} else
112+
} else {
106113
term = mkstr(word->u[0].s);
114+
}
107115
list = mklist(term, list);
108116
}
109117
bindings = mkbinding(name->u[0].s, list, bindings);
@@ -113,15 +121,17 @@ static Binding *extract(Tree *tree, Binding *bindings) {
113121
return bindings;
114122
}
115123

116-
extern Closure *extractbindings(Tree *tree0) {
124+
extern Closure
125+
*extractbindings(Tree *tree0) {
117126
Chain me;
118127
Tree *volatile tree = tree0;
119128
Binding *volatile bindings = NULL;
120129

121130
gcdisable();
122131

123-
if (tree->kind == nList && tree->u[1].p == NULL)
124-
tree = tree->u[0].p;
132+
if (tree->kind == nList && tree->u[1].p == NULL) {
133+
tree = tree->u[0].p;
134+
}
125135

126136
me.closure = mkclosure(NULL, NULL);
127137
me.next = chain;
@@ -132,8 +142,9 @@ extern Closure *extractbindings(Tree *tree0) {
132142
while (tree->kind == nClosure) {
133143
bindings = extract(tree->u[0].p, bindings);
134144
tree = tree->u[1].p;
135-
if (tree->kind == nList && tree->u[1].p == NULL)
136-
tree = tree->u[0].p;
145+
if (tree->kind == nList && tree->u[1].p == NULL) {
146+
tree = tree->u[0].p;
147+
}
137148
}
138149

139150
CatchException (e)
@@ -159,7 +170,8 @@ extern Closure *extractbindings(Tree *tree0) {
159170

160171
DefineTag(Binding, static);
161172

162-
extern Binding *mkbinding(char *name, List *defn, Binding *next) {
173+
extern Binding
174+
*mkbinding(char *name, List *defn, Binding *next) {
163175
assert(next == NULL || next->name != NULL);
164176
validatevar(name);
165177
gcdisable();
@@ -171,10 +183,11 @@ extern Binding *mkbinding(char *name, List *defn, Binding *next) {
171183
RefReturn(binding);
172184
}
173185

174-
extern Binding *reversebindings(Binding *binding) {
175-
if (binding == NULL)
186+
extern Binding
187+
*reversebindings(Binding *binding) {
188+
if (binding == NULL) {
176189
return NULL;
177-
else {
190+
} else {
178191
Binding *prev, *next;
179192
prev = NULL;
180193
do {
@@ -186,13 +199,15 @@ extern Binding *reversebindings(Binding *binding) {
186199
}
187200
}
188201

189-
static void *BindingCopy(void *op) {
202+
static void
203+
*BindingCopy(void *op) {
190204
void *np = gcnew(Binding);
191205
memcpy(np, op, sizeof (Binding));
192206
return np;
193207
}
194208

195-
static size_t BindingScan(void *p) {
209+
static size_t
210+
BindingScan(void *p) {
196211
Binding *binding = p;
197212
binding->name = forward(binding->name);
198213
binding->defn = forward(binding->defn);

0 commit comments

Comments
 (0)