cleanup: Resolve warnings about shadowed variables.
[cparser] / main.c
diff --git a/main.c b/main.c
index e66c207..9c8b0c3 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>
 
@@ -122,10 +107,10 @@ static int               verbose;
 static struct obstack    cppflags_obst;
 static struct obstack    ldflags_obst;
 static struct obstack    asflags_obst;
-static char              dep_target[1024];
 static const char       *outname;
 static bool              define_intmax_types;
 static const char       *input_encoding;
+static bool              construct_dep_target;
 
 typedef enum lang_standard_t {
        STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
@@ -266,7 +251,7 @@ static void add_flag(struct obstack *obst, const char *format, ...)
 #else
        /* escape stuff... */
        for (char *c = buf; *c != '\0'; ++c) {
-               switch(*c) {
+               switch (*c) {
                case ' ':
                case '"':
                case '$':
@@ -319,38 +304,8 @@ static char const* str_lang_standard(lang_standard_t const standard)
 
 static bool run_external_preprocessor(compilation_unit_t *unit)
 {
-       static const char *common_flags = NULL;
-
-       if (common_flags == NULL) {
-               obstack_1grow(&cppflags_obst, '\0');
-               const char *flags = obstack_finish(&cppflags_obst);
-
-               /* setup default defines */
-               add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
-               add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
-               add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
-               add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
-
-               add_flag(&cppflags_obst, "-U__VERSION__");
-               add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
-
-               if (define_intmax_types) {
-                       add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
-                       add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
-                       add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
-                       add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
-               }
-
-               if (flags[0] != '\0') {
-                       size_t len = strlen(flags);
-                       obstack_1grow(&cppflags_obst, ' ');
-                       obstack_grow(&cppflags_obst, flags, len);
-               }
-               obstack_1grow(&cppflags_obst, '\0');
-               common_flags = obstack_finish(&cppflags_obst);
-       }
-
-       assert(obstack_object_size(&cppflags_obst) == 0);
+       obstack_1grow(&cppflags_obst, '\0');
+       const char *flags = obstack_finish(&cppflags_obst);
 
        const char *preprocessor = getenv("CPARSER_PP");
        if (preprocessor != NULL) {
@@ -371,18 +326,56 @@ static bool run_external_preprocessor(compilation_unit_t *unit)
        if (lang)
                add_flag(&cppflags_obst, "-x%s", lang);
 
-       add_flag(&cppflags_obst, "-std=%s", str_lang_standard(unit->standard));
+       if (unit->type == COMPILATION_UNIT_C
+        || unit->type == COMPILATION_UNIT_CXX) {
+               add_flag(&cppflags_obst, "-std=%s", str_lang_standard(unit->standard));
 
-       obstack_printf(&cppflags_obst, "%s", common_flags);
+               /* setup default defines */
+               add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
+               add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
+               add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
+               add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
+
+               add_flag(&cppflags_obst, "-U__VERSION__");
+               add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
+
+               if (define_intmax_types) {
+                       add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
+                       add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
+                       add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
+                       add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
+               }
+       }
+       if (flags[0] != '\0') {
+               size_t len = strlen(flags);
+               obstack_1grow(&cppflags_obst, ' ');
+               obstack_grow(&cppflags_obst, flags, len);
+       }
 
        /* handle dependency generation */
-       if (dep_target[0] != '\0') {
+       if (construct_dep_target) {
+               static char dep_target[4096];
+               if (outname != 0) {
+                       size_t len = strlen(outname);
+                       if (len > sizeof(dep_target)-4) /* leave room for .d extension */
+                               len = sizeof(dep_target)-4;
+                       memcpy(dep_target, outname, len);
+                       /* replace extension with .d if found */
+                       char *dot = &dep_target[len-1];
+                       for ( ; dot >= dep_target && *dot != '/'; --dot) {
+                               if (*dot == '.') {
+                                       dot[1] = 'd';
+                                       len = (dot-dep_target)+2;
+                                       break;
+                               }
+                       }
+                       dep_target[len] = '\0';
+               } else {
+                       get_output_name(dep_target, sizeof(dep_target), unit->name, ".d");
+               }
+
                add_flag(&cppflags_obst, "-MF");
                add_flag(&cppflags_obst, dep_target);
-               if (outname != NULL) {
-                       add_flag(&cppflags_obst, "-MQ");
-                       add_flag(&cppflags_obst, outname);
-               }
        }
        assert(unit->input == NULL);
        add_flag(&cppflags_obst, unit->name);
@@ -394,7 +387,7 @@ static bool run_external_preprocessor(compilation_unit_t *unit)
        }
        FILE *f = popen(commandline, "r");
        if (f == NULL) {
-               source_position_t const pos = { unit->name, 0, 0, 0 };
+               position_t const pos = { unit->name, 0, 0, 0 };
                errorf(&pos, "invoking preprocessor failed");
                return false;
        }
@@ -446,7 +439,7 @@ static void assemble(const char *out, const char *in)
        }
        int err = system(commandline);
        if (err != EXIT_SUCCESS) {
-               source_position_t const pos = { in, 0, 0, 0 };
+               position_t const pos = { in, 0, 0, 0 };
                errorf(&pos, "assembler reported an error");
                exit(EXIT_FAILURE);
        }
@@ -479,7 +472,7 @@ static void print_file_name(const char *file)
        }
        int err = system(commandline);
        if (err != EXIT_SUCCESS) {
-               source_position_t const pos = { file, 0, 0, 0 };
+               position_t const pos = { file, 0, 0, 0 };
                errorf(&pos, "linker reported an error");
                exit(EXIT_FAILURE);
        }
@@ -550,13 +543,13 @@ static FILE *make_temp_file(const char *prefix, const char **name_result)
        char *name = obstack_finish(&file_obst);
        int fd = mkstemp(name);
        if (fd == -1) {
-               source_position_t const pos = { name, 0, 0, 0 };
+               position_t const pos = { name, 0, 0, 0 };
                errorf(&pos, "could not create temporary file: %s", strerror(errno));
                return NULL;
        }
        FILE *out = fdopen(fd, "w");
        if (out == NULL) {
-               source_position_t const pos = { name, 0, 0, 0 };
+               position_t const pos = { name, 0, 0, 0 };
                errorf(&pos, "could not open temporary file as FILE*");
                return NULL;
        }
@@ -667,6 +660,7 @@ static void print_help_preprocessor(void)
        put_help("-D SYMBOL[=value]",        "");
        put_help("-U SYMBOL",                "");
        put_help("-Wp,OPTION",               "Pass option directly to preprocessor");
+       put_help("-Xpreprocessor OPTION",    "Pass option directly to preprocessor");
        put_help("-M",                       "");
        put_help("-MD",                      "");
        put_help("-MMD",                     "");
@@ -779,7 +773,10 @@ static void print_help_linker(void)
        put_help("-s",                       "Do not produce symbol table and relocation information");
        put_help("-shared",                  "Produce a shared library");
        put_help("-static",                  "Produce statically linked binary");
+       put_help("-Wa,OPTION",               "Pass option directly to assembler");
+       put_help("-Xassembler OPTION",       "Pass option directly to assembler");
        put_help("-Wl,OPTION",               "Pass option directly to linker");
+       put_help("-Xlinker OPTION",          "Pass option directly to linker");
 }
 
 static void print_help_debug(void)
@@ -979,19 +976,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)) {
@@ -1036,14 +1033,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]);
        }
