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