cleanup: Add and use macro MAX().
[cparser] / main.c
diff --git a/main.c b/main.c
index 39270af..876416a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,21 +1,6 @@
 /*
  * This file is part of cparser.
- * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
  */
 #include <config.h>
 
@@ -61,6 +46,8 @@
 #include <libfirm/be.h>
 #include <libfirm/statev.h>
 
+#include "adt/util.h"
+#include "ast_t.h"
 #include "preprocessor.h"
 #include "token_t.h"
 #include "types.h"
@@ -76,6 +63,7 @@
 #include "adt/error.h"
 #include "adt/strutil.h"
 #include "adt/array.h"
+#include "symbol_table.h"
 #include "wrappergen/write_fluffy.h"
 #include "wrappergen/write_jna.h"
 #include "wrappergen/write_compoundsizes.h"
@@ -913,9 +901,7 @@ static unsigned decide_modulo_shift(unsigned type_size)
 {
        if (architecture_modulo_shift == 0)
                return 0;
-       if (type_size < architecture_modulo_shift)
-               return architecture_modulo_shift;
-       return type_size;
+       return MAX(type_size, architecture_modulo_shift);
 }
 
 static bool is_ia32_cpu(const char *architecture)
@@ -991,19 +977,19 @@ static void init_types_and_adjust(void)
        atomic_type_properties_t *props = atomic_type_properties;
 
        /* adjust types as requested by target architecture */
