build: allow use of LLVM_CONFIG to override llvm-config config script

On systems like ubuntu 12.04, llvm-config is llvm 2.9, but
llvm-config-3.0 and llvm-config-3.3 (for example) are versions 3.0 and
3.3 respectively. Allow overriding the name/path of the llvm-config
script so people can use these versioned config scripts

Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
Reviewed-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Christopher Li <sparse@chrisli.org>
sparse{i,c}: use LLVM_CONFIG to find llc and lli

Some systems have multiple llvm versions installed, and have prefixed
executables ("<exec>-<version>"). While we could require the user to
specify a variable for each executable (LLC, LLI), using llvm-config
--bindir to locate them and allowing them to override using LLVM_CONFIG
makes much less work.

Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
parse: support c99 [static ...] in abstract array declarators

The n1570 specifies (in 6.7.6.2.3) that either type-qualifiers
(ie: "restrict") come first and are followed by "static" or the
opposite ("static" then type-qualifiers).

Also add a test.

Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
lib.c: skip --param parameters

Very dumb patch to just skip --param allow-store-data-races=0 introduced in
newer GCC versions.

Without this patch sparse recognizes parameter of the --param option as a file
name which obviously couldn't be found.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
Minor clean up for option handling

Remove the loop for short option.

Signed-off-by: Christopher Li <sparse@chrisli.org>
sparse: make bits_to_bytes round up instead of down

Currently, sparse handles arrays of bools incorrectly. If you declare an
array of bools like this:

    static _Bool boolarray[3] = {
        [0] = 1,
        [1] = 1,
    };

...you get warnings like this (which are bogus):

    ./test.c:2:10: warning: Initializer entry defined twice
    ./test.c:3:10:   also defined here

The problem is that bits_to_bytes rounds down instead of up, and sparse
defaults to _Bool being only 1 bit in size. This causes sparse to think
that they sit within the same byte, when they do not.

Fix bits_to_bytes to round up instead of down, and fix the call in
init_ctype to no longer correct for it. Also add a validation
test to ensure that we don't break this in the future.

Signed-off-by: Jeff Layton <jlayton@primarydata.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
round up the array element size to byte align

When layout the array element, the element size should
round up to byte align.

Signed-off-by: Christopher Li <sparse@chrisli.org>
Fix scoping of extern symbols in block scope

We'd only match them with other symbols marked 'extern', but really, we
should match them with any top-level non-static symbol.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Christopher Li <sparse@chrisli.org>
Make same_symbol list share the same scope

Consider the following case, extern inline declare after
extern declare of the same function.

extern int g(int);
extern __inline__ int g(int x)
{
        return x;
}

Sparse will give the first function global scope and
the second one file scope. Also the first one will get the
function body from the second one. That cause the failure
of the validation/extern-inlien.c

This change rebind the scope of the same_symbol chain to
the new scope. It will pass the extern-inline.c test case.

Signed-off-by: Christopher Li <sparse@chrisli.org>
Fix initializers in anonymous structs and unions

