1 /* Specific flags and argument handling of the C preprocessor.
2 Copyright (C) 1999-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
24 #include "opt-suggestions.h"
28 /* The `cpp' executable installed in $(bindir) and $(cpp_install_dir)
29 is a customized version of the gcc driver. It forces -E; -S and -c
30 are errors. It defaults to -x c for files with unrecognized
31 extensions, unless -x options appear in argv, in which case we
32 assume the user knows what they're doing. If no explicit input is
33 mentioned, it will read stdin. */
35 /* Suffixes for known sorts of input files. Note that we do not list
36 files which are normally considered to have been preprocessed already,
37 since the user's expectation is that `cpp' always preprocesses. */
38 static const char *const known_suffixes
[] =
40 ".c", ".C", ".S", ".m",
41 ".cc", ".cxx", ".cpp", ".cp", ".c++",
46 /* Filter the command line before processing by the gcc driver proper. */
48 lang_specific_driver (struct cl_decoded_option
**in_decoded_options
,
49 unsigned int *in_decoded_options_count
,
50 int *in_added_libraries ATTRIBUTE_UNUSED
)
52 struct cl_decoded_option
*decoded_options
= *in_decoded_options
;
53 unsigned int argc
= *in_decoded_options_count
;
55 /* Do we need to read stdin? */
58 /* Do we need to insert -E? */
61 /* Have we seen an input file? */
64 /* Positions to insert -xc, -xassembler-with-cpp, and -o, if necessary.
65 0 means unnecessary. */
66 unsigned int lang_c_here
= 0;
67 unsigned int lang_S_here
= 0;
68 unsigned int o_here
= 0;
70 /* Do we need to fix up an input file with an unrecognized suffix? */
74 struct cl_decoded_option
*new_decoded_options
;
75 unsigned int new_argc
;
76 extern int is_cpp_driver
;
80 /* First pass. If we see an -S or -c, barf. If we see an input file,
81 turn off read_stdin. If we see a second input file, it is actually
82 the output file. If we see a third input file, barf. */
83 for (i
= 1; i
< argc
; i
++)
85 switch (decoded_options
[i
].opt_index
)
93 fatal_error (input_location
,
94 "%qs is not a valid option to the preprocessor",
95 decoded_options
[i
].orig_option_with_args_text
);
102 case OPT_SPECIAL_input_file
:
104 const char *file
= decoded_options
[i
].arg
;
106 if (strcmp (file
, "-") == 0)
113 fatal_error (input_location
, "too many input files");
116 else if (seen_input
== 2)
125 int l
= strlen (file
);
127 const char *const *suff
;
129 for (suff
= known_suffixes
; *suff
; suff
++)
130 if (!strcmp (*suff
, &file
[l
- strlen(*suff
)]))
138 /* .s files are a special case; we have to
139 treat them like .S files so
140 -D__ASSEMBLER__ will be in effect. */
141 if (!strcmp (".s", &file
[l
- 2]))
154 /* If we don't need to edit the command line, we can bail early. */
156 new_argc
= argc
+ need_E
+ read_stdin
+ !!lang_c_here
+ !!lang_S_here
;
158 if (new_argc
== argc
&& !o_here
)
161 new_decoded_options
= XNEWVEC (struct cl_decoded_option
, new_argc
);
163 new_decoded_options
[0] = decoded_options
[0];
167 generate_option (OPT_E
, NULL
, 1, CL_DRIVER
, &new_decoded_options
[j
++]);
169 for (i
= 1; i
< argc
; i
++, j
++)
171 if (i
== lang_c_here
)
172 generate_option (OPT_x
, "c", 1, CL_DRIVER
, &new_decoded_options
[j
++]);
173 else if (i
== lang_S_here
)
174 generate_option (OPT_x
, "assembler-with-cpp", 1, CL_DRIVER
,
175 &new_decoded_options
[j
++]);
176 else if (i
== o_here
)
178 generate_option (OPT_o
, decoded_options
[i
].arg
, 1, CL_DRIVER
,
179 &new_decoded_options
[j
]);
183 new_decoded_options
[j
] = decoded_options
[i
];
187 generate_option_input_file ("-", &new_decoded_options
[j
++]);
189 *in_decoded_options_count
= new_argc
;
190 *in_decoded_options
= new_decoded_options
;
193 /* Called before linking. Returns 0 on success and -1 on failure. */
194 int lang_specific_pre_link (void)
196 return 0; /* Not used for cpp. */
199 /* Number of extra output files that lang_specific_pre_link may generate. */
200 int lang_specific_extra_outfiles
= 0; /* Not used for cpp. */