@@ -1199,6 +1196,18 @@ static bool output_preprocessor_tokens(compilation_unit_t *unit, FILE *out)
        return res && error_count == 0;
 }
 
+static void copy_file(FILE *dest, FILE *input)
+{
+       char buf[16384];
+
+       while (!feof(input) && !ferror(dest)) {
+               size_t read = fread(buf, 1, sizeof(buf), input);
+               if (fwrite(buf, 1, read, dest) != read) {
+                       perror("could not write output");
+               }
+       }
+}
+
 static bool open_input(compilation_unit_t *unit)
 {
        /* input already available as FILE? */
@@ -1212,7 +1221,7 @@ static bool open_input(compilation_unit_t *unit)
        } else {
                unit->input = fopen(inputname, "r");
                if (unit->input == NULL) {
-                       source_position_t const pos = { inputname, 0, 0, 0 };
+                       position_t const pos = { inputname, 0, 0, 0 };
                        errorf(&pos, "could not open: %s", strerror(errno));
                        return false;
                }
@@ -1255,14 +1264,12 @@ 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) {
-                               source_position_t const pos = { inputname, 0, 0, 0 };
+                       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;
                                break;
@@ -1270,13 +1277,33 @@ again:
                        unit->type = COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION;
                        goto again;
                }
