From b4cabe3cf07dd490432368332e8ce9087d57dacd Mon Sep 17 00:00:00 2001 From: Eivind Fonn Date: Sun, 15 Nov 2015 13:21:53 +0100 Subject: [PATCH] Fix flycheck clang args loading (zhengyangfeng00) --- layers/+lang/c-c++/config.el | 2 +- layers/+lang/c-c++/funcs.el | 49 ++++++++++++++++++++++++++++++++++ layers/+lang/c-c++/packages.el | 44 ++++-------------------------- 3 files changed, 55 insertions(+), 40 deletions(-) create mode 100644 layers/+lang/c-c++/funcs.el diff --git a/layers/+lang/c-c++/config.el b/layers/+lang/c-c++/config.el index d9a9c561876d..075b65ccde28 100644 --- a/layers/+lang/c-c++/config.el +++ b/layers/+lang/c-c++/config.el @@ -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 diff --git a/layers/+lang/c-c++/funcs.el b/layers/+lang/c-c++/funcs.el new file mode 100644 index 000000000000..854c58b0abc6 --- /dev/null +++ b/layers/+lang/c-c++/funcs.el @@ -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 +;; 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)))) diff --git a/layers/+lang/c-c++/packages.el b/layers/+lang/c-c++/packages.el index 191817e414dd..98f007b52780 100644 --- a/layers/+lang/c-c++/packages.el +++ b/layers/+lang/c-c++/packages.el @@ -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))) @@ -126,8 +90,10 @@ :init (push 'company-c-headers company-backends-c-mode-common)))) (defun c-c++/post-init-flycheck () - (dolist (mode '(c-mode-hook c++-mode-hook)) - (spacemacs/add-flycheck-hook mode))) + (dolist (mode '(c-mode c++-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