aboutsummaryrefslogtreecommitdiff
path: root/offload/unittests/Conformance/include/mathtest/CommandLine.hpp
blob: b28594aafbcf26ed26eac02c4103aca6630b43d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the definition of custom command-line argument parsers
/// using llvm::cl.
///
//===----------------------------------------------------------------------===//

#ifndef MATHTEST_COMMANDLINE_HPP
#define MATHTEST_COMMANDLINE_HPP

#include "mathtest/TestConfig.hpp"

#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"

#include <string>

namespace llvm {
namespace cl {

struct TestConfigsArg {
  enum class Mode { Default, All, Explicit } Mode = Mode::Default;
  llvm::SmallVector<mathtest::TestConfig, 4> Explicit;
};

template <> class parser<TestConfigsArg> : public basic_parser<TestConfigsArg> {
public:
  parser(Option &O) : basic_parser<TestConfigsArg>(O) {}

  static bool isAllowed(const mathtest::TestConfig &Config) {
    static const llvm::SmallVector<mathtest::TestConfig, 4> &AllTestConfigs =
        mathtest::getAllTestConfigs();

    return llvm::is_contained(AllTestConfigs, Config);
  }

  bool parse(Option &O, StringRef ArgName, StringRef ArgValue,
             TestConfigsArg &Val) {
    ArgValue = ArgValue.trim();
    if (ArgValue.empty())
      return O.error(
          "Expected '" + getValueName() +
          "', but got an empty string. Omit the flag to use defaults");

    if (ArgValue.equals_insensitive("all")) {
      Val.Mode = TestConfigsArg::Mode::All;
      return false;
    }

    llvm::SmallVector<StringRef, 8> Pairs;
    ArgValue.split(Pairs, ',', /*MaxSplit=*/-1, /*KeepEmpty=*/false);

    Val.Mode = TestConfigsArg::Mode::Explicit;
    Val.Explicit.clear();

    for (StringRef Pair : Pairs) {
      llvm::SmallVector<StringRef, 2> Parts;
      Pair.split(Parts, ':');

      if (Parts.size() != 2)
        return O.error("Expected '<provider>:<platform>', got '" + Pair + "'");

      StringRef Provider = Parts[0].trim();
      StringRef Platform = Parts[1].trim();

      if (Provider.empty() || Platform.empty())
        return O.error("Provider and platform must not be empty in '" + Pair +
                       "'");

      mathtest::TestConfig Config = {Provider.str(), Platform.str()};
      if (!isAllowed(Config))
        return O.error("Invalid pair '" + Pair + "'");

      Val.Explicit.push_back(Config);
    }

    return false;
  }

  StringRef getValueName() const override {
    return "all|provider:platform[,provider:platform...]";
  }

  void printOptionDiff(const Option &O, const TestConfigsArg &V, OptVal Default,
                       size_t GlobalWidth) const {
    printOptionNoValue(O, GlobalWidth);
  }
};
} // namespace cl
} // namespace llvm

#endif // MATHTEST_COMMANDLINE_HPP