OptionUtils.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //===--- OptionUtils.cpp - Utilities for command line arguments -----------===//
  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 "clang/Basic/Diagnostic.h"
  9. #include "clang/Basic/DiagnosticDriver.h"
  10. #include "clang/Driver/OptionUtils.h"
  11. #include "llvm/Option/ArgList.h"
  12. using namespace clang;
  13. using namespace llvm::opt;
  14. namespace {
  15. template <typename IntTy>
  16. IntTy getLastArgIntValueImpl(const ArgList &Args, OptSpecifier Id,
  17. IntTy Default, DiagnosticsEngine *Diags,
  18. unsigned Base) {
  19. IntTy Res = Default;
  20. if (Arg *A = Args.getLastArg(Id)) {
  21. if (StringRef(A->getValue()).getAsInteger(Base, Res)) {
  22. if (Diags)
  23. Diags->Report(diag::err_drv_invalid_int_value)
  24. << A->getAsString(Args) << A->getValue();
  25. }
  26. }
  27. return Res;
  28. }
  29. } // namespace
  30. namespace clang {
  31. int getLastArgIntValue(const ArgList &Args, OptSpecifier Id, int Default,
  32. DiagnosticsEngine *Diags, unsigned Base) {
  33. return getLastArgIntValueImpl<int>(Args, Id, Default, Diags, Base);
  34. }
  35. uint64_t getLastArgUInt64Value(const ArgList &Args, OptSpecifier Id,
  36. uint64_t Default, DiagnosticsEngine *Diags,
  37. unsigned Base) {
  38. return getLastArgIntValueImpl<uint64_t>(Args, Id, Default, Diags, Base);
  39. }
  40. } // namespace clang