Use stdbool more widely

We already use the C99 bool type from stdbool.h in a few places.  However
there are many other places we represent boolean values as plain ints.
This patch changes that.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Pass 'unsigned char' type to isdigit()/isspace()/isprint() functions

The isdigit(), isprint(), etc. functions take an int, whose value is
required to be in the range of an _unsigned_ char, or EOF.  This, horribly,
means that systems which have a signed char by default need casts to pass
a char variable safely to these functions.

We can't do this more nicely by making the variables themselves 'unsigned
char *' because then we'll get warnings passing them to the strchr() etc.
functions.

At least the cygwin version of these functions, are designed to generate
warnings if this isn't done, as explained by this comment from ctype.h:
   These macros are intentionally written in a manner that will trigger
   a gcc -Wall warning if the user mistakenly passes a 'char' instead
   of an int containing an 'unsigned char'.

Signed-off-by: Serge Lamikhov-Center <Serge.Lamikhov@gmail.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Add option to run_tests.sh to stop immediately on a failing test

This is a debugging convenience option, which makes it much easier to find
the failing tests and fix them one by one.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Fix valgrind errors in sw_tree1

The sw_tree1 testcase has accumulated some valgrind errors, at least in
the "realloc" mode.
  * It had both a realloc_fdt() and explicit xmalloc() for the initial
allocation which was redundant and caused errors.
  * It doesn't make sense to call fdt_resize() until after we've created
the initial stub tree
  * Alignment gaps inserted into the tree contain uninitialized data, which
trips an error when we write it out.  We could zero the buffer, but that
would make it easier to miss real bugs, so we add suppressions for the
valgrind warnings instead.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Remove unused srcpos_warn() function

This function has no users, and we can replace it more generally later.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Fix typo in type of srcpos_verror() et al.

The srcpos_verror() and srcpos_error() functions declare the format
string as 'char const *' instead of 'const char *'.  Fix it.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Fix indentation of srcpos_verror()

Somehow this function ended up with a 7 space indent, instead of the usual
8 space (1 tab) indent.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Fix memory leak in srcpos_verror()

Since dtc runs are short, we don't care that much about memory leaks.
Still, leaking the source position string every time we print an error
messages is pretty nasty.  Fix it.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Make srcpos_{v,}error() more widely useful

Allow them to take a prefix argument giving the general type of error,
which will be useful in future.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Move integer literal processing back to the lexer

At the moment integer literals are passed from the lexer to the parser as
a string, where it's evaluated into an integer by eval_literal().  That
strange approach happened because we needed to know whether we were
processing dts-v0 or dts-v1 - only known at the parser level - to know
how to interpret the literal properly.

dts-v0 support has been gone for some time now, and the base and bits
parameters to eval_literal() are essentially useless.

So, clean things up by moving the literal interpretation back to the lexer.
This also introduces a new lexical_error() function to report malformed
literals and set the treesource_error flag so that they'll cause a parse
failure at the top level.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Move character literal processing to the lexer

To match the processing of integer literals, character literals are passed
as a string from lexer to parser then interpreted there.  This is just as
awkward as it was for integer literals, without the excuse that we used to
need the information about the dts version to process them correctly.

So, move character literal processing back to the lexer as well, cleaning
things up.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Die on failed /incbin/ seeks

Failing to open an input file, with /include/ or /incbin/ is treated as
immediately fatal inside srcfile_relative_open().  However, filing to
seek() to the requested offset in an /incbin/ is not.  This is a bit oddly
inconsistent, and leaves us with a strange case that's awkward to deal with
down the line.

So, get rid of it and have failed seeks on an /incbin/ be immediately
fatal.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Correct locations in parser error messaes

The print_error() function used in several places in the parser uses the
location information in yylloc to describe the location of the error.
This is not correct in most cases.  yylloc gives the location of the
lookahead token, whereas the error is generally associated with one of
the already parsed non-terminals.

