xref: /dragonfly/contrib/gcc-4.7/gcc/cppspec.c (revision e4b17023)
1*e4b17023SJohn Marino /* Specific flags and argument handling of the C preprocessor.
2*e4b17023SJohn Marino    Copyright (C) 1999, 2007, 2010, 2011 Free Software Foundation, Inc.
3*e4b17023SJohn Marino 
4*e4b17023SJohn Marino This file is part of GCC.
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
7*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
8*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
9*e4b17023SJohn Marino version.
10*e4b17023SJohn Marino 
11*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*e4b17023SJohn Marino for more details.
15*e4b17023SJohn Marino 
16*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
17*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
18*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
19*e4b17023SJohn Marino 
20*e4b17023SJohn Marino #include "config.h"
21*e4b17023SJohn Marino #include "system.h"
22*e4b17023SJohn Marino #include "coretypes.h"
23*e4b17023SJohn Marino #include "tm.h"
24*e4b17023SJohn Marino #include "gcc.h"
25*e4b17023SJohn Marino #include "opts.h"
26*e4b17023SJohn Marino 
27*e4b17023SJohn Marino /* The `cpp' executable installed in $(bindir) and $(cpp_install_dir)
28*e4b17023SJohn Marino    is a customized version of the gcc driver.  It forces -E; -S and -c
29*e4b17023SJohn Marino    are errors.  It defaults to -x c for files with unrecognized
30*e4b17023SJohn Marino    extensions, unless -x options appear in argv, in which case we
31*e4b17023SJohn Marino    assume the user knows what they're doing.  If no explicit input is
32*e4b17023SJohn Marino    mentioned, it will read stdin.  */
33*e4b17023SJohn Marino 
34*e4b17023SJohn Marino /* Suffixes for known sorts of input files.  Note that we do not list
35*e4b17023SJohn Marino    files which are normally considered to have been preprocessed already,
36*e4b17023SJohn Marino    since the user's expectation is that `cpp' always preprocesses.  */
37*e4b17023SJohn Marino static const char *const known_suffixes[] =
38*e4b17023SJohn Marino {
39*e4b17023SJohn Marino   ".c",  ".C",   ".S",   ".m",
40*e4b17023SJohn Marino   ".cc", ".cxx", ".cpp", ".cp",  ".c++",
41*e4b17023SJohn Marino   ".sx",
42*e4b17023SJohn Marino   NULL
43*e4b17023SJohn Marino };
44*e4b17023SJohn Marino 
45*e4b17023SJohn Marino /* Filter the command line before processing by the gcc driver proper.  */
46*e4b17023SJohn Marino void
lang_specific_driver(struct cl_decoded_option ** in_decoded_options,unsigned int * in_decoded_options_count,int * in_added_libraries ATTRIBUTE_UNUSED)47*e4b17023SJohn Marino lang_specific_driver (struct cl_decoded_option **in_decoded_options,
48*e4b17023SJohn Marino 		      unsigned int *in_decoded_options_count,
49*e4b17023SJohn Marino 		      int *in_added_libraries ATTRIBUTE_UNUSED)
50*e4b17023SJohn Marino {
51*e4b17023SJohn Marino   struct cl_decoded_option *decoded_options = *in_decoded_options;
52*e4b17023SJohn Marino   unsigned int argc = *in_decoded_options_count;
53*e4b17023SJohn Marino 
54*e4b17023SJohn Marino   /* Do we need to read stdin? */
55*e4b17023SJohn Marino   int read_stdin = 1;
56*e4b17023SJohn Marino 
57*e4b17023SJohn Marino   /* Do we need to insert -E? */
58*e4b17023SJohn Marino   int need_E = 1;
59*e4b17023SJohn Marino 
60*e4b17023SJohn Marino   /* Have we seen an input file? */
61*e4b17023SJohn Marino   int seen_input = 0;
62*e4b17023SJohn Marino 
63*e4b17023SJohn Marino   /* Positions to insert -xc, -xassembler-with-cpp, and -o, if necessary.
64*e4b17023SJohn Marino      0 means unnecessary.  */
65*e4b17023SJohn Marino   unsigned int lang_c_here = 0;
66*e4b17023SJohn Marino   unsigned int lang_S_here = 0;
67*e4b17023SJohn Marino   unsigned int o_here = 0;
68*e4b17023SJohn Marino 
69*e4b17023SJohn Marino   /* Do we need to fix up an input file with an unrecognized suffix? */
70*e4b17023SJohn Marino   int need_fixups = 1;
71*e4b17023SJohn Marino 
72*e4b17023SJohn Marino   unsigned int i, j;
73*e4b17023SJohn Marino   struct cl_decoded_option *new_decoded_options;
74*e4b17023SJohn Marino   unsigned int new_argc;
75*e4b17023SJohn Marino   extern int is_cpp_driver;
76*e4b17023SJohn Marino 
77*e4b17023SJohn Marino   is_cpp_driver = 1;
78*e4b17023SJohn Marino 
79*e4b17023SJohn Marino   /* First pass.  If we see an -S or -c, barf.  If we see an input file,
80*e4b17023SJohn Marino      turn off read_stdin.  If we see a second input file, it is actually
81*e4b17023SJohn Marino      the output file.  If we see a third input file, barf.  */
82*e4b17023SJohn Marino   for (i = 1; i < argc; i++)
83*e4b17023SJohn Marino     {
84*e4b17023SJohn Marino       switch (decoded_options[i].opt_index)
85*e4b17023SJohn Marino 	{
86*e4b17023SJohn Marino 	case OPT_E:
87*e4b17023SJohn Marino 	  need_E = 0;
88*e4b17023SJohn Marino 	  break;
89*e4b17023SJohn Marino 
90*e4b17023SJohn Marino 	case OPT_S:
91*e4b17023SJohn Marino 	case OPT_c:
92*e4b17023SJohn Marino 	  fatal_error ("%qs is not a valid option to the preprocessor",
93*e4b17023SJohn Marino 		       decoded_options[i].orig_option_with_args_text);
94*e4b17023SJohn Marino 	  return;
95*e4b17023SJohn Marino 
96*e4b17023SJohn Marino 	case OPT_x:
97*e4b17023SJohn Marino 	  need_fixups = 0;
98*e4b17023SJohn Marino 	  break;
99*e4b17023SJohn Marino 
100*e4b17023SJohn Marino 	case OPT_SPECIAL_input_file:
101*e4b17023SJohn Marino 	  {
102*e4b17023SJohn Marino 	    const char *file = decoded_options[i].arg;
103*e4b17023SJohn Marino 
104*e4b17023SJohn Marino 	    if (strcmp (file, "-") == 0)
105*e4b17023SJohn Marino 	      read_stdin = 0;
106*e4b17023SJohn Marino 	    else
107*e4b17023SJohn Marino 	      {
108*e4b17023SJohn Marino 		seen_input++;
109*e4b17023SJohn Marino 		if (seen_input == 3)
110*e4b17023SJohn Marino 		  {
111*e4b17023SJohn Marino 		    fatal_error ("too many input files");
112*e4b17023SJohn Marino 		    return;
113*e4b17023SJohn Marino 		  }
114*e4b17023SJohn Marino 		else if (seen_input == 2)
115*e4b17023SJohn Marino 		  {
116*e4b17023SJohn Marino 		    o_here = i;
117*e4b17023SJohn Marino 		  }
118*e4b17023SJohn Marino 		else
119*e4b17023SJohn Marino 		  {
120*e4b17023SJohn Marino 		    read_stdin = 0;
121*e4b17023SJohn Marino 		    if (need_fixups)
122*e4b17023SJohn Marino 		      {
123*e4b17023SJohn Marino 			int l = strlen (file);
124*e4b17023SJohn Marino 			int known = 0;
125*e4b17023SJohn Marino 			const char *const *suff;
126*e4b17023SJohn Marino 
127*e4b17023SJohn Marino 			for (suff = known_suffixes; *suff; suff++)
128*e4b17023SJohn Marino 			  if (!strcmp (*suff, &file[l - strlen(*suff)]))
129*e4b17023SJohn Marino 			    {
130*e4b17023SJohn Marino 			      known = 1;
131*e4b17023SJohn Marino 			      break;
132*e4b17023SJohn Marino 			    }
133*e4b17023SJohn Marino 
134*e4b17023SJohn Marino 			if (! known)
135*e4b17023SJohn Marino 			  {
136*e4b17023SJohn Marino 			    /* .s files are a special case; we have to
137*e4b17023SJohn Marino 			       treat them like .S files so
138*e4b17023SJohn Marino 			       -D__ASSEMBLER__ will be in effect.  */
139*e4b17023SJohn Marino 			    if (!strcmp (".s", &file[l - 2]))
140*e4b17023SJohn Marino 			      lang_S_here = i;
141*e4b17023SJohn Marino 			    else
142*e4b17023SJohn Marino 			      lang_c_here = i;
143*e4b17023SJohn Marino 			  }
144*e4b17023SJohn Marino 		      }
145*e4b17023SJohn Marino 		  }
146*e4b17023SJohn Marino 	      }
147*e4b17023SJohn Marino 	  }
148*e4b17023SJohn Marino 	  break;
149*e4b17023SJohn Marino 	}
150*e4b17023SJohn Marino     }
151*e4b17023SJohn Marino 
152*e4b17023SJohn Marino   /* If we don't need to edit the command line, we can bail early.  */
153*e4b17023SJohn Marino 
154*e4b17023SJohn Marino   new_argc = argc + need_E + read_stdin + !!lang_c_here + !!lang_S_here;
155*e4b17023SJohn Marino 
156*e4b17023SJohn Marino   if (new_argc == argc && !o_here)
157*e4b17023SJohn Marino     return;
158*e4b17023SJohn Marino 
159*e4b17023SJohn Marino   new_decoded_options = XNEWVEC (struct cl_decoded_option, new_argc);
160*e4b17023SJohn Marino 
161*e4b17023SJohn Marino   new_decoded_options[0] = decoded_options[0];
162*e4b17023SJohn Marino   j = 1;
163*e4b17023SJohn Marino 
164*e4b17023SJohn Marino   if (need_E)
165*e4b17023SJohn Marino     generate_option (OPT_E, NULL, 1, CL_DRIVER, &new_decoded_options[j++]);
166*e4b17023SJohn Marino 
167*e4b17023SJohn Marino   for (i = 1; i < argc; i++, j++)
168*e4b17023SJohn Marino     {
169*e4b17023SJohn Marino       if (i == lang_c_here)
170*e4b17023SJohn Marino 	generate_option (OPT_x, "c", 1, CL_DRIVER, &new_decoded_options[j++]);
171*e4b17023SJohn Marino       else if (i == lang_S_here)
172*e4b17023SJohn Marino 	generate_option (OPT_x, "assembler-with-cpp", 1, CL_DRIVER,
173*e4b17023SJohn Marino 			 &new_decoded_options[j++]);
174*e4b17023SJohn Marino       else if (i == o_here)
175*e4b17023SJohn Marino 	{
176*e4b17023SJohn Marino 	  generate_option (OPT_o, decoded_options[i].arg, 1, CL_DRIVER,
177*e4b17023SJohn Marino 			   &new_decoded_options[j]);
178*e4b17023SJohn Marino 	  continue;
179*e4b17023SJohn Marino 	}
180*e4b17023SJohn Marino 
181*e4b17023SJohn Marino       new_decoded_options[j] = decoded_options[i];
182*e4b17023SJohn Marino     }
183*e4b17023SJohn Marino 
184*e4b17023SJohn Marino   if (read_stdin)
185*e4b17023SJohn Marino     generate_option_input_file ("-", &new_decoded_options[j++]);
186*e4b17023SJohn Marino 
187*e4b17023SJohn Marino   *in_decoded_options_count = new_argc;
188*e4b17023SJohn Marino   *in_decoded_options = new_decoded_options;
189*e4b17023SJohn Marino }
190*e4b17023SJohn Marino 
191*e4b17023SJohn Marino /* Called before linking.  Returns 0 on success and -1 on failure.  */
lang_specific_pre_link(void)192*e4b17023SJohn Marino int lang_specific_pre_link (void)
193*e4b17023SJohn Marino {
194*e4b17023SJohn Marino   return 0;  /* Not used for cpp.  */
195*e4b17023SJohn Marino }
196*e4b17023SJohn Marino 
197*e4b17023SJohn Marino /* Number of extra output files that lang_specific_pre_link may generate.  */
198*e4b17023SJohn Marino int lang_specific_extra_outfiles = 0;  /* Not used for cpp.  */
199