ClangCommentCommandInfoEmitter.cpp revision 243791
1243791Sdim//===--- ClangCommentCommandInfoEmitter.cpp - Generate command lists -----====//
2243791Sdim//
3243791Sdim//                     The LLVM Compiler Infrastructure
4243791Sdim//
5243791Sdim// This file is distributed under the University of Illinois Open Source
6243791Sdim// License. See LICENSE.TXT for details.
7243791Sdim//
8243791Sdim//===----------------------------------------------------------------------===//
9243791Sdim//
10243791Sdim// This tablegen backend emits command lists and efficient matchers command
11243791Sdim// names that are used in documentation comments.
12243791Sdim//
13243791Sdim//===----------------------------------------------------------------------===//
14243791Sdim
15243791Sdim#include "llvm/TableGen/Record.h"
16243791Sdim#include "llvm/TableGen/StringMatcher.h"
17243791Sdim#include <vector>
18243791Sdim
19243791Sdimusing namespace llvm;
20243791Sdim
21243791Sdimnamespace clang {
22243791Sdimvoid EmitClangCommentCommandInfo(RecordKeeper &Records, raw_ostream &OS) {
23243791Sdim  OS << "// This file is generated by TableGen.  Do not edit.\n\n";
24243791Sdim
25243791Sdim  OS << "namespace {\n"
26243791Sdim        "const CommandInfo Commands[] = {\n";
27243791Sdim  std::vector<Record *> Tags = Records.getAllDerivedDefinitions("Command");
28243791Sdim  for (size_t i = 0, e = Tags.size(); i != e; ++i) {
29243791Sdim    Record &Tag = *Tags[i];
30243791Sdim    OS << "  { "
31243791Sdim       << "\"" << Tag.getValueAsString("Name") << "\", "
32243791Sdim       << "\"" << Tag.getValueAsString("EndCommandName") << "\", "
33243791Sdim       << i << ", "
34243791Sdim       << Tag.getValueAsInt("NumArgs") << ", "
35243791Sdim       << Tag.getValueAsBit("IsInlineCommand") << ", "
36243791Sdim       << Tag.getValueAsBit("IsBlockCommand") << ", "
37243791Sdim       << Tag.getValueAsBit("IsBriefCommand") << ", "
38243791Sdim       << Tag.getValueAsBit("IsReturnsCommand") << ", "
39243791Sdim       << Tag.getValueAsBit("IsParamCommand") << ", "
40243791Sdim       << Tag.getValueAsBit("IsTParamCommand") << ", "
41243791Sdim       << Tag.getValueAsBit("IsDeprecatedCommand") << ", "
42243791Sdim       << Tag.getValueAsBit("IsEmptyParagraphAllowed") << ", "
43243791Sdim       << Tag.getValueAsBit("IsVerbatimBlockCommand") << ", "
44243791Sdim       << Tag.getValueAsBit("IsVerbatimBlockEndCommand") << ", "
45243791Sdim       << Tag.getValueAsBit("IsVerbatimLineCommand") << ", "
46243791Sdim       << Tag.getValueAsBit("IsDeclarationCommand") << ", "
47243791Sdim       << /* IsUnknownCommand = */ "0"
48243791Sdim       << " }";
49243791Sdim    if (i + 1 != e)
50243791Sdim      OS << ",";
51243791Sdim    OS << "\n";
52243791Sdim  }
53243791Sdim  OS << "};\n"
54243791Sdim        "} // unnamed namespace\n\n";
55243791Sdim
56243791Sdim  std::vector<StringMatcher::StringPair> Matches;
57243791Sdim  for (size_t i = 0, e = Tags.size(); i != e; ++i) {
58243791Sdim    Record &Tag = *Tags[i];
59243791Sdim    std::string Name = Tag.getValueAsString("Name");
60243791Sdim    std::string Return;
61243791Sdim    raw_string_ostream(Return) << "return &Commands[" << i << "];";
62243791Sdim    Matches.push_back(StringMatcher::StringPair(Name, Return));
63243791Sdim  }
64243791Sdim
65243791Sdim  OS << "const CommandInfo *CommandTraits::getBuiltinCommandInfo(\n"
66243791Sdim     << "                                         StringRef Name) {\n";
67243791Sdim  StringMatcher("Name", Matches, OS).Emit();
68243791Sdim  OS << "  return NULL;\n"
69243791Sdim     << "}\n\n";
70243791Sdim}
71243791Sdim} // end namespace clang
72243791Sdim
73