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"
10 #include "llvm/Support/Errc.h"
15 Expected
<NameOrPattern
>
16 NameOrPattern::create(StringRef Pattern
, MatchStyle MS
,
17 function_ref
<Error(Error
)> ErrorCallback
) {
19 case MatchStyle::Literal
:
20 return NameOrPattern(Pattern
);
21 case MatchStyle::Wildcard
: {
22 SmallVector
<char, 32> Data
;
23 bool IsPositiveMatch
= !Pattern
.consume_front("!");
24 Expected
<GlobPattern
> GlobOrErr
= GlobPattern::create(Pattern
);
26 // If we couldn't create it as a glob, report the error, but try again
27 // with a literal if the error reporting is non-fatal.
29 if (Error E
= ErrorCallback(GlobOrErr
.takeError()))
31 return create(Pattern
, MatchStyle::Literal
, ErrorCallback
);
34 return NameOrPattern(std::make_shared
<GlobPattern
>(*GlobOrErr
),
37 case MatchStyle::Regex
: {
40 if (!RegEx
.isValid(Err
))
41 return createStringError(errc::invalid_argument
,
42 "cannot compile regular expression \'" +
43 Pattern
+ "\': " + Err
);
44 SmallVector
<char, 32> Data
;
45 return NameOrPattern(std::make_shared
<Regex
>(
46 ("^" + Pattern
.ltrim('^').rtrim('$') + "$").toStringRef(Data
)));
49 llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");
52 } // end namespace objcopy
53 } // end namespace llvm