xref: /freebsd/contrib/llvm-project/clang/utils/TableGen/TableGen.cpp (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1 //===- TableGen.cpp - Top-Level TableGen implementation for Clang ---------===//
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 file contains the main function for Clang's TableGen.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "TableGenBackends.h" // Declares all backends.
14 #include "ASTTableGen.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/PrettyStackTrace.h"
17 #include "llvm/Support/Signals.h"
18 #include "llvm/TableGen/Error.h"
19 #include "llvm/TableGen/Main.h"
20 #include "llvm/TableGen/Record.h"
21 
22 using namespace llvm;
23 using namespace clang;
24 
25 enum ActionType {
26   PrintRecords,
27   DumpJSON,
28   GenClangAttrClasses,
29   GenClangAttrParserStringSwitches,
30   GenClangAttrSubjectMatchRulesParserStringSwitches,
31   GenClangAttrImpl,
32   GenClangAttrList,
33   GenClangAttrSubjectMatchRuleList,
34   GenClangAttrPCHRead,
35   GenClangAttrPCHWrite,
36   GenClangAttrHasAttributeImpl,
37   GenClangAttrSpellingListIndex,
38   GenClangAttrASTVisitor,
39   GenClangAttrTemplateInstantiate,
40   GenClangAttrParsedAttrList,
41   GenClangAttrParsedAttrImpl,
42   GenClangAttrParsedAttrKinds,
43   GenClangAttrTextNodeDump,
44   GenClangAttrNodeTraverse,
45   GenClangBasicReader,
46   GenClangBasicWriter,
47   GenClangDiagsDefs,
48   GenClangDiagGroups,
49   GenClangDiagsIndexName,
50   GenClangCommentNodes,
51   GenClangDeclNodes,
52   GenClangStmtNodes,
53   GenClangTypeNodes,
54   GenClangTypeReader,
55   GenClangTypeWriter,
56   GenClangOpcodes,
57   GenClangSACheckers,
58   GenClangSyntaxNodeList,
59   GenClangSyntaxNodeClasses,
60   GenClangCommentHTMLTags,
61   GenClangCommentHTMLTagsProperties,
62   GenClangCommentHTMLNamedCharacterReferences,
63   GenClangCommentCommandInfo,
64   GenClangCommentCommandList,
65   GenClangOpenCLBuiltins,
66   GenClangOpenCLBuiltinTests,
67   GenArmNeon,
68   GenArmFP16,
69   GenArmBF16,
70   GenArmNeonSema,
71   GenArmNeonTest,
72   GenArmMveHeader,
73   GenArmMveBuiltinDef,
74   GenArmMveBuiltinSema,
75   GenArmMveBuiltinCG,
76   GenArmMveBuiltinAliases,
77   GenArmSveHeader,
78   GenArmSveBuiltins,
79   GenArmSveBuiltinCG,
80   GenArmSveTypeFlags,
81   GenArmSveRangeChecks,
82   GenArmCdeHeader,
83   GenArmCdeBuiltinDef,
84   GenArmCdeBuiltinSema,
85   GenArmCdeBuiltinCG,
86   GenArmCdeBuiltinAliases,
87   GenRISCVVectorHeader,
88   GenRISCVVectorBuiltins,
89   GenRISCVVectorBuiltinCG,
90   GenAttrDocs,
91   GenDiagDocs,
92   GenOptDocs,
93   GenDataCollectors,
94   GenTestPragmaAttributeSupportedAttributes
95 };
96 
97 namespace {
98 cl::opt<ActionType> Action(
99     cl::desc("Action to perform:"),
100     cl::values(
101         clEnumValN(PrintRecords, "print-records",
102                    "Print all records to stdout (default)"),
103         clEnumValN(DumpJSON, "dump-json",
104                    "Dump all records as machine-readable JSON"),
105         clEnumValN(GenClangAttrClasses, "gen-clang-attr-classes",
106                    "Generate clang attribute clases"),
107         clEnumValN(GenClangAttrParserStringSwitches,
108                    "gen-clang-attr-parser-string-switches",
109                    "Generate all parser-related attribute string switches"),
110         clEnumValN(GenClangAttrSubjectMatchRulesParserStringSwitches,
111                    "gen-clang-attr-subject-match-rules-parser-string-switches",
112                    "Generate all parser-related attribute subject match rule"
113                    "string switches"),
114         clEnumValN(GenClangAttrImpl, "gen-clang-attr-impl",
115                    "Generate clang attribute implementations"),
116         clEnumValN(GenClangAttrList, "gen-clang-attr-list",
117                    "Generate a clang attribute list"),
118         clEnumValN(GenClangAttrSubjectMatchRuleList,
119                    "gen-clang-attr-subject-match-rule-list",
120                    "Generate a clang attribute subject match rule list"),
121         clEnumValN(GenClangAttrPCHRead, "gen-clang-attr-pch-read",
122                    "Generate clang PCH attribute reader"),
123         clEnumValN(GenClangAttrPCHWrite, "gen-clang-attr-pch-write",
124                    "Generate clang PCH attribute writer"),
125         clEnumValN(GenClangAttrHasAttributeImpl,
126                    "gen-clang-attr-has-attribute-impl",
127                    "Generate a clang attribute spelling list"),
128         clEnumValN(GenClangAttrSpellingListIndex,
129                    "gen-clang-attr-spelling-index",
130                    "Generate a clang attribute spelling index"),
131         clEnumValN(GenClangAttrASTVisitor, "gen-clang-attr-ast-visitor",
132                    "Generate a recursive AST visitor for clang attributes"),
133         clEnumValN(GenClangAttrTemplateInstantiate,
134                    "gen-clang-attr-template-instantiate",
135                    "Generate a clang template instantiate code"),
136         clEnumValN(GenClangAttrParsedAttrList,
137                    "gen-clang-attr-parsed-attr-list",
138                    "Generate a clang parsed attribute list"),
139         clEnumValN(GenClangAttrParsedAttrImpl,
140                    "gen-clang-attr-parsed-attr-impl",
141                    "Generate the clang parsed attribute helpers"),
142         clEnumValN(GenClangAttrParsedAttrKinds,
143                    "gen-clang-attr-parsed-attr-kinds",
144                    "Generate a clang parsed attribute kinds"),
145         clEnumValN(GenClangAttrTextNodeDump, "gen-clang-attr-text-node-dump",
146                    "Generate clang attribute text node dumper"),
147         clEnumValN(GenClangAttrNodeTraverse, "gen-clang-attr-node-traverse",
148                    "Generate clang attribute traverser"),
149         clEnumValN(GenClangDiagsDefs, "gen-clang-diags-defs",
150                    "Generate Clang diagnostics definitions"),
151         clEnumValN(GenClangDiagGroups, "gen-clang-diag-groups",
152                    "Generate Clang diagnostic groups"),
153         clEnumValN(GenClangDiagsIndexName, "gen-clang-diags-index-name",
154                    "Generate Clang diagnostic name index"),
155         clEnumValN(GenClangBasicReader, "gen-clang-basic-reader",
156                    "Generate Clang BasicReader classes"),
157         clEnumValN(GenClangBasicWriter, "gen-clang-basic-writer",
158                    "Generate Clang BasicWriter classes"),
159         clEnumValN(GenClangCommentNodes, "gen-clang-comment-nodes",
160                    "Generate Clang AST comment nodes"),
161         clEnumValN(GenClangDeclNodes, "gen-clang-decl-nodes",
162                    "Generate Clang AST declaration nodes"),
163         clEnumValN(GenClangStmtNodes, "gen-clang-stmt-nodes",
164                    "Generate Clang AST statement nodes"),
165         clEnumValN(GenClangTypeNodes, "gen-clang-type-nodes",
166                    "Generate Clang AST type nodes"),
167         clEnumValN(GenClangTypeReader, "gen-clang-type-reader",
168                    "Generate Clang AbstractTypeReader class"),
169         clEnumValN(GenClangTypeWriter, "gen-clang-type-writer",
170                    "Generate Clang AbstractTypeWriter class"),
171         clEnumValN(GenClangOpcodes, "gen-clang-opcodes",
172                    "Generate Clang constexpr interpreter opcodes"),
173         clEnumValN(GenClangSACheckers, "gen-clang-sa-checkers",
174                    "Generate Clang Static Analyzer checkers"),
175         clEnumValN(GenClangSyntaxNodeList, "gen-clang-syntax-node-list",
176                    "Generate list of Clang Syntax Tree node types"),
177         clEnumValN(GenClangSyntaxNodeClasses, "gen-clang-syntax-node-classes",
178                    "Generate definitions of Clang Syntax Tree node clasess"),
179         clEnumValN(GenClangCommentHTMLTags, "gen-clang-comment-html-tags",
180                    "Generate efficient matchers for HTML tag "
181                    "names that are used in documentation comments"),
182         clEnumValN(GenClangCommentHTMLTagsProperties,
183                    "gen-clang-comment-html-tags-properties",
184                    "Generate efficient matchers for HTML tag "
185                    "properties"),
186         clEnumValN(GenClangCommentHTMLNamedCharacterReferences,
187                    "gen-clang-comment-html-named-character-references",
188                    "Generate function to translate named character "
189                    "references to UTF-8 sequences"),
190         clEnumValN(GenClangCommentCommandInfo, "gen-clang-comment-command-info",
191                    "Generate command properties for commands that "
192                    "are used in documentation comments"),
193         clEnumValN(GenClangCommentCommandList, "gen-clang-comment-command-list",
194                    "Generate list of commands that are used in "
195                    "documentation comments"),
196         clEnumValN(GenClangOpenCLBuiltins, "gen-clang-opencl-builtins",
197                    "Generate OpenCL builtin declaration handlers"),
198         clEnumValN(GenClangOpenCLBuiltinTests, "gen-clang-opencl-builtin-tests",
199                    "Generate OpenCL builtin declaration tests"),
200         clEnumValN(GenArmNeon, "gen-arm-neon", "Generate arm_neon.h for clang"),
201         clEnumValN(GenArmFP16, "gen-arm-fp16", "Generate arm_fp16.h for clang"),
202         clEnumValN(GenArmBF16, "gen-arm-bf16", "Generate arm_bf16.h for clang"),
203         clEnumValN(GenArmNeonSema, "gen-arm-neon-sema",
204                    "Generate ARM NEON sema support for clang"),
205         clEnumValN(GenArmNeonTest, "gen-arm-neon-test",
206                    "Generate ARM NEON tests for clang"),
207         clEnumValN(GenArmSveHeader, "gen-arm-sve-header",
208                    "Generate arm_sve.h for clang"),
209         clEnumValN(GenArmSveBuiltins, "gen-arm-sve-builtins",
210                    "Generate arm_sve_builtins.inc for clang"),
211         clEnumValN(GenArmSveBuiltinCG, "gen-arm-sve-builtin-codegen",
212                    "Generate arm_sve_builtin_cg_map.inc for clang"),
213         clEnumValN(GenArmSveTypeFlags, "gen-arm-sve-typeflags",
214                    "Generate arm_sve_typeflags.inc for clang"),
215         clEnumValN(GenArmSveRangeChecks, "gen-arm-sve-sema-rangechecks",
216                    "Generate arm_sve_sema_rangechecks.inc for clang"),
217         clEnumValN(GenArmMveHeader, "gen-arm-mve-header",
218                    "Generate arm_mve.h for clang"),
219         clEnumValN(GenArmMveBuiltinDef, "gen-arm-mve-builtin-def",
220                    "Generate ARM MVE builtin definitions for clang"),
221         clEnumValN(GenArmMveBuiltinSema, "gen-arm-mve-builtin-sema",
222                    "Generate ARM MVE builtin sema checks for clang"),
223         clEnumValN(GenArmMveBuiltinCG, "gen-arm-mve-builtin-codegen",
224                    "Generate ARM MVE builtin code-generator for clang"),
225         clEnumValN(GenArmMveBuiltinAliases, "gen-arm-mve-builtin-aliases",
226                    "Generate list of valid ARM MVE builtin aliases for clang"),
227         clEnumValN(GenArmCdeHeader, "gen-arm-cde-header",
228                    "Generate arm_cde.h for clang"),
229         clEnumValN(GenArmCdeBuiltinDef, "gen-arm-cde-builtin-def",
230                    "Generate ARM CDE builtin definitions for clang"),
231         clEnumValN(GenArmCdeBuiltinSema, "gen-arm-cde-builtin-sema",
232                    "Generate ARM CDE builtin sema checks for clang"),
233         clEnumValN(GenArmCdeBuiltinCG, "gen-arm-cde-builtin-codegen",
234                    "Generate ARM CDE builtin code-generator for clang"),
235         clEnumValN(GenArmCdeBuiltinAliases, "gen-arm-cde-builtin-aliases",
236                    "Generate list of valid ARM CDE builtin aliases for clang"),
237         clEnumValN(GenRISCVVectorHeader, "gen-riscv-vector-header",
238                    "Generate riscv_vector.h for clang"),
239         clEnumValN(GenRISCVVectorBuiltins, "gen-riscv-vector-builtins",
240                    "Generate riscv_vector_builtins.inc for clang"),
241         clEnumValN(GenRISCVVectorBuiltinCG, "gen-riscv-vector-builtin-codegen",
242                    "Generate riscv_vector_builtin_cg.inc for clang"),
243         clEnumValN(GenAttrDocs, "gen-attr-docs",
244                    "Generate attribute documentation"),
245         clEnumValN(GenDiagDocs, "gen-diag-docs",
246                    "Generate diagnostic documentation"),
247         clEnumValN(GenOptDocs, "gen-opt-docs", "Generate option documentation"),
248         clEnumValN(GenDataCollectors, "gen-clang-data-collectors",
249                    "Generate data collectors for AST nodes"),
250         clEnumValN(GenTestPragmaAttributeSupportedAttributes,
251                    "gen-clang-test-pragma-attribute-supported-attributes",
252                    "Generate a list of attributes supported by #pragma clang "
253                    "attribute for testing purposes")));
254 
255 cl::opt<std::string>
256 ClangComponent("clang-component",
257                cl::desc("Only use warnings from specified component"),
258                cl::value_desc("component"), cl::Hidden);
259 
260 bool ClangTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
261   switch (Action) {
262   case PrintRecords:
263     OS << Records;           // No argument, dump all contents
264     break;
265   case DumpJSON:
266     EmitJSON(Records, OS);
267     break;
268   case GenClangAttrClasses:
269     EmitClangAttrClass(Records, OS);
270     break;
271   case GenClangAttrParserStringSwitches:
272     EmitClangAttrParserStringSwitches(Records, OS);
273     break;
274   case GenClangAttrSubjectMatchRulesParserStringSwitches:
275     EmitClangAttrSubjectMatchRulesParserStringSwitches(Records, OS);
276     break;
277   case GenClangAttrImpl:
278     EmitClangAttrImpl(Records, OS);
279     break;
280   case GenClangAttrList:
281     EmitClangAttrList(Records, OS);
282     break;
283   case GenClangAttrSubjectMatchRuleList:
284     EmitClangAttrSubjectMatchRuleList(Records, OS);
285     break;
286   case GenClangAttrPCHRead:
287     EmitClangAttrPCHRead(Records, OS);
288     break;
289   case GenClangAttrPCHWrite:
290     EmitClangAttrPCHWrite(Records, OS);
291     break;
292   case GenClangAttrHasAttributeImpl:
293     EmitClangAttrHasAttrImpl(Records, OS);
294     break;
295   case GenClangAttrSpellingListIndex:
296     EmitClangAttrSpellingListIndex(Records, OS);
297     break;
298   case GenClangAttrASTVisitor:
299     EmitClangAttrASTVisitor(Records, OS);
300     break;
301   case GenClangAttrTemplateInstantiate:
302     EmitClangAttrTemplateInstantiate(Records, OS);
303     break;
304   case GenClangAttrParsedAttrList:
305     EmitClangAttrParsedAttrList(Records, OS);
306     break;
307   case GenClangAttrParsedAttrImpl:
308     EmitClangAttrParsedAttrImpl(Records, OS);
309     break;
310   case GenClangAttrParsedAttrKinds:
311     EmitClangAttrParsedAttrKinds(Records, OS);
312     break;
313   case GenClangAttrTextNodeDump:
314     EmitClangAttrTextNodeDump(Records, OS);
315     break;
316   case GenClangAttrNodeTraverse:
317     EmitClangAttrNodeTraverse(Records, OS);
318     break;
319   case GenClangDiagsDefs:
320     EmitClangDiagsDefs(Records, OS, ClangComponent);
321     break;
322   case GenClangDiagGroups:
323     EmitClangDiagGroups(Records, OS);
324     break;
325   case GenClangDiagsIndexName:
326     EmitClangDiagsIndexName(Records, OS);
327     break;
328   case GenClangCommentNodes:
329     EmitClangASTNodes(Records, OS, CommentNodeClassName, "");
330     break;
331   case GenClangDeclNodes:
332     EmitClangASTNodes(Records, OS, DeclNodeClassName, "Decl");
333     EmitClangDeclContext(Records, OS);
334     break;
335   case GenClangStmtNodes:
336     EmitClangASTNodes(Records, OS, StmtNodeClassName, "");
337     break;
338   case GenClangTypeNodes:
339     EmitClangTypeNodes(Records, OS);
340     break;
341   case GenClangTypeReader:
342     EmitClangTypeReader(Records, OS);
343     break;
344   case GenClangTypeWriter:
345     EmitClangTypeWriter(Records, OS);
346     break;
347   case GenClangBasicReader:
348     EmitClangBasicReader(Records, OS);
349     break;
350   case GenClangBasicWriter:
351     EmitClangBasicWriter(Records, OS);
352     break;
353   case GenClangOpcodes:
354     EmitClangOpcodes(Records, OS);
355     break;
356   case GenClangSACheckers:
357     EmitClangSACheckers(Records, OS);
358     break;
359   case GenClangCommentHTMLTags:
360     EmitClangCommentHTMLTags(Records, OS);
361     break;
362   case GenClangCommentHTMLTagsProperties:
363     EmitClangCommentHTMLTagsProperties(Records, OS);
364     break;
365   case GenClangCommentHTMLNamedCharacterReferences:
366     EmitClangCommentHTMLNamedCharacterReferences(Records, OS);
367     break;
368   case GenClangCommentCommandInfo:
369     EmitClangCommentCommandInfo(Records, OS);
370     break;
371   case GenClangCommentCommandList:
372     EmitClangCommentCommandList(Records, OS);
373     break;
374   case GenClangOpenCLBuiltins:
375     EmitClangOpenCLBuiltins(Records, OS);
376     break;
377   case GenClangOpenCLBuiltinTests:
378     EmitClangOpenCLBuiltinTests(Records, OS);
379     break;
380   case GenClangSyntaxNodeList:
381     EmitClangSyntaxNodeList(Records, OS);
382     break;
383   case GenClangSyntaxNodeClasses:
384     EmitClangSyntaxNodeClasses(Records, OS);
385     break;
386   case GenArmNeon:
387     EmitNeon(Records, OS);
388     break;
389   case GenArmFP16:
390     EmitFP16(Records, OS);
391     break;
392   case GenArmBF16:
393     EmitBF16(Records, OS);
394     break;
395   case GenArmNeonSema:
396     EmitNeonSema(Records, OS);
397     break;
398   case GenArmNeonTest:
399     EmitNeonTest(Records, OS);
400     break;
401   case GenArmMveHeader:
402     EmitMveHeader(Records, OS);
403     break;
404   case GenArmMveBuiltinDef:
405     EmitMveBuiltinDef(Records, OS);
406     break;
407   case GenArmMveBuiltinSema:
408     EmitMveBuiltinSema(Records, OS);
409     break;
410   case GenArmMveBuiltinCG:
411     EmitMveBuiltinCG(Records, OS);
412     break;
413   case GenArmMveBuiltinAliases:
414     EmitMveBuiltinAliases(Records, OS);
415     break;
416   case GenArmSveHeader:
417     EmitSveHeader(Records, OS);
418     break;
419   case GenArmSveBuiltins:
420     EmitSveBuiltins(Records, OS);
421     break;
422   case GenArmSveBuiltinCG:
423     EmitSveBuiltinCG(Records, OS);
424     break;
425   case GenArmSveTypeFlags:
426     EmitSveTypeFlags(Records, OS);
427     break;
428   case GenArmSveRangeChecks:
429     EmitSveRangeChecks(Records, OS);
430     break;
431   case GenArmCdeHeader:
432     EmitCdeHeader(Records, OS);
433     break;
434   case GenArmCdeBuiltinDef:
435     EmitCdeBuiltinDef(Records, OS);
436     break;
437   case GenArmCdeBuiltinSema:
438     EmitCdeBuiltinSema(Records, OS);
439     break;
440   case GenArmCdeBuiltinCG:
441     EmitCdeBuiltinCG(Records, OS);
442     break;
443   case GenArmCdeBuiltinAliases:
444     EmitCdeBuiltinAliases(Records, OS);
445     break;
446   case GenRISCVVectorHeader:
447     EmitRVVHeader(Records, OS);
448     break;
449   case GenRISCVVectorBuiltins:
450     EmitRVVBuiltins(Records, OS);
451     break;
452   case GenRISCVVectorBuiltinCG:
453     EmitRVVBuiltinCG(Records, OS);
454     break;
455   case GenAttrDocs:
456     EmitClangAttrDocs(Records, OS);
457     break;
458   case GenDiagDocs:
459     EmitClangDiagDocs(Records, OS);
460     break;
461   case GenOptDocs:
462     EmitClangOptDocs(Records, OS);
463     break;
464   case GenDataCollectors:
465     EmitClangDataCollectors(Records, OS);
466     break;
467   case GenTestPragmaAttributeSupportedAttributes:
468     EmitTestPragmaAttributeSupportedAttributes(Records, OS);
469     break;
470   }
471 
472   return false;
473 }
474 }
475 
476 int main(int argc, char **argv) {
477   sys::PrintStackTraceOnErrorSignal(argv[0]);
478   PrettyStackTraceProgram X(argc, argv);
479   cl::ParseCommandLineOptions(argc, argv);
480 
481   llvm_shutdown_obj Y;
482 
483   return TableGenMain(argv[0], &ClangTableGenMain);
484 }
485 
486 #ifdef __has_feature
487 #if __has_feature(address_sanitizer)
488 #include <sanitizer/lsan_interface.h>
489 // Disable LeakSanitizer for this binary as it has too many leaks that are not
490 // very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h .
491 int __lsan_is_turned_off() { return 1; }
492 #endif  // __has_feature(address_sanitizer)
493 #endif  // defined(__has_feature)
494