Blame SOURCES/gcc11-Wbidi-chars.patch

a46658
commit 51c500269bf53749b107807d84271385fad35628
a46658
Author: Marek Polacek <polacek@redhat.com>
a46658
Date:   Wed Oct 6 14:33:59 2021 -0400
4c2713
a46658
    libcpp: Implement -Wbidi-chars for CVE-2021-42574 [PR103026]
a46658
    
a46658
    From a link below:
a46658
    "An issue was discovered in the Bidirectional Algorithm in the Unicode
a46658
    Specification through 14.0. It permits the visual reordering of
a46658
    characters via control sequences, which can be used to craft source code
a46658
    that renders different logic than the logical ordering of tokens
a46658
    ingested by compilers and interpreters. Adversaries can leverage this to
a46658
    encode source code for compilers accepting Unicode such that targeted
a46658
    vulnerabilities are introduced invisibly to human reviewers."
a46658
    
a46658
    More info:
a46658
    https://nvd.nist.gov/vuln/detail/CVE-2021-42574
a46658
    https://trojansource.codes/
a46658
    
a46658
    This is not a compiler bug.  However, to mitigate the problem, this patch
a46658
    implements -Wbidi-chars=[none|unpaired|any] to warn about possibly
a46658
    misleading Unicode bidirectional control characters the preprocessor may
a46658
    encounter.
a46658
    
a46658
    The default is =unpaired, which warns about improperly terminated
a46658
    bidirectional control characters; e.g. a LRE without its corresponding PDF.
a46658
    The level =any warns about any use of bidirectional control characters.
a46658
    
a46658
    This patch handles both UCNs and UTF-8 characters.  UCNs designating
a46658
    bidi characters in identifiers are accepted since r204886.  Then r217144
a46658
    enabled -fextended-identifiers by default.  Extended characters in C/C++
a46658
    identifiers have been accepted since r275979.  However, this patch still
a46658
    warns about mixing UTF-8 and UCN bidi characters; there seems to be no
a46658
    good reason to allow mixing them.
a46658
    
a46658
    We warn in different contexts: comments (both C and C++-style), string
a46658
    literals, character constants, and identifiers.  Expectedly, UCNs are ignored
a46658
    in comments and raw string literals.  The bidirectional control characters
a46658
    can nest so this patch handles that as well.
a46658
    
a46658
    I have not included nor tested this at all with Fortran (which also has
a46658
    string literals and line comments).
a46658
    
a46658
    Dave M. posted patches improving diagnostic involving Unicode characters.
a46658
    This patch does not make use of this new infrastructure yet.
a46658
    
a46658
            PR preprocessor/103026
a46658
    
a46658
    gcc/c-family/ChangeLog:
a46658
    
a46658
            * c.opt (Wbidi-chars, Wbidi-chars=): New option.
a46658
    
a46658
    gcc/ChangeLog:
a46658
    
a46658
            * doc/invoke.texi: Document -Wbidi-chars.
a46658
    
a46658
    libcpp/ChangeLog:
a46658
    
a46658
            * include/cpplib.h (enum cpp_bidirectional_level): New.
a46658
            (struct cpp_options): Add cpp_warn_bidirectional.
a46658
            (enum cpp_warning_reason): Add CPP_W_BIDIRECTIONAL.
a46658
            * internal.h (struct cpp_reader): Add warn_bidi_p member
a46658
            function.
a46658
            * init.c (cpp_create_reader): Set cpp_warn_bidirectional.
a46658
            * lex.c (bidi): New namespace.
a46658
            (get_bidi_utf8): New function.
a46658
            (get_bidi_ucn): Likewise.
a46658
            (maybe_warn_bidi_on_close): Likewise.
a46658
            (maybe_warn_bidi_on_char): Likewise.
a46658
            (_cpp_skip_block_comment): Implement warning about bidirectional
a46658
            control characters.
a46658
            (skip_line_comment): Likewise.
a46658
            (forms_identifier_p): Likewise.
a46658
            (lex_identifier): Likewise.
a46658
            (lex_string): Likewise.
a46658
            (lex_raw_string): Likewise.
a46658
    
a46658
    gcc/testsuite/ChangeLog:
a46658
    
a46658
            * c-c++-common/Wbidi-chars-1.c: New test.
a46658
            * c-c++-common/Wbidi-chars-2.c: New test.
a46658
            * c-c++-common/Wbidi-chars-3.c: New test.
a46658
            * c-c++-common/Wbidi-chars-4.c: New test.
a46658
            * c-c++-common/Wbidi-chars-5.c: New test.
a46658
            * c-c++-common/Wbidi-chars-6.c: New test.
a46658
            * c-c++-common/Wbidi-chars-7.c: New test.
a46658
            * c-c++-common/Wbidi-chars-8.c: New test.
a46658
            * c-c++-common/Wbidi-chars-9.c: New test.
a46658
            * c-c++-common/Wbidi-chars-10.c: New test.
a46658
            * c-c++-common/Wbidi-chars-11.c: New test.
a46658
            * c-c++-common/Wbidi-chars-12.c: New test.
a46658
            * c-c++-common/Wbidi-chars-13.c: New test.
a46658
            * c-c++-common/Wbidi-chars-14.c: New test.
a46658
            * c-c++-common/Wbidi-chars-15.c: New test.
a46658
            * c-c++-common/Wbidi-chars-16.c: New test.
a46658
            * c-c++-common/Wbidi-chars-17.c: New test.
4c2713
4c2713
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
a46658
index 8a4cd634f77..3976fc368db 100644
4c2713
--- a/gcc/c-family/c.opt
4c2713
+++ b/gcc/c-family/c.opt
4c2713
@@ -374,6 +374,30 @@ Wbad-function-cast
4c2713
 C ObjC Var(warn_bad_function_cast) Warning
4c2713
 Warn about casting functions to incompatible types.
4c2713
 
a46658
+Wbidi-chars
a46658
+C ObjC C++ ObjC++ Warning Alias(Wbidi-chars=,any,none)
4c2713
+;
4c2713
+
a46658
+Wbidi-chars=
4c2713
+C ObjC C++ ObjC++ RejectNegative Joined Warning CPP(cpp_warn_bidirectional) CppReason(CPP_W_BIDIRECTIONAL) Var(warn_bidirectional) Init(bidirectional_unpaired) Enum(cpp_bidirectional_level)
a46658
+-Wbidi-chars=[none|unpaired|any] Warn about UTF-8 bidirectional control characters.
4c2713
+
4c2713
+; Required for these enum values.
4c2713
+SourceInclude
4c2713
+cpplib.h
4c2713
+
4c2713
+Enum
a46658
+Name(cpp_bidirectional_level) Type(int) UnknownError(argument %qs to %<-Wbidi-chars%> not recognized)
4c2713
+
4c2713
+EnumValue
4c2713
+Enum(cpp_bidirectional_level) String(none) Value(bidirectional_none)
4c2713
+
4c2713
+EnumValue
4c2713
+Enum(cpp_bidirectional_level) String(unpaired) Value(bidirectional_unpaired)
4c2713
+
4c2713
+EnumValue
4c2713
+Enum(cpp_bidirectional_level) String(any) Value(bidirectional_any)
4c2713
+
4c2713
 Wbool-compare
4c2713
 C ObjC C++ ObjC++ Var(warn_bool_compare) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
4c2713
 Warn about boolean expression compared with an integer value different from true/false.
4c2713
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
a46658
index 6070288856c..a22758d18ee 100644
4c2713
--- a/gcc/doc/invoke.texi
4c2713
+++ b/gcc/doc/invoke.texi
4c2713
@@ -325,7 +325,9 @@ Objective-C and Objective-C++ Dialects}.
4c2713
 -Warith-conversion @gol
4c2713
 -Warray-bounds  -Warray-bounds=@var{n} @gol
4c2713
 -Wno-attributes  -Wattribute-alias=@var{n} -Wno-attribute-alias @gol
4c2713
--Wno-attribute-warning  -Wbool-compare  -Wbool-operation @gol
4c2713
+-Wno-attribute-warning  @gol
a46658
+-Wbidi-chars=@r{[}none@r{|}unpaired@r{|}any@r{]} @gol
4c2713
+-Wbool-compare  -Wbool-operation @gol
4c2713
 -Wno-builtin-declaration-mismatch @gol
4c2713
 -Wno-builtin-macro-redefined  -Wc90-c99-compat  -Wc99-c11-compat @gol
4c2713
 -Wc11-c2x-compat @gol
a46658
@@ -7557,6 +7559,23 @@ Attributes considered include @code{alloc_align}, @code{alloc_size},
4c2713
 This is the default.  You can disable these warnings with either
4c2713
 @option{-Wno-attribute-alias} or @option{-Wattribute-alias=0}.
4c2713
 
