2012-01-27 84 views
2

我正在Emacs下編寫OCaml。我已經配置Emacs,以便Meta-x compilemake -k給出帶有超鏈接的警告。但對於通過failwith引發的錯誤,也不能給出一個超鏈接,例如:如何在Emacs中跟蹤「failwith」錯誤?

analyzing (ZONE)... 
Fatal error: exception Failure("to do") 
Raised at file "pervasives.ml", line 22, characters 22-33 
Called from file "list.ml", line 69, characters 12-15 
make: *** [all] Error 2 

Compilation exited abnormally with code 2 at Fri Jan 27 18:44:10 

我在我的代碼中的許多failwith "to do",並且需要知道哪一個引發錯誤,沒有人知道如何讓Emacs的定位這種錯誤?

回答

3

查看following bug report的某些elisp添加到您的.emacs,以便編譯主模式知道如何解析OCaml回溯報告。

下面是建議的代碼(加圖阿雷格模式鉤):

(defun caml-change-error-alist-for-backtraces() 
    "Hook to change the compilation-error-regexp-alist variable, to 
    search the ocaml backtraces for error locations" 
    (interactive) 
    (progn 
    (setq compilation-error-regexp-alist-alist 
      (append 
      '((caml-backtrace 
"^ *\\(?:Raised at\\|Called from\\) file \\(\"?\\)\\([^,\" \n\t<>]+\\)\\1,\ 
lines? \\([0-9]+\\)-?\\([0-9]+\\)?\\(?:$\\|,\ 
\\(?: characters? \\([0-9]+\\)-?\\([0-9]+\\)?:?\\)?\\)" 
       2 (3 . 4) (5 . 6))) 
      compilation-error-regexp-alist-alist)) 
    (setq compilation-error-regexp-alist 
      (append compilation-error-regexp-alist '(caml-backtrace))))) 

(add-hook 'caml-mode-hook 'caml-change-error-alist-for-backtraces) 
(add-hook 'tuareg-mode-hook 'caml-change-error-alist-for-backtraces) 


(defun caml-change-error-alist-for-assert-failure() 
    "Hook to change the compilation-error-regexp-alist variable, to 
    search the assert failure messages for error locations" 
    (interactive) 
    (progn 
    (setq compilation-error-regexp-alist-alist 
      (append 
      '((caml-assert-failure 
       "Assert_failure(\"\\([^,\" \n\t<>]+\\)\", \\([0-9]+\\), \\([0-9]+\\))" 
       1 2 3)) 
      compilation-error-regexp-alist-alist)) 
    (setq compilation-error-regexp-alist 
      (append compilation-error-regexp-alist '(caml-assert-failure))))) 

(add-hook 'caml-mode-hook 'caml-change-error-alist-for-assert-failure) 
(add-hook 'tuareg-mode-hook 'caml-change-error-alist-for-assert-failure) 
2

有時編譯爲字節碼會爲您提供更精確的堆棧跟蹤。

  1. 確保您與-g選項編譯(或使用debug標籤與ocamlbuild)
  2. 編譯成字節碼,堆棧跟蹤更加精確。
  3. 確保$OCAMLRUNPARAMb選項設置(請here

M-x next-error讓你跟着堆棧跟蹤(如果你使用caml-mode從分佈和至少OCaml的3.11)。

+0

真的? 'M-x next-error'不適用於我的堆棧跟蹤。 – Thomas 2012-01-27 19:21:56

+0

是的,如果你使用'caml-mode'並且至少OCaml 3.11。參見[這裏](http://caml.inria.fr/mantis/view.php?id=4628)。 – 2012-01-27 19:40:51