abseil-faster-strsplit-delimiter.cpp
4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-faster-strsplit-delimiter %t
// FIXME: Fix the checker to work in C++17 mode.
namespace absl {
class string_view {
public:
string_view();
string_view(const char *);
};
namespace strings_internal {
struct Splitter {};
struct MaxSplitsImpl {
MaxSplitsImpl();
~MaxSplitsImpl();
};
} //namespace strings_internal
template <typename Delim>
strings_internal::Splitter StrSplit(absl::string_view, Delim) {
return {};
}
template <typename Delim, typename Pred>
strings_internal::Splitter StrSplit(absl::string_view, Delim, Pred) {
return {};
}
class ByAnyChar {
public:
explicit ByAnyChar(absl::string_view);
~ByAnyChar();
};
template <typename Delim>
strings_internal::MaxSplitsImpl MaxSplits(Delim, int) {
return {};
}
} //namespace absl
void SplitDelimiters() {
absl::StrSplit("ABC", "A");
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit() called with a string literal consisting of a single character; consider using the character overload [abseil-faster-strsplit-delimiter]
// CHECK-FIXES: absl::StrSplit("ABC", 'A');
absl::StrSplit("ABC", "\x01");
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit() called with a string literal consisting of a single character; consider using the character overload [abseil-faster-strsplit-delimiter]
// CHECK-FIXES: absl::StrSplit("ABC", '\x01');
absl::StrSplit("ABC", "\001");
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit() called with a string literal consisting of a single character; consider using the character overload [abseil-faster-strsplit-delimiter]
// CHECK-FIXES: absl::StrSplit("ABC", '\001');
absl::StrSplit("ABC", R"(A)");
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit() called with a string literal consisting of a single character; consider using the character overload [abseil-faster-strsplit-delimiter]
// CHECK-FIXES: absl::StrSplit("ABC", 'A');
absl::StrSplit("ABC", R"(')");
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit() called with a string literal consisting of a single character; consider using the character overload [abseil-faster-strsplit-delimiter]
// CHECK-FIXES: absl::StrSplit("ABC", '\'');
absl::StrSplit("ABC", R"(
)");
// CHECK-MESSAGES: [[@LINE-2]]:25: warning: absl::StrSplit() called with a string literal consisting of a single character; consider using the character overload [abseil-faster-strsplit-delimiter]
// CHECK-FIXES: absl::StrSplit("ABC", '\n');
absl::StrSplit("ABC", R"delimiter(A)delimiter");
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit() called with a string literal consisting of a single character; consider using the character overload [abseil-faster-strsplit-delimiter]
// CHECK-FIXES: absl::StrSplit("ABC", 'A');
absl::StrSplit("ABC", absl::ByAnyChar("\n"));
// CHECK-MESSAGES: [[@LINE-1]]:41: warning: absl::StrSplit()
// CHECK-FIXES: absl::StrSplit("ABC", '\n');
// Works with predicate
absl::StrSplit("ABC", "A", [](absl::string_view) { return true; });
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit()
// CHECK-FIXES: absl::StrSplit("ABC", 'A', [](absl::string_view) { return true; });
// Doesn't do anything with other strings lenghts.
absl::StrSplit("ABC", "AB");
absl::StrSplit("ABC", absl::ByAnyChar(""));
absl::StrSplit("ABC", absl::ByAnyChar(" \t"));
// Escapes a single quote in the resulting character literal.
absl::StrSplit("ABC", "'");
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit()
// CHECK-FIXES: absl::StrSplit("ABC", '\'');
absl::StrSplit("ABC", "\"");
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit()
// CHECK-FIXES: absl::StrSplit("ABC", '\"');
absl::StrSplit("ABC", absl::MaxSplits("\t", 1));
// CHECK-MESSAGES: [[@LINE-1]]:41: warning: absl::MaxSplits()
// CHECK-FIXES: absl::StrSplit("ABC", absl::MaxSplits('\t', 1));
auto delim = absl::MaxSplits(absl::ByAnyChar(" "), 1);
// CHECK-MESSAGES: [[@LINE-1]]:48: warning: absl::MaxSplits()
// CHECK-FIXES: auto delim = absl::MaxSplits(' ', 1);
}
#define MACRO(str) absl::StrSplit("ABC", str)
void Macro() {
MACRO("A");
}
template <typename T>
void FunctionTemplate() {
// This one should not warn because ByAnyChar is a dependent type.
absl::StrSplit("TTT", T("A"));
// This one will warn, but we are checking that we get a correct warning only
// once.
absl::StrSplit("TTT", "A");
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: absl::StrSplit()
// CHECK-FIXES: absl::StrSplit("TTT", 'A');
}
void FunctionTemplateCaller() {
FunctionTemplate<absl::ByAnyChar>();
FunctionTemplate<absl::string_view>();
}