From e46468407a7bb7f8b2fe13675a5a1c32b85f8cad Mon Sep 17 00:00:00 2001 From: Sirraide Date: Wed, 10 Jul 2024 12:10:44 +0200 Subject: [Clang] Allow raw string literals in C as an extension (#88265) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This enables raw R"" string literals in C in some language modes and adds an option to disable or enable them explicitly as an extension. Background: GCC supports raw string literals in C in `-gnuXY` modes starting with gnu99. This pr both enables raw string literals in gnu99 mode and later in C and adds an `-f[no-]raw-string-literals` flag to override this behaviour. The decision not to enable raw string literals in gnu89 mode, according to the GCC devs, is intentional as that mode is supposed to be used for ‘old code’ that they don’t want to break; we’ve decided to match GCC’s behaviour here as well. The `-fraw-string-literals` flag can additionally be used to enable raw string literals in modes where they aren’t enabled by default (such as c99—as opposed to gnu99—or even e.g. C++03); conversely, the negated flag can be used to disable them in any gnuXY modes that *do* provide them by default, or to override a previous flag. However, we do *not* support disabling raw string literals (or indeed either of these two options) in C++11 mode and later, because we don’t want to just start supporting disabling features that are actually part of the language in the general case. This fixes #85703. --- clang/lib/Frontend/CompilerInvocation.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'clang/lib/Frontend/CompilerInvocation.cpp') diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index f42e28b..0082c15 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -610,6 +610,19 @@ static bool FixupInvocation(CompilerInvocation &Invocation, LangOpts.NewAlignOverride = 0; } + // The -f[no-]raw-string-literals option is only valid in C and in C++ + // standards before C++11. + if (LangOpts.CPlusPlus11) { + if (Args.hasArg(OPT_fraw_string_literals, OPT_fno_raw_string_literals)) { + Args.claimAllArgs(OPT_fraw_string_literals, OPT_fno_raw_string_literals); + Diags.Report(diag::warn_drv_fraw_string_literals_in_cxx11) + << bool(LangOpts.RawStringLiterals); + } + + // Do not allow disabling raw string literals in C++11 or later. + LangOpts.RawStringLiterals = true; + } + // Prevent the user from specifying both -fsycl-is-device and -fsycl-is-host. if (LangOpts.SYCLIsDevice && LangOpts.SYCLIsHost) Diags.Report(diag::err_drv_argument_not_allowed_with) << "-fsycl-is-device" -- cgit v1.1