a46658
+@item -Wbidi-chars=@r{[}none@r{|}unpaired@r{|}any@r{]}
a46658
+@opindex Wbidi-chars=
a46658
+@opindex Wbidi-chars
a46658
+@opindex Wno-bidi-chars
a46658
+Warn about possibly misleading UTF-8 bidirectional control characters in
a46658
+comments, string literals, character constants, and identifiers.  Such
a46658
+characters can change left-to-right writing direction into right-to-left
a46658
+(and vice versa), which can cause confusion between the logical order and
a46658
+visual order.  This may be dangerous; for instance, it may seem that a piece
a46658
+of code is not commented out, whereas it in fact is.
4c2713
+
4c2713
+There are three levels of warning supported by GCC@.  The default is
a46658
+@option{-Wbidi-chars=unpaired}, which warns about improperly terminated
a46658
+bidi contexts.  @option{-Wbidi-chars=none} turns the warning off.
a46658
+@option{-Wbidi-chars=any} warns about any use of bidirectional control
a46658
+characters.
4c2713
+
4c2713
 @item -Wbool-compare
4c2713
 @opindex Wno-bool-compare
4c2713
 @opindex Wbool-compare
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-1.c b/gcc/testsuite/c-c++-common/Wbidi-chars-1.c
4c2713
new file mode 100644
a46658
index 00000000000..34f5ac19271
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-1.c
a46658
@@ -0,0 +1,12 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
4c2713
+
4c2713
+int main() {
4c2713
+    int isAdmin = 0;
4c2713
+    /*‮ } ⁦if (isAdmin)⁩ ⁦ begin admins only */
4c2713
+/* { dg-warning "bidirectional" "" { target *-*-* } .-1 } */
4c2713
+        __builtin_printf("You are an admin.\n");
4c2713
+    /* end admins only ‮ { ⁦*/
4c2713
+/* { dg-warning "bidirectional" "" { target *-*-* } .-1 } */
4c2713
+    return 0;
4c2713
+}
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-10.c b/gcc/testsuite/c-c++-common/Wbidi-chars-10.c
4c2713
new file mode 100644
a46658
index 00000000000..3f851b69e65
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-10.c
4c2713
@@ -0,0 +1,27 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=unpaired" } */
4c2713
+/* More nesting testing.  */
4c2713
+
4c2713
+/* RLE‫ LRI⁦ PDF‬ PDI⁩*/
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int LRE_\u202a_PDF_\u202c;
4c2713
+int LRE_\u202a_PDF_\u202c_LRE_\u202a_PDF_\u202c;
4c2713
+int LRE_\u202a_LRI_\u2066_PDF_\u202c_PDI_\u2069;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int RLE_\u202b_RLI_\u2067_PDF_\u202c_PDI_\u2069;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int RLE_\u202b_RLI_\u2067_PDI_\u2069_PDF_\u202c;
4c2713
+int FSI_\u2068_LRO_\u202d_PDI_\u2069_PDF_\u202c;
4c2713
+int FSI_\u2068;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int FSI_\u2068_PDI_\u2069;
4c2713
+int FSI_\u2068_FSI_\u2068_PDI_\u2069;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069;
4c2713
+int RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDF_\u202c;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int RLI_\u2067_RLI_\u2067_RLI_\u2067_RLI_\u2067_FSI_\u2068_PDI_\u2069_PDI_\u2069_PDI_\u2069_PDI_\u2069;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-11.c b/gcc/testsuite/c-c++-common/Wbidi-chars-11.c
4c2713
new file mode 100644
a46658
index 00000000000..270ce2368a9
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-11.c
a46658
@@ -0,0 +1,13 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=unpaired" } */
4c2713
+/* Test that we warn when mixing UCN and UTF-8.  */
4c2713
+
4c2713
+int LRE_‪_PDF_\u202c;
4c2713
+/* { dg-warning "mismatch" "" { target *-*-* } .-1 } */
4c2713
+int LRE_\u202a_PDF_‬_;
4c2713
+/* { dg-warning "mismatch" "" { target *-*-* } .-1 } */
4c2713
+const char *s1 = "LRE_‪_PDF_\u202c";
4c2713
+/* { dg-warning "mismatch" "" { target *-*-* } .-1 } */
4c2713
+const char *s2 = "LRE_\u202a_PDF_‬";
4c2713
+/* { dg-warning "mismatch" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-12.c b/gcc/testsuite/c-c++-common/Wbidi-chars-12.c
4c2713
new file mode 100644
a46658
index 00000000000..b07eec1da91
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-12.c
a46658
@@ -0,0 +1,19 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile { target { c || c++11 } } } */
a46658
+/* { dg-options "-Wbidi-chars=any" } */
4c2713
+/* Test raw strings.  */
4c2713
+
4c2713
+const char *s1 = R"(a b c LRE‪ 1 2 3 PDF‬ x y z)";
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+const char *s2 = R"(a b c RLE‫ 1 2 3 PDF‬ x y z)";
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+const char *s3 = R"(a b c LRO‭ 1 2 3 PDF‬ x y z)";
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+const char *s4 = R"(a b c RLO‮ 1 2 3 PDF‬ x y z)";
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+const char *s7 = R"(a b c FSI⁨ 1 2 3 PDI⁩ x y) z";
4c2713
+/* { dg-warning "U\\+2068" "" { target *-*-* } .-1 } */
4c2713
+const char *s8 = R"(a b c PDI⁩ x y )z";
4c2713
+/* { dg-warning "U\\+2069" "" { target *-*-* } .-1 } */
4c2713
+const char *s9 = R"(a b c PDF‬ x y z)";
4c2713
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-13.c b/gcc/testsuite/c-c++-common/Wbidi-chars-13.c
4c2713
new file mode 100644
a46658
index 00000000000..b2dd9fde752
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-13.c
a46658
@@ -0,0 +1,17 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile { target { c || c++11 } } } */
a46658
+/* { dg-options "-Wbidi-chars=unpaired" } */
4c2713
+/* Test raw strings.  */
4c2713
+
4c2713
+const char *s1 = R"(a b c LRE‪ 1 2 3)";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+const char *s2 = R"(a b c RLE‫ 1 2 3)";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+const char *s3 = R"(a b c LRO‭ 1 2 3)";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+const char *s4 = R"(a b c FSI⁨ 1 2 3)";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+const char *s5 = R"(a b c LRI⁦ 1 2 3)";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+const char *s6 = R"(a b c RLI⁧ 1 2 3)";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-14.c b/gcc/testsuite/c-c++-common/Wbidi-chars-14.c
a46658
new file mode 100644
a46658
index 00000000000..ba5f75d9553
a46658
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-14.c
a46658
@@ -0,0 +1,38 @@
a46658
+/* PR preprocessor/103026 */
a46658
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=unpaired" } */
a46658
+/* Test PDI handling, which also pops any subsequent LREs, RLEs, LROs,
a46658
+   or RLOs.  */
a46658
+
a46658
+/* LRI_⁦_LRI_⁦_RLE_‫_RLE_‫_RLE_‫_PDI_⁩*/
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+// LRI_⁦_RLE_‫_RLE_‫_RLE_‫_PDI_⁩
a46658
+// LRI_⁦_RLO_‮_RLE_‫_RLE_‫_PDI_⁩
a46658
+// LRI_⁦_RLO_‮_RLE_‫_PDI_⁩
a46658
+// FSI_⁨_RLO_‮_PDI_⁩
a46658
+// FSI_⁨_FSI_⁨_RLO_‮_PDI_⁩
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+
a46658
+int LRI_\u2066_LRI_\u2066_LRE_\u202a_LRE_\u202a_LRE_\u202a_PDI_\u2069;
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+int LRI_\u2066_LRI_\u2066_LRE_\u202a_LRE_\u202a_LRE_\u202a_PDI_\u2069_PDI_\u2069;
a46658
+int LRI_\u2066_LRI_\u2066_LRI_\u2066_LRE_\u202a_LRE_\u202a_LRE_\u202a_PDI_\u2069_PDI_\u2069;
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+int PDI_\u2069;
a46658
+int LRI_\u2066_PDI_\u2069;
a46658
+int RLI_\u2067_PDI_\u2069;
a46658
+int LRE_\u202a_LRI_\u2066_PDI_\u2069;
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+int LRI_\u2066_LRE_\u202a_PDF_\u202c_PDI_\u2069;
a46658
+int LRI_\u2066_LRE_\u202a_LRE_\u202a_PDF_\u202c_PDI_\u2069;
a46658
+int RLI_\u2067_LRI_\u2066_LRE_\u202a_LRE_\u202a_PDF_\u202c_PDI_\u2069;
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+int FSI_\u2068_LRI_\u2066_LRE_\u202a_LRE_\u202a_PDF_\u202c_PDI_\u2069;
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+int RLO_\u202e_PDI_\u2069;
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+int RLI_\u2067_PDI_\u2069_RLI_\u2067;
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+int FSI_\u2068_PDF_\u202c_PDI_\u2069;
a46658
+int FSI_\u2068_FSI_\u2068_PDF_\u202c_PDI_\u2069;
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-15.c b/gcc/testsuite/c-c++-common/Wbidi-chars-15.c
a46658
new file mode 100644
a46658
index 00000000000..a0ce8ff5e2c
a46658
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-15.c
a46658
@@ -0,0 +1,59 @@
a46658
+/* PR preprocessor/103026 */
a46658
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=unpaired" } */
a46658
+/* Test unpaired bidi control chars in multiline comments.  */
a46658
+
a46658
+/*
a46658
+ * LRE‪ end
a46658
+ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+/*
a46658
+ * RLE‫ end
a46658
+ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+/*
a46658
+ * LRO‭ end
a46658
+ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+/*
a46658
+ * RLO‮ end
a46658
+ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+/*
a46658
+ * LRI⁦ end
a46658
+ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+/*
a46658
+ * RLI⁧ end
a46658
+ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+/*
a46658
+ * FSI⁨ end
a46658
+ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+/* LRE‪
a46658
+   PDF‬ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+/* FSI⁨
a46658
+   PDI⁩ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+
a46658
+/* LRE<‪>
a46658
+ *
a46658
+ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-3 } */
a46658
+
a46658
+/*
a46658
+ * LRE<‪>
a46658
+ */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
a46658
+
a46658
+/*
a46658
+ *
a46658
+ * LRE<‪> */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+
a46658
+/* RLI<⁧> */ /* PDI<⁩> */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
+/* LRE<‪> */ /* PDF<‬> */
a46658
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-16.c b/gcc/testsuite/c-c++-common/Wbidi-chars-16.c
a46658
new file mode 100644
a46658
index 00000000000..baa0159861c
a46658
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-16.c
a46658
@@ -0,0 +1,26 @@
a46658
+/* PR preprocessor/103026 */
a46658
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=any" } */
a46658
+/* Test LTR/RTL chars.  */
a46658
+
a46658
+/* LTR<‎> */
a46658
+/* { dg-warning "U\\+200E" "" { target *-*-* } .-1 } */
a46658
+// LTR<‎>
a46658
+/* { dg-warning "U\\+200E" "" { target *-*-* } .-1 } */
a46658
+/* RTL<‏> */
a46658
+/* { dg-warning "U\\+200F" "" { target *-*-* } .-1 } */
a46658
+// RTL<‏>
a46658
+/* { dg-warning "U\\+200F" "" { target *-*-* } .-1 } */
a46658
+
a46658
+const char *s1 = "LTR<‎>";
a46658
+/* { dg-warning "U\\+200E" "" { target *-*-* } .-1 } */
a46658
+const char *s2 = "LTR\u200e";
a46658
+/* { dg-warning "U\\+200E" "" { target *-*-* } .-1 } */
a46658
+const char *s3 = "LTR\u200E";
a46658
+/* { dg-warning "U\\+200E" "" { target *-*-* } .-1 } */
a46658
+const char *s4 = "RTL<‏>";
a46658
+/* { dg-warning "U\\+200F" "" { target *-*-* } .-1 } */
a46658
+const char *s5 = "RTL\u200f";
a46658
+/* { dg-warning "U\\+200F" "" { target *-*-* } .-1 } */
a46658
+const char *s6 = "RTL\u200F";
a46658
+/* { dg-warning "U\\+200F" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-17.c b/gcc/testsuite/c-c++-common/Wbidi-chars-17.c
4c2713
new file mode 100644
a46658
index 00000000000..07cb4321f96
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-17.c
a46658
@@ -0,0 +1,30 @@
a46658
+/* PR preprocessor/103026 */
a46658
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=unpaired" } */
a46658
+/* Test LTR/RTL chars.  */
a46658
+
a46658
+/* LTR<‎> */
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+// LTR<‎>
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+/* RTL<‏> */
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+// RTL<‏>
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+int ltr_\u200e;
a46658
+/* { dg-error "universal character " "" { target *-*-* } .-1 } */
a46658
+int rtl_\u200f;
a46658
+/* { dg-error "universal character " "" { target *-*-* } .-1 } */
a46658
+
a46658
+const char *s1 = "LTR<‎>";
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+const char *s2 = "LTR\u200e";
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+const char *s3 = "LTR\u200E";
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+const char *s4 = "RTL<‏>";
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+const char *s5 = "RTL\u200f";
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+const char *s6 = "RTL\u200F";
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-2.c b/gcc/testsuite/c-c++-common/Wbidi-chars-2.c
a46658
new file mode 100644
a46658
index 00000000000..2340374f276
a46658
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-2.c
a46658
@@ -0,0 +1,9 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
4c2713
+
4c2713
+int main() {
4c2713
+    /* Say hello; newline⁧/*/ return 0 ;
4c2713
+/* { dg-warning "bidirectional" "" { target *-*-* } .-1 } */
4c2713
+    __builtin_printf("Hello world.\n");
4c2713
+    return 0;
4c2713
+}
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-3.c b/gcc/testsuite/c-c++-common/Wbidi-chars-3.c
4c2713
new file mode 100644
a46658
index 00000000000..9dc7edb6e64
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-3.c
a46658
@@ -0,0 +1,11 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
4c2713
+
4c2713
+int main() {
4c2713
+    const char* access_level = "user";
4c2713
+    if (__builtin_strcmp(access_level, "user‮ ⁦// Check if admin⁩ ⁦")) {
4c2713
+/* { dg-warning "bidirectional" "" { target *-*-* } .-1 } */
4c2713
+        __builtin_printf("You are an admin.\n");
4c2713
+    }
4c2713
+    return 0;
4c2713
+}
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-4.c b/gcc/testsuite/c-c++-common/Wbidi-chars-4.c
4c2713
new file mode 100644
a46658
index 00000000000..639e5c62e88
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-4.c
a46658
@@ -0,0 +1,188 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=any -Wno-multichar -Wno-overflow" } */
4c2713
+/* Test all bidi chars in various contexts (identifiers, comments,
4c2713
+   string literals, character constants), both UCN and UTF-8.  The bidi
4c2713
+   chars here are properly terminated, except for the character constants.  */
4c2713
+
4c2713
+/* a b c LRE‪ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+/* a b c RLE‫ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+/* a b c LRO‭ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+/* a b c RLO‮ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+/* a b c LRI⁦ 1 2 3 PDI⁩ x y z */
4c2713
+/* { dg-warning "U\\+2066" "" { target *-*-* } .-1 } */
4c2713
+/* a b c RLI⁧ 1 2 3 PDI⁩ x y */
4c2713
+/* { dg-warning "U\\+2067" "" { target *-*-* } .-1 } */
4c2713
+/* a b c FSI⁨ 1 2 3 PDI⁩ x y z */
4c2713
+/* { dg-warning "U\\+2068" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+/* Same but C++ comments instead.  */
4c2713
+// a b c LRE‪ 1 2 3 PDF‬ x y z
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+// a b c RLE‫ 1 2 3 PDF‬ x y z
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+// a b c LRO‭ 1 2 3 PDF‬ x y z
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+// a b c RLO‮ 1 2 3 PDF‬ x y z
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+// a b c LRI⁦ 1 2 3 PDI⁩ x y z
4c2713
+/* { dg-warning "U\\+2066" "" { target *-*-* } .-1 } */
4c2713
+// a b c RLI⁧ 1 2 3 PDI⁩ x y
4c2713
+/* { dg-warning "U\\+2067" "" { target *-*-* } .-1 } */
4c2713
+// a b c FSI⁨ 1 2 3 PDI⁩ x y z
4c2713
+/* { dg-warning "U\\+2068" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+/* Here we're closing an unopened context, warn when =any.  */
4c2713
+/* a b c PDI⁩ x y z */
4c2713
+/* { dg-warning "U\\+2069" "" { target *-*-* } .-1 } */
4c2713
+/* a b c PDF‬ x y z */
4c2713
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-1 } */
4c2713
+// a b c PDI⁩ x y z
4c2713
+/* { dg-warning "U\\+2069" "" { target *-*-* } .-1 } */
4c2713
+// a b c PDF‬ x y z
4c2713
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-1 } */
4c2713
+
a46658
+/* Multiline comments.  */
a46658
+/* a b c PDI⁩ x y z
a46658
+   */
a46658
+/* { dg-warning "U\\+2069" "" { target *-*-* } .-2 } */
a46658
+/* a b c PDF‬ x y z
a46658
+   */
a46658
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-2 } */
a46658
+/* first
a46658
+   a b c PDI⁩ x y z
a46658
+   */
a46658
+/* { dg-warning "U\\+2069" "" { target *-*-* } .-2 } */
a46658
+/* first
a46658
+   a b c PDF‬ x y z
a46658
+   */
a46658
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-2 } */
a46658
+/* first
a46658
+   a b c PDI⁩ x y z */
a46658
+/* { dg-warning "U\\+2069" "" { target *-*-* } .-1 } */
a46658
+/* first
a46658
+   a b c PDF‬ x y z */
a46658
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-1 } */
a46658
+
4c2713
+void
4c2713
+g1 ()
4c2713
+{
4c2713
+  const char *s1 = "a b c LRE‪ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+  const char *s2 = "a b c RLE‫ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+  const char *s3 = "a b c LRO‭ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+  const char *s4 = "a b c RLO‮ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+  const char *s5 = "a b c LRI⁦ 1 2 3 PDI⁩ x y z";
4c2713
+/* { dg-warning "U\\+2066" "" { target *-*-* } .-1 } */
4c2713
+  const char *s6 = "a b c RLI⁧ 1 2 3 PDI⁩ x y z";
4c2713
+/* { dg-warning "U\\+2067" "" { target *-*-* } .-1 } */
4c2713
+  const char *s7 = "a b c FSI⁨ 1 2 3 PDI⁩ x y z";
4c2713
+/* { dg-warning "U\\+2068" "" { target *-*-* } .-1 } */
4c2713
+  const char *s8 = "a b c PDI⁩ x y z";
4c2713
+/* { dg-warning "U\\+2069" "" { target *-*-* } .-1 } */
4c2713
+  const char *s9 = "a b c PDF‬ x y z";
4c2713
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+  const char *s10 = "a b c LRE\u202a 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+  const char *s11 = "a b c LRE\u202A 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+  const char *s12 = "a b c RLE\u202b 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+  const char *s13 = "a b c RLE\u202B 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+  const char *s14 = "a b c LRO\u202d 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+  const char *s15 = "a b c LRO\u202D 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+  const char *s16 = "a b c RLO\u202e 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+  const char *s17 = "a b c RLO\u202E 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+  const char *s18 = "a b c LRI\u2066 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-warning "U\\+2066" "" { target *-*-* } .-1 } */
4c2713
+  const char *s19 = "a b c RLI\u2067 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-warning "U\\+2067" "" { target *-*-* } .-1 } */
4c2713
+  const char *s20 = "a b c FSI\u2068 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-warning "U\\+2068" "" { target *-*-* } .-1 } */
4c2713
+}
4c2713
+
4c2713
+void
4c2713
+g2 ()
4c2713
+{
4c2713
+  const char c1 = '\u202a';
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+  const char c2 = '\u202A';
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+  const char c3 = '\u202b';
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+  const char c4 = '\u202B';
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+  const char c5 = '\u202d';
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+  const char c6 = '\u202D';
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+  const char c7 = '\u202e';
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+  const char c8 = '\u202E';
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+  const char c9 = '\u2066';
4c2713
+/* { dg-warning "U\\+2066" "" { target *-*-* } .-1 } */
4c2713
+  const char c10 = '\u2067';
4c2713
+/* { dg-warning "U\\+2067" "" { target *-*-* } .-1 } */
4c2713
+  const char c11 = '\u2068';
4c2713
+/* { dg-warning "U\\+2068" "" { target *-*-* } .-1 } */
4c2713
+}
4c2713
+
4c2713
+int a‪b‬c;
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+int a‫b‬c;
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+int a‭b‬c;
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+int a‮b‬c;
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+int a⁦b⁩c;
4c2713
+/* { dg-warning "U\\+2066" "" { target *-*-* } .-1 } */
4c2713
+int a⁧b⁩c;
4c2713
+/* { dg-warning "U\\+2067" "" { target *-*-* } .-1 } */
4c2713
+int a⁨b⁩c;
4c2713
+/* { dg-warning "U\\+2068" "" { target *-*-* } .-1 } */
4c2713
+int A‬X;
4c2713
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-1 } */
4c2713
+int A\u202cY;
4c2713
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-1 } */
4c2713
+int A\u202CY2;
4c2713
+/* { dg-warning "U\\+202C" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+int d\u202ae\u202cf;
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+int d\u202Ae\u202cf2;
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+int d\u202be\u202cf;
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+int d\u202Be\u202cf2;
4c2713
+/* { dg-warning "U\\+202B" "" { target *-*-* } .-1 } */
4c2713
+int d\u202de\u202cf;
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+int d\u202De\u202cf2;
4c2713
+/* { dg-warning "U\\+202D" "" { target *-*-* } .-1 } */
4c2713
+int d\u202ee\u202cf;
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+int d\u202Ee\u202cf2;
4c2713
+/* { dg-warning "U\\+202E" "" { target *-*-* } .-1 } */
4c2713
+int d\u2066e\u2069f;
4c2713
+/* { dg-warning "U\\+2066" "" { target *-*-* } .-1 } */
4c2713
+int d\u2067e\u2069f;
4c2713
+/* { dg-warning "U\\+2067" "" { target *-*-* } .-1 } */
4c2713
+int d\u2068e\u2069f;
4c2713
+/* { dg-warning "U\\+2068" "" { target *-*-* } .-1 } */
4c2713
+int X\u2069;
4c2713
+/* { dg-warning "U\\+2069" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-5.c b/gcc/testsuite/c-c++-common/Wbidi-chars-5.c
4c2713
new file mode 100644
a46658
index 00000000000..68cb053144b
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-5.c
a46658
@@ -0,0 +1,188 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=unpaired -Wno-multichar -Wno-overflow" } */
4c2713
+/* Test all bidi chars in various contexts (identifiers, comments,
4c2713
+   string literals, character constants), both UCN and UTF-8.  The bidi
4c2713
+   chars here are properly terminated, except for the character constants.  */
4c2713
+
4c2713
+/* a b c LRE‪ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c RLE‫ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c LRO‭ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c RLO‮ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c LRI⁦ 1 2 3 PDI⁩ x y z */
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c RLI⁧ 1 2 3 PDI⁩ x y */
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c FSI⁨ 1 2 3 PDI⁩ x y z */
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+/* Same but C++ comments instead.  */
4c2713
+// a b c LRE‪ 1 2 3 PDF‬ x y z
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c RLE‫ 1 2 3 PDF‬ x y z
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c LRO‭ 1 2 3 PDF‬ x y z
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c RLO‮ 1 2 3 PDF‬ x y z
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c LRI⁦ 1 2 3 PDI⁩ x y z
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c RLI⁧ 1 2 3 PDI⁩ x y
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c FSI⁨ 1 2 3 PDI⁩ x y z
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+/* Here we're closing an unopened context, warn when =any.  */
4c2713
+/* a b c PDI⁩ x y z */
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c PDF‬ x y z */
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c PDI⁩ x y z
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c PDF‬ x y z
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
a46658
+/* Multiline comments.  */
a46658
+/* a b c PDI⁩ x y z
a46658
+   */
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-2 } */
a46658
+/* a b c PDF‬ x y z
a46658
+   */
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-2 } */
a46658
+/* first
a46658
+   a b c PDI⁩ x y z
a46658
+   */
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-2 } */
a46658
+/* first
a46658
+   a b c PDF‬ x y z
a46658
+   */
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-2 } */
a46658
+/* first
a46658
+   a b c PDI⁩ x y z */
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+/* first
a46658
+   a b c PDF‬ x y z */
a46658
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
+
4c2713
+void
4c2713
+g1 ()
4c2713
+{
4c2713
+  const char *s1 = "a b c LRE‪ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s2 = "a b c RLE‫ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s3 = "a b c LRO‭ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s4 = "a b c RLO‮ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s5 = "a b c LRI⁦ 1 2 3 PDI⁩ x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s6 = "a b c RLI⁧ 1 2 3 PDI⁩ x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s7 = "a b c FSI⁨ 1 2 3 PDI⁩ x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s8 = "a b c PDI⁩ x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s9 = "a b c PDF‬ x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+  const char *s10 = "a b c LRE\u202a 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s11 = "a b c LRE\u202A 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s12 = "a b c RLE\u202b 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s13 = "a b c RLE\u202B 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s14 = "a b c LRO\u202d 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s15 = "a b c LRO\u202D 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s16 = "a b c RLO\u202e 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s17 = "a b c RLO\u202E 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s18 = "a b c LRI\u2066 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s19 = "a b c RLI\u2067 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s20 = "a b c FSI\u2068 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+}
4c2713
+
4c2713
+void
4c2713
+g2 ()
4c2713
+{
4c2713
+  const char c1 = '\u202a';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c2 = '\u202A';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c3 = '\u202b';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c4 = '\u202B';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c5 = '\u202d';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c6 = '\u202D';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c7 = '\u202e';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c8 = '\u202E';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c9 = '\u2066';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c10 = '\u2067';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char c11 = '\u2068';
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+}
4c2713
+
4c2713
+int a‪b‬c;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a‫b‬c;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a‭b‬c;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a‮b‬c;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a⁦b⁩c;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a⁧b⁩c;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a⁨b⁩c;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int A‬X;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int A\u202cY;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int A\u202CY2;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+int d\u202ae\u202cf;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u202Ae\u202cf2;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u202be\u202cf;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u202Be\u202cf2;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u202de\u202cf;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u202De\u202cf2;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u202ee\u202cf;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u202Ee\u202cf2;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u2066e\u2069f;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u2067e\u2069f;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int d\u2068e\u2069f;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int X\u2069;
4c2713
+/* { dg-bogus "unpaired" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-6.c b/gcc/testsuite/c-c++-common/Wbidi-chars-6.c
4c2713
new file mode 100644
a46658
index 00000000000..0ce6fff2dee
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-6.c
a46658
@@ -0,0 +1,155 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=unpaired" } */
4c2713
+/* Test nesting of bidi chars in various contexts.  */
4c2713
+
4c2713
+/* Terminated by the wrong char:  */
4c2713
+/* a b c LRE‪ 1 2 3 PDI⁩ x y z */
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c RLE‫ 1 2 3 PDI⁩ x y  z*/
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c LRO‭ 1 2 3 PDI⁩ x y z */
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c RLO‮ 1 2 3 PDI⁩ x y z */
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c LRI⁦ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c RLI⁧ 1 2 3 PDF‬ x y z */
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* a b c FSI⁨ 1 2 3 PDF‬ x y  z*/
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+/* LRE‪ PDF‬ */
4c2713
+/* LRE‪ LRE‪ PDF‬ PDF‬ */
4c2713
+/* PDF‬ LRE‪ PDF‬ */
4c2713
+/* LRE‪ PDF‬ LRE‪ PDF‬ */
4c2713
+/* LRE‪ LRE‪ PDF‬ */
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* PDF‬ LRE‪ */
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+// a b c LRE‪ 1 2 3 PDI⁩ x y z
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c RLE‫ 1 2 3 PDI⁩ x y  z*/
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c LRO‭ 1 2 3 PDI⁩ x y z 
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c RLO‮ 1 2 3 PDI⁩ x y z 
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c LRI⁦ 1 2 3 PDF‬ x y z 
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c RLI⁧ 1 2 3 PDF‬ x y z 
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// a b c FSI⁨ 1 2 3 PDF‬ x y  z
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+// LRE‪ PDF‬ 
4c2713
+// LRE‪ LRE‪ PDF‬ PDF‬
4c2713
+// PDF‬ LRE‪ PDF‬
4c2713
+// LRE‪ PDF‬ LRE‪ PDF‬
4c2713
+// LRE‪ LRE‪ PDF‬
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+// PDF‬ LRE‪
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+void
4c2713
+g1 ()
4c2713
+{
4c2713
+  const char *s1 = "a b c LRE‪ 1 2 3 PDI⁩ x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s2 = "a b c LRE\u202a 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s3 = "a b c RLE‫ 1 2 3 PDI⁩ x y ";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s4 = "a b c RLE\u202b 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s5 = "a b c LRO‭ 1 2 3 PDI⁩ x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s6 = "a b c LRO\u202d 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s7 = "a b c RLO‮ 1 2 3 PDI⁩ x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s8 = "a b c RLO\u202e 1 2 3 PDI\u2069 x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s9 = "a b c LRI⁦ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s10 = "a b c LRI\u2066 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s11 = "a b c RLI⁧ 1 2 3 PDF‬ x y z\
4c2713
+    ";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
4c2713
+  const char *s12 = "a b c RLI\u2067 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s13 = "a b c FSI⁨ 1 2 3 PDF‬ x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s14 = "a b c FSI\u2068 1 2 3 PDF\u202c x y z";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s15 = "PDF‬ LRE‪";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s16 = "PDF\u202c LRE\u202a";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s17 = "LRE‪ PDF‬";
4c2713
+  const char *s18 = "LRE\u202a PDF\u202c";
4c2713
+  const char *s19 = "LRE‪ LRE‪ PDF‬ PDF‬";
4c2713
+  const char *s20 = "LRE\u202a LRE\u202a PDF\u202c PDF\u202c";
4c2713
+  const char *s21 = "PDF‬ LRE‪ PDF‬";
4c2713
+  const char *s22 = "PDF\u202c LRE\u202a PDF\u202c";
4c2713
+  const char *s23 = "LRE‪ LRE‪ PDF‬";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s24 = "LRE\u202a LRE\u202a PDF\u202c";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s25 = "PDF‬ LRE‪";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s26 = "PDF\u202c LRE\u202a";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s27 = "PDF‬ LRE\u202a";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+  const char *s28 = "PDF\u202c LRE‪";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+}
4c2713
+
4c2713
+int aLRE‪bPDI⁩;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int A\u202aB\u2069C;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int aRLE‫bPDI⁩;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a\u202bB\u2069c;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int aLRO‭bPDI⁩;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a\u202db\u2069c2;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int aRLO‮bPDI⁩;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a\u202eb\u2069;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int aLRI⁦bPDF‬;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a\u2066b\u202c;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int aRLI⁧bPDF‬c
4c2713
+;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-2 } */
4c2713
+int a\u2067b\u202c;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int aFSI⁨bPDF‬;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a\u2068b\u202c;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int aFSI⁨bPD\u202C;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int aFSI\u2068bPDF‬_;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int aLRE‪bPDF‬b; 
4c2713
+int A\u202aB\u202c;
4c2713
+int a_LRE‪_LRE‪_b_PDF‬_PDF‬;
4c2713
+int A\u202aA\u202aB\u202cB\u202c;
4c2713
+int aPDF‬bLREadPDF‬;
4c2713
+int a_\u202C_\u202a_\u202c;
4c2713
+int a_LRE‪_b_PDF‬_c_LRE‪_PDF‬;
4c2713
+int a_\u202a_\u202c_\u202a_\u202c_;
4c2713
+int a_LRE‪_b_PDF‬_c_LRE‪;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int a_\u202a_\u202c_\u202a_;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-7.c b/gcc/testsuite/c-c++-common/Wbidi-chars-7.c
4c2713
new file mode 100644
a46658
index 00000000000..d012d420ec0
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-7.c
a46658
@@ -0,0 +1,9 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=any" } */
4c2713
+/* Test we ignore UCNs in comments.  */
4c2713
+
4c2713
+// a b c \u202a 1 2 3
4c2713
+// a b c \u202A 1 2 3
4c2713
+/* a b c \u202a 1 2 3 */
4c2713
+/* a b c \u202A 1 2 3 */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-8.c b/gcc/testsuite/c-c++-common/Wbidi-chars-8.c
4c2713
new file mode 100644
a46658
index 00000000000..4f54c5092ec
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-8.c
a46658
@@ -0,0 +1,13 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=any" } */
4c2713
+/* Test \u vs \U.  */
4c2713
+
4c2713
+int a_\u202A;
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+int a_\u202a_2;
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+int a_\U0000202A_3;
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
4c2713
+int a_\U0000202a_4;
4c2713
+/* { dg-warning "U\\+202A" "" { target *-*-* } .-1 } */
a46658
diff --git a/gcc/testsuite/c-c++-common/Wbidi-chars-9.c b/gcc/testsuite/c-c++-common/Wbidi-chars-9.c
4c2713
new file mode 100644
a46658
index 00000000000..e2af1b1ca97
4c2713
--- /dev/null
a46658
+++ b/gcc/testsuite/c-c++-common/Wbidi-chars-9.c
a46658
@@ -0,0 +1,29 @@
a46658
+/* PR preprocessor/103026 */
4c2713
+/* { dg-do compile } */
a46658
+/* { dg-options "-Wbidi-chars=unpaired" } */
4c2713
+/* Test that we properly separate bidi contexts (comment/identifier/character
4c2713
+   constant/string literal).  */
4c2713
+
4c2713
+/* LRE ->‪<- */ int pdf_\u202c_1;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* RLE ->‫<- */ int pdf_\u202c_2;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* LRO ->‭<- */ int pdf_\u202c_3;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* RLO ->‮<- */ int pdf_\u202c_4;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* LRI ->⁦<-*/ int pdi_\u2069_1;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* RLI ->⁧<- */ int pdi_\u2069_12;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* FSI ->⁨<- */ int pdi_\u2069_3;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+
4c2713
+const char *s1 = "LRE\u202a"; /* PDF ->‬<- */
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+/* LRE ->‪<- */ const char *s2 = "PDF\u202c";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+const char *s3 = "LRE\u202a"; int pdf_\u202c_5;
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
+int lre_\u202a; const char *s4 = "PDF\u202c";
4c2713
+/* { dg-warning "unpaired" "" { target *-*-* } .-1 } */
4c2713
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
a46658
index 176f8c5bbce..112b9c24751 100644
4c2713
--- a/libcpp/include/cpplib.h
4c2713
+++ b/libcpp/include/cpplib.h
a46658
@@ -319,6 +319,17 @@ enum cpp_main_search
4c2713
   CMS_system,  /* Search the system INCLUDE path.  */
