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