102 lines · plain
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// This file contains the definition of custom command-line argument parsers11/// using llvm::cl.12///13//===----------------------------------------------------------------------===//14 15#ifndef MATHTEST_COMMANDLINE_HPP16#define MATHTEST_COMMANDLINE_HPP17 18#include "mathtest/TestConfig.hpp"19 20#include "llvm/ADT/STLExtras.h"21#include "llvm/ADT/SmallVector.h"22#include "llvm/ADT/StringRef.h"23#include "llvm/Support/CommandLine.h"24 25#include <string>26 27namespace llvm {28namespace cl {29 30struct TestConfigsArg {31 enum class Mode { Default, All, Explicit } Mode = Mode::Default;32 llvm::SmallVector<mathtest::TestConfig, 4> Explicit;33};34 35template <> class parser<TestConfigsArg> : public basic_parser<TestConfigsArg> {36public:37 parser(Option &O) : basic_parser<TestConfigsArg>(O) {}38 39 static bool isAllowed(const mathtest::TestConfig &Config) {40 static const llvm::SmallVector<mathtest::TestConfig, 4> &AllTestConfigs =41 mathtest::getAllTestConfigs();42 43 return llvm::is_contained(AllTestConfigs, Config);44 }45 46 bool parse(Option &O, StringRef ArgName, StringRef ArgValue,47 TestConfigsArg &Val) {48 ArgValue = ArgValue.trim();49 if (ArgValue.empty())50 return O.error(51 "Expected '" + getValueName() +52 "', but got an empty string. Omit the flag to use defaults");53 54 if (ArgValue.equals_insensitive("all")) {55 Val.Mode = TestConfigsArg::Mode::All;56 return false;57 }58 59 llvm::SmallVector<StringRef, 8> Pairs;60 ArgValue.split(Pairs, ',', /*MaxSplit=*/-1, /*KeepEmpty=*/false);61 62 Val.Mode = TestConfigsArg::Mode::Explicit;63 Val.Explicit.clear();64 65 for (StringRef Pair : Pairs) {66 llvm::SmallVector<StringRef, 2> Parts;67 Pair.split(Parts, ':');68 69 if (Parts.size() != 2)70 return O.error("Expected '<provider>:<platform>', got '" + Pair + "'");71 72 StringRef Provider = Parts[0].trim();73 StringRef Platform = Parts[1].trim();74 75 if (Provider.empty() || Platform.empty())76 return O.error("Provider and platform must not be empty in '" + Pair +77 "'");78 79 mathtest::TestConfig Config = {Provider.str(), Platform.str()};80 if (!isAllowed(Config))81 return O.error("Invalid pair '" + Pair + "'");82 83 Val.Explicit.push_back(Config);84 }85 86 return false;87 }88 89 StringRef getValueName() const override {90 return "all|provider:platform[,provider:platform...]";91 }92 93 void printOptionDiff(const Option &O, const TestConfigsArg &V, OptVal Default,94 size_t GlobalWidth) const {95 printOptionNoValue(O, GlobalWidth);96 }97};98} // namespace cl99} // namespace llvm100 101#endif // MATHTEST_COMMANDLINE_HPP102