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