2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2012-09-05 16:29:19 +00:00
|
|
|
|
|
|
|
#ifndef QMAKEPARSER_H
|
|
|
|
#define QMAKEPARSER_H
|
|
|
|
|
|
|
|
#include "qmake_global.h"
|
2016-10-24 17:30:24 +00:00
|
|
|
#include "qmakevfs.h"
|
2012-09-05 16:29:19 +00:00
|
|
|
#include "proitems.h"
|
|
|
|
|
|
|
|
#include <qhash.h>
|
|
|
|
#include <qstack.h>
|
|
|
|
#ifdef PROPARSER_THREAD_SAFE
|
|
|
|
# include <qmutex.h>
|
|
|
|
# include <qwaitcondition.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
class QMAKE_EXPORT QMakeParserHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
CategoryMask = 0xf00,
|
2013-09-02 14:43:49 +00:00
|
|
|
InfoMessage = 0x100,
|
|
|
|
WarningMessage = 0x200,
|
|
|
|
ErrorMessage = 0x300,
|
2012-09-05 16:29:19 +00:00
|
|
|
|
|
|
|
SourceMask = 0xf0,
|
|
|
|
SourceParser = 0,
|
|
|
|
|
|
|
|
CodeMask = 0xf,
|
|
|
|
WarnLanguage = 0,
|
|
|
|
WarnDeprecated,
|
|
|
|
|
|
|
|
ParserWarnLanguage = SourceParser | WarningMessage | WarnLanguage,
|
|
|
|
ParserWarnDeprecated = SourceParser | WarningMessage | WarnDeprecated,
|
|
|
|
|
|
|
|
ParserIoError = ErrorMessage | SourceParser,
|
|
|
|
ParserError
|
|
|
|
};
|
|
|
|
virtual void message(int type, const QString &msg,
|
|
|
|
const QString &fileName = QString(), int lineNo = 0) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProFileCache;
|
2013-05-29 18:18:51 +00:00
|
|
|
class QMakeVfs;
|
2012-09-05 16:29:19 +00:00
|
|
|
|
|
|
|
class QMAKE_EXPORT QMakeParser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Call this from a concurrency-free context
|
|
|
|
static void initialize();
|
|
|
|
|
2013-07-24 17:28:33 +00:00
|
|
|
enum ParseFlag {
|
|
|
|
ParseDefault = 0,
|
2013-07-24 17:45:58 +00:00
|
|
|
ParseUseCache = 1,
|
2016-10-24 17:30:24 +00:00
|
|
|
ParseReportMissing = 4,
|
|
|
|
#ifdef PROEVALUATOR_DUAL_VFS
|
|
|
|
ParseCumulative = 8
|
|
|
|
#else
|
|
|
|
ParseCumulative = 0
|
|
|
|
#endif
|
2013-07-24 17:28:33 +00:00
|
|
|
};
|
|
|
|
Q_DECLARE_FLAGS(ParseFlags, ParseFlag)
|
|
|
|
|
2013-05-29 18:18:51 +00:00
|
|
|
QMakeParser(ProFileCache *cache, QMakeVfs *vfs, QMakeParserHandler *handler);
|
2012-09-05 16:29:19 +00:00
|
|
|
|
|
|
|
enum SubGrammar { FullGrammar, TestGrammar, ValueGrammar };
|
|
|
|
// fileName is expected to be absolute and cleanPath()ed.
|
2013-07-24 17:28:33 +00:00
|
|
|
ProFile *parsedProFile(const QString &fileName, ParseFlags flags = ParseDefault);
|
2020-05-30 21:29:21 +00:00
|
|
|
ProFile *parsedProBlock(QStringView contents, int id, const QString &name, int line = 0,
|
2012-09-05 16:29:19 +00:00
|
|
|
SubGrammar grammar = FullGrammar);
|
|
|
|
|
2016-10-24 17:30:24 +00:00
|
|
|
void discardFileFromCache(int id);
|
2012-10-25 08:55:00 +00:00
|
|
|
|
2015-02-25 11:10:34 +00:00
|
|
|
#ifdef PROPARSER_DEBUG
|
|
|
|
static QString formatProBlock(const QString &block);
|
|
|
|
#endif
|
|
|
|
|
2012-09-05 16:29:19 +00:00
|
|
|
private:
|
2012-09-11 17:30:29 +00:00
|
|
|
enum ScopeNesting {
|
|
|
|
NestNone = 0,
|
|
|
|
NestLoop = 1,
|
|
|
|
NestFunction = 2
|
|
|
|
};
|
|
|
|
|
2012-09-05 16:29:19 +00:00
|
|
|
struct BlockScope {
|
2018-08-02 22:22:24 +00:00
|
|
|
BlockScope() : start(nullptr), braceLevel(0), special(false), inBranch(false), nest(NestNone) {}
|
2012-09-05 16:29:19 +00:00
|
|
|
ushort *start; // Where this block started; store length here
|
|
|
|
int braceLevel; // Nesting of braces in scope
|
|
|
|
bool special; // Single-line conditionals inside loops, etc. cannot have else branches
|
|
|
|
bool inBranch; // The 'else' branch of the previous TokBranch is still open
|
2012-09-11 17:30:29 +00:00
|
|
|
uchar nest; // Into what control structures we are nested
|
2012-09-05 16:29:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ScopeState {
|
|
|
|
StNew, // Fresh scope
|
|
|
|
StCtrl, // Control statement (for or else) met on current line
|
|
|
|
StCond // Conditionals met on current line
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Context { CtxTest, CtxValue, CtxPureValue, CtxArgs };
|
|
|
|
struct ParseCtx {
|
|
|
|
int parens; // Nesting of non-functional parentheses
|
|
|
|
int argc; // Number of arguments in current function call
|
|
|
|
int wordCount; // Number of words in current expression
|
|
|
|
Context context;
|
|
|
|
ushort quote; // Enclosing quote type
|
|
|
|
ushort terminator; // '}' if replace function call is braced, ':' if test function
|
|
|
|
};
|
|
|
|
|
2016-10-24 17:30:24 +00:00
|
|
|
bool readFile(int id, QMakeParser::ParseFlags flags, QString *contents);
|
2020-05-30 21:29:21 +00:00
|
|
|
void read(ProFile *pro, QStringView content, int line, SubGrammar grammar);
|
2012-09-05 16:29:19 +00:00
|
|
|
|
|
|
|
ALWAYS_INLINE void putTok(ushort *&tokPtr, ushort tok);
|
|
|
|
ALWAYS_INLINE void putBlockLen(ushort *&tokPtr, uint len);
|
|
|
|
ALWAYS_INLINE void putBlock(ushort *&tokPtr, const ushort *buf, uint len);
|
|
|
|
void putHashStr(ushort *&pTokPtr, const ushort *buf, uint len);
|
|
|
|
void finalizeHashStr(ushort *buf, uint len);
|
|
|
|
void putLineMarker(ushort *&tokPtr);
|
|
|
|
ALWAYS_INLINE bool resolveVariable(ushort *xprPtr, int tlen, int needSep, ushort **ptr,
|
|
|
|
ushort **buf, QString *xprBuff,
|
|
|
|
ushort **tokPtr, QString *tokBuff,
|
2020-05-30 21:29:21 +00:00
|
|
|
const ushort *cur, QStringView in);
|
2012-09-05 16:29:19 +00:00
|
|
|
void finalizeCond(ushort *&tokPtr, ushort *uc, ushort *ptr, int wordCount);
|
|
|
|
void finalizeCall(ushort *&tokPtr, ushort *uc, ushort *ptr, int argc);
|
2015-02-18 10:20:01 +00:00
|
|
|
void warnOperator(const char *msg);
|
|
|
|
bool failOperator(const char *msg);
|
|
|
|
bool acceptColon(const char *msg);
|
2015-02-17 19:45:08 +00:00
|
|
|
void putOperator(ushort *&tokPtr);
|
2012-09-05 16:29:19 +00:00
|
|
|
void finalizeTest(ushort *&tokPtr);
|
2015-02-19 19:53:53 +00:00
|
|
|
void bogusTest(ushort *&tokPtr, const QString &msg);
|
2012-09-05 16:29:19 +00:00
|
|
|
void enterScope(ushort *&tokPtr, bool special, ScopeState state);
|
|
|
|
void leaveScope(ushort *&tokPtr);
|
|
|
|
void flushCond(ushort *&tokPtr);
|
|
|
|
void flushScopes(ushort *&tokPtr);
|
|
|
|
|
|
|
|
void message(int type, const QString &msg) const;
|
|
|
|
void parseError(const QString &msg) const
|
2015-02-18 16:32:34 +00:00
|
|
|
{
|
|
|
|
message(QMakeParserHandler::ParserError, msg);
|
|
|
|
m_proFile->setOk(false);
|
|
|
|
}
|
2012-09-05 16:29:19 +00:00
|
|
|
void languageWarning(const QString &msg) const
|
|
|
|
{ message(QMakeParserHandler::ParserWarnLanguage, msg); }
|
|
|
|
void deprecationWarning(const QString &msg) const
|
|
|
|
{ message(QMakeParserHandler::ParserWarnDeprecated, msg); }
|
|
|
|
|
|
|
|
// Current location
|
|
|
|
ProFile *m_proFile;
|
|
|
|
int m_lineNo;
|
|
|
|
|
|
|
|
QStack<BlockScope> m_blockstack;
|
|
|
|
ScopeState m_state;
|
|
|
|
int m_markLine; // Put marker for this line
|
|
|
|
bool m_inError; // Current line had a parsing error; suppress followup error messages
|
|
|
|
bool m_canElse; // Conditionals met on previous line, but no scope was opened
|
2015-02-19 12:25:10 +00:00
|
|
|
int m_invert; // Pending conditional is negated
|
2012-09-05 16:29:19 +00:00
|
|
|
enum { NoOperator, AndOperator, OrOperator } m_operator; // Pending conditional is ORed/ANDed
|
|
|
|
|
|
|
|
QString m_tmp; // Temporary for efficient toQString
|
|
|
|
|
|
|
|
ProFileCache *m_cache;
|
|
|
|
QMakeParserHandler *m_handler;
|
2013-05-29 18:18:51 +00:00
|
|
|
QMakeVfs *m_vfs;
|
2012-09-05 16:29:19 +00:00
|
|
|
|
|
|
|
// This doesn't help gcc 3.3 ...
|
|
|
|
template<typename T> friend class QTypeInfo;
|
|
|
|
|
|
|
|
friend class ProFileCache;
|
|
|
|
};
|
|
|
|
|
2013-07-24 17:28:33 +00:00
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(QMakeParser::ParseFlags)
|
|
|
|
|
2012-09-05 16:29:19 +00:00
|
|
|
class QMAKE_EXPORT ProFileCache
|
|
|
|
{
|
|
|
|
public:
|
2018-05-24 18:50:32 +00:00
|
|
|
ProFileCache();
|
2012-09-05 16:29:19 +00:00
|
|
|
~ProFileCache();
|
|
|
|
|
2016-10-24 17:30:24 +00:00
|
|
|
void discardFile(int id);
|
|
|
|
void discardFile(const QString &fileName, QMakeVfs *vfs);
|
|
|
|
void discardFiles(const QString &prefix, QMakeVfs *vfs);
|
2012-09-05 16:29:19 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
ProFile *pro;
|
|
|
|
#ifdef PROPARSER_THREAD_SAFE
|
|
|
|
struct Locker {
|
|
|
|
Locker() : waiters(0), done(false) {}
|
|
|
|
QWaitCondition cond;
|
|
|
|
int waiters;
|
|
|
|
bool done;
|
|
|
|
};
|
|
|
|
Locker *locker;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2016-10-24 17:30:24 +00:00
|
|
|
QHash<int, Entry> parsed_files;
|
2012-09-05 16:29:19 +00:00
|
|
|
#ifdef PROPARSER_THREAD_SAFE
|
|
|
|
QMutex mutex;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
friend class QMakeParser;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if !defined(__GNUC__) || __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3)
|
2020-11-27 10:18:00 +00:00
|
|
|
Q_DECLARE_TYPEINFO(QMakeParser::BlockScope, Q_RELOCATABLE_TYPE);
|
2012-09-05 16:29:19 +00:00
|
|
|
Q_DECLARE_TYPEINFO(QMakeParser::Context, Q_PRIMITIVE_TYPE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
#endif // PROFILEPARSER_H
|