Skip to content

Commit 2baffdb

Browse files
author
Carlos Gálvez
committed
[clang-tidy] Detect string literals in macros in modernize-raw-string-literal
Fixes #133618
1 parent 4e4cb43 commit 2baffdb

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool containsEscapedCharacters(const MatchFinder::MatchResult &Result,
5858
*Result.SourceManager, Result.Context->getLangOpts());
5959
StringRef Text = Lexer::getSourceText(CharRange, *Result.SourceManager,
6060
Result.Context->getLangOpts());
61-
if (Text.empty() || isRawStringLiteral(Text))
61+
if (Text.empty() || !Text.contains('"') || isRawStringLiteral(Text))
6262
return false;
6363

6464
return containsEscapes(Text, R"('\"?x01)");
@@ -156,14 +156,14 @@ static bool compareStringLength(StringRef Replacement,
156156
const SourceManager &SM,
157157
const LangOptions &LangOpts) {
158158
return Replacement.size() <=
159-
Lexer::MeasureTokenLength(Literal->getBeginLoc(), SM, LangOpts);
159+
Lexer::MeasureTokenLength(SM.getSpellingLoc(Literal->getBeginLoc()), SM, LangOpts);
160160
}
161161

162162
void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) {
163163
const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("lit");
164-
if (Literal->getBeginLoc().isMacroID())
165-
return;
166164
const SourceManager &SM = *Result.SourceManager;
165+
if (SM.getSpellingLoc(Literal->getBeginLoc()).isMacroID())
166+
return;
167167
const LangOptions &LangOpts = getLangOpts();
168168
if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
169169
const std::string Replacement =
@@ -172,7 +172,8 @@ void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) {
172172
compareStringLength(Replacement, Literal, SM, LangOpts)) {
173173
diag(Literal->getBeginLoc(),
174174
"escaped string literal can be written as a raw string literal")
175-
<< FixItHint::CreateReplacement(Literal->getSourceRange(),
175+
<< FixItHint::CreateReplacement(SourceRange(SM.getSpellingLoc(Literal->getBeginLoc()),
176+
SM.getSpellingLoc(Literal->getEndLoc())),
176177
Replacement);
177178
}
178179
}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ Changes in existing checks
181181
<clang-tidy/checks/modernize/use-std-numbers>` check to support math
182182
functions of different precisions.
183183

184+
- Improved :doc:`modernize-raw-string-literal
185+
<clang-tidy/checks/modernize/raw-string-literal>` check to detect string
186+
literals passed into macros.
187+
184188
- Improved :doc:`performance-move-const-arg
185189
<clang-tidy/checks/performance/move-const-arg>` check by fixing false
186190
negatives on ternary operators calling ``std::move``.

clang-tools-extra/test/clang-tidy/checkers/modernize/raw-string-literal.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ char const *const StringizedMacroArgument = HAT(foo\\bar);
105105

106106
#define SUBST(lit_) lit_
107107
char const *const MacroArgument = SUBST("foo\\bar");
108-
// FIXME: We should be able to replace this string literal macro argument
108+
// CHECK-MESSAGES: :[[@LINE-1]]:41: warning: {{.*}} can be written as a raw string literal
109+
// CHECK-FIXES: {{^}}char const *const MacroArgument = SUBST(R"(foo\bar)");{{$}}
109110

110111
template <typename T>
111112
void fn(char const *const Arg) {

0 commit comments

Comments
 (0)