Skip to content

Commit c644be1

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

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class RawStringLiteral : public Tweak {
4343

4444
REGISTER_TWEAK(RawStringLiteral)
4545

46+
static bool isFeatureAvailable(const Tweak::Selection &Inputs) {
47+
// Raw strings are available only for C++11 or later versions, and they are
48+
// not available for C.
49+
return Inputs.AST->getASTContext().getLangOpts().CPlusPlus11;
50+
}
51+
4652
static bool isNormalString(const StringLiteral &Str, SourceLocation Cursor,
4753
SourceManager &SM) {
4854
// All chunks must be normal ASCII strings, not u8"..." etc.
@@ -76,7 +82,7 @@ bool RawStringLiteral::prepare(const Selection &Inputs) {
7682
if (!N)
7783
return false;
7884
Str = dyn_cast_or_null<StringLiteral>(N->ASTNode.get<Stmt>());
79-
return Str &&
85+
return Str && isFeatureAvailable(Inputs) &&
8086
isNormalString(*Str, Inputs.Cursor, Inputs.AST->getSourceManager()) &&
8187
needsRaw(Str->getBytes()) && canBeRaw(Str->getBytes());
8288
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ 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(
44+
const char* a = "^f^o^o^\^n^";
45+
)c");
46+
}
47+
48+
TEST_F(RawStringLiteralTest, TestCpp98) {
49+
Context = File;
50+
ExtraArgs = {"-std=c++98"}; // raw strings are unavailable
51+
// in versions prior to C++11
52+
EXPECT_UNAVAILABLE(R"cpp(
53+
const char* a = "^f^o^o^\^n^";
54+
)cpp");
55+
}
56+
3957
} // namespace
4058
} // namespace clangd
4159
} // namespace clang

0 commit comments

Comments
 (0)