blob: 4dafc2eec90373c5d80baf539d67ae8919c0687f [file] [log] [blame]
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +00001//===--- ClangCommentCommandInfoEmitter.cpp - Generate command lists -----====//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Dmitri Gribenko8f1fa252013-01-30 21:54:20 +000010// This tablegen backend emits command lists and efficient matchers for command
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000011// names that are used in documentation comments.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/TableGen/Record.h"
16#include "llvm/TableGen/StringMatcher.h"
Dmitri Gribenko8f1fa252013-01-30 21:54:20 +000017#include "llvm/TableGen/TableGenBackend.h"
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000018#include <vector>
19
20using namespace llvm;
21
22namespace clang {
23void EmitClangCommentCommandInfo(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko8f1fa252013-01-30 21:54:20 +000024 emitSourceFileHeader("A list of commands useable in documentation "
25 "comments", OS);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000026
27 OS << "namespace {\n"
28 "const CommandInfo Commands[] = {\n";
29 std::vector<Record *> Tags = Records.getAllDerivedDefinitions("Command");
30 for (size_t i = 0, e = Tags.size(); i != e; ++i) {
31 Record &Tag = *Tags[i];
32 OS << " { "
33 << "\"" << Tag.getValueAsString("Name") << "\", "
34 << "\"" << Tag.getValueAsString("EndCommandName") << "\", "
35 << i << ", "
36 << Tag.getValueAsInt("NumArgs") << ", "
37 << Tag.getValueAsBit("IsInlineCommand") << ", "
38 << Tag.getValueAsBit("IsBlockCommand") << ", "
39 << Tag.getValueAsBit("IsBriefCommand") << ", "
40 << Tag.getValueAsBit("IsReturnsCommand") << ", "
41 << Tag.getValueAsBit("IsParamCommand") << ", "
42 << Tag.getValueAsBit("IsTParamCommand") << ", "
Dmitri Gribenko0bd98382012-09-22 21:47:50 +000043 << Tag.getValueAsBit("IsDeprecatedCommand") << ", "
Fariborz Jahanianf843a582013-01-31 23:12:39 +000044 << Tag.getValueAsBit("IsHeaderfileCommand") << ", "
Dmitri Gribenkoabcf0dc2012-09-13 20:36:01 +000045 << Tag.getValueAsBit("IsEmptyParagraphAllowed") << ", "
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000046 << Tag.getValueAsBit("IsVerbatimBlockCommand") << ", "
47 << Tag.getValueAsBit("IsVerbatimBlockEndCommand") << ", "
48 << Tag.getValueAsBit("IsVerbatimLineCommand") << ", "
49 << Tag.getValueAsBit("IsDeclarationCommand") << ", "
50 << /* IsUnknownCommand = */ "0"
51 << " }";
52 if (i + 1 != e)
53 OS << ",";
54 OS << "\n";
55 }
56 OS << "};\n"
57 "} // unnamed namespace\n\n";
58
59 std::vector<StringMatcher::StringPair> Matches;
60 for (size_t i = 0, e = Tags.size(); i != e; ++i) {
61 Record &Tag = *Tags[i];
62 std::string Name = Tag.getValueAsString("Name");
63 std::string Return;
64 raw_string_ostream(Return) << "return &Commands[" << i << "];";
65 Matches.push_back(StringMatcher::StringPair(Name, Return));
66 }
67
68 OS << "const CommandInfo *CommandTraits::getBuiltinCommandInfo(\n"
69 << " StringRef Name) {\n";
70 StringMatcher("Name", Matches, OS).Emit();
71 OS << " return NULL;\n"
72 << "}\n\n";
73}
Dmitri Gribenkoaf01bed2013-02-01 20:23:57 +000074
75static std::string MangleName(StringRef Str) {
76 std::string Mangled;
77 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
78 switch (Str[i]) {
79 default:
80 Mangled += Str[i];
81 break;
82 case '[':
83 Mangled += "lsquare";
84 break;
85 case ']':
86 Mangled += "rsquare";
87 break;
88 case '{':
89 Mangled += "lbrace";
90 break;
91 case '}':
92 Mangled += "rbrace";
93 break;
94 case '$':
95 Mangled += "dollar";
96 break;
97 }
98 }
99 return Mangled;
100}
101
102void EmitClangCommentCommandList(RecordKeeper &Records, raw_ostream &OS) {
103 emitSourceFileHeader("A list of commands useable in documentation "
104 "comments", OS);
105
106 OS << "#ifndef COMMENT_COMMAND\n"
107 << "# define COMMENT_COMMAND(NAME)\n"
108 << "#endif\n";
109
110 std::vector<Record *> Tags = Records.getAllDerivedDefinitions("Command");
111 for (size_t i = 0, e = Tags.size(); i != e; ++i) {
112 Record &Tag = *Tags[i];
113 std::string MangledName = MangleName(Tag.getValueAsString("Name"));
114
115 OS << "COMMENT_COMMAND(" << MangledName << ")\n";
116 }
117}
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000118} // end namespace clang
119