xref: /llvm-project/clang/utils/TableGen/ClangCommentHTMLTagsEmitter.cpp (revision 63aa8cf6becbeb4983e3d1a7fa3cd8a7c7147118)
1 //===-- ClangCommentHTMLTagsEmitter.cpp - Generate HTML tag list ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This tablegen backend emits efficient matchers for HTML tags that are used
10 // in documentation comments.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "TableGenBackends.h"
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 void clang::EmitClangCommentHTMLTags(const RecordKeeper &Records,
23                                      raw_ostream &OS) {
24   ArrayRef<const Record *> Tags = Records.getAllDerivedDefinitions("Tag");
25   std::vector<StringMatcher::StringPair> Matches;
26   for (const Record *Tag : Tags) {
27     Matches.emplace_back(Tag->getValueAsString("Spelling").str(),
28                          "return true;");
29   }
30 
31   emitSourceFileHeader("HTML tag name matcher", OS, Records);
32 
33   OS << "bool isHTMLTagName(StringRef Name) {\n";
34   StringMatcher("Name", Matches, OS).Emit();
35   OS << "  return false;\n"
36      << "}\n\n";
37 }
38 
39 void clang::EmitClangCommentHTMLTagsProperties(const RecordKeeper &Records,
40                                                raw_ostream &OS) {
41   ArrayRef<const Record *> Tags = Records.getAllDerivedDefinitions("Tag");
42   std::vector<StringMatcher::StringPair> MatchesEndTagOptional;
43   std::vector<StringMatcher::StringPair> MatchesEndTagForbidden;
44   for (const Record *Tag : Tags) {
45     std::string Spelling = Tag->getValueAsString("Spelling").str();
46     StringMatcher::StringPair Match(Spelling, "return true;");
47     if (Tag->getValueAsBit("EndTagOptional"))
48       MatchesEndTagOptional.push_back(Match);
49     if (Tag->getValueAsBit("EndTagForbidden"))
50       MatchesEndTagForbidden.push_back(Match);
51   }
52 
53   emitSourceFileHeader("HTML tag properties", OS, Records);
54 
55   OS << "bool isHTMLEndTagOptional(StringRef Name) {\n";
56   StringMatcher("Name", MatchesEndTagOptional, OS).Emit();
57   OS << "  return false;\n"
58      << "}\n\n";
59 
60   OS << "bool isHTMLEndTagForbidden(StringRef Name) {\n";
61   StringMatcher("Name", MatchesEndTagForbidden, OS).Emit();
62   OS << "  return false;\n"
63      << "}\n\n";
64 }
65