[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/Sirraide created https://github.com/llvm/llvm-project/pull/88265 This is a tentative implementation of support for raw string literals in C following the discussion on #85703. GCC supports raw string literals in C in `-gnuXY` mode. This pr both enables raw string literals in `-gnuXY` mode in C and adds a `-f[no-]raw-string-literals` flag to override this beheviour. There are a few questions I still have though: 1. GCC does not seem to support raw string literals in C++ before C++11, even if e.g. `-std=gnu++03` is passed. Should we follow this behaviour or should we enable raw string literals in earlier C++ language modes as well if `-gnu++XY` is passed? `-fraw-string-literals` currently makes it possible to enable them in e.g. C++03. 2. `-fno-raw-string-literals` allows users to *disable* raw string literals in `-gnuXY` mode. I thought it might be useful to have this, but do we want it? 3. The implementation of this currently adds a `RawStringLiterals` option to the LangOpts; `-f[no-]raw-string-literals` overrides the default value for it which depends on the language standard. As a consequence, passing e.g. `-std=c++11 -fno-raw-string-literals` will *disable* raw string literals even though we’re in C++11 mode. Do we want to allow this or should we just ignore `-f[no-]raw-string-literals` if we’re in C++11 or later? 4. This probably deserves a note in `LanguageExtensions.rst`, but I’m not exactly sure where. 5. Should we add a flag for this to `__has_feature`/`__has_extension`? >From 40e533e6f58acbe832b3fa4e14ca9fd600cf77cf Mon Sep 17 00:00:00 2001 From: Sirraide Date: Wed, 10 Apr 2024 14:36:23 +0200 Subject: [PATCH] [Clang] Allow raw string literals in C as an extension --- clang/docs/ReleaseNotes.rst | 3 +++ clang/include/clang/Basic/LangOptions.def | 2 ++ clang/include/clang/Basic/LangStandard.h | 6 ++ clang/include/clang/Driver/Options.td | 6 ++ clang/lib/Basic/LangOptions.cpp | 1 + clang/lib/Driver/ToolChains/Clang.cpp | 2 ++ clang/lib/Format/Format.cpp | 1 + clang/lib/Lex/Lexer.cpp | 10 +- clang/test/Lexer/raw-string-ext.c | 18 ++ 9 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 clang/test/Lexer/raw-string-ext.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f96cebbde3d825..20d14130fb62bc 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -43,6 +43,9 @@ code bases. C/C++ Language Potentially Breaking Changes --- +- Clang now supports raw string literals in ``-std=gnuXY`` mode as an extension in + C. This behaviour can also be overridden using ``-f[no-]raw-string-literals``. + C++ Specific Potentially Breaking Changes - - Clang now diagnoses function/variable templates that shadow their own template parameters, e.g. ``template void T();``. diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 8ef6700ecdc78e..96bd339bb1851d 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -454,6 +454,8 @@ LANGOPT(MatrixTypes, 1, 0, "Enable or disable the builtin matrix type") LANGOPT(CXXAssumptions, 1, 1, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute") +LANGOPT(RawStringLiterals, 1, 0, "Enable or disable raw string literals") + ENUM_LANGOPT(StrictFlexArraysLevel, StrictFlexArraysLevelKind, 2, StrictFlexArraysLevelKind::Default, "Rely on strict definition of flexible arrays") diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h index 8e25afc833661c..0a308b93ada746 100644 --- a/clang/include/clang/Basic/LangStandard.h +++ b/clang/include/clang/Basic/LangStandard.h @@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); + } + /// isGNUMode - Language includes GNU extensions. bool isGNUMode() const { return Flags & GNUMode; } diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f745e573eb2686..32e6c10e1251b7 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4142,6 +4142,12 @@ def fenable_matrix : Flag<["-"], "fenable-matrix">, Group, HelpText<"Enable matrix data type and related builtin functions">, MarshallingInfoFlag>; +defm raw_string_literals : BoolFOption<"raw-string-literals", +LangOpts<"RawStringLiteral
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
llvmbot wrote: @llvm/pr-subscribers-clang-driver Author: None (Sirraide) Changes This is a tentative implementation of support for raw string literals in C following the discussion on #85703. GCC supports raw string literals in C in `-gnuXY` mode. This pr both enables raw string literals in `-gnuXY` mode in C and adds a `-f[no-]raw-string-literals` flag to override this beheviour. There are a few questions I still have though: 1. GCC does not seem to support raw string literals in C++ before C++11, even if e.g. `-std=gnu++03` is passed. Should we follow this behaviour or should we enable raw string literals in earlier C++ language modes as well if `-gnu++XY` is passed? `-fraw-string-literals` currently makes it possible to enable them in e.g. C++03. 2. `-fno-raw-string-literals` allows users to *disable* raw string literals in `-gnuXY` mode. I thought it might be useful to have this, but do we want it? 3. The implementation of this currently adds a `RawStringLiterals` option to the LangOpts; `-f[no-]raw-string-literals` overrides the default value for it which depends on the language standard. As a consequence, passing e.g. `-std=c++11 -fno-raw-string-literals` will *disable* raw string literals even though we’re in C++11 mode. Do we want to allow this or should we just ignore `-f[no-]raw-string-literals` if we’re in C++11 or later? 4. This probably deserves a note in `LanguageExtensions.rst`, but I’m not exactly sure where. 5. Should we add a flag for this to `__has_feature`/`__has_extension`? --- Full diff: https://github.com/llvm/llvm-project/pull/88265.diff 9 Files Affected: - (modified) clang/docs/ReleaseNotes.rst (+3) - (modified) clang/include/clang/Basic/LangOptions.def (+2) - (modified) clang/include/clang/Basic/LangStandard.h (+6) - (modified) clang/include/clang/Driver/Options.td (+6) - (modified) clang/lib/Basic/LangOptions.cpp (+1) - (modified) clang/lib/Driver/ToolChains/Clang.cpp (+2) - (modified) clang/lib/Format/Format.cpp (+1) - (modified) clang/lib/Lex/Lexer.cpp (+5-5) - (added) clang/test/Lexer/raw-string-ext.c (+18) ``diff diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f96cebbde3d825..20d14130fb62bc 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -43,6 +43,9 @@ code bases. C/C++ Language Potentially Breaking Changes --- +- Clang now supports raw string literals in ``-std=gnuXY`` mode as an extension in + C. This behaviour can also be overridden using ``-f[no-]raw-string-literals``. + C++ Specific Potentially Breaking Changes - - Clang now diagnoses function/variable templates that shadow their own template parameters, e.g. ``template void T();``. diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 8ef6700ecdc78e..96bd339bb1851d 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -454,6 +454,8 @@ LANGOPT(MatrixTypes, 1, 0, "Enable or disable the builtin matrix type") LANGOPT(CXXAssumptions, 1, 1, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute") +LANGOPT(RawStringLiterals, 1, 0, "Enable or disable raw string literals") + ENUM_LANGOPT(StrictFlexArraysLevel, StrictFlexArraysLevelKind, 2, StrictFlexArraysLevelKind::Default, "Rely on strict definition of flexible arrays") diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h index 8e25afc833661c..0a308b93ada746 100644 --- a/clang/include/clang/Basic/LangStandard.h +++ b/clang/include/clang/Basic/LangStandard.h @@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); + } + /// isGNUMode - Language includes GNU extensions. bool isGNUMode() const { return Flags & GNUMode; } diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f745e573eb2686..32e6c10e1251b7 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4142,6 +4142,12 @@ def fenable_matrix : Flag<["-"], "fenable-matrix">, Group, HelpText<"Enable matrix data type and related builtin functions">, MarshallingInfoFlag>; +defm raw_string_literals : BoolFOption<"raw-string-literals", +LangOpts<"RawStringLiterals">, Default, +PosFlag, +NegFlag, +BothFlags<[], [ClangOption, CC1Option], " raw string literals">>; + def fzero_call_used_regs_EQ : Joined<["-"], "fzero-call-used-regs=">, Group, Visibility<
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
jansvoboda11 wrote: > To clarify, that means setting the `RawStringLiterals` LangOpt in > `DependencyDirectivesScanner.cpp`, right? I’m assuming yes, but I just want > to make sure. Yes, that's what I had in mind 👍 https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/88265 >From 40e533e6f58acbe832b3fa4e14ca9fd600cf77cf Mon Sep 17 00:00:00 2001 From: Sirraide Date: Wed, 10 Apr 2024 14:36:23 +0200 Subject: [PATCH 1/7] [Clang] Allow raw string literals in C as an extension --- clang/docs/ReleaseNotes.rst | 3 +++ clang/include/clang/Basic/LangOptions.def | 2 ++ clang/include/clang/Basic/LangStandard.h | 6 ++ clang/include/clang/Driver/Options.td | 6 ++ clang/lib/Basic/LangOptions.cpp | 1 + clang/lib/Driver/ToolChains/Clang.cpp | 2 ++ clang/lib/Format/Format.cpp | 1 + clang/lib/Lex/Lexer.cpp | 10 +- clang/test/Lexer/raw-string-ext.c | 18 ++ 9 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 clang/test/Lexer/raw-string-ext.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f96cebbde3d82..20d14130fb62b 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -43,6 +43,9 @@ code bases. C/C++ Language Potentially Breaking Changes --- +- Clang now supports raw string literals in ``-std=gnuXY`` mode as an extension in + C. This behaviour can also be overridden using ``-f[no-]raw-string-literals``. + C++ Specific Potentially Breaking Changes - - Clang now diagnoses function/variable templates that shadow their own template parameters, e.g. ``template void T();``. diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 8ef6700ecdc78..96bd339bb1851 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -454,6 +454,8 @@ LANGOPT(MatrixTypes, 1, 0, "Enable or disable the builtin matrix type") LANGOPT(CXXAssumptions, 1, 1, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute") +LANGOPT(RawStringLiterals, 1, 0, "Enable or disable raw string literals") + ENUM_LANGOPT(StrictFlexArraysLevel, StrictFlexArraysLevelKind, 2, StrictFlexArraysLevelKind::Default, "Rely on strict definition of flexible arrays") diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h index 8e25afc833661..0a308b93ada74 100644 --- a/clang/include/clang/Basic/LangStandard.h +++ b/clang/include/clang/Basic/LangStandard.h @@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); + } + /// isGNUMode - Language includes GNU extensions. bool isGNUMode() const { return Flags & GNUMode; } diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f745e573eb268..32e6c10e1251b 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4142,6 +4142,12 @@ def fenable_matrix : Flag<["-"], "fenable-matrix">, Group, HelpText<"Enable matrix data type and related builtin functions">, MarshallingInfoFlag>; +defm raw_string_literals : BoolFOption<"raw-string-literals", +LangOpts<"RawStringLiterals">, Default, +PosFlag, +NegFlag, +BothFlags<[], [ClangOption, CC1Option], " raw string literals">>; + def fzero_call_used_regs_EQ : Joined<["-"], "fzero-call-used-regs=">, Group, Visibility<[ClangOption, CC1Option]>, diff --git a/clang/lib/Basic/LangOptions.cpp b/clang/lib/Basic/LangOptions.cpp index a0adfbf61840e..c34f0ed5ed717 100644 --- a/clang/lib/Basic/LangOptions.cpp +++ b/clang/lib/Basic/LangOptions.cpp @@ -124,6 +124,7 @@ void LangOptions::setLangDefaults(LangOptions &Opts, Language Lang, Opts.HexFloats = Std.hasHexFloats(); Opts.WChar = Std.isCPlusPlus(); Opts.Digraphs = Std.hasDigraphs(); + Opts.RawStringLiterals = Std.hasRawStringLiterals(); Opts.HLSL = Lang == Language::HLSL; if (Opts.HLSL && Opts.IncludeDefaultHeader) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 766a9b91e3c0a..c99bfe4efc413 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6536,6 +6536,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fraw_string_literals, + options::OPT_fno_raw_string_literals); if (Args.hasFlag(options::OPT_f
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: Alright, I just enabled raw string literals in the dependency scanner by default; barring any further complications, I’ll merge this once CI is done. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
benlangmuir wrote: > I assume that @benlangmuir added the scanner unit-test to demonstrate the > current behavior instead of trying to make sure it's preserved. Correct. They're only interesting to the scanner insofar as they're used in `_Pragma()` as far as I know. If we can handle them, great! You just need to update the test expectations for those cases. Thanks for working on this! https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/88265 >From 40e533e6f58acbe832b3fa4e14ca9fd600cf77cf Mon Sep 17 00:00:00 2001 From: Sirraide Date: Wed, 10 Apr 2024 14:36:23 +0200 Subject: [PATCH 1/8] [Clang] Allow raw string literals in C as an extension --- clang/docs/ReleaseNotes.rst | 3 +++ clang/include/clang/Basic/LangOptions.def | 2 ++ clang/include/clang/Basic/LangStandard.h | 6 ++ clang/include/clang/Driver/Options.td | 6 ++ clang/lib/Basic/LangOptions.cpp | 1 + clang/lib/Driver/ToolChains/Clang.cpp | 2 ++ clang/lib/Format/Format.cpp | 1 + clang/lib/Lex/Lexer.cpp | 10 +- clang/test/Lexer/raw-string-ext.c | 18 ++ 9 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 clang/test/Lexer/raw-string-ext.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f96cebbde3d825..20d14130fb62bc 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -43,6 +43,9 @@ code bases. C/C++ Language Potentially Breaking Changes --- +- Clang now supports raw string literals in ``-std=gnuXY`` mode as an extension in + C. This behaviour can also be overridden using ``-f[no-]raw-string-literals``. + C++ Specific Potentially Breaking Changes - - Clang now diagnoses function/variable templates that shadow their own template parameters, e.g. ``template void T();``. diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 8ef6700ecdc78e..96bd339bb1851d 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -454,6 +454,8 @@ LANGOPT(MatrixTypes, 1, 0, "Enable or disable the builtin matrix type") LANGOPT(CXXAssumptions, 1, 1, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute") +LANGOPT(RawStringLiterals, 1, 0, "Enable or disable raw string literals") + ENUM_LANGOPT(StrictFlexArraysLevel, StrictFlexArraysLevelKind, 2, StrictFlexArraysLevelKind::Default, "Rely on strict definition of flexible arrays") diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h index 8e25afc833661c..0a308b93ada746 100644 --- a/clang/include/clang/Basic/LangStandard.h +++ b/clang/include/clang/Basic/LangStandard.h @@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); + } + /// isGNUMode - Language includes GNU extensions. bool isGNUMode() const { return Flags & GNUMode; } diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f745e573eb2686..32e6c10e1251b7 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4142,6 +4142,12 @@ def fenable_matrix : Flag<["-"], "fenable-matrix">, Group, HelpText<"Enable matrix data type and related builtin functions">, MarshallingInfoFlag>; +defm raw_string_literals : BoolFOption<"raw-string-literals", +LangOpts<"RawStringLiterals">, Default, +PosFlag, +NegFlag, +BothFlags<[], [ClangOption, CC1Option], " raw string literals">>; + def fzero_call_used_regs_EQ : Joined<["-"], "fzero-call-used-regs=">, Group, Visibility<[ClangOption, CC1Option]>, diff --git a/clang/lib/Basic/LangOptions.cpp b/clang/lib/Basic/LangOptions.cpp index a0adfbf61840e3..c34f0ed5ed7174 100644 --- a/clang/lib/Basic/LangOptions.cpp +++ b/clang/lib/Basic/LangOptions.cpp @@ -124,6 +124,7 @@ void LangOptions::setLangDefaults(LangOptions &Opts, Language Lang, Opts.HexFloats = Std.hasHexFloats(); Opts.WChar = Std.isCPlusPlus(); Opts.Digraphs = Std.hasDigraphs(); + Opts.RawStringLiterals = Std.hasRawStringLiterals(); Opts.HLSL = Lang == Language::HLSL; if (Opts.HLSL && Opts.IncludeDefaultHeader) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 766a9b91e3c0ad..c99bfe4efc4137 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6536,6 +6536,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fraw_string_literals, + options::OPT_fno_raw_string_literals); if (Args.hasFlag(op
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: Silly me forgot to *actually update the test* after enabling raw string literals in the dependency scanner, but *now* everything should pass. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/Sirraide closed https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/AaronBallman commented: Thank you for working on this! Broadly speaking, I think the idea makes a lot of sense. > GCC does not seem to support raw string literals in C++ before C++11, even > if e.g. -std=gnu++03 is passed. Should we follow this behaviour or should we > enable raw string literals in earlier C++ language modes as well if -gnu++XY > is passed? -fraw-string-literals currently makes it possible to enable them > in e.g. C++03. I think we should follow that behavior; because `R` can be a valid macro identifier, being conservative is defensible. > -fno-raw-string-literals allows users to disable raw string literals in > -gnuXY mode. I thought it might be useful to have this, but do we want it? I think it's reasonable to have it, but I don't think we should allow it for C++11 and later modes unless there's some rationale I'm missing. (I don't think we want to let users disable language features in standards modes where the feature is standardized without some sort of reasonable justification.) > The implementation of this currently adds a RawStringLiterals option to the > LangOpts; -f[no-]raw-string-literals overrides the default value for it which > depends on the language standard. As a consequence, passing e.g. -std=c++11 > -fno-raw-string-literals will disable raw string literals even though we’re > in C++11 mode. Do we want to allow this or should we just ignore > -f[no-]raw-string-literals if we’re in C++11 or later? I think we should either ignore or diagnose it in C++11 or later. > This probably deserves a note in LanguageExtensions.rst, but I’m not exactly > sure where. It definitely should be noted in there; I would probably recommend https://clang.llvm.org/docs/LanguageExtensions.html#c-11-raw-string-literals for the C++ side of things and then something similar for C around where we document those. > Should we add a flag for this to __has_feature/__has_extension? Yes, but it's a fun question as to which one. We currently use `__has_feature` for it in C++: ``` FEATURE(cxx_raw_string_literals, LangOpts.CPlusPlus11) ``` and it seems like it would make sense to continue to do so for C++. But this isn't a language feature of C, so `__has_extension` makes sense there. But that's confusing because then we've got both, so I'm not entirely certain that's the right approach. Perhaps using `__has_feature` for both C and C++ makes the most sense? https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/AaronBallman edited https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -43,6 +43,9 @@ code bases. C/C++ Language Potentially Breaking Changes --- +- Clang now supports raw string literals in ``-std=gnuXY`` mode as an extension in + C. This behaviour can also be overridden using ``-f[no-]raw-string-literals``. AaronBallman wrote: This should mention #85703 somewhere https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); AaronBallman wrote: GCC doesn't allow them in gnu89 mode: https://godbolt.org/z/hahja6Y87 If I had to hazard a guess as to why, my guess is that there are concerns about `R` conflicting with a user-defined macro. The same concern exists in all versions of C, but code explicitly compiling in C89 mode can be a signal the code isn't being actively maintained in some regard. But still, this is a bit of a surprise, so it might be nice to understand better why the discrepancy exists. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); Sirraide wrote: I’ll file a GCC bug report about it. In the meantime, should we be conservative and disallow them in gnu89 mode? https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); Sirraide wrote: Alright, filed a bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114773 https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); Sirraide wrote: Update: This seems to be intended, so I suppose we should only support them in `-gnu99` mode and later. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); AaronBallman wrote: okay, so my guess was correct. It's still odd though because that same logic applies to C99 (it's "old" and the behavior can still change). But yeah, we should follow the same behavior as GCC in this case (less confusion for users). https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: > I don't think we should allow it for C++11 and later modes To clarify, should we allow enabling them in e.g. `c++03` mode if `-fraw-string-literals` is passed? I don’t see why not, but I’m not entirely sure whether you’re saying we should not support that flag in C++ (neither the positive nor the negative variant) at all or just not in C++11 and later. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/88265 >From 40e533e6f58acbe832b3fa4e14ca9fd600cf77cf Mon Sep 17 00:00:00 2001 From: Sirraide Date: Wed, 10 Apr 2024 14:36:23 +0200 Subject: [PATCH 1/2] [Clang] Allow raw string literals in C as an extension --- clang/docs/ReleaseNotes.rst | 3 +++ clang/include/clang/Basic/LangOptions.def | 2 ++ clang/include/clang/Basic/LangStandard.h | 6 ++ clang/include/clang/Driver/Options.td | 6 ++ clang/lib/Basic/LangOptions.cpp | 1 + clang/lib/Driver/ToolChains/Clang.cpp | 2 ++ clang/lib/Format/Format.cpp | 1 + clang/lib/Lex/Lexer.cpp | 10 +- clang/test/Lexer/raw-string-ext.c | 18 ++ 9 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 clang/test/Lexer/raw-string-ext.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f96cebbde3d825..20d14130fb62bc 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -43,6 +43,9 @@ code bases. C/C++ Language Potentially Breaking Changes --- +- Clang now supports raw string literals in ``-std=gnuXY`` mode as an extension in + C. This behaviour can also be overridden using ``-f[no-]raw-string-literals``. + C++ Specific Potentially Breaking Changes - - Clang now diagnoses function/variable templates that shadow their own template parameters, e.g. ``template void T();``. diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 8ef6700ecdc78e..96bd339bb1851d 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -454,6 +454,8 @@ LANGOPT(MatrixTypes, 1, 0, "Enable or disable the builtin matrix type") LANGOPT(CXXAssumptions, 1, 1, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute") +LANGOPT(RawStringLiterals, 1, 0, "Enable or disable raw string literals") + ENUM_LANGOPT(StrictFlexArraysLevel, StrictFlexArraysLevelKind, 2, StrictFlexArraysLevelKind::Default, "Rely on strict definition of flexible arrays") diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h index 8e25afc833661c..0a308b93ada746 100644 --- a/clang/include/clang/Basic/LangStandard.h +++ b/clang/include/clang/Basic/LangStandard.h @@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); + } + /// isGNUMode - Language includes GNU extensions. bool isGNUMode() const { return Flags & GNUMode; } diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f745e573eb2686..32e6c10e1251b7 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4142,6 +4142,12 @@ def fenable_matrix : Flag<["-"], "fenable-matrix">, Group, HelpText<"Enable matrix data type and related builtin functions">, MarshallingInfoFlag>; +defm raw_string_literals : BoolFOption<"raw-string-literals", +LangOpts<"RawStringLiterals">, Default, +PosFlag, +NegFlag, +BothFlags<[], [ClangOption, CC1Option], " raw string literals">>; + def fzero_call_used_regs_EQ : Joined<["-"], "fzero-call-used-regs=">, Group, Visibility<[ClangOption, CC1Option]>, diff --git a/clang/lib/Basic/LangOptions.cpp b/clang/lib/Basic/LangOptions.cpp index a0adfbf61840e3..c34f0ed5ed7174 100644 --- a/clang/lib/Basic/LangOptions.cpp +++ b/clang/lib/Basic/LangOptions.cpp @@ -124,6 +124,7 @@ void LangOptions::setLangDefaults(LangOptions &Opts, Language Lang, Opts.HexFloats = Std.hasHexFloats(); Opts.WChar = Std.isCPlusPlus(); Opts.Digraphs = Std.hasDigraphs(); + Opts.RawStringLiterals = Std.hasRawStringLiterals(); Opts.HLSL = Lang == Language::HLSL; if (Opts.HLSL && Opts.IncludeDefaultHeader) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 766a9b91e3c0ad..c99bfe4efc4137 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6536,6 +6536,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fraw_string_literals, + options::OPT_fno_raw_string_literals); if (Args.hasFlag(op
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
AaronBallman wrote: > > I don't think we should allow it for C++11 and later modes > > To clarify, should we allow enabling them in e.g. `c++03` mode if > `-fraw-string-literals` is passed? I don’t see why not, but I’m not entirely > sure whether you’re saying we should not support that flag in C++ (neither > the positive nor the negative variant) at all or just not in C++11 and later. I think we should allow users to enable them in C++03 modes if `-fraw-string-literals` is passed. I think it's fine to have `-fno-raw-string-literals` that allows users to disable them in C++03 mode (in case an earlier command line option opted into them and the user wants to disable the feature for some reason), but I don't know if that's worth the effort to support because I don't think we should allow `-fno-raw-string-literals` in C++11 mode. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
AaronBallman wrote: Btw, it seems that precommit CI found some valid issues to be addressed https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: > I think we should allow users to enable them in C++03 modes if > -fraw-string-literals is passed. I think it's fine to have > -fno-raw-string-literals that allows users to disable them in C++03 mode (in > case an earlier command line option opted into them and the user wants to > disable the feature for some reason), but I don't know if that's worth the > effort to support because I don't think we should allow > -fno-raw-string-literals in C++11 mode. In that case I think it might just make sense to ignore the flag in C++11 and later then and allow it before C++11. > Btw, it seems that precommit CI found some valid issues to be addressed Ah, I didn’t know that all new warnings should have a `-W` flag associated with them; it seems I’ve somehow only ever added errors so far. I’ll take a look at what happened there. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -3850,6 +3850,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { // the sequence "<::" will be unconditionally treated as "[:". // Cf. Lexer::LexTokenInternal. LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; + LangOpts.RawStringLiterals = LexingStd >= FormatStyle::LS_Cpp11; Sirraide wrote: That seems to have worked https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: @AaronBallman I just noticed something that I’ve somehow not realised until now even though I’d already written a test case for it: Not only does GCC allow raw string literals in gnuXY mode, but also UTF string literals, e.g. `u"foo"` (https://godbolt.org/z/771s8ne5d). Should we follow suit here? And if so, should we add a separate flag for that or rename `fraw-string-literals` (and the LangOption) to `fext-string-literals` or something similar? https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -3850,6 +3850,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { // the sequence "<::" will be unconditionally treated as "[:". // Cf. Lexer::LexTokenInternal. LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; + LangOpts.RawStringLiterals = LexingStd >= FormatStyle::LS_Cpp11; owenca wrote: As clang-format formats all C code as C++, there's no need to set the new `RawStringLiterals` option here. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -3850,6 +3850,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { // the sequence "<::" will be unconditionally treated as "[:". // Cf. Lexer::LexTokenInternal. LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; + LangOpts.RawStringLiterals = LexingStd >= FormatStyle::LS_Cpp11; Sirraide wrote: A bunch of the clang-format tests were failing before I made this change, but I think that may have been before I made some more changes to this; I’ll take another look at this. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -3850,6 +3850,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { // the sequence "<::" will be unconditionally treated as "[:". // Cf. Lexer::LexTokenInternal. LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; + LangOpts.RawStringLiterals = LexingStd >= FormatStyle::LS_Cpp11; owenca wrote: Can we set the default to 1 in `LangOpts.def`? That would take care of it. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -3850,6 +3850,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { // the sequence "<::" will be unconditionally treated as "[:". // Cf. Lexer::LexTokenInternal. LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; + LangOpts.RawStringLiterals = LexingStd >= FormatStyle::LS_Cpp11; Sirraide wrote: That sounds like it might work because it’s overridden by the standard anyway; I’ll try that. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
AaronBallman wrote: C has Unicode string literals as well: https://godbolt.org/z/chdjYrK9v and so if we're allowing raw string literals, it makes sense to also allow raw unicode string literals IMO. I don't think we need to rename the flag though. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
AaronBallman wrote: > In that case I think it might just make sense to ignore the flag in C++11 and > later then and allow it before C++11. I think that makes the most sense. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -3850,6 +3850,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { // the sequence "<::" will be unconditionally treated as "[:". // Cf. Lexer::LexTokenInternal. LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; + LangOpts.RawStringLiterals = LexingStd >= FormatStyle::LS_Cpp11; owenca wrote: So can undo the change to Format.cpp? https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
@@ -3850,6 +3850,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { // the sequence "<::" will be unconditionally treated as "[:". // Cf. Lexer::LexTokenInternal. LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; + LangOpts.RawStringLiterals = LexingStd >= FormatStyle::LS_Cpp11; Sirraide wrote: I think I’ve already done that; I just haven’t commited that yet iirc. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/Sirraide edited https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
cor3ntin wrote: > C has Unicode string literals as well: https://godbolt.org/z/chdjYrK9v and so > if we're allowing raw string literals, it makes sense to also allow raw > unicode string literals IMO. I don't think we need to rename the flag though. Yes, these things are completely orthogonal, it makes no sense to treat raw strings with an encoding prefix differently https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/88265 >From 40e533e6f58acbe832b3fa4e14ca9fd600cf77cf Mon Sep 17 00:00:00 2001 From: Sirraide Date: Wed, 10 Apr 2024 14:36:23 +0200 Subject: [PATCH 1/5] [Clang] Allow raw string literals in C as an extension --- clang/docs/ReleaseNotes.rst | 3 +++ clang/include/clang/Basic/LangOptions.def | 2 ++ clang/include/clang/Basic/LangStandard.h | 6 ++ clang/include/clang/Driver/Options.td | 6 ++ clang/lib/Basic/LangOptions.cpp | 1 + clang/lib/Driver/ToolChains/Clang.cpp | 2 ++ clang/lib/Format/Format.cpp | 1 + clang/lib/Lex/Lexer.cpp | 10 +- clang/test/Lexer/raw-string-ext.c | 18 ++ 9 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 clang/test/Lexer/raw-string-ext.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f96cebbde3d82..20d14130fb62b 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -43,6 +43,9 @@ code bases. C/C++ Language Potentially Breaking Changes --- +- Clang now supports raw string literals in ``-std=gnuXY`` mode as an extension in + C. This behaviour can also be overridden using ``-f[no-]raw-string-literals``. + C++ Specific Potentially Breaking Changes - - Clang now diagnoses function/variable templates that shadow their own template parameters, e.g. ``template void T();``. diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 8ef6700ecdc78..96bd339bb1851 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -454,6 +454,8 @@ LANGOPT(MatrixTypes, 1, 0, "Enable or disable the builtin matrix type") LANGOPT(CXXAssumptions, 1, 1, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute") +LANGOPT(RawStringLiterals, 1, 0, "Enable or disable raw string literals") + ENUM_LANGOPT(StrictFlexArraysLevel, StrictFlexArraysLevelKind, 2, StrictFlexArraysLevelKind::Default, "Rely on strict definition of flexible arrays") diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h index 8e25afc833661..0a308b93ada74 100644 --- a/clang/include/clang/Basic/LangStandard.h +++ b/clang/include/clang/Basic/LangStandard.h @@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); + } + /// isGNUMode - Language includes GNU extensions. bool isGNUMode() const { return Flags & GNUMode; } diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f745e573eb268..32e6c10e1251b 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4142,6 +4142,12 @@ def fenable_matrix : Flag<["-"], "fenable-matrix">, Group, HelpText<"Enable matrix data type and related builtin functions">, MarshallingInfoFlag>; +defm raw_string_literals : BoolFOption<"raw-string-literals", +LangOpts<"RawStringLiterals">, Default, +PosFlag, +NegFlag, +BothFlags<[], [ClangOption, CC1Option], " raw string literals">>; + def fzero_call_used_regs_EQ : Joined<["-"], "fzero-call-used-regs=">, Group, Visibility<[ClangOption, CC1Option]>, diff --git a/clang/lib/Basic/LangOptions.cpp b/clang/lib/Basic/LangOptions.cpp index a0adfbf61840e..c34f0ed5ed717 100644 --- a/clang/lib/Basic/LangOptions.cpp +++ b/clang/lib/Basic/LangOptions.cpp @@ -124,6 +124,7 @@ void LangOptions::setLangDefaults(LangOptions &Opts, Language Lang, Opts.HexFloats = Std.hasHexFloats(); Opts.WChar = Std.isCPlusPlus(); Opts.Digraphs = Std.hasDigraphs(); + Opts.RawStringLiterals = Std.hasRawStringLiterals(); Opts.HLSL = Lang == Language::HLSL; if (Opts.HLSL && Opts.IncludeDefaultHeader) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 766a9b91e3c0a..c99bfe4efc413 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6536,6 +6536,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fraw_string_literals, + options::OPT_fno_raw_string_literals); if (Args.hasFlag(options::OPT_f
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: Alright, I think this has the behaviour that we want now: - raw string literals are enabled in C++11 and later, as well as in C in gnuXY mode; - raw string literals can be explicitly enabled or disabled in C (and in C++ standards before C++11) using `-f[no-]raw-string-literals`; - in C++11 and later `-f[no-]raw-string-literals` is ignored and a warning is issued. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: So, apparently, this test here https://github.com/llvm/llvm-project/blob/cb76896d6e45e2c9b7ef5e47b6ec37aeca43f7a8/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp#L586-L589 is now failing, presumably because of this: https://github.com/llvm/llvm-project/blob/cb76896d6e45e2c9b7ef5e47b6ec37aeca43f7a8/clang/lib/Lex/DependencyDirectivesScanner.cpp#L71-L79 I’m not entirely sure how to fix this candidly. It doesn’t look like unconditionally enabling raw string literals is an option here... This situation reminds me of a similar issue we’re having with `'` in numeric literals (#88896). I’m personally not too familiar with the lexer, but would it be possible to pass through the original lang options here from wherever this is invoked? https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/88265 >From 40e533e6f58acbe832b3fa4e14ca9fd600cf77cf Mon Sep 17 00:00:00 2001 From: Sirraide Date: Wed, 10 Apr 2024 14:36:23 +0200 Subject: [PATCH 1/6] [Clang] Allow raw string literals in C as an extension --- clang/docs/ReleaseNotes.rst | 3 +++ clang/include/clang/Basic/LangOptions.def | 2 ++ clang/include/clang/Basic/LangStandard.h | 6 ++ clang/include/clang/Driver/Options.td | 6 ++ clang/lib/Basic/LangOptions.cpp | 1 + clang/lib/Driver/ToolChains/Clang.cpp | 2 ++ clang/lib/Format/Format.cpp | 1 + clang/lib/Lex/Lexer.cpp | 10 +- clang/test/Lexer/raw-string-ext.c | 18 ++ 9 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 clang/test/Lexer/raw-string-ext.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f96cebbde3d82..20d14130fb62b 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -43,6 +43,9 @@ code bases. C/C++ Language Potentially Breaking Changes --- +- Clang now supports raw string literals in ``-std=gnuXY`` mode as an extension in + C. This behaviour can also be overridden using ``-f[no-]raw-string-literals``. + C++ Specific Potentially Breaking Changes - - Clang now diagnoses function/variable templates that shadow their own template parameters, e.g. ``template void T();``. diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 8ef6700ecdc78..96bd339bb1851 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -454,6 +454,8 @@ LANGOPT(MatrixTypes, 1, 0, "Enable or disable the builtin matrix type") LANGOPT(CXXAssumptions, 1, 1, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute") +LANGOPT(RawStringLiterals, 1, 0, "Enable or disable raw string literals") + ENUM_LANGOPT(StrictFlexArraysLevel, StrictFlexArraysLevelKind, 2, StrictFlexArraysLevelKind::Default, "Rely on strict definition of flexible arrays") diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h index 8e25afc833661..0a308b93ada74 100644 --- a/clang/include/clang/Basic/LangStandard.h +++ b/clang/include/clang/Basic/LangStandard.h @@ -130,6 +130,12 @@ struct LangStandard { /// hasDigraphs - Language supports digraphs. bool hasDigraphs() const { return Flags & Digraphs; } + /// hasRawStringLiterals - Language supports R"()" raw string literals. + bool hasRawStringLiterals() const { +// GCC supports raw string literals in C, but not in C++ before C++11. +return isCPlusPlus11() || (!isCPlusPlus() && isGNUMode()); + } + /// isGNUMode - Language includes GNU extensions. bool isGNUMode() const { return Flags & GNUMode; } diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f745e573eb268..32e6c10e1251b 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4142,6 +4142,12 @@ def fenable_matrix : Flag<["-"], "fenable-matrix">, Group, HelpText<"Enable matrix data type and related builtin functions">, MarshallingInfoFlag>; +defm raw_string_literals : BoolFOption<"raw-string-literals", +LangOpts<"RawStringLiterals">, Default, +PosFlag, +NegFlag, +BothFlags<[], [ClangOption, CC1Option], " raw string literals">>; + def fzero_call_used_regs_EQ : Joined<["-"], "fzero-call-used-regs=">, Group, Visibility<[ClangOption, CC1Option]>, diff --git a/clang/lib/Basic/LangOptions.cpp b/clang/lib/Basic/LangOptions.cpp index a0adfbf61840e..c34f0ed5ed717 100644 --- a/clang/lib/Basic/LangOptions.cpp +++ b/clang/lib/Basic/LangOptions.cpp @@ -124,6 +124,7 @@ void LangOptions::setLangDefaults(LangOptions &Opts, Language Lang, Opts.HexFloats = Std.hasHexFloats(); Opts.WChar = Std.isCPlusPlus(); Opts.Digraphs = Std.hasDigraphs(); + Opts.RawStringLiterals = Std.hasRawStringLiterals(); Opts.HLSL = Lang == Language::HLSL; if (Opts.HLSL && Opts.IncludeDefaultHeader) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 766a9b91e3c0a..c99bfe4efc413 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6536,6 +6536,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fraw_string_literals, + options::OPT_fno_raw_string_literals); if (Args.hasFlag(options::OPT_f
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
AaronBallman wrote: > raw string literals are enabled in C++11 and later, as well as in C in gnu99 > mode and later; Why gnu99 mode and not gnu89 mode? I see GCC has that behavior, but I'm not certain why. > I’m not entirely sure how to fix this candidly. It doesn’t look like > unconditionally enabling raw string literals is an option here... This > situation reminds me of a similar issue we’re having with ' in numeric > literals (#88896). I’m personally not too familiar with the lexer, but would > it be possible to pass through the original lang options here from wherever > this is invoked? Yeah, it's pretty frustrating that we've found two instances of this in such a short period of time. :-/ That test was added in https://github.com/llvm/llvm-project/commit/ee8ed0b3099e63ba0a18cca42b9cfdf098bc6201 and it seems to be a bit of a drive-by as the author noticed the behavior. Given that dependency scanning is never going to care about raw string literals to begin with (at least that I can think of), I'm not certain there's any harm in always supporting raw string literals from dependency scanning, so we could probably do that in the worst case. But my concerns from https://github.com/llvm/llvm-project/pull/93753#issuecomment-217302 are still relevant too. CC @jansvoboda11 https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: > Why gnu99 mode and not gnu89 mode? I see GCC has that behavior, but I'm not > certain why. We went over this a while back: https://github.com/llvm/llvm-project/pull/88265#discussion_r1571088366 > I'm not certain there's any harm in always supporting raw string literals > from dependency scanning, so we could probably do that in the worst case. But my concerns from https://github.com/llvm/llvm-project/pull/93753#issuecomment-217302 are still relevant too. CC @jansvoboda11 :+1: https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
AaronBallman wrote: > > Why gnu99 mode and not gnu89 mode? I see GCC has that behavior, but I'm not > > certain why. > > We went over this a while back: [#88265 > (comment)](https://github.com/llvm/llvm-project/pull/88265#discussion_r1571088366) THAT is why this was so familiar to me! :-D Thanks! https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
https://github.com/AaronBallman approved this pull request. Clang changes LGTM modulo the dependency scanner bits. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: > Clang changes LGTM modulo the dependency scanner bits. Alright, I’ll wait for a reply from @jansvoboda11 then https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
jansvoboda11 wrote: I assume that @benlangmuir added the scanner unit-test to demonstrate the current behavior instead of trying to make sure it's preserved. I think making it so that the test passes (actually handles raw string literals) and updating the FIXME in `DependencyDirectivesScanner.cpp` should be fine. CC @Bigcheese https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Allow raw string literals in C as an extension (PR #88265)
Sirraide wrote: > I think making it so that the test passes (actually handles raw string > literals) and updating the FIXME in DependencyDirectivesScanner.cpp should be > fine To clarify, that means setting the `RawStringLiterals` LangOpt in `DependencyDirectivesScanner.cpp`, right? I’m assuming yes, but I just want to make sure. https://github.com/llvm/llvm-project/pull/88265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits