LLVM: lib/Support/GlobPattern.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

16

17using namespace llvm;

18

19

20

23

24

25 for (;;) {

26 if (S.size() < 3)

27 break;

28

31

32

33

34 if (S[1] != '-') {

35 BV[Start] = true;

37 continue;

38 }

39

40

41

42 if (Start > End)

45

46 for (int C = Start; C <= End; ++C)

49 }

50

51 for (char C : S)

53 return BV;

54}

55

56

57

61 if (!MaxSubPatterns || !S.contains('{'))

62 return std::move(SubPatterns);

63

64 struct BraceExpansion {

65 size_t Start;

68 };

70

71 BraceExpansion *CurrentBE = nullptr;

72 size_t TermBegin;

73 for (size_t I = 0, E = S.size(); I != E; ++I) {

74 if (S[I] == '[') {

75 I = S.find(']', I + 2);

76 if (I == std:🧵:npos)

79 } else if (S[I] == '{') {

80 if (CurrentBE)

82 "nested brace expansions are not supported",

85 CurrentBE->Start = I;

86 TermBegin = I + 1;

87 } else if (S[I] == ',') {

88 if (!CurrentBE)

89 continue;

90 CurrentBE->Terms.push_back(S.substr(TermBegin, I - TermBegin));

91 TermBegin = I + 1;

92 } else if (S[I] == '}') {

93 if (!CurrentBE)

94 continue;

95 if (CurrentBE->Terms.empty())

97 "empty or singleton brace expansions are not supported",

99 CurrentBE->Terms.push_back(S.substr(TermBegin, I - TermBegin));

100 CurrentBE->Length = I - CurrentBE->Start + 1;

101 CurrentBE = nullptr;

102 } else if (S[I] == '\\') {

103 if (++I == E)

106 }

107 }

108 if (CurrentBE)

111

112 size_t NumSubPatterns = 1;

113 for (auto &BE : BraceExpansions) {

114 if (NumSubPatterns > std::numeric_limits<size_t>::max() / BE.Terms.size()) {

115 NumSubPatterns = std::numeric_limits<size_t>::max();

116 break;

117 }

118 NumSubPatterns *= BE.Terms.size();

119 }

120 if (NumSubPatterns > *MaxSubPatterns)

123

124

125 for (auto &BE : reverse(BraceExpansions)) {

127 std::swap(SubPatterns, OrigSubPatterns);

129 for (StringRef Orig : OrigSubPatterns)

130 SubPatterns.emplace_back(Orig).replace(BE.Start, BE.Length, Term);

131 }

132 return std::move(SubPatterns);

133}

134

137 while (!S.empty()) {

139 if (PrefixSize == std:🧵:npos)

140 PrefixSize = S.size();

141

142 if (Best.size() < PrefixSize)

144

146

147

148

149

151

152 switch (S.front()) {

153 case '\\':

155 break;

156 case '[': {

157

160

161

162 assert(EndBracket != std:🧵:npos);

164 break;

165 }

166 case '{':

167

168

169 return Best;

170 default:

172 }

173 }

174

175 return Best;

176}

177

181 Pat.Pattern = S;

182

183

185 if (Pat.PrefixSize == std:🧵:npos) {

186 Pat.PrefixSize = S.size();

187 return Pat;

188 }

189 S = S.substr(Pat.PrefixSize);

190

191

200

203 return std::move(Err);

204 for (StringRef SubPat : SubPats) {

205 auto SubGlobOrErr = SubGlobPattern::create(SubPat);

206 if (!SubGlobOrErr)

207 return SubGlobOrErr.takeError();

208 Pat.SubGlobs.push_back(*SubGlobOrErr);

209 }

210

211 return Pat;

212}

213

215GlobPattern::SubGlobPattern::create(StringRef S) {

216 SubGlobPattern Pat;

217

218

220 for (size_t I = 0, E = S.size(); I != E; ++I) {

221 if (S[I] == '[') {

222

223

224 ++I;

225 size_t J = S.find(']', I + 1);

230 bool Invert = S[I] == '^' || S[I] == '!';

233 if (!BV)

235 if (Invert)

236 BV->flip();

237 Pat.Brackets.push_back(Bracket{J + 1, std::move(*BV)});

238 I = J;

239 } else if (S[I] == '\\') {

240 if (++I == E)

243 }

244 }

245 return Pat;

246}

