Skip to content

Commit b2ca9cb

Browse files
committed
Adopt clang-format for code formatting
1 parent a4b7754 commit b2ca9cb

7 files changed

Lines changed: 162 additions & 3 deletions

File tree

.clang-format

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
BasedOnStyle: LLVM
3+
Language: Cpp
4+
5+
UseTab: Never
6+
IndentWidth: 2
7+
ColumnLimit: 0
8+
SortIncludes: Never
9+
Cpp11BracedListStyle: false
10+
SpaceAfterCStyleCast: true
11+
AllowShortIfStatementsOnASingleLine: Always

.github/workflows/clang-format.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: C Code Linting
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.c'
7+
- '**.h'
8+
- '.clang-format'
9+
- '.github/workflows/clang-format.yml'
10+
pull_request:
11+
paths:
12+
- '**.c'
13+
- '**.h'
14+
- '.clang-format'
15+
- '.github/workflows/clang-format.yml'
16+
17+
jobs:
18+
format-check:
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- uses: actions/checkout@v3
23+
24+
- name: Install clang-format
25+
run: sudo apt-get install -y clang-format
26+
27+
- name: Setup Ruby
28+
uses: ruby/setup-ruby@v1
29+
with:
30+
ruby-version: '3.4'
31+
bundler-cache: true
32+
33+
- name: Check C code linting
34+
run: bundle exec rake format:c_check

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"llvm-vs-code-extensions.vscode-clangd"
4+
]
5+
}

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ GEM
125125
rubocop-ast (1.44.1)
126126
parser (>= 3.3.7.2)
127127
prism (~> 1.4)
128-
rubocop-on-rbs (1.5.0)
128+
rubocop-on-rbs (1.6.0)
129129
lint_roller (~> 1.1)
130130
rbs (~> 3.5)
131131
rubocop (>= 1.72.1, < 2.0)
@@ -157,7 +157,7 @@ GEM
157157
uri (>= 0.12.0)
158158
stringio (3.1.7)
159159
strong_json (2.1.2)
160-
strscan (3.1.3)
160+
strscan (3.1.4)
161161
tempfile (0.3.1)
162162
terminal-table (4.0.0)
163163
unicode-display_width (>= 1.1.1, < 4)

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,43 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
198198

199199
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
200200

201+
### C Code Formatting
202+
203+
This project uses `clang-format` to enforce consistent formatting of C code with a `.clang-format` configuration in the root directory.
204+
205+
#### Setup
206+
207+
First, install clang-format:
208+
209+
```bash
210+
# macOS
211+
brew install clang-format
212+
213+
# Ubuntu/Debian
214+
sudo apt-get install clang-format
215+
216+
# Windows
217+
choco install llvm
218+
```
219+
220+
#### Usage
221+
222+
Format all C source files:
223+
224+
```bash
225+
rake format:c
226+
```
227+
228+
Check formatting without making changes:
229+
230+
```bash
231+
rake format:c_check
232+
```
233+
234+
#### Editor Integration
235+
236+
For VS Code users, install the "clangd" extension which will automatically use the project's `.clang-format` file.
237+
201238
## Contributing
202239

203240
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/rbs.

Rakefile

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,78 @@ task :confirm_templates => :templates do
4949
sh "git diff --exit-code -- include src"
5050
end
5151

52+
# Task to format C code using clang-format
53+
namespace :format do
54+
dirs = ["src", "ext", "include"]
55+
excluded_files = ["ext/rbs_extension/lexer.c"]
56+
57+
# Find all C source and header files, then filter out excluded files
58+
files = `find #{dirs.join(" ")} -type f -name "*.c" -o -name "*.h"`.split("\n").reject do |file|
59+
excluded_files.include?(file)
60+
end
61+
62+
desc "Format C source files using clang-format"
63+
task :c do
64+
puts "Formatting C files..."
65+
66+
# Check if clang-format is installed
67+
unless system("which clang-format > /dev/null 2>&1")
68+
abort "Error: clang-format not found. Please install clang-format first."
69+
end
70+
71+
if files.empty?
72+
puts "No C files found to format"
73+
next
74+
end
75+
76+
puts "Found #{files.length} files to format (excluding generated files)"
77+
78+
exit_status = 0
79+
files.each do |file|
80+
puts "Formatting #{file}"
81+
unless system("clang-format -i -style=file #{file}")
82+
puts "❌ Error formatting #{file}"
83+
exit_status = 1
84+
end
85+
end
86+
87+
exit exit_status unless exit_status == 0
88+
puts "✅ All files formatted successfully"
89+
end
90+
91+
desc "Check if C source files are properly formatted"
92+
task :c_check do
93+
puts "Checking C file formatting..."
94+
95+
# Check if clang-format is installed
96+
unless system("which clang-format > /dev/null 2>&1")
97+
abort "Error: clang-format not found. Please install clang-format first."
98+
end
99+
100+
if files.empty?
101+
puts "No C files found to check"
102+
next
103+
end
104+
105+
puts "Found #{files.length} files to check (excluding generated files)"
106+
107+
needs_format = false
108+
files.each do |file|
109+
unless system("clang-format --dry-run -Werror -style=file #{file} >/dev/null 2>&1")
110+
puts "❌ #{file} needs formatting"
111+
needs_format = true
112+
end
113+
end
114+
115+
if needs_format
116+
warn "Some files need formatting. Run 'rake format:c' to format them."
117+
exit 1
118+
else
119+
puts "✅ All files are properly formatted"
120+
end
121+
end
122+
end
123+
52124
rule ".c" => ".re" do |t|
53125
puts "⚠️⚠️⚠️ #{t.name} is older than #{t.source}. You may need to run `rake lexer` ⚠️⚠️⚠️"
54126
end

ext/rbs_extension/location.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static VALUE location_aref(VALUE self, VALUE name) {
257257
}
258258

259259
static VALUE rbs_constant_to_ruby_symbol(rbs_constant_t *constant) {
260-
return ID2SYM(rb_intern2((const char *) constant->start, constant->length));
260+
return ID2SYM(rb_intern2((const char *)constant->start, constant->length));
261261
}
262262

263263
static VALUE location_optional_keys(VALUE self) {

0 commit comments

Comments
 (0)