This patch corrects this, adding a location parameter to print_error() and
supplying it with the appropriate bison @N symbols.

This probably breaks yacc compatiblity, but too bad - accurate error
messages are more important.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Clean up parser error messages

Generally edit parser error messages for brevity and clarity.  Replace
the print_error() function with a a new macro for brevity and clarity in
the source.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
dtc: avoid shadowing dirname()

dirname() is declared as a function, and later on try_open() another
variable dirname is used, which shadows the dirname() function
declaration and will produce the following warnings/errors with
-Wshadow turned on:

srcpos.c: In function 'try_open':
srcpos.c:71:35: error: declaration of 'dirname' shadows a global
declaration [-Werror=shadow]
srcpos.c:37:14: error: shadowed declaration is here [-Werror=shadow]
srcpos.c: In function 'srcfile_add_search_path':
srcpos.c:183:42: error: declaration of 'dirname' shadows a global
declaration [-Werror=shadow]
srcpos.c:37:14: error: shadowed declaration is here [-Werror=shadow]
cc1: all warnings being treated as errors

Fix this by renaming the function dirname() to get_dirname().

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
libfdt: avoid shadowing "err" in FDT_CHECK_HEADER

FDT_CHECK_HEADER declares an internal variable named "err" whose name is
far too generic and will produce the following -Wshadow warnings:

libfdt/fdt_ro.c: In function 'fdt_node_offset_by_compatible':
libfdt/fdt_ro.c:555:2: error: declaration of 'err' shadows a previous
local [-Werror=shadow]
libfdt/fdt_ro.c:553:14: error: shadowed declaration is here
[-Werror=shadow]
cc1: all warnings being treated as errors

Since this variable is only used internally in the macro, rename to
__err which should be prefixed enough not to cause new shadow warnings.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
libfdt: avoid shadowing "err" in FDT_RW_CHECK_HEADER

FDT_RW_CHECK_HEADER declares an internal variable named "err" which is
far too generic and will produce the following -Wshadow warnings:

libfdt/fdt_rw.c: In function 'fdt_add_mem_rsv':
libfdt/fdt_rw.c:177:2: error: declaration of 'err' shadows a previous
local [-Werror=shadow]
libfdt/fdt_rw.c:175:6: error: shadowed declaration is here
[-Werror=shadow]
libfdt/fdt_rw.c: In function 'fdt_del_mem_rsv':
libfdt/fdt_rw.c:194:2: error: declaration of 'err' shadows a previous
local [-Werror=shadow]
libfdt/fdt_rw.c:192:6: error: shadowed declaration is here
[-Werror=shadow]
libfdt/fdt_rw.c: In function 'fdt_set_name':
...

Since this variable is only used internally in the macro, rename to
__err which should be prefixed enough not to cause new shadow warnings.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
Makefile: enable -Wshadow by default

Now that all -Wshadow build warnings/errors are fixed, turn on -Wshadow
by default to make sure we would catch new potential shadow warnings.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
dtc: fix some more -Wshadow warnings

Building on a RHEL6 system produced the following -Wshadow warnings in
fstree.c, util.c and checks.c:

cc1: warnings being treated as errors
checks.c: In function 'parse_checks_option':
checks.c:709: error: declaration of 'optarg' shadows a global
declaration
/usr/include/getopt.h:59: error: shadowed declaration is here
make[1]: *** [checks.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make[1]: *** Waiting for unfinished jobs....
cc1: warnings being treated as errors
fstree.c: In function 'read_fstree':
fstree.c:40: error: declaration of 'tmpnam' shadows a global
declaration
/usr/include/stdio.h:208: error: shadowed declaration is here
make[1]: *** [fstree.o] Error 1
cc1: warnings being treated as errors
util.c: In function 'xstrdup':
util.c:42: error: declaration of 'dup' shadows a global declaration
/usr/include/unistd.h:528: error: shadowed declaration is here

Fix all of these -Wshadow warnings by using slightly different variable
names which won't collide with anything else.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>