Skip to content

Commit 9a6fa83

Browse files
committed
Detect solc version and if >= 0.6.0 use --old-reporter
Fix #52 This patch makes it so that the solidity flycheck checker works properly with solc versions >= 0.6.0 Since the output of solc >= 0.6.0 differs quite a bit from the old versions we then utilize the `--old-reporter` argument which reverts to the previous form of output.
1 parent 20d7695 commit 9a6fa83

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

solidity-flycheck.el

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ Possible values are:
7676
:package-version '(solidity . "0.1.5"))
7777

7878
(flycheck-def-option-var flycheck-solidity-solium-soliumrcfile nil solium-check
79-
"The path to use for soliumrc.json
79+
"The path to use for soliumrc.json
8080
8181
The value of this variable is either a string denoting a path to the soliumrc.json
8282
or nil, to use the current directory. When non-nil,
8383
we pass the directory to solium via the `--config' option."
84-
:type '(choice (const :tag "No custom soliumrc" nil)
85-
(string :tag "Custom soliumrc file location"))
86-
:safe #'stringp
87-
:package-version '(solidity-mode . "0.1.4"))
84+
:type '(choice (const :tag "No custom soliumrc" nil)
85+
(string :tag "Custom soliumrc file location"))
86+
:safe #'stringp
87+
:package-version '(solidity-mode . "0.1.4"))
8888

8989
(when solidity-flycheck-solium-checker-active
9090
;; define solium flycheck syntax checker
@@ -126,28 +126,52 @@ we pass the directory to solium via the `--config' option."
126126
(setq flycheck-solium-checker-executable solidity-solium-path))
127127
(error (format "Solidity Mode Configuration error. Requested solium flycheck integration but can't find solium at: %s" solidity-solium-path)))))
128128

129+
(defun get-solc-version ()
130+
"Query solc executable and return its version.
131+
132+
The result is returned in a list with 3 elements.MAJOR MINOR PATCH.
133+
134+
If the solc output can't be parsed an error is returned."
135+
(let ((output (shell-command-to-string (format "%s --version" solidity-solc-path))))
136+
(if (string-match "Version: \\([[:digit:]]+\\)\.\\([[:digit:]]+\\)\.\\([[:digit:]]+\\)" output)
137+
(list (match-string 1 output)
138+
(match-string 2 output)
139+
(match-string 3 output))
140+
(error "Could not parse the output of %s --version:\n %s" solidity-solc-path output))))
141+
142+
(defun solc-gt-0.6.0 ()
143+
"Return `t` if solc >= 0.6.0 and `nil` otherwise."
144+
(let* ((version (get-solc-version))
145+
(major (string-to-number (nth 0 version)))
146+
(minor (string-to-number (nth 1 version)))
147+
(patch (string-to-number (nth 2 version))))
148+
(if (and (>= major 0) (>= minor 6)) t nil)))
149+
129150
(when solidity-flycheck-solc-checker-active
130151
;; add a solidity mode callback to set the executable of solc for flycheck
131152
;; define solidity's flycheck syntax checker
132153
;; expanded the flycheck-define-checker macro in order to eval certain args, as per advice given in gitter
133154
;; https://gitter.im/flycheck/flycheck?at=5a43b3a8232e79134d98872b
134155
(flycheck-def-executable-var solidity-checker "solc")
135-
(if (funcall flycheck-executable-find solidity-solc-path)
136-
(progn
137-
(flycheck-define-command-checker 'solidity-checker
138-
"A Solidity syntax checker using the solc compiler"
139-
:command '("solc" source-inplace)
140-
:error-patterns '((error line-start (file-name) ":" line ":" column ":" " Error: " (message))
141-
(error line-start "Error: " (message))
142-
(warning line-start (file-name) ":" line ":" column ":" " Warning: " (message)))
143-
:modes 'solidity-mode
144-
:predicate #'(lambda nil (eq major-mode 'solidity-mode))
145-
:next-checkers `((,solidity-flycheck-chaining-error-level . solium-checker))
146-
:standard-input 'nil
147-
:working-directory 'nil)
148-
(add-to-list 'flycheck-checkers 'solidity-checker)
149-
(setq flycheck-solidity-checker-executable solidity-solc-path))
150-
(error (format "Solidity Mode Configuration error. Requested solc flycheck integration but can't find solc at: %s" solidity-solc-path))))
156+
(let* ((cmd (if (solc-gt-0.6.0) '("solc" "--old-reporter" source-inplace) '("solc" source-inplace))))
157+
(if (funcall flycheck-executable-find solidity-solc-path)
158+
(progn
159+
(flycheck-define-command-checker 'solidity-checker
160+
"A Solidity syntax checker using the solc compiler"
161+
:command cmd
162+
:error-patterns '(
163+
(error line-start (file-name) ":" line ":" column ":" " Error: " (message))
164+
(error line-start (file-name) ":" line ":" column ":" " Compiler error: " (message))
165+
(error line-start "Error: " (message))
166+
(warning line-start (file-name) ":" line ":" column ":" " Warning: " (message)))
167+
:modes 'solidity-mode
168+
:predicate #'(lambda nil (eq major-mode 'solidity-mode))
169+
:next-checkers `((,solidity-flycheck-chaining-error-level . solium-checker))
170+
:standard-input 'nil
171+
:working-directory 'nil)
172+
(add-to-list 'flycheck-checkers 'solidity-checker)
173+
(setq flycheck-solidity-checker-executable solidity-solc-path))
174+
(error (format "Solidity Mode Configuration error. Requested solc flycheck integration but can't find solc at: %s" solidity-solc-path)))))
151175

152176
(provide 'solidity-flycheck)
153177
;;; solidity-flycheck.el ends here

solidity-mode.el

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
(defcustom solidity-mode-hook nil
4444
"Callback hook to execute whenever a solidity file is loaded."
45+
:type 'hook
4546
:group 'solidity)
4647

4748
(defcustom solidity-comment-style 'star
@@ -500,6 +501,7 @@ Cursor must be at the function's name. Does not currently work for constructors
500501

501502
;;; Support for imenu
502503
(defun solidity-mode-imenu-generic-expression ()
504+
"Generic expressions for solidity mode imenu."
503505
(let* ((spacetabs "[\t\n ]+")
504506
(optional-spacetabs "[\t\n ]*")
505507
(ident-group "\\([A-Za-z_][A-Za-z0-9_]*\\)")
@@ -547,6 +549,7 @@ Cursor must be at the function's name. Does not currently work for constructors
547549
(set (make-local-variable 'comment-line-break-function)
548550
'c-indent-new-comment-line)
549551

552+
550553
(when solidity-mode-disable-c-mode-hook
551554
(set (make-local-variable 'c-mode-hook) nil))
552555

0 commit comments

Comments
 (0)