Commit 49634b3a by Neil Booth Committed by Neil Booth

cppfiles.c (struct _cpp_file): Rename once_only_next to next_file.

	* cppfiles.c (struct _cpp_file): Rename once_only_next to
	next_file.  Remove import and pragma_once, add once_only.
	(find_file): Add new file structures to the all_files list.
	(should_stack_file): Mark #import-ed files once-only, and
	don't stack them if the file has already been stacked.
	(_cp_mark_file_once_only): Simplify.
	* cpphash.h (struct cpp_reader): Rename once_only_files
	to all_files.  Rename saw_pragma_once to seen_once_only.
	(_cpp_mark_file_once_only): Update prototype.
	* cpplib.c (do_pragma_once): Update.
testsuite:
	* import1.c, import2.c: New tests.

From-SVN: r70106
parent cf1ee30c
2003-08-02 Neil Booth <neil@daikokuya.co.uk> 2003-08-02 Neil Booth <neil@daikokuya.co.uk>
* cppfiles.c (struct _cpp_file): Rename once_only_next to
next_file. Remove import and pragma_once, add once_only.
(find_file): Add new file structures to the all_files list.
(should_stack_file): Mark #import-ed files once-only, and
don't stack them if the file has already been stacked.
(_cp_mark_file_once_only): Simplify.
* cpphash.h (struct cpp_reader): Rename once_only_files
to all_files. Rename saw_pragma_once to seen_once_only.
(_cpp_mark_file_once_only): Update prototype.
* cpplib.c (do_pragma_once): Update.
2003-08-02 Neil Booth <neil@daikokuya.co.uk>
* cppfiles.c (ENOTDIR): Remove. * cppfiles.c (ENOTDIR): Remove.
(open_file_in_dir): Rename find_file_in_dir. Handle errors (open_file_in_dir): Rename find_file_in_dir. Handle errors
other than ENOENT here. other than ENOENT here.
......
...@@ -70,8 +70,8 @@ struct _cpp_file ...@@ -70,8 +70,8 @@ struct _cpp_file
been calculated yet. */ been calculated yet. */
const char *dir_name; const char *dir_name;
/* Chain through #import-ed files or those containing #pragma once. */ /* Chain through all files. */
struct _cpp_file *once_only_next; struct _cpp_file *next_file;
/* The contents of NAME after calling read_file(). */ /* The contents of NAME after calling read_file(). */
const uchar *buffer; const uchar *buffer;
...@@ -97,11 +97,8 @@ struct _cpp_file ...@@ -97,11 +97,8 @@ struct _cpp_file
/* Number of times the file has been stacked for preprocessing. */ /* Number of times the file has been stacked for preprocessing. */
unsigned short stack_count; unsigned short stack_count;
/* If opened with #import. */ /* If opened with #import or contains #pragma once. */
bool import; bool once_only;
/* If contains #pragma once. */
bool pragma_once;
/* If read() failed before. */ /* If read() failed before. */
bool dont_read; bool dont_read;
...@@ -383,13 +380,21 @@ find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool fake) ...@@ -383,13 +380,21 @@ find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool fake)
entry = search_cache (*hash_slot, file->dir); entry = search_cache (*hash_slot, file->dir);
if (entry) if (entry)
{ break;
/* Cache for START_DIR too, sharing the _cpp_file structure. */ }
free ((char *) file->name);
free (file); if (entry)
file = entry->u.file; {
break; /* Cache for START_DIR too, sharing the _cpp_file structure. */
} free ((char *) file->name);
free (file);
file = entry->u.file;
}
else
{
/* This is a new file; put it in the list. */
file->next_file = pfile->all_files;
pfile->all_files = file;
} }
/* Store this new result in the hash table. */ /* Store this new result in the hash table. */
...@@ -520,6 +525,22 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) ...@@ -520,6 +525,22 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
{ {
_cpp_file *f; _cpp_file *f;
/* Skip once-only files. */
if (file->once_only)
return false;
/* We must mark the file once-only if #import now, before header
guard checks. Otherwise, undefining the header guard might
cause the file to be re-stacked. */
if (import)
{
_cpp_mark_file_once_only (pfile, file);
/* Don't stack files that have been stacked before. */
if (file->stack_count)
return false;
}
/* Skip if the file had a header guard and the macro is defined. /* Skip if the file had a header guard and the macro is defined.
PCH relies on this appearing before the PCH handler below. */ PCH relies on this appearing before the PCH handler below. */
if (file->cmacro && file->cmacro->type == NT_MACRO) if (file->cmacro && file->cmacro->type == NT_MACRO)
...@@ -534,34 +555,23 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) ...@@ -534,34 +555,23 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
return false; return false;
} }
/* Did this file contain #pragma once? */
if (file->pragma_once)
return false;
/* Are we #import-ing a previously #import-ed file? */
if (import && file->import)
return false;
/* Read the file's contents. */
if (!read_file (pfile, file)) if (!read_file (pfile, file))
return false; return false;
/* Nothing to check if this isn't #import and there haven't been any /* Now we've read the file's contents, we can stack it if there
#pragma_once directives. */ are no once-only files. */
if (!import && !pfile->saw_pragma_once) if (!pfile->seen_once_only)
return true; return true;
/* We may have #imported it under a different name, though. Look /* We may have read the file under a different name. Look
for likely candidates and compare file contents to be sure. */ for likely candidates and compare file contents to be sure. */
for (f = pfile->once_only_files; f; f = f->once_only_next) for (f = pfile->all_files; f; f = f->next_file)
{ {
if (f == file) if (f == file)
continue; continue;
if (!f->pragma_once && !(f->import && import)) if ((import || f->once_only)
continue; && f->err_no == 0
if (f->err_no == 0
&& f->st.st_mtime == file->st.st_mtime && f->st.st_mtime == file->st.st_mtime
&& f->st.st_size == file->st.st_size && f->st.st_size == file->st.st_size
&& read_file (pfile, f) && read_file (pfile, f)
...@@ -571,9 +581,6 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) ...@@ -571,9 +581,6 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
break; break;
} }
if (import || f != NULL)
_cpp_mark_file_once_only (pfile, file, import);
return f == NULL; return f == NULL;
} }
...@@ -619,27 +626,12 @@ stack_file (cpp_reader *pfile, _cpp_file *file, bool import) ...@@ -619,27 +626,12 @@ stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
return true; return true;
} }
/* Mark FILE to be included once only. IMPORT is true if because of /* Mark FILE to be included once only. */
#import, otherwise it is assumed to be #pragma once. */
void void
_cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file, bool import) _cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file)
{ {
/* Put it on the once-only list if it's not on there already (an pfile->seen_once_only = true;
earlier #include with a #pragma once might have put it on there file->once_only = true;
already). */
if (!file->import && !file->pragma_once)
{
file->once_only_next = pfile->once_only_files;
pfile->once_only_files = file;
}
if (import)
file->import = true;
else
{
pfile->saw_pragma_once = true;
file->pragma_once = true;
}
} }
/* Return the directory from which searching for FNAME should start, /* Return the directory from which searching for FNAME should start,
......
...@@ -359,8 +359,8 @@ struct cpp_reader ...@@ -359,8 +359,8 @@ struct cpp_reader
struct cpp_dir *bracket_include; /* <> */ struct cpp_dir *bracket_include; /* <> */
struct cpp_dir no_search_path; /* No path. */ struct cpp_dir no_search_path; /* No path. */
/* Chain of files that were #import-ed or contain #pragma once. */ /* Chain of all hashed _cpp_file instances. */
struct _cpp_file *once_only_files; struct _cpp_file *all_files;
/* File and directory hash table. */ /* File and directory hash table. */
struct htab *file_hash; struct htab *file_hash;
...@@ -371,8 +371,9 @@ struct cpp_reader ...@@ -371,8 +371,9 @@ struct cpp_reader
directory. */ directory. */
bool quote_ignores_source_dir; bool quote_ignores_source_dir;
/* Non-zero if any file has contained #pragma once. */ /* Non-zero if any file has contained #pragma once or #import has
bool saw_pragma_once; been used. */
bool seen_once_only;
/* Multiple include optimization. */ /* Multiple include optimization. */
const cpp_hashnode *mi_cmacro; const cpp_hashnode *mi_cmacro;
...@@ -514,7 +515,7 @@ extern void _cpp_init_hashtable (cpp_reader *, hash_table *); ...@@ -514,7 +515,7 @@ extern void _cpp_init_hashtable (cpp_reader *, hash_table *);
extern void _cpp_destroy_hashtable (cpp_reader *); extern void _cpp_destroy_hashtable (cpp_reader *);
/* In cppfiles.c */ /* In cppfiles.c */
extern void _cpp_mark_file_once_only (cpp_reader *, struct _cpp_file *, bool); extern void _cpp_mark_file_once_only (cpp_reader *, struct _cpp_file *);
extern void _cpp_fake_include (cpp_reader *, const char *); extern void _cpp_fake_include (cpp_reader *, const char *);
extern bool _cpp_stack_file (cpp_reader *, const char *); extern bool _cpp_stack_file (cpp_reader *, const char *);
extern bool _cpp_stack_include (cpp_reader *, const char *, int, extern bool _cpp_stack_include (cpp_reader *, const char *, int,
......
...@@ -1167,7 +1167,7 @@ do_pragma_once (cpp_reader *pfile) ...@@ -1167,7 +1167,7 @@ do_pragma_once (cpp_reader *pfile)
cpp_error (pfile, DL_WARNING, "#pragma once in main file"); cpp_error (pfile, DL_WARNING, "#pragma once in main file");
check_eol (pfile); check_eol (pfile);
_cpp_mark_file_once_only (pfile, pfile->buffer->file, false); _cpp_mark_file_once_only (pfile, pfile->buffer->file);
} }
/* Handle #pragma GCC poison, to poison one or more identifiers so /* Handle #pragma GCC poison, to poison one or more identifiers so
......
2003-08-02 Neil Booth <neil@daikokuya.co.uk>
* import1.c, import2.c: New tests.
2003-08-01 Jakub Jelinek <jakub@redhat.com> 2003-08-01 Jakub Jelinek <jakub@redhat.com>
* g++.dg/eh/crossjump1.C: New test. * g++.dg/eh/crossjump1.C: New test.
......
/* Copyright (C) 2003 Free Software Foundation, Inc. */
/* { dg-do preprocess } */
/* { dg-options "" } */
/* This tests that our eagerness to apply the multiple include guard
optimization to the #import doesn't stop us marking the file
once-only.
Neil Booth, 2 August 2003. */
#include "import1.h"
#import "import1.h"
#undef IMPORT1_H
#define BUG
#include "import1.h"
#ifndef IMPORT1_H
#define IMPORT1_H
#ifdef BUG
#error Should not happen
#endif
#endif
/* Copyright (C) 2003 Free Software Foundation, Inc. */
/* { dg-do preprocess } */
/* { dg-options "" } */
/* This tests that the file is only included once
Neil Booth, 2 August 2003. */
#include "import2.h"
#import "import2.h"
#include "import2.h"
#ifdef BUG
#error Should not happen!
#endif
#define BUG
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment