diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index c75d83a6d1a7a..800027bb4c1ff 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -51,6 +51,11 @@ C/C++ Language Potentially Breaking Changes ensure they are not caught by these optimizations. It is also possible to use ``-fwrapv-pointer`` or ``-fno-delete-null-pointer-checks`` to make pointer arithmetic on null pointers well-defined. (#GH130734, #GH130742, #GH130952) +- Use of the dollar sign (``$``) in an identifier is no longer a conforming + extension in either C or C++, so ``-fdollars-in-identifiers`` is no longer + enabled by default. Use of the dollar sign in an identifier will now be + diagnosed as an error unless ``-fdollars-in-identifiers`` is explicitly + enabled. C++ Specific Potentially Breaking Changes ----------------------------------------- diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td index f29edfa835d42..41d3e29d1324e 100644 --- a/clang/include/clang/Basic/DiagnosticLexKinds.td +++ b/clang/include/clang/Basic/DiagnosticLexKinds.td @@ -57,6 +57,9 @@ def warn_no_newline_eof : Warning<"no newline at end of file">, def ext_dollar_in_identifier : Extension<"'$' in identifier">, InGroup>; +def warn_dollar_in_identifier : Warning< + "'$' in identifier; did you mean to enable '-fdollars-in-identifiers'?">, + InGroup>; def ext_charize_microsoft : Extension< "charizing operator #@ is a Microsoft extension">, InGroup; diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 930c1c06d1a76..954ca11f8b8b2 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -119,7 +119,7 @@ LANGOPT(WChar , 1, 0, "wchar_t keyword") LANGOPT(Char8 , 1, 0, "char8_t keyword") LANGOPT(IEEE128 , 1, 0, "__ieee128 keyword") LANGOPT(DeclSpecKeyword , 1, 0, "__declspec keyword") -BENIGN_LANGOPT(DollarIdents , 1, 1, "'$' in identifiers") +COMPATIBLE_LANGOPT(DollarIdents, 1, 0, "'$' in identifiers") BENIGN_LANGOPT(AsmPreprocessor, 1, 0, "preprocessor in asm mode") LANGOPT(GNUMode , 1, 1, "GNU extensions") LANGOPT(GNUKeywords , 1, 1, "GNU keywords") diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index e9acb20348654..7ffc9e2008c63 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -632,7 +632,6 @@ defvar open_cl = LangOpts<"OpenCL">; defvar cuda = LangOpts<"CUDA">; defvar hip = LangOpts<"HIP">; defvar gnu_mode = LangOpts<"GNUMode">; -defvar asm_preprocessor = LangOpts<"AsmPreprocessor">; defvar hlsl = LangOpts<"HLSL">; defvar std = !strconcat("LangStandard::getLangStandardForKind(", lang_std.KeyPath, ")"); @@ -2092,7 +2091,7 @@ def fno_discard_value_names : Flag<["-"], "fno-discard-value-names">, Group, Visibility<[ClangOption, DXCOption]>, HelpText<"Do not discard value names in LLVM IR">; defm dollars_in_identifiers : BoolFOption<"dollars-in-identifiers", - LangOpts<"DollarIdents">, Default, + LangOpts<"DollarIdents">, DefaultFalse, PosFlag, NegFlag, BothFlags<[], [ClangOption, CC1Option], " '$' in identifiers">>; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index c601967a8715c..b281b9ddb6b05 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -4035,6 +4035,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally. LangOpts.DeclSpecKeyword = 1; // To get __declspec. LangOpts.C99 = 1; // To get kw_restrict for non-underscore-prefixed restrict. + LangOpts.DollarIdents = 1; // To allow $ in identifiers. return LangOpts; } diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 93200458f04b4..d23857875d924 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -4036,8 +4036,11 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) { MIOpt.ReadToken(); return LexIdentifierContinue(Result, CurPtr); } - Kind = tok::unknown; + // We do not want to diagnose in ASM preprocessor mode because the + // assembler isn't necessarily trying to lex identifiers to begin with. + if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) + Diag(CurPtr - 1, diag::warn_dollar_in_identifier); break; // C99 6.4.4: Character Constants. diff --git a/clang/test/AST/ByteCode/codegen.m b/clang/test/AST/ByteCode/codegen.m index 6139596c6337a..a7b3a100165eb 100644 --- a/clang/test/AST/ByteCode/codegen.m +++ b/clang/test/AST/ByteCode/codegen.m @@ -3,7 +3,7 @@ /// See test/CodeGenObjC/constant-strings.m /// Test that we let the APValue we create for ObjCStringLiterals point to the right expression. -// RUN: %clang_cc1 -triple x86_64-macho -emit-llvm -o %t %s -fexperimental-new-constant-interpreter +// RUN: %clang_cc1 -triple x86_64-macho -fdollars-in-identifiers -emit-llvm -o %t %s -fexperimental-new-constant-interpreter // RUN: FileCheck --check-prefix=CHECK-NEXT < %t %s // Check that we set alignment 1 on the string. diff --git a/clang/test/C/C99/n717.c b/clang/test/C/C99/n717.c index 25010b4137065..240ff2007c7e9 100644 --- a/clang/test/C/C99/n717.c +++ b/clang/test/C/C99/n717.c @@ -37,7 +37,7 @@ M(\U123456789) // Okay-ish, two tokens (valid-per-spec-but-actually-invalid UCN // GH87106. M(\u0024) // expected-error {{character '$' cannot be specified by a universal character name}} M(\U00000024) // expected-error {{character '$' cannot be specified by a universal character name}} -M($) +M($) // expected-warning {{'$' in identifier; did you mean to enable '-fdollars-in-identifiers'?}} // These should always be rejected because they're not valid identifier // characters. diff --git a/clang/test/C/drs/dr0xx.c b/clang/test/C/drs/dr0xx.c index 5fe023deaece9..ffcd63b0cc9a7 100644 --- a/clang/test/C/drs/dr0xx.c +++ b/clang/test/C/drs/dr0xx.c @@ -1,9 +1,9 @@ -/* RUN: %clang_cc1 -std=c89 -fsyntax-only -verify=expected,c89only -pedantic -Wno-declaration-after-statement -Wno-c11-extensions %s - RUN: %clang_cc1 -std=c89 -fsyntax-only -verify=expected,c89only -pedantic -Wno-declaration-after-statement -Wno-c11-extensions -fno-signed-char %s - RUN: %clang_cc1 -std=c99 -fsyntax-only -verify=expected,c99untilc2x -pedantic -Wno-c11-extensions %s - RUN: %clang_cc1 -std=c11 -fsyntax-only -verify=expected,c99untilc2x -pedantic %s - RUN: %clang_cc1 -std=c17 -fsyntax-only -verify=expected,c99untilc2x -pedantic %s - RUN: %clang_cc1 -std=c2x -fsyntax-only -verify=expected,c2xandup -pedantic %s +/* RUN: %clang_cc1 -std=c89 -fdollars-in-identifiers -fsyntax-only -verify=expected,c89only -pedantic -Wno-declaration-after-statement -Wno-c11-extensions %s + RUN: %clang_cc1 -std=c89 -fdollars-in-identifiers -fsyntax-only -verify=expected,c89only -pedantic -Wno-declaration-after-statement -Wno-c11-extensions -fno-signed-char %s + RUN: %clang_cc1 -std=c99 -fdollars-in-identifiers -fsyntax-only -verify=expected,c99untilc2x -pedantic -Wno-c11-extensions %s + RUN: %clang_cc1 -std=c11 -fdollars-in-identifiers -fsyntax-only -verify=expected,c99untilc2x -pedantic %s + RUN: %clang_cc1 -std=c17 -fdollars-in-identifiers -fsyntax-only -verify=expected,c99untilc2x -pedantic %s + RUN: %clang_cc1 -std=c2x -fdollars-in-identifiers -fsyntax-only -verify=expected,c2xandup -pedantic %s */ /* The following are DRs which do not require tests to demonstrate diff --git a/clang/test/CodeGen/ms-inline-asm.c b/clang/test/CodeGen/ms-inline-asm.c index c3eef9a23e166..811e3ccd2a89a 100644 --- a/clang/test/CodeGen/ms-inline-asm.c +++ b/clang/test/CodeGen/ms-inline-asm.c @@ -1,5 +1,5 @@ // REQUIRES: x86-registered-target -// RUN: %clang_cc1 %s -triple i386-apple-darwin10 -fasm-blocks -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 %s -triple i386-apple-darwin10 -fdollars-in-identifiers -fasm-blocks -emit-llvm -o - | FileCheck %s void t1(void) { // CHECK: @t1 diff --git a/clang/test/CodeGenObjC/extern-void-class-decl.m b/clang/test/CodeGenObjC/extern-void-class-decl.m index 826622b94c1bb..55b2f9565439a 100644 --- a/clang/test/CodeGenObjC/extern-void-class-decl.m +++ b/clang/test/CodeGenObjC/extern-void-class-decl.m @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 -fdollars-in-identifiers %s -emit-llvm -o - | FileCheck %s extern void OBJC_CLASS_$_f; Class c = (Class)&OBJC_CLASS_$_f; diff --git a/clang/test/Lexer/dollar-idents.c b/clang/test/Lexer/dollar-idents.c index a1263b4e572cc..6fd32a13bc591 100644 --- a/clang/test/Lexer/dollar-idents.c +++ b/clang/test/Lexer/dollar-idents.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -dump-tokens %s 2>&1 | FileCheck %s +// RUN: %clang_cc1 -dump-tokens -fdollars-in-identifiers %s 2>&1 | FileCheck %s // RUN: %clang_cc1 -dump-tokens -x assembler-with-cpp %s 2>&1 | FileCheck %s --check-prefix=CHECK-ASM // PR3808 diff --git a/clang/test/Lexer/gh128939.cpp b/clang/test/Lexer/gh128939.cpp new file mode 100644 index 0000000000000..498fe34b41ffc --- /dev/null +++ b/clang/test/Lexer/gh128939.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -E -fdollars-in-identifiers %s 2>&1 | FileCheck %s --check-prefix=CHECK-DOLLARS +// RUN: %clang_cc1 -E %s 2>&1 | FileCheck %s --check-prefix=CHECK-NO-DOLLARS +// RUN: %clang_cc1 -verify -x assembler-with-cpp %s +// GH128939 + +#define FOO$ 10 // expected-warning {{ISO C99 requires whitespace after the macro name}} +#define STR(x) #x +#define STR2(x) STR(x) +const char *p = STR2(FOO$); + +// CHECK-NO-DOLLARS: const char *p = "$ 10$"; +// CHECK-DOLLARS: const char *p = "10"; + +#define STR3 STR( +const char *q = STR3$10); + +// CHECK-NO-DOLLARS: const char *q = "$10"; +// CHECK-DOLLARS: const char *q = STR3$10); diff --git a/clang/test/Preprocessor/c90.c b/clang/test/Preprocessor/c90.c index 3b9105fe6ee91..cbf90b87b1e6d 100644 --- a/clang/test/Preprocessor/c90.c +++ b/clang/test/Preprocessor/c90.c @@ -1,4 +1,4 @@ -/* RUN: %clang_cc1 %s -std=c89 -Eonly -verify -pedantic-errors +/* RUN: %clang_cc1 %s -std=c89 -Eonly -DTEST_THIS_TOO -verify -pedantic-errors * RUN: %clang_cc1 %s -std=c89 -E | FileCheck %s */ @@ -7,7 +7,18 @@ #define foo`bar /* expected-error {{whitespace required after macro name}} */ #define foo2!bar /* expected-warning {{whitespace recommended after macro name}} */ -#define foo3$bar /* expected-error {{'$' in identifier}} */ +#define foo3$bar /* expected-error {{whitespace required after macro name}} + expected-warning {{'$' in identifier; did you mean to enable '-fdollars-in-identifiers'?}} + */ + +#define test$ /* expected-error {{whitespace required after macro name}} + expected-warning {{'$' in identifier; did you mean to enable '-fdollars-in-identifiers'?}} + */ +#ifdef TEST_THIS_TOO +#define $ /* expected-error {{macro name must be an identifier}} + expected-warning {{'$' in identifier; did you mean to enable '-fdollars-in-identifiers'?}} + */ +#endif /* CHECK-NOT: this comment should be missing * CHECK: {{^}}// this comment should be present{{$}} diff --git a/clang/test/SemaCXX/scope-check.cpp b/clang/test/SemaCXX/scope-check.cpp index 7eec58ca057a9..d23b042087a18 100644 --- a/clang/test/SemaCXX/scope-check.cpp +++ b/clang/test/SemaCXX/scope-check.cpp @@ -434,7 +434,8 @@ namespace test_recovery { a0:; switch (c) { - case $: // expected-error {{}} + case $: // expected-error {{}} \ + expected-warning {{'$' in identifier; did you mean to enable '-fdollars-in-identifiers'?}} case 0: int x = 56; // expected-note {{jump bypasses variable initialization}} case 1: // expected-error {{cannot jump}} diff --git a/clang/unittests/Analysis/MacroExpansionContextTest.cpp b/clang/unittests/Analysis/MacroExpansionContextTest.cpp index 19074d7dcfdd4..e091459532420 100644 --- a/clang/unittests/Analysis/MacroExpansionContextTest.cpp +++ b/clang/unittests/Analysis/MacroExpansionContextTest.cpp @@ -41,6 +41,7 @@ class MacroExpansionContextTest : public ::testing::Test { TargetOpts->Triple = "x86_64-pc-linux-unknown"; Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); LangOpts.CPlusPlus20 = 1; // For __VA_OPT__ + LangOpts.DollarIdents = 1; // For $identifier$ testing. } IntrusiveRefCntPtr InMemoryFileSystem;