This commit is contained in:
ry
2022-01-11 18:05:13 -08:00
parent 2046befee2
commit 8e7b654716
846 changed files with 71287 additions and 4 deletions

View File

@@ -0,0 +1,258 @@
#+TITLE: lang/cc
#+DATE: January 16, 2017
#+SINCE: v2.0
#+STARTUP: inlineimages
* Table of Contents :TOC_3:noexport:
- [[#description][Description]]
- [[#module-flags][Module Flags]]
- [[#plugins][Plugins]]
- [[#prerequisites][Prerequisites]]
- [[#lsp-servers][LSP servers]]
- [[#irony-server][irony-server]]
- [[#macos][macOS]]
- [[#rtags][rtags]]
- [[#configure][Configure]]
- [[#project-compile-settings][Project compile settings]]
- [[#known-issues-with-bear-on-macos][Known issues with bear on macOS]]
- [[#configure-lsp-servers][Configure LSP servers]]
- [[#lsp-mode-with-clangd][LSP-mode with clangd]]
- [[#lsp-mode-with-ccls][LSP-mode with ccls]]
- [[#eglot-with-clangd][Eglot with clangd]]
- [[#eglot-with-ccls][Eglot with ccls]]
- [[#appendix][Appendix]]
- [[#eglot-specific-bindings][Eglot specific bindings]]
* Description
This module adds support for the C-family of languages: C, C++, and Objective-C.
+ Code completion (~company-irony~)
+ eldoc support (~irony-eldoc~)
+ Syntax-checking (~flycheck-irony~)
+ Code navigation (~rtags~)
+ File Templates ([[../../editor/file-templates/templates/c-mode][c-mode]], [[../../editor/file-templates/templates/c++-mode][c++-mode]])
+ Snippets ([[https://github.com/hlissner/doom-snippets/tree/master/cc-mode][cc-mode]], [[https://github.com/hlissner/doom-snippets/tree/master/c-mode][c-mode]], [[https://github.com/hlissner/doom-snippets/tree/master/c++-mode][c++-mode]])
+ Several improvements to C++11 indentation and syntax highlighting.
** Module Flags
+ ~+lsp~ Disables irony+rtags and replaces them with LSP (ccls by default). This
requires the =:tools lsp= module.
** Plugins
+ [[https://github.com/Kitware/CMake][cmake-mode]]
+ [[https://github.com/chachi/cuda-mode][cuda-mode]]
+ [[https://github.com/liblit/demangle-mode][demangle-mode]]
+ [[https://github.com/jart/disaster][disaster]]
+ [[https://github.com/ludwigpacifici/modern-cpp-font-lock][modern-cpp-font-lock]]
+ [[https://github.com/salmanebah/opencl-mode][opencl-mode]]
+ [[https://github.com/jimhourihan/glsl-mode][glsl-mode]]*
+ [[https://github.com/guidoschmidt/company-glsl][company-glsl]]*
+ =+lsp=
+ [[https://github.com/MaskRay/emacs-ccls][ccls]] if =:tools lsp= has *no* =+eglot= flag
+ =-lsp=
+ [[https://github.com/Sarcasm/irony-mode][irony]]
+ [[https://github.com/ikirill/irony-eldoc][irony-eldoc]]
+ [[https://github.com/Sarcasm/flycheck-irony][flycheck-irony]]
+ [[https://github.com/Sarcasm/company-irony][company-irony]]
+ [[https://github.com/hotpxl/company-irony-c-headers][company-irony-c-headers]]
+ [[https://github.com/Andersbakken/rtags][rtags]]
+ [[https://github.com/Andersbakken/rtags][ivy-rtags]]
+ [[https://github.com/Andersbakken/rtags][helm-rtags]]
* Prerequisites
This module's requirements change depending on how you use it.
+ If =+lsp= is enabled, you need one of *clangd v9+* or *ccls*.
+ If =+lsp= is *not* enabled, you need *irony-server* and *rtags*.
+ Other features in this module depend on:
+ (optional) glslangValidator, for GLSL completion in ~glsl-mode~
+ (optional) cmake, for code completion in ~cmake-mode~
+ You will need a C/C++ compiler, like =gcc= or =clang=.
** LSP servers
=lsp-mode= and =eglot= support a few LSP servers, but =clangd= and =ccls= are
recommended.
+ clangd (must be v9 or newer) :: clangd is included with =llvm= which should be
available through your OS' package manager.
- Linux:
- Debian 11 & Ubuntu 20.10: ~# apt-get install clangd-11~
- 20.04 LTS: [[https://pkgs.org/search/?q=clangd][clangd-10]]
- Fedora & CentOS/RHEL 8+: ~# dnf install clang-tools-extra~
- openSUSE: ~# zypper install clang~
- Arch: ~# pacman -S clang~
- BSDs:
- NetBSD & OpenBSD: ~# pkg_add clang-tools-extra~
- macOS: ~$ brew install llvm~ // 1GB+ installation! May take a while!
- Windows: use the win64 installer from [[https://releases.llvm.org/download.html][LLVM's GitHub release page]].
+ ccls :: Available in many OS' package managers as =ccls=. Otherwise, there are
alternative install methods listed [[https://github.com/MaskRay/ccls/wiki/Install][in the project's wiki]].
+ cmake-language-server :: available through ~pip~ on most distributions
** irony-server
Irony powers the code completion, eldoc and syntax checking systems.
After installing its dependencies (Clang and CMake), run ~M-x
irony-install-server~ in Emacs.
*** macOS
Due to linking issues, macOS users must compile irony-server manually:
#+BEGIN_SRC sh
brew install cmake
brew install llvm
git clone https://github.com/Sarcasm/irony-mode irony-mode
#+END_SRC
#+BEGIN_SRC bash
mkdir irony-mode/server/build
pushd irony-mode/server/build
DEST="$HOME/.emacs.d/.local/etc/irony-server/"
cmake -DCMAKE_PREFIX_PATH=/usr/local/opt/llvm \
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON \
-DCMAKE_INSTALL_PREFIX="$DEST" ../
cmake --build . --use-stderr --config Release --target install
install_name_tool -change @rpath/libclang.dylib \
/usr/local/opt/llvm/lib/libclang.dylib \
"$DEST/bin/irony-server"
# Cleanup
popd
rm -rf irony-mode
#+END_SRC
** rtags
Code navigation requires an [[https://github.com/Andersbakken/rtags][rtags]] server (~rdm~) installed. This should be
available through your OS's package manager.
This module will auto-start ~rdm~ when you open C/C++ buffers (so long as one
isn't already running). If you prefer to run it yourself:
#+BEGIN_SRC sh
rdm &
rc -J $PROJECT_ROOT # loads PROJECT_ROOT's compile_commands.json
#+END_SRC
* Configure
** Project compile settings
By default, a set of default compile settings are defined in
~+cc-default-compiler-options~ for C, C++ and Objective C. Irony, rtags and
flycheck will fall back to these. *This variable does nothing for LSP users.*
For a more universal solution: both LSP servers and irony will recognize a
[[https://sarcasm.github.io/notes/dev/compilation-database.html#ninja][compilation database]] (a ~compile_commands.json~ file). There are [[https://sarcasm.github.io/notes/dev/compilation-database.html][many ways to
generate one]]. Here is an example using [[http://www.cmake.org/][CMake]] and [[https://github.com/rizsotto/Bear][bear]]:
#+BEGIN_SRC sh
# For CMake projects
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
#+END_SRC
#+BEGIN_SRC sh
# For non-CMake projects
make clean
bear make
#+END_SRC
Use ~M-x +cc/reload-compile-db~ to reload your compile db in an already-open
C/C++/ObjC buffer.
*** Known issues with bear on macOS
MacOS' [[https://support.apple.com/en-us/HT204899][System Integrity Protection (SIP)]] might interfere with bear if ~make~ is
under ~/usr/bin/~ which results in an empty compilation database.
From the bear [[https://github.com/rizsotto/Bear#empty-compilation-database-on-os-x-captain-or-fedora][readme]]:
#+begin_quote
Security extension/modes on different operating systems might disable library
preloads. This case Bear behaves normally, but the result compilation database
will be empty. (Please make sure it's not the case when reporting bugs.) Notable
examples for enabled security modes are: OS X 10.11 (check with csrutil status |
grep 'System Integrity Protection'), and Fedora, CentOS, RHEL (check with
sestatus | grep 'SELinux status').
Workaround could be to disable the security feature while running Bear. (This
might involve reboot of your computer, so might be heavy workaround.) Another
option if the build tool is not installed under certain directories. Or use
tools which are using compiler wrappers. (It injects a fake compiler which does
record the compiler invocation and calls the real compiler too.) An example for
such tool might be scan-build. The build system shall respect CC and CXX
environment variables.
#+end_quote
A workaround might be to install ~make~ via Homebrew which puts ~gmake~
under ~/usr/local/~.
#+BEGIN_SRC sh
brew install make
#+END_SRC
#+BEGIN_SRC sh
make clean
bear gmake
#+END_SRC
Additional info:
+ [[https://github.com/rizsotto/Bear/issues/158][Empty compilation database with compiler in /usr/local]]
+ [[https://github.com/rizsotto/Bear/issues/152][Workaround for 'Empty compilation database on OS X Captain]]
** Configure LSP servers
Search for your combination of =(LSP client package, LSP server)=. You are using
=LSP-mode= by default, =eglot= if you have =:tools (lsp +eglot)= active in your
=init.el= file.
*** LSP-mode with clangd
#+BEGIN_SRC elisp
(setq lsp-clients-clangd-args '("-j=3"
"--background-index"
"--clang-tidy"
"--completion-style=detailed"
"--header-insertion=never"
"--header-insertion-decorators=0"))
(after! lsp-clangd (set-lsp-priority! 'clangd 2))
#+END_SRC
This will both set your clangd flags and choose =clangd= as the default LSP server everywhere clangd can be used.
*** LSP-mode with ccls
#+BEGIN_SRC elisp
(after! ccls
(setq ccls-initialization-options '(:index (:comments 2) :completion (:detailedLabel t)))
(set-lsp-priority! 'ccls 2)) ; optional as ccls is the default in Doom
#+END_SRC
This will both set your ccls flags and choose ccls as the default server. [[https://github.com/MaskRay/ccls/wiki/Customization#--initjson][CCLS
documentation]] lists available options, use =t= for ~true~, =:json-false= for
~false~, and =:json-null= for ~null~.
*** Eglot with clangd
#+BEGIN_SRC elisp
(set-eglot-client! 'cc-mode '("clangd" "-j=3" "--clang-tidy"))
#+END_SRC
This will both set your clangd flags and choose clangd as the default server (if
it is the last =set-eglot-client! 'cc-mode= in your config).
*** Eglot with ccls
#+BEGIN_SRC elisp
(set-eglot-client! 'cc-mode '("ccls" "--init={\"index\": {\"threads\": 3}}"))
#+END_SRC
This will both set your ccls flags and choose ccls as the default server (if it
is the last =set-eglot-client! 'cc-mode= in your config). [[https://github.com/MaskRay/ccls/wiki/Customization#--initjson][CCLS documentation]]
lists available options
* Appendix
** Eglot specific bindings
When using =+lsp= and =:tools (lsp +eglot)=, lsp-mode is replaced with eglot,
and an additional function to get inheritance type hierarchy is added
| Binding | Description |
|------------------------------+--------------------------------------------------|
| ~<localleader> c t~ | ~Display inheritance type hierarchy (upwards)~ |
| ~<prefix> <localleader> c t~ | ~Display inheritance type hierarchy (downwards)~ |

View File

@@ -0,0 +1,330 @@
;;; lang/cc/autoload.el -*- lexical-binding: t; -*-
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.cl\\'" . opencl-mode))
;; The plusses in c++-mode can be annoying to search for ivy/helm (which reads
;; queries as regexps), so we add these for convenience.
;;;###autoload (defalias 'cpp-mode 'c++-mode)
;;;###autoload (defvaralias 'cpp-mode-map 'c++-mode-map)
;;
;; Library
;;;###autoload
(defun +cc-c++-lineup-inclass (langelem)
"Indent inclass lines one level further than access modifier keywords."
(and (eq major-mode 'c++-mode)
(or (assoc 'access-label c-syntactic-context)
(save-excursion
(save-match-data
(re-search-backward
"\\(?:p\\(?:ublic\\|r\\(?:otected\\|ivate\\)\\)\\)"
(c-langelem-pos langelem) t))))
'++))
;;;###autoload
(defun +cc-lineup-arglist-close (langlem)
"Line up the closing brace in an arglist with the opening brace IF cursor is
preceded by the opening brace or a comma (disregarding whitespace in between)."
(when (save-excursion
(save-match-data
(skip-chars-backward " \t\n" (c-langelem-pos langelem))
(memq (char-before) (list ?, ?\( ?\;))))
(c-lineup-arglist langlem)))
(defun +cc--re-search-for (regexp)
(save-excursion
(save-restriction
(save-match-data
(widen)
(goto-char (point-min))
(re-search-forward regexp magic-mode-regexp-match-limit t)))))
;;;###autoload
(defun +cc-c-c++-objc-mode ()
"Uses heuristics to detect `c-mode', `objc-mode' or `c++-mode'.
1. Checks if there are nearby cpp/cc/m/mm files with the same name.
2. Checks for ObjC and C++-specific keywords and libraries.
3. Falls back to `+cc-default-header-file-mode', if set.
4. Otherwise, activates `c-mode'.
This is meant to replace `c-or-c++-mode' (introduced in Emacs 26.1), which
doesn't support specification of the fallback mode and whose heuristics are
simpler."
(let ((base (file-name-sans-extension (buffer-file-name (buffer-base-buffer)))))
(cond ((file-exists-p! (or (concat base ".cpp")
(concat base ".cc")))
(c++-mode))
((or (file-exists-p! (or (concat base ".m")
(concat base ".mm")))
(+cc--re-search-for
(concat "^[ \t\r]*\\(?:"
"@\\(?:class\\|interface\\|property\\|end\\)\\_>"
"\\|#import +<Foundation/Foundation.h>"
"\\|[-+] ([a-zA-Z0-9_]+)"
"\\)")))
(objc-mode))
((+cc--re-search-for
(let ((id "[a-zA-Z0-9_]+") (ws "[ \t\r]+") (ws-maybe "[ \t\r]*"))
(concat "^" ws-maybe "\\(?:"
"using" ws "\\(?:namespace" ws "std;\\|std::\\)"
"\\|" "namespace" "\\(?:" ws id "\\)?" ws-maybe "{"
"\\|" "class" ws id ws-maybe "[:{\n]"
"\\|" "template" ws-maybe "<.*>"
"\\|" "#include" ws-maybe "<\\(?:string\\|iostream\\|map\\)>"
"\\)")))
(c++-mode))
((functionp +cc-default-header-file-mode)
(funcall +cc-default-header-file-mode))
((c-mode)))))
(defun +cc-resolve-include-paths ()
(cl-loop with path = (or buffer-file-name default-directory)
for dir in +cc-default-include-paths
if (file-name-absolute-p dir)
collect dir
else if (projectile-locate-dominating-file path dir)
collect (expand-file-name dir it)))
;;
;; Commands
;;;###autoload
(defun +cc/reload-compile-db ()
"Reload the current project's JSON compilation database."
(interactive)
(unless (memq major-mode '(c-mode c++-mode objc-mode))
(user-error "Not a C/C++/ObjC buffer"))
;; first rtag
(when (and (featurep 'rtags)
rtags-enabled
(executable-find rtags-rc-binary-name))
(with-temp-buffer
(message "Reloaded compile commands for rtags daemon")
(rtags-call-rc :silent t "-J" (or (doom-project-root) default-directory))))
;; then irony
(when (and (featurep 'irony) irony-mode)
(+cc-init-irony-compile-options-h))
;; Otherwise, LSP
(when (bound-and-true-p lsp-mode)
(lsp-workspace-restart))
(when (bound-and-true-p eglot-managed-mode)
(eglot-reconnect)))
;;;###autoload
(defun +cc/imenu ()
"Invoke `rtags-imenu' if a running rdm process is available, otherwise invoke
`imenu'."
(interactive)
(call-interactively
(if (and (processp rtags-rdm-process)
(not (eq (process-status rtags-rdm-process) 'exit))
(not (eq (process-status rtags-rdm-process) 'signal)))
#'rtags-imenu
#'imenu)))
;; Eglot specific helper, courtesy of MaskRay
;;;###autoload
(defun +cc/eglot-ccls-show-inheritance-hierarchy (&optional derived)
"Show inheritance hierarchy for the thing at point.
If DERIVED is non-nil (interactively, with prefix argument), show
the children of class at point."
(interactive "P")
(if-let* ((res (jsonrpc-request
(eglot--current-server-or-lose)
:$ccls/inheritance
(append (eglot--TextDocumentPositionParams)
`(:derived ,(if derived t :json-false))
'(:levels 100) '(:hierarchy t))))
(tree (list (cons 0 res))))
(with-help-window "*ccls inheritance*"
(with-current-buffer standard-output
(while tree
(pcase-let ((`(,depth . ,node) (pop tree)))
(cl-destructuring-bind (&key uri range) (plist-get node :location)
(insert (make-string depth ?\ ) (plist-get node :name) "\n")
(make-text-button (+ (point-at-bol 0) depth) (point-at-eol 0)
'action (lambda (_arg)
(interactive)
(find-file (eglot--uri-to-path uri))
(goto-char (car (eglot--range-region range)))))
(cl-loop for child across (plist-get node :children)
do (push (cons (1+ depth) child) tree)))))))
(eglot--error "Hierarchy unavailable")))
;;;###autoload
(defun +cc-cmake-lookup-documentation-fn (_)
"Look up the symbol at point in CMake's documentation."
(condition-case _
(progn
(save-window-excursion (cmake-help))
(when-let (buf (get-buffer "*CMake Help*"))
(pop-to-buffer buf)
t))
(error nil)))
;;
;; Hooks
;;;###autoload
(defun +cc-fontify-constants-h ()
"Better fontification for preprocessor constants"
(when (memq major-mode '(c-mode c++-mode))
(font-lock-add-keywords
nil '(("\\<[A-Z]*_[0-9A-Z_]+\\>" . font-lock-constant-face)
("\\<[A-Z]\\{3,\\}\\>" . font-lock-constant-face))
t)))
(defvar +cc--project-includes-alist nil)
;;;###autoload
(defun +cc-init-irony-compile-options-h ()
"Initialize compiler options for irony-mode. It searches for the nearest
compilation database and initailizes it, otherwise falling back on
`+cc-default-compiler-options' and `+cc-default-include-paths'.
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)
(let ((project-root (doom-project-root))
(include-paths (+cc-resolve-include-paths)))
(setf (alist-get project-root +cc--project-includes-alist)
include-paths)
(irony-cdb--update-compile-options
(append (delq nil (cdr-safe (assq major-mode +cc-default-compiler-options)))
(cl-loop for path in include-paths
collect (format "-I%s" path)))
project-root)))))
;; ;;;###autoload
;; (defun +cc|init-ccls-compile-options ()
;; "TODO"
;; (when (memq major-mode '(c-mode c++-mode objc-mode))
;; (when-let (include-paths (+cc-resolve-include-paths))
;; (let ((args (delq nil (cdr-safe (assq major-mode +cc-default-compiler-options)))))
;; (setf (alist-get (or (lsp-workspace-root)
;; (lsp--suggest-project-root)
;; (doom-project-root))
;; +cc--project-includes-alist)
;; include-paths)
;; (setq ccls-initialization-options
;; `(:clang (:extraArgs
;; [,@(cl-loop for path in include-paths
;; collect (format "-I%s" path))])))))))
;;;###autoload
(defun +cc-init-ffap-integration-h ()
"Takes the local project include paths and registers them with ffap.
This way, `find-file-at-point' (and `+lookup/file') will know where to find most
header files."
(when-let (project-root (or (bound-and-true-p irony--working-directory)
(and (featurep 'lsp)
(or (lsp-workspace-root)
(doom-project-root)))))
(require 'ffap)
(make-local-variable 'ffap-c-path)
(make-local-variable 'ffap-c++-path)
(cl-loop for dir in (or (cdr (assoc project-root +cc--project-includes-alist))
(+cc-resolve-include-paths))
do (add-to-list (pcase major-mode
(`c-mode 'ffap-c-path)
(`c++-mode 'ffap-c++-path))
(expand-file-name dir project-root)))))
;;
;;; CCLS specific helpers
;; ccls-show-vars ccls-show-base ccls-show-derived ccls-show-members have a
;; parameter while others are interactive.
;;
;; (+cc/ccls-show-base 1) direct bases
;; (+cc/ccls-show-derived 1) direct derived
;; (+cc/ccls-show-member 2) => 2 (Type) => nested classes / types in a namespace
;; (+cc/ccls-show-member 3) => 3 (Func) => member functions / functions in a namespace
;; (+cc/ccls-show-member 0) => member variables / variables in a namespace
;; (+cc/ccls-show-vars 1) => field
;; (+cc/ccls-show-vars 2) => local variable
;; (+cc/ccls-show-vars 3) => field or local variable. 3 = 1 | 2
;; (+cc/ccls-show-vars 4) => parameter
;;;###autoload
(defun +cc/ccls-show-callee ()
"Show callees of symbol under point."
(interactive)
(lsp-ui-peek-find-custom "$ccls/call" '(:callee t)))
;;;###autoload
(defun +cc/ccls-show-caller ()
"Show callers of symbol under point."
(interactive)
(lsp-ui-peek-find-custom "$ccls/call"))
;;;###autoload
(defun +cc/ccls-show-vars (kind)
"Show variables of type KIND as symbol under point.
1 -> field
2 -> local variable
3 -> field or local variables. 3 = 1 | 2.
4 -> parameter"
(lsp-ui-peek-find-custom "$ccls/vars" `(:kind ,kind)))
;;;###autoload
(defun +cc/ccls-show-base (levels)
"Show bases of class under point up to LEVELS levels (1 for direct bases)."
(lsp-ui-peek-find-custom "$ccls/inheritance" `(:levels ,levels)))
;;;###autoload
(defun +cc/ccls-show-derived (levels)
"Show derived classes from class under point down to LEVELS levels (1 for direct derived)."
(lsp-ui-peek-find-custom "$ccls/inheritance" `(:levels ,levels :derived t)))
;;;###autoload
(defun +cc/ccls-show-member (kind)
"Show member elements of kind KIND for class/namespace under point.
0 -> member variables/ variables in a namespace
2 -> nested classes / types in a namespace
3 -> member functions / functions in a namespace"
(lsp-ui-peek-find-custom "$ccls/member" `(:kind ,kind)))
;; The meaning of :role corresponds to https://github.com/maskray/ccls/blob/master/src/symbol.h
;;;###autoload
(defun +cc/ccls-show-references-address ()
"References w/ Role::Address bit (e.g. variables explicitly being taken addresses)"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :role 128)))
;;;###autoload
(defun +cc/ccls-show-references-macro ()
"References w/ Role::Dynamic bit (macro expansions)"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :role 64)))
;;;###autoload
(defun +cc/ccls-show-references-not-call ()
"References w/o Role::Call bit (e.g. where functions are taken addresses)"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :excludeRole 32)))
;;;###autoload
(defun +cc/ccls-show-references-read ()
"References w/ Role::Read"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :role 8)))
;;;###autoload
(defun +cc/ccls-show-references-write ()
"References w/ Role::Write"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :role 16)))

View File

@@ -0,0 +1,306 @@
;;; lang/cc/config.el --- c, c++, and obj-c -*- lexical-binding: t; -*-
(defvar +cc-default-include-paths
(list "include"
"includes")
"A list of default relative paths which will be searched for up from the
current file, to be passed to irony as extra header search paths. Paths can be
absolute. This is ignored if your project has a compilation database.
This is ignored by ccls.")
(defvar +cc-default-header-file-mode 'c-mode
"Fallback major mode for .h files if all other heuristics fail (in
`+cc-c-c++-objc-mode').")
(defvar +cc-default-compiler-options
`((c-mode . nil)
(c++-mode
. ,(list "-std=c++1z" ; use C++17 draft 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++)
"-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.
This is ignored by ccls.")
;;
;;; Packages
(use-package! cc-mode
:mode ("\\.mm\\'" . objc-mode)
;; Use `c-mode'/`c++-mode'/`objc-mode' depending on heuristics
:mode ("\\.h\\'" . +cc-c-c++-objc-mode)
;; Ensure find-file-at-point recognize system libraries in C modes. It must be
;; set up before the likes of irony/lsp are initialized. Also, we use
;; local-vars hooks to ensure these only run in their respective major modes,
;; and not their derived modes.
:hook ((c-mode-local-vars c++-mode-local-vars objc-mode-local-vars) . +cc-init-ffap-integration-h)
;;; Improve fontification in C/C++ (also see `modern-cpp-font-lock')
:hook (c-mode-common . rainbow-delimiters-mode)
:hook ((c-mode c++-mode) . +cc-fontify-constants-h)
:config
(set-docsets! 'c-mode "C")
(set-docsets! 'c++-mode "C++" "Boost")
(set-electric! '(c-mode c++-mode objc-mode java-mode) :chars '(?\n ?\} ?\{))
(set-rotate-patterns! 'c++-mode
:symbols '(("public" "protected" "private")
("class" "struct")))
(set-ligatures! '(c-mode c++-mode)
;; Functional
;; :def "void "
;; Types
:null "nullptr"
:true "true" :false "false"
:int "int" :float "float"
:str "std::string"
:bool "bool"
;; Flow
:not "!"
:and "&&" :or "||"
:for "for"
:return "return"
:yield "#require")
;; HACK Suppress 'Args out of range' error in when multiple modifications are
;; performed at once in a `c++-mode' buffer, e.g. with `iedit' or
;; multiple cursors.
(undefadvice! +cc--suppress-silly-errors-a (fn &rest args)
:around #'c-after-change-mark-abnormal-strings
(ignore-errors (apply fn args)))
;; Custom style, based off of linux
(setq c-basic-offset tab-width
c-backspace-function #'delete-backward-char)
(c-add-style
"doom" '((c-comment-only-line-offset . 0)
(c-hanging-braces-alist (brace-list-open)
(brace-entry-open)
(substatement-open after)
(block-close . c-snug-do-while)
(arglist-cont-nonempty))
(c-cleanup-list brace-else-brace)
(c-offsets-alist
(knr-argdecl-intro . 0)
(substatement-open . 0)
(substatement-label . 0)
(statement-cont . +)
(case-label . +)
;; align args with open brace OR don't indent at all (if open
;; brace is at eolp and close brace is after arg with no trailing
;; comma)
(brace-list-intro . 0)
(brace-list-close . -)
(arglist-intro . +)
(arglist-close +cc-lineup-arglist-close 0)
;; don't over-indent lambda blocks
(inline-open . 0)
(inlambda . 0)
;; indent access keywords +1 level, and properties beneath them
;; another level
(access-label . -)
(inclass +cc-c++-lineup-inclass +)
(label . 0))))
(when (listp c-default-style)
(setf (alist-get 'other c-default-style) "doom"))
(after! ffap
(add-to-list 'ffap-alist '(c-mode . ffap-c-mode))))
(use-package! modern-cpp-font-lock
:hook (c++-mode . modern-c++-font-lock-mode))
(use-package! irony
:unless (featurep! +lsp)
:commands irony-install-server
;; Initialize compilation database, if present. Otherwise, fall back on
;; `+cc-default-compiler-options'.
:hook (irony-mode . +cc-init-irony-compile-options-h)
;; Only initialize `irony-mode' if the server is available. Otherwise fail
;; quietly and gracefully.
:hook ((c-mode-local-vars c++-mode-local-vars objc-mode-local-vars) . +cc-init-irony-mode-maybe-h)
:preface (setq irony-server-install-prefix (concat doom-etc-dir "irony-server/"))
:config
(defun +cc-init-irony-mode-maybe-h ()
(if (file-directory-p irony-server-install-prefix)
(irony-mode +1)
(message "Irony server isn't installed")))
(setq irony-cdb-search-directory-list '("." "build" "build-conda"))
(use-package! irony-eldoc
:hook (irony-mode . irony-eldoc))
(use-package! flycheck-irony
:when (featurep! :checkers syntax)
:config (flycheck-irony-setup))
(use-package! company-irony
:when (featurep! :completion company)
:init (set-company-backend! 'irony-mode '(:separate company-irony-c-headers company-irony))
:config (require 'company-irony-c-headers)))
;;
;; Major modes
(after! cmake-mode
(set-docsets! 'cmake-mode "CMake")
(set-popup-rule! "^\\*CMake Help\\*" :size 0.4 :ttl t)
(set-lookup-handlers! 'cmake-mode
:documentation '+cc-cmake-lookup-documentation-fn))
(use-package! company-cmake ; for `cmake-mode'
:when (featurep! :completion company)
:after cmake-mode
:config (set-company-backend! 'cmake-mode 'company-cmake))
(use-package! demangle-mode
:hook llvm-mode)
(use-package! company-glsl ; for `glsl-mode'
:when (featurep! :completion company)
:after glsl-mode
:config (set-company-backend! 'glsl-mode 'company-glsl))
;;
;; Rtags Support
(use-package! rtags
:unless (featurep! +lsp)
;; Only initialize rtags-mode if rtags and rdm are available.
:hook ((c-mode-local-vars c++-mode-local-vars objc-mode-local-vars) . +cc-init-rtags-maybe-h)
:preface (setq rtags-install-path (concat doom-etc-dir "rtags/"))
:config
(defun +cc-init-rtags-maybe-h ()
"Start an rtags server in c-mode and c++-mode buffers.
If rtags or rdm aren't available, fail silently instead of throwing a breaking error."
(and (require 'rtags nil t)
(rtags-executable-find rtags-rdm-binary-name)
(rtags-start-process-unless-running)))
(setq rtags-autostart-diagnostics t
rtags-use-bookmarks nil
rtags-completions-enabled nil
rtags-display-result-backend
(cond ((featurep! :completion ivy) 'ivy)
((featurep! :completion helm) 'helm)
('default))
;; These executables are named rtags-* on debian
rtags-rc-binary-name
(or (cl-find-if #'executable-find (list rtags-rc-binary-name "rtags-rc"))
rtags-rc-binary-name)
rtags-rdm-binary-name
(or (cl-find-if #'executable-find (list rtags-rdm-binary-name "rtags-rdm"))
rtags-rdm-binary-name)
;; If not using ivy or helm to view results, use a pop-up window rather
;; than displaying it in the current window...
rtags-results-buffer-other-window t
;; ...and don't auto-jump to first match before making a selection.
rtags-jump-to-first-match nil)
(set-lookup-handlers! '(c-mode c++-mode)
:definition #'rtags-find-symbol-at-point
:references #'rtags-find-references-at-point)
;; Use rtags-imenu instead of imenu/counsel-imenu
(define-key! (c-mode-map c++-mode-map) [remap imenu] #'+cc/imenu)
;; Ensure rtags cleans up after itself properly when exiting Emacs, rather
;; than display a jarring confirmation prompt for killing it.
(add-hook! 'kill-emacs-hook (ignore-errors (rtags-cancel-process)))
(add-hook 'rtags-jump-hook #'better-jumper-set-jump)
(add-hook 'rtags-after-find-file-hook #'recenter))
;;
;; LSP
(when (featurep! +lsp)
(add-hook! '(c-mode-local-vars-hook
c++-mode-local-vars-hook
objc-mode-local-vars-hook
cmake-mode-local-vars-hook)
#'lsp!)
(map! :after ccls
:map (c-mode-map c++-mode-map)
:n "C-h" (cmd! (ccls-navigate "U"))
:n "C-j" (cmd! (ccls-navigate "R"))
:n "C-k" (cmd! (ccls-navigate "L"))
:n "C-l" (cmd! (ccls-navigate "D"))
(:localleader
:desc "Preprocess file" "lp" #'ccls-preprocess-file
:desc "Reload cache & CCLS" "lf" #'ccls-reload)
(:after lsp-ui-peek
(:localleader
:desc "Callers list" "c" #'+cc/ccls-show-caller
:desc "Callees list" "C" #'+cc/ccls-show-callee
:desc "References (address)" "a" #'+cc/ccls-show-references-address
:desc "References (not call)" "f" #'+cc/ccls-show-references-not-call
:desc "References (Macro)" "m" #'+cc/ccls-show-references-macro
:desc "References (Read)" "r" #'+cc/ccls-show-references-read
:desc "References (Write)" "w" #'+cc/ccls-show-references-write)))
(when (featurep! :tools lsp +eglot)
;; Map eglot specific helper
(map! :localleader
:after cc-mode
:map c++-mode-map
:desc "Show type inheritance hierarchy" "ct" #'+cc/eglot-ccls-inheritance-hierarchy)
;; NOTE : This setting is untested yet
(after! eglot
;; IS-MAC custom configuration
(when IS-MAC
(add-to-list 'eglot-workspace-configuration
`((:ccls . ((:clang . ,(list :extraArgs ["-isystem/Library/Developer/CommandLineTools/usr/include/c++/v1"
"-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
"-isystem/usr/local/include"]
:resourceDir (cdr (doom-call-process "clang" "-print-resource-dir"))))))))))))
(use-package! ccls
:when (featurep! +lsp)
:unless (featurep! :tools lsp +eglot)
:defer t
:init
(defvar ccls-sem-highlight-method 'font-lock)
(after! projectile
(add-to-list 'projectile-globally-ignored-directories ".ccls-cache")
(add-to-list 'projectile-project-root-files-bottom-up ".ccls-root")
(add-to-list 'projectile-project-root-files-top-down-recurring "compile_commands.json"))
;; Avoid using `:after' because it ties the :config below to when `lsp-mode'
;; loads, rather than `ccls' loads.
(after! lsp-mode (require 'ccls))
:config
(set-evil-initial-state! 'ccls-tree-mode 'emacs)
;; Disable `ccls-sem-highlight-method' if `lsp-enable-semantic-highlighting'
;; is nil. Otherwise, it appears ccls bypasses it.
(setq-hook! 'lsp-configure-hook
ccls-sem-highlight-method (if lsp-enable-semantic-highlighting
ccls-sem-highlight-method))
(when (or IS-MAC IS-LINUX)
(setq ccls-initialization-options
`(:index (:trackDependency 1
:threads ,(max 1 (/ (doom-system-cpus) 2))))))
(when IS-MAC
(setq ccls-initialization-options
(append ccls-initialization-options
`(:clang ,(list :extraArgs ["-isystem/Library/Developer/CommandLineTools/usr/include/c++/v1"
"-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
"-isystem/usr/local/include"]
:resourceDir (cdr (doom-call-process "clang" "-print-resource-dir"))))))))

View File

@@ -0,0 +1,24 @@
;; -*- lexical-binding: t; no-byte-compile: t; -*-
;;; lang/cc/doctor.el
(assert! (or (not (featurep! +lsp))
(featurep! :tools lsp))
"This module requires (:tools lsp)")
(when (require 'rtags nil t)
;; rtags
(when-let (bins (cl-remove-if #'rtags-executable-find
(list rtags-rdm-binary-name
rtags-rc-binary-name)))
(warn! "Couldn't find the rtag client and/or server programs %s. Disabling rtags support"
bins)))
;; irony server
(when (require 'irony nil t)
(unless (file-directory-p irony-server-install-prefix)
(warn! "Irony server isn't installed. Run M-x irony-install-server")))
(when (featurep! :completion company)
;; glslangValidator
(unless (executable-find "glslangValidator")
(warn! "Couldn't find glslangValidator. GLSL code completion is disabled")))

View File

@@ -0,0 +1,34 @@
;; -*- no-byte-compile: t; -*-
;;; lang/cc/packages.el
(package! cmake-mode
:recipe (:host github :repo "emacsmirror/cmake-mode" :files (:defaults "*"))
:pin "d5d77de8c4c69e348b182eeb30222b2f1ba8db7b")
(package! cuda-mode :pin "7f593518fd135fc6af994024bcb47986dfa502d2")
(package! demangle-mode :pin "04f545adab066708d6151f13da65aaf519f8ac4e")
(package! disaster :pin "10a785facc60d89d78e0d5177985ab1af1741bb4")
(package! modern-cpp-font-lock :pin "43c6b68ff58fccdf9deef11674a172e4eaa8455c")
(package! opencl-mode :pin "15091eff92c33ee0d1ece40eb99299ef79fee92d")
(when (package! glsl-mode :pin "9b2e5f28e489a1f73c4aed734105618ac0dc0c43")
(when (featurep! :completion company)
(package! company-glsl
:recipe (:host github :repo "Kaali/company-glsl")
:pin "404cd0694ab34971f9c01eb22126cd2e7d3f9dc4")))
(if (featurep! +lsp)
(unless (featurep! :tools lsp +eglot)
;; ccls package is necessary only for lsp-mode.
(package! ccls :pin "675a5704c14a27931e835a431beea3631d92e8e6"))
(when (package! irony :pin "5063d6b16d5d0a444bbc7599caabfdc6b512f70c")
(package! irony-eldoc :pin "73e79a89fad982a2ba072f2fcc1b4e41f0aa2978")
(when (featurep! :checkers syntax)
(package! flycheck-irony :pin "42dbecd4a865cabeb301193bb4d660e26ae3befe"))
(when (featurep! :completion company)
(package! company-irony :pin "b44711dfce445610c1ffaec4951c6ff3882b216a")
(package! company-irony-c-headers :pin "72c386aeb079fb261d9ec02e39211272f76bbd97")))
(when (package! rtags :pin "db39790fda5c2443bc790b8971ac140914f7e9c2")
(when (featurep! :completion ivy)
(package! ivy-rtags))
(when (featurep! :completion helm)
(package! helm-rtags))))