4c2713
 };
4c2713
 
a46658
+/* The possible bidirectional control characters checking levels, from least
4c2713
+   restrictive to most.  */
4c2713
+enum cpp_bidirectional_level {
4c2713
+  /* No checking.  */
4c2713
+  bidirectional_none,
a46658
+  /* Only detect unpaired uses of bidirectional control characters.  */
4c2713
+  bidirectional_unpaired,
a46658
+  /* Detect any use of bidirectional control characters.  */
4c2713
+  bidirectional_any
4c2713
+};
4c2713
+
4c2713
 /* This structure is nested inside struct cpp_reader, and
4c2713
    carries all the options visible to the command line.  */
4c2713
 struct cpp_options
a46658
@@ -539,6 +550,10 @@ struct cpp_options
4c2713
   /* True if warn about differences between C++98 and C++11.  */
4c2713
   bool cpp_warn_cxx11_compat;
4c2713
 
a46658
+  /* Nonzero if bidirectional control characters checking is on.  See enum
4c2713
+     cpp_bidirectional_level.  */
4c2713
+  unsigned char cpp_warn_bidirectional;
4c2713
+
4c2713
   /* Dependency generation.  */
4c2713
   struct
4c2713
   {
a46658
@@ -643,7 +658,8 @@ enum cpp_warning_reason {
4c2713
   CPP_W_C90_C99_COMPAT,
4c2713
   CPP_W_C11_C2X_COMPAT,
4c2713
   CPP_W_CXX11_COMPAT,
4c2713
-  CPP_W_EXPANSION_TO_DEFINED
4c2713
+  CPP_W_EXPANSION_TO_DEFINED,
4c2713
+  CPP_W_BIDIRECTIONAL
4c2713
 };
4c2713
 
4c2713
 /* Callback for header lookup for HEADER, which is the name of a
4c2713
diff --git a/libcpp/init.c b/libcpp/init.c
4c2713
index 5a424e23553..f9a8f5f088f 100644
4c2713
--- a/libcpp/init.c
4c2713
+++ b/libcpp/init.c
4c2713
@@ -223,6 +223,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
4c2713
       = ENABLE_CANONICAL_SYSTEM_HEADERS;
4c2713
   CPP_OPTION (pfile, ext_numeric_literals) = 1;
4c2713
   CPP_OPTION (pfile, warn_date_time) = 0;
4c2713
+  CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
4c2713
 
4c2713
   /* Default CPP arithmetic to something sensible for the host for the
4c2713
      benefit of dumb users like fix-header.  */
a46658
diff --git a/libcpp/internal.h b/libcpp/internal.h
a46658
index 8577cab6c83..0ce0246c5a2 100644
a46658
--- a/libcpp/internal.h
a46658
+++ b/libcpp/internal.h
a46658
@@ -597,6 +597,13 @@ struct cpp_reader
a46658
   /* Location identifying the main source file -- intended to be line
a46658
      zero of said file.  */
a46658
   location_t main_loc;
a46658
+
a46658
+  /* Returns true iff we should warn about UTF-8 bidirectional control
a46658
+     characters.  */
a46658
+  bool warn_bidi_p () const
a46658
+  {
a46658
+    return CPP_OPTION (this, cpp_warn_bidirectional) != bidirectional_none;
a46658
+  }
a46658
 };
