Skip to content

Commit 225b745

Browse files
authored
Merge pull request #1112 from libcpr/feature/clang_tidy_format_update
Clang-{Tidy,Format} Update
2 parents 99f044e + b847b91 commit 225b745

File tree

9 files changed

+154
-12
lines changed

9 files changed

+154
-12
lines changed

.github/workflows/clang-format.yml

+2-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@ jobs:
99
steps:
1010
- name: Update package list
1111
run: sudo dnf update -y
12-
- name: Install dependencies
13-
run: sudo dnf install -y openssl-devel cmake git gcc clang ninja-build
14-
- name: Install clang-tidy
12+
- name: Install clang-format
1513
run: sudo dnf install -y clang-tools-extra
1614
- name: Checkout
1715
uses: actions/checkout@v3
1816
- name: Check format
19-
uses: RafikFarhad/[email protected]
20-
with:
21-
sources: "include/**/*.hpp,include/**/*.cpp,cpr/**/*.hpp,cpr/**/*.cpp"
22-
style: "file"
17+
run: bash scripts/check_clang_format.sh

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ _site/
4747
*.swp
4848

4949
# VSCode
50-
.vscode/
50+
.vscode/*
51+
!.vscode/tasks.json
5152
.vs/
53+
!.vs/tasks.json
5254

5355
# clangd
5456
.cache/

.vscode/tasks.json

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "🗑️ Delete build dir",
8+
"type": "shell",
9+
"command": "${workspaceFolder}/scripts/delete_build_dir.sh",
10+
"problemMatcher": [],
11+
"group": {
12+
"kind": "build"
13+
},
14+
"presentation": {
15+
"clear": true
16+
},
17+
"options": {
18+
"cwd": "${workspaceFolder}"
19+
}
20+
},
21+
{
22+
"label": "📝 Run clang-format",
23+
"type": "shell",
24+
"command": "${workspaceFolder}/scripts/run_clang_format.sh",
25+
"args": [
26+
"cpr",
27+
"include",
28+
"test"
29+
],
30+
"problemMatcher": [],
31+
"group": {
32+
"kind": "build"
33+
},
34+
"presentation": {
35+
"clear": true
36+
},
37+
"options": {
38+
"cwd": "${workspaceFolder}"
39+
}
40+
},
41+
{
42+
"label": "📑 Check clang-format",
43+
"type": "shell",
44+
"command": "${workspaceFolder}/scripts/check_clang_format.sh",
45+
"args": [
46+
"cpr",
47+
"include",
48+
"test"
49+
],
50+
"problemMatcher": [],
51+
"group": {
52+
"kind": "build"
53+
},
54+
"presentation": {
55+
"clear": true
56+
},
57+
"options": {
58+
"cwd": "${workspaceFolder}"
59+
}
60+
}
61+
]
62+
}

include/cpr/callback.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include "cprtypes.h"
55

66
#include <atomic>
7-
#include <functional>
87
#include <cstdint>
8+
#include <functional>
99
#include <optional>
1010
#include <utility>
1111

scripts/check_clang_format.sh

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
# Based on: https://gist.github.com/leilee/1d0915a583f8f29414cc21cd86e7151b
4+
# Checks if all files are formatted based on the clang-format formatting rules.
5+
# Execute as follows:
6+
# ./scripts/check_clang_format.sh tests src
7+
8+
printf "📑 Checking if your code fulfills all clang-format rules...\n"
9+
10+
RET_CODE=0
11+
12+
function format() {
13+
for f in $(find $@ -name '*.h' -or -name '*.hpp' -or -name '*.c' -or -name '*.cpp'); do
14+
clang-format -i --dry-run --Werror --style=file ${f};
15+
ret=$?
16+
if [ $ret -ne 0 ]; then
17+
RET_CODE=$ret
18+
fi
19+
done
20+
21+
echo "~~~ $@ directory checked ~~~";
22+
}
23+
24+
# Check all of the arguments first to make sure they're all directories
25+
for dir in "$@"; do
26+
if [ ! -d "${dir}" ]; then
27+
echo "${dir} is not a directory";
28+
else
29+
format ${dir};
30+
fi
31+
done
32+
33+
RED='\033[0;31m'
34+
GREEN='\033[0;32m'
35+
NC='\033[0m'
36+
37+
if [ $RET_CODE -eq 0 ]; then
38+
printf "${GREEN}Everything up to standard :party: ${NC}\n"
39+
else
40+
printf "${RED}Not up to formatting standard :sad_face: ${NC}\n"
41+
echo "Try running run_clang_format.sh to format all files."
42+
fi
43+
44+
exit $RET_CODE

scripts/delete_build_dir.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
printf "🗑️ Clearing your build directory...\n"
4+
rm -rf build/*
5+
6+
GREEN='\033[0;32m'
7+
NC='\033[0m'
8+
9+
printf "${GREEN}Done. Build directory deleted.${NC}\n"

scripts/run_clang_format.sh

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
# Based on: https://gist.github.com/leilee/1d0915a583f8f29414cc21cd86e7151b
4+
# Run from the project root directory as follows:
5+
# ./scripts/run_clang_format.sh tests src
6+
7+
printf "📝 Running clang-format...\n"
8+
9+
function format() {
10+
for f in $(find $@ -name '*.h' -or -name '*.hpp' -or -name '*.c' -or -name '*.cpp'); do
11+
echo "format ${f}";
12+
clang-format -i --style=file ${f};
13+
done
14+
15+
echo "~~~ $@ directory formatted ~~~";
16+
}
17+
18+
# Check all of the arguments first to make sure they're all directories
19+
for dir in "$@"; do
20+
if [ ! -d "${dir}" ]; then
21+
echo "${dir} is not a directory";
22+
else
23+
format ${dir};
24+
fi
25+
done
26+
27+
GREEN='\033[0;32m'
28+
NC='\033[0m'
29+
30+
printf "${GREEN}Done. All files were formatted (if required).${NC}\n"

test/abstractServer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ bool AbstractServer::IsConnectionActive(mg_mgr* mgr, mg_connection* conn) {
132132
return false;
133133
}
134134

135-
uint16_t AbstractServer::GetRemotePort(const mg_connection* conn) {
135+
uint16_t AbstractServer::GetRemotePort(const mg_connection* conn) {
136136
return (conn->rem.port >> 8) | (conn->rem.port << 8);
137137
}
138138

139-
uint16_t AbstractServer::GetLocalPort(const mg_connection* conn) {
139+
uint16_t AbstractServer::GetLocalPort(const mg_connection* conn) {
140140
return (conn->loc.port >> 8) | (conn->loc.port << 8);
141141
}
142142

test/resolve_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ TEST(ResolveTests, RedirectMultiple) {
2828
Resolve resolve2{"www.example1.com", "127.0.0.1", {server->GetPort()}};
2929

3030
Response response = cpr::Get(url1, std::vector<Resolve>{resolve1, resolve2}, Header{{"RedirectLocation", url2.str()}});
31-
31+
3232
std::string expected_text{"Hello world!"};
3333
EXPECT_EQ(expected_text, response.text);
3434
EXPECT_EQ(url2, response.url);

0 commit comments

Comments
 (0)