Skip to content

Commit 1496c52

Browse files
authored
add linter (#4)
1 parent 2daa9c7 commit 1496c52

File tree

9 files changed

+204
-137
lines changed

9 files changed

+204
-137
lines changed

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BasedOnStyle: Google
2+
SortIncludes: Never

.github/workflows/linter.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Lint
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened, ready_for_review]
6+
paths-ignore:
7+
- '**.md'
8+
- 'docs/**'
9+
push:
10+
branches:
11+
- main
12+
paths-ignore:
13+
- '**.md'
14+
- 'docs/**'
15+
16+
permissions:
17+
contents: read
18+
19+
concurrency:
20+
group: ${{ github.workflow }}-${{ github.ref }}
21+
cancel-in-progress: true
22+
23+
jobs:
24+
lint:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
28+
29+
- name: Run clang-format
30+
uses: jidicula/clang-format-action@f62da5e3d3a2d88ff364771d9d938773a618ab5e # v4.11.0
31+
with:
32+
clang-format-version: '17'
33+
fallback-style: 'Google'

benchmarks/benchmark.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
#include "performancecounters/benchmarker.h"
3+
#include "version_weaver.h"
34
#include <algorithm>
45
#include <charconv>
56
#include <filesystem>
@@ -8,13 +9,13 @@
89
#include <random>
910
#include <stdlib.h>
1011
#include <vector>
11-
#include "version_weaver.h"
1212

1313
void pretty_print(size_t volume, size_t bytes, std::string name,
1414
event_aggregate agg, bool display = true) {
1515
printf("%-40s : ", name.c_str());
1616
printf(" %5.2f Gi/s ", volume / agg.fastest_elapsed_ns());
17-
double range = (agg.elapsed_ns() - agg.fastest_elapsed_ns())/agg.elapsed_ns() * 100.0;
17+
double range =
18+
(agg.elapsed_ns() - agg.fastest_elapsed_ns()) / agg.elapsed_ns() * 100.0;
1819
printf(" %5.2f GB/s (%2.0f %%) ", bytes / agg.fastest_elapsed_ns(), range);
1920
if (collector.has_events()) {
2021
printf(" %5.2f GHz ", agg.fastest_cycles() / agg.fastest_elapsed_ns());
@@ -32,20 +33,22 @@ void bench(const std::vector<std::string> &input) {
3233
bytes += input[i].size();
3334
}
3435
std::cout << "volume : " << volume << " strings" << std::endl;
35-
std::cout << "volume : " << bytes / 1024 / 1024.
36-
<< " MB" << std::endl;
36+
std::cout << "volume : " << bytes / 1024 / 1024. << " MB" << std::endl;
3737
volatile size_t sum = 0;
3838

3939
size_t min_repeat = 10;
4040
size_t min_time_ns = 10000000000;
4141
size_t max_repeat = 100000;
4242
pretty_print(volume, bytes, "validate",
43-
bench([&input,&sum]() { for(std::string_view v : input) { sum += version_weaver::validate(v); } },
44-
min_repeat, min_time_ns, max_repeat));
45-
43+
bench(
44+
[&input, &sum]() {
45+
for (std::string_view v : input) {
46+
sum += version_weaver::validate(v);
47+
}
48+
},
49+
min_repeat, min_time_ns, max_repeat));
4650
}
4751

