Blame SOURCES/gcc11-Wbidi-chars.patch

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