--- /dev/null +++ b/libcpp/include/vls-primary-origin.h @@ -0,0 +1,9 @@ +/* Source origin for the private VLS GCC frontend. */ +#ifndef LIBCPP_VLS_PRIMARY_ORIGIN_H +#define LIBCPP_VLS_PRIMARY_ORIGIN_H + +struct cpp_reader; +/* Bytes skipped on the first physical line, or -1 for an unverified input. */ +extern int cpp_vls_primary_utf8_prefix (const cpp_reader *); + +#endif --- a/libcpp/files.cc +++ b/libcpp/files.cc @@ -23,6 +23,7 @@ #include "config.h" #include "system.h" #include "cpplib.h" +#include "vls-primary-origin.h" #include "internal.h" #include "mkdeps.h" #include "obstack.h" @@ -102,6 +103,10 @@ /* Number of times the file has been stacked for preprocessing. */ unsigned short stack_count; + + /* For the VLS observer, 0 means unknown. Otherwise this records one + plus the byte prefix consumed on line one of the actual UTF-8 input. */ + unsigned char vls_primary_utf8_prefix; /* If opened with #import or contains #pragma once. */ bool once_only : 1; @@ -796,12 +801,19 @@ cpp_error_at (pfile, CPP_DL_WARNING, loc, "%s is shorter than expected", file->path); + const bool record_origin = (pfile && file == pfile->main_file && input_charset + && !strcasecmp (input_charset, "UTF-8")); + const int prefix = record_origin ? cpp_check_utf8_bom ((const char *) buf, total) : 0; file->buffer = _cpp_convert_input (pfile, input_charset, buf, size + pad, total, &file->buffer_start, &file->st.st_size); file->buffer_valid = file->buffer; + if (record_origin && file->buffer_valid + && file->buffer - file->buffer_start == prefix + && file->st.st_size == (off_t) (total - prefix)) + file->vls_primary_utf8_prefix = 1 + prefix; return file->buffer_valid; } @@ -2360,6 +2372,16 @@ } } +/* The conversion result is retained after the preprocessor releases its + input buffer. Asking for an origin never reopens a mutable pathname. */ +int +cpp_vls_primary_utf8_prefix (const cpp_reader *pfile) +{ + if (!pfile || !pfile->main_file) + return -1; + return (int) pfile->main_file->vls_primary_utf8_prefix - 1; +} + /* Return the file name associated with FILE. */ const char * _cpp_get_file_name (_cpp_file *file) --- a/gcc/cp/vls-eval.cc +++ b/gcc/cp/vls-eval.cc @@ -4,6 +4,8 @@ #include "coretypes.h" #include "cp-tree.h" #include "vls-eval.h" +#include "c-family/c-pragma.h" +#include "vls-primary-origin.h" #include #include --- a/gcc/cp/vls-ide.inc +++ b/gcc/cp/vls-ide.inc @@ -87,7 +87,13 @@ if (!line || !column || SOURCE_LINE (map, last) != line || final < column || final == UINT_MAX || final - column + 1 != length) return false; - out = {line, column, final + 1}; + const int skipped = cpp_vls_primary_utf8_prefix (parse_in); + if (skipped < 0) + return false; + const unsigned prefix = line == 1 ? (unsigned) skipped : 0; + if (prefix > UINT_MAX - (final + 1)) + return false; + out = {line, column + prefix, final + 1 + prefix}; return true; }