Blame SOURCES/gcc11-Wbidi-chars.patch

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