1 //===- CommonConfig.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ObjCopy/CommonConfig.h"
14 Expected
<NameOrPattern
>
15 NameOrPattern::create(StringRef Pattern
, MatchStyle MS
,
16 function_ref
<Error(Error
)> ErrorCallback
) {
18 case MatchStyle::Literal
:
19 return NameOrPattern(Pattern
);
20 case MatchStyle::Wildcard
: {
21 SmallVector
<char, 32> Data
;
22 bool IsPositiveMatch
= true;
23 if (Pattern
[0] == '!') {
24 IsPositiveMatch
= false;
25 Pattern
= Pattern
.drop_front();
27 Expected
<GlobPattern
> GlobOrErr
= GlobPattern::create(Pattern
);
29 // If we couldn't create it as a glob, report the error, but try again
30 // with a literal if the error reporting is non-fatal.
32 if (Error E
= ErrorCallback(GlobOrErr
.takeError()))
34 return create(Pattern
, MatchStyle::Literal
, ErrorCallback
);
37 return NameOrPattern(std::make_shared
<GlobPattern
>(*GlobOrErr
),
40 case MatchStyle::Regex
: {
41 SmallVector
<char, 32> Data
;
42 return NameOrPattern(std::make_shared
<Regex
>(
43 ("^" + Pattern
.ltrim('^').rtrim('$') + "$").toStringRef(Data
)));
46 llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");
49 } // end namespace objcopy
50 } // end namespace llvm