12dc4a80eSAlexey Lapshin //===- CommonConfig.cpp ---------------------------------------------------===//
22dc4a80eSAlexey Lapshin //
32dc4a80eSAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42dc4a80eSAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
52dc4a80eSAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62dc4a80eSAlexey Lapshin //
72dc4a80eSAlexey Lapshin //===----------------------------------------------------------------------===//
82dc4a80eSAlexey Lapshin
92dc4a80eSAlexey Lapshin #include "llvm/ObjCopy/CommonConfig.h"
10bdcb841aSIlia Kuklin #include "llvm/Support/Errc.h"
112dc4a80eSAlexey Lapshin
122dc4a80eSAlexey Lapshin namespace llvm {
132dc4a80eSAlexey Lapshin namespace objcopy {
142dc4a80eSAlexey Lapshin
152dc4a80eSAlexey Lapshin Expected<NameOrPattern>
create(StringRef Pattern,MatchStyle MS,function_ref<Error (Error)> ErrorCallback)162dc4a80eSAlexey Lapshin NameOrPattern::create(StringRef Pattern, MatchStyle MS,
172dc4a80eSAlexey Lapshin function_ref<Error(Error)> ErrorCallback) {
182dc4a80eSAlexey Lapshin switch (MS) {
192dc4a80eSAlexey Lapshin case MatchStyle::Literal:
202dc4a80eSAlexey Lapshin return NameOrPattern(Pattern);
212dc4a80eSAlexey Lapshin case MatchStyle::Wildcard: {
222dc4a80eSAlexey Lapshin SmallVector<char, 32> Data;
23*01c5b5c1SKazu Hirata bool IsPositiveMatch = !Pattern.consume_front("!");
242dc4a80eSAlexey Lapshin Expected<GlobPattern> GlobOrErr = GlobPattern::create(Pattern);
252dc4a80eSAlexey Lapshin
262dc4a80eSAlexey Lapshin // If we couldn't create it as a glob, report the error, but try again
272dc4a80eSAlexey Lapshin // with a literal if the error reporting is non-fatal.
282dc4a80eSAlexey Lapshin if (!GlobOrErr) {
292dc4a80eSAlexey Lapshin if (Error E = ErrorCallback(GlobOrErr.takeError()))
302dc4a80eSAlexey Lapshin return std::move(E);
312dc4a80eSAlexey Lapshin return create(Pattern, MatchStyle::Literal, ErrorCallback);
322dc4a80eSAlexey Lapshin }
332dc4a80eSAlexey Lapshin
342dc4a80eSAlexey Lapshin return NameOrPattern(std::make_shared<GlobPattern>(*GlobOrErr),
352dc4a80eSAlexey Lapshin IsPositiveMatch);
362dc4a80eSAlexey Lapshin }
372dc4a80eSAlexey Lapshin case MatchStyle::Regex: {
38bdcb841aSIlia Kuklin Regex RegEx(Pattern);
39bdcb841aSIlia Kuklin std::string Err;
40bdcb841aSIlia Kuklin if (!RegEx.isValid(Err))
41bdcb841aSIlia Kuklin return createStringError(errc::invalid_argument,
42bdcb841aSIlia Kuklin "cannot compile regular expression \'" +
43bdcb841aSIlia Kuklin Pattern + "\': " + Err);
442dc4a80eSAlexey Lapshin SmallVector<char, 32> Data;
452dc4a80eSAlexey Lapshin return NameOrPattern(std::make_shared<Regex>(
462dc4a80eSAlexey Lapshin ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));
472dc4a80eSAlexey Lapshin }
482dc4a80eSAlexey Lapshin }
492dc4a80eSAlexey Lapshin llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");
502dc4a80eSAlexey Lapshin }
512dc4a80eSAlexey Lapshin
522dc4a80eSAlexey Lapshin } // end namespace objcopy
532dc4a80eSAlexey Lapshin } // end namespace llvm
54