/* Character scanner.
- Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
- Free Software Foundation, Inc.
+ Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
+ 2010 Free Software Foundation, Inc.
Contributed by Andy Vaught
This file is part of GCC.
#include "config.h"
#include "system.h"
#include "gfortran.h"
-#include "toplev.h"
+#include "toplev.h" /* For set_src_pwd. */
#include "debug.h"
#include "flags.h"
#include "cpp.h"
static gfc_file *file_head, *current_file;
-static int continue_flag, end_flag, openmp_flag;
+static int continue_flag, end_flag, openmp_flag, gcc_attribute_flag;
static int continue_count, continue_line;
static locus openmp_locus;
+static locus gcc_attribute_locus;
gfc_source_form gfc_current_form;
static gfc_linebuf *line_head, *line_tail;
static FILE *gfc_src_file;
static gfc_char_t *gfc_src_preprocessor_lines[2];
-extern int pedantic;
-
static struct gfc_file_change
{
const char *filename;
static void
add_path_to_list (gfc_directorylist **list, const char *path,
- bool use_for_modules)
+ bool use_for_modules, bool head)
{
gfc_directorylist *dir;
const char *p;
if (*p++ == '\0')
return;
- dir = *list;
- if (!dir)
- dir = *list = XCNEW (gfc_directorylist);
+ if (head || *list == NULL)
+ {
+ dir = XCNEW (gfc_directorylist);
+ if (!head)
+ *list = dir;
+ }
else
{
+ dir = *list;
while (dir->next)
dir = dir->next;
dir = dir->next;
}
- dir->next = NULL;
+ dir->next = head ? *list : NULL;
+ if (head)
+ *list = dir;
dir->use_for_modules = use_for_modules;
dir->path = XCNEWVEC (char, strlen (p) + 2);
strcpy (dir->path, p);
void
-gfc_add_include_path (const char *path, bool use_for_modules)
+gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir)
{
- add_path_to_list (&include_dirs, path, use_for_modules);
- gfc_cpp_add_include_path (xstrdup(path), true);
+ add_path_to_list (&include_dirs, path, use_for_modules, file_dir);
+
+ /* For '#include "..."' these directories are automatically searched. */
+ if (!file_dir)
+ gfc_cpp_add_include_path (xstrdup(path), true);
}
void
gfc_add_intrinsic_modules_path (const char *path)
{
- add_path_to_list (&intrinsic_modules_dirs, path, true);
+ add_path_to_list (&intrinsic_modules_dirs, path, true, false);
}
static FILE *
-open_included_file (const char *name, gfc_directorylist *list, bool module)
+open_included_file (const char *name, gfc_directorylist *list,
+ bool module, bool system)
{
char *fullname;
gfc_directorylist *p;
f = gfc_open_file (fullname);
if (f != NULL)
- return f;
+ {
+ if (gfc_cpp_makedep ())
+ gfc_cpp_add_dep (fullname, system);
+
+ return f;
+ }
}
return NULL;
FILE *
gfc_open_included_file (const char *name, bool include_cwd, bool module)
{
- FILE *f;
+ FILE *f = NULL;
- if (IS_ABSOLUTE_PATH (name))
- return gfc_open_file (name);
-
- if (include_cwd)
+ if (IS_ABSOLUTE_PATH (name) || include_cwd)
{
f = gfc_open_file (name);
- if (f != NULL)
- return f;
+ if (f && gfc_cpp_makedep ())
+ gfc_cpp_add_dep (name, false);
}
- return open_included_file (name, include_dirs, module);
+ if (!f)
+ f = open_included_file (name, include_dirs, module, false);
+
+ return f;
}
FILE *
gfc_open_intrinsic_module (const char *name)
{
+ FILE *f = NULL;
+
if (IS_ABSOLUTE_PATH (name))
- return gfc_open_file (name);
+ {
+ f = gfc_open_file (name);
+ if (f && gfc_cpp_makedep ())
+ gfc_cpp_add_dep (name, true);
+ }
- return open_included_file (name, intrinsic_modules_dirs, true);
+ if (!f)
+ f = open_included_file (name, intrinsic_modules_dirs, true, true);
+
+ return f;
}
/* Skip a comment. When we come here the parse pointer is positioned
immediately after the comment character. If we ever implement
- compiler directives withing comments, here is where we parse the
+ compiler directives within comments, here is where we parse the
directive. */
static void
}
+/* Return true if GCC$ was matched. */
+static bool
+skip_gcc_attribute (locus start)
+{
+ bool r = false;
+ char c;
+ locus old_loc = gfc_current_locus;
+
+ if ((c = next_char ()) == 'g' || c == 'G')
+ if ((c = next_char ()) == 'c' || c == 'C')
+ if ((c = next_char ()) == 'c' || c == 'C')
+ if ((c = next_char ()) == '$')
+ r = true;
+
+ if (r == false)
+ gfc_current_locus = old_loc;
+ else
+ {
+ gcc_attribute_flag = 1;
+ gcc_attribute_locus = old_loc;
+ gfc_current_locus = start;
+ }
+
+ return r;
+}
+
+
+
/* Comment lines are null lines, lines containing only blanks or lines
on which the first nonblank line is a '!'.
Return true if !$ openmp conditional compilation sentinel was
if (c == '!')
{
+ /* Keep the !GCC$ line. */
+ if (at_bol && skip_gcc_attribute (start))
+ return false;
+
/* If -fopenmp, we need to handle here 2 things:
1) don't treat !$omp as comments, but directives
2) handle OpenMP conditional compilation, where
!$ should be treated as 2 spaces (for initial lines
only if followed by space). */
- if (gfc_option.flag_openmp && at_bol)
+ if (gfc_option.gfc_flag_openmp && at_bol)
{
locus old_loc = gfc_current_locus;
if (next_char () == '$')
if (openmp_flag && at_bol)
openmp_flag = 0;
+
+ gcc_attribute_flag = 0;
gfc_current_locus = start;
return false;
}
if (c == '!' || c == 'c' || c == 'C' || c == '*')
{
+ if (skip_gcc_attribute (start))
+ {
+ /* Canonicalize to *$omp. */
+ *start.nextc = '*';
+ return;
+ }
+
/* If -fopenmp, we need to handle here 2 things:
1) don't treat !$omp|c$omp|*$omp as comments, but directives
2) handle OpenMP conditional compilation, where
&& continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
- if (gfc_option.flag_openmp)
+ if (gfc_option.gfc_flag_openmp)
{
if (next_char () == '$')
{
}
openmp_flag = 0;
+ gcc_attribute_flag = 0;
gfc_current_locus = start;
}
context or not. */
gfc_char_t
-gfc_next_char_literal (int in_string)
+gfc_next_char_literal (gfc_instring in_string)
{
locus old_loc;
int i, prev_openmp_flag;
if (!in_string && c == '!')
{
+ if (gcc_attribute_flag
+ && memcmp (&gfc_current_locus, &gcc_attribute_locus,
+ sizeof (gfc_current_locus)) == 0)
+ goto done;
+
if (openmp_flag
&& memcmp (&gfc_current_locus, &openmp_locus,
sizeof (gfc_current_locus)) == 0)
goto done;
}
+ /* Check to see if the continuation line was truncated. */
+ if (gfc_option.warn_line_truncation && gfc_current_locus.lb != NULL
+ && gfc_current_locus.lb->truncated)
+ {
+ int maxlen = gfc_option.free_line_length;
+ gfc_current_locus.lb->truncated = 0;
+ gfc_current_locus.nextc += maxlen;
+ gfc_warning_now ("Line truncated at %L", &gfc_current_locus);
+ gfc_current_locus.nextc -= maxlen;
+ }
+
if (c != '&')
goto done;
{
if (in_string)
{
- if (gfc_option.warn_ampersand)
- gfc_warning_now ("Missing '&' in continued character "
- "constant at %C");
gfc_current_locus.nextc--;
+ if (gfc_option.warn_ampersand && in_string == INSTRING_WARN)
+ gfc_warning ("Missing '&' in continued character "
+ "constant at %C");
}
/* Both !$omp and !$ -fopenmp continuation lines have & on the
continuation line only optionally. */
}
}
}
- else
+ else /* Fixed form. */
{
/* Fixed form continuation. */
if (!in_string && c == '!')
if (c != '\n')
goto done;
+ /* Check to see if the continuation line was truncated. */
+ if (gfc_option.warn_line_truncation && gfc_current_locus.lb != NULL
+ && gfc_current_locus.lb->truncated)
+ {
+ gfc_current_locus.lb->truncated = 0;
+ gfc_warning_now ("Line truncated at %L", &gfc_current_locus);
+ }
+
prev_openmp_flag = openmp_flag;
continue_flag = 1;
old_loc = gfc_current_locus;
do
{
- c = gfc_next_char_literal (0);
+ c = gfc_next_char_literal (NONSTRING);
}
while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
do
{
old_loc = gfc_current_locus;
- c = gfc_next_char_literal (0);
+ c = gfc_next_char_literal (NONSTRING);
/* Issue a warning for nonconforming tabs. We keep track of the line
number because the Fortran matchers will often back up and the same
line will be scanned multiple times. */
static int linenum = 0, current_line = 1;
int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
int trunc_flag = 0, seen_comment = 0;
- int seen_printable = 0, seen_ampersand = 0;
+ int seen_printable = 0, seen_ampersand = 0, quoted = ' ';
gfc_char_t *buffer;
bool found_tab = false;
if (c == '&')
{
if (seen_ampersand)
- seen_ampersand = 0;
+ {
+ seen_ampersand = 0;
+ seen_printable = 1;
+ }
else
seen_ampersand = 1;
}
&& (c == '*' || c == 'c' || c == 'd'))
seen_comment = 1;
+ if (quoted == ' ')
+ {
+ if (c == '\'' || c == '"')
+ quoted = c;
+ }
+ else if (c == quoted)
+ quoted = ' ';
+
+ /* Is this a free-form comment? */
+ if (c == '!' && quoted == ' ')
+ seen_comment = 1;
+
/* Vendor extension: "<tab>1" marks a continuation line. */
if (found_tab)
{
}
else if (i >= maxlen)
{
+ bool trunc_warn = true;
+
+ /* Enhancement, if the very next non-space character is an ampersand
+ or comment that we would otherwise warn about, don't mark as
+ truncated. */
+
/* Truncate the rest of the line. */
for (;;)
{
c = getc (input);
+ if (c == '\r' || c == ' ')
+ continue;
+
if (c == '\n' || c == EOF)
break;
- trunc_flag = 1;
+ if (!trunc_warn && c != '!')
+ trunc_warn = true;
+
+ if (trunc_warn && ((gfc_current_form == FORM_FIXED && c == '&')
+ || c == '!'))
+ trunc_warn = false;
+
+ if (c == '!')
+ seen_comment = 1;
+
+ if (trunc_warn && !seen_comment)
+ trunc_flag = 1;
}
c = '\n';
if (strcmp (current_file->filename, filename) != 0)
{
- gfc_free (current_file->filename);
+ /* FIXME: we leak the old filename because a pointer to it may be stored
+ in the linemap. Alternative could be using GC or updating linemap to
+ point to the new name, but there is no API for that currently. */
current_file->filename = xstrdup (filename);
}
}
-static try load_file (const char *, bool);
+static gfc_try load_file (const char *, const char *, bool);
/* include_line()-- Checks a line buffer to see if it is an include
line. If so, we call load_file() recursively to load the included
c = line;
- if (gfc_option.flag_openmp)
+ if (gfc_option.gfc_flag_openmp)
{
if (gfc_current_form == FORM_FREE)
{
read by anything else. */
filename = gfc_widechar_to_char (begin, -1);
- load_file (filename, false);
+ if (load_file (filename, NULL, false) == FAILURE)
+ exit (1);
+
gfc_free (filename);
return true;
}
/* Load a file into memory by calling load_line until the file ends. */
-static try
-load_file (const char *filename, bool initial)
+static gfc_try
+load_file (const char *realfilename, const char *displayedname, bool initial)
{
gfc_char_t *line;
gfc_linebuf *b;
FILE *input;
int len, line_len;
bool first_line;
+ const char *filename;
+
+ filename = displayedname ? displayedname : realfilename;
for (f = current_file; f; f = f->up)
if (strcmp (filename, f->filename) == 0)
{
- gfc_error_now ("File '%s' is being included recursively", filename);
+ fprintf (stderr, "%s:%d: Error: File '%s' is being included "
+ "recursively\n", current_file->filename, current_file->line,
+ filename);
return FAILURE;
}
gfc_src_file = NULL;
}
else
- input = gfc_open_file (filename);
+ input = gfc_open_file (realfilename);
if (input == NULL)
{
gfc_error_now ("Can't open file '%s'", filename);
}
else
{
- input = gfc_open_included_file (filename, false, false);
+ input = gfc_open_included_file (realfilename, false, false);
if (input == NULL)
{
- gfc_error_now ("Can't open included file '%s'", filename);
+ fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n",
+ current_file->filename, current_file->line, filename);
return FAILURE;
}
}
&& line[2] == (unsigned char) '\xBF')))
{
int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
- gfc_char_t *new = gfc_get_wide_string (line_len);
+ gfc_char_t *new_char = gfc_get_wide_string (line_len);
- wide_strcpy (new, &line[n]);
+ wide_strcpy (new_char, &line[n]);
gfc_free (line);
- line = new;
+ line = new_char;
len -= n;
}
/* Open a new file and start scanning from that file. Returns SUCCESS
- if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
+ if everything went OK, FAILURE otherwise. If form == FORM_UNKNOWN
it tries to determine the source form from the filename, defaulting
to free form. */
-try
+gfc_try
gfc_new_file (void)
{
- try result;
+ gfc_try result;
if (gfc_cpp_enabled ())
{
result = gfc_cpp_preprocess (gfc_source_file);
if (!gfc_cpp_preprocess_only ())
- result = load_file (gfc_cpp_temporary_file (), true);
+ result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
}
else
- result = load_file (gfc_source_file, true);
+ result = load_file (gfc_source_file, NULL, true);
gfc_current_locus.lb = line_head;
gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;