File: faster-strsplit-delimiter.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (124 lines) | stat: -rw-r--r-- 4,650 bytes parent folder | download | duplicates (12)
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
// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-faster-strsplit-delimiter %t

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>();
}