blob: b0bf75217dd43f6776334f320e5ff567aeac6736 [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") << ", "
Fariborz Jahanian2a268f22013-03-05 01:05:07 +000050 << Tag.getValueAsBit("IsFunctionDeclarationCommand") << ", "
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000051 << /* IsUnknownCommand = */ "0"
52 << " }";
53 if (i + 1 != e)
54 OS << ",";
55 OS << "\n";
56 }
57 OS << "};\n"
58 "} // unnamed namespace\n\n";
59
60 std::vector<StringMatcher::StringPair> Matches;
61 for (size_t i = 0, e = Tags.size(); i != e; ++i) {
62 Record &Tag = *Tags[i];
63 std::string Name = Tag.getValueAsString("Name");
64 std::string Return;
65 raw_string_ostream(Return) << "return &Commands[" << i << "];";
66 Matches.push_back(StringMatcher::StringPair(Name, Return));
67 }
68
69 OS << "const CommandInfo *CommandTraits::getBuiltinCommandInfo(\n"
70 << " StringRef Name) {\n";
71 StringMatcher("Name", Matches, OS).Emit();
72 OS << " return NULL;\n"
73 << "}\n\n";
74}
Dmitri Gribenkoaf01bed2013-02-01 20:23:57 +000075
76static std::string MangleName(StringRef Str) {
77 std::string Mangled;
78 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
79 switch (Str[i]) {
80 default:
81 Mangled += Str[i];
82 break;
83 case '[':
84 Mangled += "lsquare";
85 break;
86 case ']':
87 Mangled += "rsquare";
88 break;
89 case '{':
90 Mangled += "lbrace";
91 break;
92 case '}':
93 Mangled += "rbrace";
94 break;
95 case '$':
96 Mangled += "dollar";
97 break;
98 }
99 }
100 return Mangled;
101}
102
103void EmitClangCommentCommandList(RecordKeeper &Records, raw_ostream &OS) {
104 emitSourceFileHeader("A list of commands useable in documentation "
105 "comments", OS);
106
107 OS << "#ifndef COMMENT_COMMAND\n"
108 << "# define COMMENT_COMMAND(NAME)\n"
109 << "#endif\n";
110
111 std::vector<Record *> Tags = Records.getAllDerivedDefinitions("Command");
112 for (size_t i = 0, e = Tags.size(); i != e; ++i) {
113 Record &Tag = *Tags[i];
114 std::string MangledName = MangleName(Tag.getValueAsString("Name"));
115
116 OS << "COMMENT_COMMAND(" << MangledName << ")\n";
117 }
118}
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000119} // end namespace clang
120