Fix handling of __func__

It's not a string literal, it's a static array

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Fix tab handling in nextchar_slow()

... and streamline it a bit more, now that we get all \t hitting
the damn thing.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Fix ,##__VA_ARGS__ kludge

a) it actually allows any number of ##<arg>##... in between, as long as all
those args are empty.
b) it does *not* allow ## after __VA_ARGS__ - if it's there magic disappears

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
compile-i386: fix use-after-free in func_cleanup()

compile-i386 sometimes crashes due a use-after-free error.  Since
f->pseudo_list is freed first, which invalidates some atom->op* in
f->atom_list.  Further checks like `atom->op1->flags & STOR_WANTS_FREE'
will read garbage, which may lead to a double free.

This patch switches the cleanup order and frees f->atom_list first.
Those marked as STOR_WANTS_FREE won't appear in f->pseudo_list.

Signed-off-by: Xi Wang <xi.wang@gmail.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
Reviewed-by: Pekka Enberg <penberg@kernel.org>
check missing or duplicate goto labels

This patch sets ->stmt of a SYM_LABEL to the corresponding label
statement.  If ->stmt was already set, it is a duplicate label.

On the other hand, if ->stmt of a goto label is not set during
evaluation, the label was never declared.

Signed-off-by: Christopher Li <sparse@chrisli.org>
sparse, llvm: Add _Bool to cast validation test

This patch adds casting backend validation test cases for the _Bool data type.

Signed-off-by: Pekka Enberg <penberg@kernel.org>
sparse, llvm: Simplify output_data() type logic

Use LLVMTypeOf() in output_data() to simplify type handling logic.

Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Christopher Li <sparse@chrisli.org>
Acked-by: Jeff Garzik <jgarzik@redhat.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
sparse, llvm: Fix string initializer code generation

This fixes code generation for string initializer such as:

  static char *foo = "Foo !\n";

It's the same fix Ben proposed earlier with the small difference that we now
use LLVMTypeOf() instead of symbol_type() to resolve the type.

The generated LLVM IR looks as follows:

  [penberg@tux sparse]$ ./sparse-llvm foo.c |llvm-dis
  ; ModuleID = '<stdin>'

  @"<noident>" = private global [7 x i8] c"Foo !\0A\00"
  @foo = private global [7 x i8]* @"<noident>"

Reported-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Christopher Li <sparse@chrisli.org>
Acked-by: Jeff Garzik <jgarzik@redhat.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
sparse, llvm: Fix global string access code generation

This patch attempts to fix code generation for global string access:

  static char *foo = "Foo !\n";

  extern int puts(const char *s);

  int main(int argc, char *argv[])
  {
          puts(foo);

          return 0;
  }

The generated executable causes a SIGSEGV:

  [penberg@tux sparse]$ ./sparsec foo.c && ./a.out
  Segmentation fault

But as pointed out by Xi Wang, the problem is in "output_load()":

  I guess the problem is that sparse-llvm generates an incorrect type `load i64*'
  in llvm from the sparse instruction load.64.

         load.64     %r2 <- 0[foo]
         call.32     %r1 <- puts, %r2
         ret.32      $0

  With the new ->ctype in pseudo sparse-llvm should be able to generate the
  correct type.  I am playing with an LLVM backend with typed pseudos; it
  generates the following code, which seems okay.

  @0 = internal global [7 x i8] c"Foo !\0A\00", align 1
  @foo = internal global i8* getelementptr inbounds ([7 x i8]* @0, i64 0, i64 0), align 8

  define i32 @main(i32 %argc, i8** %argv) {
  entry:
   %0 = load i8** @foo
   %1 = call i32 @puts(i8* %0)
   ret i32 0
  }

So I'll leave the output_load() fix to a separate patch for now.

Cc: Xi Wang <xi.wang@gmail.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Christopher Li <sparse@chrisli.org>
Acked-by: Jeff Garzik <jgarzik@redhat.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
Merge branch 'for-chris' of git://github.com/penberg/sparse-llvm

