return file->path;
}
+/* Retrofit the just-entered main file asif it was an include. This
+ will permit correct include_next use, and mark it as a system
+ header if that's where it resides. We use filesystem-appropriate
+ prefix matching of the include path to locate the main file. */
+void
+cpp_retrofit_as_include (cpp_reader *pfile)
+{
+ /* We should be the outermost. */
+ gcc_assert (!pfile->buffer->prev);
+
+ if (const char *name = pfile->main_file->name)
+ {
+ /* Locate name on the include dir path, using a prefix match. */
+ size_t name_len = strlen (name);
+ for (cpp_dir *dir = pfile->quote_include; dir; dir = dir->next)
+ if (dir->len < name_len
+ && IS_DIR_SEPARATOR (name[dir->len])
+ && !filename_ncmp (name, dir->name, dir->len))
+ {
+ pfile->main_file->dir = dir;
+ if (dir->sysp)
+ cpp_make_system_header (pfile, 1, 0);
+ break;
+ }
+ }
+
+ /* Initialize controlling macro state. */
+ pfile->mi_valid = true;
+ pfile->mi_cmacro = 0;
+}
+
/* Could not open FILE. The complication is dependency output. */
static void
open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets,
normalized_none
};
+enum cpp_main_search
+{
+ CMS_none, /* A regular source file. */
+ CMS_header, /* Is a directly-specified header file (eg PCH or
+ header-unit). */
+ CMS_user, /* Search the user INCLUDE path. */
+ CMS_system, /* Search the system INCLUDE path. */
+};
+
/* This structure is nested inside struct cpp_reader, and
carries all the options visible to the command line. */
struct cpp_options
/* The maximum depth of the nested #include. */
unsigned int max_include_depth;
+
+ cpp_main_search main_search : 8;
};
/* Diagnostic levels. To get a diagnostic without associating a
too. If there was an error opening the file, it returns NULL. */
extern const char *cpp_read_main_file (cpp_reader *, const char *,
bool injecting = false);
+extern location_t cpp_main_loc (const cpp_reader *);
+
+/* Adjust for the main file to be an include. */
+extern void cpp_retrofit_as_include (cpp_reader *);
/* Set up built-ins with special behavior. Use cpp_init_builtins()
instead unless your know what you are doing. */
deps_add_default_target (deps, fname);
pfile->main_file
- = _cpp_find_file (pfile, fname, &pfile->no_search_path, /*angle=*/0,
- _cpp_FFK_NORMAL, 0);
+ = _cpp_find_file (pfile, fname,
+ CPP_OPTION (pfile, preprocessed) ? &pfile->no_search_path
+ : CPP_OPTION (pfile, main_search) == CMS_user
+ ? pfile->quote_include
+ : CPP_OPTION (pfile, main_search) == CMS_system
+ ? pfile->bracket_include : &pfile->no_search_path,
+ /*angle=*/0, _cpp_FFK_NORMAL, 0);
+
if (_cpp_find_failed (pfile->main_file))
return NULL;
LINEMAP_LINE (last), LINEMAP_SYSP (last));
}
- return ORDINARY_MAP_FILE_NAME (LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table));
+ auto *map = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table);
+ pfile->main_loc = MAP_START_LOCATION (map);
+
+ return ORDINARY_MAP_FILE_NAME (map);
+}
+
+location_t
+cpp_main_loc (const cpp_reader *pfile)
+{
+ return pfile->main_loc;
}
/* For preprocessed files, if the very first characters are
/* If non-zero, the lexer will use this location for the next token
instead of getting a location from the linemap. */
location_t forced_token_location;
+
+ /* Location identifying the main source file -- intended to be line
+ zero of said file. */
+ location_t main_loc;
};
/* Character classes. Based on the more primitive macros in safe-ctype.h.
inline int
_cpp_in_main_source_file (cpp_reader *pfile)
{
- return pfile->buffer->file == pfile->main_file;
+ return (!CPP_OPTION (pfile, main_search)
+ && pfile->buffer->file == pfile->main_file);
}
/* True if NODE is a macro for the purposes of ifdef, defined etc. */