Skip to content

Commit 65c26a5

Browse files
Alex Hollanddadrian
authored andcommitted
Add formatting enhancements (zmap#501)
Add formatting check to TravisCI and rerun formatting script.
1 parent a2940af commit 65c26a5

File tree

8 files changed

+50
-17
lines changed

8 files changed

+50
-17
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ DerivePointerAlignment: false
88
PointerAlignment: Right
99
BreakStringLiterals: false
1010
SortIncludes: false
11-
11+
ReflowComments: false

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ addons:
2121
before_install:
2222
- pip install --user sh
2323
- export PATH=/opt/bin:$PATH
24+
before_script:
25+
- wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
26+
- echo "deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-6.0 main" | sudo tee -a /etc/apt/sources.list
27+
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
28+
- sudo apt-get update -qq
29+
- sudo apt-get install libstdc++-4.9-dev libllvm6.0 clang-format-6.0
2430
script:
2531
- mkdir __build
2632
- cd __build
2733
- cmake -DENABLE_DEVELOPMENT=ON -DWITH_REDIS=ON $TRAVIS_BUILD_DIR
2834
- make
2935
- cd $TRAVIS_BUILD_DIR
3036
- python ./scripts/check_manfile.py
37+
- ./checkFormat.sh
3138
compiler: clang
3239
notifications:
3340
email:

checkFormat.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
CLANG_FORMAT=clang-format-6.0
4+
5+
files_to_lint=$(find ./src ./lib -type f -name '*.c' -or -name '*.h')
6+
7+
fail=0
8+
for f in ${files_to_lint}; do
9+
d="$(diff -u "$f" <($CLANG_FORMAT -style=file "$f") || true)"
10+
if ! [ -z "$d" ]; then
11+
printf "The file %s is not compliant with the coding style:\n%s\n" "$f" "$d"
12+
fail=1
13+
fi
14+
done
15+
16+
if [ "$fail" -eq "1" ]; then
17+
if [ ! -z $ZMAP_ENFORCE_FORMAT ]; then
18+
exit 1
19+
fi
20+
fi

format.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ set -e
33
set -o pipefail
44

55
MAJOR_REV=$((clang-format --version | awk '{print $3}' | cut -d '.' -f 1) || echo 0)
6-
if [ $MAJOR_REV -ne 5 ]; then
7-
echo "error: need clang-format version 5.x"
6+
if [ $MAJOR_REV -lt 5 ]; then
7+
echo "error: need at least clang-format version 5.x"
88
exit 1
99
fi
1010
11-
FORMAT_CMD="clang-format -i"
11+
FORMAT_CMD="clang-format -i -style=file"
1212
1313
# No files passed, format everything
1414
if [ $# -eq 0 ]; then

src/probe_modules/module_dns.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ static int dns_global_initialize(struct state_conf *conf)
584584
qtypes = xmalloc(sizeof(uint16_t) * num_questions);
585585

586586
char *qtype_str = NULL;
587-
char **domains = (char**) xmalloc(sizeof(char *) * num_questions);
587+
char **domains = (char **)xmalloc(sizeof(char *) * num_questions);
588588

589589
for (int i = 0; i < num_questions; i++) {
590590
domains[i] = (char *)default_domain;

src/probe_modules/module_udp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ int udp_template_build(udp_payload_template_t *t, char *out, unsigned int len,
585585

586586
switch (c->ftype) {
587587

588-
// These fields have a specified output length value
588+
// These fields have a specified output length value
589589

590590
case UDP_DATA:
591591
if (!(c->data && c->length))
@@ -877,7 +877,7 @@ static fielddef_t fields[] = {
877877
probe_module_t module_udp = {
878878
.name = "udp",
879879
.packet_length = sizeof(struct ether_header) + sizeof(struct ip) +
880-
sizeof(struct udphdr) + MAX_UDP_PAYLOAD_LEN,
880+
sizeof(struct udphdr) + MAX_UDP_PAYLOAD_LEN,
881881
.pcap_filter = "udp || icmp",
882882
.pcap_snaplen = 1500,
883883
.port_args = 1,

src/send.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,19 @@ iterator_t *send_init(void)
138138

139139
// only allow bandwidth or rate
140140
if (zconf.bandwidth > 0 && zconf.rate > 0) {
141-
log_fatal("send", "Must specify rate or bandwidth, or neither, not both.");
141+
log_fatal(
142+
"send",
143+
"Must specify rate or bandwidth, or neither, not both.");
142144
}
143145
// convert specified bandwidth to packet rate
144146
if (zconf.bandwidth > 0) {
145147
size_t pkt_len = zconf.probe_module->packet_length;
146148
pkt_len *= 8;
147-
// 7 byte MAC preamble, 1 byte Start frame, 4 byte CRC, 12 byte inter-frame gap
149+
// 7 byte MAC preamble, 1 byte Start frame, 4 byte CRC, 12 byte
150+
// inter-frame gap
148151
pkt_len += 8 * 24;
149-
// adjust calculated length if less than the minimum size of an ethernet frame
152+
// adjust calculated length if less than the minimum size of an
153+
// ethernet frame
150154
if (pkt_len < 84 * 8) {
151155
pkt_len = 84 * 8;
152156
}
@@ -164,9 +168,10 @@ iterator_t *send_init(void)
164168
zconf.rate = 1;
165169
}
166170
}
167-
log_debug("send",
168-
"using bandwidth %lu bits/s for %zu byte probe, rate set to %d pkt/s",
169-
zconf.bandwidth, pkt_len/8, zconf.rate);
171+
log_debug(
172+
"send",
173+
"using bandwidth %lu bits/s for %zu byte probe, rate set to %d pkt/s",
174+
zconf.bandwidth, pkt_len / 8, zconf.rate);
170175
}
171176
// log rate, if explicitly specified
172177
if (zconf.rate <= 0) {
@@ -329,9 +334,10 @@ int send_run(sock_t st, shard_t *s)
329334
double t = now();
330335
assert(count > last_count);
331336
assert(t > last_time);
332-
double multiplier = (double)(count - last_count) /
333-
(t - last_time) /
334-
(zconf.rate / zconf.senders);
337+
double multiplier =
338+
(double)(count - last_count) /
339+
(t - last_time) /
340+
(zconf.rate / zconf.senders);
335341
uint32_t old_delay = delay;
336342
delay *= multiplier;
337343
if (delay == old_delay) {

src/topt_compat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#error "gcc version >= 4 is required"
33
#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 6
44
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
5-
#elif __GNUC_MINOR__ >= 4
5+
#elif __GNUC_MINOR__ >= 4
66
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
77
#endif
88

0 commit comments

Comments
 (0)