Skip to content

Commit d753701

Browse files
committed
Fix RawStringLiteral being available for C and C++ versions prior to C++11
1 parent 71bdd2c commit d753701

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88
#include "ParsedAST.h"
99
#include "refactor/Tweak.h"
10+
#include "clang/AST/ASTContext.h"
1011
#include "clang/AST/Stmt.h"
1112
#include "clang/Basic/LangOptions.h"
1213
#include "clang/Basic/SourceLocation.h"
@@ -43,6 +44,12 @@ class RawStringLiteral : public Tweak {
4344

4445
REGISTER_TWEAK(RawStringLiteral)
4546

47+
static bool isFeatureAvailable(const ASTContext &Context) {
48+
// Raw strings are available only for C++11 or later versions, and they are
49+
// not available for C.
50+
return Context.getLangOpts().CPlusPlus11;
51+
}
52+
4653
static bool isNormalString(const StringLiteral &Str, SourceLocation Cursor,
4754
SourceManager &SM) {
4855
// All chunks must be normal ASCII strings, not u8"..." etc.
@@ -72,6 +79,9 @@ static bool canBeRaw(llvm::StringRef Content) {
7279
}
7380

7481
bool RawStringLiteral::prepare(const Selection &Inputs) {
82+
if (!isFeatureAvailable(Inputs.AST->getASTContext())) {
83+
return false;
84+
}
7585
const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
7686
if (!N)
7787
return false;

clang-tools-extra/clangd/unittests/tweaks/RawStringLiteralTests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ literal)")cpp";
3636
EXPECT_EQ(apply(Input), Output);
3737
}
3838

39+
TEST_F(RawStringLiteralTest, TestC) {
40+
Context = File;
41+
FileName = "TestTU.c";
42+
ExtraArgs = {"-xc"}; // raw strings are unavailable in C
43+
EXPECT_UNAVAILABLE(R"c(const char *a = ^"^f^o^o^\^n^";)c");
44+
}
45+
46+
TEST_F(RawStringLiteralTest, TestCpp98) {
47+
Context = File;
48+
ExtraArgs = {"-std=c++98"}; // raw strings are unavailable
49+
// in versions prior to C++11
50+
EXPECT_UNAVAILABLE(R"cpp(const char *a = ^"^f^o^o^\^n^";)cpp");
51+
}
52+
3953
} // namespace
4054
} // namespace clangd
4155
} // namespace clang

0 commit comments

Comments
 (0)