RefactoringOptions.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- RefactoringOptions.h - Clang refactoring library -----------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGOPTIONS_H
  14. #define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGOPTIONS_H
  15. #include "clang/Basic/LLVM.h"
  16. #include "clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h"
  17. #include "clang/Tooling/Refactoring/RefactoringOption.h"
  18. #include "clang/Tooling/Refactoring/RefactoringOptionVisitor.h"
  19. #include "llvm/Support/Error.h"
  20. #include <optional>
  21. #include <type_traits>
  22. namespace clang {
  23. namespace tooling {
  24. /// A refactoring option that stores a value of type \c T.
  25. template <typename T,
  26. typename = std::enable_if_t<traits::IsValidOptionType<T>::value>>
  27. class OptionalRefactoringOption : public RefactoringOption {
  28. public:
  29. void passToVisitor(RefactoringOptionVisitor &Visitor) final {
  30. Visitor.visit(*this, Value);
  31. }
  32. bool isRequired() const override { return false; }
  33. using ValueType = std::optional<T>;
  34. const ValueType &getValue() const { return Value; }
  35. protected:
  36. std::optional<T> Value;
  37. };
  38. /// A required refactoring option that stores a value of type \c T.
  39. template <typename T,
  40. typename = std::enable_if_t<traits::IsValidOptionType<T>::value>>
  41. class RequiredRefactoringOption : public OptionalRefactoringOption<T> {
  42. public:
  43. using ValueType = T;
  44. const ValueType &getValue() const {
  45. return *OptionalRefactoringOption<T>::Value;
  46. }
  47. bool isRequired() const final { return true; }
  48. };
  49. } // end namespace tooling
  50. } // end namespace clang
  51. #endif // LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGOPTIONS_H
  52. #ifdef __GNUC__
  53. #pragma GCC diagnostic pop
  54. #endif