00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include "system.h"
00024 #include "cpplib.h"
00025 #include "internal.h"
00026 #include "mkdeps.h"
00027 #include "localedir.h"
00028
00029 static void init_library (void);
00030 static void mark_named_operators (cpp_reader *);
00031 static void read_original_filename (cpp_reader *);
00032 static void read_original_directory (cpp_reader *);
00033 static void post_options (cpp_reader *);
00034
00035
00036
00037
00038 #if HAVE_DESIGNATED_INITIALIZERS
00039
00040 #define init_trigraph_map()
00041 #define TRIGRAPH_MAP \
00042 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
00043
00044 #define END };
00045 #define s(p, v) [p] = v,
00046
00047 #else
00048
00049 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
00050 static void init_trigraph_map (void) { \
00051 unsigned char *x = _cpp_trigraph_map;
00052
00053 #define END }
00054 #define s(p, v) x[p] = v;
00055
00056 #endif
00057
00058 TRIGRAPH_MAP
00059 s('=', '#') s(')', ']') s('!', '|')
00060 s('(', '[') s('\'', '^') s('>', '}')
00061 s('/', '\\') s('<', '{') s('-', '~')
00062 END
00063
00064 #undef s
00065 #undef END
00066 #undef TRIGRAPH_MAP
00067
00068 /* A set of booleans indicating what CPP features each source language
00069 requires. */
00070 struct lang_flags
00071 {
00072 char c99;
00073 char cplusplus;
00074 char extended_numbers;
00075 char std;
00076 char cplusplus_comments;
00077 char digraphs;
00078 };
00079
00080 static const struct lang_flags lang_defaults[] =
00081 { /* c99 c++ xnum std // digr */
00082 /* GNUC89 */ { 0, 0, 1, 0, 1, 1 },
00083 /* GNUC99 */ { 1, 0, 1, 0, 1, 1 },
00084 /* STDC89 */ { 0, 0, 0, 1, 0, 0 },
00085 /* STDC94 */ { 0, 0, 0, 1, 0, 1 },
00086 /* STDC99 */ { 1, 0, 1, 1, 1, 1 },
00087 /* GNUCXX */ { 0, 1, 1, 0, 1, 1 },
00088 /* CXX98 */ { 0, 1, 1, 1, 1, 1 },
00089 /* ASM */ { 0, 0, 1, 0, 1, 0 }
00090 };
00091
00092 /* Sets internal flags correctly for a given language. */
00093 void
00094 cpp_set_lang (cpp_reader *pfile, enum c_lang lang)
00095 {
00096 const struct lang_flags *l = &lang_defaults[(int) lang];
00097
00098 CPP_OPTION (pfile, lang) = lang;
00099
00100 CPP_OPTION (pfile, c99) = l->c99;
00101 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
00102 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
00103 CPP_OPTION (pfile, std) = l->std;
00104 CPP_OPTION (pfile, trigraphs) = l->std;
00105 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
00106 CPP_OPTION (pfile, digraphs) = l->digraphs;
00107 }
00108
00109 /* Initialize library global state. */
00110 static void
00111 init_library (void)
00112 {
00113 static int initialized = 0;
00114
00115 if (! initialized)
00116 {
00117 initialized = 1;
00118
00119 /* Set up the trigraph map. This doesn't need to do anything if
00120 we were compiled with a compiler that supports C99 designated
00121 initializers. */
00122 init_trigraph_map ();
00123
00124 #ifdef ENABLE_NLS
00125 (void) bindtextdomain (PACKAGE, LOCALEDIR);
00126 #endif
00127 }
00128 }
00129
00130
00131 cpp_reader *
00132 cpp_create_reader (enum c_lang lang, hash_table *table,
00133 struct line_maps *line_table)
00134 {
00135 cpp_reader *pfile;
00136
00137
00138 init_library ();
00139
00140 pfile = xcalloc (1, sizeof (cpp_reader));
00141
00142 cpp_set_lang (pfile, lang);
00143 CPP_OPTION (pfile, warn_multichar) = 1;
00144 CPP_OPTION (pfile, discard_comments) = 1;
00145 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
00146 CPP_OPTION (pfile, show_column) = 1;
00147 CPP_OPTION (pfile, tabstop) = 8;
00148 CPP_OPTION (pfile, operator_names) = 1;
00149 CPP_OPTION (pfile, warn_trigraphs) = 2;
00150 CPP_OPTION (pfile, warn_endif_labels) = 1;
00151 CPP_OPTION (pfile, warn_deprecated) = 1;
00152 CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99);
00153 CPP_OPTION (pfile, dollars_in_ident) = 1;
00154 CPP_OPTION (pfile, warn_dollars) = 1;
00155 CPP_OPTION (pfile, warn_variadic_macros) = 1;
00156
00157
00158
00159 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
00160 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
00161 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
00162 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
00163 CPP_OPTION (pfile, unsigned_char) = 0;
00164 CPP_OPTION (pfile, unsigned_wchar) = 1;
00165 CPP_OPTION (pfile, bytes_big_endian) = 1;
00166
00167
00168 CPP_OPTION (pfile, narrow_charset) = _cpp_default_encoding ();
00169 CPP_OPTION (pfile, wide_charset) = 0;
00170
00171
00172 CPP_OPTION (pfile, input_charset) = _cpp_default_encoding ();
00173
00174
00175
00176
00177
00178 pfile->no_search_path.name = (char *) "";
00179
00180
00181 pfile->line_table = line_table;
00182
00183
00184 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
00185
00186
00187 pfile->avoid_paste.type = CPP_PADDING;
00188 pfile->avoid_paste.val.source = NULL;
00189 pfile->eof.type = CPP_EOF;
00190 pfile->eof.flags = 0;
00191
00192
00193 _cpp_init_tokenrun (&pfile->base_run, 250);
00194 pfile->cur_run = &pfile->base_run;
00195 pfile->cur_token = pfile->base_run.base;
00196
00197
00198 pfile->context = &pfile->base_context;
00199 pfile->base_context.macro = 0;
00200 pfile->base_context.prev = pfile->base_context.next = 0;
00201
00202
00203 pfile->a_buff = _cpp_get_buff (pfile, 0);
00204 pfile->u_buff = _cpp_get_buff (pfile, 0);
00205
00206
00207 _cpp_expand_op_stack (pfile);
00208
00209
00210 _obstack_begin (&pfile->buffer_ob, 0, 0,
00211 (void *(*) (long)) xmalloc,
00212 (void (*) (void *)) free);
00213
00214 _cpp_init_files (pfile);
00215
00216 _cpp_init_hashtable (pfile, table);
00217
00218 return pfile;
00219 }
00220
00221
00222
00223 void
00224 cpp_destroy (cpp_reader *pfile)
00225 {
00226 cpp_context *context, *contextn;
00227 tokenrun *run, *runn;
00228
00229 free (pfile->op_stack);
00230
00231 while (CPP_BUFFER (pfile) != NULL)
00232 _cpp_pop_buffer (pfile);
00233
00234 if (pfile->out.base)
00235 free (pfile->out.base);
00236
00237 if (pfile->macro_buffer)
00238 {
00239 free (pfile->macro_buffer);
00240 pfile->macro_buffer = NULL;
00241 pfile->macro_buffer_len = 0;
00242 }
00243
00244 if (pfile->deps)
00245 deps_free (pfile->deps);
00246 obstack_free (&pfile->buffer_ob, 0);
00247
00248 _cpp_destroy_hashtable (pfile);
00249 _cpp_cleanup_files (pfile);
00250 _cpp_destroy_iconv (pfile);
00251
00252 _cpp_free_buff (pfile->a_buff);
00253 _cpp_free_buff (pfile->u_buff);
00254 _cpp_free_buff (pfile->free_buffs);
00255
00256 for (run = &pfile->base_run; run; run = runn)
00257 {
00258 runn = run->next;
00259 free (run->base);
00260 if (run != &pfile->base_run)
00261 free (run);
00262 }
00263
00264 for (context = pfile->base_context.next; context; context = contextn)
00265 {
00266 contextn = context->next;
00267 free (context);
00268 }
00269
00270 free (pfile);
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 struct builtin
00288 {
00289 const uchar *name;
00290 unsigned short len;
00291 unsigned short value;
00292 };
00293
00294 #define B(n, t) { DSC(n), t }
00295 static const struct builtin builtin_array[] =
00296 {
00297 B("__TIME__", BT_TIME),
00298 B("__DATE__", BT_DATE),
00299 B("__FILE__", BT_FILE),
00300 B("__BASE_FILE__", BT_BASE_FILE),
00301 B("__LINE__", BT_SPECLINE),
00302 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
00303
00304
00305 B("_Pragma", BT_PRAGMA),
00306 B("__STDC__", BT_STDC),
00307 };
00308
00309 static const struct builtin operator_array[] =
00310 {
00311 B("and", CPP_AND_AND),
00312 B("and_eq", CPP_AND_EQ),
00313 B("bitand", CPP_AND),
00314 B("bitor", CPP_OR),
00315 B("compl", CPP_COMPL),
00316 B("not", CPP_NOT),
00317 B("not_eq", CPP_NOT_EQ),
00318 B("or", CPP_OR_OR),
00319 B("or_eq", CPP_OR_EQ),
00320 B("xor", CPP_XOR),
00321 B("xor_eq", CPP_XOR_EQ)
00322 };
00323 #undef B
00324
00325
00326 static void
00327 mark_named_operators (cpp_reader *pfile)
00328 {
00329 const struct builtin *b;
00330
00331 for (b = operator_array;
00332 b < (operator_array + ARRAY_SIZE (operator_array));
00333 b++)
00334 {
00335 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
00336 hp->flags |= NODE_OPERATOR;
00337 hp->is_directive = 0;
00338 hp->directive_index = b->value;
00339 }
00340 }
00341
00342
00343
00344
00345 void
00346 cpp_init_builtins (cpp_reader *pfile, int hosted)
00347 {
00348 const struct builtin *b;
00349 size_t n = ARRAY_SIZE (builtin_array);
00350
00351 if (CPP_OPTION (pfile, traditional))
00352 n -= 2;
00353
00354 for(b = builtin_array; b < builtin_array + n; b++)
00355 {
00356 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
00357 hp->type = NT_MACRO;
00358 hp->flags |= NODE_BUILTIN | NODE_WARN;
00359 hp->value.builtin = b->value;
00360 }
00361
00362 if (CPP_OPTION (pfile, cplusplus))
00363 _cpp_define_builtin (pfile, "__cplusplus 1");
00364 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
00365 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
00366 else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
00367 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
00368 else if (CPP_OPTION (pfile, c99))
00369 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
00370
00371 if (hosted)
00372 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 1");
00373 else
00374 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 0");
00375
00376 if (CPP_OPTION (pfile, objc))
00377 _cpp_define_builtin (pfile, "__OBJC__ 1");
00378 }
00379
00380
00381
00382 #if ENABLE_CHECKING
00383 static void sanity_checks (cpp_reader *);
00384 static void sanity_checks (cpp_reader *pfile)
00385 {
00386 cppchar_t test = 0;
00387 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
00388
00389
00390
00391 test--;
00392 if (test < 1)
00393 cpp_error (pfile, CPP_DL_ICE, "cppchar_t must be an unsigned type");
00394
00395 if (CPP_OPTION (pfile, precision) > max_precision)
00396 cpp_error (pfile, CPP_DL_ICE,
00397 "preprocessor arithmetic has maximum precision of %lu bits;"
00398 " target requires %lu bits",
00399 (unsigned long) max_precision,
00400 (unsigned long) CPP_OPTION (pfile, precision));
00401
00402 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
00403 cpp_error (pfile, CPP_DL_ICE,
00404 "CPP arithmetic must be at least as precise as a target int");
00405
00406 if (CPP_OPTION (pfile, char_precision) < 8)
00407 cpp_error (pfile, CPP_DL_ICE, "target char is less than 8 bits wide");
00408
00409 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
00410 cpp_error (pfile, CPP_DL_ICE,
00411 "target wchar_t is narrower than target char");
00412
00413 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
00414 cpp_error (pfile, CPP_DL_ICE,
00415 "target int is narrower than target char");
00416
00417
00418 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
00419 cpp_error (pfile, CPP_DL_ICE,
00420 "CPP half-integer narrower than CPP character");
00421
00422 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
00423 cpp_error (pfile, CPP_DL_ICE,
00424 "CPP on this host cannot handle wide character constants over"
00425 " %lu bits, but the target requires %lu bits",
00426 (unsigned long) BITS_PER_CPPCHAR_T,
00427 (unsigned long) CPP_OPTION (pfile, wchar_precision));
00428 }
00429 #else
00430 # define sanity_checks(PFILE)
00431 #endif
00432
00433
00434
00435 void
00436 cpp_post_options (cpp_reader *pfile)
00437 {
00438 sanity_checks (pfile);
00439
00440 post_options (pfile);
00441
00442
00443 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
00444 mark_named_operators (pfile);
00445 }
00446
00447
00448
00449
00450 const char *
00451 cpp_read_main_file (cpp_reader *pfile, const char *fname)
00452 {
00453 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
00454 {
00455 if (!pfile->deps)
00456 pfile->deps = deps_init ();
00457
00458
00459 deps_add_default_target (pfile->deps, fname);
00460 }
00461
00462 pfile->main_file
00463 = _cpp_find_file (pfile, fname, &pfile->no_search_path, false);
00464 if (_cpp_find_failed (pfile->main_file))
00465 return NULL;
00466
00467 _cpp_stack_file (pfile, pfile->main_file, false);
00468
00469
00470
00471 if (CPP_OPTION (pfile, preprocessed))
00472 {
00473 read_original_filename (pfile);
00474 fname = pfile->line_table->maps[pfile->line_table->used-1].to_file;
00475 }
00476 return fname;
00477 }
00478
00479
00480
00481
00482
00483 static void
00484 read_original_filename (cpp_reader *pfile)
00485 {
00486 const cpp_token *token, *token1;
00487
00488
00489
00490 token = _cpp_lex_direct (pfile);
00491 if (token->type == CPP_HASH)
00492 {
00493 token1 = _cpp_lex_direct (pfile);
00494 _cpp_backup_tokens (pfile, 1);
00495
00496
00497 if (token1->type == CPP_NUMBER)
00498 {
00499 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
00500 read_original_directory (pfile);
00501 return;
00502 }
00503 }
00504
00505
00506 _cpp_backup_tokens (pfile, 1);
00507 }
00508
00509
00510
00511
00512 static void
00513 read_original_directory (cpp_reader *pfile)
00514 {
00515 const cpp_token *hash, *token;
00516
00517
00518
00519 hash = _cpp_lex_direct (pfile);
00520 if (hash->type != CPP_HASH)
00521 {
00522 _cpp_backup_tokens (pfile, 1);
00523 return;
00524 }
00525
00526 token = _cpp_lex_direct (pfile);
00527
00528 if (token->type != CPP_NUMBER)
00529 {
00530 _cpp_backup_tokens (pfile, 2);
00531 return;
00532 }
00533
00534 token = _cpp_lex_direct (pfile);
00535
00536 if (token->type != CPP_STRING
00537 || ! (token->val.str.len >= 5
00538 && token->val.str.text[token->val.str.len-2] == '/'
00539 && token->val.str.text[token->val.str.len-3] == '/'))
00540 {
00541 _cpp_backup_tokens (pfile, 3);
00542 return;
00543 }
00544
00545 if (pfile->cb.dir_change)
00546 {
00547 char *debugdir = alloca (token->val.str.len - 3);
00548
00549 memcpy (debugdir, (const char *) token->val.str.text + 1,
00550 token->val.str.len - 4);
00551 debugdir[token->val.str.len - 4] = '\0';
00552
00553 pfile->cb.dir_change (pfile, debugdir);
00554 }
00555 }
00556
00557
00558
00559
00560
00561
00562
00563 int
00564 cpp_finish (cpp_reader *pfile, FILE *deps_stream)
00565 {
00566
00567 if (CPP_OPTION (pfile, warn_unused_macros))
00568 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
00569
00570
00571
00572
00573
00574
00575 while (pfile->buffer)
00576 _cpp_pop_buffer (pfile);
00577
00578
00579 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
00580 && deps_stream && pfile->errors == 0)
00581 {
00582 deps_write (pfile->deps, deps_stream, 72);
00583
00584 if (CPP_OPTION (pfile, deps.phony_targets))
00585 deps_phony_targets (pfile->deps, deps_stream);
00586 }
00587
00588
00589 if (CPP_OPTION (pfile, print_include_names))
00590 _cpp_report_missing_guards (pfile);
00591
00592 return pfile->errors;
00593 }
00594
00595 static void
00596 post_options (cpp_reader *pfile)
00597 {
00598
00599 if (CPP_OPTION (pfile, cplusplus))
00600 CPP_OPTION (pfile, warn_traditional) = 0;
00601
00602
00603
00604 if (CPP_OPTION (pfile, preprocessed))
00605 {
00606 pfile->state.prevent_expansion = 1;
00607 CPP_OPTION (pfile, traditional) = 0;
00608 }
00609
00610 if (CPP_OPTION (pfile, warn_trigraphs) == 2)
00611 CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs);
00612
00613 if (CPP_OPTION (pfile, traditional))
00614 {
00615 CPP_OPTION (pfile, cplusplus_comments) = 0;
00616
00617
00618 CPP_OPTION (pfile, show_column) = 0;
00619 CPP_OPTION (pfile, trigraphs) = 0;
00620 CPP_OPTION (pfile, warn_trigraphs) = 0;
00621 }
00622 }