opts-global.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* Command line option handling. Code involving global state that
  2. should not be shared with the driver.
  3. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "diagnostic.h"
  20. #include "opts.h"
  21. #include "flags.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h" /* Required by langhooks.h. */
  32. #include "fold-const.h"
  33. #include "predict.h"
  34. #include "tm.h"
  35. #include "hard-reg-set.h"
  36. #include "input.h"
  37. #include "function.h"
  38. #include "basic-block.h"
  39. #include "tree-ssa-alias.h"
  40. #include "internal-fn.h"
  41. #include "gimple-expr.h"
  42. #include "is-a.h"
  43. #include "gimple.h"
  44. #include "langhooks.h"
  45. #include "rtl.h"
  46. #include "dbgcnt.h"
  47. #include "debug.h"
  48. #include "hash-map.h"
  49. #include "plugin-api.h"
  50. #include "ipa-ref.h"
  51. #include "cgraph.h"
  52. #include "lto-streamer.h"
  53. #include "output.h"
  54. #include "plugin.h"
  55. #include "toplev.h"
  56. #include "tree-pass.h"
  57. #include "context.h"
  58. #include "asan.h"
  59. typedef const char *const_char_p; /* For DEF_VEC_P. */
  60. static vec<const_char_p> ignored_options;
  61. /* Input file names. */
  62. const char **in_fnames;
  63. unsigned num_in_fnames;
  64. /* Return a malloced slash-separated list of languages in MASK. */
  65. static char *
  66. write_langs (unsigned int mask)
  67. {
  68. unsigned int n = 0, len = 0;
  69. const char *lang_name;
  70. char *result;
  71. for (n = 0; (lang_name = lang_names[n]) != 0; n++)
  72. if (mask & (1U << n))
  73. len += strlen (lang_name) + 1;
  74. result = XNEWVEC (char, len);
  75. len = 0;
  76. for (n = 0; (lang_name = lang_names[n]) != 0; n++)
  77. if (mask & (1U << n))
  78. {
  79. if (len)
  80. result[len++] = '/';
  81. strcpy (result + len, lang_name);
  82. len += strlen (lang_name);
  83. }
  84. result[len] = 0;
  85. return result;
  86. }
  87. /* Complain that switch DECODED does not apply to this front end (mask
  88. LANG_MASK). */
  89. static void
  90. complain_wrong_lang (const struct cl_decoded_option *decoded,
  91. unsigned int lang_mask)
  92. {
  93. const struct cl_option *option = &cl_options[decoded->opt_index];
  94. const char *text = decoded->orig_option_with_args_text;
  95. char *ok_langs = NULL, *bad_lang = NULL;
  96. unsigned int opt_flags = option->flags;
  97. if (!lang_hooks.complain_wrong_lang_p (option))
  98. return;
  99. opt_flags &= ((1U << cl_lang_count) - 1) | CL_DRIVER;
  100. if (opt_flags != CL_DRIVER)
  101. ok_langs = write_langs (opt_flags);
  102. if (lang_mask != CL_DRIVER)
  103. bad_lang = write_langs (lang_mask);
  104. if (opt_flags == CL_DRIVER)
  105. error ("command line option %qs is valid for the driver but not for %s",
  106. text, bad_lang);
  107. else if (lang_mask == CL_DRIVER)
  108. gcc_unreachable ();
  109. else
  110. /* Eventually this should become a hard error IMO. */
  111. warning (0, "command line option %qs is valid for %s but not for %s",
  112. text, ok_langs, bad_lang);
  113. free (ok_langs);
  114. free (bad_lang);
  115. }
  116. /* Buffer the unknown option described by the string OPT. Currently,
  117. we only complain about unknown -Wno-* options if they may have
  118. prevented a diagnostic. Otherwise, we just ignore them. Note that
  119. if we do complain, it is only as a warning, not an error; passing
  120. the compiler an unrecognized -Wno-* option should never change
  121. whether the compilation succeeds or fails. */
  122. static void
  123. postpone_unknown_option_warning (const char *opt)
  124. {
  125. ignored_options.safe_push (opt);
  126. }
  127. /* Produce a warning for each option previously buffered. */
  128. void
  129. print_ignored_options (void)
  130. {
  131. while (!ignored_options.is_empty ())
  132. {
  133. const char *opt;
  134. opt = ignored_options.pop ();
  135. warning_at (UNKNOWN_LOCATION, 0,
  136. "unrecognized command line option %qs", opt);
  137. }
  138. }
  139. /* Handle an unknown option DECODED, returning true if an error should
  140. be given. */
  141. static bool
  142. unknown_option_callback (const struct cl_decoded_option *decoded)
  143. {
  144. const char *opt = decoded->arg;
  145. if (opt[1] == 'W' && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-'
  146. && !(decoded->errors & CL_ERR_NEGATIVE))
  147. {
  148. /* We don't generate warnings for unknown -Wno-* options unless
  149. we issue diagnostics. */
  150. postpone_unknown_option_warning (opt);
  151. return false;
  152. }
  153. else
  154. return true;
  155. }
  156. /* Handle a front-end option; arguments and return value as for
  157. handle_option. */
  158. static bool
  159. lang_handle_option (struct gcc_options *opts,
  160. struct gcc_options *opts_set,
  161. const struct cl_decoded_option *decoded,
  162. unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
  163. location_t loc,
  164. const struct cl_option_handlers *handlers,
  165. diagnostic_context *dc)
  166. {
  167. gcc_assert (opts == &global_options);
  168. gcc_assert (opts_set == &global_options_set);
  169. gcc_assert (dc == global_dc);
  170. gcc_assert (decoded->canonical_option_num_elements <= 2);
  171. return lang_hooks.handle_option (decoded->opt_index, decoded->arg,
  172. decoded->value, kind, loc, handlers);
  173. }
  174. /* Handle FILENAME from the command line. */
  175. static void
  176. add_input_filename (const char *filename)
  177. {
  178. num_in_fnames++;
  179. in_fnames = XRESIZEVEC (const char *, in_fnames, num_in_fnames);
  180. in_fnames[num_in_fnames - 1] = filename;
  181. }
  182. /* Handle the vector of command line options (located at LOC), storing
  183. the results of processing DECODED_OPTIONS and DECODED_OPTIONS_COUNT
  184. in OPTS and OPTS_SET and using DC for diagnostic state. LANG_MASK
  185. contains has a single bit set representing the current language.
  186. HANDLERS describes what functions to call for the options. */
  187. static void
  188. read_cmdline_options (struct gcc_options *opts, struct gcc_options *opts_set,
  189. struct cl_decoded_option *decoded_options,
  190. unsigned int decoded_options_count,
  191. location_t loc,
  192. unsigned int lang_mask,
  193. const struct cl_option_handlers *handlers,
  194. diagnostic_context *dc)
  195. {
  196. unsigned int i;
  197. for (i = 1; i < decoded_options_count; i++)
  198. {
  199. if (decoded_options[i].opt_index == OPT_SPECIAL_input_file)
  200. {
  201. /* Input files should only ever appear on the main command
  202. line. */
  203. gcc_assert (opts == &global_options);
  204. gcc_assert (opts_set == &global_options_set);
  205. if (opts->x_main_input_filename == NULL)
  206. {
  207. opts->x_main_input_filename = decoded_options[i].arg;
  208. opts->x_main_input_baselength
  209. = base_of_path (opts->x_main_input_filename,
  210. &opts->x_main_input_basename);
  211. }
  212. add_input_filename (decoded_options[i].arg);
  213. continue;
  214. }
  215. read_cmdline_option (opts, opts_set,
  216. decoded_options + i, loc, lang_mask, handlers,
  217. dc);
  218. }
  219. }
  220. /* Language mask determined at initialization. */
  221. static unsigned int initial_lang_mask;
  222. /* Initialize global options-related settings at start-up. */
  223. void
  224. init_options_once (void)
  225. {
  226. /* Perform language-specific options initialization. */
  227. initial_lang_mask = lang_hooks.option_lang_mask ();
  228. lang_hooks.initialize_diagnostics (global_dc);
  229. /* ??? Ideally, we should do this earlier and the FEs will override
  230. it if desired (none do it so far). However, the way the FEs
  231. construct their pretty-printers means that all previous settings
  232. are overriden. */
  233. diagnostic_color_init (global_dc);
  234. }
  235. /* Decode command-line options to an array, like
  236. decode_cmdline_options_to_array and with the same arguments but
  237. using the default lang_mask. */
  238. void
  239. decode_cmdline_options_to_array_default_mask (unsigned int argc,
  240. const char **argv,
  241. struct cl_decoded_option **decoded_options,
  242. unsigned int *decoded_options_count)
  243. {
  244. decode_cmdline_options_to_array (argc, argv,
  245. initial_lang_mask | CL_COMMON | CL_TARGET,
  246. decoded_options, decoded_options_count);
  247. }
  248. /* Set *HANDLERS to the default set of option handlers for use in the
  249. compilers proper (not the driver). */
  250. void
  251. set_default_handlers (struct cl_option_handlers *handlers)
  252. {
  253. handlers->unknown_option_callback = unknown_option_callback;
  254. handlers->wrong_lang_callback = complain_wrong_lang;
  255. handlers->num_handlers = 3;
  256. handlers->handlers[0].handler = lang_handle_option;
  257. handlers->handlers[0].mask = initial_lang_mask;
  258. handlers->handlers[1].handler = common_handle_option;
  259. handlers->handlers[1].mask = CL_COMMON;
  260. handlers->handlers[2].handler = target_handle_option;
  261. handlers->handlers[2].mask = CL_TARGET;
  262. }
  263. /* Parse command line options and set default flag values. Do minimal
  264. options processing. The decoded options are in *DECODED_OPTIONS
  265. and *DECODED_OPTIONS_COUNT; settings go in OPTS, OPTS_SET and DC;
  266. the options are located at LOC. */
  267. void
  268. decode_options (struct gcc_options *opts, struct gcc_options *opts_set,
  269. struct cl_decoded_option *decoded_options,
  270. unsigned int decoded_options_count,
  271. location_t loc, diagnostic_context *dc)
  272. {
  273. struct cl_option_handlers handlers;
  274. unsigned int lang_mask;
  275. lang_mask = initial_lang_mask;
  276. set_default_handlers (&handlers);
  277. default_options_optimization (opts, opts_set,
  278. decoded_options, decoded_options_count,
  279. loc, lang_mask, &handlers, dc);
  280. read_cmdline_options (opts, opts_set,
  281. decoded_options, decoded_options_count,
  282. loc, lang_mask,
  283. &handlers, dc);
  284. finish_options (opts, opts_set, loc);
  285. }
  286. /* Process common options that have been deferred until after the
  287. handlers have been called for all options. */
  288. void
  289. handle_common_deferred_options (void)
  290. {
  291. unsigned int i;
  292. cl_deferred_option *opt;
  293. vec<cl_deferred_option> v;
  294. if (common_deferred_options)
  295. v = *((vec<cl_deferred_option> *) common_deferred_options);
  296. else
  297. v = vNULL;
  298. if (flag_dump_all_passed)
  299. enable_rtl_dump_file ();
  300. if (flag_opt_info)
  301. opt_info_switch_p (NULL);
  302. FOR_EACH_VEC_ELT (v, i, opt)
  303. {
  304. switch (opt->opt_index)
  305. {
  306. case OPT_fcall_used_:
  307. fix_register (opt->arg, 0, 1);
  308. break;
  309. case OPT_fcall_saved_:
  310. fix_register (opt->arg, 0, 0);
  311. break;
  312. case OPT_fdbg_cnt_:
  313. dbg_cnt_process_opt (opt->arg);
  314. break;
  315. case OPT_fdbg_cnt_list:
  316. dbg_cnt_list_all_counters ();
  317. break;
  318. case OPT_fdebug_prefix_map_:
  319. add_debug_prefix_map (opt->arg);
  320. break;
  321. case OPT_fdump_:
  322. if (!g->get_dumps ()->dump_switch_p (opt->arg))
  323. error ("unrecognized command line option %<-fdump-%s%>", opt->arg);
  324. break;
  325. case OPT_fopt_info_:
  326. if (!opt_info_switch_p (opt->arg))
  327. error ("unrecognized command line option %<-fopt-info-%s%>",
  328. opt->arg);
  329. break;
  330. case OPT_fenable_:
  331. case OPT_fdisable_:
  332. if (opt->opt_index == OPT_fenable_)
  333. enable_pass (opt->arg);
  334. else
  335. disable_pass (opt->arg);
  336. break;
  337. case OPT_ffixed_:
  338. /* Deferred. */
  339. fix_register (opt->arg, 1, 1);
  340. break;
  341. case OPT_fplugin_:
  342. #ifdef ENABLE_PLUGIN
  343. add_new_plugin (opt->arg);
  344. #else
  345. error ("plugin support is disabled; configure with --enable-plugin");
  346. #endif
  347. break;
  348. case OPT_fplugin_arg_:
  349. #ifdef ENABLE_PLUGIN
  350. parse_plugin_arg_opt (opt->arg);
  351. #else
  352. error ("plugin support is disabled; configure with --enable-plugin");
  353. #endif
  354. break;
  355. case OPT_frandom_seed:
  356. /* The real switch is -fno-random-seed. */
  357. if (!opt->value)
  358. set_random_seed (NULL);
  359. break;
  360. case OPT_frandom_seed_:
  361. set_random_seed (opt->arg);
  362. break;
  363. case OPT_fstack_limit:
  364. /* The real switch is -fno-stack-limit. */
  365. if (!opt->value)
  366. stack_limit_rtx = NULL_RTX;
  367. break;
  368. case OPT_fstack_limit_register_:
  369. {
  370. int reg = decode_reg_name (opt->arg);
  371. if (reg < 0)
  372. error ("unrecognized register name %qs", opt->arg);
  373. else
  374. stack_limit_rtx = gen_rtx_REG (Pmode, reg);
  375. }
  376. break;
  377. case OPT_fstack_limit_symbol_:
  378. stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (opt->arg));
  379. break;
  380. case OPT_fasan_shadow_offset_:
  381. if (!(flag_sanitize & SANITIZE_KERNEL_ADDRESS))
  382. error ("-fasan-shadow-offset should only be used "
  383. "with -fsanitize=kernel-address");
  384. if (!set_asan_shadow_offset (opt->arg))
  385. error ("unrecognized shadow offset %qs", opt->arg);
  386. break;
  387. default:
  388. gcc_unreachable ();
  389. }
  390. }
  391. }