Skip to content

Commit

Permalink
Feature: Macros for identifying the wide and narrow execution string …
Browse files Browse the repository at this point in the history
…literal encoding

gcc/c-family
	* c-cppbuiltin.c (c_cpp_builtins): Add predefined
	{__GNUC_EXECUTION_CHARSET_NAME} and
	_WIDE_EXECUTION_CHARSET_NAME} macros.

gcc/
	* doc/cpp.texi: Document new macros.

gcc/testsuite/
	* c-c++-common/cpp/wide-narrow-predef-macros.c: New test.

libcpp/
	* charset.c (init_iconv_desc): Initialize "to" and "from" fields.
	* directives.c (cpp_get_narrow_charset_name): New function.
	(cpp_get_wide_charset_name): Likewise.
	* include/cpplib.h (cpp_get_narrow_charset_name): Prototype.
	(cpp_get_wide_charset_name): Likewise.
	* internal.h (cset_converter): Add "to" and "from" fields.
  • Loading branch information
ThePhD authored and Jeff Law committed Dec 1, 2020
1 parent 39836f8 commit eccec86
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
7 changes: 7 additions & 0 deletions gcc/c-family/c-cppbuiltin.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,13 @@ c_cpp_builtins (cpp_reader *pfile)

define_language_independent_builtin_macros (pfile);

/* encoding definitions used by users and libraries */
builtin_define_with_value ("__GNUC_EXECUTION_CHARSET_NAME",
cpp_get_narrow_charset_name (pfile), 1);
builtin_define_with_value ("__GNUC_WIDE_EXECUTION_CHARSET_NAME",
cpp_get_wide_charset_name (pfile), 1);


if (c_dialect_cxx ())
{
int major;
Expand Down
9 changes: 9 additions & 0 deletions gcc/doc/cpp.texi
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,15 @@ features are supported by GCC.
@item __NO_MATH_ERRNO__
This macro is defined if @option{-fno-math-errno} is used, or enabled
by another option such as @option{-ffast-math} or by default.

@item __GNUC_EXECUTION_CHARSET_NAME
@itemx __GNUC_WIDE_EXECUTION_CHARSET_NAME
These macros are defined to expand to a narrow string literal of
the name of the narrow and wide compile-time execution character
set used. It directly reflects the name passed to the options
@option{-fexec-charset} and @option{-fwide-exec-charset}, or the defaults
documented for those options (that is, it can expand to something like
@code{"UTF-8"}). @xref{Invocation}.
@end table

@node System-specific Predefined Macros
Expand Down
13 changes: 13 additions & 0 deletions gcc/testsuite/c-c++-common/cpp/wide-narrow-predef-macros.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
{ dg-do compile }
*/

#if !defined(__GNUC_EXECUTION_CHARSET_NAME)
#error "Required implementation macro for comple-time charset name is not present"
#endif
#if !defined(__GNUC_WIDE_EXECUTION_CHARSET_NAME)
#error "Required implementation macro for wide comple-time charset name is not present"
#endif

const char narrow_name[] = __GNUC_EXECUTION_CHARSET_NAME;
const char wide_name[] = __GNUC_WIDE_EXECUTION_CHARSET_NAME;
3 changes: 3 additions & 0 deletions libcpp/charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,9 @@ init_iconv_desc (cpp_reader *pfile, const char *to, const char *from)
char *pair;
size_t i;

ret.to = to;
ret.from = from;

if (!strcasecmp (to, from))
{
ret.func = convert_no_conversion;
Expand Down
14 changes: 14 additions & 0 deletions libcpp/directives.c
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,20 @@ cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
pfile->cb = *cb;
}

/* The narrow character set identifier. */
const char *
cpp_get_narrow_charset_name (cpp_reader *pfile)
{
return pfile->narrow_cset_desc.to;
}

/* The wide character set identifier. */
const char *
cpp_get_wide_charset_name (cpp_reader *pfile)
{
return pfile->wide_cset_desc.to;
}

/* The dependencies structure. (Creates one if it hasn't already been.) */
class mkdeps *
cpp_get_deps (cpp_reader *pfile)
Expand Down
5 changes: 5 additions & 0 deletions libcpp/include/cpplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,11 @@ extern class mkdeps *cpp_get_deps (cpp_reader *) ATTRIBUTE_PURE;
extern const char *cpp_find_header_unit (cpp_reader *, const char *file,
bool angle_p, location_t);

/* Call these to get name data about the various compile-time
charsets. */
extern const char *cpp_get_narrow_charset_name (cpp_reader *) ATTRIBUTE_PURE;
extern const char *cpp_get_wide_charset_name (cpp_reader *) ATTRIBUTE_PURE;

/* This function reads the file, but does not start preprocessing. It
returns the name of the original file; this is the same as the
input file, except for preprocessed input. This will generate at
Expand Down
2 changes: 2 additions & 0 deletions libcpp/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ struct cset_converter
convert_f func;
iconv_t cd;
int width;
const char* from;
const char* to;
};

#define BITS_PER_CPPCHAR_T (CHAR_BIT * sizeof (cppchar_t))
Expand Down

0 comments on commit eccec86

Please sign in to comment.