Sarcasm / irony-mode

A C/C++ minor mode for Emacs powered by libclang
GNU General Public License v3.0
906 stars 99 forks source link

`irony-cdb-autosetup-compile-options` not called by `irony-mode-hook` #195

Open UberLambda opened 9 years ago

UberLambda commented 9 years ago

I've setup irony with a .clang_completer file, and I have the following run by Emacs at startup:

; Enable irony mode for c-family files
(add-hook 'c-mode-hook 'irony-mode) ; For better flycheck/ac C support
(add-hook 'c++-mode-hook 'irony-mode)
(add-hook 'objc-mode-hook 'irony-mode)

; Let irony source autocompletions when a buffer is opened.
; Also, make sure to autosetup the Clang flags.
(defun my-irony-mode-hook ()
  (define-key irony-mode-map [remap completion-at-point]
    'irony-completion-at-point-async)
  (define-key irony-mode-map [remap complete-symbol]
    'irony-completion-at-point-async))

(add-hook 'irony-mode-hook 'my-irony-mode-hook)
(add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)

; Enable irony's flycheck checker
(add-hook 'flycheck-mode-hook #'flycheck-irony-setup)

; Enable irony's autocompletion
(defun my-ac-irony-setup ()
  (yas-minor-mode 1)
  (auto-complete-mode 1)

  (add-to-list 'ac-sources 'ac-source-irony)
  (define-key irony-mode-map (kbd "M-RET") 'ac-complete-irony-async))

(add-hook 'irony-mode-hook 'my-ac-irony-setup)

If I open a c-mode buffer, though, M-x irony-cdb-menu shows that no completion database is loaded the first time, even though irony-mode is enabled and, as such, irony-mode-hook should have run already. M-x describe-variable irony-mode-hook seems to be correct, since all of the three callbacks defined in the config snipped above seem to be present.

Running irony-cdb-autosetup-compile-options (and restarting FlyCheck to refresh the error list) by hand seems to fix this issue.

Sarcasm commented 9 years ago

For some reason it seems like the hook isn't called, I don't know why just yet.

What Emacs version do you have?

UberLambda commented 9 years ago

GNU Emacs 24.5.1


If it is of any help, I uploaded my .emacs.d here. More specifically, irony settings are in this file.

Sarcasm commented 9 years ago

Okay, the hook isn't called at home for me (Emacs 24.5.1) but it is at work (older version). I will take a look.

Be careful in your configuration:

(defun my-load-irony-options()
  (irony-cdb-json-add-compile-commands-path (projectile-project-root)
                        (concat (file-name-as-directory "build")
                            "compile_commands.json"))

I'm not sure if duplicates are cleaned up properly, I will have to take a look but I plan on changing irony-cdb a bit.

UberLambda commented 9 years ago

Yeah, I had a FIXME comment about that... does simply clearing irony-cdb-json--project-alist reset the list, or is extra work needed to reset it?

Sarcasm commented 9 years ago

I checked, I'm using add-to-list so duplicate are taken care of by irony.

Sarcasm commented 9 years ago

I was wrong, even with Emacs 24.5.1 it works for me, I wasn't on master and I had some weird configuration (to test what I'm working on) but once the configuration restored it works fine.

After looking at your configuration more carefully I'm wondering if you could try to replace the following in your configuration:

;; Add "../build" (relative to the projectile root directory) of Cxx projects
;; as the path where to find "compile_commands.json", then load irony's options.
(defun my-load-irony-options()
  (irony-cdb-json-add-compile-commands-path (projectile-project-root)
                        (concat (file-name-as-directory "build")
                            "compile_commands.json"))
  (irony-cdb-autosetup-compile-options))

;; Reload and apply the Clang flags each time an irony buffer is opened
;; FIXME: the json's dir gets added every time, but the list never gets reset...
;; FIXME: this should run on 'irony-mode-hook, but it does not seem to work
;(add-hook 'irony-mode-hook 'my-load-irony-options)
(add-hook 'c-mode-hook 'my-load-irony-options)
(add-hook 'c++-mode-hook 'my-load-irony-options)
(add-hook 'objc-mode-hook 'my-load-irony-options)

By this:

(add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)

Then you can call irony-cdb-json-add-compile-commands-path manually if the path isn't already set.

Can you tell me if this work?

I'm not sure it's safe to call the projectile function in irony mode hook, how can you guarantee the order of initialization of the minor mode? If for some reasons something in projectile errors when projectile isn't yet initialized, (irony-cdb-autosetup-compile-options) may not be called for example.

UberLambda commented 9 years ago

I tried removing the custom callback and calling irony-cdb-json-add-compile-commands-path from the M-x menu manually, using ido-mode to select first the build directory and then compile_options.json's location.

Irony now doesn't seem to load compile_options.json at all for some reason (atleast M-x irony-cdb-menu tells me there is no completion database)...

Sarcasm commented 9 years ago

Can you try that: https://github.com/Sarcasm/irony-mode/issues/190#issuecomment-93062550

UberLambda commented 9 years ago

Sure.

Output before adding compile_commands.json with irony-cdb-json-add-compile-commands-path:

Making completion list... [2 times]
check-compile-options: Symbol's function definition is void: irony-cdb-json--ensure-project-alist-loaded
In include /home/paolo/gamedev/bw/core/src/bw/core.h

After adding compile_commands.json:

Making completion list... [2 times]
In include /home/paolo/gamedev/bw/core/src/bw/core.h
E: failed to locate compilation database