Permalink
Browse files

Fix detection of clang system includes

  • Loading branch information...
randomphrase committed Mar 15, 2014
1 parent a301ade commit f29298b58687bead8e7da39d96325085d8fe81b7
Showing with 57 additions and 3 deletions.
  1. +26 −0 ede-compdb-test.el
  2. +31 −3 ede-compdb.el
View
@@ -135,6 +135,32 @@ in-source build"
(kill-buffer buf)))
))))
+(ert-deftest parse-clang-system-includes ()
+ "Tests discovery of clang system includes"
+ (with-temp-buffer
+ ;; This is a real example from Apple Clang 5.0
+ (insert "clang -cc1 version 5.0 based upon LLVM 3.3svn default target x86_64-apple-darwin13.1.0
+ignoring nonexistent directory \"/usr/include/c++/v1\"
+ignoring nonexistent directory \"/usr/local/include\"
+#include \"...\" search starts here:
+#include <...> search starts here:
+ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
+ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/include
+ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
+ /usr/include
+ /System/Library/Frameworks (framework directory)
+ /Library/Frameworks (framework directory)
+End of search list.
+")
+ (goto-char (point-min))
+
+ (should (equal (ede-compdb-parse-clang-system-includes)
+ '("/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1"
+ "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/5.0/include"
+ "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"
+ "/usr/include")))
+ ))
+
(ert-deftest open-file-parsed ()
"Tests the parsing of source files in a project. We ensure it correctly locates all include files, amongst other things."
;;:expected-result :passed ;; TODO failed if we can't locate cmake on the path
View
@@ -162,6 +162,30 @@
(defvar ede-compdb-compiler-cache nil "Cached include paths for each compiler detected.")
+(defun ede-compdb-parse-clang-system-includes ()
+ "Parse the current buffer for clang system includes."
+
+ (while (not (or (eobp) (looking-at "^#include \\(\"...\"\\|<...>\\) search starts here:")))
+ (forward-line))
+
+ (let (result)
+ (while (not (or (eobp) (looking-at "^End of search list.")))
+ (when (looking-at "^ +\\(.+?\\)\\( (framework directory)\\)?$")
+ ;; For now ignore framework directories
+ (unless (match-string 2)
+ (setq result (cons (file-truename (match-string 1)) result))))
+ (forward-line))
+
+ (reverse result)))
+
+(defun ede-compdb-clang-get-system-includes (clang)
+ "Return a list of system include paths by querying the compiler CLANG."
+ ;; The equivalent function in semantic/bovine/clang is obsolete, this version should work with modern clangs...
+ (with-temp-buffer
+ (call-process clang nil t nil "-x" "c++" "-Xclang" "-v" "-c" "-")
+ (goto-char (point-min))
+ (ede-compdb-parse-clang-system-includes)))
+
(defun ede-compdb-compiler-include-path (comp)
"Look up include paths for COMP and add to INCLUDE-PATHS."
(let ((path (cdr (assoc comp ede-compdb-compiler-cache))))
@@ -170,6 +194,9 @@
;; FIXME: this is pretty simplistic but it will do for now...
(setq path (cdr (assoc '--with-gxx-include-dir
(semantic-gcc-fields (semantic-gcc-query comp "-v")))))
+ ;; Try clang if GCC fails
+ (unless path
+ (setq path (ede-compdb-clang-get-system-includes comp)))
(add-to-list 'ede-compdb-compiler-cache (cons comp path)))
path))
@@ -219,9 +246,10 @@ from the command line (which is most of them!)"
(setq seenopt t)
)))
- (let ((cpath (ede-compdb-compiler-include-path (oref this compiler))))
- (when cpath
- (object-add-to-list this :include-path cpath t)))
+ (oset this include-path
+ (nconc
+ (oref this include-path)
+ (ede-compdb-compiler-include-path (oref this compiler))))
))

0 comments on commit f29298b

Please sign in to comment.