Path: blob/main/contrib/llvm-project/llvm/lib/ObjCopy/CommonConfig.cpp
35233 views
//===- CommonConfig.cpp ---------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/ObjCopy/CommonConfig.h"9#include "llvm/Support/Errc.h"1011namespace llvm {12namespace objcopy {1314Expected<NameOrPattern>15NameOrPattern::create(StringRef Pattern, MatchStyle MS,16function_ref<Error(Error)> ErrorCallback) {17switch (MS) {18case MatchStyle::Literal:19return NameOrPattern(Pattern);20case MatchStyle::Wildcard: {21SmallVector<char, 32> Data;22bool IsPositiveMatch = !Pattern.consume_front("!");23Expected<GlobPattern> GlobOrErr = GlobPattern::create(Pattern);2425// 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.27if (!GlobOrErr) {28if (Error E = ErrorCallback(GlobOrErr.takeError()))29return std::move(E);30return create(Pattern, MatchStyle::Literal, ErrorCallback);31}3233return NameOrPattern(std::make_shared<GlobPattern>(*GlobOrErr),34IsPositiveMatch);35}36case MatchStyle::Regex: {37Regex RegEx(Pattern);38std::string Err;39if (!RegEx.isValid(Err))40return createStringError(errc::invalid_argument,41"cannot compile regular expression \'" +42Pattern + "\': " + Err);43SmallVector<char, 32> Data;44return NameOrPattern(std::make_shared<Regex>(45("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));46}47}48llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");49}5051} // end namespace objcopy52} // end namespace llvm535455