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
symbols.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
4
5#ifndef SYMBOLS_H
6#define SYMBOLS_H
7
8#include "token.h"
9#include <qhashfunctions.h>
10#include <qlist.h>
11#include <qstack.h>
12#include <qstring.h>
13#include <qset.h>
14
16
18{
19 inline SubArray() = default;
20 inline SubArray(const QByteArray &a):array(a),from(0), len(a.size()){}
21 inline SubArray(const char *s):array(s),from(0) { len = array.size(); }
22 SubArray(const QByteArray &a, qsizetype from, qsizetype len)
23 : array(a), from(from), len(len)
24 {
25 }
29 inline bool operator==(const SubArray &other) const {
30 if (len != other.len)
31 return false;
32 const auto begin = array.cbegin() + from;
33 const auto end = begin + len;
34 const auto other_begin = other.array.cbegin() + other.from;
35 return std::equal(begin, end, other_begin);
36 }
37};
38
39inline size_t qHash(const SubArray &key, size_t seed = 0)
40{
41 return qHash(QLatin1StringView(key.array.constData() + key.from, key.len), seed);
42}
43
44
45struct Symbol
46{
47 inline Symbol() = default;
48 inline Symbol(int lineNum, Token token) : lineNum(lineNum), token(token) { }
49 inline Symbol(int lineNum, Token token, const QByteArray &lexem)
50 : lineNum(lineNum), token(token), lex(lexem), len(lex.size())
51 {
52 }
53 Symbol(int lineNum, Token token, const QByteArray &lexem, qsizetype from, qsizetype len)
54 : lineNum(lineNum), token(token), lex(lexem), from(from), len(len)
55 {
56 }
57 int lineNum = -1;
58 Token token = NOTOKEN;
59 inline QByteArray lexem() const { return lex.mid(from, len); }
60 inline QByteArray unquotedLexem() const { return lex.mid(from+1, len-2); }
61 inline QByteArrayView lexemView() const { return QByteArrayView{lex}.mid(from, len); }
62 inline QByteArrayView unquotedLexemView() const { return QByteArrayView{lex}.mid(from+1, len-2); }
63 void mergeStringLiteral(const Symbol &next);
64 inline operator SubArray() const { return SubArray(lex, from, len); }
65 bool operator==(const Symbol& o) const
66 {
67 return SubArray(lex, from, len) == SubArray(o.lex, o.from, o.len);
68 }
72};
74
76
84
86{
87public:
88 const SafeSymbols &constTop() const { return top(); }
89 inline bool hasNext() {
90 while (!isEmpty() && constTop().index >= constTop().symbols.size())
91 pop();
92 return !isEmpty();
93 }
94 inline Token next() {
95 while (!isEmpty() && constTop().index >= constTop().symbols.size())
96 pop();
97 if (isEmpty())
98 return NOTOKEN;
99 return constTop().symbols.at(top().index++).token;
100 }
101 bool test(Token);
102 inline const Symbol &symbol() const { return constTop().symbols.at(constTop().index-1); }
103 inline Token token() { return symbol().token; }
104 inline QByteArray lexem() const { return symbol().lexem(); }
105 inline QByteArray unquotedLexem() { return symbol().unquotedLexem(); }
106
107 bool dontReplaceSymbol(const QByteArray &name) const;
109};
110
111inline bool SymbolStack::test(Token token)
112{
113 qsizetype stackPos = size() - 1;
114 while (stackPos >= 0 && at(stackPos).index >= at(stackPos).symbols.size())
115 --stackPos;
116 if (stackPos < 0)
117 return false;
118 if (at(stackPos).symbols.at(at(stackPos).index).token == token) {
119 next();
120 return true;
121 }
122 return false;
123}
124
125inline bool SymbolStack::dontReplaceSymbol(const QByteArray &name) const
126{
127 auto matchesName = [&name](const SafeSymbols &sf) {
128 return name == sf.expandedMacro || sf.excludedSymbols.contains(name);
129 };
130 return std::any_of(cbegin(), cend(), matchesName);
131}
132
133inline QSet<QByteArray> SymbolStack::excludeSymbols() const
134{
135 QSet<QByteArray> set;
136 for (const SafeSymbols &sf : *this) {
137 set << sf.expandedMacro;
138 set += sf.excludedSymbols;
139 }
140 return set;
141}
142
143QT_END_NAMESPACE
144
145#endif // SYMBOLS_H
Definition moc.h:223
void parse()
Definition moc.cpp:677
bool requireCompleteTypes
Definition moc.h:235
void generate(FILE *out, FILE *jsonOutput)
Definition moc.cpp:1190
bool noInclude
Definition moc.h:233
int evaluateCondition()
void setDebugIncludes(bool value)
void parseDefineArguments(Macro *m)
void skipUntilEndif()
QHash< QByteArray, QByteArray > nonlocalIncludePathResolutionCache
Symbols preprocessed(const QByteArray &filename, QFile *device)
void substituteUntilNewline(Symbols &substituted)
QList< QByteArray > frameworks
static bool preprocessOnly
static Symbols macroExpandIdentifier(Preprocessor *that, SymbolStack &symbols, int lineNum, QByteArray *macroName)
QByteArray resolveInclude(const QByteArray &filename, const QByteArray &relativeTo)
static Symbols tokenize(const QByteArray &input, int lineNum=1, TokenizeMode mode=TokenizeCpp)
static void macroExpand(Symbols *into, Preprocessor *that, const Symbols &toExpand, qsizetype &index, int lineNum, bool one, const QSet< QByteArray > &excludeSymbols=QSet< QByteArray >())
@ PreparePreprocessorStatement
@ TokenizePreprocessorStatement
The QCommandLineOption class defines a possible command-line option. \inmodule QtCore.
The QCommandLineParser class provides a means for handling the command line options.
Definition qlist.h:80
QByteArray unquotedLexem()
Definition symbols.h:105
const Symbol & symbol() const
Definition symbols.h:102
Token token()
Definition symbols.h:103
bool dontReplaceSymbol(const QByteArray &name) const
Definition symbols.h:125
bool hasNext()
Definition symbols.h:89
QByteArray lexem() const
Definition symbols.h:104
Token next()
Definition symbols.h:94
const SafeSymbols & constTop() const
Definition symbols.h:88
bool test(Token)
Definition symbols.h:111
QHash< MacroName, Macro > Macros
SubArray MacroName
#define qPrintable(string)
Definition qstring.h:1685
#define QStringLiteral(str)
Definition qstring.h:1826
static QByteArray combinePath(const QString &infile, const QString &outfile)
Definition main.cpp:40
static bool hasNext(const Symbols &symbols, int i)
Definition main.cpp:78
static const Symbol & next(const Symbols &symbols, int &i)
Definition main.cpp:81
int runMoc(int argc, char **argv)
Definition main.cpp:170
static auto openFileForWriting(const QString &name)
Definition main.cpp:62
void error(const char *msg="Invalid argument")
Definition main.cpp:56
static QStringList argumentsFromCommandLineAndFile(const QStringList &arguments, bool &hasOptionFiles)
Definition main.cpp:138
QByteArray composePreprocessorOutput(const Symbols &symbols)
Definition main.cpp:85
int main(int argc, char *argv[])
[ctor_close]
Simple structure used by the Doc and DocParser classes.
Symbols symbols
bool isVariadic
bool isFunction
Symbols arguments
qsizetype index
Definition symbols.h:81
Symbols symbols
Definition symbols.h:78
QByteArray expandedMacro
Definition symbols.h:79
QByteArray array
Definition symbols.h:26
SubArray(const char *s)
Definition symbols.h:21
qsizetype len
Definition symbols.h:28
bool operator==(const SubArray &other) const
Definition symbols.h:29
qsizetype from
Definition symbols.h:27
SubArray()=default
SubArray(const QByteArray &a)
Definition symbols.h:20
SubArray(const QByteArray &a, qsizetype from, qsizetype len)
Definition symbols.h:22
qsizetype from
Definition symbols.h:70
QByteArrayView unquotedLexemView() const
Definition symbols.h:62
Symbol(int lineNum, Token token)
Definition symbols.h:48
operator SubArray() const
Definition symbols.h:64
QByteArrayView lexemView() const
Definition symbols.h:61
Token token
Definition symbols.h:58
void mergeStringLiteral(const Symbol &next)
bool operator==(const Symbol &o) const
Definition symbols.h:65
int lineNum
Definition symbols.h:57
Symbol()=default
Symbol(int lineNum, Token token, const QByteArray &lexem, qsizetype from, qsizetype len)
Definition symbols.h:53
QByteArray lexem() const
Definition symbols.h:59
QByteArray lex
Definition symbols.h:69
QByteArray unquotedLexem() const
Definition symbols.h:60
qsizetype len
Definition symbols.h:71
Symbol(int lineNum, Token token, const QByteArray &lexem)
Definition symbols.h:49
size_t qHash(const SubArray &key, size_t seed=0)
Definition symbols.h:39
Q_DECLARE_TYPEINFO(Symbol, Q_RELOCATABLE_TYPE)
Q_DECLARE_TYPEINFO(SafeSymbols, Q_RELOCATABLE_TYPE)
QList< Symbol > Symbols
Definition symbols.h:75