1 //===--- 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 //
10 // This tablegen backend emits command lists and efficient matchers for command
11 // names that are used in documentation comments.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/TableGen/Record.h"
16 #include "llvm/TableGen/StringMatcher.h"
17 #include "llvm/TableGen/TableGenBackend.h"
18 #include <vector>
19 
20 using namespace llvm;
21 
22 namespace clang {
EmitClangCommentCommandInfo(RecordKeeper & Records,raw_ostream & OS)23 void EmitClangCommentCommandInfo(RecordKeeper &Records, raw_ostream &OS) {
24   emitSourceFileHeader("A list of commands useable in documentation "
25                        "comments", OS);
26 
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") << ", "
43        << Tag.getValueAsBit("IsThrowsCommand") << ", "
44        << Tag.getValueAsBit("IsDeprecatedCommand") << ", "
45        << Tag.getValueAsBit("IsHeaderfileCommand") << ", "
46        << Tag.getValueAsBit("IsEmptyParagraphAllowed") << ", "
47        << Tag.getValueAsBit("IsVerbatimBlockCommand") << ", "
48        << Tag.getValueAsBit("IsVerbatimBlockEndCommand") << ", "
49        << Tag.getValueAsBit("IsVerbatimLineCommand") << ", "
50        << Tag.getValueAsBit("IsDeclarationCommand") << ", "
51        << Tag.getValueAsBit("IsFunctionDeclarationCommand") << ", "
52        << Tag.getValueAsBit("IsRecordLikeDetailCommand") << ", "
53        << Tag.getValueAsBit("IsRecordLikeDeclarationCommand") << ", "
54        << /* IsUnknownCommand = */ "0"
55        << " }";
56     if (i + 1 != e)
57       OS << ",";
58     OS << "\n";
59   }
60   OS << "};\n"
61         "} // unnamed namespace\n\n";
62 
63   std::vector<StringMatcher::StringPair> Matches;
64   for (size_t i = 0, e = Tags.size(); i != e; ++i) {
65     Record &Tag = *Tags[i];
66     std::string Name = Tag.getValueAsString("Name");
67     std::string Return;
68     raw_string_ostream(Return) << "return &Commands[" << i << "];";
69     Matches.emplace_back(std::move(Name), std::move(Return));
70   }
71 
72   OS << "const CommandInfo *CommandTraits::getBuiltinCommandInfo(\n"
73      << "                                         StringRef Name) {\n";
74   StringMatcher("Name", Matches, OS).Emit();
75   OS << "  return nullptr;\n"
76      << "}\n\n";
77 }
78 
MangleName(StringRef Str)79 static std::string MangleName(StringRef Str) {
80   std::string Mangled;
81   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
82     switch (Str[i]) {
83     default:
84       Mangled += Str[i];
85       break;
86     case '[':
87       Mangled += "lsquare";
88       break;
89     case ']':
90       Mangled += "rsquare";
91       break;
92     case '{':
93       Mangled += "lbrace";
94       break;
95     case '}':
96       Mangled += "rbrace";
97       break;
98     case '$':
99       Mangled += "dollar";
100       break;
101     case '/':
102       Mangled += "slash";
103       break;
104     }
105   }
106   return Mangled;
107 }
108 
EmitClangCommentCommandList(RecordKeeper & Records,raw_ostream & OS)109 void EmitClangCommentCommandList(RecordKeeper &Records, raw_ostream &OS) {
110   emitSourceFileHeader("A list of commands useable in documentation "
111                        "comments", OS);
112 
113   OS << "#ifndef COMMENT_COMMAND\n"
114      << "#  define COMMENT_COMMAND(NAME)\n"
115      << "#endif\n";
116 
117   std::vector<Record *> Tags = Records.getAllDerivedDefinitions("Command");
118   for (size_t i = 0, e = Tags.size(); i != e; ++i) {
119     Record &Tag = *Tags[i];
120     std::string MangledName = MangleName(Tag.getValueAsString("Name"));
121 
122     OS << "COMMENT_COMMAND(" << MangledName << ")\n";
123   }
124 }
125 } // end namespace clang
126