OSDN Git Service

PR middle-end/16585
[pf3gnuchains/gcc-fork.git] / gcc / java / jv-scan.c
1 /* Main for jv-scan
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4    Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "intl.h"
28
29 #include "obstack.h"            /* We use obstacks in lex.c */
30
31 #include "version.h"
32
33 #ifdef HAVE_LOCALE_H
34 #include <locale.h>
35 #endif
36
37 #ifdef HAVE_LANGINFO_CODESET
38 #include <langinfo.h>
39 #endif
40
41 #include <getopt.h>
42
43 extern void fatal_error (const char *msgid, ...)
44      ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
45 void warning (const char *msgid, ...) ATTRIBUTE_PRINTF_1;
46 void report (void);
47
48 static void usage (void) ATTRIBUTE_NORETURN;
49 static void help (void) ATTRIBUTE_NORETURN;
50 static void version (void) ATTRIBUTE_NORETURN;
51
52 #define JC1_LITE
53 #include "jcf.h"
54 #include "parse.h"
55
56 /* Current input file and output file IO streams.  */
57 FILE *finput, *out;
58
59 /* Executable name.  */
60 char *exec_name;
61
62 struct line_maps line_table;
63
64 /* Flags matching command line options.  */
65 int flag_find_main = 0;
66 int flag_dump_class = 0;
67 int flag_list_filename = 0;
68 int flag_complexity = 0;
69 int flag_assert = 1;
70
71 int pedantic = 0;
72
73 \f
74
75 /* This is used to mark options with no short value.  */
76 #define LONG_OPT(Num)  ((Num) + 128)
77
78 #define OPT_HELP      LONG_OPT (0)
79 #define OPT_VERSION   LONG_OPT (1)
80 #define OPT_ENCODING  LONG_OPT (2)
81
82 static const struct option options[] =
83 {
84   { "help",      no_argument,       NULL, OPT_HELP },
85   { "version",   no_argument,       NULL, OPT_VERSION },
86   { "print-main", no_argument,      &flag_find_main, 1 },
87   { "list-filename", no_argument,   &flag_list_filename, 1 },
88   { "list-class", no_argument,      &flag_dump_class, 1 },
89   { "encoding",  required_argument, NULL, OPT_ENCODING },
90   { "complexity", no_argument,      &flag_complexity, 1 },
91   { "no-assert", no_argument,       &flag_assert, 0 },
92   { "assert",    no_argument,       &flag_assert, 1 },
93   { NULL,        no_argument,       NULL, 0 }
94 };
95
96 static void
97 usage (void)
98 {
99   fprintf (stderr, _("Try 'jv-scan --help' for more information.\n"));
100   exit (1);
101 }
102
103 static void
104 help (void)
105 {
106   printf (_("Usage: jv-scan [OPTION]... FILE...\n\n"));
107   printf (_("Print useful information read from Java source files.\n\n"));
108   printf (_("  --no-assert             Don't recognize the assert keyword\n"));
109   printf (_("  --complexity            Print cyclomatic complexity of input file\n"));
110   printf (_("  --encoding NAME         Specify encoding of input file\n"));
111   printf (_("  --print-main            Print name of class containing 'main'\n"));
112   printf (_("  --list-class            List all classes defined in file\n"));
113   printf (_("  --list-filename         Print input filename when listing class names\n"));
114   printf (_("  -o FILE                 Set output file name\n"));
115   printf ("\n");
116   printf (_("  --help                  Print this help, then exit\n"));
117   printf (_("  --version               Print version number, then exit\n"));
118   printf ("\n");
119   printf (_("For bug reporting instructions, please see:\n"
120             "%s.\n"), bug_report_url);
121   exit (0);
122 }
123
124 static void
125 version (void)
126 {
127   printf ("jv-scan (GCC) %s\n\n", version_string);
128   printf ("Copyright %s 2004 Free Software Foundation, Inc.\n", _("(C)"));
129   printf (_("This is free software; see the source for copying conditions.  There is NO\n"
130             "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
131   exit (0);
132 }
133
134 #ifdef USE_MAPPED_LOCATION
135 /* FIXME - this is the same as the function in tree.c, which is awkward.
136    Probably the cleanest solution is to move the function to line-map.c.
137    This is difficult as long as we still support --disable-mapped-location,
138    since whether expanded_location has a column fields depends on
139    USE_MAPPED_LOCATION. */
140
141 expanded_location
142 expand_location (source_location loc)
143 {
144   expanded_location xloc;
145   if (loc == 0) { xloc.file = NULL; xloc.line = 0;  xloc.column = 0; }
146   else
147     {
148       const struct line_map *map = linemap_lookup (&line_table, loc);
149       xloc.file = map->to_file;
150       xloc.line = SOURCE_LINE (map, loc);
151       xloc.column = SOURCE_COLUMN (map, loc);
152     };
153   return xloc;
154 }
155 #endif
156
157 /* jc1-lite main entry point */
158 int
159 main (int argc, char **argv)
160 {
161   int i = 1;
162   const char *output_file = NULL;
163   const char *encoding = NULL;
164   long ft;
165   int opt;
166
167   exec_name = argv[0];
168
169   /* Default for output */
170   out = stdout;
171
172   gcc_init_libintl ();
173
174   /* Process options first.  We use getopt_long and not
175      getopt_long_only because we only support `--' long options here.  */
176   while ((opt = getopt_long (argc, argv, "o:", options, NULL)) != -1)
177     {
178       switch (opt)
179         {
180         case 0:
181           /* Already handled.  */
182           break;
183
184         case 'o':
185           output_file = optarg;
186           break;
187
188         case OPT_HELP:
189           help ();
190           break;
191
192         case OPT_VERSION:
193           version ();
194           break;
195
196         case OPT_ENCODING:
197           encoding = optarg;
198           break;
199
200         default:
201           usage ();
202           break;
203         }
204     }
205
206   /* No flags? Do nothing */
207   if (! flag_find_main && ! flag_dump_class && ! flag_complexity)
208     return 0;
209
210   /* Check on bad usage */
211   if (flag_find_main + flag_dump_class + flag_complexity > 1)
212     fatal_error
213       ("only one of '--print-main', '--list-class', and '--complexity' allowed");
214
215   if (output_file && !(out = fopen (output_file, "w")))
216     fatal_error ("can't open output file '%s'", output_file);
217
218   ft = ftell (out);
219
220   gcc_obstack_init (&temporary_obstack);
221   java_push_parser_context ();
222
223   for ( i = optind; i < argc; i++ )
224     if (argv [i])
225       {
226         char *filename = argv[i];
227         if ( (finput = fopen (filename, "r")) )
228           {
229             /* There's no point in trying to find the current encoding
230                unless we are going to do something intelligent with it
231                -- hence the test for iconv.  */
232 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
233             setlocale (LC_CTYPE, "");
234             if (encoding == NULL)
235               encoding = nl_langinfo (CODESET);
236 #endif  
237             if (encoding == NULL || *encoding == '\0')
238               encoding = DEFAULT_ENCODING;
239
240             java_init_lex (finput, encoding);
241             ctxp->filename = filename;
242             yyparse ();
243             report ();
244             if (ftell (out) != ft)
245               fputc ('\n', out);
246             ft = ftell (out);
247             fclose (finput);
248             reset_report ();
249           }
250         else
251           fatal_error ("file not found '%s'", argv [i]);
252       }
253
254   /* Flush and close */
255   if (ftell (out) != ft)
256     fputc ('\n', out);
257   if (!output_file)
258     fclose (out);
259
260   return 0;
261 }
262
263 \f
264
265 /* Error report, memory, obstack initialization and other utility
266    functions */
267
268 void
269 fatal_error (const char *msgid, ...)
270 {
271   va_list ap;
272   va_start (ap, msgid);
273   fprintf (stderr, _("%s: error: "), exec_name);
274   vfprintf (stderr, _(msgid), ap);
275   fputc ('\n', stderr);
276   va_end (ap);
277   exit (1);
278 }
279
280 void
281 warning (const char *msgid, ...)
282 {
283   va_list ap;
284   va_start (ap, msgid);
285   fprintf (stderr, _("%s: warning: "), exec_name);
286   vfprintf (stderr, _(msgid), ap);
287   fputc ('\n', stderr);
288   va_end (ap);
289 }
290
291 void
292 fancy_abort (const char *file, int line, const char *func)
293 {
294   fatal_error ("abort in %s, at %s:%d", func, file, line);
295 }