lang/cc: rewrite init of irony-mode & compiler options

This commit is contained in:
Henrik Lissner 2017-09-19 05:06:50 +02:00
parent 2a7f1b20b9
commit b288f34f4e
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 47 additions and 26 deletions

View file

@ -64,18 +64,19 @@
t))
;;;###autoload
(defun +cc|irony-add-include-paths ()
"Seek out and add the nearest include/ folders to clang's options."
(when-let (dir (locate-dominating-file buffer-file-name "include/"))
(cl-pushnew (concat "-I" (expand-file-name "include/" dir))
irony-additional-clang-options :test #'equal)))
(defun +cc|irony-init-compile-options ()
"Initialize compiler options for irony-mode. It searches for the nearest
compilation database and initailizes it. If none was found, it uses
`+cc-c++-compiler-options'.
See https://github.com/Sarcasm/irony-mode#compilation-database for details on
compilation dbs."
(when (memq major-mode '(c-mode c++-mode objc-mode))
(require 'irony-cdb)
(unless (irony-cdb-autosetup-compile-options)
(irony-cdb--update-compile-options
(append (delq nil (cdr-safe (assq major-mode +cc-compiler-options)))
(cl-loop for path in +cc-include-paths
collect (format "-I %s" (shell-quote-argument path))))
(doom-project-root)))))
;;;###autoload
(defun +cc|use-c++11 ()
"Enable C++11 support with clang (via irony)."
(cl-pushnew "-std=c++11" irony-additional-clang-options :test #'equal)
(when IS-MAC
;; NOTE beware: you'll get abi-inconsistencies when passing std-objects to
;; libraries linked with libstdc++ (e.g. if you use boost which wasn't
;; compiled with libc++)
(cl-pushnew "-stdlib=libc++" irony-additional-clang-options :test #'equal)))

View file

@ -1,5 +1,30 @@
;;; lang/cc/config.el --- c, c++, and obj-c -*- lexical-binding: t; -*-
(defvar +cc-include-paths (list "include/")
"A list of paths, relative to a project root, to search for headers in
C/C++. Paths can be absolute.
The purpose of this variable is to ensure syntax checkers and code-completion
knows where to look for headers.")
(defvar +cc-compiler-options
`((c-mode . nil)
(c++-mode
. ,(list "-std=c++11" ; use C++11 by default
(when IS-MAC
;; NOTE beware: you'll get abi-inconsistencies when passing
;; std-objects to libraries linked with libstdc++ (e.g. if you use
;; boost which wasn't compiled with libc++)
(list "-stdlib=libc++"))))
(objc-mode . nil))
"A list of default compiler options for the C family. These are ignored if a
compilation database is present in the project.")
;;
;; Plugins
;;
(def-package! cc-mode
:commands (c-mode c++-mode objc-mode java-mode)
:mode ("\\.mm" . objc-mode)
@ -90,19 +115,14 @@
:preface
(setq irony-server-install-prefix (concat doom-etc-dir "irony-server/"))
:init
(defun +cc|init-irony-mode ()
;; The major-mode check is necessary because some modes derive themselves
;; from a c base mode, like java-mode or php-mode.
(when (and (memq major-mode '(c-mode c++-mode objc-mode))
(file-directory-p irony-server-install-prefix))
(irony-mode +1)))
(add-hook 'c-mode-common-hook #'+cc|init-irony-mode)
(add-hook! (c-mode c++-mode objc-mode) #'irony-mode)
:config
(make-variable-buffer-local 'irony-additional-clang-options)
;; Add nearest include/ path to compiler options
(add-hook! '(c-mode-hook c++-mode-hook) #'+cc|irony-add-include-paths)
;; Use C++11/14 by default
(add-hook 'c++-mode-hook #'+cc|use-c++11))
(unless (file-directory-p irony-server-install-prefix)
(warn "irony-mode: server isn't installed; run M-x irony-install-server"))
;; Initialize compilation database, if present. Otherwise, fall back on
;; `+cc-compiler-options'.
(add-hook 'irony-mode-hook #'+cc|irony-init-compile-options))
(def-package! irony-eldoc
:after irony