Pull 'for-chris' tree from Pekka Enberg:

"Please pull the latest sparse-llvm tree from:
 git@github.com:penberg/sparse-llvm.git for-chris

 It has one new validation test case and fixes to handle strings properly
 in the LLVM backend."

Pekka Enberg (4):
      sparse, llvm: Add _Bool to cast validation test
      sparse, llvm: Simplify output_data() type logic
      sparse, llvm: Fix string initializer code generation
      sparse, llvm: Fix global string access code generation
sparse: Add '__vector_size__' to ignored attributes

We already had "vector_size" but we also need __vector_size__ to silence
some warnings in glibc:

/usr/include/bits/link.h:67:45: error: attribute '__vector_size__': unknown attribute

Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
sparse: Add 'error' to ignored attributes

Add some more ignored attributes which are used in glibc header files.  It
silences the warnings such as the following:

/usr/include/bits/fcntl2.h:36:1: error: attribute '__error__': unknown attribute

Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
FAQ: update the website address and call it Wiki

Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
Signed-off-by: Christopher Li <sparse@chrisli.org>
ptrlist.c: fix a typo in a comment

Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
Signed-off-by: Christopher Li <sparse@chrisli.org>
sparse, llvm: Fix SIGSEGV for extern symbols

Jonathan Neuschäfer writes:

  compiling a little real-world program with sparse-llvm, it segfaulted.
  Using a tool called "delta"[1] and some bash scripting, I managed to
  reduce the code to this test case:

    extern struct foo *foop;
    extern void func(struct foo *f);
    int main(int argc, char **argv) {
        func(foop);
    }

The problem is that pseudo_to_value() does not know abou the extern
symbol because Sparse never calls output_data() on it which registers
globals with LLVMAddGlobal().

As explained by Linus, 'extern' symbols are just names with types. They
don't have any value associated with them, they just have the type and
the name. Therefore we need to explicitly call LLVMAddGlobal() for
symbols we have not encountered in pseudo_to_value().

Reported by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
Cc: Christopher Li <sparse@chrisli.org>
Cc: Jeff Garzik <jgarzik@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
sparse, llvm: 'Verify' the LLVM module before writing it

In later versions sparse-llvm should stop on wrong llvm code, instead
of outputting it, but for now there are too many warnings being produced
for this to be feasible (I think).

Cc: Pekka Enberg <penberg@kernel.org>
Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
sparse, llvm: Fix 'void' return type code generation

Jonathan Neuschäfer reports:

  A simple function like this will compile to the following llvm
  bitcode:

        /* C */
        void func(void) {
                return;
        }

        /* LLVM */
        define i8 @func() {
        L0:
          ret void
        }

  The return type of the function and the type in the return instruction
  don't match.

  I found this inconsistency by running LLVM's bitcode validation on the
  bitcode produced by sparse-llvm.

Move 'void *' special-casing from sym_basetype_type() to sym_ptr_type()
to fix the issue.

Reported by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
Cc: Christopher Li <sparse@chrisli.org>
Cc: Jeff Garzik <jgarzik@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
sparse, llvm: convert the condition of branch/select to bool

LLVM expects the first argument of "br" and "select" to be of type i1,
so add an "icmp ne <srcty> %src, 0" for other types.

Cc: Christopher Li <sparse@chrisli.org>
Cc: Jeff Garzik <jgarzik@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
sparse, llvm: Fix type of loaded values

Instead of making the computed address a pointer to an int type
large enough to hold a pointer, make it a pointer to the memory
object being loaded.  This fixes another LLVM warning.

Cc: Pekka Enberg <penberg@kernel.org>
Cc: Christopher Li <sparse@chrisli.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Acked-by: Jeff Garzik <jgarzik@redhat.com>
Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
Merge branch 'llvm/core' of github.com:penberg/sparse-llvm

Please pull the latest Sparse/LLVM tree from:

  git@github.com:penberg/sparse-llvm.git llvm/core

It contains few LLVM backend fixes from myself and Jonathan Neuschäfer.