-       ir_type *type_long_double = be_params->type_long_double;
-       if (type_long_double != NULL) {
-               set_typeprops_type(&props[ATOMIC_TYPE_LONG_DOUBLE], type_long_double);
-               atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = get_type_mode(type_long_double);
+       ir_type *const type_ld = be_params->type_long_double;
+       if (type_ld) {
+               set_typeprops_type(&props[ATOMIC_TYPE_LONG_DOUBLE], type_ld);
+               atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = get_type_mode(type_ld);
        }
 
-       ir_type *type_long_long = be_params->type_long_long;
-       if (type_long_long != NULL)
-               set_typeprops_type(&props[ATOMIC_TYPE_LONGLONG], type_long_long);
+       ir_type *const type_ll = be_params->type_long_long;
+       if (type_ll)
+               set_typeprops_type(&props[ATOMIC_TYPE_LONGLONG], type_ll);
 
-       ir_type *type_unsigned_long_long = be_params->type_unsigned_long_long;
-       if (type_unsigned_long_long != NULL)
-               set_typeprops_type(&props[ATOMIC_TYPE_ULONGLONG], type_unsigned_long_long);
+       ir_type *const type_ull = be_params->type_unsigned_long_long;
+       if (type_ull)
+               set_typeprops_type(&props[ATOMIC_TYPE_ULONGLONG], type_ull);
 
        /* operating system ABI specifics */
        if (firm_is_darwin_os(target_machine)) {
@@ -1048,14 +1034,14 @@ static void init_types_and_adjust(void)
        props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
 
        /* initialize defaults for unsupported types */
-       if (type_long_long == NULL) {
+       if (!type_ll) {
                copy_typeprops(&props[ATOMIC_TYPE_LONGLONG], &props[ATOMIC_TYPE_LONG]);
        }
-       if (type_unsigned_long_long == NULL) {
+       if (!type_ull) {
                copy_typeprops(&props[ATOMIC_TYPE_ULONGLONG],
                               &props[ATOMIC_TYPE_ULONG]);
        }
-       if (type_long_double == NULL) {
+       if (!type_ld) {
                copy_typeprops(&props[ATOMIC_TYPE_LONG_DOUBLE],
                               &props[ATOMIC_TYPE_DOUBLE]);
        }
@@ -1215,8 +1201,10 @@ static void copy_file(FILE *dest, FILE *input)
 {
        char buf[16384];
 
-       while (!feof(input) && !ferror(dest)) {
+       for (;;) {
                size_t read = fread(buf, 1, sizeof(buf), input);
+               if (read == 0)
+                       break;
                if (fwrite(buf, 1, read, dest) != read) {
                        perror("could not write output");
                }
@@ -1279,13 +1267,11 @@ static int compilation_loop(compile_mode_t mode, compilation_unit_t *units,
 again:
                switch (unit->type) {
                case COMPILATION_UNIT_IR: {
-                       bool res = open_input(unit);
-                       if (!res) {
+                       if (!open_input(unit)) {
                                result = EXIT_FAILURE;
                                break;
                        }
-                       res = !ir_import_file(unit->input, unit->name);
-                       if (!res) {
+                       if (ir_import_file(unit->input, unit->name)) {
                                position_t const pos = { inputname, 0, 0, 0 };
                                errorf(&pos, "import of firm graph failed");
                                result = EXIT_FAILURE;
@@ -1298,8 +1284,7 @@ again:
                        if (external_preprocessor == NULL) {
                                panic("preprocessed assembler not possible with internal preprocessor yet");
                        }
-                       bool res = run_external_preprocessor(unit);
-                       if (!res) {
+                       if (!run_external_preprocessor(unit)) {
                                result = EXIT_FAILURE;
                                break;
                        }
@@ -1321,8 +1306,7 @@ again:
                case COMPILATION_UNIT_C:
                case COMPILATION_UNIT_CXX:
                        if (external_preprocessor != NULL) {
-                               bool res = run_external_preprocessor(unit);
-                               if (!res) {
+                               if (!run_external_preprocessor(unit)) {
                                        result = EXIT_FAILURE;
                                        break;
                                }
@@ -1332,16 +1316,14 @@ again:
 
                case COMPILATION_UNIT_PREPROCESSED_C:
                case COMPILATION_UNIT_PREPROCESSED_CXX: {
-                       bool res = open_input(unit);
-                       if (!res) {
+                       if (!open_input(unit)) {
                                result = EXIT_FAILURE;
                                break;
                        }
                        init_tokens();
 
                        if (mode == PreprocessOnly) {
-                               bool res = output_preprocessor_tokens(unit, out);
-                               if (!res) {
+                               if (!output_preprocessor_tokens(unit, out)) {
                                        result = EXIT_FAILURE;
                                        break;
                                }
@@ -1605,7 +1587,7 @@ int main(int argc, char **argv)
                        if (option[0] == 'o') {
                                GET_ARG_AFTER(outname, "-o");
                        } else if (option[0] == 'g') {
-                               /* TODO: parse -gX with 0<=x<=3... */
+                               /* TODO: parse -gX with 0<=X<=3... */
                                set_be_option("debug=frameinfo");
                                set_be_option("ia32-optcc=false");
                        } else if (SINGLE_OPTION('c')) {
@@ -1652,12 +1634,12 @@ int main(int argc, char **argv)
                                        errorf(NULL, "unknown language '%s'", opt);
                                        argument_errors = true;
                                }
-                       } else if (streq(option, "M")) {
+                       } else if (SINGLE_OPTION('M')) {
                                mode = PreprocessOnly;
                                add_flag(&cppflags_obst, "-M");
                        } else if (streq(option, "MMD") ||
                                   streq(option, "MD")) {
-                           construct_dep_target = true;
+                               construct_dep_target = true;
                                add_flag(&cppflags_obst, "-%s", option);
                        } else if (streq(option, "MM")  ||
                                   streq(option, "MP")) {
@@ -1795,8 +1777,7 @@ int main(int argc, char **argv)
                                                   streq(opt, "stack-protector-all")) {
                                                fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
                                        } else {
-                                               int res = firm_option(orig_opt);
-                                               if (res == 0) {
+                                               if (firm_option(orig_opt) == 0) {
                                                        errorf(NULL, "unknown Firm option '-f%s'", orig_opt);
                                                        argument_errors = true;
                                                        continue;
@@ -1811,8 +1792,7 @@ int main(int argc, char **argv)
                                        fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
                                        help |= HELP_FIRM;
                                } else {
-                                       int res = be_parse_arg(opt);
-                                       if (res == 0) {
+                                       if (be_parse_arg(opt) == 0) {
                                                errorf(NULL, "unknown Firm backend option '-b %s'", opt);
                                                argument_errors = true;
                                        } else if (strstart(opt, "isa=")) {
@@ -1845,7 +1825,7 @@ int main(int argc, char **argv)
                                           || streq(option+1, "format-security")
                                           || streq(option+1, "old-style-declaration")
                                           || streq(option+1, "type-limits")) {
-                                   /* ignore (gcc compatibility) */
+                                       /* ignore (gcc compatibility) */
                                } else {
                                        set_warning_opt(&option[1]);
                                }
@@ -1887,14 +1867,12 @@ int main(int argc, char **argv)
                                } else if (strstart(opt, "tune=")) {
                                        GET_ARG_AFTER(opt, "-mtune=");
                                        snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
-                                       int res = be_parse_arg(arch_opt);
-                                       if (res == 0)
+                                       if (be_parse_arg(arch_opt) == 0)
                                                argument_errors = true;
                                } else if (strstart(opt, "cpu=")) {
                                        GET_ARG_AFTER(opt, "-mcpu=");
                                        snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
-                                       int res = be_parse_arg(arch_opt);
-                                       if (res == 0)
+                                       if (be_parse_arg(arch_opt) == 0)
                                                argument_errors = true;
                                } else if (strstart(opt, "fpmath=")) {
                                        GET_ARG_AFTER(opt, "-mfpmath=");
@@ -1908,15 +1886,13 @@ int main(int argc, char **argv)
                                        }
                                        if (!argument_errors) {
                                                snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
-                                               int res = be_parse_arg(arch_opt);
-                                               if (res == 0)
+                                               if (be_parse_arg(arch_opt) == 0)
                                                        argument_errors = true;
                                        }
                                } else if (strstart(opt, "preferred-stack-boundary=")) {
                                        GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
                                        snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
-                                       int res = be_parse_arg(arch_opt);
-                                       if (res == 0)
+                                       if (be_parse_arg(arch_opt) == 0)
                                                argument_errors = true;
                                } else if (streq(opt, "rtd")) {
                                        default_calling_convention = CC_STDCALL;
@@ -1926,8 +1902,7 @@ int main(int argc, char **argv)
                                } else if (streq(opt, "soft-float")) {
                                        add_flag(&ldflags_obst, "-msoft-float");
                                        snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=softfloat", cpu_arch);
-                                       int res = be_parse_arg(arch_opt);
-                                       if (res == 0)
+                                       if (be_parse_arg(arch_opt) == 0)
                                                argument_errors = true;
                                } else if (streq(opt, "sse2")) {
                                        /* ignore for now, our x86 backend always uses sse when
@@ -2289,11 +2264,11 @@ int main(int argc, char **argv)
 
        /* link program file */
        if (mode == CompileAssembleLink) {
-               int result = link_program(units);
-               if (result != EXIT_SUCCESS) {
+               int const link_result = link_program(units);
+               if (link_result != EXIT_SUCCESS) {
                        if (out != stdout)
                                unlink(outname);
-                       return result;
+                       return link_result;
                }
        }