xref: /freebsd/contrib/llvm-project/llvm/lib/ObjCopy/CommonConfig.cpp (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ObjCopy/CommonConfig.h"
10 
11 namespace llvm {
12 namespace objcopy {
13 
14 Expected<NameOrPattern>
15 NameOrPattern::create(StringRef Pattern, MatchStyle MS,
16                       function_ref<Error(Error)> ErrorCallback) {
17   switch (MS) {
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();
26     }
27     Expected<GlobPattern> GlobOrErr = GlobPattern::create(Pattern);
28 
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.
31     if (!GlobOrErr) {
32       if (Error E = ErrorCallback(GlobOrErr.takeError()))
33         return std::move(E);
34       return create(Pattern, MatchStyle::Literal, ErrorCallback);
35     }
36 
37     return NameOrPattern(std::make_shared<GlobPattern>(*GlobOrErr),
38                          IsPositiveMatch);
39   }
40   case MatchStyle::Regex: {
41     SmallVector<char, 32> Data;
42     return NameOrPattern(std::make_shared<Regex>(
43         ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));
44   }
45   }
46   llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");
47 }
48 
49 } // end namespace objcopy
50 } // end namespace llvm
51