From 2c3de0bdb63f933fecf7cf717396a16a29e38e10 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sat, 10 Jul 2021 20:10:37 +0200 Subject: implement a basic linter Currently it just checks if the kernel doesn't accidentally use arch-dependent headers. --- Makefile | 5 ++++- tools/linter/main.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100755 tools/linter/main.rb diff --git a/Makefile b/Makefile index 4441263..1d5571a 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ out/obj/%.c.o: src/%.c $(CC) $(CFLAGS) -c $^ -o $@ -.PHONY: boot debug clean +.PHONY: boot debug lint clean boot: out/fs/boot/kernel.bin qemu-system-i386 -kernel $< $(QFLAGS) -no-shutdown @@ -39,5 +39,8 @@ debug: out/fs/boot/kernel.bin sleep 1 gdb +lint: + @tools/linter/main.rb + clean: rm -rv out/ diff --git a/tools/linter/main.rb b/tools/linter/main.rb new file mode 100755 index 0000000..1198fd1 --- /dev/null +++ b/tools/linter/main.rb @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby +# must be run in the root directory of the repo +# currently it only checks if code in src/kernel/ uses anything arch-dependent + +$return = 0 + +def warn(msg) + STDERR.puts "[LINTER] #{msg}" + $return = 1 +end + +whitelist = ["arch/generic.h"] + +Dir["src/kernel/**.?"] + .each do |path| + File.read(path) + .lines + .map(&:strip) # strip whitespace + .filter{|line| line[0] == '#'} # find preprocessor directives + .map{|line| line[1..].strip} # get rid of the #, strip again + .filter{|l| l.start_with? "include"} # find includes + .map{|l| /([<"](.+)[">])/.match(l)[2]} # get the name of the included file + .filter{|l| l.start_with? "arch/"} # find files in arch/ + .filter{|l| not whitelist.include? l} # filter out whitelisted headers + .each{|inc| warn "#{path} includes #{inc}"} + end + +exit $return -- cgit v1.2.3