2012-01-27 81 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