diff options
author | Marek Polacek <polacek@redhat.com> | 2022-01-19 19:05:22 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2022-01-24 17:48:23 -0500 |
commit | ae36f839632ddb67a53c26e9c7e73b0f56c4c11b (patch) | |
tree | 79e2972dff52b095ab4272a276dd1042aa6eb23e /libcpp | |
parent | e89d0befe3ec3238fca6de2cb078eb403b8c7e99 (diff) | |
download | gcc-ae36f839632ddb67a53c26e9c7e73b0f56c4c11b.zip gcc-ae36f839632ddb67a53c26e9c7e73b0f56c4c11b.tar.gz gcc-ae36f839632ddb67a53c26e9c7e73b0f56c4c11b.tar.bz2 |
preprocessor: -Wbidi-chars and UCNs [PR104030]
Stephan Bergmann reported that our -Wbidi-chars breaks the build
of LibreOffice because we warn about UCNs even when their usage
is correct: LibreOffice constructs strings piecewise, as in:
aText = u"\u202D" + aText;
and warning about that is overzealous. Since no editor (AFAIK)
interprets UCNs to show them as Unicode characters, there's less
risk in misinterpreting them, and so perhaps we shouldn't warn
about them by default. However, identifiers containing UCNs or
programs generating other programs could still cause confusion,
so I'm keeping the UCN checking. To turn it on, you just need
to use -Wbidi-chars=unpaired,ucn or -Wbidi-chars=any,ucn.
The implementation is done by using the new EnumSet feature.
PR preprocessor/104030
gcc/c-family/ChangeLog:
* c.opt (Wbidi-chars): Mark as EnumSet. Also accept =ucn.
gcc/ChangeLog:
* doc/invoke.texi: Update documentation for -Wbidi-chars.
libcpp/ChangeLog:
* include/cpplib.h (enum cpp_bidirectional_level): Add
bidirectional_ucn. Set values explicitly.
* internal.h (cpp_reader): Adjust warn_bidi_p.
* lex.cc (maybe_warn_bidi_on_close): Don't warn about UCNs
unless UCN checking is on.
(maybe_warn_bidi_on_char): Likewise.
gcc/testsuite/ChangeLog:
* c-c++-common/Wbidi-chars-10.c: Turn on UCN checking.
* c-c++-common/Wbidi-chars-11.c: Likewise.
* c-c++-common/Wbidi-chars-14.c: Likewise.
* c-c++-common/Wbidi-chars-16.c: Likewise.
* c-c++-common/Wbidi-chars-17.c: Likewise.
* c-c++-common/Wbidi-chars-4.c: Likewise.
* c-c++-common/Wbidi-chars-5.c: Likewise.
* c-c++-common/Wbidi-chars-6.c: Likewise.
* c-c++-common/Wbidi-chars-7.c: Likewise.
* c-c++-common/Wbidi-chars-8.c: Likewise.
* c-c++-common/Wbidi-chars-9.c: Likewise.
* c-c++-common/Wbidi-chars-ranges.c: Likewise.
* c-c++-common/Wbidi-chars-18.c: New test.
* c-c++-common/Wbidi-chars-19.c: New test.
* c-c++-common/Wbidi-chars-20.c: New test.
* c-c++-common/Wbidi-chars-21.c: New test.
* c-c++-common/Wbidi-chars-22.c: New test.
* c-c++-common/Wbidi-chars-23.c: New test.
Diffstat (limited to 'libcpp')
-rw-r--r-- | libcpp/include/cpplib.h | 11 | ||||
-rw-r--r-- | libcpp/internal.h | 3 | ||||
-rw-r--r-- | libcpp/lex.cc | 16 |
3 files changed, 18 insertions, 12 deletions
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 940c79f..3eba6f7 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -319,15 +319,16 @@ enum cpp_main_search CMS_system, /* Search the system INCLUDE path. */ }; -/* The possible bidirectional control characters checking levels, from least - restrictive to most. */ +/* The possible bidirectional control characters checking levels. */ enum cpp_bidirectional_level { /* No checking. */ - bidirectional_none, + bidirectional_none = 0, /* Only detect unpaired uses of bidirectional control characters. */ - bidirectional_unpaired, + bidirectional_unpaired = 1, /* Detect any use of bidirectional control characters. */ - bidirectional_any + bidirectional_any = 2, + /* Also warn about UCNs. */ + bidirectional_ucn = 4 }; /* This structure is nested inside struct cpp_reader, and diff --git a/libcpp/internal.h b/libcpp/internal.h index 364c41c..badfd1b 100644 --- a/libcpp/internal.h +++ b/libcpp/internal.h @@ -605,7 +605,8 @@ struct cpp_reader characters. */ bool warn_bidi_p () const { - return CPP_OPTION (this, cpp_warn_bidirectional) != bidirectional_none; + return (CPP_OPTION (this, cpp_warn_bidirectional) + & (bidirectional_unpaired|bidirectional_any)); } }; diff --git a/libcpp/lex.cc b/libcpp/lex.cc index 4d73657..fb1dfab 100644 --- a/libcpp/lex.cc +++ b/libcpp/lex.cc @@ -1560,8 +1560,11 @@ class unpaired_bidi_rich_location : public rich_location static void maybe_warn_bidi_on_close (cpp_reader *pfile, const uchar *p) { - if (CPP_OPTION (pfile, cpp_warn_bidirectional) == bidirectional_unpaired - && bidi::vec.count () > 0) + const auto warn_bidi = CPP_OPTION (pfile, cpp_warn_bidirectional); + if (bidi::vec.count () > 0 + && (warn_bidi & bidirectional_unpaired + && (!bidi::current_ctx_ucn_p () + || (warn_bidi & bidirectional_ucn)))) { const location_t loc = linemap_position_for_column (pfile->line_table, @@ -1597,7 +1600,7 @@ maybe_warn_bidi_on_char (cpp_reader *pfile, bidi::kind kind, const auto warn_bidi = CPP_OPTION (pfile, cpp_warn_bidirectional); - if (warn_bidi != bidirectional_none) + if (warn_bidi & (bidirectional_unpaired|bidirectional_any)) { rich_location rich_loc (pfile->line_table, loc); rich_loc.set_escape_on_output (true); @@ -1605,10 +1608,10 @@ maybe_warn_bidi_on_char (cpp_reader *pfile, bidi::kind kind, /* It seems excessive to warn about a PDI/PDF that is closing an opened context because we've already warned about the opening character. Except warn when we have a UCN x UTF-8 - mismatch. */ + mismatch, if UCN checking is enabled. */ if (kind == bidi::current_ctx ()) { - if (warn_bidi == bidirectional_unpaired + if (warn_bidi == (bidirectional_unpaired|bidirectional_ucn) && bidi::current_ctx_ucn_p () != ucn_p) { rich_loc.add_range (bidi::current_ctx_loc ()); @@ -1617,7 +1620,8 @@ maybe_warn_bidi_on_char (cpp_reader *pfile, bidi::kind kind, "a context by \"%s\"", bidi::to_str (kind)); } } - else if (warn_bidi == bidirectional_any) + else if (warn_bidi & bidirectional_any + && (!ucn_p || (warn_bidi & bidirectional_ucn))) { if (kind == bidi::kind::PDF || kind == bidi::kind::PDI) cpp_warning_at (pfile, CPP_W_BIDIRECTIONAL, &rich_loc, |