Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hard to setup complex include paths for flycheck-clang projects #659

Closed
stsquad opened this issue Jun 10, 2015 · 8 comments
Closed

Hard to setup complex include paths for flycheck-clang projects #659

stsquad opened this issue Jun 10, 2015 · 8 comments

Comments

@stsquad
Copy link

stsquad commented Jun 10, 2015

I'm trying to improve my use of flycheck-clang with QEMU. The problem is the build includes multiple target binaries and multiple source directories. I've tried manually adding include paths, e.g:

(setq flycheck-clang-include-path '("./include/" "./aarch64-softmmu/" "./target-arm/"))

However this falls down as you go into sub-directories so the pathes need to be relative to the project root.

What would make this easier is if the clang checker supported a config file and then I could modify the the make system to spit out something useful for the particular thing I'm building.

@swsnr
Copy link
Contributor

swsnr commented Jun 10, 2015

@stsquad The paths are not relative to the project root. That's documented, actually. You need to use absolute paths, ideally via .dir-locals.el—which is how these options are intended to be used.

Alternatively add a hook to C Mode which automatically computes the absolute include paths by resolving the relative paths against the project root (which you could obtain from Projectile, for instance), e.g. something like this:

(add-hook 'c-mode-hook (lambda () (set flycheck-clang-include-path (mapcar (lambda (p) (expand-file-name p (projectile-project-root)) my-relative-include-paths)

I know that Clang supports “compilation database” files, which appear to be JSON files with flags that common build systems like CMake can generate, but I do not know how to make Clang load such databases, and I do not have the time to find out. I don't use C/C++. Pull requests welcome.

For complex C/C++ packages, you may also want to look at Irony Mode and Flycheck Irony or similar extensions.

I'm closing this issue, since it's more a question than a problem in Flycheck, and in any case not something that I'd fix, for said reasons.

@swsnr swsnr closed this as completed Jun 10, 2015
@swsnr swsnr added the question label Jun 10, 2015
@stsquad
Copy link
Author

stsquad commented Jun 10, 2015

OK I'm getting there. You can build a compilation database with a tool called bear:

./configure <blah>
bear make

And then check any file using clang-check:

clang-check cputlb.c

So I've started adding another flychecker type:

(flycheck-define-checker c/c++-clang-check
  "A C/C++ syntax checker using Clang.

See URL `http://clang.llvm.org/'."
  :command ("clang-check"
            "--extra-arg=-Wno-unknown-warning-option" ; silence GCC options
            "--extra-arg=-Wno-null-character"         ; silence null
            "--extra-arg=-fno-color-diagnostics"      ; Do not include color codes in output
            "--extra-arg=-fno-caret-diagnostics"      ; Do not visually indicate the source
            "--extra-arg=-fno-diagnostics-show-option" ; Do not show the corresponding
            (eval flycheck-clang-args)
            source)
  :error-patterns
  ((error line-start
          (message "In file included from") " " (file-name) ":" line ":"
          line-end)
   (info line-start (file-name) ":" line ":" column
         ": note: " (optional (message)) line-end)
   (warning line-start (file-name) ":" line ":" column
            ": warning: " (optional (message)) line-end)
   (error line-start (file-name) ":" line ":" column
          ": " (or "fatal error" "error") ": " (optional (message)) line-end))
  :error-filter
  (lambda (errors)
    (let ((errors (flycheck-sanitize-errors errors)))
      (dolist (err errors)
        ;; Clang will output empty messages for #error/#warning pragmas without
        ;; messages.  We fill these empty errors with a dummy message to get
        ;; them past our error filtering
        (setf (flycheck-error-message err)
              (or (flycheck-error-message err) "no message")))
      (flycheck-fold-include-levels errors "In file included from")))
  :modes (c-mode c++-mode)
  :next-checkers ((warning . c/c++-cppcheck)))

Unfortunately this falls over because flycheck has moved the file to be compile checked. Is there any way to suppress this?

@swsnr
Copy link
Contributor

swsnr commented Jun 10, 2015

@stsquad source-original. And read the documentation, please.

@stsquad
Copy link
Author

stsquad commented Jun 10, 2015

@lunaryorn it took some finding, I figured it out from the source eventually. pull req incoming.

@swsnr
Copy link
Contributor

swsnr commented Jun 10, 2015

@stsquad It's in the docstring of flycheck-substitute-argument, isn't it?

@stsquad
Copy link
Author

stsquad commented Jun 10, 2015

@lunaryorn it is, but I missed it from the flycheck-define-checker info. I'll have to re-read because something has gone wrong...

@themattman
Copy link

I'd like to use the compilation database as well. I work with large codebases and can't list all include paths for flycheck. Does flycheck support the compile_commands.json format now?

@stsquad
Copy link
Author

stsquad commented Jul 12, 2017

@themattman see https://github.com/kumar8600/flycheck-clangcheck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants