| 1 | //===- CommonConfig.cpp ---------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ObjCopy/CommonConfig.h" |
| 10 | #include "llvm/Support/Errc.h" |
| 11 | |
| 12 | using namespace llvm; |
| 13 | using namespace llvm::objcopy; |
| 14 | |
| 15 | Expected<NameOrPattern> |
| 16 | NameOrPattern::create(StringRef Pattern, MatchStyle MS, |
| 17 | function_ref<Error(Error)> ErrorCallback) { |
| 18 | switch (MS) { |
| 19 | case MatchStyle::Literal: |
| 20 | return NameOrPattern(Pattern); |
| 21 | case MatchStyle::Wildcard: { |
| 22 | bool IsPositiveMatch = !Pattern.consume_front(Prefix: "!" ); |
| 23 | Expected<GlobPattern> GlobOrErr = GlobPattern::create(Pat: Pattern); |
| 24 | |
| 25 | // If we couldn't create it as a glob, report the error, but try again |
| 26 | // with a literal if the error reporting is non-fatal. |
| 27 | if (!GlobOrErr) { |
| 28 | if (Error E = ErrorCallback(GlobOrErr.takeError())) |
| 29 | return std::move(E); |
| 30 | return create(Pattern, MS: MatchStyle::Literal, ErrorCallback); |
| 31 | } |
| 32 | |
| 33 | return NameOrPattern(std::make_shared<GlobPattern>(args&: *GlobOrErr), |
| 34 | IsPositiveMatch); |
| 35 | } |
| 36 | case MatchStyle::Regex: { |
| 37 | Regex RegEx(Pattern); |
| 38 | std::string Err; |
| 39 | if (!RegEx.isValid(Error&: Err)) |
| 40 | return createStringError(EC: errc::invalid_argument, |
| 41 | S: "cannot compile regular expression \'" + |
| 42 | Pattern + "\': " + Err); |
| 43 | SmallVector<char, 32> Data; |
| 44 | return NameOrPattern(std::make_shared<Regex>( |
| 45 | args: ("^" + Pattern.ltrim(Char: '^').rtrim(Char: '$') + "$" ).toStringRef(Out&: Data))); |
| 46 | } |
| 47 | } |
| 48 | llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum" ); |
| 49 | } |
| 50 | |