CommonConfig.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #include "llvm/ObjCopy/CommonConfig.h"
  9. namespace llvm {
  10. namespace objcopy {
  11. Expected<NameOrPattern>
  12. NameOrPattern::create(StringRef Pattern, MatchStyle MS,
  13. function_ref<Error(Error)> ErrorCallback) {
  14. switch (MS) {
  15. case MatchStyle::Literal:
  16. return NameOrPattern(Pattern);
  17. case MatchStyle::Wildcard: {
  18. SmallVector<char, 32> Data;
  19. bool IsPositiveMatch = true;
  20. if (Pattern[0] == '!') {
  21. IsPositiveMatch = false;
  22. Pattern = Pattern.drop_front();
  23. }
  24. Expected<GlobPattern> GlobOrErr = GlobPattern::create(Pattern);
  25. // If we couldn't create it as a glob, report the error, but try again
  26. // 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. return NameOrPattern(std::make_shared<GlobPattern>(*GlobOrErr),
  33. IsPositiveMatch);
  34. }
  35. case MatchStyle::Regex: {
  36. SmallVector<char, 32> Data;
  37. return NameOrPattern(std::make_shared<Regex>(
  38. ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));
  39. }
  40. }
  41. llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");
  42. }
  43. } // end namespace objcopy
  44. } // end namespace llvm