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 | namespace llvm { |
13 | namespace 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 | SmallVector<char, 32> Data; |
23 | bool IsPositiveMatch = !Pattern.consume_front(Prefix: "!" ); |
24 | Expected<GlobPattern> GlobOrErr = GlobPattern::create(Pat: Pattern); |
25 | |
26 | // If we couldn't create it as a glob, report the error, but try again |
27 | // with a literal if the error reporting is non-fatal. |
28 | if (!GlobOrErr) { |
29 | if (Error E = ErrorCallback(GlobOrErr.takeError())) |
30 | return std::move(E); |
31 | return create(Pattern, MS: MatchStyle::Literal, ErrorCallback); |
32 | } |
33 | |
34 | return NameOrPattern(std::make_shared<GlobPattern>(args&: *GlobOrErr), |
35 | IsPositiveMatch); |
36 | } |
37 | case MatchStyle::Regex: { |
38 | Regex RegEx(Pattern); |
39 | std::string Err; |
40 | if (!RegEx.isValid(Error&: Err)) |
41 | return createStringError(EC: errc::invalid_argument, |
42 | S: "cannot compile regular expression \'" + |
43 | Pattern + "\': " + Err); |
44 | SmallVector<char, 32> Data; |
45 | return NameOrPattern(std::make_shared<Regex>( |
46 | args: ("^" + Pattern.ltrim(Char: '^').rtrim(Char: '$') + "$" ).toStringRef(Out&: Data))); |
47 | } |
48 | } |
49 | llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum" ); |
50 | } |
51 | |
52 | } // end namespace objcopy |
53 | } // end namespace llvm |
54 | |