diff options
author | Jordan Rupprecht <rupprecht@google.com> | 2019-10-17 18:09:05 +0000 |
---|---|---|
committer | Jordan Rupprecht <rupprecht@google.com> | 2019-10-17 18:09:05 +0000 |
commit | 48993d5ab9413f0e5b94dfa292a233ce55b09e3e (patch) | |
tree | cf748a7d685b4845162615960d62e1cb61e68e49 /llvm/lib/Support/GlobPattern.cpp | |
parent | 05c3d90248db372083d4314fb74f677837adf01d (diff) | |
download | bcm5719-llvm-48993d5ab9413f0e5b94dfa292a233ce55b09e3e.tar.gz bcm5719-llvm-48993d5ab9413f0e5b94dfa292a233ce55b09e3e.zip |
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
Diffstat (limited to 'llvm/lib/Support/GlobPattern.cpp')
-rw-r--r-- | llvm/lib/Support/GlobPattern.cpp | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/llvm/lib/Support/GlobPattern.cpp b/llvm/lib/Support/GlobPattern.cpp index 6011be86d77..8dae6941ec7 100644 --- a/llvm/lib/Support/GlobPattern.cpp +++ b/llvm/lib/Support/GlobPattern.cpp @@ -19,7 +19,7 @@ using namespace llvm; static bool hasWildcard(StringRef S) { - return S.find_first_of("?*[") != StringRef::npos; + return S.find_first_of("?*[\\") != StringRef::npos; } // Expands character ranges and returns a bitmap. @@ -60,8 +60,9 @@ static Expected<BitVector> expand(StringRef S, StringRef Original) { } // This is a scanner for the glob pattern. -// A glob pattern token is one of "*", "?", "[<chars>]", "[^<chars>]" -// (which is a negative form of "[<chars>]"), or a non-meta character. +// 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. // This function returns the first token in S. static Expected<BitVector> scan(StringRef &S, StringRef Original) { switch (S[0]) { @@ -74,14 +75,16 @@ static Expected<BitVector> scan(StringRef &S, StringRef Original) { S = S.substr(1); return BitVector(256, true); case '[': { - size_t End = S.find(']', 1); + // ']' is allowed as the first character of a character class. '[]' is + // invalid. So, just skip the first character. + size_t End = S.find(']', 2); 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); - if (Chars.startswith("^")) { + if (Chars.startswith("^") || Chars.startswith("!")) { Expected<BitVector> BV = expand(Chars.substr(1), Original); if (!BV) return BV.takeError(); @@ -89,6 +92,11 @@ static Expected<BitVector> scan(StringRef &S, StringRef Original) { } return expand(Chars, Original); } + case '\\': + // Eat this character and fall through below to treat it like a non-meta + // character. + S = S.substr(1); + LLVM_FALLTHROUGH; default: BitVector BV(256, false); BV[(uint8_t)S[0]] = true; @@ -107,8 +115,9 @@ Expected<GlobPattern> GlobPattern::create(StringRef S) { return Pat; } - // S is something like "foo*". We can use startswith(). - if (S.endswith("*") && !hasWildcard(S.drop_back())) { + // S is something like "foo*", and the "* is not escaped. We can use + // startswith(). + if (S.endswith("*") && !S.endswith("\\*") && !hasWildcard(S.drop_back())) { Pat.Prefix = S.drop_back(); return Pat; } |