Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:insignificant reason:build-tool
4
5#include "lalr.h"
6#include "dotgraph.h"
7#include "parsetable.h"
8#include "cppgenerator.h"
9#include "recognizer.h"
10
11#include <QtCore/qcoreapplication.h>
12#include <QtCore/qfile.h>
13#include <QtCore/qstringlist.h>
14#include <QtCore/qdebug.h>
15
16#include <cstdlib>
17
18#define QLALR_NO_DEBUG_TABLE
19#define QLALR_NO_DEBUG_DOT
20
21using namespace Qt::StringLiterals;
22
23static void help_me ()
24{
25 qerr() << "Usage: qlalr [options] [input file name]" << Qt::endl
26 << Qt::endl
27 << " --help, -h\t\tdisplay this help and exit" << Qt::endl
28 << " --verbose, -v\t\tverbose output" << Qt::endl
29 << " --no-debug\t\tno debug information" << Qt::endl
30 << " --no-lines\t\tno #line directives" << Qt::endl
31 << " --dot\t\t\tgenerate a graph" << Qt::endl
32 << " --use-pragma-once\tuse #pragma once instead of ifndef/define header guards" << Qt::endl
33 << " --qt-security={critical,significant,insignificant}[:<reason>]" << Qt::endl
34 << "\t\t\tmark the output files with Qt-Security (QUIP-23) headers" << Qt::endl
35 << "\t\t\twith given score and free-form <reason>" << Qt::endl
36 << " --qt\t\t\tadd the Qt copyright header and Qt-specific types and macros" << Qt::endl
37 << " --no-copyright-header\tdisable automatic copyright header from input file" << Qt::endl
38 << " --exit-on-warn\texit with status code 2 on warning" << Qt::endl
39 << Qt::endl;
40 exit (0);
41}
42
43int main (int argc, char *argv[])
44{
45 QCoreApplication app (argc, argv);
46
47 bool generate_dot = false;
48 bool generate_report = false;
49 bool no_lines = false;
50 bool debug_info = true;
51 bool use_pragma_once = false;
52 std::optional<CppGenerator::SecurityHeader> security;
53 bool qt_copyright = false;
54 bool no_copyright_header = false;
55 bool warnings_are_errors = false;
56 QString file_name;
57
58 const QStringList args = app.arguments().mid(1);
59 for (const QString &arg : args) {
60 if (arg == "-h"_L1 || arg == "--help"_L1)
61 help_me ();
62
63 else if (arg == "-v"_L1 || arg == "--verbose"_L1)
64 generate_report = true;
65
66 else if (arg == "--dot"_L1)
67 generate_dot = true;
68
69 else if (arg == "--no-lines"_L1)
70 no_lines = true;
71
72 else if (arg == "--no-debug"_L1)
73 debug_info = false;
74
75 else if (arg == "--use-pragma-once"_L1)
76 use_pragma_once = true;
77
78 else if (constexpr auto qtsec = "--qt-security="_L1; arg.startsWith(qtsec))
79 {
80 auto sh = CppGenerator::SecurityHeader::parse(QStringView{arg}.slice(qtsec.size()));
81 if (!sh)
82 qerr() << "*** Warning. Could not parse `" << arg << "'" << Qt::endl;
83 else
84 security = std::move(sh);
85 }
86
87 else if (arg == "--qt"_L1)
88 qt_copyright = true;
89
90 else if (arg == "--no-copyright-header"_L1)
91 no_copyright_header = true;
92
93 else if (arg == "--exit-on-warn"_L1)
94 warnings_are_errors = true;
95
96 else if (file_name.isEmpty ())
97 file_name = arg;
98
99 else
100 qerr() << "*** Warning. Ignore argument `" << arg << "'" << Qt::endl;
101 }
102
103 if (file_name.isEmpty ())
104 {
105 help_me ();
106 exit (EXIT_SUCCESS);
107 }
108
109 Grammar grammar;
110 Recognizer p (&grammar, no_lines);
111
112 if (! p.parse (file_name))
113 exit (EXIT_FAILURE);
114
115 if (grammar.rules.empty())
116 {
117 qerr() << "*** Fatal. No rules!" << Qt::endl;
118 exit (EXIT_FAILURE);
119 }
120
121 else if (grammar.start == grammar.names.end ())
122 {
123 qerr() << "*** Fatal. No start symbol!" << Qt::endl;
124 exit (EXIT_FAILURE);
125 }
126
128 grammar.buildRuleMap ();
129
130 Automaton aut (&grammar);
131 aut.build ();
132
133 CppGenerator gen (p, grammar, aut, generate_report);
134 gen.setDebugInfo (debug_info);
135 gen.setUsePragmaOnce(use_pragma_once);
136 if (security)
137 gen.setSecurityHeader(std::move(*security));
138 if (qt_copyright) {
139 gen.setCopyright (true);
140 gen.setEmitQtCode (true);
141 } else if (!no_copyright_header && !grammar.inputCopyright.isEmpty()) {
142 gen.setCopyright (true);
143 gen.setCopyrightText (grammar.inputCopyright);
144 }
145 gen.setWarningsAreErrors (warnings_are_errors);
146 gen ();
147
148 if (generate_dot)
149 {
150 DotGraph genDotFile (qout());
151 genDotFile (&aut);
152 }
153
154 else if (generate_report)
155 {
156 ParseTable genParseTable (qout());
157 genParseTable(&aut);
158 }
159
160 return EXIT_SUCCESS;
161}
162
163QString Recognizer::expand (const QString &text) const
164{
165 QString code = text;
166
167 if (_M_grammar->start != _M_grammar->names.end ())
168 {
169 code = code.replace ("$start_id"_L1, QString::number (std::distance (_M_grammar->names.begin (), _M_grammar->start)));
170 code = code.replace ("$start"_L1, *_M_grammar->start);
171 }
172
173 code = code.replace ("$header"_L1, _M_grammar->table_name.toLower () + "_p.h"_L1);
174
175 code = code.replace ("$table"_L1, _M_grammar->table_name);
176 code = code.replace ("$parser"_L1, _M_grammar->table_name);
177
178 if (_M_current_rule != _M_grammar->rules.end ())
179 {
180 code = code.replace ("$rule_number"_L1, QString::number (std::distance (_M_grammar->rules.begin (), _M_current_rule)));
181 code = code.replace ("$rule"_L1, *_M_current_rule->lhs);
182 }
183
184 return code;
185}
Automaton(Grammar *g)
Definition lalr.cpp:237
void build()
Definition lalr.cpp:258
CppGenerator(const Recognizer &p, Grammar &grammar, Automaton &aut, bool verbose)
void setUsePragmaOnce(bool use)
void setWarningsAreErrors(bool e)
void setSecurityHeader(SecurityHeader header)
void setDebugInfo(bool d)
void setEmitQtCode(bool t)
void setCopyright(bool t)
void operator()(Automaton *a)
Definition dotgraph.cpp:16
debug_infot rules
Definition lalr.h:242
Name start
Definition lalr.h:238
void buildRuleMap()
Definition lalr.cpp:194
void buildExtendedGrammar()
Definition lalr.cpp:214
void operator()(Automaton *a)
QTextStream & qout()
Definition qev.cpp:12
static void help_me()
Definition main.cpp:23
int main(int argc, char *argv[])
[ctor_close]