Skip to content

Commit c6ca318

Browse files
committed
flymake-verilator.el is roughly finished.
0 parents  commit c6ca318

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.*~

README.org

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
* flymake-verilator.el
2+
3+
An Emacs flymake handler for syntax-checking Verilog using =verilator=.
4+
5+
** Installation
6+
7+
First install =verilator=.
8+
9+
You'll need to add the directory containing `flymake-json.el` to your `load-path`,
10+
and then ~(require 'flymake-verilator)~.
11+
12+
** Usage
13+
14+
Add the following to your emacs init file:
15+
16+
~(require 'flymake-verilator)~

flymake-verilator.el

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
;;; flymake-verilator --- Flymake for Verilog -*- lexical-binding: t; -*-
2+
;;; Commentary:
3+
;;; Code:
4+
5+
(require 'flymake)
6+
7+
(defvar verilog--flymake-proc nil
8+
"A flymake verilog process.")
9+
10+
(defvar verilog-flymake-command '("verilator" "--lint-only" "-Wall")
11+
"Command for verilog's flymake.")
12+
13+
(defvar verilog--flymake-output-buffer " *stderr of verilog-flymake*"
14+
"Buffer for verilog's flymake output.")
15+
16+
(defun verilog-flymake-done (report-fn
17+
source-buffer
18+
output-buffer)
19+
(with-current-buffer source-buffer
20+
(save-excursion
21+
(save-restriction
22+
(with-current-buffer output-buffer
23+
(goto-char (point-min))
24+
(let ((diags))
25+
(while (search-forward-regexp
26+
"^\\(%.*\\): .*:\\([0-9]+\\):\\([0-9]+\\): \\(.*\\)$"
27+
nil t)
28+
(let* ((msg (match-string 4))
29+
(level-msg (match-string 1))
30+
(line (string-to-number (match-string 2)))
31+
(column (string-to-number (match-string 3)))
32+
(beg)
33+
(end)
34+
(level))
35+
(setq level (cond
36+
((string-match-p "%Error" level-msg) ':error)
37+
((string-match-p "%Warning" level-msg) ':warning)
38+
(t :note)))
39+
(setq beg (with-current-buffer source-buffer
40+
(save-excursion
41+
(save-restriction
42+
(goto-char (point-min))
43+
(or (equal line 1)
44+
(forward-line (- line 1)))
45+
(- (+ (point) column) 1)))))
46+
(setq end (if (equal level ':error)
47+
(+ beg 1)
48+
(+ beg (- (length msg) (string-match ": '" msg) 4))))
49+
(setq diags
50+
(cons (flymake-make-diagnostic
51+
source-buffer beg end level msg)
52+
diags))
53+
))
54+
(funcall report-fn diags)
55+
))))))
56+
57+
(defun verilog-flymake-detect (report-fn &rest _args)
58+
"A Flymake backend for verilog.
59+
Spawn an verilator process that byte-compiles a file representing the
60+
current buffer state and calls REPORT-FN when done."
61+
(when verilog--flymake-proc
62+
(when (process-live-p verilog--flymake-proc)
63+
(kill-process verilog--flymake-proc)))
64+
(let ((source-buffer (current-buffer))
65+
(coding-system-for-write 'utf-8-unix)
66+
(coding-system-for-read 'utf-8))
67+
(save-restriction
68+
(widen)
69+
(let* ((output-buffer (generate-new-buffer " *verilog-flymake*")))
70+
(setq verilog--flymake-proc
71+
(make-process
72+
:name "verilog-flymake-process"
73+
:buffer output-buffer
74+
:command (append verilog-flymake-command
75+
(list (buffer-file-name source-buffer)))
76+
:connection-type 'pipe
77+
:sentinel
78+
(lambda (proc _event)
79+
(unless (process-live-p proc)
80+
(unwind-protect
81+
(cond
82+
((not (and (buffer-live-p source-buffer)
83+
(eq proc (with-current-buffer source-buffer
84+
verilog--flymake-proc))))
85+
(flymake-log :warning
86+
"verilog-flymake process %s obsolete" proc))
87+
((memq (process-status proc) '(exit signal))
88+
(verilog-flymake-done report-fn
89+
source-buffer
90+
verilog--flymake-output-buffer
91+
))
92+
(t
93+
(funcall report-fn
94+
:panic
95+
:explanation
96+
(format "process %s died" proc))))
97+
(kill-buffer output-buffer)
98+
(kill-buffer verilog--flymake-output-buffer)
99+
)))
100+
:stderr verilog--flymake-output-buffer
101+
:noquery t))))))
102+
103+
(defun verilog-setup-flymake-backend ()
104+
"Setup `flymake-verilator' for verilog buffer."
105+
(add-hook 'flymake-diagnostic-functions 'verilog-flymake-detect nil t))
106+
107+
(add-hook 'verilog-mode-hook 'verilog-setup-flymake-backend)
108+
109+
(provide 'flymake-verilator)
110+
;;; flymake-verilator.el ends here.

0 commit comments

Comments
 (0)