247

250 Pattern.drop_front(PrefixSize).drop_back(SuffixSize));

251}

252

255 return false;

257 return false;

258 if (SubGlobs.empty() && S.empty())

259 return true;

260 for (auto &Glob : SubGlobs)

261 if (Glob.match(S))

262 return true;

263 return false;

264}

265

266

267

268

269bool GlobPattern::SubGlobPattern::match(StringRef Str) const {

270 const char *P = Pat.data(), *SegmentBegin = nullptr, *S = Str.data(),

271 *SavedS = S;

272 const char *const PEnd = P + Pat.size(), *const End = S + Str.size();

273 size_t B = 0, SavedB = 0;

274 while (S != End) {

275 if (P == PEnd)

276 ;

277 else if (*P == '*') {

278

279

280 SegmentBegin = ++P;

281 SavedS = S;

282 SavedB = B;

283 continue;

284 } else if (*P == '[') {

285 if (Brackets[B].Bytes[uint8_t(*S)]) {

286 P = Pat.data() + Brackets[B++].NextOffset;

287 ++S;

288 continue;

289 }

290 } else if (*P == '\\') {

291 if (*++P == *S) {

292 ++P;

293 ++S;

294 continue;

295 }

296 } else if (*P == *S || *P == '?') {

297 ++P;

298 ++S;

299 continue;

300 }

301 if (!SegmentBegin)

302 return false;

303

304

305 P = SegmentBegin;

306 S = ++SavedS;

307 B = SavedB;

308 }

309

310

311 return getPat().find_first_not_of('*', P - Pat.data()) == std:🧵:npos;

312}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")

static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")

constexpr char SuffixStart

static StringRef maxPlainSubstring(StringRef S)

Definition GlobPattern.cpp:135

static Expected< SmallVector< std::string, 1 > > parseBraceExpansions(StringRef S, std::optional< size_t > MaxSubPatterns)

Definition GlobPattern.cpp:59

static Expected< BitVector > expand(StringRef S, StringRef Original)

Definition GlobPattern.cpp:21

Tagged union holding either a T or a Error.

Error takeError()

Take ownership of the stored error.

This class implements a glob pattern matcher similar to the one found in bash, but with some key diff...

LLVM_ABI_FOR_TEST StringRef longest_substr() const

Definition GlobPattern.cpp:248

LLVM_ABI bool match(StringRef S) const

Definition GlobPattern.cpp:253

static LLVM_ABI Expected< GlobPattern > create(StringRef Pat, std::optional< size_t > MaxSubPatterns={})

Definition GlobPattern.cpp:179

void assign(size_type NumElts, ValueParamT Elt)

reference emplace_back(ArgTypes &&... Args)

void push_back(const T &Elt)

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

StringRef - Represent a constant reference to a string, i.e.

static constexpr size_t npos

bool consume_back(StringRef Suffix)

Returns true if this StringRef has the given suffix and removes that suffix.

std::string str() const

str - Get the contents as an std::string.

constexpr StringRef substr(size_t Start, size_t N=npos) const

Return a reference to the substring from [Start, Start + N).

constexpr bool empty() const

empty - Check if the string is empty.

StringRef drop_front(size_t N=1) const

Return a StringRef equal to 'this' but with the first N elements dropped.

constexpr size_t size() const

size - Get the string size.

char front() const

front - Get the first character in the string.

size_t find_last_of(char C, size_t From=npos) const

Find the last character in the string that is C, or npos if not found.

constexpr const char * data() const

data - Get a pointer to the start of the string (which may not be null terminated).

bool contains(StringRef Other) const

Return true if the given string is a substring of *this, and false otherwise.

bool consume_front(StringRef Prefix)

Returns true if this StringRef has the given prefix and removes that prefix.

size_t find_first_of(char C, size_t From=0) const

Find the first character in the string that is C, or npos if not found.

StringRef take_front(size_t N=1) const

Return a StringRef equal to 'this' but with only the first N elements remaining.

size_t find(char C, size_t From=0) const

Search for the first character C in the string.

@ C

The default llvm calling convention, compatible with C.

This is an optimization pass for GlobalISel generic memory operations.

auto reverse(ContainerTy &&C)

Error make_error(ArgTs &&... Args)

Make a Error instance representing failure using the given error info type.

void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)

Implement std::swap in terms of BitVector swap.