xref: /llvm-project/mlir/lib/TableGen/GenInfo.cpp (revision 1c5a50e32815a49a41d79ff529ca8611ee49c5c8)
1 //===- GenInfo.cpp - Generator info -----------------------------*- C++ -*-===//
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 
9 #include "mlir/TableGen/GenInfo.h"
10 
11 #include "mlir/TableGen/GenNameParser.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Support/ManagedStatic.h"
14 
15 using namespace mlir;
16 
17 static llvm::ManagedStatic<std::vector<GenInfo>> generatorRegistry;
18 
GenRegistration(StringRef arg,StringRef description,const GenFunction & function)19 GenRegistration::GenRegistration(StringRef arg, StringRef description,
20                                  const GenFunction &function) {
21   generatorRegistry->emplace_back(arg, description, function);
22 }
23 
GenNameParser(llvm::cl::Option & opt)24 GenNameParser::GenNameParser(llvm::cl::Option &opt)
25     : llvm::cl::parser<const GenInfo *>(opt) {
26   for (const auto &kv : *generatorRegistry) {
27     addLiteralOption(kv.getGenArgument(), &kv, kv.getGenDescription());
28   }
29 }
30 
printOptionInfo(const llvm::cl::Option & o,size_t globalWidth) const31 void GenNameParser::printOptionInfo(const llvm::cl::Option &o,
32                                     size_t globalWidth) const {
33   GenNameParser *tp = const_cast<GenNameParser *>(this);
34   llvm::array_pod_sort(tp->Values.begin(), tp->Values.end(),
35                        [](const GenNameParser::OptionInfo *vT1,
36                           const GenNameParser::OptionInfo *vT2) {
37                          return vT1->Name.compare(vT2->Name);
38                        });
39   using llvm::cl::parser;
40   parser<const GenInfo *>::printOptionInfo(o, globalWidth);
41 }
42