[clang-tools-extra] 2f3d4f6 - [clangd] Do not offer RawStringLiteral code action in C and C++98 (#69775)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 24 14:29:47 PDT 2023


Author: robozati
Date: 2023-10-24T17:29:43-04:00
New Revision: 2f3d4f6c9829bfc17a3e2d034286c03940d506ec

URL: https://github.com/llvm/llvm-project/commit/2f3d4f6c9829bfc17a3e2d034286c03940d506ec
DIFF: https://github.com/llvm/llvm-project/commit/2f3d4f6c9829bfc17a3e2d034286c03940d506ec.diff

LOG: [clangd] Do not offer RawStringLiteral code action in C and C++98 (#69775)

Raw string literals are a C++ feature first added in C++11.

Fixes https://github.com/clangd/clangd/issues/1795.

Added: 
    

Modified: 
    clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
    clang-tools-extra/clangd/unittests/tweaks/RawStringLiteralTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp b/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
index f5021b820f38d7f..5613ceacdfe1bfc 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,6 +44,12 @@ 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) {
   // All chunks must be normal ASCII strings, not u8"..." etc.
@@ -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