Skip to content

Commit 4d48996

Browse files
authored
[clang-format][NFC] Clean up around StringRef initializations (#149765)
Consistently use `constexpr StringRef Code("string literal");`.
1 parent fcdcc4e commit 4d48996

12 files changed

+76
-76
lines changed

clang/lib/Format/BreakableToken.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace clang {
2626
namespace format {
2727

28-
static constexpr StringRef Blanks = " \t\v\f\r";
28+
static constexpr StringRef Blanks(" \t\v\f\r");
2929

3030
static StringRef getLineCommentIndentPrefix(StringRef Comment,
3131
const FormatStyle &Style) {
@@ -513,7 +513,7 @@ BreakableBlockComment::BreakableBlockComment(
513513
Decoration = "";
514514
}
515515
for (size_t i = 1, e = Content.size(); i < e && !Decoration.empty(); ++i) {
516-
const StringRef &Text = Content[i];
516+
const StringRef Text(Content[i]);
517517
if (i + 1 == e) {
518518
// If the last line is empty, the closing "*/" will have a star.
519519
if (Text.empty())

clang/lib/Format/IntegerLiteralSeparatorFixer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace format {
1919

2020
enum class Base { Binary, Decimal, Hex, Other };
2121

22-
static Base getBase(const StringRef IntegerLiteral) {
22+
static Base getBase(StringRef IntegerLiteral) {
2323
assert(IntegerLiteral.size() > 1);
2424

2525
if (IntegerLiteral[0] > '0') {
@@ -164,8 +164,8 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
164164
return {Result, 0};
165165
}
166166

167-
bool IntegerLiteralSeparatorFixer::checkSeparator(
168-
const StringRef IntegerLiteral, int DigitsPerGroup) const {
167+
bool IntegerLiteralSeparatorFixer::checkSeparator(StringRef IntegerLiteral,
168+
int DigitsPerGroup) const {
169169
assert(DigitsPerGroup > 0);
170170

171171
int I = 0;
@@ -184,7 +184,7 @@ bool IntegerLiteralSeparatorFixer::checkSeparator(
184184
return true;
185185
}
186186

187-
std::string IntegerLiteralSeparatorFixer::format(const StringRef IntegerLiteral,
187+
std::string IntegerLiteralSeparatorFixer::format(StringRef IntegerLiteral,
188188
int DigitsPerGroup,
189189
int DigitCount,
190190
bool RemoveSeparator) const {

clang/lib/Format/IntegerLiteralSeparatorFixer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class IntegerLiteralSeparatorFixer {
2626
const FormatStyle &Style);
2727

2828
private:
29-
bool checkSeparator(const StringRef IntegerLiteral, int DigitsPerGroup) const;
30-
std::string format(const StringRef IntegerLiteral, int DigitsPerGroup,
29+
bool checkSeparator(StringRef IntegerLiteral, int DigitsPerGroup) const;
30+
std::string format(StringRef IntegerLiteral, int DigitsPerGroup,
3131
int DigitCount, bool RemoveSeparator) const;
3232

3333
char Separator;

clang/lib/Format/ObjCPropertyAttributeOrderFixer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void ObjCPropertyAttributeOrderFixer::sortPropertyAttributes(
6666
return;
6767
}
6868

69-
const StringRef Attribute{Tok->TokenText};
69+
const StringRef Attribute(Tok->TokenText);
7070
StringRef Value;
7171

7272
// Also handle `getter=getFoo` attributes.

clang/unittests/Format/BracesInserterTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ TEST_F(BracesInserterTest, InsertBracesRange) {
257257
FormatStyle Style = getLLVMStyle();
258258
Style.InsertBraces = true;
259259

260-
const StringRef Code("while (a)\n"
261-
" if (b)\n"
262-
" return;");
260+
constexpr StringRef Code("while (a)\n"
261+
" if (b)\n"
262+
" return;");
263263

264264
verifyFormat("while (a) {\n"
265265
" if (b)\n"

clang/unittests/Format/FormatTest.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4803,12 +4803,13 @@ TEST_F(FormatTest, FormatsInlineASM) {
48034803
"int i;");
48044804

48054805
auto Style = getLLVMStyleWithColumns(0);
4806-
const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"};
4807-
const StringRef Code2{"asm(\"xyz\"\n"
4808-
" : \"=a\"(a), \"=d\"(b)\n"
4809-
" : \"a\"(data));"};
4810-
const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4811-
" : \"a\"(data));"};
4806+
constexpr StringRef Code1(
4807+
"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4808+
constexpr StringRef Code2("asm(\"xyz\"\n"
4809+
" : \"=a\"(a), \"=d\"(b)\n"
4810+
" : \"a\"(data));");
4811+
constexpr StringRef Code3("asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4812+
" : \"a\"(data));");
48124813

48134814
Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
48144815
verifyFormat(Code1, Style);
@@ -6704,16 +6705,16 @@ TEST_F(FormatTest, EscapedNewlines) {
67046705
" int x(int a);",
67056706
AlignLeft);
67066707

6707-
constexpr StringRef Code{"#define A \\\n"
6708+
constexpr StringRef Code("#define A \\\n"
67086709
" int a123; \\\n"
67096710
" int a; \\\n"
6710-
" int a1234;"};
6711+
" int a1234;");
67116712
verifyFormat(Code, AlignLeft);
67126713

6713-
constexpr StringRef Code2{"#define A \\\n"
6714+
constexpr StringRef Code2("#define A \\\n"
67146715
" int a123; \\\n"
67156716
" int a; \\\n"
6716-
" int a1234;"};
6717+
" int a1234;");
67176718
auto LastLine = getLLVMStyle();
67186719
LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine;
67196720
verifyFormat(Code2, LastLine);
@@ -12108,9 +12109,9 @@ TEST_F(FormatTest, PointerAlignmentFallback) {
1210812109
FormatStyle Style = getLLVMStyle();
1210912110
Style.DerivePointerAlignment = true;
1211012111

12111-
const StringRef Code("int* p;\n"
12112-
"int *q;\n"
12113-
"int * r;");
12112+
constexpr StringRef Code("int* p;\n"
12113+
"int *q;\n"
12114+
"int * r;");
1211412115

1211512116
EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
1211612117
verifyFormat("int *p;\n"
@@ -15025,7 +15026,7 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1502515026
" aaaaaaaaaaaaaaaaaa,\n"
1502615027
" aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
1502715028

15028-
constexpr StringRef Code{"void foo() { /* Empty */ }"};
15029+
constexpr StringRef Code("void foo() { /* Empty */ }");
1502915030
verifyFormat(Code);
1503015031
verifyFormat(Code, "void foo() { /* Empty */\n"
1503115032
"}");
@@ -27255,7 +27256,7 @@ TEST_F(FormatTest, IndentAccessModifiers) {
2725527256

2725627257
TEST_F(FormatTest, LimitlessStringsAndComments) {
2725727258
auto Style = getLLVMStyleWithColumns(0);
27258-
constexpr StringRef Code =
27259+
constexpr StringRef Code(
2725927260
"/**\n"
2726027261
" * This is a multiline comment with quite some long lines, at least for "
2726127262
"the LLVM Style.\n"
@@ -27276,7 +27277,7 @@ TEST_F(FormatTest, LimitlessStringsAndComments) {
2727627277
" const std::string SmallString = \"Hello World\";\n"
2727727278
" // Small line comment\n"
2727827279
" return String.size() > SmallString.size();\n"
27279-
"}";
27280+
"}");
2728027281
verifyNoChange(Code, Style);
2728127282
}
2728227283

@@ -28400,9 +28401,9 @@ TEST_F(FormatTest, InsertNewlineAtEOF) {
2840028401
verifyNoChange("int i;\n", Style);
2840128402
verifyFormat("int i;\n", "int i;", Style);
2840228403

28403-
constexpr StringRef Code{"namespace {\n"
28404+
constexpr StringRef Code("namespace {\n"
2840428405
"int i;\n"
28405-
"} // namespace"};
28406+
"} // namespace");
2840628407
verifyFormat(Code.str() + '\n', Code, Style,
2840728408
{tooling::Range(19, 13)}); // line 3
2840828409
}
@@ -28411,7 +28412,7 @@ TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
2841128412
FormatStyle Style = getLLVMStyle();
2841228413
Style.KeepEmptyLines.AtEndOfFile = true;
2841328414

28414-
const StringRef Code{"int i;\n\n"};
28415+
constexpr StringRef Code("int i;\n\n");
2841528416
verifyNoChange(Code, Style);
2841628417
verifyFormat(Code, "int i;\n\n\n", Style);
2841728418
}
@@ -28644,8 +28645,8 @@ TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
2864428645
}
2864528646

2864628647
TEST_F(FormatTest, BreakAdjacentStringLiterals) {
28647-
constexpr StringRef Code{
28648-
"return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
28648+
constexpr StringRef Code(
28649+
"return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";");
2864928650

2865028651
verifyFormat("return \"Code\"\n"
2865128652
" \"\\0\\52\\26\\55\\55\\0\"\n"
@@ -29056,9 +29057,9 @@ TEST_F(FormatTest, KeepFormFeed) {
2905629057
auto Style = getLLVMStyle();
2905729058
Style.KeepFormFeed = true;
2905829059

29059-
constexpr StringRef NoFormFeed{"int i;\n"
29060+
constexpr StringRef NoFormFeed("int i;\n"
2906029061
"\n"
29061-
"void f();"};
29062+
"void f();");
2906229063
verifyFormat(NoFormFeed,
2906329064
"int i;\n"
2906429065
" \f\n"
@@ -29080,9 +29081,9 @@ TEST_F(FormatTest, KeepFormFeed) {
2908029081
"void f();\f",
2908129082
Style);
2908229083

29083-
constexpr StringRef FormFeed{"int i;\n"
29084+
constexpr StringRef FormFeed("int i;\n"
2908429085
"\f\n"
29085-
"void f();"};
29086+
"void f();");
2908629087
verifyNoChange(FormFeed, Style);
2908729088

2908829089
Style.LineEnding = FormatStyle::LE_LF;
@@ -29092,10 +29093,10 @@ TEST_F(FormatTest, KeepFormFeed) {
2909229093
"void f();",
2909329094
Style);
2909429095

29095-
constexpr StringRef FormFeedBeforeEmptyLine{"int i;\n"
29096+
constexpr StringRef FormFeedBeforeEmptyLine("int i;\n"
2909629097
"\f\n"
2909729098
"\n"
29098-
"void f();"};
29099+
"void f();");
2909929100
Style.MaxEmptyLinesToKeep = 2;
2910029101
verifyFormat(FormFeedBeforeEmptyLine,
2910129102
"int i;\n"

clang/unittests/Format/FormatTestComments.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,11 +1120,11 @@ TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
11201120
" }\n"
11211121
"}"));
11221122

1123-
const StringRef Code("void func() {\n"
1124-
" // clang-format off\n"
1125-
" #define KV(value) #value, value\n"
1126-
" // clang-format on\n"
1127-
"}");
1123+
constexpr StringRef Code("void func() {\n"
1124+
" // clang-format off\n"
1125+
" #define KV(value) #value, value\n"
1126+
" // clang-format on\n"
1127+
"}");
11281128
verifyNoChange(Code);
11291129

11301130
auto Style = getLLVMStyle();

clang/unittests/Format/FormatTestJava.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,17 +631,17 @@ TEST_F(FormatTestJava, SwitchExpression) {
631631
"});",
632632
Style);
633633

634-
constexpr StringRef Code1{"i = switch (day) {\n"
634+
constexpr StringRef Code1("i = switch (day) {\n"
635635
" case THURSDAY, SATURDAY -> 8;\n"
636636
" case WEDNESDAY -> 9;\n"
637637
" default -> 0;\n"
638-
"};"};
638+
"};");
639639
verifyFormat(Code1, Style);
640640

641641
Style.IndentCaseLabels = true;
642642
verifyFormat(Code1, Style);
643643

644-
constexpr StringRef Code2{"i = switch (day) {\n"
644+
constexpr StringRef Code2("i = switch (day) {\n"
645645
" case THURSDAY, SATURDAY -> {\n"
646646
" foo();\n"
647647
" yield 8;\n"
@@ -653,17 +653,17 @@ TEST_F(FormatTestJava, SwitchExpression) {
653653
" default -> {\n"
654654
" yield 0;\n"
655655
" }\n"
656-
"};"};
656+
"};");
657657
verifyFormat(Code2, Style);
658658

659659
Style.IndentCaseLabels = false;
660660
verifyFormat(Code2, Style);
661661

662-
constexpr StringRef Code3{"switch (day) {\n"
662+
constexpr StringRef Code3("switch (day) {\n"
663663
"case THURSDAY, SATURDAY -> i = 8;\n"
664664
"case WEDNESDAY -> i = 9;\n"
665665
"default -> i = 0;\n"
666-
"};"};
666+
"};");
667667
verifyFormat(Code3, Style);
668668

669669
Style.IndentCaseLabels = true;

clang/unittests/Format/FormatTestSelective.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -672,15 +672,14 @@ TEST_F(FormatTestSelective, FormatMacroRegardlessOfPreviousIndent) {
672672
// need to be adapted.
673673
Style = getLLVMStyle();
674674

675-
const StringRef Code{" class Foo {\n"
676-
" void test() {\n"
677-
" #ifdef 1\n"
678-
" #define some\n" // format this line
679-
" #endif\n"
680-
" }};"};
681-
682-
EXPECT_EQ(Style.IndentPPDirectives,
683-
FormatStyle::PPDirectiveIndentStyle::PPDIS_None);
675+
constexpr StringRef Code(" class Foo {\n"
676+
" void test() {\n"
677+
" #ifdef 1\n"
678+
" #define some\n" // format this line
679+
" #endif\n"
680+
" }};");
681+
682+
EXPECT_EQ(Style.IndentPPDirectives, FormatStyle::PPDIS_None);
684683
EXPECT_EQ(" class Foo {\n"
685684
" void test() {\n"
686685
" #ifdef 1\n"

clang/unittests/Format/IntegerLiteralSeparatorTest.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) {
2424
EXPECT_EQ(Style.IntegerLiteralSeparator.Decimal, 0);
2525
EXPECT_EQ(Style.IntegerLiteralSeparator.Hex, 0);
2626

27-
const StringRef Binary("b = 0b10011'11'0110'1u;");
27+
constexpr StringRef Binary("b = 0b10011'11'0110'1u;");
2828
verifyFormat(Binary, Style);
2929
Style.IntegerLiteralSeparator.Binary = -1;
3030
verifyFormat("b = 0b100111101101u;", Binary, Style);
@@ -33,14 +33,14 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) {
3333
Style.IntegerLiteralSeparator.Binary = 4;
3434
verifyFormat("b = 0b1001'1110'1101u;", Binary, Style);
3535

36-
const StringRef Decimal("d = 184467'440737'0'95505'92Ull;");
36+
constexpr StringRef Decimal("d = 184467'440737'0'95505'92Ull;");
3737
verifyFormat(Decimal, Style);
3838
Style.IntegerLiteralSeparator.Decimal = -1;
3939
verifyFormat("d = 18446744073709550592Ull;", Decimal, Style);
4040
Style.IntegerLiteralSeparator.Decimal = 3;
4141
verifyFormat("d = 18'446'744'073'709'550'592Ull;", Decimal, Style);
4242

43-
const StringRef Hex("h = 0xDEAD'BEEF'DE'AD'BEE'Fuz;");
43+
constexpr StringRef Hex("h = 0xDEAD'BEEF'DE'AD'BEE'Fuz;");
4444
verifyFormat(Hex, Style);
4545
Style.IntegerLiteralSeparator.Hex = -1;
4646
verifyFormat("h = 0xDEADBEEFDEADBEEFuz;", Hex, Style);
@@ -87,9 +87,9 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) {
8787

8888
TEST_F(IntegerLiteralSeparatorTest, UnderscoreAsSeparator) {
8989
FormatStyle Style = getLLVMStyle();
90-
const StringRef Binary("B = 0B10011_11_0110_1;");
91-
const StringRef Decimal("d = 184467_440737_0_95505_92;");
92-
const StringRef Hex("H = 0XDEAD_BEEF_DE_AD_BEE_F;");
90+
constexpr StringRef Binary("B = 0B10011_11_0110_1;");
91+
constexpr StringRef Decimal("d = 184467_440737_0_95505_92;");
92+
constexpr StringRef Hex("H = 0XDEAD_BEEF_DE_AD_BEE_F;");
9393

9494
auto TestUnderscore = [&](auto Language) {
9595
Style.Language = Language;
@@ -173,16 +173,16 @@ TEST_F(IntegerLiteralSeparatorTest, FixRanges) {
173173
FormatStyle Style = getLLVMStyle();
174174
Style.IntegerLiteralSeparator.Decimal = 3;
175175

176-
const StringRef Code("i = -12'34;\n"
177-
"// clang-format off\n"
178-
"j = 123'4;\n"
179-
"// clang-format on\n"
180-
"k = +1'23'4;");
181-
const StringRef Expected("i = -1'234;\n"
176+
constexpr StringRef Code("i = -12'34;\n"
182177
"// clang-format off\n"
183178
"j = 123'4;\n"
184179
"// clang-format on\n"
185-
"k = +1'234;");
180+
"k = +1'23'4;");
181+
constexpr StringRef Expected("i = -1'234;\n"
182+
"// clang-format off\n"
183+
"j = 123'4;\n"
184+
"// clang-format on\n"
185+
"k = +1'234;");
186186

187187
verifyFormat(Expected, Code, Style);
188188

0 commit comments

Comments
 (0)