The "trivial fix" is to just save off the offset in check_designators
(add a new field to the "EXPR_IDENTIFIER" part of "struct expression"
and then pick up that offset in "convert_ident()"

However, that has exactly the same issue with the whole fake
EXPR_IDENTIFIER created by "next_designators()". Now we need to
initialize the offset field there too. And, for the same reason as
before, the field that "next_designator()" picks may not *have* an
identifier, because the next designator may in fact be an anonymous
union.

Anyway, with all that said, maybe this really confusing and hacky
patch would work. It passes my tests. The magic offset calculation in
next_designators() has a big comment about what the heck it is doing,
and otherwise it's a fairly straightforward case of "save offset from
check_designators() time to be used by convert_ident()".

My stupid test-case is this incredibly hacky thing:

    struct A {
        int aa;
        int bb;
    };

    struct S {
        int a;
        union {
            int b;
            int c;
        } u[10];
        int dummy;
        union {
            int d;
            int e;
        };
    };

    int fn(void)
    {
        struct S s = {
            .a = 1,
            .u[2].b = 2,
            .dummy = 1,
             { 3 }
        };
        return s.dummy*1000 + s.d*100 + s.u[2].b*10 + s.a; // 1321
    }

where I use "./test-linearize" to verify that the initializer layout
matches the code generation layout (so that 'fn' function *should*
linearize to just a simple "ret.32 $1321", and with this patch it
does).

But I bet this misses some case. However, the current state wrt
initializer offsets really is very broken, so maybe it's ok.

Signed-off-by: Christopher Li <sparse@chrisli.org>
sparse-llvm: Fix LLVM 3.5 linker errors

llvm-config 3.5 no longer lists the non-LLVM libraries needed for linkage when
passed --libs. The --system-libs flag was added for this purpose. This adds
these libraries while silently doing nothing for older versions of LLVM.

Signed-off-by: Omar Sandoval <osandov@osandov.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
Use LLVM_CONFIG instead of llvm-config in Makefile

Signed-off-by: Pavel Roskin <proski@gnu.org>
Reviewed-by: Omar Sandoval <osandov@osandov.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
sparse: Make -Werror turn warnigns into errors

Make sparse fail and return an error code if a warning is encountered
and -Werror is specified or a hard error is found. This allows to use
sparse in automated build systems to more easily catch new sparse
warnings.

The validation script is extended to parse the expected output message
for an error message and validate the a non zero return value if such
a error message is found.

Also changes cgcc to die if the checker fails.

Signed-off-by: Thomas Graf <tgraf@suug.ch>
Signed-off-by: Christopher Li <sparse@chrisli.org>
rename -Werror to -Wsparse-error

Sparse is often share compile flags. So Werror is usually
mean gcc should treat warning as error. Apply the same
option to sparse will cause the Linux kernel checking fail
the build. We don't want that.

Rename the sparse option to -Wsparse-error. It allow caller
to control gcc and sparse behavior seperately.

It also make sparse return error status only when -Wsparse-error
is present.

Signed-off-by: Christopher Li <sparse@chrisli.org>
sparse: treat function pointers as pointers to const data

This code snippet:

static void bar(void const *arg)
{
	int (*foo)(void) = arg;
}

produces the following warning:

test.c:4:28: warning: incorrect type in initializer (different modifiers)
test.c:4:28:    expected int ( *foo )( ... )
test.c:4:28:    got void const *arg

which is caused by the fact that the function pointer 'foo' is not annotated
as being a pointer to const data. However, dereferencing a function pointer
does not produce an lvalue, so a function pointer points to const data by
definition, and we should treat it accordingly.

To avoid producing a warning on the inverse case, i.e.,

static void bar(void)
{
	void *foo = bar;
}

we only address the case where the function pointer is the target of
an assignment.

Reviewed-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Christopher Li <sparse@chrisli.org>
Add the __restrict__ keyword

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Christopher Li <sparse@chrisli.org>
sparse: add 'gnu_inline' to the ignored attributes

Add some more ignored attributes which are used in glibc header files,
along with a simple test case which includes all three inline attributes
(__gnu_inline__, __always_inline__ and __noinline__).

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Christopher Li <sparse@chrisli.org>
don't call isdigit/tolower with a char argument

This suppresses some "array subscript has type 'char'" warnings from
gcc (version 4.8.3). (see also, commit cf5114a1)

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Christopher Li <sparse@chrisli.org>
Makefile: suppress error message from shell

In particular, on systems which do not have 'llvm-config' installed,
every invocation of make issues the following messages:

    /bin/sh: llvm-config: command not found
    make: llvm-config: Command not found

A simple solution would be to suppress these messages by redirecting
stderr to the bit-bucket within the definitions of HAVE_LLVM_VERSION
and LLVM_VERSION.

As an alternative, however, we move the definition of LLVM_VERSION
down the file within the HAVE_LLVM conditional, which ensures that
the 'llvm-config' command exists. In addition, the HAVE_LLVM_VERSION
variable is replaced with an equivalent conditional expression.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Christopher Li <sparse@chrisli.org>
don't run sparse{c,i} tests when sparse-llvm is disabled

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Christopher Li <sparse@chrisli.org>