[clang-tools-extra] [clangd] Fix RawStringLiteral being available to C and C++ versions prior to C++11 (PR #69775)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 23 07:24:01 PDT 2023


https://github.com/robozati updated https://github.com/llvm/llvm-project/pull/69775

>From e8eb79384dcd90c8a2e96a3e4e8c35bb0f42031c Mon Sep 17 00:00:00 2001
From: Gabriel Pezati <gabrielpezati at gmail.com>
Date: Mon, 23 Oct 2023 11:26:47 -0300
Subject: [PATCH] Fix RawStringLiteral being available to C and C++ versions
 prior to C++11

---
 .../clangd/refactor/tweaks/RawStringLiteral.cpp    | 14 ++++++++++++--
 .../unittests/tweaks/RawStringLiteralTests.cpp     | 14 ++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp b/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
index f5021b820f38d7f..3188b8ef55598ac 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 #include "ParsedAST.h"
 #include "refactor/Tweak.h"
+#include "clang/AST/ASTContext.h"
 #include "clang/AST/Stmt.h"
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/SourceLocation.h"
@@ -43,8 +44,14 @@ class RawStringLiteral : public Tweak {
 
 REGISTER_TWEAK(RawStringLiteral)
 
+static bool isFeatureAvailable(const ASTContext &Context) {
+  // Raw strings are available only for C++11 or later versions, and they are
+  // not available for C.
+  return Context.getLangOpts().CPlusPlus11;
+}
+
 static bool isNormalString(const StringLiteral &Str, SourceLocation Cursor,
-                          SourceManager &SM) {
+                           SourceManager &SM) {
   // All chunks must be normal ASCII strings, not u8"..." etc.
   if (!Str.isOrdinary())
     return false;
@@ -56,7 +63,7 @@ static bool isNormalString(const StringLiteral &Str, SourceLocation Cursor,
       LastTokenBeforeCursor = *I;
   }
   // Token we care about must be a normal "string": not raw, u8, etc.
-  const char* Data = SM.getCharacterData(LastTokenBeforeCursor);
+  const char *Data = SM.getCharacterData(LastTokenBeforeCursor);
   return Data && *Data == '"';
 }
 
@@ -72,6 +79,9 @@ static bool canBeRaw(llvm::StringRef Content) {
 }
 
 bool RawStringLiteral::prepare(const Selection &Inputs) {
+  if (!isFeatureAvailable(Inputs.AST->getASTContext())) {
+    return false;
+  }
   const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
   if (!N)
     return false;
diff --git a/clang-tools-extra/clangd/unittests/tweaks/RawStringLiteralTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/RawStringLiteralTests.cpp
index 4bc304559705031..2681b8c815dbae3 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/RawStringLiteralTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/RawStringLiteralTests.cpp
@@ -36,6 +36,20 @@ literal)")cpp";
   EXPECT_EQ(apply(Input), Output);
 }
 
+TEST_F(RawStringLiteralTest, TestC) {
+  Context = File;
+  FileName = "TestTU.c";
+  ExtraArgs = {"-xc"}; // raw strings are unavailable in C
+  EXPECT_UNAVAILABLE(R"c(const char *a = ^"^f^o^o^\^n^";)c");
+}
+
+TEST_F(RawStringLiteralTest, TestCpp98) {
+  Context = File;
+  ExtraArgs = {"-std=c++98"}; // raw strings are unavailable
+                              // in versions prior to C++11
+  EXPECT_UNAVAILABLE(R"cpp(const char *a = ^"^f^o^o^\^n^";)cpp");
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang



More information about the cfe-commits mailing list