2016-12-21 07:09:09 +08:00
|
|
|
//===-- GlobPattern.cpp - Glob pattern matcher implementation -------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-12-21 07:09:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a glob pattern matcher.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/GlobPattern.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static bool hasWildcard(StringRef S) {
|
Reapply r375051: [support] GlobPattern: add support for `\` and `[!...]`, and allow `]` in more places
Reland r375051 (reverted in r375052) after fixing lld tests on Windows in r375126 and r375131.
Original description: Update GlobPattern in libSupport to handle a few more cases. It does not fully match the `fnmatch` used by GNU objcopy since named character classes (e.g. `[[:digit:]]`) are not supported, but this should support most existing use cases (mostly just `*` is what's used anyway).
This will be used to implement the `--wildcard` flag in llvm-objcopy to be more compatible with GNU objcopy.
This is split off of D66613 to land the libSupport changes separately. The llvm-objcopy part will land soon.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: nickdesaulniers, emaste, arichardson, hiraditya, jakehehrlich, abrachet, seiya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66613
llvm-svn: 375149
2019-10-18 02:09:05 +08:00
|
|
|
return S.find_first_of("?*[\\") != StringRef::npos;
|
2016-12-21 07:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expands character ranges and returns a bitmap.
|
|
|
|
// For example, "a-cf-hz" is expanded to "abcfghz".
|
|
|
|
static Expected<BitVector> expand(StringRef S, StringRef Original) {
|
|
|
|
BitVector BV(256, false);
|
|
|
|
|
|
|
|
// Expand X-Y.
|
|
|
|
for (;;) {
|
|
|
|
if (S.size() < 3)
|
|
|
|
break;
|
|
|
|
|
2017-07-31 17:26:50 +08:00
|
|
|
uint8_t Start = S[0];
|
|
|
|
uint8_t End = S[2];
|
|
|
|
|
2016-12-21 07:09:09 +08:00
|
|
|
// If it doesn't start with something like X-Y,
|
|
|
|
// consume the first character and proceed.
|
|
|
|
if (S[1] != '-') {
|
2017-07-31 17:26:50 +08:00
|
|
|
BV[Start] = true;
|
2016-12-21 07:09:09 +08:00
|
|
|
S = S.substr(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It must be in the form of X-Y.
|
|
|
|
// Validate it and then interpret the range.
|
2017-07-31 17:26:50 +08:00
|
|
|
if (Start > End)
|
2016-12-21 07:09:09 +08:00
|
|
|
return make_error<StringError>("invalid glob pattern: " + Original,
|
|
|
|
errc::invalid_argument);
|
|
|
|
|
2017-07-31 17:26:50 +08:00
|
|
|
for (int C = Start; C <= End; ++C)
|
|
|
|
BV[(uint8_t)C] = true;
|
2016-12-21 07:09:09 +08:00
|
|
|
S = S.substr(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (char C : S)
|
2017-07-31 17:26:50 +08:00
|
|
|
BV[(uint8_t)C] = true;
|
2016-12-21 07:09:09 +08:00
|
|
|
return BV;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a scanner for the glob pattern.
|
Reapply r375051: [support] GlobPattern: add support for `\` and `[!...]`, and allow `]` in more places
Reland r375051 (reverted in r375052) after fixing lld tests on Windows in r375126 and r375131.
Original description: Update GlobPattern in libSupport to handle a few more cases. It does not fully match the `fnmatch` used by GNU objcopy since named character classes (e.g. `[[:digit:]]`) are not supported, but this should support most existing use cases (mostly just `*` is what's used anyway).
This will be used to implement the `--wildcard` flag in llvm-objcopy to be more compatible with GNU objcopy.
This is split off of D66613 to land the libSupport changes separately. The llvm-objcopy part will land soon.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: nickdesaulniers, emaste, arichardson, hiraditya, jakehehrlich, abrachet, seiya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66613
llvm-svn: 375149
2019-10-18 02:09:05 +08:00
|
|
|
// A glob pattern token is one of "*", "?", "\", "[<chars>]", "[^<chars>]"
|
|
|
|
// (which is a negative form of "[<chars>]"), "[!<chars>]" (which is
|
|
|
|
// equivalent to "[^<chars>]"), or a non-meta character.
|
2016-12-21 07:09:09 +08:00
|
|
|
// This function returns the first token in S.
|
|
|
|
static Expected<BitVector> scan(StringRef &S, StringRef Original) {
|
|
|
|
switch (S[0]) {
|
|
|
|
case '*':
|
|
|
|
S = S.substr(1);
|
|
|
|
// '*' is represented by an empty bitvector.
|
|
|
|
// All other bitvectors are 256-bit long.
|
|
|
|
return BitVector();
|
|
|
|
case '?':
|
|
|
|
S = S.substr(1);
|
|
|
|
return BitVector(256, true);
|
|
|
|
case '[': {
|
Reapply r375051: [support] GlobPattern: add support for `\` and `[!...]`, and allow `]` in more places
Reland r375051 (reverted in r375052) after fixing lld tests on Windows in r375126 and r375131.
Original description: Update GlobPattern in libSupport to handle a few more cases. It does not fully match the `fnmatch` used by GNU objcopy since named character classes (e.g. `[[:digit:]]`) are not supported, but this should support most existing use cases (mostly just `*` is what's used anyway).
This will be used to implement the `--wildcard` flag in llvm-objcopy to be more compatible with GNU objcopy.
This is split off of D66613 to land the libSupport changes separately. The llvm-objcopy part will land soon.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: nickdesaulniers, emaste, arichardson, hiraditya, jakehehrlich, abrachet, seiya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66613
llvm-svn: 375149
2019-10-18 02:09:05 +08:00
|
|
|
// ']' is allowed as the first character of a character class. '[]' is
|
|
|
|
// invalid. So, just skip the first character.
|
|
|
|
size_t End = S.find(']', 2);
|
2016-12-21 07:09:09 +08:00
|
|
|
if (End == StringRef::npos)
|
|
|
|
return make_error<StringError>("invalid glob pattern: " + Original,
|
|
|
|
errc::invalid_argument);
|
|
|
|
|
|
|
|
StringRef Chars = S.substr(1, End - 1);
|
|
|
|
S = S.substr(End + 1);
|
Reapply r375051: [support] GlobPattern: add support for `\` and `[!...]`, and allow `]` in more places
Reland r375051 (reverted in r375052) after fixing lld tests on Windows in r375126 and r375131.
Original description: Update GlobPattern in libSupport to handle a few more cases. It does not fully match the `fnmatch` used by GNU objcopy since named character classes (e.g. `[[:digit:]]`) are not supported, but this should support most existing use cases (mostly just `*` is what's used anyway).
This will be used to implement the `--wildcard` flag in llvm-objcopy to be more compatible with GNU objcopy.
This is split off of D66613 to land the libSupport changes separately. The llvm-objcopy part will land soon.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: nickdesaulniers, emaste, arichardson, hiraditya, jakehehrlich, abrachet, seiya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66613
llvm-svn: 375149
2019-10-18 02:09:05 +08:00
|
|
|
if (Chars.startswith("^") || Chars.startswith("!")) {
|
2016-12-21 07:09:09 +08:00
|
|
|
Expected<BitVector> BV = expand(Chars.substr(1), Original);
|
|
|
|
if (!BV)
|
|
|
|
return BV.takeError();
|
|
|
|
return BV->flip();
|
|
|
|
}
|
|
|
|
return expand(Chars, Original);
|
|
|
|
}
|
Reapply r375051: [support] GlobPattern: add support for `\` and `[!...]`, and allow `]` in more places
Reland r375051 (reverted in r375052) after fixing lld tests on Windows in r375126 and r375131.
Original description: Update GlobPattern in libSupport to handle a few more cases. It does not fully match the `fnmatch` used by GNU objcopy since named character classes (e.g. `[[:digit:]]`) are not supported, but this should support most existing use cases (mostly just `*` is what's used anyway).
This will be used to implement the `--wildcard` flag in llvm-objcopy to be more compatible with GNU objcopy.
This is split off of D66613 to land the libSupport changes separately. The llvm-objcopy part will land soon.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: nickdesaulniers, emaste, arichardson, hiraditya, jakehehrlich, abrachet, seiya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66613
llvm-svn: 375149
2019-10-18 02:09:05 +08:00
|
|
|
case '\\':
|
|
|
|
// Eat this character and fall through below to treat it like a non-meta
|
|
|
|
// character.
|
|
|
|
S = S.substr(1);
|
|
|
|
LLVM_FALLTHROUGH;
|
2016-12-21 07:09:09 +08:00
|
|
|
default:
|
|
|
|
BitVector BV(256, false);
|
2017-07-31 17:26:50 +08:00
|
|
|
BV[(uint8_t)S[0]] = true;
|
2016-12-21 07:09:09 +08:00
|
|
|
S = S.substr(1);
|
|
|
|
return BV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<GlobPattern> GlobPattern::create(StringRef S) {
|
|
|
|
GlobPattern Pat;
|
|
|
|
|
|
|
|
// S doesn't contain any metacharacter,
|
|
|
|
// so the regular string comparison should work.
|
|
|
|
if (!hasWildcard(S)) {
|
|
|
|
Pat.Exact = S;
|
|
|
|
return Pat;
|
|
|
|
}
|
|
|
|
|
Reapply r375051: [support] GlobPattern: add support for `\` and `[!...]`, and allow `]` in more places
Reland r375051 (reverted in r375052) after fixing lld tests on Windows in r375126 and r375131.
Original description: Update GlobPattern in libSupport to handle a few more cases. It does not fully match the `fnmatch` used by GNU objcopy since named character classes (e.g. `[[:digit:]]`) are not supported, but this should support most existing use cases (mostly just `*` is what's used anyway).
This will be used to implement the `--wildcard` flag in llvm-objcopy to be more compatible with GNU objcopy.
This is split off of D66613 to land the libSupport changes separately. The llvm-objcopy part will land soon.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: nickdesaulniers, emaste, arichardson, hiraditya, jakehehrlich, abrachet, seiya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66613
llvm-svn: 375149
2019-10-18 02:09:05 +08:00
|
|
|
// S is something like "foo*", and the "* is not escaped. We can use
|
|
|
|
// startswith().
|
|
|
|
if (S.endswith("*") && !S.endswith("\\*") && !hasWildcard(S.drop_back())) {
|
2016-12-21 07:09:09 +08:00
|
|
|
Pat.Prefix = S.drop_back();
|
|
|
|
return Pat;
|
|
|
|
}
|
|
|
|
|
|
|
|
// S is something like "*foo". We can use endswith().
|
|
|
|
if (S.startswith("*") && !hasWildcard(S.drop_front())) {
|
|
|
|
Pat.Suffix = S.drop_front();
|
|
|
|
return Pat;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we need to do real glob pattern matching.
|
|
|
|
// Parse the pattern now.
|
|
|
|
StringRef Original = S;
|
|
|
|
while (!S.empty()) {
|
|
|
|
Expected<BitVector> BV = scan(S, Original);
|
|
|
|
if (!BV)
|
|
|
|
return BV.takeError();
|
|
|
|
Pat.Tokens.push_back(*BV);
|
|
|
|
}
|
|
|
|
return Pat;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobPattern::match(StringRef S) const {
|
|
|
|
if (Exact)
|
|
|
|
return S == *Exact;
|
|
|
|
if (Prefix)
|
|
|
|
return S.startswith(*Prefix);
|
|
|
|
if (Suffix)
|
|
|
|
return S.endswith(*Suffix);
|
|
|
|
return matchOne(Tokens, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs glob pattern Pats against string S.
|
|
|
|
bool GlobPattern::matchOne(ArrayRef<BitVector> Pats, StringRef S) const {
|
|
|
|
for (;;) {
|
|
|
|
if (Pats.empty())
|
|
|
|
return S.empty();
|
|
|
|
|
|
|
|
// If Pats[0] is '*', try to match Pats[1..] against all possible
|
|
|
|
// tail strings of S to see at least one pattern succeeds.
|
|
|
|
if (Pats[0].size() == 0) {
|
|
|
|
Pats = Pats.slice(1);
|
|
|
|
if (Pats.empty())
|
|
|
|
// Fast path. If a pattern is '*', it matches anything.
|
|
|
|
return true;
|
|
|
|
for (size_t I = 0, E = S.size(); I < E; ++I)
|
|
|
|
if (matchOne(Pats, S.substr(I)))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Pats[0] is not '*', it must consume one character.
|
2017-07-31 17:26:50 +08:00
|
|
|
if (S.empty() || !Pats[0][(uint8_t)S[0]])
|
2016-12-21 07:09:09 +08:00
|
|
|
return false;
|
|
|
|
Pats = Pats.slice(1);
|
|
|
|
S = S.substr(1);
|
|
|
|
}
|
|
|
|
}
|