10b57cec5SDimitry Andric //===--- ClangCommentCommandInfoEmitter.cpp - Generate command lists -----====// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This tablegen backend emits command lists and efficient matchers for command 100b57cec5SDimitry Andric // names that are used in documentation comments. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 14a7dea167SDimitry Andric #include "TableGenBackends.h" 15a7dea167SDimitry Andric 160b57cec5SDimitry Andric #include "llvm/TableGen/Record.h" 170b57cec5SDimitry Andric #include "llvm/TableGen/StringMatcher.h" 180b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h" 190b57cec5SDimitry Andric #include <vector> 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace llvm; 220b57cec5SDimitry Andric 23a7dea167SDimitry Andric void clang::EmitClangCommentCommandInfo(RecordKeeper &Records, raw_ostream &OS) { 240b57cec5SDimitry Andric emitSourceFileHeader("A list of commands useable in documentation " 250b57cec5SDimitry Andric "comments", OS); 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric OS << "namespace {\n" 280b57cec5SDimitry Andric "const CommandInfo Commands[] = {\n"; 290b57cec5SDimitry Andric std::vector<Record *> Tags = Records.getAllDerivedDefinitions("Command"); 300b57cec5SDimitry Andric for (size_t i = 0, e = Tags.size(); i != e; ++i) { 310b57cec5SDimitry Andric Record &Tag = *Tags[i]; 320b57cec5SDimitry Andric OS << " { " 330b57cec5SDimitry Andric << "\"" << Tag.getValueAsString("Name") << "\", " 340b57cec5SDimitry Andric << "\"" << Tag.getValueAsString("EndCommandName") << "\", " 350b57cec5SDimitry Andric << i << ", " 360b57cec5SDimitry Andric << Tag.getValueAsInt("NumArgs") << ", " 370b57cec5SDimitry Andric << Tag.getValueAsBit("IsInlineCommand") << ", " 380b57cec5SDimitry Andric << Tag.getValueAsBit("IsBlockCommand") << ", " 390b57cec5SDimitry Andric << Tag.getValueAsBit("IsBriefCommand") << ", " 400b57cec5SDimitry Andric << Tag.getValueAsBit("IsReturnsCommand") << ", " 410b57cec5SDimitry Andric << Tag.getValueAsBit("IsParamCommand") << ", " 420b57cec5SDimitry Andric << Tag.getValueAsBit("IsTParamCommand") << ", " 430b57cec5SDimitry Andric << Tag.getValueAsBit("IsThrowsCommand") << ", " 440b57cec5SDimitry Andric << Tag.getValueAsBit("IsDeprecatedCommand") << ", " 450b57cec5SDimitry Andric << Tag.getValueAsBit("IsHeaderfileCommand") << ", " 460b57cec5SDimitry Andric << Tag.getValueAsBit("IsEmptyParagraphAllowed") << ", " 470b57cec5SDimitry Andric << Tag.getValueAsBit("IsVerbatimBlockCommand") << ", " 480b57cec5SDimitry Andric << Tag.getValueAsBit("IsVerbatimBlockEndCommand") << ", " 490b57cec5SDimitry Andric << Tag.getValueAsBit("IsVerbatimLineCommand") << ", " 500b57cec5SDimitry Andric << Tag.getValueAsBit("IsDeclarationCommand") << ", " 510b57cec5SDimitry Andric << Tag.getValueAsBit("IsFunctionDeclarationCommand") << ", " 520b57cec5SDimitry Andric << Tag.getValueAsBit("IsRecordLikeDetailCommand") << ", " 530b57cec5SDimitry Andric << Tag.getValueAsBit("IsRecordLikeDeclarationCommand") << ", " 540b57cec5SDimitry Andric << /* IsUnknownCommand = */ "0" 550b57cec5SDimitry Andric << " }"; 560b57cec5SDimitry Andric if (i + 1 != e) 570b57cec5SDimitry Andric OS << ","; 580b57cec5SDimitry Andric OS << "\n"; 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric OS << "};\n" 610b57cec5SDimitry Andric "} // unnamed namespace\n\n"; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric std::vector<StringMatcher::StringPair> Matches; 640b57cec5SDimitry Andric for (size_t i = 0, e = Tags.size(); i != e; ++i) { 650b57cec5SDimitry Andric Record &Tag = *Tags[i]; 665ffd83dbSDimitry Andric std::string Name = std::string(Tag.getValueAsString("Name")); 670b57cec5SDimitry Andric std::string Return; 680b57cec5SDimitry Andric raw_string_ostream(Return) << "return &Commands[" << i << "];"; 690b57cec5SDimitry Andric Matches.emplace_back(std::move(Name), std::move(Return)); 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric OS << "const CommandInfo *CommandTraits::getBuiltinCommandInfo(\n" 730b57cec5SDimitry Andric << " StringRef Name) {\n"; 740b57cec5SDimitry Andric StringMatcher("Name", Matches, OS).Emit(); 750b57cec5SDimitry Andric OS << " return nullptr;\n" 760b57cec5SDimitry Andric << "}\n\n"; 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric static std::string MangleName(StringRef Str) { 800b57cec5SDimitry Andric std::string Mangled; 810b57cec5SDimitry Andric for (unsigned i = 0, e = Str.size(); i != e; ++i) { 820b57cec5SDimitry Andric switch (Str[i]) { 830b57cec5SDimitry Andric default: 840b57cec5SDimitry Andric Mangled += Str[i]; 850b57cec5SDimitry Andric break; 86*349cc55cSDimitry Andric case '(': 87*349cc55cSDimitry Andric Mangled += "lparen"; 88*349cc55cSDimitry Andric break; 89*349cc55cSDimitry Andric case ')': 90*349cc55cSDimitry Andric Mangled += "rparen"; 91*349cc55cSDimitry Andric break; 920b57cec5SDimitry Andric case '[': 930b57cec5SDimitry Andric Mangled += "lsquare"; 940b57cec5SDimitry Andric break; 950b57cec5SDimitry Andric case ']': 960b57cec5SDimitry Andric Mangled += "rsquare"; 970b57cec5SDimitry Andric break; 980b57cec5SDimitry Andric case '{': 990b57cec5SDimitry Andric Mangled += "lbrace"; 1000b57cec5SDimitry Andric break; 1010b57cec5SDimitry Andric case '}': 1020b57cec5SDimitry Andric Mangled += "rbrace"; 1030b57cec5SDimitry Andric break; 1040b57cec5SDimitry Andric case '$': 1050b57cec5SDimitry Andric Mangled += "dollar"; 1060b57cec5SDimitry Andric break; 1070b57cec5SDimitry Andric case '/': 1080b57cec5SDimitry Andric Mangled += "slash"; 1090b57cec5SDimitry Andric break; 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric return Mangled; 1130b57cec5SDimitry Andric } 1140b57cec5SDimitry Andric 115a7dea167SDimitry Andric void clang::EmitClangCommentCommandList(RecordKeeper &Records, raw_ostream &OS) { 1160b57cec5SDimitry Andric emitSourceFileHeader("A list of commands useable in documentation " 1170b57cec5SDimitry Andric "comments", OS); 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric OS << "#ifndef COMMENT_COMMAND\n" 1200b57cec5SDimitry Andric << "# define COMMENT_COMMAND(NAME)\n" 1210b57cec5SDimitry Andric << "#endif\n"; 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric std::vector<Record *> Tags = Records.getAllDerivedDefinitions("Command"); 1240b57cec5SDimitry Andric for (size_t i = 0, e = Tags.size(); i != e; ++i) { 1250b57cec5SDimitry Andric Record &Tag = *Tags[i]; 1260b57cec5SDimitry Andric std::string MangledName = MangleName(Tag.getValueAsString("Name")); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric OS << "COMMENT_COMMAND(" << MangledName << ")\n"; 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric } 131