a46658
 
a46658
 /* Character classes.  Based on the more primitive macros in safe-ctype.h.
4c2713
diff --git a/libcpp/lex.c b/libcpp/lex.c
a46658
index fa2253d41c3..6a4fbce6030 100644
4c2713
--- a/libcpp/lex.c
4c2713
+++ b/libcpp/lex.c
a46658
@@ -1164,6 +1164,324 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
4c2713
     }
4c2713
 }
4c2713
 
4c2713
+namespace bidi {
4c2713
+  enum class kind {
a46658
+    NONE, LRE, RLE, LRO, RLO, LRI, RLI, FSI, PDF, PDI, LTR, RTL
4c2713
+  };
4c2713
+
4c2713
+  /* All the UTF-8 encodings of bidi characters start with E2.  */
4c2713
+  constexpr uchar utf8_start = 0xe2;
4c2713
+
4c2713
+  /* A vector holding currently open bidi contexts.  We use a char for
4c2713
+     each context, its LSB is 1 if it represents a PDF context, 0 if it
4c2713
+     represents a PDI context.  The next bit is 1 if this context was open
4c2713
+     by a bidi character written as a UCN, and 0 when it was UTF-8.  */
4c2713
+  semi_embedded_vec <unsigned char, 16> vec;
4c2713
+
4c2713
+  /* Close the whole comment/identifier/string literal/character constant
4c2713
+     context.  */
4c2713
+  void on_close ()
4c2713
+  {
4c2713
+    vec.truncate (0);
4c2713
+  }
4c2713
+
4c2713
+  /* Pop the last element in the vector.  */
4c2713
+  void pop ()
4c2713
+  {
4c2713
+    unsigned int len = vec.count ();
4c2713
+    gcc_checking_assert (len > 0);
4c2713
+    vec.truncate (len - 1);
4c2713
+  }
4c2713
+
a46658
+  /* Return the context of the Ith element.  */
a46658
+  kind ctx_at (unsigned int i)
a46658
+  {
a46658
+    return (vec[i] & 1) ? kind::PDF : kind::PDI;
a46658
+  }
a46658
+
4c2713
+  /* Return which context is currently opened.  */
4c2713
+  kind current_ctx ()
4c2713
+  {
4c2713
+    unsigned int len = vec.count ();
4c2713
+    if (len == 0)
4c2713
+      return kind::NONE;
a46658
+    return ctx_at (len - 1);
4c2713
+  }
4c2713
+
4c2713
+  /* Return true if the current context comes from a UCN origin, that is,
4c2713
+     the bidi char which started this bidi context was written as a UCN.  */
4c2713
+  bool current_ctx_ucn_p ()
4c2713
+  {
4c2713
+    unsigned int len = vec.count ();
4c2713
+    gcc_checking_assert (len > 0);
4c2713
+    return (vec[len - 1] >> 1) & 1;
4c2713
+  }
4c2713
+
4c2713
+  /* We've read a bidi char, update the current vector as necessary.  */
4c2713
+  void on_char (kind k, bool ucn_p)
4c2713
+  {
4c2713
+    switch (k)
4c2713
+      {
4c2713
+      case kind::LRE:
4c2713
+      case kind::RLE:
4c2713
+      case kind::LRO:
4c2713
+      case kind::RLO:
4c2713
+	vec.push (ucn_p ? 3u : 1u);
4c2713
+	break;
4c2713
+      case kind::LRI:
4c2713
+      case kind::RLI:
4c2713
+      case kind::FSI:
4c2713
+	vec.push (ucn_p ? 2u : 0u);
4c2713
+	break;
a46658
+      /* PDF terminates the scope of the last LRE, RLE, LRO, or RLO
a46658
+	 whose scope has not yet been terminated.  */
4c2713
+      case kind::PDF:
4c2713
+	if (current_ctx () == kind::PDF)
4c2713
+	  pop ();
4c2713
+	break;
a46658
+      /* PDI terminates the scope of the last LRI, RLI, or FSI whose
a46658
+	 scope has not yet been terminated, as well as the scopes of
a46658
+	 any subsequent LREs, RLEs, LROs, or RLOs whose scopes have not
a46658
+	 yet been terminated.  */
4c2713
+      case kind::PDI:
a46658
+	for (int i = vec.count () - 1; i >= 0; --i)
a46658
+	  if (ctx_at (i) == kind::PDI)
a46658
+	    {
a46658
+	      vec.truncate (i);
a46658
+	      break;
a46658
+	    }
a46658
+	break;
a46658
+      case kind::LTR:
a46658
+      case kind::RTL:
a46658
+	/* These aren't popped by a PDF/PDI.  */
4c2713
+	break;
4c2713
+      [[likely]] case kind::NONE:
4c2713
+	break;
4c2713
+      default:
4c2713
+	abort ();
4c2713
+      }
4c2713
+  }
4c2713
+
4c2713
+  /* Return a descriptive string for K.  */
4c2713
+  const char *to_str (kind k)
4c2713
+  {
4c2713
+    switch (k)
4c2713
+      {
4c2713
+      case kind::LRE:
4c2713
+	return "U+202A (LEFT-TO-RIGHT EMBEDDING)";
4c2713
+      case kind::RLE:
4c2713
+	return "U+202B (RIGHT-TO-LEFT EMBEDDING)";
4c2713
+      case kind::LRO:
4c2713
+	return "U+202D (LEFT-TO-RIGHT OVERRIDE)";
4c2713
+      case kind::RLO:
4c2713
+	return "U+202E (RIGHT-TO-LEFT OVERRIDE)";
4c2713
+      case kind::LRI:
4c2713
+	return "U+2066 (LEFT-TO-RIGHT ISOLATE)";
4c2713
+      case kind::RLI:
4c2713
+	return "U+2067 (RIGHT-TO-LEFT ISOLATE)";
4c2713
+      case kind::FSI:
4c2713
+	return "U+2068 (FIRST STRONG ISOLATE)";
4c2713
+      case kind::PDF:
4c2713
+	return "U+202C (POP DIRECTIONAL FORMATTING)";
4c2713
+      case kind::PDI:
4c2713
+	return "U+2069 (POP DIRECTIONAL ISOLATE)";
a46658
+      case kind::LTR:
a46658
+	return "U+200E (LEFT-TO-RIGHT MARK)";
a46658
+      case kind::RTL:
a46658
+	return "U+200F (RIGHT-TO-LEFT MARK)";
4c2713
+      default:
4c2713
+	abort ();
4c2713
+      }
4c2713
+  }
4c2713
+}
4c2713
+
4c2713
+/* Parse a sequence of 3 bytes starting with P and return its bidi code.  */
4c2713
+
4c2713
+static bidi::kind
4c2713
+get_bidi_utf8 (const unsigned char *const p)
4c2713
+{
4c2713
+  gcc_checking_assert (p[0] == bidi::utf8_start);
4c2713
+
4c2713
+  if (p[1] == 0x80)
4c2713
+    switch (p[2])
4c2713
+      {
4c2713
+      case 0xaa:
4c2713
+	return bidi::kind::LRE;
4c2713
+      case 0xab:
4c2713
+	return bidi::kind::RLE;
4c2713
+      case 0xac:
4c2713
+	return bidi::kind::PDF;
4c2713
+      case 0xad:
4c2713
+	return bidi::kind::LRO;
4c2713
+      case 0xae:
4c2713
+	return bidi::kind::RLO;
a46658
+      case 0x8e:
a46658
+	return bidi::kind::LTR;
a46658
+      case 0x8f:
a46658
+	return bidi::kind::RTL;
4c2713
+      default:
4c2713
+	break;
4c2713
+      }
4c2713
+  else if (p[1] == 0x81)
4c2713
+    switch (p[2])
4c2713
+      {
4c2713
+      case 0xa6:
4c2713
+	return bidi::kind::LRI;
4c2713
+      case 0xa7:
4c2713
+	return bidi::kind::RLI;
4c2713
+      case 0xa8:
4c2713
+	return bidi::kind::FSI;
4c2713
+      case 0xa9:
4c2713
+	return bidi::kind::PDI;
4c2713
+      default:
4c2713
+	break;
4c2713
+      }
4c2713
+
4c2713
+  return bidi::kind::NONE;
4c2713
+}
4c2713
+
4c2713
+/* Parse a UCN where P points just past \u or \U and return its bidi code.  */
4c2713
+
4c2713
+static bidi::kind
4c2713
+get_bidi_ucn (const unsigned char *p, bool is_U)
4c2713
+{
4c2713
+  /* 6.4.3 Universal Character Names
4c2713
+      \u hex-quad
4c2713
+      \U hex-quad hex-quad
4c2713
+     where \unnnn means \U0000nnnn.  */
4c2713
+
4c2713
+  if (is_U)
4c2713
+    {
4c2713
+      if (p[0] != '0' || p[1] != '0' || p[2] != '0' || p[3] != '0')
4c2713
+	return bidi::kind::NONE;
4c2713
+      /* Skip 4B so we can treat \u and \U the same below.  */
4c2713
+      p += 4;
4c2713
+    }
4c2713
+
4c2713
+  /* All code points we are looking for start with 20xx.  */
4c2713
+  if (p[0] != '2' || p[1] != '0')
4c2713
+    return bidi::kind::NONE;
4c2713
+  else if (p[2] == '2')
4c2713
+    switch (p[3])
4c2713
+      {
4c2713
+      case 'a':
4c2713
+      case 'A':
4c2713
+	return bidi::kind::LRE;
4c2713
+      case 'b':
4c2713
+      case 'B':
4c2713
+	return bidi::kind::RLE;
4c2713
+      case 'c':
4c2713
+      case 'C':
4c2713
+	return bidi::kind::PDF;
4c2713
+      case 'd':
4c2713
+      case 'D':
4c2713
+	return bidi::kind::LRO;
4c2713
+      case 'e':
4c2713
+      case 'E':
4c2713
+	return bidi::kind::RLO;
4c2713
+      default:
4c2713
+	break;
4c2713
+      }
4c2713
+  else if (p[2] == '6')
4c2713
+    switch (p[3])
4c2713
+      {
4c2713
+      case '6':
4c2713
+	return bidi::kind::LRI;
4c2713
+      case '7':
4c2713
+	return bidi::kind::RLI;
4c2713
+      case '8':
4c2713
+	return bidi::kind::FSI;
4c2713
+      case '9':
4c2713
+	return bidi::kind::PDI;
4c2713
+      default:
4c2713
+	break;
4c2713
+      }
a46658
+  else if (p[2] == '0')
a46658
+    switch (p[3])
a46658
+      {
a46658
+      case 'e':
a46658
+      case 'E':
a46658
+	return bidi::kind::LTR;
a46658
+      case 'f':
a46658
+      case 'F':
a46658
+	return bidi::kind::RTL;
a46658
+      default:
a46658
+	break;
a46658
+      }
4c2713
+
4c2713
+  return bidi::kind::NONE;
4c2713
+}
4c2713
+
4c2713
+/* We're closing a bidi context, that is, we've encountered a newline,
4c2713
+   are closing a C-style comment, or are at the end of a string literal,
4c2713
+   character constant, or identifier.  Warn if this context was not
4c2713
+   properly terminated by a PDI or PDF.  P points to the last character
4c2713
+   in this context.  */
4c2713
+
4c2713
+static void
4c2713
+maybe_warn_bidi_on_close (cpp_reader *pfile, const uchar *p)
4c2713
+{
4c2713
+  if (CPP_OPTION (pfile, cpp_warn_bidirectional) == bidirectional_unpaired
4c2713
+      && bidi::vec.count () > 0)
4c2713
+    {
4c2713
+      const location_t loc
4c2713
+	= linemap_position_for_column (pfile->line_table,
4c2713
+				       CPP_BUF_COLUMN (pfile->buffer, p));
4c2713
+      cpp_warning_with_line (pfile, CPP_W_BIDIRECTIONAL, loc, 0,
a46658
+			     "unpaired UTF-8 bidirectional control character "
4c2713
+			     "detected");
4c2713
+    }
4c2713
+  /* We're done with this context.  */
4c2713
+  bidi::on_close ();
4c2713
+}
4c2713
+
4c2713
+/* We're at the beginning or in the middle of an identifier/comment/string
4c2713
+   literal/character constant.  Warn if we've encountered a bidi character.
4c2713
+   KIND says which bidi character it was; P points to it in the character
4c2713
+   stream.  UCN_P is true iff this bidi character was written as a UCN.  */
4c2713
+
4c2713
+static void
4c2713
+maybe_warn_bidi_on_char (cpp_reader *pfile, const uchar *p, bidi::kind kind,
4c2713
+			 bool ucn_p)
4c2713
+{
4c2713
+  if (__builtin_expect (kind == bidi::kind::NONE, 1))
4c2713
+    return;
4c2713
+
4c2713
+  const auto warn_bidi = CPP_OPTION (pfile, cpp_warn_bidirectional);
4c2713
+
4c2713
+  if (warn_bidi != bidirectional_none)
4c2713
+    {
4c2713
+      const location_t loc
4c2713
+	= linemap_position_for_column (pfile->line_table,
4c2713
+				       CPP_BUF_COLUMN (pfile->buffer, p));
4c2713
+      /* It seems excessive to warn about a PDI/PDF that is closing
4c2713
+	 an opened context because we've already warned about the
4c2713
+	 opening character.  Except warn when we have a UCN x UTF-8
4c2713
+	 mismatch.  */
4c2713
+      if (kind == bidi::current_ctx ())
4c2713
+	{
4c2713
+	  if (warn_bidi == bidirectional_unpaired
4c2713
+	      && bidi::current_ctx_ucn_p () != ucn_p)
4c2713
+	    cpp_warning_with_line (pfile, CPP_W_BIDIRECTIONAL, loc, 0,
4c2713
+				   "UTF-8 vs UCN mismatch when closing "
4c2713
+				   "a context by \"%s\"", bidi::to_str (kind));
4c2713
+	}
4c2713
+      else if (warn_bidi == bidirectional_any)
4c2713
+	{
4c2713
+	  if (kind == bidi::kind::PDF || kind == bidi::kind::PDI)
4c2713
+	    cpp_warning_with_line (pfile, CPP_W_BIDIRECTIONAL, loc, 0,
4c2713
+				   "\"%s\" is closing an unopened context",
4c2713
+				   bidi::to_str (kind));
4c2713
+	  else
4c2713
+	    cpp_warning_with_line (pfile, CPP_W_BIDIRECTIONAL, loc, 0,
4c2713
+				   "found problematic Unicode character \"%s\"",
4c2713
+				   bidi::to_str (kind));
4c2713
+	}
4c2713
+    }
4c2713
+  /* We're done with this context.  */
4c2713
+  bidi::on_char (kind, ucn_p);
4c2713
+}
4c2713
+
4c2713
 /* Skip a C-style block comment.  We find the end of the comment by
4c2713
    seeing if an asterisk is before every '/' we encounter.  Returns
4c2713
    nonzero if comment terminated by EOF, zero otherwise.
a46658
@@ -1175,6 +1493,7 @@ _cpp_skip_block_comment (cpp_reader *pfile)
4c2713
   cpp_buffer *buffer = pfile->buffer;
4c2713
   const uchar *cur = buffer->cur;
4c2713
   uchar c;
a46658
+  const bool warn_bidi_p = pfile->warn_bidi_p ();
a46658
 
4c2713
   cur++;
4c2713
   if (*cur == '/')
a46658
@@ -1189,7 +1508,11 @@ _cpp_skip_block_comment (cpp_reader *pfile)
4c2713
       if (c == '/')
4c2713
 	{
4c2713
 	  if (cur[-2] == '*')
4c2713
-	    break;
4c2713
+	    {
4c2713
+	      if (warn_bidi_p)
4c2713
+		maybe_warn_bidi_on_close (pfile, cur);
4c2713
+	      break;
4c2713
+	    }
4c2713
 
4c2713
 	  /* Warn about potential nested comments, but not if the '/'
4c2713
 	     comes immediately before the true comment delimiter.
a46658
@@ -1208,6 +1531,8 @@ _cpp_skip_block_comment (cpp_reader *pfile)
4c2713
 	{
4c2713
 	  unsigned int cols;
4c2713
 	  buffer->cur = cur - 1;
4c2713
+	  if (warn_bidi_p)
4c2713
+	    maybe_warn_bidi_on_close (pfile, cur);
4c2713
 	  _cpp_process_line_notes (pfile, true);
4c2713
 	  if (buffer->next_line >= buffer->rlimit)
4c2713
 	    return true;
a46658
@@ -1218,6 +1543,13 @@ _cpp_skip_block_comment (cpp_reader *pfile)
4c2713
 
4c2713
 	  cur = buffer->cur;
4c2713
 	}
4c2713
+      /* If this is a beginning of a UTF-8 encoding, it might be
a46658
+	 a bidirectional control character.  */
4c2713
+      else if (__builtin_expect (c == bidi::utf8_start, 0) && warn_bidi_p)
4c2713
+	{
4c2713
+	  bidi::kind kind = get_bidi_utf8 (cur - 1);
4c2713
+	  maybe_warn_bidi_on_char (pfile, cur, kind, /*ucn_p=*/false);
4c2713
+	}
4c2713
     }
