/* Process the notes created by add_line_note as far as the current location. */ void _cpp_process_line_notes (cpp_reader *pfile, int in_comment) { cpp_buffer *buffer = pfile->buffer; for (;;) { _cpp_line_note *note = &buffer->notes[buffer->cur_note]; unsigned int col; if (note->pos > buffer->cur) break; buffer->cur_note++; col = CPP_BUF_COLUMN (buffer, note->pos + 1); if (note->type == '\\' || note->type == ' ') { if (note->type == ' ' && !in_comment) cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col, "backslash and newline separated by space"); if (buffer->next_line > buffer->rlimit) { cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, col, "backslash-newline at end of file"); /* Prevent "no newline at end of file" warning. */ buffer->next_line = buffer->rlimit; } buffer->line_base = note->pos; CPP_INCREMENT_LINE (pfile, 0); } else if (_cpp_trigraph_map[note->type]) { if (CPP_OPTION (pfile, warn_trigraphs) && (!in_comment || warn_in_comment (pfile, note))) { if (CPP_OPTION (pfile, trigraphs)) cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col, "trigraph ??%c converted to %c", note->type, (int) _cpp_trigraph_map[note->type]); else { cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col, "trigraph ??%c ignored, use -trigraphs to enable", note->type); } } } else abort (); } }
/* Skips whitespace, saving the next non-whitespace character. */ static void skip_whitespace (cpp_reader *pfile, cppchar_t c) { cpp_buffer *buffer = pfile->buffer; bool saw_NUL = false; do { /* Horizontal space always OK. */ if (c == ' ' || c == '\t') ; /* Just \f \v or \0 left. */ else if (c == '\0') saw_NUL = true; else if (pfile->state.in_directive && CPP_PEDANTIC (pfile)) cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, CPP_BUF_COL (buffer), "%s in preprocessing directive", c == '\f' ? "form feed" : "vertical tab"); c = *buffer->cur++; } /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */ while (is_nvspace (c)); if (saw_NUL) cpp_error (pfile, CPP_DL_WARNING, "null character(s) ignored"); buffer->cur--; }
/* Skip a C-style block comment. We find the end of the comment by seeing if an asterisk is before every '/' we encounter. Returns nonzero if comment terminated by EOF, zero otherwise. Buffer->cur points to the initial asterisk of the comment. */ bool _cpp_skip_block_comment (cpp_reader *pfile) { cpp_buffer *buffer = pfile->buffer; const uchar *cur = buffer->cur; uchar c; cur++; if (*cur == '/') cur++; for (;;) { /* People like decorating comments with '*', so check for '/' instead for efficiency. */ c = *cur++; if (c == '/') { if (cur[-2] == '*') break; /* Warn about potential nested comments, but not if the '/' comes immediately before the true comment delimiter. Don't bother to get it right across escaped newlines. */ if (CPP_OPTION (pfile, warn_comments) && cur[0] == '*' && cur[1] != '/') { buffer->cur = cur; cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, CPP_BUF_COL (buffer), "\"/*\" within comment"); } } else if (c == '\n') { unsigned int cols; buffer->cur = cur - 1; _cpp_process_line_notes (pfile, true); if (buffer->next_line >= buffer->rlimit) return true; _cpp_clean_line (pfile); cols = buffer->next_line - buffer->line_base; CPP_INCREMENT_LINE (pfile, cols); cur = buffer->cur; } } buffer->cur = cur; _cpp_process_line_notes (pfile, true); return false; }
/* After parsing an identifier or other sequence, produce a warning about sequences not in NFC/NFKC. */ static void warn_about_normalization (cpp_reader *pfile, const cpp_token *token, const struct normalize_state *s) { if (CPP_OPTION (pfile, warn_normalize) < NORMALIZE_STATE_RESULT (s) && !pfile->state.skipping) { /* Make sure that the token is printed using UCNs, even if we'd otherwise happily print UTF-8. */ unsigned char *buf = XNEWVEC (unsigned char, cpp_token_len (token)); size_t sz; sz = cpp_spell_token (pfile, token, buf, false) - buf; if (NORMALIZE_STATE_RESULT (s) == normalized_C) cpp_error_with_line (pfile, CPP_DL_WARNING, token->src_loc, 0, "`%.*s' is not in NFKC", (int) sz, buf); else cpp_error_with_line (pfile, CPP_DL_WARNING, token->src_loc, 0, "`%.*s' is not in NFC", (int) sz, buf); }
/* CUR points to the asterisk introducing a comment in the current context. IN_DEFINE is true if we are in the replacement text of a macro. The asterisk and following comment is copied to the buffer pointed to by pfile->out.cur, which must be of sufficient size. Unterminated comments are diagnosed, and correctly terminated in the output. pfile->out.cur is updated depending upon IN_DEFINE, -C, -CC and pfile->state.in_directive. Returns a pointer to the first character after the comment in the input buffer. */ static const uchar * copy_comment (cpp_reader *pfile, const uchar *cur, int in_define) { bool unterminated, copy = false; unsigned int from_line = pfile->line; cpp_buffer *buffer = pfile->buffer; buffer->cur = cur; if (pfile->context->prev) unterminated = false, skip_macro_block_comment (pfile); else unterminated = _cpp_skip_block_comment (pfile); if (unterminated) cpp_error_with_line (pfile, CPP_DL_ERROR, from_line, 0, "unterminated comment"); /* Comments in directives become spaces so that tokens are properly separated when the ISO preprocessor re-lexes the line. The exception is #define. */ if (pfile->state.in_directive) { if (in_define) { if (CPP_OPTION (pfile, discard_comments_in_macro_exp)) pfile->out.cur--; else copy = true; } else pfile->out.cur[-1] = ' '; } else if (CPP_OPTION (pfile, discard_comments)) pfile->out.cur--; else copy = true; if (copy) { size_t len = (size_t) (buffer->cur - cur); memcpy (pfile->out.cur, cur, len); pfile->out.cur += len; if (unterminated) { *pfile->out.cur++ = '*'; *pfile->out.cur++ = '/'; } } return buffer->cur; }
/* Returns true if a fresh line has been loaded. */ bool _cpp_get_fresh_line (cpp_reader *pfile) { int return_at_eof; /* We can't get a new line until we leave the current directive. */ if (pfile->state.in_directive) return false; for (;;) { cpp_buffer *buffer = pfile->buffer; if (!buffer->need_line) return true; if (buffer->next_line < buffer->rlimit) { _cpp_clean_line (pfile); return true; } /* First, get out of parsing arguments state. */ if (pfile->state.parsing_args) return false; /* End of buffer. Non-empty files should end in a newline. */ if (buffer->buf != buffer->rlimit && buffer->next_line > buffer->rlimit && !buffer->from_stage3) { /* Only warn once. */ buffer->next_line = buffer->rlimit; cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line - 1, CPP_BUF_COLUMN (buffer, buffer->cur), "no newline at end of file"); } return_at_eof = buffer->return_at_eof; _cpp_pop_buffer (pfile); if (pfile->buffer == NULL || return_at_eof) return false; } }
/* Writes out the preprocessed file, handling spacing and paste avoidance issues. */ void _cpp_preprocess_dir_only (cpp_reader *pfile, const struct _cpp_dir_only_callbacks *cb) { struct cpp_buffer *buffer; const unsigned char *cur, *base, *next_line, *rlimit; cppchar_t c, last_c; unsigned flags; linenum_type lines; int col; source_location loc; restart: /* Buffer initialization ala _cpp_clean_line(). */ buffer = pfile->buffer; buffer->cur_note = buffer->notes_used = 0; buffer->cur = buffer->line_base = buffer->next_line; buffer->need_line = false; /* This isn't really needed. It prevents a compiler warning, though. */ loc = pfile->line_table->highest_line; /* Scan initialization. */ next_line = cur = base = buffer->cur; rlimit = buffer->rlimit; flags = DO_BOL; lines = 0; col = 1; for (last_c = '\n', c = *cur; cur < rlimit; last_c = c, c = *++cur, ++col) { /* Skip over escaped newlines. */ if (__builtin_expect (c == '\\', false)) { const unsigned char *tmp = cur + 1; while (is_nvspace (*tmp) && tmp < rlimit) tmp++; if (*tmp == '\r') tmp++; if (*tmp == '\n' && tmp < rlimit) { CPP_INCREMENT_LINE (pfile, 0); lines++; col = 0; cur = tmp; c = last_c; continue; } } if (__builtin_expect (last_c == '#', false) && !(flags & DO_SPECIAL)) { if (c != '#' && (flags & DO_BOL)) { struct line_maps *line_table; if (!pfile->state.skipping && next_line != base) cb->print_lines (lines, base, next_line - base); /* Prep things for directive handling. */ buffer->next_line = cur; buffer->need_line = true; _cpp_get_fresh_line (pfile); /* Ensure proper column numbering for generated error messages. */ buffer->line_base -= col - 1; _cpp_handle_directive (pfile, 0 /* ignore indented */); /* Sanitize the line settings. Duplicate #include's can mess things up. */ line_table = pfile->line_table; line_table->highest_location = line_table->highest_line; /* The if block prevents us from outputing line information when the file ends with a directive and no newline. Note that we must use pfile->buffer, not buffer. */ if (pfile->buffer->next_line < pfile->buffer->rlimit) cb->maybe_print_line (pfile->line_table->highest_line); goto restart; } flags &= ~DO_BOL; pfile->mi_valid = false; } else if (__builtin_expect (last_c == '/', false) \ && !(flags & DO_SPECIAL) && c != '*' && c != '/') { /* If a previous slash is not starting a block comment, clear the DO_BOL flag. */ flags &= ~DO_BOL; pfile->mi_valid = false; } switch (c) { case '/': if ((flags & DO_BLOCK_COMMENT) && last_c == '*') { flags &= ~DO_BLOCK_COMMENT; c = 0; } else if (!(flags & DO_SPECIAL) && last_c == '/') flags |= DO_LINE_COMMENT; else if (!(flags & DO_SPECIAL)) /* Mark the position for possible error reporting. */ loc = linemap_position_for_column (pfile->line_table, col); break; case '*': if (!(flags & DO_SPECIAL)) { if (last_c == '/') flags |= DO_BLOCK_COMMENT; else { flags &= ~DO_BOL; pfile->mi_valid = false; } } break; case '\'': case '"': { unsigned state = (c == '"') ? DO_STRING : DO_CHAR; if (!(flags & DO_SPECIAL)) { flags |= state; flags &= ~DO_BOL; pfile->mi_valid = false; } else if ((flags & state) && last_c != '\\') flags &= ~state; break; } case '\\': { if ((flags & (DO_STRING | DO_CHAR)) && last_c == '\\') c = 0; if (!(flags & DO_SPECIAL)) { flags &= ~DO_BOL; pfile->mi_valid = false; } break; } case '\n': CPP_INCREMENT_LINE (pfile, 0); lines++; col = 0; flags &= ~DO_LINE_SPECIAL; if (!(flags & DO_SPECIAL)) flags |= DO_BOL; break; case '#': next_line = cur; /* Don't update DO_BOL yet. */ break; case ' ': case '\t': case '\f': case '\v': case '\0': break; default: if (!(flags & DO_SPECIAL)) { flags &= ~DO_BOL; pfile->mi_valid = false; } break; } } if (flags & DO_BLOCK_COMMENT) cpp_error_with_line (pfile, CPP_DL_ERROR, loc, 0, "unterminated comment"); if (!pfile->state.skipping && cur != base) { /* If the file was not newline terminated, add rlimit, which is guaranteed to point to a newline, to the end of our range. */ if (cur[-1] != '\n') { cur++; CPP_INCREMENT_LINE (pfile, 0); lines++; } cb->print_lines (lines, base, cur - base); } _cpp_pop_buffer (pfile); if (pfile->buffer) goto restart; }
/* Copies the next logical line in the current buffer (starting at buffer->cur) to the output buffer. The output is guaranteed to terminate with a NUL character. buffer->cur is updated. If MACRO is non-NULL, then we are scanning the replacement list of MACRO, and we call save_replacement_text() every time we meet an argument. */ bool _cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro) { bool result = true; cpp_context *context; const uchar *cur; uchar *out; struct fun_macro fmacro; unsigned int c, paren_depth = 0, quote; enum ls lex_state = ls_none; bool header_ok; const uchar *start_of_input_line; fmacro.buff = NULL; quote = 0; header_ok = pfile->state.angled_headers; CUR (pfile->context) = pfile->buffer->cur; RLIMIT (pfile->context) = pfile->buffer->rlimit; pfile->out.cur = pfile->out.base; pfile->out.first_line = pfile->line; /* start_of_input_line is needed to make sure that directives really, really start at the first character of the line. */ start_of_input_line = pfile->buffer->cur; new_context: context = pfile->context; cur = CUR (context); check_output_buffer (pfile, RLIMIT (context) - cur); out = pfile->out.cur; for (;;) { if (!context->prev && cur >= pfile->buffer->notes[pfile->buffer->cur_note].pos) { pfile->buffer->cur = cur; _cpp_process_line_notes (pfile, false); } c = *cur++; *out++ = c; /* Whitespace should "continue" out of the switch, non-whitespace should "break" out of it. */ switch (c) { case ' ': case '\t': case '\f': case '\v': case '\0': continue; case '\n': /* If this is a macro's expansion, pop it. */ if (context->prev) { pfile->out.cur = out - 1; _cpp_pop_context (pfile); goto new_context; } /* Omit the newline from the output buffer. */ pfile->out.cur = out - 1; pfile->buffer->cur = cur; pfile->buffer->need_line = true; pfile->line++; if ((lex_state == ls_fun_open || lex_state == ls_fun_close) && !pfile->state.in_directive && _cpp_get_fresh_line (pfile)) { /* Newlines in arguments become a space, but we don't clear any in-progress quote. */ if (lex_state == ls_fun_close) out[-1] = ' '; cur = pfile->buffer->cur; continue; } goto done; case '<': if (header_ok) quote = '>'; break; case '>': if (c == quote) quote = 0; break; case '"': case '\'': if (c == quote) quote = 0; else if (!quote) quote = c; break; case '\\': /* Skip escaped quotes here, it's easier than above. */ if (*cur == '\\' || *cur == '"' || *cur == '\'') *out++ = *cur++; break; case '/': /* Traditional CPP does not recognize comments within literals. */ if (!quote && *cur == '*') { pfile->out.cur = out; cur = copy_comment (pfile, cur, macro != 0); out = pfile->out.cur; continue; } break; case '_': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': if (!pfile->state.skipping && (quote == 0 || macro)) { cpp_hashnode *node; uchar *out_start = out - 1; pfile->out.cur = out_start; node = lex_identifier (pfile, cur - 1); out = pfile->out.cur; cur = CUR (context); if (node->type == NT_MACRO /* Should we expand for ls_answer? */ && (lex_state == ls_none || lex_state == ls_fun_open) && !pfile->state.prevent_expansion) { /* Macros invalidate MI optimization. */ pfile->mi_valid = false; if (! (node->flags & NODE_BUILTIN) && node->value.macro->fun_like) { maybe_start_funlike (pfile, node, out_start, &fmacro); lex_state = ls_fun_open; fmacro.line = pfile->line; continue; } else if (!recursive_macro (pfile, node)) { /* Remove the object-like macro's name from the output, and push its replacement text. */ pfile->out.cur = out_start; push_replacement_text (pfile, node); lex_state = ls_none; goto new_context; } } else if (macro && (node->flags & NODE_MACRO_ARG) != 0) { /* Found a parameter in the replacement text of a #define. Remove its name from the output. */ pfile->out.cur = out_start; save_replacement_text (pfile, macro, node->value.arg_index); out = pfile->out.base; } else if (lex_state == ls_hash) { lex_state = ls_predicate; continue; } else if (pfile->state.in_expression && node == pfile->spec_nodes.n_defined) { lex_state = ls_defined; continue; } } break; case '(': if (quote == 0) { paren_depth++; if (lex_state == ls_fun_open) { if (recursive_macro (pfile, fmacro.node)) lex_state = ls_none; else { lex_state = ls_fun_close; paren_depth = 1; out = pfile->out.base + fmacro.offset; fmacro.args[0] = fmacro.offset; } } else if (lex_state == ls_predicate) lex_state = ls_answer; else if (lex_state == ls_defined) lex_state = ls_defined_close; } break; case ',': if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1) save_argument (&fmacro, out - pfile->out.base); break; case ')': if (quote == 0) { paren_depth--; if (lex_state == ls_fun_close && paren_depth == 0) { cpp_macro *m = fmacro.node->value.macro; m->used = 1; lex_state = ls_none; save_argument (&fmacro, out - pfile->out.base); /* A single zero-length argument is no argument. */ if (fmacro.argc == 1 && m->paramc == 0 && out == pfile->out.base + fmacro.offset + 1) fmacro.argc = 0; if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc)) { /* Remove the macro's invocation from the output, and push its replacement text. */ pfile->out.cur = (pfile->out.base + fmacro.offset); CUR (context) = cur; replace_args_and_push (pfile, &fmacro); goto new_context; } } else if (lex_state == ls_answer || lex_state == ls_defined_close) lex_state = ls_none; } break; case '#': if (cur - 1 == start_of_input_line /* A '#' from a macro doesn't start a directive. */ && !pfile->context->prev && !pfile->state.in_directive) { /* A directive. With the way _cpp_handle_directive currently works, we only want to call it if either we know the directive is OK, or we want it to fail and be removed from the output. If we want it to be passed through (the assembler case) then we must not call _cpp_handle_directive. */ pfile->out.cur = out; cur = skip_whitespace (pfile, cur, true /* skip_comments */); out = pfile->out.cur; if (*cur == '\n') { /* Null directive. Ignore it and don't invalidate the MI optimization. */ pfile->buffer->need_line = true; pfile->line++; result = false; goto done; } else { bool do_it = false; if (is_numstart (*cur) && CPP_OPTION (pfile, lang) != CLK_ASM) do_it = true; else if (is_idstart (*cur)) /* Check whether we know this directive, but don't advance. */ do_it = lex_identifier (pfile, cur)->is_directive; if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM) { /* This is a kludge. We want to have the ISO preprocessor lex the next token. */ pfile->buffer->cur = cur; _cpp_handle_directive (pfile, false /* indented */); result = false; goto done; } } } if (pfile->state.in_expression) { lex_state = ls_hash; continue; } break; default: break; } /* Non-whitespace disables MI optimization and stops treating '<' as a quote in #include. */ header_ok = false; if (!pfile->state.in_directive) pfile->mi_valid = false; if (lex_state == ls_none) continue; /* Some of these transitions of state are syntax errors. The ISO preprocessor will issue errors later. */ if (lex_state == ls_fun_open) /* Missing '('. */ lex_state = ls_none; else if (lex_state == ls_hash || lex_state == ls_predicate || lex_state == ls_defined) lex_state = ls_none; /* ls_answer and ls_defined_close keep going until ')'. */ } done: if (fmacro.buff) _cpp_release_buff (pfile, fmacro.buff); if (lex_state == ls_fun_close) cpp_error_with_line (pfile, CPP_DL_ERROR, fmacro.line, 0, "unterminated argument list invoking macro \"%s\"", NODE_NAME (fmacro.node)); return result; }
static struct cpp_dir * remove_duplicates (cpp_reader *pfile, struct cpp_dir *head, struct cpp_dir *system, struct cpp_dir *join, int verbose) { struct cpp_dir **pcur, *tmp, *cur; struct stat st; for (pcur = &head; *pcur; ) { int reason = REASON_QUIET; cur = *pcur; if (stat (cur->name, &st)) { /* Dirs that don't exist are silently ignored, unless verbose. */ if (errno != ENOENT) cpp_errno (pfile, CPP_DL_ERROR, cur->name); else { /* If -Wmissing-include-dirs is given, warn. */ cpp_options *opts = cpp_get_options (pfile); if (opts->warn_missing_include_dirs && cur->user_supplied_p) cpp_errno (pfile, CPP_DL_WARNING, cur->name); reason = REASON_NOENT; } } else if (!S_ISDIR (st.st_mode)) /* APPLE LOCAL begin headermaps 3871393 */ { /* Only check for headermap if this is a regular file and if there is no path-constructor function in CUR. */ if (S_ISREG (st.st_mode) && !cur->construct) { /* Try to load the file as a headermap. We will get back NULL if this fails, and there won't be any warnings/errors emitted unless the load function is fairly sure it's dealing with a headermap file that is malformed. */ struct hmap_header_map *header_map; header_map = hmap_load_header_map (cur->name); if (header_map) { /* Successfully loaded a headermap. Store a pointer to it and set up the construct function pointer in cur. */ cur->header_map = header_map; cur->construct = hmap_construct_pathname; pcur = &cur->next; continue; } } /* If we fall through to here, it's some other kind of file. */ cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0, "%s: not a directory", cur->name); } /* APPLE LOCAL end headermaps 3871393 */ else { INO_T_COPY (cur->ino, st.st_ino); cur->dev = st.st_dev; /* Remove this one if it is in the system chain. */ reason = REASON_DUP_SYS; for (tmp = system; tmp; tmp = tmp->next) if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev && cur->construct == tmp->construct) break; if (!tmp) { /* Duplicate of something earlier in the same chain? */ reason = REASON_DUP; for (tmp = head; tmp != cur; tmp = tmp->next) if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev && cur->construct == tmp->construct) break; if (tmp == cur /* Last in the chain and duplicate of JOIN? */ && !(cur->next == NULL && join && INO_T_EQ (cur->ino, join->ino) && cur->dev == join->dev && cur->construct == join->construct)) { /* Unique, so keep this directory. */ pcur = &cur->next; continue; } } } /* Remove this entry from the chain. */ *pcur = cur->next; free_path (cur, verbose ? reason: REASON_QUIET); } *pcur = join; return head; }