[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:33:05 PDT 2023
https://github.com/robozati updated https://github.com/llvm/llvm-project/pull/69775
>From d7537012cc7adea063338ea8ede9a2e0f7decb50 Mon Sep 17 00:00:00 2001
From: Gabriel Pezati <gabrielpezati at gmail.com>
Date: Mon, 23 Oct 2023 11:36:19 -0300
Subject: [PATCH] Fix RawStringLiteral being available for C and C++ versions
prior to C++11
---
.../clangd/refactor/tweaks/RawStringLiteral.cpp | 10 ++++++++++
.../unittests/tweaks/RawStringLiteralTests.cpp | 14 ++++++++++++++
2 files changed, 24 insertions(+)
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