diff options
author | aaw <aaw@138bc75d-0d04-0410-961f-82ee72b054a4> | 2007-07-30 18:29:20 +0000 |
---|---|---|
committer | aaw <aaw@138bc75d-0d04-0410-961f-82ee72b054a4> | 2007-07-30 18:29:20 +0000 |
commit | fcde64dc3abc672d1353e44993b839c4db1d5758 (patch) | |
tree | bb584ffda6bfb46d8b03b533da0e2f408e0bbd23 /libcpp | |
parent | 81a410b1e4f861bb1eb31c7fa7daedf6bf70abc4 (diff) | |
download | gcc-fcde64dc3abc672d1353e44993b839c4db1d5758.tar.gz |
libcpp/
* directives-only.c: New file.
* internal.h (struct _cpp_dir_only_callbacks): New.
(_cpp_preprocess_dir_only): New function.
* directives.c (_cpp_handle_directive): Check directives_only before
disabling execution of indented directives.
* files.c (_cpp_stack_file): Add directives_only check.
* include/cpplib.h (struct cpp_options): Add directives_only.
(cpp_init_special_builtins): New function.
* init.c (cpp_init_special_builtins): New function.
(cpp_init_builtins): Move builtin_array initialization to
cpp_init_special_builtins.
(post_options): Check directives_only before setting
pfile->state.prevent_expansion = 1.
* macro.c (_cpp_builtin_macro_text): Print an error if __COUNTER__
is expanded inside a directive while -fdirectives-only is enabled.
* Makefile.in (libcpp_a_OBJS): Add directives-only.o.
(libcpp_a_SOURCES): Add directives-only.c.
gcc/
* c-ppoutput.c (print_lines_directives_only): New function.
(scan_translation_unit_directives_only): New function.
(preprocess_file): Add call to scan_translation_unit_directives_only.
* c-opts.c (c_common_handle_option): Add OPT_fdirectives_only.
(sanitize_cpp_opts): Add default flag_dump_macros setting for
-fdirectives-only. Add errors for -fdirectives-only conflict with
-Wunused-macros and -traditional.
(finish_options): Add builtin macro initialization for
-fdirectives-only + -fpreprocessed.
* c.opt (fdirectives-only): New.
* doc/cppopts.texi (fdirectives-only): New.
gcc/testsuite/
* gcc.dg/cpp/counter-2.c: New test.
* gcc.dg/cpp/counter-3.c: New test.
* gcc.dg/cpp/dir-only-1.c: New test.
* gcc.dg/cpp/dir-only-1.h: New file.
* gcc.dg/cpp/dir-only-2.c: New test.
* gcc.dg/cpp/dir-only-3.c: New test.
* gcc.dg/cpp/dir-only-3a.h: New file.
* gcc.dg/cpp/dir-only-3b.h: New file.
* gcc.dg/cpp/dir-only-4.c: New test.
* gcc.dg/cpp/dir-only-5.c: New test.
* gcc.dg/cpp/dir-only-6.c: New test.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@127066 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libcpp')
-rw-r--r-- | libcpp/ChangeLog | 20 | ||||
-rw-r--r-- | libcpp/Makefile.in | 12 | ||||
-rw-r--r-- | libcpp/directives-only.c | 240 | ||||
-rw-r--r-- | libcpp/directives.c | 7 | ||||
-rw-r--r-- | libcpp/files.c | 3 | ||||
-rw-r--r-- | libcpp/include/cpplib.h | 7 | ||||
-rw-r--r-- | libcpp/init.c | 27 | ||||
-rw-r--r-- | libcpp/internal.h | 11 | ||||
-rw-r--r-- | libcpp/macro.c | 3 |
9 files changed, 313 insertions, 17 deletions
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index 65b199dfa69..0157cf3cee2 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,3 +1,23 @@ +2007-07-30 Ollie Wild <aaw@google.com> + + * directives-only.c: New file. + * internal.h (struct _cpp_dir_only_callbacks): New. + (_cpp_preprocess_dir_only): New function. + * directives.c (_cpp_handle_directive): Check directives_only before + disabling execution of indented directives. + * files.c (_cpp_stack_file): Add directives_only check. + * include/cpplib.h (struct cpp_options): Add directives_only. + (cpp_init_special_builtins): New function. + * init.c (cpp_init_special_builtins): New function. + (cpp_init_builtins): Move builtin_array initialization to + cpp_init_special_builtins. + (post_options): Check directives_only before setting + pfile->state.prevent_expansion = 1. + * macro.c (_cpp_builtin_macro_text): Print an error if __COUNTER__ + is expanded inside a directive while -fdirectives-only is enabled. + * Makefile.in (libcpp_a_OBJS): Add directives-only.o. + (libcpp_a_SOURCES): Add directives-only.c. + 2007-07-04 Uros Bizjak <ubizjak@gmail.com> * traditional.c (_cpp_scan_out_logical_line): Initialize diff --git a/libcpp/Makefile.in b/libcpp/Makefile.in index 166f1faaee0..960a42d56e3 100644 --- a/libcpp/Makefile.in +++ b/libcpp/Makefile.in @@ -69,14 +69,14 @@ INCLUDES = -I$(srcdir) -I. -I$(srcdir)/../include @INCINTL@ \ ALL_CFLAGS = $(CFLAGS) $(WARN_CFLAGS) $(INCLUDES) $(CPPFLAGS) -libcpp_a_OBJS = charset.o directives.o errors.o expr.o files.o \ - identifiers.o init.o lex.o line-map.o macro.o mkdeps.o \ - pch.o symtab.o traditional.o +libcpp_a_OBJS = charset.o directives.o directives-only.o errors.o \ + expr.o files.o identifiers.o init.o lex.o line-map.o macro.o \ + mkdeps.o pch.o symtab.o traditional.o makedepend_OBJS = makedepend.o -libcpp_a_SOURCES = charset.c directives.c errors.c expr.c files.c \ - identifiers.c init.c lex.c line-map.c macro.c mkdeps.c \ - pch.c symtab.c traditional.c +libcpp_a_SOURCES = charset.c directives.c directives-only.c errors.c \ + expr.c files.c identifiers.c init.c lex.c line-map.c macro.c \ + mkdeps.c pch.c symtab.c traditional.c all: libcpp.a makedepend$(EXEEXT) $(USED_CATALOGS) diff --git a/libcpp/directives-only.c b/libcpp/directives-only.c new file mode 100644 index 00000000000..d50cebbdae3 --- /dev/null +++ b/libcpp/directives-only.c @@ -0,0 +1,240 @@ +/* CPP Library - directive only preprocessing for distributed compilation. + Copyright (C) 2007 + Free Software Foundation, Inc. + Contributed by Ollie Wild <aaw@google.com>. + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include "config.h" +#include "system.h" +#include "cpplib.h" +#include "internal.h" + +/* DO (Directive only) flags. */ +#define DO_BOL (1 << 0) /* At the beginning of a logical line. */ +#define DO_STRING (1 << 1) /* In a string constant. */ +#define DO_CHAR (1 << 2) /* In a character constant. */ +#define DO_BLOCK_COMMENT (1 << 3) /* In a block comment. */ +#define DO_LINE_COMMENT (1 << 4) /* In a single line "//-style" comment. */ + +#define DO_LINE_SPECIAL (DO_STRING | DO_CHAR | DO_LINE_COMMENT) +#define DO_SPECIAL (DO_LINE_SPECIAL | DO_BLOCK_COMMENT) + +/* 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; + int lines, 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. */ + LINEMAP_POSITION_FOR_COLUMN (loc, 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; +} diff --git a/libcpp/directives.c b/libcpp/directives.c index ccb9f32b61d..38ca949f8c9 100644 --- a/libcpp/directives.c +++ b/libcpp/directives.c @@ -424,8 +424,13 @@ _cpp_handle_directive (cpp_reader *pfile, int indented) does not cause '#define foo bar' to get executed when compiled with -save-temps, we recognize directives in -fpreprocessed mode only if the # is in column 1. macro.c - puts a space in front of any '#' at the start of a macro. */ + puts a space in front of any '#' at the start of a macro. + + We exclude the -fdirectives-only case because macro expansion + has not been performed yet, and block comments can cause spaces + to preceed the directive. */ if (CPP_OPTION (pfile, preprocessed) + && !CPP_OPTION (pfile, directives_only) && (indented || !(dir->flags & IN_I))) { skip = 0; diff --git a/libcpp/files.c b/libcpp/files.c index e03324b2e4f..73f88bb3cde 100644 --- a/libcpp/files.c +++ b/libcpp/files.c @@ -791,7 +791,8 @@ _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) /* Stack the buffer. */ buffer = cpp_push_buffer (pfile, file->buffer, file->st.st_size, - CPP_OPTION (pfile, preprocessed)); + CPP_OPTION (pfile, preprocessed) + && !CPP_OPTION (pfile, directives_only)); buffer->file = file; buffer->sysp = sysp; diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index ee46c4f169f..2dac4c2d988 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -440,6 +440,9 @@ struct cpp_options /* True means error callback should be used for diagnostics. */ bool client_diagnostic; + + /* True disables tokenization outside of preprocessing directives. */ + bool directives_only; }; /* Callback for header lookup for HEADER, which is the name of a @@ -644,6 +647,10 @@ extern struct deps *cpp_get_deps (cpp_reader *); too. If there was an error opening the file, it returns NULL. */ extern const char *cpp_read_main_file (cpp_reader *, const char *); +/* Set up built-ins with special behavior. Use cpp_init_builtins() + instead unless your know what you are doing. */ +extern void cpp_init_special_builtins (cpp_reader *); + /* Set up built-ins like __FILE__. */ extern void cpp_init_builtins (cpp_reader *, int); diff --git a/libcpp/init.c b/libcpp/init.c index 71583df94d6..62f4f95dcf4 100644 --- a/libcpp/init.c +++ b/libcpp/init.c @@ -350,11 +350,8 @@ mark_named_operators (cpp_reader *pfile) } } -/* Read the builtins table above and enter them, and language-specific - macros, into the hash table. HOSTED is true if this is a hosted - environment. */ void -cpp_init_builtins (cpp_reader *pfile, int hosted) +cpp_init_special_builtins (cpp_reader *pfile) { const struct builtin *b; size_t n = ARRAY_SIZE (builtin_array); @@ -363,10 +360,7 @@ cpp_init_builtins (cpp_reader *pfile, int hosted) n -= 2; else if (! CPP_OPTION (pfile, stdc_0_in_system_headers) || CPP_OPTION (pfile, std)) - { - n--; - _cpp_define_builtin (pfile, "__STDC__ 1"); - } + n--; for (b = builtin_array; b < builtin_array + n; b++) { @@ -375,6 +369,20 @@ cpp_init_builtins (cpp_reader *pfile, int hosted) hp->flags |= NODE_BUILTIN | NODE_WARN; hp->value.builtin = (enum builtin_type) b->value; } +} + +/* Read the builtins table above and enter them, and language-specific + macros, into the hash table. HOSTED is true if this is a hosted + environment. */ +void +cpp_init_builtins (cpp_reader *pfile, int hosted) +{ + cpp_init_special_builtins (pfile); + + if (!CPP_OPTION (pfile, traditional) + && (! CPP_OPTION (pfile, stdc_0_in_system_headers) + || CPP_OPTION (pfile, std))) + _cpp_define_builtin (pfile, "__STDC__ 1"); if (CPP_OPTION (pfile, cplusplus)) _cpp_define_builtin (pfile, "__cplusplus 1"); @@ -622,7 +630,8 @@ post_options (cpp_reader *pfile) preprocessed text. Read preprocesed source in ISO mode. */ if (CPP_OPTION (pfile, preprocessed)) { - pfile->state.prevent_expansion = 1; + if (!CPP_OPTION (pfile, directives_only)) + pfile->state.prevent_expansion = 1; CPP_OPTION (pfile, traditional) = 0; } diff --git a/libcpp/internal.h b/libcpp/internal.h index d000cfda54f..8561088f905 100644 --- a/libcpp/internal.h +++ b/libcpp/internal.h @@ -575,6 +575,17 @@ extern void _cpp_do_file_change (cpp_reader *, enum lc_reason, const char *, unsigned int, unsigned int); extern void _cpp_pop_buffer (cpp_reader *); +/* In directives.c */ +struct _cpp_dir_only_callbacks +{ + /* Called to print a block of lines. */ + void (*print_lines) (int, const void *, size_t); + void (*maybe_print_line) (source_location); +}; + +extern void _cpp_preprocess_dir_only (cpp_reader *, + const struct _cpp_dir_only_callbacks *); + /* In traditional.c. */ extern bool _cpp_scan_out_logical_line (cpp_reader *, cpp_macro *); extern bool _cpp_read_logical_line_trad (cpp_reader *); diff --git a/libcpp/macro.c b/libcpp/macro.c index c8d099ed210..f2427172ca0 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -265,6 +265,9 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node) break; case BT_COUNTER: + if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive) + cpp_error (pfile, CPP_DL_ERROR, + "__COUNTER__ expanded inside directive with -fdirectives-only"); number = pfile->counter++; break; } |