48-
4952
int main(int argc, char **argv) {
5053
bench({"1.2.4", "13.4.1"});
5154
return EXIT_SUCCESS;

benchmarks/performancecounters/linux-perf-events.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
#pragma once
22
#ifdef __linux__
33

4-
#include <asm/unistd.h> // for __NR_perf_event_open
5-
#include <linux/perf_event.h> // for perf event constants
6-
#include <sys/ioctl.h> // for ioctl
7-
#include <unistd.h> // for syscall
4+
#include <asm/unistd.h> // for __NR_perf_event_open
5+
#include <linux/perf_event.h> // for perf event constants
6+
#include <sys/ioctl.h> // for ioctl
7+
#include <unistd.h> // for syscall
88

9-
#include <cerrno> // for errno
10-
#include <cstring> // for memset
9+
#include <cerrno> // for errno
10+
#include <cstring> // for memset
1111
#include <stdexcept>
1212

1313
#include <iostream>
1414
#include <vector>
1515

16-
template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents {
16+
template <int TYPE = PERF_TYPE_HARDWARE>
17+
class LinuxEvents {
1718
int fd;
1819
bool working;
1920
perf_event_attr attribs{};
2021
size_t num_events{};
2122
std::vector<uint64_t> temp_result_vec{};
2223
std::vector<uint64_t> ids{};
2324

24-
public:
25+
public:
2526
explicit LinuxEvents(std::vector<int> config_vec) : fd(0), working(true) {
2627
memset(&attribs, 0, sizeof(attribs));
2728
attribs.type = TYPE;
@@ -32,11 +33,11 @@ template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents {
3233

3334
attribs.sample_period = 0;
3435
attribs.read_format = PERF_FORMAT_GROUP | PERF_FORMAT_ID;
35-
const int pid = 0; // the current process
36-
const int cpu = -1; // all CPUs
36+
const int pid = 0; // the current process
37+
const int cpu = -1; // all CPUs
3738
const unsigned long flags = 0;
3839

39-
int group = -1; // no group
40+
int group = -1; // no group
4041
num_events = config_vec.size();
4142
ids.resize(config_vec.size());
4243
uint32_t i = 0;
@@ -98,7 +99,7 @@ template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents {
9899

99100
bool is_working() { return working; }
100101

101-
private:
102+
private:
102103
void report_error(const std::string &) { working = false; }
103104
};
104105
#endif

clang-format-ignore.txt

Whitespace-only changes.

include/version_weaver.h

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,64 @@
11
#ifndef VERSION_WEAVER_H
22
#define VERSION_WEAVER_H
33
#include <optional>
4-
#include <string_view>
54
#include <string>
5+
#include <string_view>
66
#include <variant>
77

88
namespace version_weaver {
99

10-
// https://semver.org/#does-semver-have-a-size-limit-on-the-version-string
11-
static constexpr size_t MAX_VERSION_LENGTH = 256;
12-
13-
bool validate(std::string_view version);
14-
bool gt(std::string_view version1, std::string_view version2);
15-
bool lt(std::string_view version1, std::string_view version2);
16-
bool satisfies(std::string_view version, std::string_view range);
17-
std::string coerce(std::string_view version);
18-
std::string minimum(std::string_view range);
19-
std::string clean(std::string_view range);
20-
21-
// A normal version number MUST take the form X.Y.Z where X, Y, and Z are
22-
// non-negative integers, and MUST NOT contain leading zeroes.
23-
// X is the major version, Y is the minor version, and Z is the patch version.
24-
// Each element MUST increase numerically.
25-
// For instance: 1.9.0 -> 1.10.0 -> 1.11.0.
26-
struct Version {
27-
28-
std::string_view major;
29-
std::string_view minor;
30-
std::string_view patch;
31-
32-
// A pre-release version MAY be denoted by appending a hyphen and a series
33-
// of dot separated identifiers immediately following the patch version.
34-
// - Identifiers MUST comprise only ASCII alphanumerics and hyphens [0-9A-Za-z-].
35-
// - Identifiers MUST NOT be empty.
36-
// - Numeric identifiers MUST NOT include leading zeroes.
37-
//
38-
// Examples: 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-0.3.7, 1.0.0-x.7.z.92, 1.0.0-x-y-z.--.
39-
std::optional<std::string_view> pre_release;
40-
41-
// Build metadata MAY be denoted by appending a plus sign and a series of dot
42-
// separated identifiers immediately following the patch or pre-release version.
43-
// - Identifiers MUST comprise only ASCII alphanumerics and hyphens [0-9A-Za-z-].
44-
// - Identifiers MUST NOT be empty.
45-
// Build metadata MUST be ignored when determining version precedence.
46-
// Thus two versions that differ only in the build metadata, have the same precedence.
47-
//
48-
// Examples: 1.0.0-alpha+001, 1.0.0+20130313144700, 1.0.0-beta+exp.sha.5114f85, 1.0.0+21AF26D3----117B344092BD.
49-
std::optional<std::string_view> build;
50-
};
51-
52-
enum ParseError {
53-
VERSION_LARGER_THAN_MAX_LENGTH,
54-
INVALID_INPUT,
55-
};
56-
57-
std::variant<Version, ParseError> parse(std::string_view version);
58-
}
59-
60-
#endif // VERSION_WEAVER_H
10+
// https://semver.org/#does-semver-have-a-size-limit-on-the-version-string
11+
static constexpr size_t MAX_VERSION_LENGTH = 256;
12+
13+
bool validate(std::string_view version);
14+
bool gt(std::string_view version1, std::string_view version2);
15+
bool lt(std::string_view version1, std::string_view version2);
16+
bool satisfies(std::string_view version, std::string_view range);
17+
std::string coerce(std::string_view version);
18+
std::string minimum(std::string_view range);
19+
std::string clean(std::string_view range);
20+
21+
// A normal version number MUST take the form X.Y.Z where X, Y, and Z are
22+
// non-negative integers, and MUST NOT contain leading zeroes.
23+
// X is the major version, Y is the minor version, and Z is the patch version.
24+
// Each element MUST increase numerically.
25+
// For instance: 1.9.0 -> 1.10.0 -> 1.11.0.
26+
struct Version {
27+
std::string_view major;
28+
std::string_view minor;
29+
std::string_view patch;
30+
31+
// A pre-release version MAY be denoted by appending a hyphen and a series
32+
// of dot separated identifiers immediately following the patch version.
33+
// - Identifiers MUST comprise only ASCII alphanumerics and hyphens
34+
// [0-9A-Za-z-].
35+
// - Identifiers MUST NOT be empty.
36+
// - Numeric identifiers MUST NOT include leading zeroes.
37+
//
38+
// Examples: 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-0.3.7, 1.0.0-x.7.z.92, 1.0.0-x-y-z.--.
39+
std::optional<std::string_view> pre_release;
40+
41+
// Build metadata MAY be denoted by appending a plus sign and a series of dot
42+
// separated identifiers immediately following the patch or pre-release
43+
// version.
44+
// - Identifiers MUST comprise only ASCII alphanumerics and hyphens
45+
// [0-9A-Za-z-].
46+
// - Identifiers MUST NOT be empty.
47+
// Build metadata MUST be ignored when determining version precedence.
48+
// Thus two versions that differ only in the build metadata, have the same
49+
// precedence.
50+
//
51+
// Examples: 1.0.0-alpha+001, 1.0.0+20130313144700, 1.0.0-beta+exp.sha.5114f85,
52+
// 1.0.0+21AF26D3----117B344092BD.
53+
std::optional<std::string_view> build;
54+
};
55+
56+
enum ParseError {
57+
VERSION_LARGER_THAN_MAX_LENGTH,
58+
INVALID_INPUT,
59+
};
60+
61+
std::variant<Version, ParseError> parse(std::string_view version);
62+
} // namespace version_weaver
63+
64+
#endif // VERSION_WEAVER_H

0 commit comments

Comments
 (0)