50 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/ObjCopy/CommonConfig.h"10#include "llvm/Support/Errc.h"11 12using namespace llvm;13using namespace llvm::objcopy;14 15Expected<NameOrPattern>16NameOrPattern::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("!");23 Expected<GlobPattern> GlobOrErr = GlobPattern::create(Pattern);24 25 // If we couldn't create it as a glob, report the error, but try again26 // 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, MatchStyle::Literal, ErrorCallback);31 }32 33 return NameOrPattern(std::make_shared<GlobPattern>(*GlobOrErr),34 IsPositiveMatch);35 }36 case MatchStyle::Regex: {37 Regex RegEx(Pattern);38 std::string Err;39 if (!RegEx.isValid(Err))40 return createStringError(errc::invalid_argument,41 "cannot compile regular expression \'" +42 Pattern + "\': " + Err);43 SmallVector<char, 32> Data;44 return NameOrPattern(std::make_shared<Regex>(45 ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));46 }47 }48 llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");49}50