-               case COMPILATION_UNIT_ASSEMBLER:
-                       panic("TODO: preprocess for assembler");
+               case COMPILATION_UNIT_ASSEMBLER: {
+                       if (external_preprocessor == NULL) {
+                               panic("preprocessed assembler not possible with internal preprocessor yet");
+                       }
+                       if (!run_external_preprocessor(unit)) {
+                               result = EXIT_FAILURE;
+                               break;
+                       }
+                       /* write file to output... */
+                       FILE *asm_out;
+                       if (mode == PreprocessOnly) {
+                               asm_out = out;
+                       } else {
+                               asm_out = make_temp_file("ccs", &unit->name);
+                       }
+                       assert(unit->input != NULL);
+                       assert(unit->input_is_pipe);
+                       copy_file(asm_out, unit->input);
+                       if (asm_out != out)
+                               fclose(asm_out);
+                       unit->type = COMPILATION_UNIT_PREPROCESSED_ASSEMBLER;
+                       goto 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;
                                }
@@ -1286,16 +1313,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;
                                }
@@ -1482,7 +1507,6 @@ int main(int argc, char **argv)
        char                cpu_arch[16]         = "ia32";
        compilation_unit_t *units                = NULL;
        compilation_unit_t *last_unit            = NULL;
-       bool                construct_dep_target = false;
        bool                produce_statev       = false;
        const char         *filtev               = NULL;
        bool                profile_generate     = false;
@@ -1562,7 +1586,7 @@ int main(int argc, char **argv)
                        } else if (option[0] == 'g') {
                                /* TODO: parse -gX with 0<=x<=3... */
                                set_be_option("debug=frameinfo");
-                               set_be_option("ia32-nooptcc=yes");
+                               set_be_option("ia32-optcc=false");
                        } else if (SINGLE_OPTION('c')) {
                                mode = CompileAssemble;
                        } else if (SINGLE_OPTION('E')) {
@@ -1750,8 +1774,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;
@@ -1766,8 +1789,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=")) {
@@ -1775,7 +1797,11 @@ int main(int argc, char **argv)
                                        }
                                }
                        } else if (option[0] == 'W') {
-                               if (strstart(option + 1, "p,")) {
+                               if (strstart(option + 1, "a,")) {
+                                       const char *opt;
+                                       GET_ARG_AFTER(opt, "-Wa,");
+                                       add_flag(&asflags_obst, "-Wa,%s", opt);
+                               } else if (strstart(option + 1, "p,")) {
                                        // pass options directly to the preprocessor
                                        const char *opt;
                                        GET_ARG_AFTER(opt, "-Wp,");
@@ -1838,14 +1864,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=");
@@ -1859,15 +1883,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;
@@ -1877,8 +1899,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
@@ -1899,6 +1920,23 @@ int main(int argc, char **argv)
                                                add_flag(&ldflags_obst, "-m%u", machine_size);
                                        }
                                }
+                       } else if (option[0] == 'X') {
+                               if (streq(option + 1, "assembler")) {
+                                       const char *opt;
+                                       GET_ARG_AFTER(opt, "-Xassembler");
+                                       add_flag(&asflags_obst, "-Xassembler");
+                                       add_flag(&asflags_obst, opt);
+                               } else if (streq(option + 1, "preprocessor")) {
+                                       const char *opt;
+                                       GET_ARG_AFTER(opt, "-Xpreprocessor");
+                                       add_flag(&cppflags_obst, "-Xpreprocessor");
+                                       add_flag(&cppflags_obst, opt);
+                               } else if (streq(option + 1, "linker")) {
+                                       const char *opt;
+                                       GET_ARG_AFTER(opt, "-Xlinker");
+                                       add_flag(&ldflags_obst, "-Xlinker");
+                                       add_flag(&ldflags_obst, opt);
+                               }
                        } else if (streq(option, "pg")) {
                                set_be_option("gprof");
                                add_flag(&ldflags_obst, "-pg");
@@ -2141,21 +2179,11 @@ int main(int argc, char **argv)
        if (do_timing)
                timer_init();
 
-       if (construct_dep_target) {
-               if (outname != 0 && strlen(outname) >= 2) {
-                       get_output_name(dep_target, sizeof(dep_target), outname, ".d");
-               } else {
-                       get_output_name(dep_target, sizeof(dep_target), units->name, ".d");
-               }
-       } else {
-               dep_target[0] = '\0';
-       }
-
        char outnamebuf[4096];
        if (outname == NULL) {
                const char *filename = units->name;
 
-               switch(mode) {
+               switch (mode) {
                case BenchmarkParser:
                case PrintAst:
                case PrintFluffy:
@@ -2200,7 +2228,7 @@ int main(int argc, char **argv)
        } else {
                out = fopen(outname, "w");
                if (out == NULL) {
-                       source_position_t const pos = { outname, 0, 0, 0 };
+                       position_t const pos = { outname, 0, 0, 0 };
                        errorf(&pos, "could not open for writing: %s", strerror(errno));
                        return EXIT_FAILURE;
                }
@@ -2233,11 +2261,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;
                }
        }