4c2713
 
4c2713
   buffer->cur = cur;
a46658
@@ -1233,9 +1565,31 @@ skip_line_comment (cpp_reader *pfile)
4c2713
 {
4c2713
   cpp_buffer *buffer = pfile->buffer;
4c2713
   location_t orig_line = pfile->line_table->highest_line;
a46658
+  const bool warn_bidi_p = pfile->warn_bidi_p ();
4c2713
 
4c2713
-  while (*buffer->cur != '\n')
4c2713
-    buffer->cur++;
4c2713
+  if (!warn_bidi_p)
4c2713
+    while (*buffer->cur != '\n')
4c2713
+      buffer->cur++;
4c2713
+  else
4c2713
+    {
4c2713
+      while (*buffer->cur != '\n'
4c2713
+	     && *buffer->cur != bidi::utf8_start)
4c2713
+	buffer->cur++;
4c2713
+      if (__builtin_expect (*buffer->cur == bidi::utf8_start, 0))
4c2713
+	{
4c2713
+	  while (*buffer->cur != '\n')
4c2713
+	    {
4c2713
+	      if (__builtin_expect (*buffer->cur == bidi::utf8_start, 0))
4c2713
+		{
4c2713
+		  bidi::kind kind = get_bidi_utf8 (buffer->cur);
4c2713
+		  maybe_warn_bidi_on_char (pfile, buffer->cur, kind,
4c2713
+					   /*ucn_p=*/false);
4c2713
+		}
4c2713
+	      buffer->cur++;
4c2713
+	    }
4c2713
+	  maybe_warn_bidi_on_close (pfile, buffer->cur);
4c2713
+	}
4c2713
+    }
4c2713
 
4c2713
   _cpp_process_line_notes (pfile, true);
4c2713
   return orig_line != pfile->line_table->highest_line;
a46658
@@ -1346,11 +1700,13 @@ static const cppchar_t utf8_signifier = 0xC0;
4c2713
 
4c2713
 /* Returns TRUE if the sequence starting at buffer->cur is valid in
4c2713
    an identifier.  FIRST is TRUE if this starts an identifier.  */
4c2713
+
4c2713
 static bool
4c2713
 forms_identifier_p (cpp_reader *pfile, int first,
4c2713
 		    struct normalize_state *state)
4c2713
 {
4c2713
   cpp_buffer *buffer = pfile->buffer;
a46658
+  const bool warn_bidi_p = pfile->warn_bidi_p ();
4c2713
 
4c2713
   if (*buffer->cur == '$')
4c2713
     {
a46658
@@ -1373,6 +1729,13 @@ forms_identifier_p (cpp_reader *pfile, int first,
4c2713
       cppchar_t s;
4c2713
       if (*buffer->cur >= utf8_signifier)
4c2713
 	{
4c2713
+	  if (__builtin_expect (*buffer->cur == bidi::utf8_start, 0)
4c2713
+	      && warn_bidi_p)
4c2713
+	    {
4c2713
+	      bidi::kind kind = get_bidi_utf8 (buffer->cur);
4c2713
+	      maybe_warn_bidi_on_char (pfile, buffer->cur, kind,
4c2713
+				       /*ucn_p=*/false);
4c2713
+	    }
4c2713
 	  if (_cpp_valid_utf8 (pfile, &buffer->cur, buffer->rlimit, 1 + !first,
4c2713
 			       state, &s))
4c2713
 	    return true;
a46658
@@ -1381,6 +1744,13 @@ forms_identifier_p (cpp_reader *pfile, int first,
4c2713
 	       && (buffer->cur[1] == 'u' || buffer->cur[1] == 'U'))
4c2713
 	{
4c2713
 	  buffer->cur += 2;
4c2713
+	  if (warn_bidi_p)
4c2713
+	    {
4c2713
+	      bidi::kind kind = get_bidi_ucn (buffer->cur,
4c2713
+					      buffer->cur[-1] == 'U');
4c2713
+	      maybe_warn_bidi_on_char (pfile, buffer->cur, kind,
4c2713
+				       /*ucn_p=*/true);
4c2713
+	    }
4c2713
 	  if (_cpp_valid_ucn (pfile, &buffer->cur, buffer->rlimit, 1 + !first,
4c2713
 			      state, &s, NULL, NULL))
4c2713
 	    return true;
a46658
@@ -1489,6 +1859,7 @@ lex_identifier (cpp_reader *pfile, const uchar *base, bool starts_ucn,
4c2713
   const uchar *cur;
4c2713
   unsigned int len;
4c2713
   unsigned int hash = HT_HASHSTEP (0, *base);
a46658
+  const bool warn_bidi_p = pfile->warn_bidi_p ();
4c2713
 
4c2713
   cur = pfile->buffer->cur;
4c2713
   if (! starts_ucn)
a46658
@@ -1512,6 +1883,8 @@ lex_identifier (cpp_reader *pfile, const uchar *base, bool starts_ucn,
a46658
 	    pfile->buffer->cur++;
a46658
 	  }
a46658
       } while (forms_identifier_p (pfile, false, nst));
4c2713
+      if (warn_bidi_p)
4c2713
+	maybe_warn_bidi_on_close (pfile, pfile->buffer->cur);
4c2713
       result = _cpp_interpret_identifier (pfile, base,
4c2713
 					  pfile->buffer->cur - base);
4c2713
       *spelling = cpp_lookup (pfile, base, pfile->buffer->cur - base);
a46658
@@ -1758,6 +2131,7 @@ static void
4c2713
 lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
4c2713
 {
4c2713
   const uchar *pos = base;
a46658
+  const bool warn_bidi_p = pfile->warn_bidi_p ();
4c2713
 
4c2713
   /* 'tis a pity this information isn't passed down from the lexer's
4c2713
      initial categorization of the token.  */
a46658
@@ -1994,8 +2368,15 @@ lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
4c2713
 	  pos = base = pfile->buffer->cur;
4c2713
 	  note = &pfile->buffer->notes[pfile->buffer->cur_note];
4c2713
 	}
4c2713
+      else if (__builtin_expect ((unsigned char) c == bidi::utf8_start, 0)
4c2713
+	       && warn_bidi_p)
4c2713
+	maybe_warn_bidi_on_char (pfile, pos - 1, get_bidi_utf8 (pos - 1),
4c2713
+				 /*ucn_p=*/false);
4c2713
     }
4c2713
 
4c2713
+  if (warn_bidi_p)
4c2713
+    maybe_warn_bidi_on_close (pfile, pos);
4c2713
+
4c2713
   if (CPP_OPTION (pfile, user_literals))
4c2713
     {
4c2713
       /* If a string format macro, say from inttypes.h, is placed touching
a46658
@@ -2090,15 +2471,27 @@ lex_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
4c2713
   else
4c2713
     terminator = '>', type = CPP_HEADER_NAME;
4c2713
 
a46658
+  const bool warn_bidi_p = pfile->warn_bidi_p ();
4c2713
   for (;;)
4c2713
     {
4c2713
       cppchar_t c = *cur++;
4c2713
 
4c2713
       /* In #include-style directives, terminators are not escapable.  */
4c2713
       if (c == '\\' && !pfile->state.angled_headers && *cur != '\n')
4c2713
-	cur++;
4c2713
+	{
4c2713
+	  if ((cur[0] == 'u' || cur[0] == 'U') && warn_bidi_p)
4c2713
+	    {
4c2713
+	      bidi::kind kind = get_bidi_ucn (cur + 1, cur[0] == 'U');
4c2713
+	      maybe_warn_bidi_on_char (pfile, cur, kind, /*ucn_p=*/true);
4c2713
+	    }
4c2713
+	  cur++;
4c2713
+	}
4c2713
       else if (c == terminator)
4c2713
-	break;
4c2713
+	{
4c2713
+	  if (warn_bidi_p)
4c2713
+	    maybe_warn_bidi_on_close (pfile, cur - 1);
4c2713
+	  break;
4c2713
+	}
4c2713
       else if (c == '\n')
4c2713
 	{
4c2713
 	  cur--;
a46658
@@ -2115,6 +2508,11 @@ lex_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
4c2713
 	}
4c2713
       else if (c == '\0')
4c2713
 	saw_NUL = true;
4c2713
+      else if (__builtin_expect (c == bidi::utf8_start, 0) && warn_bidi_p)
4c2713
+	{
4c2713
+	  bidi::kind kind = get_bidi_utf8 (cur - 1);
4c2713
+	  maybe_warn_bidi_on_char (pfile, cur - 1, kind, /*ucn_p=*/false);
4c2713
+	}
4c2713
     }
4c2713
 
4c2713
   if (saw_NUL && !pfile->state.skipping)