Using Sparse to Test C Code

Does anyone have any experience with Sparse ? It seems that I can not find any documentation, so the warnings and errors that it produces are not clear to me. I tried checking the mailing list and man page, but this is actually not enough either.

For example, I use INT_MAX in one of my files. This generates an error (undefined identifier), although I #include limits.h.

Is there a place to explain errors and warnings?

+4
source share
2 answers

Sparse is not meant to be lint, for example. Sparse is designed to create a parsing tree for arbitrary code so that it can be further analyzed.

In your example, you either want to define GNU_SOURCE (which I suppose __GNUC__ is included) that provides the bit you need in limits.h

I would not independently determine __GNUC__, since several of the actions that it activates can behave as undefined without all the other switches that GNU_SOURCE turns on.

My point is not to help you make a squash mistake by mistake, to repeat that sparse is mainly used as a library, and not as a standalone static analysis tool.

From my copy of README (not sure if I have the current version):

This means that a user of the library will literally just need to do struct string_list *filelist = NULL; char *file; action(sparse_initialize(argc, argv, filelist)); FOR_EACH_PTR_NOTAG(filelist, file) { action(sparse(file)); } END_FOR_EACH_PTR_NOTAG(file); and he is now done - having a full C parse of the file he opened. The library doesn't need any more setup, and once done does not impose any more requirements. The user is free to do whatever he wants with the parse tree that got built up, and needs not worry about the library ever again. There is no extra state, there are no parser callbacks, there is only the parse tree that is described by the header files. The action function takes a pointer to a symbol_list and does whatever it likes with it. The library also contains (as an example user) a few clients that do the preprocessing, parsing and type evaluation and just print out the results. These clients were done to verify and debug the library, and also as trivial examples of what you can do with the parse tree once it is formed, so that users can see how the tree is organized. 

Incoming clients are more “functional test suites and examples” than anything. This is a very useful tool, but you can consider a different angle of use if you want to use it. I like it because it does not use * lex / bison, which makes it a lot easier to crack.

+5
source

If you look at limits.h, you will see that INT_MAX is defined inside this #if

 /* If we are not using GNU CC we have to define all the symbols ourself. Otherwise use gcc definitions (see below). */ #if !defined __GNUC__ || __GNUC__ < 2 

to make it work, you have to determine __GNUC__ , before turning limits .h

+1
source

All Articles