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)

43 return make_error("invalid glob pattern: " + Original,

44 errc::invalid_argument);

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)

77 return make_error("invalid glob pattern, unmatched '['",

78 errc::invalid_argument);

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

80 if (CurrentBE)

81 return make_error(

82 "nested brace expansions are not supported",

83 errc::invalid_argument);

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

96 return make_error(

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

98 errc::invalid_argument);

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)

104 return make_error("invalid glob pattern, stray '\\'",

105 errc::invalid_argument);

106 }

107 }

108 if (CurrentBE)

109 return make_error("incomplete brace expansion",

110 errc::invalid_argument);

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)

121 return make_error("too many brace expansions",

122 errc::invalid_argument);

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

138

139

141 Pat.Prefix = S.substr(0, PrefixSize);

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

143 return Pat;

144 S = S.substr(PrefixSize);

145

148 return std::move(Err);

149 for (StringRef SubPat : SubPats) {

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

151 if (!SubGlobOrErr)

152 return SubGlobOrErr.takeError();

153 Pat.SubGlobs.push_back(*SubGlobOrErr);

154 }

155

156 return Pat;

157}

158

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

161 SubGlobPattern Pat;

162

163

164 Pat.Pat.assign(S.begin(), S.end());

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

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

167

168

169 ++I;

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

172 return make_error("invalid glob pattern, unmatched '['",

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

178 if (!BV)

180 if (Invert)

181 BV->flip();

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

183 I = J;

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

185 if (++I == E)

186 return make_error("invalid glob pattern, stray '\\'",

188 }

189 }

190 return Pat;

191}

192

195 return false;

197 return true;

198 for (auto &Glob : SubGlobs)

199 if (Glob.match(S))

200 return true;

201 return false;

202}

203

204

205

206

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

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

209 *SavedS = S;

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

211 size_t B = 0, SavedB = 0;

212 while (S != End) {

213 if (P == PEnd)

214 ;

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

216

217

218 SegmentBegin = ++P;

219 SavedS = S;

220 SavedB = B;

221 continue;

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

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

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

225 ++S;

226 continue;

227 }

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

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

230 ++P;

231 ++S;

232 continue;

233 }

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

235 ++P;

236 ++S;

237 continue;

238 }

239 if (!SegmentBegin)

240 return false;

241

242

243 P = SegmentBegin;

244 S = ++SavedS;

245 B = SavedB;

246 }

247

248

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

250}

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

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

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

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...

bool match(StringRef S) const

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

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.

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.

constexpr size_t size() const

size - Get the string size.

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.

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

Search for the first character C in the string.

static constexpr size_t npos

@ C

The default llvm calling convention, compatible with C.

This is an optimization pass for GlobalISel generic memory operations.

auto reverse(ContainerTy &&C)

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

Implement std::swap in terms of BitVector swap.