alexmurray / flycheck-clang-analyzer

Integrate Clang Static Analyzer with flycheck for on-the-fly static analysis in Emacs
35 stars 5 forks source link

I get a "suspicious state" error #6

Closed hisnawi closed 7 years ago

hisnawi commented 7 years ago

I see this being reported in the minibuffer when opening a C file:

Suspicious state from syntax checker clang-analyzer: Flycheck checker clang-analyzer returned non-zero exit code 1, but its output contained no errors: In file included from /home/user/testinit.c:327:

alexmurray commented 7 years ago

Can you post the output from M-x flycheck-compile?

hisnawi commented 7 years ago

We use our own build system, compiler and assembler. Maybe that's why I am seeing this error. It has to match with Flycheck compiler, doesn't it?

Is there a way to suppress these warning in the mini buffer that come from flycheck-analyzer, since I want to keep it running in other projects where we use gcc.

alexmurray commented 7 years ago

Ok, I suggest adding a .dir-locals.el which adds clang-analyzer to flycheck-disabled-checkers

hisnawi commented 7 years ago

I tried both of these lines individually in .dir-locals.el but I still see clang-analyzer being fired.

((emacs-lisp-mode (flycheck-disabled-checkers clang-analyzer)))

(eval-after-load 'flycheck '(setq flycheck-disabled-checkers '(clang-analyzer)))

Could you advice on what I need to include in my file?

alexmurray commented 7 years ago

Try the following:

((nil . ((flycheck-disabled-checkers . (clang-analyzer)))))
alexmurray commented 7 years ago

Out of interest - what flycheck checker do you use for normal gcc projects? irony, rtags or the inbuilt c/c++-gcc or c/c++-clang?

alexmurray commented 7 years ago

I would like to try and make flycheck-clang-analyzer more robust so you don't get this error in the first place - so I want to make it so we don't even try and run if none of these standard checkers are running.

alexmurray commented 7 years ago

Also can you post the output of running M-x flycheck-verify-setup for this same buffer (without having first disabled the checker)?

hisnawi commented 7 years ago

Thanks for the hint. What is weird is that flycheck-analyzer works fine for me on the same project/same file at home (maybe I have more recent clang/gcc installed? I could only install older versions of gcc/clang because I have no root access). Here is the result of flycheck-clang-analyzer at home and work:

** @work 
Syntax checkers for buffer test_init.c in c-mode:

  irony
    - may enable:   yes
    - predicate:    t
    - Irony Mode:   enabled
    - irony-server: Found at /home/user/emacs.d-2/irony/bin/irony-server

  c/c++-clang
    * may enable: yes
    * executable: Found at /home/user/bin/clang

  c/c++-gcc
    - may enable: yes
    - executable: Found at /usr/bin/gcc

  c/c++-cppcheck
    - may enable: Automatically disabled!
    - executable: Not found

  clang-analyzer
    - may enable: yes
    - predicate:  t
    - executable: Found at /home/user/bin/clang
    - Backend:    irony

Flycheck Mode is enabled.  Use C-u C-c ! x to enable disabled checkers.

--------------------

Flycheck version: 31snapshot (package: 20170626.1707)
Emacs version:    25.2.1
System:           x86_64-unknown-linux-gnu
Window system:    x

** @home 
Syntax checkers for buffer test_init.c in c-mode:

  irony
    - may enable:   yes
    - predicate:    t
    - Irony Mode:   enabled
    - irony-server: Found at /home/user/emacs.d-2/irony/bin/irony-server

  c/c++-clang
    - may enable: yes
    - executable: Found at /home/linuxbrew/.linuxbrew/bin/clang

  c/c++-gcc
    - may enable: yes
    - executable: Found at /home/linuxbrew/.linuxbrew/bin/gcc

  c/c++-cppcheck
    - may enable: Automatically disabled!
    - executable: Not found

  clang-analyzer
    - may enable: yes
    - predicate:  t
    - executable: Found at /home/linuxbrew/.linuxbrew/bin/clang
    - Backend:    irony

Flycheck Mode is enabled.  Use C-u C-c ! x to enable disabled checkers.

--------------------

Flycheck version: 31snapshot (package: 20170626.1707)
Emacs version:    26.0.50.2
System:           x86_64-pc-linux-gnu
Window system:    x

And I have a question: ((nil . ((flycheck-disabled-checkers . (clang-analyzer))))) worked. but everytime I start emacs in that project it keep asking me to confirm that the file is safe to evaluate. Is there a way to suppress that message and always consider the file safe?

alexmurray commented 7 years ago

You could try adding the following to your emacs config:

(put 'flycheck-disabled-checkers 'safe-local-variable #'listp)

https://www.reddit.com/r/emacs/comments/6fatop/how_to_trust_code_in_dirlocalsel_and_pass_the_is/

(editing to change #'stringp to #'listp)

hisnawi commented 7 years ago

I did add this line:

(put 'flycheck-disabled-checkers 'safe-local-variable #'stringp)

But I still get this message. I put it in the :init section of use-package flycheck, in the :config and even in the very beginning of the init.el file. But I keep getting it.

alexmurray commented 7 years ago

Apologies - instead of #'stringp can you try #'listp?

hisnawi commented 7 years ago

Awesome...that worked.

Any advice on what to do to make clang-analyzer work for me at work? Is there more debug info I could get you to check where I am having my setup wrong?

alexmurray commented 7 years ago

Sure - if you could post the output of M-x flycheck-compile that would help.

alexmurray commented 7 years ago

Or if you can't share it, look at the output when running that on both home and work machines and see if you can give me a hint as to what is different...

hisnawi commented 7 years ago

Ok, so I take that back. my home file and work file were no in sync. I have now the latest version of the same file in both work and home and they show the same behaviour which is the following: This is the result of flycheck-compile - clang-analyzer:

-*- mode: compilation; default-directory: "/home/user/test/" -*-
Compilation started at Sun Jul  2 19:53:42

clang --analyze -I./headers -I./source/mon -I./source/debug -I./source/io/headers -I./source/ib/include -I./inc -fno-color-diagnostics -fno-caret-diagnostics -fno-diagnostics-show-option /workspace/user/new/source/test.c
In file included from /home/user/test/source/io/test.c:60:
In file included from ./source/io/headers/io_pkg.h:38:
./headers/montypes.h:248:4: error: unknown type name 'mon_t'
./headers/montypes.h:259:9: error: a parameter list without types is only allowed in a function
      definition

Compilation exited abnormally with code 1 at Sun Jul  2 19:53:42

And this is what I see in the Messages buffer:

Suspicious state from syntax checker clang-analyzer: Flycheck checker clang-analyzer returned non-zero exit code 1, but its output contained no errors: In file included from /home/user/test/source/io/flycheck_test.c:60:
./headers/montypes.h:248:4: error: unknown type name 'mon_t'
./headers/montypes.h:259:9: error: a parameter list without types is only allowed in a function definition

Try installing a more recent version of clang-analyzer, and please open a bug report if the issue persists in the latest release.  Thanks!
Compilation exited abnormally with code 1

Unfortunately I only see clang errors/warnings when I do list all flycheck errors (Irony) and I don't see (clang-analyzer) erros listed.

alexmurray commented 7 years ago

Ok - can you also share M-x irony-cdb-menu?

hisnawi commented 7 years ago
Compilation Database: irony-cdb-clang-complete

  Working Directory: /home/user/test/
  Compile Options:   -I./headers -I./source/mon -I./source/debug -I./source/io/headers -I./source/ib/include
alexmurray commented 7 years ago

Ok so we seem to extract the correct options out of irony - does completion etc work with irony in this buffer? Or are you missing something in your .clang_complete so that clang knows what mon_t is - or is one of the files missing a #include so that it knows the definition of mon_t?

hisnawi commented 7 years ago

Yes irony completion works fine. But I figured the issue. We configure some of out structures as a define that helps the compiler building (like I mentioned we use our own compile system). This is an example:

typedef struct monpte
{
 int x;
 int y;
} MKOFFSETS(monpte);

And since we are not using clang no gcc there is no way for Irony to figure this that this is a structure and will complain that it can't find it.

I think in this case my only option is to disable flycheck-analyzer and only depend on basic Irony clang checking. Unless you might know something that could help me with this situation.

alexmurray commented 7 years ago

Where is MKOFFSETS defined? Is this in another header which needs to be included? Or can you add an extra line to .clang_complete to define this somehow?

hisnawi commented 7 years ago

It is actually part of makefiles, all these defines get preprocessed through the make process and operate on these structures creating offsets for the files they are in. I don't think there is a way to make it work with Irony/clang.

alexmurray commented 7 years ago

What if you add something like -DMKOFFSETS(t)=t to .clang_complete?

hisnawi commented 7 years ago

Wow! That actually worked! Thank you very much Alex. I can now see clang-analyzer warnings and don't get the suspicious...etc message.

alexmurray commented 7 years ago

You're welcome - glad I could get it working for you.