Skip to content

Commit

Permalink
Fix flycheck clang args loading (zhengyangfeng00)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBB committed Dec 9, 2015
1 parent 549219e commit 98c0702
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
2 changes: 1 addition & 1 deletion layers/+lang/c-c++/config.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; packages.el --- C/C++ Layer packages File for Spacemacs
;;; config.el --- C/C++ Layer config File for Spacemacs
;;
;; Copyright (c) 2012-2014 Sylvain Benner
;; Copyright (c) 2014-2015 Sylvain Benner & Contributors
Expand Down
49 changes: 49 additions & 0 deletions layers/+lang/c-c++/funcs.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
;;; funcs.el --- C/C++ Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2014 Sylvain Benner
;; Copyright (c) 2014-2015 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3

;; Based on the Sarcasm/irony-mode compilation database code.
(defun company-mode/find-clang-complete-file ()
(when buffer-file-name
(let ((dir (locate-dominating-file buffer-file-name ".clang_complete")))
(when dir
(concat (file-name-as-directory dir) ".clang_complete")))))

;; Based on the Sarcasm/irony-mode compilation database code.
(defun company-mode/load-clang-complete-file (cc-file)
"Load the flags from CC-FILE, one flag per line."
(let ((invocation-dir (expand-file-name (file-name-directory cc-file)))
(case-fold-search nil)
compile-flags)
(with-temp-buffer
(insert-file-contents cc-file)
;; Replace relative paths with absolute paths (by @trishume)
;; (goto-char (point-min))
(while (re-search-forward "\\(-I\\|-isystem\n\\)\\(\\S-\\)" nil t)
(replace-match (format "%s%s" (match-string 1)
(expand-file-name (match-string 2) invocation-dir))))
;; Turn lines into a list
(setq compile-flags
;; remove whitespaces at the end of each line, if any
(mapcar #'(lambda (line)
(if (string-match "[ \t]+$" line)
(replace-match "" t t line)
line))
(split-string (buffer-string) "\n" t))))
compile-flags))

(defun c-c++/load-clang-args ()
"Sets the arguments for company-clang based on a project-specific text file."
(unless company-clang-arguments
(let* ((cc-file (company-mode/find-clang-complete-file))
(flags (if cc-file (company-mode/load-clang-complete-file cc-file) '())))
(setq-local company-clang-arguments flags)
(setq flycheck-clang-args flags))))
42 changes: 4 additions & 38 deletions layers/+lang/c-c++/packages.el
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,9 @@

(when c-c++-enable-clang-support
(push 'company-clang company-backends-c-mode-common)
;; .clang_complete file loading
;; Sets the arguments for company-clang based on a project-specific text file.

;; START Based on the Sarcasm/irony-mode compilation database code.
(defun company-mode/find-clang-complete-file ()
(when buffer-file-name
(let ((dir (locate-dominating-file buffer-file-name ".clang_complete")))
(when dir
(concat (file-name-as-directory dir) ".clang_complete")))))

(defun company-mode/load-clang-complete-file (cc-file)
"Load the flags from CC-FILE, one flag per line."
(let ((invocation-dir (expand-file-name (file-name-directory cc-file)))
(case-fold-search nil)
compile-flags)
(with-temp-buffer
(insert-file-contents cc-file)
;; Replace relative paths with absolute paths (by @trishume)
;; (goto-char (point-min))
(while (re-search-forward "\\(-I\\|-isystem\n\\)\\(\\S-\\)" nil t)
(replace-match (format "%s%s" (match-string 1)
(expand-file-name (match-string 2) invocation-dir))))
;; Turn lines into a list
(setq compile-flags
;; remove whitespaces at the end of each line, if any
(mapcar #'(lambda (line)
(if (string-match "[ \t]+$" line)
(replace-match "" t t line)
line))
(split-string (buffer-string) "\n" t))))
compile-flags))
;; END Back to things written by @trishume

(defun company-mode/more-than-prefix-guesser ()
(unless company-clang-arguments
(let* ((cc-file (company-mode/find-clang-complete-file))
(flags (if cc-file (company-mode/load-clang-complete-file cc-file) '())))
(setq-local company-clang-arguments flags)
(setq flycheck-clang-args flags)))
(c-c++/load-clang-args)
(company-clang-guess-prefix))

(setq company-clang-prefix-guesser 'company-mode/more-than-prefix-guesser)))
Expand All @@ -127,7 +91,9 @@

(defun c-c++/post-init-flycheck ()
(dolist (mode '(c-mode c++-mode))
(spacemacs/add-flycheck-hook mode)))
(spacemacs/add-flycheck-hook mode))
(when c-c++-enable-clang-support
(spacemacs/add-to-hooks 'c-c++/load-clang-args '(c-mode-hook c++-mode-hook))))

(defun c-c++/init-gdb-mi ()
(use-package gdb-mi
Expand Down

0 comments on commit 98c0702

Please sign in to comment.