2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-08-14 22:52:37 +08:00
|
|
|
#include "cppparser.h"
|
2021-08-15 16:49:37 +08:00
|
|
|
#include "parserutils.h"
|
2021-08-19 17:08:01 +08:00
|
|
|
#include "../utils.h"
|
2022-12-10 21:23:49 +08:00
|
|
|
#include "qsynedit/syntaxer/cpp.h"
|
2021-08-15 16:49:37 +08:00
|
|
|
|
2021-08-20 01:06:10 +08:00
|
|
|
#include <QApplication>
|
2021-08-29 17:23:40 +08:00
|
|
|
#include <QDate>
|
2021-08-15 16:49:37 +08:00
|
|
|
#include <QHash>
|
|
|
|
#include <QQueue>
|
2023-07-14 17:44:14 +08:00
|
|
|
#include <QRegularExpression>
|
2021-08-20 01:06:10 +08:00
|
|
|
#include <QThread>
|
2021-08-29 17:23:40 +08:00
|
|
|
#include <QTime>
|
2021-08-14 22:52:37 +08:00
|
|
|
|
2021-08-20 01:06:10 +08:00
|
|
|
static QAtomicInt cppParserCount(0);
|
2023-02-21 22:40:29 +08:00
|
|
|
|
2023-10-10 19:59:39 +08:00
|
|
|
CppParser::CppParser(QObject *parent) : QObject(parent),
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
|
|
|
mMutex()
|
|
|
|
#else
|
|
|
|
mMutex(QMutex::Recursive)
|
|
|
|
#endif
|
2021-08-14 22:52:37 +08:00
|
|
|
{
|
2021-08-20 01:06:10 +08:00
|
|
|
mParserId = cppParserCount.fetchAndAddRelaxed(1);
|
2022-07-28 13:51:38 +08:00
|
|
|
mLanguage = ParserLanguage::CPlusPlus;
|
2021-08-20 01:06:10 +08:00
|
|
|
mSerialCount = 0;
|
|
|
|
updateSerialId();
|
|
|
|
mUniqId = 0;
|
|
|
|
mParsing = false;
|
|
|
|
//mStatementList ; // owns the objects
|
|
|
|
//mFilesToScan;
|
|
|
|
//mIncludePaths;
|
|
|
|
//mProjectIncludePaths;
|
|
|
|
//mProjectFiles;
|
|
|
|
// mCurrentScope;
|
|
|
|
//mCurrentClassScope;
|
|
|
|
//mSkipList;
|
|
|
|
mParseLocalHeaders = true;
|
|
|
|
mParseGlobalHeaders = true;
|
|
|
|
mLockCount = 0;
|
2021-08-27 16:38:55 +08:00
|
|
|
mIsSystemHeader = false;
|
|
|
|
mIsHeader = false;
|
|
|
|
mIsProjectFile = false;
|
|
|
|
|
2021-10-27 16:39:23 +08:00
|
|
|
mCppKeywords = CppKeywords;
|
2021-10-30 21:15:07 +08:00
|
|
|
mCppTypeKeywords = CppTypeKeywords;
|
2022-04-25 10:47:19 +08:00
|
|
|
mEnabled = true;
|
2022-10-18 12:24:59 +08:00
|
|
|
|
2022-10-22 19:33:20 +08:00
|
|
|
internalClear();
|
|
|
|
|
2021-08-20 01:06:10 +08:00
|
|
|
//mNamespaces;
|
|
|
|
//mBlockBeginSkips;
|
|
|
|
//mBlockEndSkips;
|
|
|
|
//mInlineNamespaceEndSkips;
|
|
|
|
}
|
2021-08-14 22:52:37 +08:00
|
|
|
|
2021-08-20 01:06:10 +08:00
|
|
|
CppParser::~CppParser()
|
|
|
|
{
|
2023-08-12 13:51:35 +08:00
|
|
|
//qDebug()<<"delete parser";
|
2021-08-20 01:06:10 +08:00
|
|
|
while (true) {
|
|
|
|
//wait for all methods finishes running
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (!mParsing && (mLockCount == 0)) {
|
|
|
|
mParsing = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-08-12 13:51:35 +08:00
|
|
|
//qDebug()<<"waiting for parse finished";
|
2021-08-20 01:06:10 +08:00
|
|
|
QThread::msleep(50);
|
|
|
|
QCoreApplication* app = QApplication::instance();
|
|
|
|
app->processEvents();
|
|
|
|
}
|
2022-10-10 18:05:18 +08:00
|
|
|
//qDebug()<<"-------- parser deleted ------------";
|
2021-08-14 22:52:37 +08:00
|
|
|
}
|
2021-08-15 16:49:37 +08:00
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
void CppParser::addHardDefineByLine(const QString &line)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (line.startsWith('#')) {
|
2022-10-22 10:44:10 +08:00
|
|
|
mPreprocessor.addHardDefineByLine(line.mid(1).trimmed());
|
2021-08-19 23:49:23 +08:00
|
|
|
} else {
|
2022-10-22 10:44:10 +08:00
|
|
|
mPreprocessor.addHardDefineByLine(line);
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::addIncludePath(const QString &value)
|
|
|
|
{
|
2021-08-20 01:06:10 +08:00
|
|
|
QMutexLocker locker(&mMutex);
|
2022-10-22 10:44:10 +08:00
|
|
|
mPreprocessor.addIncludePath(includeTrailingPathDelimiter(value));
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 22:51:14 +08:00
|
|
|
void CppParser::removeProjectFile(const QString &value)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
|
|
|
|
mProjectFiles.remove(value);
|
|
|
|
mFilesToScan.remove(value);
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
void CppParser::addProjectIncludePath(const QString &value)
|
|
|
|
{
|
2021-08-20 01:06:10 +08:00
|
|
|
QMutexLocker locker(&mMutex);
|
2022-10-22 10:44:10 +08:00
|
|
|
mPreprocessor.addProjectIncludePath(includeTrailingPathDelimiter(value));
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 01:06:10 +08:00
|
|
|
void CppParser::clearIncludePaths()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2022-10-22 10:44:10 +08:00
|
|
|
mPreprocessor.clearIncludePaths();
|
2021-08-20 01:06:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::clearProjectIncludePaths()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2022-10-22 10:44:10 +08:00
|
|
|
mPreprocessor.clearProjectIncludePaths();
|
2021-08-20 01:06:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::clearProjectFiles()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
mProjectFiles.clear();
|
|
|
|
}
|
|
|
|
|
2021-09-24 18:02:42 +08:00
|
|
|
QList<PStatement> CppParser::getListOfFunctions(const QString &fileName, const QString &phrase, int line)
|
2021-08-21 22:15:44 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2021-09-24 18:02:42 +08:00
|
|
|
QList<PStatement> result;
|
|
|
|
if (mParsing)
|
|
|
|
return result;
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement statement = doFindStatementOf(fileName,phrase, line);
|
2021-08-21 22:15:44 +08:00
|
|
|
if (!statement)
|
2021-09-24 18:02:42 +08:00
|
|
|
return result;
|
2022-01-26 12:17:15 +08:00
|
|
|
PStatement parentScope;
|
|
|
|
if (statement->kind == StatementKind::skClass) {
|
|
|
|
parentScope = statement;
|
|
|
|
} else
|
|
|
|
parentScope = statement->parentScope.lock();
|
2021-08-21 22:15:44 +08:00
|
|
|
if (parentScope && parentScope->kind == StatementKind::skNamespace) {
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatementList namespaceStatementsList = doFindNamespace(parentScope->command);
|
2021-08-21 22:15:44 +08:00
|
|
|
if (namespaceStatementsList) {
|
2021-08-29 00:48:23 +08:00
|
|
|
for (PStatement& namespaceStatement : *namespaceStatementsList) {
|
2021-09-24 18:02:42 +08:00
|
|
|
result.append(
|
|
|
|
getListOfFunctions(fileName,line,statement,namespaceStatement));
|
2021-08-21 22:15:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
2021-09-24 18:02:42 +08:00
|
|
|
result.append(
|
|
|
|
getListOfFunctions(fileName,line,statement,parentScope)
|
|
|
|
);
|
|
|
|
return result;
|
2021-08-21 22:15:44 +08:00
|
|
|
}
|
|
|
|
|
2022-11-10 08:05:04 +08:00
|
|
|
PStatement CppParser::findScopeStatement(const QString &filename, int line)
|
2022-03-23 14:13:10 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing) {
|
|
|
|
return PStatement();
|
|
|
|
}
|
2023-03-11 17:32:57 +08:00
|
|
|
return doFindScopeStatement(filename,line);
|
|
|
|
}
|
|
|
|
|
|
|
|
PStatement CppParser::doFindScopeStatement(const QString &filename, int line) const
|
|
|
|
{
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(filename);
|
2022-03-23 14:13:10 +08:00
|
|
|
if (!fileIncludes)
|
|
|
|
return PStatement();
|
|
|
|
|
2022-11-28 11:28:02 +08:00
|
|
|
return fileIncludes->scopes.findScopeAtLine(line);
|
2022-03-23 14:13:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PFileIncludes CppParser::findFileIncludes(const QString &filename, bool deleteIt)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(filename,PFileIncludes());
|
2022-03-23 14:13:10 +08:00
|
|
|
if (deleteIt && fileIncludes)
|
2022-10-22 10:44:10 +08:00
|
|
|
mPreprocessor.includesList().remove(filename);
|
2022-03-23 14:13:10 +08:00
|
|
|
return fileIncludes;
|
|
|
|
}
|
2021-08-29 00:48:23 +08:00
|
|
|
QString CppParser::findFirstTemplateParamOf(const QString &fileName, const QString &phrase, const PStatement& currentScope)
|
2021-08-22 05:50:26 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return "";
|
2022-11-27 13:32:14 +08:00
|
|
|
return doFindFirstTemplateParamOf(fileName,phrase,currentScope);
|
2021-08-22 05:50:26 +08:00
|
|
|
}
|
|
|
|
|
2023-06-08 17:15:55 +08:00
|
|
|
QString CppParser::findTemplateParamOf(const QString &fileName, const QString &phrase, int index, const PStatement ¤tScope)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return "";
|
|
|
|
return doFindTemplateParamOf(fileName,phrase,index,currentScope);
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:08:46 +08:00
|
|
|
PStatement CppParser::findFunctionAt(const QString &fileName, int line)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(fileName);
|
2021-08-22 16:08:46 +08:00
|
|
|
if (!fileIncludes)
|
|
|
|
return PStatement();
|
2021-08-29 00:48:23 +08:00
|
|
|
for (PStatement& statement : fileIncludes->statements) {
|
2021-08-22 16:08:46 +08:00
|
|
|
if (statement->kind != StatementKind::skFunction
|
|
|
|
&& statement->kind != StatementKind::skConstructor
|
|
|
|
&& statement->kind != StatementKind::skDestructor)
|
|
|
|
continue;
|
|
|
|
if (statement->line == line || statement->definitionLine == line)
|
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::findLastOperator(const QString &phrase) const
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int phraseLength = phrase.length();
|
|
|
|
int i = phraseLength-1;
|
2021-08-22 16:08:46 +08:00
|
|
|
|
|
|
|
// Obtain stuff after first operator
|
|
|
|
while (i>=0) {
|
2023-03-12 23:45:03 +08:00
|
|
|
if ((i+1<phraseLength) &&
|
2021-08-22 16:08:46 +08:00
|
|
|
(phrase[i + 1] == '>') && (phrase[i] == '-'))
|
|
|
|
return i;
|
2023-03-12 23:45:03 +08:00
|
|
|
else if ((i+1<phraseLength) &&
|
2021-08-22 16:08:46 +08:00
|
|
|
(phrase[i + 1] == ':') && (phrase[i] == ':'))
|
|
|
|
return i;
|
|
|
|
else if (phrase[i] == '.')
|
|
|
|
return i;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PStatementList CppParser::findNamespace(const QString &name)
|
|
|
|
{
|
2021-08-22 21:23:58 +08:00
|
|
|
QMutexLocker locker(&mMutex);
|
2023-03-11 17:32:57 +08:00
|
|
|
return doFindNamespace(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
PStatementList CppParser::doFindNamespace(const QString &name) const
|
|
|
|
{
|
2021-08-22 16:08:46 +08:00
|
|
|
return mNamespaces.value(name,PStatementList());
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
|
2021-11-05 10:44:23 +08:00
|
|
|
PStatement CppParser::findStatement(const QString &fullname)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2023-03-11 17:32:57 +08:00
|
|
|
return doFindStatement(fullname);
|
|
|
|
}
|
|
|
|
|
|
|
|
PStatement CppParser::doFindStatement(const QString &fullname) const
|
|
|
|
{
|
2021-11-05 10:44:23 +08:00
|
|
|
if (fullname.isEmpty())
|
|
|
|
return PStatement();
|
|
|
|
QStringList phrases = fullname.split("::");
|
|
|
|
if (phrases.isEmpty())
|
|
|
|
return PStatement();
|
|
|
|
PStatement parentStatement;
|
|
|
|
PStatement statement;
|
2023-08-07 14:23:57 +08:00
|
|
|
|
|
|
|
for (int i=(phrases[0].isEmpty()?1:0);i<phrases.count();i++) {
|
|
|
|
const QString& phrase=phrases[i];
|
2021-11-05 10:44:23 +08:00
|
|
|
if (parentStatement && parentStatement->kind == StatementKind::skNamespace) {
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatementList lst = doFindNamespace(parentStatement->fullName);
|
2021-11-05 10:44:23 +08:00
|
|
|
foreach (const PStatement& namespaceStatement, *lst) {
|
|
|
|
statement = findMemberOfStatement(phrase,namespaceStatement);
|
|
|
|
if (statement)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
statement = findMemberOfStatement(phrase,parentStatement);
|
|
|
|
}
|
|
|
|
if (!statement)
|
|
|
|
return PStatement();
|
|
|
|
parentStatement = statement;
|
|
|
|
}
|
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:08:46 +08:00
|
|
|
PStatement CppParser::findStatementOf(const QString &fileName, const QString &phrase, int line)
|
|
|
|
{
|
2021-11-05 10:44:23 +08:00
|
|
|
QMutexLocker locker(&mMutex);
|
2021-12-19 10:16:46 +08:00
|
|
|
if (mParsing)
|
|
|
|
return PStatement();
|
2023-03-11 17:32:57 +08:00
|
|
|
return doFindStatementOf(fileName,phrase,line);
|
|
|
|
}
|
|
|
|
PStatement CppParser::doFindStatementOf(const QString &fileName, const QString &phrase, int line) const
|
|
|
|
{
|
|
|
|
return doFindStatementOf(fileName,phrase,doFindScopeStatement(fileName,line));
|
2021-08-22 16:08:46 +08:00
|
|
|
}
|
|
|
|
|
2021-12-05 10:52:17 +08:00
|
|
|
PStatement CppParser::findStatementOf(const QString &fileName,
|
|
|
|
const QString &phrase,
|
|
|
|
const PStatement& currentScope,
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement &parentScopeType)
|
2021-08-22 16:08:46 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2023-03-11 17:32:57 +08:00
|
|
|
if (mParsing)
|
|
|
|
return PStatement();
|
|
|
|
return doFindStatementOf(fileName,phrase,currentScope,parentScopeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
PStatement CppParser::doFindStatementOf(const QString &fileName,
|
|
|
|
const QString &phrase,
|
|
|
|
const PStatement& currentScope,
|
|
|
|
PStatement &parentScopeType) const
|
|
|
|
{
|
2021-08-22 16:08:46 +08:00
|
|
|
PStatement result;
|
2021-08-25 00:20:07 +08:00
|
|
|
parentScopeType = currentScope;
|
2021-08-22 16:08:46 +08:00
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
//find the start scope statement
|
2021-08-22 16:08:46 +08:00
|
|
|
QString namespaceName, remainder;
|
|
|
|
QString nextScopeWord,operatorToken,memberName;
|
|
|
|
PStatement statement;
|
|
|
|
getFullNamespace(phrase, namespaceName, remainder);
|
|
|
|
if (!namespaceName.isEmpty()) { // (namespace )qualified Name
|
|
|
|
PStatementList namespaceList = mNamespaces.value(namespaceName);
|
|
|
|
|
|
|
|
if (!namespaceList || namespaceList->isEmpty())
|
|
|
|
return PStatement();
|
|
|
|
|
|
|
|
if (remainder.isEmpty())
|
|
|
|
return namespaceList->front();
|
|
|
|
|
|
|
|
remainder = splitPhrase(remainder,nextScopeWord,operatorToken,memberName);
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
for (PStatement& currentNamespace: *namespaceList) {
|
2021-08-22 16:08:46 +08:00
|
|
|
statement = findMemberOfStatement(nextScopeWord,currentNamespace);
|
|
|
|
if (statement)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//not found in namespaces;
|
|
|
|
if (!statement)
|
|
|
|
return PStatement();
|
|
|
|
// found in namespace
|
|
|
|
} else if ((phrase.length()>2) &&
|
|
|
|
(phrase[0]==':') && (phrase[1]==':')) {
|
|
|
|
//global
|
|
|
|
remainder= phrase.mid(2);
|
|
|
|
remainder= splitPhrase(remainder,nextScopeWord,operatorToken,memberName);
|
|
|
|
statement= findMemberOfStatement(nextScopeWord,PStatement());
|
|
|
|
if (!statement)
|
|
|
|
return PStatement();
|
|
|
|
} else {
|
|
|
|
//unqualified name
|
2021-08-25 00:20:07 +08:00
|
|
|
parentScopeType = currentScope;
|
2021-08-22 16:08:46 +08:00
|
|
|
remainder = splitPhrase(remainder,nextScopeWord,operatorToken,memberName);
|
2021-12-07 08:23:27 +08:00
|
|
|
statement = findStatementStartingFrom(fileName,nextScopeWord,parentScopeType);
|
2021-08-22 16:08:46 +08:00
|
|
|
if (!statement)
|
|
|
|
return PStatement();
|
|
|
|
}
|
2021-08-25 00:20:07 +08:00
|
|
|
parentScopeType = currentScope;
|
2021-08-22 16:08:46 +08:00
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
if (!memberName.isEmpty() && (statement->kind == StatementKind::skTypedef)) {
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement typeStatement = doFindTypeDefinitionOf(fileName,statement->type, parentScopeType);
|
2021-08-22 21:23:58 +08:00
|
|
|
if (typeStatement)
|
|
|
|
statement = typeStatement;
|
|
|
|
}
|
2021-08-22 16:08:46 +08:00
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
//using alias like 'using std::vector;'
|
2022-03-16 16:36:25 +08:00
|
|
|
if (statement->kind == StatementKind::skAlias) {
|
2023-03-11 17:32:57 +08:00
|
|
|
statement = doFindAliasedStatement(statement);
|
2021-08-22 21:23:58 +08:00
|
|
|
if (!statement)
|
|
|
|
return PStatement();
|
|
|
|
}
|
2021-08-22 16:08:46 +08:00
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
if (statement->kind == StatementKind::skConstructor) {
|
|
|
|
// we need the class, not the construtor
|
|
|
|
statement = statement->parentScope.lock();
|
|
|
|
if (!statement)
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
PStatement lastScopeStatement;
|
|
|
|
QString typeName;
|
|
|
|
PStatement typeStatement;
|
|
|
|
while (!memberName.isEmpty()) {
|
2022-04-17 19:49:04 +08:00
|
|
|
if (statement->kind!=StatementKind::skClass
|
|
|
|
&& operatorToken == "::") {
|
|
|
|
return PStatement();
|
|
|
|
}
|
2021-08-22 21:23:58 +08:00
|
|
|
if (statement->kind == StatementKind::skVariable
|
|
|
|
|| statement->kind == StatementKind::skParameter
|
|
|
|
|| statement->kind == StatementKind::skFunction) {
|
|
|
|
|
|
|
|
bool isSTLContainerFunctions = false;
|
|
|
|
|
|
|
|
if (statement->kind == StatementKind::skFunction){
|
|
|
|
PStatement parentScope = statement->parentScope.lock();
|
|
|
|
if (parentScope
|
|
|
|
&& STLContainers.contains(parentScope->fullName)
|
|
|
|
&& STLElementMethods.contains(statement->command)
|
|
|
|
&& lastScopeStatement) {
|
|
|
|
isSTLContainerFunctions = true;
|
|
|
|
PStatement lastScopeParent = lastScopeStatement->parentScope.lock();
|
2023-03-11 17:32:57 +08:00
|
|
|
typeName=doFindFirstTemplateParamOf(fileName,lastScopeStatement->type,
|
2021-08-22 21:23:58 +08:00
|
|
|
lastScopeParent );
|
2023-03-11 17:32:57 +08:00
|
|
|
typeStatement=doFindTypeDefinitionOf(fileName, typeName,
|
2021-08-22 21:23:58 +08:00
|
|
|
lastScopeParent );
|
2023-03-11 19:56:40 +08:00
|
|
|
} else if (parentScope
|
|
|
|
&& STLMaps.contains(parentScope->fullName)
|
|
|
|
&& STLElementMethods.contains(statement->command)
|
|
|
|
&& lastScopeStatement) {
|
|
|
|
isSTLContainerFunctions = true;
|
|
|
|
PStatement lastScopeParent = lastScopeStatement->parentScope.lock();
|
|
|
|
typeName=doFindTemplateParamOf(fileName,lastScopeStatement->type,1,
|
|
|
|
lastScopeParent );
|
|
|
|
typeStatement=doFindTypeDefinitionOf(fileName, typeName,
|
|
|
|
lastScopeParent );
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isSTLContainerFunctions)
|
2023-03-11 17:32:57 +08:00
|
|
|
typeStatement = doFindTypeDefinitionOf(fileName,statement->type, parentScopeType);
|
2021-08-22 21:23:58 +08:00
|
|
|
|
|
|
|
//it's stl smart pointer
|
|
|
|
if ((typeStatement)
|
|
|
|
&& STLPointers.contains(typeStatement->fullName)
|
|
|
|
&& (operatorToken == "->")) {
|
|
|
|
PStatement parentScope = statement->parentScope.lock();
|
2023-03-11 17:32:57 +08:00
|
|
|
typeName=doFindFirstTemplateParamOf(fileName,statement->type, parentScope);
|
|
|
|
typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
2021-08-22 21:23:58 +08:00
|
|
|
} else if ((typeStatement)
|
|
|
|
&& STLContainers.contains(typeStatement->fullName)
|
|
|
|
&& nextScopeWord.endsWith(']')) {
|
|
|
|
//it's a std container
|
|
|
|
PStatement parentScope = statement->parentScope.lock();
|
2023-03-11 17:32:57 +08:00
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,statement->type,
|
2021-08-22 21:23:58 +08:00
|
|
|
parentScope);
|
2023-03-11 17:32:57 +08:00
|
|
|
typeStatement = doFindTypeDefinitionOf(fileName, typeName,
|
2021-08-22 21:23:58 +08:00
|
|
|
parentScope);
|
2023-03-11 19:56:40 +08:00
|
|
|
} else if ((typeStatement)
|
|
|
|
&& STLMaps.contains(typeStatement->fullName)
|
|
|
|
&& nextScopeWord.endsWith(']')) {
|
|
|
|
//it's a std container
|
|
|
|
PStatement parentScope = statement->parentScope.lock();
|
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,statement->type,
|
|
|
|
parentScope);
|
|
|
|
typeStatement = doFindTypeDefinitionOf(fileName, typeName,
|
|
|
|
parentScope);
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
|
|
|
lastScopeStatement = statement;
|
|
|
|
if (typeStatement)
|
|
|
|
statement = typeStatement;
|
|
|
|
} else
|
|
|
|
lastScopeStatement = statement;
|
|
|
|
remainder = splitPhrase(remainder,nextScopeWord,operatorToken,memberName);
|
|
|
|
PStatement memberStatement = findMemberOfStatement(nextScopeWord,statement);
|
|
|
|
if (!memberStatement)
|
|
|
|
return PStatement();
|
2021-08-22 16:08:46 +08:00
|
|
|
|
2021-08-25 00:20:07 +08:00
|
|
|
parentScopeType=statement;
|
2021-08-22 21:23:58 +08:00
|
|
|
statement = memberStatement;
|
|
|
|
if (!memberName.isEmpty() && (statement->kind == StatementKind::skTypedef)) {
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement typeStatement = doFindTypeDefinitionOf(fileName,statement->type, parentScopeType);
|
2021-08-22 21:23:58 +08:00
|
|
|
if (typeStatement)
|
|
|
|
statement = typeStatement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return statement;
|
2021-08-22 16:08:46 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement CppParser::evalExpression(
|
2021-12-04 18:38:54 +08:00
|
|
|
const QString &fileName,
|
2022-12-11 19:47:43 +08:00
|
|
|
QStringList &phraseExpression,
|
2021-12-04 18:38:54 +08:00
|
|
|
const PStatement ¤tScope)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
2021-12-06 11:37:37 +08:00
|
|
|
return PEvalStatement();
|
2021-12-08 21:44:40 +08:00
|
|
|
// qDebug()<<phraseExpression;
|
2021-12-04 18:38:54 +08:00
|
|
|
int pos = 0;
|
2021-12-06 09:02:39 +08:00
|
|
|
return doEvalExpression(fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
currentScope,
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement(),
|
2021-12-06 09:02:39 +08:00
|
|
|
true);
|
2021-12-04 18:38:54 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement CppParser::doFindStatementOf(const QString &fileName, const QString &phrase, const PStatement& currentClass) const
|
2021-08-22 16:08:46 +08:00
|
|
|
{
|
|
|
|
PStatement statementParentType;
|
2023-03-11 17:32:57 +08:00
|
|
|
return doFindStatementOf(fileName,phrase,currentClass,statementParentType);
|
2021-08-22 16:08:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
|
2021-12-18 23:36:58 +08:00
|
|
|
PStatement CppParser::findStatementOf(const QString &fileName, const QStringList &expression, const PStatement ¤tScope)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return PStatement();
|
2023-03-11 17:32:57 +08:00
|
|
|
return doFindStatementOf(fileName,expression,currentScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
PStatement CppParser::doFindStatementOf(const QString &fileName, const QStringList &expression, const PStatement ¤tScope) const
|
|
|
|
{
|
2021-12-18 23:36:58 +08:00
|
|
|
QString memberOperator;
|
|
|
|
QStringList memberExpression;
|
|
|
|
QStringList ownerExpression = getOwnerExpressionAndMember(expression,memberOperator,memberExpression);
|
|
|
|
if (memberExpression.isEmpty()) {
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
QString phrase = memberExpression[0];
|
|
|
|
if (memberOperator.isEmpty()) {
|
|
|
|
return findStatementStartingFrom(fileName,phrase,currentScope);
|
|
|
|
} else if (ownerExpression.isEmpty()) {
|
2023-07-27 15:44:33 +08:00
|
|
|
return findMemberOfStatement(fileName, phrase,PStatement());
|
2021-12-18 23:36:58 +08:00
|
|
|
} else {
|
|
|
|
int pos = 0;
|
|
|
|
PEvalStatement ownerEvalStatement = doEvalExpression(fileName,
|
|
|
|
ownerExpression,
|
|
|
|
pos,
|
|
|
|
currentScope,
|
|
|
|
PEvalStatement(),
|
|
|
|
true);
|
|
|
|
if (!ownerEvalStatement) {
|
|
|
|
return PStatement();
|
|
|
|
}
|
2022-06-01 17:02:03 +08:00
|
|
|
if (ownerEvalStatement->effectiveTypeStatement &&
|
|
|
|
ownerEvalStatement->effectiveTypeStatement->kind == StatementKind::skNamespace) {
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatementList lst = doFindNamespace(ownerEvalStatement->effectiveTypeStatement->fullName);
|
2022-06-01 17:02:03 +08:00
|
|
|
foreach (const PStatement& namespaceStatement, *lst) {
|
|
|
|
PStatement statement = findMemberOfStatement(phrase,namespaceStatement);
|
|
|
|
if (statement)
|
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
return PStatement();
|
2023-06-13 17:16:09 +08:00
|
|
|
} else if (ownerEvalStatement->typeStatement
|
|
|
|
&& STLIterators.contains(ownerEvalStatement->typeStatement->command)
|
|
|
|
&& memberOperator=="->"
|
|
|
|
) {
|
|
|
|
PStatement parentScope = ownerEvalStatement->typeStatement->parentScope.lock();
|
|
|
|
if (STLContainers.contains(parentScope->fullName)) {
|
|
|
|
QString typeName=doFindFirstTemplateParamOf(fileName,ownerEvalStatement->templateParams, parentScope);
|
|
|
|
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
|
|
|
return findMemberOfStatement(phrase, typeStatement);
|
|
|
|
} else {
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
} else if (STLMaps.contains(parentScope->fullName)) {
|
|
|
|
QString typeName=doFindTemplateParamOf(fileName,ownerEvalStatement->templateParams,1,parentScope);
|
|
|
|
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
|
|
|
|
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
|
|
|
return findMemberOfStatement(phrase, typeStatement);
|
|
|
|
} else {
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
}
|
2022-06-01 17:02:03 +08:00
|
|
|
}
|
2021-12-18 23:36:58 +08:00
|
|
|
return findMemberOfStatement(phrase, ownerEvalStatement->effectiveTypeStatement);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-19 10:16:46 +08:00
|
|
|
PStatement CppParser::findStatementOf(const QString &fileName, const QStringList &expression, int line)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return PStatement();
|
2023-03-11 17:32:57 +08:00
|
|
|
return doFindStatementOf(fileName,expression,line);
|
|
|
|
}
|
|
|
|
|
|
|
|
PStatement CppParser::doFindStatementOf(const QString &fileName, const QStringList &expression, int line) const
|
|
|
|
{
|
|
|
|
PStatement statement = doFindStatementOf(fileName,expression,doFindScopeStatement(fileName,line));
|
2023-02-05 21:55:23 +08:00
|
|
|
if (statement && statement->line != line
|
|
|
|
&& statement->definitionLine != line) {
|
|
|
|
PStatement parentStatement = statement->parentScope.lock();
|
|
|
|
if (parentStatement &&
|
|
|
|
(parentStatement->line == line && parentStatement->fileName == fileName)) {
|
|
|
|
return parentStatement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return statement;
|
2021-12-19 10:16:46 +08:00
|
|
|
}
|
|
|
|
|
2022-03-15 21:33:27 +08:00
|
|
|
PStatement CppParser::findAliasedStatement(const PStatement &statement)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return PStatement();
|
2023-03-11 17:32:57 +08:00
|
|
|
return doFindAliasedStatement(statement);
|
|
|
|
}
|
2023-07-03 09:36:27 +08:00
|
|
|
|
|
|
|
QList<PStatement> CppParser::listTypeStatements(const QString &fileName, int line)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return QList<PStatement>();
|
|
|
|
return doListTypeStatements(fileName,line);
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement CppParser::doFindAliasedStatement(const PStatement &statement) const
|
|
|
|
{
|
2022-03-15 21:33:27 +08:00
|
|
|
if (!statement)
|
|
|
|
return PStatement();
|
2022-03-16 16:24:39 +08:00
|
|
|
QString alias = statement->type;
|
|
|
|
int pos = statement->type.lastIndexOf("::");
|
|
|
|
if (pos<0)
|
|
|
|
return PStatement();
|
|
|
|
QString nsName=statement->type.mid(0,pos);
|
|
|
|
QString name = statement->type.mid(pos+2);
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatementList namespaceStatements = doFindNamespace(nsName);
|
2022-03-16 16:24:39 +08:00
|
|
|
if (!namespaceStatements)
|
|
|
|
return PStatement();
|
|
|
|
foreach (const PStatement& namespaceStatement, *namespaceStatements) {
|
|
|
|
QList<PStatement> resultList = findMembersOfStatement(name,namespaceStatement);
|
|
|
|
foreach(const PStatement& resultStatement,resultList) {
|
|
|
|
if (resultStatement->kind != StatementKind::skAlias)
|
|
|
|
return resultStatement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PStatement();
|
2022-03-15 21:33:27 +08:00
|
|
|
}
|
|
|
|
|
2023-07-03 09:36:27 +08:00
|
|
|
QList<PStatement> CppParser::doListTypeStatements(const QString &fileName, int line) const
|
|
|
|
{
|
|
|
|
QList<PStatement> result;
|
|
|
|
QSet<QString> usedNamespaces;
|
|
|
|
PStatement scopeStatement = doFindScopeStatement(fileName,line);
|
|
|
|
while (true) {
|
|
|
|
const StatementMap& statementMap = mStatementList.childrenStatements(scopeStatement);
|
|
|
|
foreach (const PStatement statement, statementMap.values()) {
|
|
|
|
if (isTypeStatement(statement->kind))
|
|
|
|
result.append(statement);
|
|
|
|
}
|
|
|
|
if (!scopeStatement)
|
|
|
|
break;
|
|
|
|
usedNamespaces = usedNamespaces.unite(scopeStatement->usingList);
|
|
|
|
scopeStatement=scopeStatement->parentScope.lock();
|
|
|
|
}
|
|
|
|
usedNamespaces = usedNamespaces.unite(internalGetFileUsings(fileName));
|
|
|
|
foreach(const QString& ns, usedNamespaces) {
|
|
|
|
PStatementList namespaceStatementsList=doFindNamespace(ns);
|
|
|
|
foreach (const PStatement& namespaceStatement,*namespaceStatementsList) {
|
|
|
|
const StatementMap& statementMap = mStatementList.childrenStatements(namespaceStatement);
|
|
|
|
foreach (const PStatement statement, statementMap.values()) {
|
|
|
|
if (isTypeStatement(statement->kind))
|
|
|
|
result.append(statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement CppParser::findStatementStartingFrom(const QString &fileName, const QString &phrase, const PStatement& startScope) const
|
2021-08-22 21:23:58 +08:00
|
|
|
{
|
|
|
|
PStatement scopeStatement = startScope;
|
|
|
|
|
|
|
|
// repeat until reach global
|
|
|
|
PStatement result;
|
|
|
|
while (scopeStatement) {
|
|
|
|
//search members of current scope
|
|
|
|
result = findStatementInScope(phrase, scopeStatement);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
// not found
|
|
|
|
// search members of all usings (in current scope )
|
2021-08-29 10:14:07 +08:00
|
|
|
foreach (const QString& namespaceName, scopeStatement->usingList) {
|
2021-08-22 21:23:58 +08:00
|
|
|
result = findStatementInNamespace(phrase,namespaceName);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
scopeStatement = scopeStatement->parentScope.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search all global members
|
2023-07-27 15:44:33 +08:00
|
|
|
result = findMemberOfStatement(fileName, phrase,PStatement());
|
2021-08-22 21:23:58 +08:00
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
//Find in all global usings
|
2023-03-11 17:32:57 +08:00
|
|
|
const QSet<QString>& fileUsings = internalGetFileUsings(fileName);
|
2021-08-22 21:23:58 +08:00
|
|
|
// add members of all fusings
|
2021-08-29 00:48:23 +08:00
|
|
|
for (const QString& namespaceName:fileUsings) {
|
2021-08-22 21:23:58 +08:00
|
|
|
result = findStatementInNamespace(phrase,namespaceName);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::findTypeDefinitionOf(const QString &fileName, const QString &aType, const PStatement& currentClass)
|
2021-08-22 21:23:58 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
|
|
|
|
if (mParsing)
|
|
|
|
return PStatement();
|
2022-04-17 19:49:04 +08:00
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
return doFindTypeDefinitionOf(fileName,aType,currentClass);
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:17:15 +08:00
|
|
|
PStatement CppParser::findTypeDef(const PStatement &statement, const QString &fileName)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
|
|
|
|
if (mParsing)
|
|
|
|
return PStatement();
|
|
|
|
return getTypeDef(statement, fileName, "");
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
bool CppParser::freeze()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return false;
|
|
|
|
mLockCount++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::freeze(const QString &serialId)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return false;
|
|
|
|
if (mSerialId!=serialId)
|
|
|
|
return false;
|
|
|
|
mLockCount++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList CppParser::getClassesList()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
|
|
|
|
QStringList list;
|
2021-08-29 00:48:23 +08:00
|
|
|
return list;
|
2021-08-22 21:23:58 +08:00
|
|
|
// fills List with a list of all the known classes
|
|
|
|
QQueue<PStatement> queue;
|
|
|
|
queue.enqueue(PStatement());
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
PStatement statement = queue.dequeue();
|
|
|
|
StatementMap statementMap = mStatementList.childrenStatements(statement);
|
2021-08-29 00:48:23 +08:00
|
|
|
for (PStatement& child:statementMap) {
|
2021-08-22 21:23:58 +08:00
|
|
|
if (child->kind == StatementKind::skClass)
|
|
|
|
list.append(child->command);
|
|
|
|
if (!child->children.isEmpty())
|
|
|
|
queue.enqueue(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2022-06-04 14:44:50 +08:00
|
|
|
QStringList CppParser::getFileDirectIncludes(const QString &filename)
|
2021-08-22 21:23:58 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
2022-06-04 14:44:50 +08:00
|
|
|
return QStringList();
|
2021-08-22 21:23:58 +08:00
|
|
|
if (filename.isEmpty())
|
2022-06-04 14:44:50 +08:00
|
|
|
return QStringList();
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(filename,PFileIncludes());
|
2021-08-22 21:23:58 +08:00
|
|
|
|
|
|
|
if (fileIncludes) {
|
2022-06-04 14:44:50 +08:00
|
|
|
return fileIncludes->directIncludes;
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
2022-06-04 14:44:50 +08:00
|
|
|
return QStringList();
|
2021-08-22 21:23:58 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet<QString> CppParser::getFileIncludes(const QString &filename)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
QSet<QString> list;
|
|
|
|
if (mParsing)
|
|
|
|
return list;
|
|
|
|
if (filename.isEmpty())
|
|
|
|
return list;
|
|
|
|
list.insert(filename);
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(filename,PFileIncludes());
|
2021-08-22 21:23:58 +08:00
|
|
|
|
|
|
|
if (fileIncludes) {
|
2021-08-29 10:14:07 +08:00
|
|
|
foreach (const QString& file, fileIncludes->includeFiles.keys()) {
|
2021-08-22 21:23:58 +08:00
|
|
|
list.insert(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet<QString> CppParser::getFileUsings(const QString &filename)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2023-03-11 17:32:57 +08:00
|
|
|
return internalGetFileUsings(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet<QString> CppParser::internalGetFileUsings(const QString &filename) const
|
|
|
|
{
|
2021-09-04 00:13:42 +08:00
|
|
|
QSet<QString> result;
|
2021-08-22 21:23:58 +08:00
|
|
|
if (filename.isEmpty())
|
2021-09-04 00:13:42 +08:00
|
|
|
return result;
|
2023-06-20 21:07:26 +08:00
|
|
|
// if (mParsing)
|
|
|
|
// return result;
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes= mPreprocessor.includesList().value(filename,PFileIncludes());
|
2021-08-22 21:23:58 +08:00
|
|
|
if (fileIncludes) {
|
2021-09-04 00:13:42 +08:00
|
|
|
foreach (const QString& usingName, fileIncludes->usings) {
|
|
|
|
result.insert(usingName);
|
|
|
|
}
|
|
|
|
foreach (const QString& subFile,fileIncludes->includeFiles.keys()){
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes subIncludes = mPreprocessor.includesList().value(subFile,PFileIncludes());
|
2021-09-04 00:13:42 +08:00
|
|
|
if (subIncludes) {
|
|
|
|
foreach (const QString& usingName, subIncludes->usings) {
|
|
|
|
result.insert(usingName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
2021-09-04 00:13:42 +08:00
|
|
|
return result;
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:24:59 +08:00
|
|
|
QString CppParser::getHeaderFileName(const QString &relativeTo, const QString &headerName, bool fromNext)
|
2021-08-22 21:23:58 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2022-10-18 12:24:59 +08:00
|
|
|
QString currentDir = includeTrailingPathDelimiter(extractFileDir(relativeTo));
|
|
|
|
QStringList includes;
|
|
|
|
QStringList projectIncludes;
|
2022-10-18 23:22:29 +08:00
|
|
|
bool found=false;
|
2022-10-22 10:44:10 +08:00
|
|
|
if (fromNext && mPreprocessor.includePaths().contains(currentDir)) {
|
|
|
|
foreach(const QString& s, mPreprocessor.includePathList()) {
|
2022-10-18 12:24:59 +08:00
|
|
|
if (found) {
|
|
|
|
includes.append(s);
|
|
|
|
continue;
|
|
|
|
} else if (s == currentDir)
|
|
|
|
found = true;
|
|
|
|
}
|
2022-10-22 10:44:10 +08:00
|
|
|
projectIncludes = mPreprocessor.projectIncludePathList();
|
|
|
|
} else if (fromNext && mPreprocessor.projectIncludePaths().contains(currentDir)) {
|
|
|
|
includes = mPreprocessor.includePathList();
|
|
|
|
foreach(const QString& s, mPreprocessor.projectIncludePathList()) {
|
2022-10-18 12:24:59 +08:00
|
|
|
if (found) {
|
|
|
|
includes.append(s);
|
|
|
|
continue;
|
|
|
|
} else if (s == currentDir)
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-22 10:44:10 +08:00
|
|
|
includes = mPreprocessor.includePathList();
|
|
|
|
projectIncludes = mPreprocessor.projectIncludePathList();
|
2022-10-18 12:24:59 +08:00
|
|
|
}
|
|
|
|
return ::getHeaderFilename(relativeTo, headerName, includes,
|
2023-08-17 13:24:08 +08:00
|
|
|
projectIncludes);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::isLineVisible(const QString &fileName, int line)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2023-08-17 19:24:49 +08:00
|
|
|
if (mParsing) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-08-17 13:24:08 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(fileName);
|
|
|
|
if (!fileIncludes)
|
|
|
|
return true;
|
|
|
|
return fileIncludes->isLineVisible(line);
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::invalidateFile(const QString &fileName)
|
|
|
|
{
|
2022-03-22 19:08:26 +08:00
|
|
|
if (!mEnabled)
|
|
|
|
return;
|
2021-08-22 21:23:58 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing || mLockCount>0)
|
|
|
|
return;
|
|
|
|
updateSerialId();
|
|
|
|
mParsing = true;
|
|
|
|
}
|
|
|
|
QSet<QString> files = calculateFilesToBeReparsed(fileName);
|
|
|
|
internalInvalidateFiles(files);
|
|
|
|
mParsing = false;
|
|
|
|
}
|
|
|
|
|
2021-08-22 23:48:00 +08:00
|
|
|
bool CppParser::isIncludeLine(const QString &line)
|
|
|
|
{
|
|
|
|
QString trimmedLine = line.trimmed();
|
|
|
|
if ((trimmedLine.length() > 0)
|
|
|
|
&& trimmedLine.startsWith('#')) { // it's a preprocessor line
|
|
|
|
if (trimmedLine.mid(1).trimmed().startsWith("include"))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-18 12:24:59 +08:00
|
|
|
bool CppParser::isIncludeNextLine(const QString &line)
|
|
|
|
{
|
|
|
|
QString trimmedLine = line.trimmed();
|
|
|
|
if ((trimmedLine.length() > 0)
|
|
|
|
&& trimmedLine.startsWith('#')) { // it's a preprocessor line
|
|
|
|
if (trimmedLine.mid(1).trimmed().startsWith("include_next"))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
bool CppParser::isProjectHeaderFile(const QString &fileName)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2022-10-22 10:44:10 +08:00
|
|
|
return ::isSystemHeaderFile(fileName,mPreprocessor.projectIncludePaths());
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::isSystemHeaderFile(const QString &fileName)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
2022-10-22 10:44:10 +08:00
|
|
|
return ::isSystemHeaderFile(fileName,mPreprocessor.includePaths());
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::parseFile(const QString &fileName, bool inProject, bool onlyIfNotParsed, bool updateView)
|
|
|
|
{
|
|
|
|
if (!mEnabled)
|
|
|
|
return;
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing || mLockCount>0)
|
|
|
|
return;
|
|
|
|
updateSerialId();
|
|
|
|
mParsing = true;
|
|
|
|
if (updateView)
|
|
|
|
emit onBusy();
|
|
|
|
emit onStartParsing();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto action = finally([&,this]{
|
|
|
|
mParsing = false;
|
|
|
|
|
|
|
|
if (updateView)
|
|
|
|
emit onEndParsing(mFilesScannedCount,1);
|
|
|
|
else
|
|
|
|
emit onEndParsing(mFilesScannedCount,0);
|
|
|
|
});
|
|
|
|
QString fName = fileName;
|
2022-10-22 10:44:10 +08:00
|
|
|
if (onlyIfNotParsed && mPreprocessor.scannedFiles().contains(fName))
|
2021-08-22 21:23:58 +08:00
|
|
|
return;
|
|
|
|
|
2022-10-23 23:06:55 +08:00
|
|
|
if (inProject) {
|
|
|
|
QSet<QString> filesToReparsed = calculateFilesToBeReparsed(fileName);
|
|
|
|
QStringList files = sortFilesByIncludeRelations(filesToReparsed);
|
2022-10-24 10:58:30 +08:00
|
|
|
internalInvalidateFiles(filesToReparsed);
|
2021-08-22 21:23:58 +08:00
|
|
|
|
2022-10-23 23:06:55 +08:00
|
|
|
mFilesToScanCount = files.count();
|
|
|
|
mFilesScannedCount = 0;
|
2021-08-22 21:23:58 +08:00
|
|
|
|
2022-10-23 23:06:55 +08:00
|
|
|
foreach (const QString& file,files) {
|
|
|
|
mFilesScannedCount++;
|
|
|
|
emit onProgress(file,mFilesToScanCount,mFilesScannedCount);
|
|
|
|
if (!mPreprocessor.scannedFiles().contains(file)) {
|
|
|
|
internalParse(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
internalInvalidateFile(fileName);
|
|
|
|
mFilesToScanCount = 1;
|
|
|
|
mFilesScannedCount = 0;
|
2021-08-22 21:23:58 +08:00
|
|
|
|
2022-10-22 19:33:20 +08:00
|
|
|
mFilesScannedCount++;
|
2022-10-23 23:06:55 +08:00
|
|
|
emit onProgress(fileName,mFilesToScanCount,mFilesScannedCount);
|
|
|
|
internalParse(fileName);
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
2022-10-23 23:06:55 +08:00
|
|
|
|
|
|
|
// if (inProject)
|
|
|
|
// mProjectFiles.insert(fileName);
|
|
|
|
// else {
|
|
|
|
// mProjectFiles.remove(fileName);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Parse from disk or stream
|
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::parseFileList(bool updateView)
|
|
|
|
{
|
|
|
|
if (!mEnabled)
|
|
|
|
return;
|
2021-08-22 23:48:00 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing || mLockCount>0)
|
|
|
|
return;
|
|
|
|
updateSerialId();
|
|
|
|
mParsing = true;
|
|
|
|
if (updateView)
|
|
|
|
emit onBusy();
|
|
|
|
emit onStartParsing();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto action = finally([&,this]{
|
|
|
|
mParsing = false;
|
|
|
|
if (updateView)
|
|
|
|
emit onEndParsing(mFilesScannedCount,1);
|
|
|
|
else
|
|
|
|
emit onEndParsing(mFilesScannedCount,0);
|
|
|
|
});
|
|
|
|
// Support stopping of parsing when files closes unexpectedly
|
|
|
|
mFilesScannedCount = 0;
|
|
|
|
mFilesToScanCount = mFilesToScan.count();
|
2022-10-22 19:33:20 +08:00
|
|
|
|
|
|
|
QStringList files = sortFilesByIncludeRelations(mFilesToScan);
|
2021-08-22 23:48:00 +08:00
|
|
|
// parse header files in the first parse
|
2022-10-22 19:33:20 +08:00
|
|
|
foreach (const QString& file, files) {
|
|
|
|
mFilesScannedCount++;
|
|
|
|
emit onProgress(mCurrentFile,mFilesToScanCount,mFilesScannedCount);
|
|
|
|
if (!mPreprocessor.scannedFiles().contains(file)) {
|
|
|
|
internalParse(file);
|
2021-08-22 23:48:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mFilesToScan.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::parseHardDefines()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return;
|
2021-08-27 16:38:55 +08:00
|
|
|
int oldIsSystemHeader = mIsSystemHeader;
|
|
|
|
mIsSystemHeader = true;
|
2021-08-22 23:48:00 +08:00
|
|
|
mParsing=true;
|
|
|
|
{
|
2021-08-27 16:38:55 +08:00
|
|
|
auto action = finally([&,this]{
|
2021-08-22 23:48:00 +08:00
|
|
|
mParsing = false;
|
2021-08-27 16:38:55 +08:00
|
|
|
mIsSystemHeader=oldIsSystemHeader;
|
2021-08-22 23:48:00 +08:00
|
|
|
});
|
2022-10-22 10:44:10 +08:00
|
|
|
for (const PDefine& define:mPreprocessor.hardDefines()) {
|
2021-08-22 23:48:00 +08:00
|
|
|
addStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
PStatement(), // defines don't belong to any scope
|
|
|
|
"",
|
|
|
|
"", // define has no type
|
|
|
|
define->name,
|
|
|
|
define->args,
|
|
|
|
"",
|
|
|
|
define->value,
|
|
|
|
-1,
|
|
|
|
StatementKind::skPreprocessor,
|
2022-11-01 09:02:17 +08:00
|
|
|
StatementScope::Global,
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility::None,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-22 23:48:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::parsing() const
|
2021-08-22 21:23:58 +08:00
|
|
|
{
|
|
|
|
return mParsing;
|
|
|
|
}
|
|
|
|
|
2022-10-18 12:24:59 +08:00
|
|
|
void CppParser::resetParser()
|
2021-08-22 23:48:00 +08:00
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (!mParsing && mLockCount ==0) {
|
|
|
|
mParsing = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QThread::msleep(50);
|
|
|
|
QCoreApplication* app = QApplication::instance();
|
|
|
|
app->processEvents();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto action = finally([this]{
|
|
|
|
mParsing = false;
|
|
|
|
});
|
|
|
|
emit onBusy();
|
|
|
|
mUniqId = 0;
|
2022-10-18 12:24:59 +08:00
|
|
|
|
2021-08-22 23:48:00 +08:00
|
|
|
mParseLocalHeaders = true;
|
|
|
|
mParseGlobalHeaders = true;
|
2021-08-27 16:38:55 +08:00
|
|
|
mIsSystemHeader = false;
|
|
|
|
mIsHeader = false;
|
|
|
|
mIsProjectFile = false;
|
2022-10-18 12:24:59 +08:00
|
|
|
mFilesScannedCount=0;
|
|
|
|
mFilesToScanCount = 0;
|
2021-08-22 23:48:00 +08:00
|
|
|
|
|
|
|
mCurrentScope.clear();
|
2023-03-12 18:24:58 +08:00
|
|
|
mMemberAccessibilities.clear();
|
2021-08-22 23:48:00 +08:00
|
|
|
mStatementList.clear();
|
|
|
|
|
2022-10-18 12:24:59 +08:00
|
|
|
mProjectFiles.clear();
|
|
|
|
mBlockBeginSkips.clear(); //list of for/catch block begin token index;
|
|
|
|
mBlockEndSkips.clear(); //list of for/catch block end token index;
|
|
|
|
mInlineNamespaceEndSkips.clear(); // list for inline namespace end token index;
|
|
|
|
mFilesToScan.clear(); // list of base files to scan
|
2022-10-18 19:09:46 +08:00
|
|
|
mNamespaces.clear(); // namespace and the statements in its scope
|
2021-08-28 09:01:40 +08:00
|
|
|
mInlineNamespaces.clear();
|
2021-08-22 23:48:00 +08:00
|
|
|
|
2022-10-22 10:44:10 +08:00
|
|
|
mPreprocessor.clear();
|
2022-10-22 10:59:39 +08:00
|
|
|
mTokenizer.clear();
|
2022-10-18 12:24:59 +08:00
|
|
|
|
2021-08-22 23:48:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::unFreeze()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
mLockCount--;
|
|
|
|
}
|
|
|
|
|
2021-09-13 22:45:50 +08:00
|
|
|
QSet<QString> CppParser::scannedFiles()
|
|
|
|
{
|
2022-10-22 10:44:10 +08:00
|
|
|
return mPreprocessor.scannedFiles();
|
2021-09-13 22:45:50 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 17:05:16 +08:00
|
|
|
bool CppParser::isFileParsed(const QString &filename)
|
|
|
|
{
|
2022-10-22 10:44:10 +08:00
|
|
|
return mPreprocessor.scannedFiles().contains(filename);
|
2022-08-24 17:05:16 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
QString CppParser::getScopePrefix(const PStatement& statement) const{
|
2023-03-12 18:24:58 +08:00
|
|
|
switch (statement->accessibility) {
|
|
|
|
case StatementAccessibility::Public:
|
2021-08-29 22:08:43 +08:00
|
|
|
return "public";
|
2023-03-12 18:24:58 +08:00
|
|
|
case StatementAccessibility::Private:
|
2021-08-29 22:08:43 +08:00
|
|
|
return "private";
|
2023-03-12 18:24:58 +08:00
|
|
|
case StatementAccessibility::Protected:
|
2021-08-29 22:08:43 +08:00
|
|
|
return "protected";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:23:40 +08:00
|
|
|
QString CppParser::prettyPrintStatement(const PStatement& statement, const QString& filename, int line)
|
|
|
|
{
|
|
|
|
QString result;
|
2022-03-23 12:30:58 +08:00
|
|
|
switch(statement->kind) {
|
|
|
|
case StatementKind::skPreprocessor:
|
|
|
|
if (statement->command == "__FILE__")
|
|
|
|
result = '"'+filename+'"';
|
|
|
|
else if (statement->command == "__LINE__")
|
|
|
|
result = QString("\"%1\"").arg(line);
|
|
|
|
else if (statement->command == "__DATE__")
|
|
|
|
result = QString("\"%1\"").arg(QDate::currentDate().toString(Qt::ISODate));
|
|
|
|
else if (statement->command == "__TIME__")
|
|
|
|
result = QString("\"%1\"").arg(QTime::currentTime().toString(Qt::ISODate));
|
|
|
|
else {
|
|
|
|
QString hintText = "#define";
|
|
|
|
if (statement->command != "")
|
|
|
|
hintText += ' ' + statement->command;
|
|
|
|
if (statement->args != "")
|
|
|
|
hintText += ' ' + statement->args;
|
|
|
|
if (statement->value != "")
|
|
|
|
hintText += ' ' + statement->value;
|
|
|
|
result = hintText;
|
2021-08-29 17:23:40 +08:00
|
|
|
}
|
2022-03-23 12:30:58 +08:00
|
|
|
break;
|
|
|
|
case StatementKind::skEnumClassType:
|
|
|
|
result = "enum class "+statement->command;
|
|
|
|
break;
|
|
|
|
case StatementKind::skEnumType:
|
|
|
|
result = "enum "+statement->command;
|
|
|
|
break;
|
|
|
|
case StatementKind::skEnum:
|
2023-08-07 14:23:57 +08:00
|
|
|
if (!statement->type.isEmpty())
|
|
|
|
result = statement->type + "::";
|
|
|
|
else
|
|
|
|
result = "";
|
|
|
|
result += statement->command;
|
|
|
|
if (!statement->value.isEmpty())
|
|
|
|
result += "(" + statement->value + ")";
|
2022-03-23 12:30:58 +08:00
|
|
|
break;
|
|
|
|
case StatementKind::skTypedef:
|
|
|
|
result = "typedef "+statement->type+" "+statement->command;
|
|
|
|
if (!statement->args.isEmpty())
|
|
|
|
result += " "+statement->args;
|
|
|
|
break;
|
|
|
|
case StatementKind::skAlias:
|
|
|
|
result = "using "+statement->type;
|
|
|
|
break;
|
|
|
|
case StatementKind::skFunction:
|
|
|
|
case StatementKind::skVariable:
|
|
|
|
case StatementKind::skParameter:
|
|
|
|
case StatementKind::skClass:
|
2022-11-01 09:02:17 +08:00
|
|
|
if (statement->scope!= StatementScope::Local)
|
2022-03-23 12:30:58 +08:00
|
|
|
result = getScopePrefix(statement)+ ' '; // public
|
|
|
|
result += statement->type + ' '; // void
|
|
|
|
result += statement->fullName; // A::B::C::Bar
|
|
|
|
result += statement->args; // (int a)
|
|
|
|
break;
|
|
|
|
case StatementKind::skNamespace:
|
|
|
|
result = statement->fullName; // Bar
|
|
|
|
break;
|
|
|
|
case StatementKind::skConstructor:
|
|
|
|
result = getScopePrefix(statement); // public
|
|
|
|
result += QObject::tr("constructor") + ' '; // constructor
|
|
|
|
result += statement->type + ' '; // void
|
|
|
|
result += statement->fullName; // A::B::C::Bar
|
|
|
|
result += statement->args; // (int a)
|
|
|
|
break;
|
|
|
|
case StatementKind::skDestructor:
|
|
|
|
result = getScopePrefix(statement); // public
|
|
|
|
result += QObject::tr("destructor") + ' '; // constructor
|
|
|
|
result += statement->type + ' '; // void
|
|
|
|
result += statement->fullName; // A::B::C::Bar
|
|
|
|
result += statement->args; // (int a)
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2021-08-29 17:23:40 +08:00
|
|
|
}
|
2021-08-29 22:08:43 +08:00
|
|
|
return result;
|
2021-08-22 23:48:00 +08:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:54:23 +08:00
|
|
|
QString CppParser::getTemplateParam(const PStatement& statement,
|
|
|
|
const QString& filename,
|
|
|
|
const QString& phrase,
|
|
|
|
int index,
|
2023-03-11 17:32:57 +08:00
|
|
|
const PStatement& currentScope) const
|
2021-08-22 05:50:26 +08:00
|
|
|
{
|
|
|
|
if (!statement)
|
|
|
|
return "";
|
|
|
|
if (statement->kind != StatementKind::skTypedef)
|
|
|
|
return "";
|
|
|
|
if (statement->type == phrase) // prevent infinite loop
|
|
|
|
return "";
|
2022-11-30 09:54:23 +08:00
|
|
|
return doFindTemplateParamOf(filename,statement->type,index,currentScope);
|
2021-08-22 05:50:26 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
int CppParser::getTemplateParamStart(const QString &s, int startAt, int index) const
|
2021-08-22 05:50:26 +08:00
|
|
|
{
|
2022-11-29 16:48:40 +08:00
|
|
|
int i = startAt+1;
|
|
|
|
int count=0;
|
|
|
|
while (count<index) {
|
2023-07-14 17:44:14 +08:00
|
|
|
i = getTemplateParamEnd(s,i)+1;
|
2022-11-29 16:48:40 +08:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
int CppParser::getTemplateParamEnd(const QString &s, int startAt) const {
|
2021-08-22 05:50:26 +08:00
|
|
|
int i = startAt;
|
2022-11-29 16:48:40 +08:00
|
|
|
int level = 1; // pretend we start on top of '<'
|
2021-08-22 05:50:26 +08:00
|
|
|
while (i < s.length()) {
|
|
|
|
switch (s[i].unicode()) {
|
|
|
|
case '<':
|
|
|
|
level++;
|
|
|
|
break;
|
|
|
|
case ',':
|
2022-11-29 16:48:40 +08:00
|
|
|
if (level == 1) {
|
2021-08-22 05:50:26 +08:00
|
|
|
return i;
|
2022-11-29 16:48:40 +08:00
|
|
|
}
|
2021-10-20 18:05:43 +08:00
|
|
|
break;
|
2021-08-22 05:50:26 +08:00
|
|
|
case '>':
|
|
|
|
level--;
|
|
|
|
if (level==0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return startAt;
|
|
|
|
}
|
|
|
|
|
2022-11-07 11:24:23 +08:00
|
|
|
void CppParser::addProjectFile(const QString &fileName, bool needScan)
|
2021-08-19 23:49:23 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
//value.replace('/','\\'); // only accept full file names
|
|
|
|
|
|
|
|
// Update project listing
|
2022-11-07 11:24:23 +08:00
|
|
|
mProjectFiles.insert(fileName);
|
2021-08-19 23:49:23 +08:00
|
|
|
|
|
|
|
// Only parse given file
|
2022-11-07 11:24:23 +08:00
|
|
|
if (needScan && !mPreprocessor.scannedFiles().contains(fileName)) {
|
|
|
|
mFilesToScan.insert(fileName);
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 18:24:58 +08:00
|
|
|
PStatement CppParser::addInheritedStatement(const PStatement& derived, const PStatement& inherit, StatementAccessibility access)
|
2021-08-15 16:49:37 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
PStatement statement = addStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
derived,
|
|
|
|
inherit->fileName,
|
|
|
|
inherit->type, // "Type" is already in use
|
|
|
|
inherit->command,
|
|
|
|
inherit->args,
|
|
|
|
inherit->noNameArgs,
|
|
|
|
inherit->value,
|
|
|
|
inherit->line,
|
|
|
|
inherit->kind,
|
|
|
|
inherit->scope,
|
|
|
|
access,
|
2023-07-04 20:10:08 +08:00
|
|
|
inherit->properties | StatementProperty::spInherited);
|
2021-08-15 16:49:37 +08:00
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::addChildStatement(const PStatement& parent, const QString &fileName,
|
2022-03-23 12:30:58 +08:00
|
|
|
const QString &aType,
|
2021-08-29 00:48:23 +08:00
|
|
|
const QString &command, const QString &args,
|
2022-10-31 19:37:24 +08:00
|
|
|
const QString& noNameArgs,
|
2021-08-29 00:48:23 +08:00
|
|
|
const QString &value, int line, StatementKind kind,
|
2023-03-12 18:24:58 +08:00
|
|
|
const StatementScope& scope, const StatementAccessibility& classScope,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperties properties)
|
2021-08-15 16:49:37 +08:00
|
|
|
{
|
|
|
|
return addStatement(
|
2021-08-18 05:34:04 +08:00
|
|
|
parent,
|
|
|
|
fileName,
|
|
|
|
aType,
|
|
|
|
command,
|
|
|
|
args,
|
2022-10-31 19:37:24 +08:00
|
|
|
noNameArgs,
|
2021-08-18 05:34:04 +08:00
|
|
|
value,
|
|
|
|
line,
|
|
|
|
kind,
|
|
|
|
scope,
|
|
|
|
classScope,
|
2022-11-16 10:29:20 +08:00
|
|
|
properties);
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::addStatement(const PStatement& parent,
|
|
|
|
const QString &fileName,
|
|
|
|
const QString &aType,
|
|
|
|
const QString &command,
|
|
|
|
const QString &args,
|
2022-10-31 19:37:24 +08:00
|
|
|
const QString &noNameArgs,
|
2021-08-29 00:48:23 +08:00
|
|
|
const QString &value,
|
|
|
|
int line, StatementKind kind,
|
|
|
|
const StatementScope& scope,
|
2023-03-12 18:24:58 +08:00
|
|
|
const StatementAccessibility& accessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperties properties)
|
2021-08-15 16:49:37 +08:00
|
|
|
{
|
|
|
|
// Move '*', '&' to type rather than cmd (it's in the way for code-completion)
|
|
|
|
QString newType = aType;
|
|
|
|
QString newCommand = command;
|
|
|
|
while (!newCommand.isEmpty() && (newCommand.front() == '*' || newCommand.front() == '&')) {
|
|
|
|
newType += newCommand.front();
|
|
|
|
newCommand.remove(0,1); // remove first
|
|
|
|
}
|
2022-10-28 09:47:34 +08:00
|
|
|
// if (newCommand.startsWith("::") && parent && kind!=StatementKind::skBlock ) {
|
|
|
|
// qDebug()<<command<<fileName<<line<<kind<<parent->fullName;
|
|
|
|
// }
|
2021-08-15 16:49:37 +08:00
|
|
|
|
|
|
|
if (kind == StatementKind::skConstructor
|
|
|
|
|| kind == StatementKind::skFunction
|
|
|
|
|| kind == StatementKind::skDestructor
|
2022-04-18 16:56:31 +08:00
|
|
|
|| kind == StatementKind::skVariable
|
|
|
|
) {
|
2021-08-15 16:49:37 +08:00
|
|
|
//find
|
2022-11-16 10:29:20 +08:00
|
|
|
if (properties.testFlag(StatementProperty::spHasDefinition)) {
|
2022-04-18 16:56:31 +08:00
|
|
|
PStatement oldStatement = findStatementInScope(newCommand,noNameArgs,kind,parent);
|
2022-11-16 10:29:20 +08:00
|
|
|
if (oldStatement && !oldStatement->hasDefinition()) {
|
|
|
|
oldStatement->setHasDefinition(true);
|
2022-04-18 16:56:31 +08:00
|
|
|
if (oldStatement->fileName!=fileName) {
|
2022-10-22 19:33:20 +08:00
|
|
|
PFileIncludes fileIncludes=mPreprocessor.includesList().value(fileName);
|
|
|
|
if (fileIncludes) {
|
|
|
|
fileIncludes->statements.insert(oldStatement->fullName,
|
2022-04-18 16:56:31 +08:00
|
|
|
oldStatement);
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-18 16:56:31 +08:00
|
|
|
oldStatement->definitionLine = line;
|
|
|
|
oldStatement->definitionFileName = fileName;
|
|
|
|
return oldStatement;
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PStatement result = std::make_shared<Statement>();
|
|
|
|
result->parentScope = parent;
|
|
|
|
result->type = newType;
|
|
|
|
if (!newCommand.isEmpty())
|
|
|
|
result->command = newCommand;
|
|
|
|
else {
|
|
|
|
mUniqId++;
|
|
|
|
result->command = QString("__STATEMENT__%1").arg(mUniqId);
|
|
|
|
}
|
|
|
|
result->args = args;
|
|
|
|
result->noNameArgs = noNameArgs;
|
|
|
|
result->value = value;
|
|
|
|
result->kind = kind;
|
|
|
|
result->scope = scope;
|
2023-03-12 18:24:58 +08:00
|
|
|
result->accessibility = accessibility;
|
2022-11-16 10:29:20 +08:00
|
|
|
result->properties = properties;
|
2021-08-15 16:49:37 +08:00
|
|
|
result->line = line;
|
|
|
|
result->definitionLine = line;
|
|
|
|
result->fileName = fileName;
|
|
|
|
result->definitionFileName = fileName;
|
2022-11-16 10:29:20 +08:00
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
result->setInProject(mIsProjectFile);
|
|
|
|
result->setInSystemHeader(mIsSystemHeader);
|
|
|
|
} else {
|
|
|
|
result->setInProject(false);
|
|
|
|
result->setInSystemHeader(true);
|
|
|
|
}
|
2021-08-15 16:49:37 +08:00
|
|
|
//result->children;
|
|
|
|
//result->friends;
|
2022-11-01 09:02:17 +08:00
|
|
|
if (scope == StatementScope::Local)
|
2021-08-15 16:49:37 +08:00
|
|
|
result->fullName = newCommand;
|
|
|
|
else
|
|
|
|
result->fullName = getFullStatementName(newCommand, parent);
|
2021-09-30 11:20:43 +08:00
|
|
|
result->usageCount = -1;
|
2021-08-23 17:27:17 +08:00
|
|
|
mStatementList.add(result);
|
2021-08-15 16:49:37 +08:00
|
|
|
if (result->kind == StatementKind::skNamespace) {
|
|
|
|
PStatementList namespaceList = mNamespaces.value(result->fullName,PStatementList());
|
|
|
|
if (!namespaceList) {
|
|
|
|
namespaceList=std::make_shared<StatementList>();
|
|
|
|
mNamespaces.insert(result->fullName,namespaceList);
|
|
|
|
}
|
|
|
|
namespaceList->append(result);
|
|
|
|
}
|
2021-08-19 23:49:23 +08:00
|
|
|
|
|
|
|
if (result->kind!= StatementKind::skBlock) {
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(fileName);
|
2021-08-19 23:49:23 +08:00
|
|
|
if (fileIncludes) {
|
|
|
|
fileIncludes->statements.insert(result->fullName,result);
|
|
|
|
}
|
|
|
|
}
|
2023-07-04 20:10:08 +08:00
|
|
|
// if (result->command=="sync_with_stdio") {
|
|
|
|
// qDebug()<<result->fullName<<result->isStatic()<<(int)result->accessibility;
|
|
|
|
// }
|
2021-08-15 16:49:37 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:29:20 +08:00
|
|
|
PStatement CppParser::addStatement(const PStatement &parent,
|
|
|
|
const QString &fileName,
|
|
|
|
const QString &aType,
|
|
|
|
const QString &command,
|
|
|
|
int argStart, int argEnd,
|
|
|
|
const QString &value, int line,
|
|
|
|
StatementKind kind, const StatementScope &scope,
|
2023-03-12 18:24:58 +08:00
|
|
|
const StatementAccessibility &classScope,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperties properties)
|
2022-10-31 19:37:24 +08:00
|
|
|
{
|
2022-11-04 20:27:35 +08:00
|
|
|
Q_ASSERT(mTokenizer[argStart]->text=='(');
|
2022-11-09 22:26:26 +08:00
|
|
|
QString args;
|
|
|
|
QString noNameArgs;
|
2022-10-31 19:37:24 +08:00
|
|
|
|
|
|
|
int start=argStart+1;
|
|
|
|
bool typeGetted = false;
|
|
|
|
int braceLevel=0;
|
|
|
|
QString word;
|
|
|
|
for (int i=start;i<argEnd;i++) {
|
|
|
|
QChar ch=mTokenizer[i]->text[0];
|
2022-11-04 20:27:35 +08:00
|
|
|
if (this->isIdentChar(ch)) {
|
2022-10-31 19:37:24 +08:00
|
|
|
QString spaces=(i>argStart)?" ":"";
|
|
|
|
args+=spaces;
|
2022-11-09 22:22:33 +08:00
|
|
|
word += mTokenizer[i]->text;
|
2022-10-31 19:37:24 +08:00
|
|
|
if (!typeGetted) {
|
|
|
|
noNameArgs+=spaces+word;
|
|
|
|
if (mCppTypeKeywords.contains(word) || !isCppKeyword(word))
|
|
|
|
typeGetted = true;
|
|
|
|
} else {
|
|
|
|
if (isCppKeyword(word)) {
|
|
|
|
noNameArgs+=spaces+word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
word="";
|
|
|
|
} else if (this->isDigitChar(ch)) {
|
|
|
|
args+=" ";
|
|
|
|
} else {
|
|
|
|
switch(ch.unicode()) {
|
|
|
|
case ',':
|
|
|
|
if (braceLevel==0)
|
|
|
|
typeGetted=false;
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
case '[':
|
|
|
|
case '<':
|
|
|
|
case '(':
|
|
|
|
braceLevel++;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
case ']':
|
|
|
|
case '>':
|
|
|
|
case ')':
|
|
|
|
braceLevel--;
|
|
|
|
break;
|
|
|
|
//todo: * and & processing
|
|
|
|
case '*':
|
|
|
|
case '&':
|
|
|
|
if (braceLevel==0)
|
|
|
|
word+=ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
noNameArgs+= mTokenizer[i]->text;
|
|
|
|
}
|
|
|
|
args+=mTokenizer[i]->text;
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:26:26 +08:00
|
|
|
args="("+args.trimmed()+")";
|
|
|
|
noNameArgs="("+noNameArgs.trimmed()+")";
|
2022-10-31 19:37:24 +08:00
|
|
|
return addStatement(
|
|
|
|
parent,
|
|
|
|
fileName,
|
|
|
|
aType,
|
|
|
|
command,
|
|
|
|
args,
|
|
|
|
noNameArgs,
|
|
|
|
value,
|
|
|
|
line,
|
|
|
|
kind,
|
|
|
|
scope,
|
|
|
|
classScope,
|
2022-11-16 10:29:20 +08:00
|
|
|
properties);
|
2022-10-31 19:37:24 +08:00
|
|
|
}
|
|
|
|
|
2023-05-29 09:34:07 +08:00
|
|
|
void CppParser::addMethodParameterStatement(QStringList words, int line, const PStatement &functionStatement)
|
|
|
|
{
|
|
|
|
if (words.isEmpty())
|
|
|
|
return;
|
|
|
|
QString args,suffix;
|
|
|
|
QString cmd=words.last();
|
|
|
|
parseCommandTypeAndArgs(cmd,suffix,args);
|
|
|
|
words.pop_back();
|
|
|
|
if (!cmd.isEmpty()) {
|
|
|
|
PStatement statement = doFindStatementOf(mCurrentFile,cmd,functionStatement);
|
|
|
|
bool noCmd = (statement && isTypeStatement(statement->kind));
|
|
|
|
if (!noCmd) {
|
|
|
|
addStatement(
|
|
|
|
functionStatement,
|
|
|
|
mCurrentFile,
|
|
|
|
words.join(" ")+" "+suffix, // 'int*'
|
|
|
|
cmd, // a
|
|
|
|
args,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
line,
|
|
|
|
StatementKind::skParameter,
|
|
|
|
StatementScope::Local,
|
|
|
|
StatementAccessibility::None,
|
|
|
|
StatementProperty::spHasDefinition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
void CppParser::setInheritance(int index, const PStatement& classStatement, bool isStruct)
|
2021-08-19 23:49:23 +08:00
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2021-08-19 23:49:23 +08:00
|
|
|
// Clear it. Assume it is assigned
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility lastInheritScopeType = StatementAccessibility::None;
|
2021-08-19 23:49:23 +08:00
|
|
|
// Assemble a list of statements in text form we inherit from
|
|
|
|
while (true) {
|
2022-10-31 19:37:24 +08:00
|
|
|
QString currentText = mTokenizer[index]->text;
|
|
|
|
if (currentText=='(') {
|
|
|
|
//skip to matching ')'
|
|
|
|
index=mTokenizer[index]->matchIndex;
|
2023-08-08 10:40:59 +08:00
|
|
|
} else if (currentText=="::"
|
|
|
|
|| (isIdentChar(currentText[0]))) {
|
|
|
|
KeywordType keywordType = mCppKeywords.value(currentText, KeywordType::None);
|
|
|
|
if (keywordType!=KeywordType::None) {
|
|
|
|
StatementAccessibility inheritScopeType = getClassMemberAccessibility(mTokenizer[index]->text);
|
|
|
|
if (inheritScopeType != StatementAccessibility::None) {
|
|
|
|
lastInheritScopeType = inheritScopeType;
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-31 19:37:24 +08:00
|
|
|
QString basename = currentText;
|
2023-08-07 14:23:57 +08:00
|
|
|
bool isGlobal = false;
|
|
|
|
index++;
|
|
|
|
if (basename=="::") {
|
|
|
|
if (index>=tokenCount || !isIdentChar(mTokenizer[index]->text[0])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isGlobal=true;
|
|
|
|
basename=mTokenizer[index]->text;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
//remove template staff
|
|
|
|
if (basename.endsWith('>')) {
|
|
|
|
int pBegin = basename.indexOf('<');
|
|
|
|
if (pBegin>=0)
|
|
|
|
basename.truncate(pBegin);
|
|
|
|
}
|
2023-08-07 14:23:57 +08:00
|
|
|
|
|
|
|
while (index+1<tokenCount
|
|
|
|
&& mTokenizer[index]->text=="::"
|
|
|
|
&& isIdentChar(mTokenizer[index+1]->text[0])){
|
|
|
|
basename += "::" + mTokenizer[index+1]->text;
|
|
|
|
index+=2;
|
|
|
|
//remove template staff
|
|
|
|
if (basename.endsWith('>')) {
|
|
|
|
int pBegin = basename.indexOf('<');
|
|
|
|
if (pBegin>=0)
|
|
|
|
basename.truncate(pBegin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
// Find the corresponding PStatement
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement statement = doFindStatementOf(mCurrentFile,basename,
|
2023-08-07 14:23:57 +08:00
|
|
|
isGlobal?PStatement():classStatement->parentScope.lock());
|
2023-08-08 10:40:59 +08:00
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
if (statement && statement->kind == StatementKind::skClass) {
|
|
|
|
inheritClassStatement(classStatement,isStruct,statement,lastInheritScopeType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-08 10:40:59 +08:00
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
index++;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (index >= tokenCount)
|
2021-08-19 23:49:23 +08:00
|
|
|
break;
|
|
|
|
if (mTokenizer[index]->text.front() == '{'
|
|
|
|
|| mTokenizer[index]->text.front() == ';')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
bool CppParser::isCurrentScope(const QString &command) const
|
2021-08-19 23:49:23 +08:00
|
|
|
{
|
|
|
|
PStatement statement = getCurrentScope();
|
|
|
|
if (!statement)
|
|
|
|
return false;
|
|
|
|
QString s = command;
|
|
|
|
// remove template staff
|
2022-11-02 22:48:25 +08:00
|
|
|
if (s.endsWith('>')) {
|
|
|
|
int i= command.indexOf('<');
|
|
|
|
if (i>=0) {
|
|
|
|
s.truncate(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QString s2 = statement->command;
|
|
|
|
if (s2.endsWith('>')) {
|
|
|
|
int i= s2.indexOf('<');
|
|
|
|
if (i>=0) {
|
|
|
|
s2.truncate(i);
|
|
|
|
}
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
2022-11-02 22:48:25 +08:00
|
|
|
return (s2 == s);
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
|
2022-11-02 13:38:26 +08:00
|
|
|
void CppParser::addSoloScopeLevel(PStatement& statement, int line, bool shouldResetBlock)
|
2021-08-15 16:49:37 +08:00
|
|
|
{
|
|
|
|
// Add class list
|
|
|
|
|
2022-11-02 13:38:26 +08:00
|
|
|
PStatement parentScope;
|
|
|
|
if (shouldResetBlock && statement && (statement->kind == StatementKind::skBlock)) {
|
|
|
|
parentScope = statement->parentScope.lock();
|
|
|
|
while (parentScope && (parentScope->kind == StatementKind::skBlock)) {
|
|
|
|
parentScope = parentScope->parentScope.lock();
|
|
|
|
}
|
|
|
|
if (!parentScope)
|
|
|
|
statement.reset();
|
|
|
|
}
|
2021-08-15 16:49:37 +08:00
|
|
|
|
2023-03-12 18:24:58 +08:00
|
|
|
if (mMemberAccessibilities.count()>0) {
|
|
|
|
mMemberAccessibilities.back() = mCurrentMemberAccessibility;
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentScope.append(statement);
|
|
|
|
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(mCurrentFile);
|
2021-08-15 16:49:37 +08:00
|
|
|
|
|
|
|
if (fileIncludes) {
|
2021-08-22 05:50:26 +08:00
|
|
|
fileIncludes->scopes.addScope(line,statement);
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set new scope
|
|
|
|
if (!statement)
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility = StatementAccessibility::None; // {}, namespace or class that doesn't exist
|
2021-08-15 16:49:37 +08:00
|
|
|
else if (statement->kind == StatementKind::skNamespace)
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility = StatementAccessibility::None;
|
2021-08-15 16:49:37 +08:00
|
|
|
else if (statement->type == "class")
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility = StatementAccessibility::Private; // classes are private by default
|
2021-08-15 16:49:37 +08:00
|
|
|
else
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility = StatementAccessibility::Public; // structs are public by default
|
|
|
|
mMemberAccessibilities.push_back(mCurrentMemberAccessibility);
|
2022-11-11 21:20:57 +08:00
|
|
|
#ifdef QT_DEBUG
|
2023-02-06 19:37:01 +08:00
|
|
|
// if (mCurrentClassScope.count()<=2)
|
|
|
|
// qDebug()<<"++add scope"<<mCurrentFile<<line<<mCurrentClassScope.count();
|
2022-11-11 21:20:57 +08:00
|
|
|
#endif
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
void CppParser::removeScopeLevel(int line)
|
|
|
|
{
|
|
|
|
// Remove class list
|
|
|
|
if (mCurrentScope.isEmpty())
|
|
|
|
return; // TODO: should be an exception
|
2022-11-11 21:20:57 +08:00
|
|
|
#ifdef QT_DEBUG
|
2023-02-06 19:37:01 +08:00
|
|
|
// if (mCurrentClassScope.count()<=2)
|
|
|
|
// qDebug()<<"--remove scope"<<mCurrentFile<<line<<mCurrentClassScope.count();
|
2022-11-11 21:20:57 +08:00
|
|
|
#endif
|
2022-11-02 13:38:26 +08:00
|
|
|
PStatement currentScope = getCurrentScope();
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(mCurrentFile);
|
2022-11-05 08:42:54 +08:00
|
|
|
if (currentScope) {
|
|
|
|
if (currentScope->kind == StatementKind::skBlock) {
|
|
|
|
if (currentScope->children.isEmpty()) {
|
|
|
|
// remove no children block
|
|
|
|
if (fileIncludes) {
|
|
|
|
fileIncludes->scopes.removeLastScope();
|
|
|
|
}
|
|
|
|
mStatementList.deleteStatement(currentScope);
|
|
|
|
} else {
|
|
|
|
fileIncludes->statements.insert(currentScope->fullName,currentScope);
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
2022-11-05 08:42:54 +08:00
|
|
|
} else if (currentScope->kind == StatementKind::skClass) {
|
|
|
|
mIndex=indexOfNextSemicolon(mIndex);
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mCurrentScope.pop_back();
|
2023-03-12 18:24:58 +08:00
|
|
|
mMemberAccessibilities.pop_back();
|
2021-08-19 23:49:23 +08:00
|
|
|
|
|
|
|
// Set new scope
|
|
|
|
currentScope = getCurrentScope();
|
|
|
|
// fileIncludes:=FindFileIncludes(fCurrentFile);
|
2021-08-22 05:50:26 +08:00
|
|
|
if (fileIncludes && fileIncludes->scopes.lastScope()!=currentScope) {
|
|
|
|
fileIncludes->scopes.addScope(line,currentScope);
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!currentScope) {
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility = StatementAccessibility::None;
|
2021-08-19 23:49:23 +08:00
|
|
|
} else {
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility = mMemberAccessibilities.back();
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 07:26:13 +08:00
|
|
|
void CppParser::internalClear()
|
|
|
|
{
|
|
|
|
mCurrentScope.clear();
|
2023-03-12 18:24:58 +08:00
|
|
|
mMemberAccessibilities.clear();
|
2021-11-12 07:26:13 +08:00
|
|
|
mIndex = 0;
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility = StatementAccessibility::None;
|
2021-11-12 07:26:13 +08:00
|
|
|
mBlockBeginSkips.clear();
|
|
|
|
mBlockEndSkips.clear();
|
|
|
|
mInlineNamespaceEndSkips.clear();
|
|
|
|
}
|
|
|
|
|
2022-10-22 19:33:20 +08:00
|
|
|
QStringList CppParser::sortFilesByIncludeRelations(const QSet<QString> &files)
|
|
|
|
{
|
|
|
|
QStringList result;
|
2022-10-23 21:42:11 +08:00
|
|
|
QSet<QString> saveScannedFiles;
|
|
|
|
|
|
|
|
saveScannedFiles=mPreprocessor.scannedFiles();
|
2022-10-22 19:33:20 +08:00
|
|
|
|
|
|
|
//rebuild file include relations
|
|
|
|
foreach(const QString& file, files) {
|
2022-10-24 10:58:30 +08:00
|
|
|
if (mPreprocessor.scannedFiles().contains(file))
|
|
|
|
continue;
|
2022-10-22 19:33:20 +08:00
|
|
|
//already removed in interalInvalidateFiles
|
|
|
|
//mPreprocessor.removeScannedFile(file);
|
2022-10-23 23:06:55 +08:00
|
|
|
//we only use local include relations
|
|
|
|
mPreprocessor.setScanOptions(false, true);
|
2023-02-15 16:24:24 +08:00
|
|
|
mPreprocessor.preprocess(file);
|
2022-10-22 19:33:20 +08:00
|
|
|
mPreprocessor.clearTempResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet<QString> fileSet=files;
|
|
|
|
while (!fileSet.isEmpty()) {
|
|
|
|
bool found=false;
|
|
|
|
foreach (const QString& file,fileSet) {
|
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(file);
|
|
|
|
bool hasInclude=false;
|
|
|
|
foreach(const QString& inc,fileIncludes->includeFiles.keys()) {
|
|
|
|
if (fileSet.contains(inc)) {
|
|
|
|
hasInclude=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!hasInclude) {
|
|
|
|
result.push_front(file);
|
|
|
|
fileSet.remove(file);
|
|
|
|
found=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
foreach (const QString& file,fileSet) {
|
|
|
|
result.push_front(file);
|
|
|
|
fileSet.remove(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-23 21:42:11 +08:00
|
|
|
QSet<QString> newScannedFiles = mPreprocessor.scannedFiles();
|
|
|
|
foreach(const QString& file, newScannedFiles) {
|
|
|
|
if (!saveScannedFiles.contains(file))
|
|
|
|
mPreprocessor.removeScannedFile(file);
|
2022-10-22 19:33:20 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-07 14:23:57 +08:00
|
|
|
int CppParser::evaluateConstExpr(int endIndex, bool &ok)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (mIndex>=endIndex) {
|
|
|
|
ok=false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
result = evaluateAdditionConstExpr(endIndex,ok);
|
|
|
|
if (mIndex!=endIndex)
|
|
|
|
ok = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::evaluateAdditionConstExpr(int endIndex, bool &ok)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (mIndex>=endIndex) {
|
|
|
|
ok=false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
result = evaluateMultiplyConstExpr(endIndex,ok);
|
|
|
|
if (!ok)
|
|
|
|
return result;
|
|
|
|
while (mIndex<endIndex) {
|
|
|
|
if (mTokenizer[mIndex]->text=='+') {
|
|
|
|
mIndex++;
|
|
|
|
int temp = evaluateMultiplyConstExpr(endIndex,ok);
|
|
|
|
if (!ok)
|
|
|
|
return result;
|
|
|
|
result+=temp;
|
|
|
|
} else if (mTokenizer[mIndex]->text=='-') {
|
|
|
|
mIndex++;
|
|
|
|
int temp = evaluateMultiplyConstExpr(endIndex,ok);
|
|
|
|
if (!ok)
|
|
|
|
return result;
|
|
|
|
result-=temp;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::evaluateMultiplyConstExpr(int endIndex, bool &ok)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (mIndex>=endIndex) {
|
|
|
|
ok=false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
result = evaluateConstExprTerm(endIndex,ok);
|
|
|
|
if (!ok)
|
|
|
|
return result;
|
|
|
|
while (mIndex<endIndex) {
|
|
|
|
if (mTokenizer[mIndex]->text=='*') {
|
|
|
|
mIndex++;
|
|
|
|
int temp = evaluateConstExprTerm(endIndex,ok);
|
|
|
|
if (!ok)
|
|
|
|
return result;
|
|
|
|
result*=temp;
|
|
|
|
} else if (mTokenizer[mIndex]->text=='/') {
|
|
|
|
mIndex++;
|
|
|
|
int temp = evaluateConstExprTerm(endIndex,ok);
|
|
|
|
if (!ok)
|
|
|
|
return result;
|
|
|
|
result/=temp;
|
|
|
|
} else if (mTokenizer[mIndex]->text=='%') {
|
|
|
|
mIndex++;
|
|
|
|
int temp = evaluateConstExprTerm(endIndex,ok);
|
|
|
|
if (!ok)
|
|
|
|
return result;
|
|
|
|
result%=temp;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::evaluateConstExprTerm(int endIndex, bool &ok)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (mIndex>=endIndex) {
|
|
|
|
ok=false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (mTokenizer[mIndex]->text=="(") {
|
|
|
|
mIndex++;
|
|
|
|
result = evaluateConstExpr(endIndex, ok);
|
|
|
|
if (mIndex>=endIndex || mTokenizer[mIndex]->text!=')')
|
|
|
|
ok=false;
|
|
|
|
mIndex++;
|
|
|
|
} else if (isIdentChar(mTokenizer[mIndex]->text[0])
|
|
|
|
|| mTokenizer[mIndex]->text=="::") {
|
|
|
|
QString s = mTokenizer[mIndex]->text;
|
|
|
|
QSet<QString> searched;
|
2023-08-07 18:28:30 +08:00
|
|
|
bool isGlobal = false;
|
2023-08-07 14:23:57 +08:00
|
|
|
mIndex++;
|
|
|
|
if (s=="::") {
|
|
|
|
if (mIndex>=endIndex || !isIdentChar(mTokenizer[mIndex]->text[0])) {
|
|
|
|
ok=false;
|
|
|
|
return result;
|
|
|
|
}
|
2023-08-07 18:28:30 +08:00
|
|
|
isGlobal = true;
|
2023-08-07 14:23:57 +08:00
|
|
|
s+=mTokenizer[mIndex]->text;
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
while (mIndex+1<endIndex
|
|
|
|
&& mTokenizer[mIndex]->text=="::"
|
|
|
|
&& isIdentChar(mTokenizer[mIndex+1]->text[0])){
|
|
|
|
s += "::" + mTokenizer[mIndex+1]->text;
|
|
|
|
mIndex+=2;
|
|
|
|
}
|
|
|
|
while (true){
|
|
|
|
//prevent infinite loop
|
|
|
|
if (searched.contains(s)) {
|
|
|
|
ok=false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
searched.insert(s);
|
2023-08-07 18:28:30 +08:00
|
|
|
PStatement statement = doFindStatementOf(
|
|
|
|
mCurrentFile,s,
|
|
|
|
isGlobal?PStatement():getCurrentScope());
|
|
|
|
|
2023-08-07 14:23:57 +08:00
|
|
|
if (!statement) {
|
|
|
|
ok=false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (statement->kind == StatementKind::skEnum) {
|
|
|
|
result = statement->value.toInt(&ok);
|
|
|
|
break;
|
2023-08-07 18:28:30 +08:00
|
|
|
} else if (statement->kind == StatementKind::skAlias) {
|
|
|
|
s = statement->value;
|
|
|
|
} else {
|
|
|
|
ok=false;
|
|
|
|
return result;
|
2023-08-07 14:23:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = evaluateLiteralNumber(endIndex,ok);
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::evaluateLiteralNumber(int endIndex, bool &ok)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (mIndex>=endIndex) {
|
|
|
|
ok=false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (mTokenizer[mIndex]->text.startsWith("0x")
|
|
|
|
|| mTokenizer[mIndex]->text.startsWith("0X"))
|
|
|
|
result = mTokenizer[mIndex]->text.mid(2).toInt(&ok,16);
|
|
|
|
else if (mTokenizer[mIndex]->text.startsWith("0b")
|
|
|
|
|| mTokenizer[mIndex]->text.startsWith("0B"))
|
|
|
|
result = mTokenizer[mIndex]->text.mid(2).toInt(&ok,2);
|
|
|
|
else if (mTokenizer[mIndex]->text.startsWith("0"))
|
|
|
|
result = mTokenizer[mIndex]->text.toInt(&ok,8);
|
|
|
|
else
|
|
|
|
result = mTokenizer[mIndex]->text.toInt(&ok);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
bool CppParser::checkForKeyword(KeywordType& keywordType)
|
|
|
|
{
|
2022-11-04 20:27:35 +08:00
|
|
|
keywordType = mCppKeywords.value(mTokenizer[mIndex]->text,KeywordType::NotKeyword);
|
2022-11-01 09:02:17 +08:00
|
|
|
switch(keywordType) {
|
|
|
|
case KeywordType::Catch:
|
|
|
|
case KeywordType::For:
|
|
|
|
case KeywordType::Public:
|
|
|
|
case KeywordType::Private:
|
2023-08-08 10:40:59 +08:00
|
|
|
case KeywordType::Protected:
|
2023-03-12 10:50:47 +08:00
|
|
|
case KeywordType::Struct:
|
2022-11-01 09:02:17 +08:00
|
|
|
case KeywordType::Enum:
|
|
|
|
case KeywordType::Inline:
|
|
|
|
case KeywordType::Namespace:
|
|
|
|
case KeywordType::Typedef:
|
|
|
|
case KeywordType::Using:
|
|
|
|
case KeywordType::Friend:
|
2022-11-04 20:27:35 +08:00
|
|
|
case KeywordType::None:
|
|
|
|
case KeywordType::NotKeyword:
|
2022-11-04 23:44:11 +08:00
|
|
|
case KeywordType::DeclType:
|
2023-03-12 12:42:44 +08:00
|
|
|
case KeywordType::Operator:
|
2022-11-01 00:01:46 +08:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
bool CppParser::checkForNamespace(KeywordType keywordType)
|
2021-08-15 17:52:39 +08:00
|
|
|
{
|
2022-11-01 09:02:17 +08:00
|
|
|
return (keywordType==KeywordType::Namespace &&(mIndex < mTokenizer.tokenCount()-1))
|
2022-11-01 00:01:46 +08:00
|
|
|
|| (
|
2022-11-01 09:02:17 +08:00
|
|
|
keywordType==KeywordType::Inline
|
2022-11-01 00:01:46 +08:00
|
|
|
&& (mIndex+1 < mTokenizer.tokenCount()-1)
|
|
|
|
&&mTokenizer[mIndex+1]->text == "namespace"
|
|
|
|
);
|
2021-08-15 17:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::checkForPreprocessor()
|
|
|
|
{
|
2021-08-23 17:27:17 +08:00
|
|
|
return (mTokenizer[mIndex]->text.startsWith('#'));
|
2021-08-15 17:52:39 +08:00
|
|
|
}
|
|
|
|
|
2022-11-04 20:27:35 +08:00
|
|
|
//bool CppParser::checkForLambda()
|
|
|
|
//{
|
|
|
|
// return (mIndex+1<mTokenizer.tokenCount()
|
|
|
|
// && mTokenizer[mIndex]->text.startsWith('[')
|
|
|
|
// && mTokenizer[mIndex+1]->text=='(');
|
|
|
|
//}
|
2022-11-02 22:48:25 +08:00
|
|
|
|
2023-03-12 18:24:58 +08:00
|
|
|
bool CppParser::checkForAccessibilitySpecifiers(KeywordType keywordType)
|
2021-08-15 17:52:39 +08:00
|
|
|
{
|
2023-03-12 18:24:58 +08:00
|
|
|
return (keywordType == KeywordType::Public || keywordType == KeywordType::Protected
|
|
|
|
|| keywordType == KeywordType::Private);
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
bool CppParser::checkForStructs(KeywordType keywordType)
|
2021-08-15 20:25:54 +08:00
|
|
|
{
|
|
|
|
int dis = 0;
|
2023-03-12 19:17:39 +08:00
|
|
|
|
2023-03-12 10:50:47 +08:00
|
|
|
// int keyLen = calcKeyLenForStruct(word);
|
|
|
|
// if (keyLen<0)
|
|
|
|
// return false;
|
|
|
|
// bool result = (word.length() == keyLen) || isSpaceChar(word[keyLen])
|
|
|
|
// || (word[keyLen] == '[');
|
|
|
|
bool result;
|
2023-03-12 19:17:39 +08:00
|
|
|
if (keywordType == KeywordType::Friend
|
|
|
|
|| keywordType == KeywordType::Public
|
|
|
|
|| keywordType == KeywordType::Private) {
|
|
|
|
dis = 1;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex+dis>=mTokenizer.tokenCount()) {
|
|
|
|
mIndex++;
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-12 10:50:47 +08:00
|
|
|
result = (mCppKeywords.value(mTokenizer[mIndex+dis]->text,KeywordType::None)==KeywordType::Struct);
|
|
|
|
} else {
|
|
|
|
result = (keywordType==KeywordType::Struct);
|
|
|
|
}
|
2021-08-15 20:25:54 +08:00
|
|
|
|
|
|
|
if (result) {
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
|
|
|
|
|
|
|
if (mIndex >= tokenCount - 2 - dis)
|
2023-03-12 10:50:47 +08:00
|
|
|
return false;
|
2021-08-15 20:25:54 +08:00
|
|
|
if (mTokenizer[mIndex + 2+dis]->text[0] != ';') { // not: class something;
|
|
|
|
int i = mIndex+dis +1;
|
|
|
|
// the check for ']' was added because of this example:
|
|
|
|
// struct option long_options[] = {
|
|
|
|
// {"debug", 1, 0, 'D'},
|
|
|
|
// {"info", 0, 0, 'i'},
|
|
|
|
// ...
|
|
|
|
// };
|
2023-03-12 23:45:03 +08:00
|
|
|
while (i < tokenCount) {
|
2021-08-15 20:25:54 +08:00
|
|
|
QChar ch = mTokenizer[i]->text.back();
|
|
|
|
if (ch=='{' || ch == ':')
|
|
|
|
break;
|
|
|
|
switch(ch.unicode()) {
|
|
|
|
case ';':
|
2022-11-01 00:01:46 +08:00
|
|
|
case '{':
|
2021-08-15 20:25:54 +08:00
|
|
|
case '}':
|
|
|
|
case ',':
|
2022-11-01 00:01:46 +08:00
|
|
|
case '(':
|
2021-08-15 20:25:54 +08:00
|
|
|
case ')':
|
2022-11-01 00:01:46 +08:00
|
|
|
case '[':
|
2021-08-15 20:25:54 +08:00
|
|
|
case ']':
|
|
|
|
case '=':
|
|
|
|
case '*':
|
|
|
|
case '&':
|
|
|
|
case '%':
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '~':
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:47:35 +08:00
|
|
|
bool CppParser::checkForTypedefEnum()
|
|
|
|
{
|
|
|
|
//we assume that typedef is the current index, so we check the next
|
|
|
|
//should call CheckForTypedef first!!!
|
2022-11-01 00:01:46 +08:00
|
|
|
return (mIndex+1 < mTokenizer.tokenCount() ) &&
|
2021-08-16 00:47:35 +08:00
|
|
|
(mTokenizer[mIndex + 1]->text == "enum");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::checkForTypedefStruct()
|
|
|
|
{
|
|
|
|
//we assume that typedef is the current index, so we check the next
|
|
|
|
//should call CheckForTypedef first!!!
|
|
|
|
if (mIndex+1 >= mTokenizer.tokenCount())
|
|
|
|
return false;
|
2023-03-12 10:50:47 +08:00
|
|
|
return (mCppKeywords.value(mTokenizer[mIndex+1]->text,KeywordType::None)==KeywordType::Struct);
|
|
|
|
|
2021-08-16 00:47:35 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
bool CppParser::checkForUsing(KeywordType keywordType)
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
2022-11-01 09:02:17 +08:00
|
|
|
return keywordType==KeywordType::Using && (mIndex < mTokenizer.tokenCount()-1);
|
2021-08-16 00:47:35 +08:00
|
|
|
}
|
|
|
|
|
2022-11-04 23:44:11 +08:00
|
|
|
void CppParser::checkAndHandleMethodOrVar(KeywordType keywordType)
|
2022-11-04 20:27:35 +08:00
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
|
|
|
|
|
|
|
if (mIndex+2>=tokenCount) {
|
2023-03-12 12:42:44 +08:00
|
|
|
mIndex+=2; // let's finish;
|
2022-11-04 20:27:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString currentText=mTokenizer[mIndex]->text;
|
2022-11-04 23:44:11 +08:00
|
|
|
if (keywordType==KeywordType::DeclType) {
|
|
|
|
if (mTokenizer[mIndex+1]->text=='(') {
|
|
|
|
currentText="auto";
|
|
|
|
mIndex=mTokenizer[mIndex+1]->matchIndex+1;
|
2023-03-02 11:28:03 +08:00
|
|
|
|
2022-11-04 23:44:11 +08:00
|
|
|
} else {
|
|
|
|
currentText=mTokenizer[mIndex+1]->text;
|
|
|
|
mIndex+=2;
|
|
|
|
}
|
2023-03-10 20:13:52 +08:00
|
|
|
} else {
|
2023-03-12 12:42:44 +08:00
|
|
|
if (keywordType == KeywordType::Operator) {
|
2023-03-10 20:13:52 +08:00
|
|
|
handleOperatorOverloading("",
|
|
|
|
mIndex,
|
|
|
|
false);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-04 23:44:11 +08:00
|
|
|
mIndex++;
|
2023-03-10 20:13:52 +08:00
|
|
|
}
|
2022-11-04 20:27:35 +08:00
|
|
|
//next token must be */&/word/(/{
|
|
|
|
if (mTokenizer[mIndex]->text=='(') {
|
|
|
|
int indexAfterParentheis=mTokenizer[mIndex]->matchIndex+1;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (indexAfterParentheis>=tokenCount) {
|
2022-11-04 20:27:35 +08:00
|
|
|
//error
|
|
|
|
mIndex=indexAfterParentheis;
|
|
|
|
} else if (mTokenizer[indexAfterParentheis]->text=='(') {
|
|
|
|
// operator overloading like (operator int)
|
|
|
|
if (mTokenizer[mIndex+1]->text=="operator") {
|
|
|
|
mIndex=indexAfterParentheis;
|
|
|
|
handleMethod(StatementKind::skFunction,"",
|
|
|
|
mergeArgs(mIndex+1,mTokenizer[mIndex]->matchIndex-1),
|
2023-03-10 20:13:52 +08:00
|
|
|
indexAfterParentheis,false,false,true);
|
2022-11-04 20:27:35 +08:00
|
|
|
} else {
|
2023-03-02 11:28:03 +08:00
|
|
|
handleVar(currentText,false,false);
|
2022-11-04 20:27:35 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-02-06 14:04:38 +08:00
|
|
|
if (currentText=="operator") {
|
2022-11-04 20:27:35 +08:00
|
|
|
// operator overloading
|
2023-02-06 14:04:38 +08:00
|
|
|
handleOperatorOverloading(
|
2023-03-10 20:13:52 +08:00
|
|
|
"",
|
2023-02-06 14:04:38 +08:00
|
|
|
mIndex,
|
|
|
|
false);
|
2022-11-04 20:27:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//check for constructor like Foo::Foo()
|
|
|
|
QString name;
|
|
|
|
QString temp,temp2;
|
|
|
|
QString parentName;
|
|
|
|
if (splitLastMember(currentText,name,temp)) {
|
|
|
|
//has '::'
|
|
|
|
bool isDestructor=false;
|
|
|
|
if (!splitLastMember(temp,parentName,temp2))
|
|
|
|
parentName=temp;
|
|
|
|
if (name.startsWith('~'))
|
|
|
|
name=name.mid(1);
|
2022-11-05 18:58:15 +08:00
|
|
|
if (removeTemplateParams(name)==removeTemplateParams(parentName)) {
|
2022-11-04 20:27:35 +08:00
|
|
|
handleMethod( (isDestructor?StatementKind::skDestructor:StatementKind::skConstructor),
|
|
|
|
"",
|
|
|
|
currentText,
|
|
|
|
mIndex,false,false);
|
2022-11-05 18:58:15 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-11-04 20:27:35 +08:00
|
|
|
}
|
|
|
|
// check for constructor like:
|
|
|
|
// class Foo {
|
|
|
|
// Foo();
|
|
|
|
// };
|
|
|
|
PStatement scope=getCurrentScope();
|
|
|
|
if (scope && scope->kind==StatementKind::skClass
|
|
|
|
&& removeTemplateParams(scope->command) == removeTemplateParams(currentText)) {
|
|
|
|
handleMethod(StatementKind::skConstructor,"",
|
|
|
|
currentText,
|
|
|
|
mIndex,false,false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// function call, skip it
|
2022-11-09 22:22:33 +08:00
|
|
|
mIndex=moveToEndOfStatement(mIndex,true);
|
2022-11-04 20:27:35 +08:00
|
|
|
}
|
|
|
|
} else if (mTokenizer[mIndex]->text.startsWith('*')
|
|
|
|
|| mTokenizer[mIndex]->text.startsWith('&')
|
2023-02-06 19:37:01 +08:00
|
|
|
|| mTokenizer[mIndex]->text=="::"
|
2022-11-04 20:27:35 +08:00
|
|
|
|| tokenIsIdentifier(mTokenizer[mIndex]->text)
|
|
|
|
) {
|
|
|
|
// it should be function/var
|
|
|
|
|
|
|
|
bool isStatic = false;
|
|
|
|
bool isFriend = false;
|
|
|
|
bool isExtern = false;
|
|
|
|
|
2023-02-06 14:04:38 +08:00
|
|
|
QString sType; // should contain type "int"
|
|
|
|
QString sName; // should contain function name "foo::function"
|
|
|
|
if (mTokenizer[mIndex]->text=="::") {
|
|
|
|
mIndex--;
|
|
|
|
} else {
|
|
|
|
if (currentText=="::") {
|
|
|
|
sName = currentText;
|
|
|
|
} else {
|
2023-02-25 12:15:54 +08:00
|
|
|
if (currentText == "static")
|
|
|
|
isStatic = true;
|
|
|
|
else if (currentText == "friend")
|
|
|
|
isFriend = true;
|
|
|
|
else if (currentText == "extern")
|
|
|
|
isExtern = true;
|
2023-02-06 14:04:38 +08:00
|
|
|
sType = currentText;
|
|
|
|
}
|
|
|
|
}
|
2022-11-04 20:27:35 +08:00
|
|
|
|
|
|
|
// Gather data for the string parts
|
2023-03-12 23:45:03 +08:00
|
|
|
while (mIndex+1 < tokenCount) {
|
2023-02-06 14:04:38 +08:00
|
|
|
if (mTokenizer[mIndex]->text=="operator") {
|
|
|
|
handleOperatorOverloading(sType,
|
2023-03-10 20:50:46 +08:00
|
|
|
//sName,
|
2023-02-06 14:04:38 +08:00
|
|
|
mIndex,
|
2023-03-10 20:50:46 +08:00
|
|
|
isStatic);
|
2023-02-06 14:04:38 +08:00
|
|
|
return;
|
|
|
|
} else if (mTokenizer[mIndex + 1]->text == '(') {
|
2023-08-13 15:41:17 +08:00
|
|
|
#ifdef ENABLE_SDCC
|
2023-08-13 14:46:53 +08:00
|
|
|
if (mLanguage==ParserLanguage::SDCC && mTokenizer[mIndex]->text=="__at") {
|
|
|
|
if (!sName.isEmpty()) {
|
|
|
|
sType = sType+" "+sName;
|
|
|
|
sName = "";
|
|
|
|
}
|
|
|
|
sType+=" __at";
|
|
|
|
mIndex++;
|
|
|
|
int idx= mTokenizer[mIndex]->matchIndex;
|
|
|
|
if (idx<tokenCount) {
|
|
|
|
for (int i=mIndex;i<=idx;i++) {
|
|
|
|
sType+=mTokenizer[i]->text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mIndex=idx+1;
|
|
|
|
continue;
|
|
|
|
}
|
2023-08-13 15:41:17 +08:00
|
|
|
#endif
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex+2<tokenCount && mTokenizer[mIndex+2]->text == '*') {
|
2022-11-04 20:27:35 +08:00
|
|
|
//foo(*blabla), it's a function pointer var
|
2023-02-06 14:04:38 +08:00
|
|
|
handleVar(sType+" "+sName,isExtern,isStatic);
|
2022-11-04 20:27:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int indexAfter=mTokenizer[mIndex + 1]->matchIndex+1;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (indexAfter>=tokenCount) {
|
2022-11-04 20:27:35 +08:00
|
|
|
//error
|
|
|
|
mIndex=indexAfter;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//if it's like: foo(...)(...)
|
|
|
|
if (mTokenizer[indexAfter]->text=='(') {
|
|
|
|
//foo(...)(...), it's a function pointer var
|
2023-02-06 14:04:38 +08:00
|
|
|
handleVar(sType+" "+sName,isExtern,isStatic);
|
2022-11-04 20:27:35 +08:00
|
|
|
//Won't implement: ignore function decl like int (text)(int x) { };
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//it's not a function define
|
2023-05-24 20:32:27 +08:00
|
|
|
if (mTokenizer[indexAfter]->text == ',') {
|
|
|
|
// var decl with init
|
|
|
|
handleVar(sType+" "+sName,isExtern,isStatic);
|
|
|
|
return;
|
2022-11-04 20:27:35 +08:00
|
|
|
}
|
2023-02-06 19:37:01 +08:00
|
|
|
if (mTokenizer[indexAfter]->text[0] == ';' && sType!="void") {
|
2022-11-04 20:27:35 +08:00
|
|
|
//function can only be defined in global/namespaces/classes
|
|
|
|
PStatement currentScope=getCurrentScope();
|
|
|
|
if (currentScope) {
|
|
|
|
//in namespace, it might be function or object initilization
|
2023-02-06 19:37:01 +08:00
|
|
|
if (currentScope->kind == StatementKind::skNamespace) {
|
|
|
|
if (isNotFuncArgs(mIndex + 1)) {
|
|
|
|
// var decl with init
|
|
|
|
handleVar(sType+" "+sName,isExtern,isStatic);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-04 20:27:35 +08:00
|
|
|
} else if (currentScope->kind != StatementKind::skClass) {
|
2023-02-06 19:37:01 +08:00
|
|
|
//not in class, it can't be a valid function definition
|
2022-11-04 20:27:35 +08:00
|
|
|
// var decl with init
|
2023-02-06 14:04:38 +08:00
|
|
|
handleVar(sType+" "+sName,isExtern,isStatic);
|
2022-11-04 20:27:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//variable can't be initialized in class definition, it must be a function
|
|
|
|
} else if (isNotFuncArgs(mIndex + 1)){
|
|
|
|
// var decl with init
|
2023-02-06 14:04:38 +08:00
|
|
|
handleVar(sType+" "+sName,isExtern,isStatic);
|
2022-11-04 20:27:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 17:40:59 +08:00
|
|
|
bool isDestructor = false;
|
2023-02-06 19:37:01 +08:00
|
|
|
if (!sName.isEmpty()) {
|
|
|
|
if (sName.endsWith("::"))
|
|
|
|
sName+=mTokenizer[mIndex]->text;
|
2023-03-24 17:40:59 +08:00
|
|
|
else if (sName.endsWith("~")) {
|
|
|
|
isDestructor=true;
|
|
|
|
sName+=mTokenizer[mIndex]->text;
|
|
|
|
} else {
|
2023-02-06 19:37:01 +08:00
|
|
|
sType += " "+sName;
|
|
|
|
sName = mTokenizer[mIndex]->text;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
sName = mTokenizer[mIndex]->text;
|
2022-11-04 20:27:35 +08:00
|
|
|
mIndex++;
|
|
|
|
|
2023-03-24 17:40:59 +08:00
|
|
|
if (isDestructor)
|
|
|
|
handleMethod(StatementKind::skDestructor,sType,
|
|
|
|
sName,mIndex,false,isFriend);
|
|
|
|
else {
|
|
|
|
sType=sType.trimmed();
|
|
|
|
if (sType.isEmpty())
|
|
|
|
handleMethod(StatementKind::skConstructor,sType,
|
|
|
|
sName,mIndex,false,isFriend);
|
|
|
|
else
|
|
|
|
handleMethod(StatementKind::skFunction,sType,
|
2022-11-04 20:27:35 +08:00
|
|
|
sName,mIndex,isStatic,isFriend);
|
2023-03-24 17:40:59 +08:00
|
|
|
}
|
2022-11-04 20:27:35 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
} else if (
|
|
|
|
mTokenizer[mIndex + 1]->text == ','
|
|
|
|
||mTokenizer[mIndex + 1]->text == ';'
|
|
|
|
||mTokenizer[mIndex + 1]->text == ':'
|
2022-11-05 18:58:15 +08:00
|
|
|
||mTokenizer[mIndex + 1]->text == '{'
|
2022-11-11 21:20:57 +08:00
|
|
|
|| mTokenizer[mIndex + 1]->text == '=') {
|
2023-02-06 14:04:38 +08:00
|
|
|
handleVar(sType+" "+sName,isExtern,isStatic);
|
|
|
|
return;
|
|
|
|
} else if ( mTokenizer[mIndex + 1]->text == "::") {
|
2023-02-06 19:37:01 +08:00
|
|
|
sName = sName + mTokenizer[mIndex]->text+ "::";
|
2023-02-06 14:04:38 +08:00
|
|
|
mIndex+=2;
|
2023-03-24 17:40:59 +08:00
|
|
|
} else if (mTokenizer[mIndex]->text == "~") {
|
|
|
|
sName = sName + "~";
|
|
|
|
mIndex++;
|
2023-02-06 14:04:38 +08:00
|
|
|
} else {
|
2023-02-05 20:36:10 +08:00
|
|
|
QString s = mTokenizer[mIndex]->text;
|
2023-02-06 19:37:01 +08:00
|
|
|
if (sName.endsWith("::")) {
|
|
|
|
sName+=s;
|
|
|
|
} else {
|
|
|
|
if (!sName.isEmpty()) {
|
|
|
|
sType = sType+" "+sName;
|
|
|
|
sName = "";
|
|
|
|
}
|
2023-02-06 14:04:38 +08:00
|
|
|
if (s == "static")
|
|
|
|
isStatic = true;
|
|
|
|
else if (s == "friend")
|
|
|
|
isFriend = true;
|
|
|
|
else if (s == "extern")
|
|
|
|
isExtern = true;
|
2023-08-13 14:46:53 +08:00
|
|
|
if (!s.isEmpty() && !(s=="extern")) {
|
2023-02-06 14:04:38 +08:00
|
|
|
sType = sType + ' '+ s;
|
2023-08-13 14:46:53 +08:00
|
|
|
}
|
2023-02-05 20:36:10 +08:00
|
|
|
}
|
2022-11-04 20:27:35 +08:00
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement CppParser::doFindTypeDefinitionOf(const QString &fileName, const QString &aType, const PStatement ¤tClass) const
|
|
|
|
{
|
2023-03-11 19:30:56 +08:00
|
|
|
if (aType.isEmpty())
|
|
|
|
return PStatement();
|
2023-03-11 17:32:57 +08:00
|
|
|
// Remove pointer stuff from type
|
|
|
|
QString s = aType; // 'Type' is a keyword
|
|
|
|
int position = s.length()-1;
|
|
|
|
while ((position >= 0) && (s[position] == '*'
|
|
|
|
|| s[position] == ' '
|
|
|
|
|| s[position] == '&'))
|
|
|
|
position--;
|
|
|
|
if (position != s.length()-1)
|
|
|
|
s.truncate(position+1);
|
|
|
|
|
|
|
|
// Strip template stuff
|
|
|
|
position = s.indexOf('<');
|
|
|
|
if (position >= 0) {
|
|
|
|
int endPos = getBracketEnd(s,position);
|
|
|
|
s.remove(position,endPos-position+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use last word only (strip 'const', 'static', etc)
|
|
|
|
position = s.lastIndexOf(' ');
|
|
|
|
if (position >= 0)
|
|
|
|
s = s.mid(position+1);
|
|
|
|
|
|
|
|
PStatement scopeStatement = currentClass;
|
|
|
|
|
|
|
|
PStatement statement = doFindStatementOf(fileName,s,currentClass);
|
|
|
|
return getTypeDef(statement,fileName,aType);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CppParser::doFindFirstTemplateParamOf(const QString &fileName, const QString &phrase, const PStatement ¤tScope) const
|
2022-11-29 16:48:40 +08:00
|
|
|
{
|
|
|
|
return doFindTemplateParamOf(fileName,phrase,0,currentScope);
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
QString CppParser::doFindTemplateParamOf(const QString &fileName, const QString &phrase, int index, const PStatement ¤tScope) const
|
2022-11-27 13:32:14 +08:00
|
|
|
{
|
2023-03-11 19:30:56 +08:00
|
|
|
if (phrase.isEmpty())
|
|
|
|
return QString();
|
2022-11-27 13:32:14 +08:00
|
|
|
// Remove pointer stuff from type
|
|
|
|
QString s = phrase; // 'Type' is a keyword
|
|
|
|
int i = s.indexOf('<');
|
|
|
|
if (i>=0) {
|
2022-11-29 16:48:40 +08:00
|
|
|
i=getTemplateParamStart(s,i,index);
|
|
|
|
int t=getTemplateParamEnd(s,i);
|
2022-11-29 18:30:00 +08:00
|
|
|
//qDebug()<<index<<s<<s.mid(i,t-i)<<i<<t;
|
2023-07-14 17:44:14 +08:00
|
|
|
return s.mid(i,t-i).replace(QRegularExpression("\\s+"),"");
|
2022-11-27 13:32:14 +08:00
|
|
|
}
|
|
|
|
int position = s.length()-1;
|
|
|
|
while ((position >= 0) && (s[position] == '*'
|
|
|
|
|| s[position] == ' '
|
|
|
|
|| s[position] == '&'))
|
|
|
|
position--;
|
|
|
|
if (position != s.length()-1)
|
|
|
|
s.truncate(position+1);
|
|
|
|
|
|
|
|
PStatement scopeStatement = currentScope;
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement statement = doFindStatementOf(fileName,s,currentScope);
|
2022-11-30 09:54:23 +08:00
|
|
|
return getTemplateParam(statement,fileName, phrase,index, currentScope);
|
2022-11-27 13:32:14 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
int CppParser::getCurrentBlockEndSkip() const
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
|
|
|
if (mBlockEndSkips.isEmpty())
|
|
|
|
return mTokenizer.tokenCount()+1;
|
|
|
|
return mBlockEndSkips.back();
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
int CppParser::getCurrentBlockBeginSkip() const
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
|
|
|
if (mBlockBeginSkips.isEmpty())
|
|
|
|
return mTokenizer.tokenCount()+1;
|
|
|
|
return mBlockBeginSkips.back();
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
int CppParser::getCurrentInlineNamespaceEndSkip() const
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
|
|
|
if (mInlineNamespaceEndSkips.isEmpty())
|
|
|
|
return mTokenizer.tokenCount()+1;
|
|
|
|
return mInlineNamespaceEndSkips.back();
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement CppParser::getCurrentScope() const
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
|
|
|
if (mCurrentScope.isEmpty()) {
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
return mCurrentScope.back();
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
void CppParser::getFullNamespace(const QString &phrase, QString &sNamespace, QString &member) const
|
2021-08-17 23:30:14 +08:00
|
|
|
{
|
|
|
|
sNamespace = "";
|
|
|
|
member = phrase;
|
|
|
|
int strLen = phrase.length();
|
|
|
|
if (strLen==0)
|
|
|
|
return;
|
|
|
|
int lastI =-1;
|
|
|
|
int i=0;
|
|
|
|
while (i<strLen) {
|
|
|
|
if ((i+1<strLen) && (phrase[i]==':') && (phrase[i+1]==':') ) {
|
|
|
|
if (!mNamespaces.contains(sNamespace)) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
lastI = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sNamespace += phrase[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i>=strLen) {
|
|
|
|
if (mNamespaces.contains(sNamespace)) {
|
|
|
|
sNamespace = phrase;
|
|
|
|
member = "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lastI >= 0) {
|
|
|
|
sNamespace = phrase.mid(0,lastI);
|
|
|
|
member = phrase.mid(lastI+2);
|
|
|
|
} else {
|
|
|
|
sNamespace = "";
|
|
|
|
member = phrase;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
QString CppParser::getFullStatementName(const QString &command, const PStatement& parent) const
|
2021-08-17 23:30:14 +08:00
|
|
|
{
|
|
|
|
PStatement scopeStatement=parent;
|
|
|
|
while (scopeStatement && !isNamedScope(scopeStatement->kind))
|
|
|
|
scopeStatement = scopeStatement->parentScope.lock();
|
|
|
|
if (scopeStatement)
|
|
|
|
return scopeStatement->fullName + "::" + command;
|
|
|
|
else
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::getIncompleteClass(const QString &command, const PStatement& parentScope)
|
2021-08-17 23:30:14 +08:00
|
|
|
{
|
|
|
|
QString s=command;
|
|
|
|
//remove template parameter
|
|
|
|
int p = s.indexOf('<');
|
|
|
|
if (p>=0) {
|
|
|
|
s.truncate(p);
|
|
|
|
}
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement result = doFindStatementOf(mCurrentFile,s,parentScope);
|
2021-08-17 23:30:14 +08:00
|
|
|
if (result && result->kind!=StatementKind::skClass)
|
|
|
|
return PStatement();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
StatementScope CppParser::getScope()
|
|
|
|
{
|
|
|
|
// Don't blindly trust levels. Namespaces and externs can have levels too
|
|
|
|
PStatement currentScope = getCurrentScope();
|
|
|
|
|
|
|
|
// Invalid class or namespace/extern
|
|
|
|
if (!currentScope || (currentScope->kind == StatementKind::skNamespace))
|
2022-11-01 09:02:17 +08:00
|
|
|
return StatementScope::Global;
|
2021-08-17 23:30:14 +08:00
|
|
|
else if (currentScope->kind == StatementKind::skClass)
|
2022-11-01 09:02:17 +08:00
|
|
|
return StatementScope::ClassLocal;
|
2021-08-17 23:30:14 +08:00
|
|
|
else
|
2022-11-01 09:02:17 +08:00
|
|
|
return StatementScope::Local;
|
2021-08-17 23:30:14 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
QString CppParser::getStatementKey(const QString &sName, const QString &sType, const QString &sNoNameArgs) const
|
2021-08-17 23:30:14 +08:00
|
|
|
{
|
|
|
|
return sName + "--" + sType + "--" + sNoNameArgs;
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::getTypeDef(const PStatement& statement,
|
2023-03-11 17:32:57 +08:00
|
|
|
const QString& fileName, const QString& aType) const
|
2021-08-22 21:23:58 +08:00
|
|
|
{
|
|
|
|
if (!statement) {
|
|
|
|
return PStatement();
|
|
|
|
}
|
2021-12-07 08:23:27 +08:00
|
|
|
if (statement->kind == StatementKind::skClass
|
|
|
|
|| statement->kind == StatementKind::skEnumType
|
|
|
|
|| statement->kind == StatementKind::skEnumClassType) {
|
2021-08-22 21:23:58 +08:00
|
|
|
return statement;
|
2022-03-16 16:36:25 +08:00
|
|
|
} else if (statement->kind == StatementKind::skTypedef) {
|
2021-08-22 21:23:58 +08:00
|
|
|
if (statement->type == aType) // prevent infinite loop
|
|
|
|
return statement;
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement result = doFindTypeDefinitionOf(fileName,statement->type, statement->parentScope.lock());
|
2021-08-22 21:23:58 +08:00
|
|
|
if (!result) // found end of typedef trail, return result
|
|
|
|
return statement;
|
2021-08-22 23:48:00 +08:00
|
|
|
return result;
|
2022-03-16 16:36:25 +08:00
|
|
|
} else if (statement->kind == StatementKind::skAlias) {
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement result = doFindAliasedStatement(statement);
|
2022-03-16 16:36:25 +08:00
|
|
|
if (!result) // found end of typedef trail, return result
|
|
|
|
return statement;
|
|
|
|
return result;
|
2021-08-22 21:23:58 +08:00
|
|
|
} else
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
|
2021-08-17 23:30:14 +08:00
|
|
|
void CppParser::handleCatchBlock()
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2021-08-17 23:30:14 +08:00
|
|
|
int startLine= mTokenizer[mIndex]->line;
|
|
|
|
mIndex++; // skip for/catch;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (!((mIndex < tokenCount) && (mTokenizer[mIndex]->text.startsWith('('))))
|
2021-08-18 05:34:04 +08:00
|
|
|
return;
|
|
|
|
//skip params
|
2022-11-01 09:02:17 +08:00
|
|
|
int i2=mTokenizer[mIndex]->matchIndex+1;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (i2>=tokenCount)
|
2021-08-18 05:34:04 +08:00
|
|
|
return;
|
|
|
|
if (mTokenizer[i2]->text.startsWith('{')) {
|
|
|
|
mBlockBeginSkips.append(i2);
|
2022-11-01 09:02:17 +08:00
|
|
|
int i = indexOfMatchingBrace(i2);
|
|
|
|
mBlockEndSkips.append(i);
|
2021-08-18 05:34:04 +08:00
|
|
|
} else {
|
2022-11-01 09:02:17 +08:00
|
|
|
int i=indexOfNextSemicolon(i2);
|
2021-08-18 05:34:04 +08:00
|
|
|
mBlockEndSkips.append(i);
|
|
|
|
}
|
|
|
|
// add a block
|
|
|
|
PStatement block = addStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
startLine,
|
|
|
|
StatementKind::skBlock,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2022-11-02 10:42:55 +08:00
|
|
|
addSoloScopeLevel(block,startLine);
|
2022-11-05 08:42:54 +08:00
|
|
|
scanMethodArgs(block,mIndex);
|
2022-11-01 09:02:17 +08:00
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex+1;
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 22:10:54 +08:00
|
|
|
void CppParser::handleEnum(bool isTypedef)
|
2021-08-18 05:34:04 +08:00
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2021-08-18 05:34:04 +08:00
|
|
|
QString enumName = "";
|
|
|
|
bool isEnumClass = false;
|
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
mIndex++; //skip 'enum'
|
2021-08-27 00:49:50 +08:00
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex < tokenCount && mTokenizer[mIndex]->text == "class") {
|
2021-08-27 00:49:50 +08:00
|
|
|
//enum class
|
|
|
|
isEnumClass = true;
|
|
|
|
mIndex++; //skip class
|
|
|
|
}
|
2022-11-01 22:10:54 +08:00
|
|
|
bool isAdhocVar=false;
|
2023-08-07 14:23:57 +08:00
|
|
|
bool isNonameEnum=false;
|
2022-11-01 22:10:54 +08:00
|
|
|
int endIndex=-1;
|
2023-03-12 23:45:03 +08:00
|
|
|
if ((mIndex< tokenCount) && mTokenizer[mIndex]->text.startsWith('{')) { // enum {...} NAME
|
2021-08-18 05:34:04 +08:00
|
|
|
// Skip to the closing brace
|
2022-11-01 09:02:17 +08:00
|
|
|
int i = indexOfMatchingBrace(mIndex);
|
2021-08-18 05:34:04 +08:00
|
|
|
// Have we found the name?
|
2023-03-12 23:45:03 +08:00
|
|
|
if (i + 1 < tokenCount) {
|
2022-11-01 22:10:54 +08:00
|
|
|
enumName = mTokenizer[i + 1]->text.trimmed();
|
|
|
|
if (!isIdentifierOrPointer(enumName)) {
|
2023-08-07 14:23:57 +08:00
|
|
|
if (isTypedef || isEnumClass) {
|
|
|
|
//not a valid enum, skip to j
|
|
|
|
mIndex=indexOfNextSemicolon(i+1)+1;
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
isNonameEnum = true;
|
2022-11-01 22:10:54 +08:00
|
|
|
}
|
|
|
|
if (!isTypedef) {
|
|
|
|
//it's an ad-hoc enum var define;
|
|
|
|
if (isEnumClass) {
|
|
|
|
//Enum class can't add hoc, just skip to ;
|
|
|
|
mIndex=indexOfNextSemicolon(i+1)+1;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-13 22:19:56 +08:00
|
|
|
enumName = "___enum___"+enumName+"__";
|
2022-11-01 22:10:54 +08:00
|
|
|
isAdhocVar=true;
|
|
|
|
}
|
2021-08-27 00:49:50 +08:00
|
|
|
}
|
2022-11-01 22:10:54 +08:00
|
|
|
endIndex=i+1;
|
2023-03-12 23:45:03 +08:00
|
|
|
} else if (mIndex+1< tokenCount && mTokenizer[mIndex+1]->text.startsWith('{')){ // enum NAME {...};
|
2022-11-01 22:10:54 +08:00
|
|
|
enumName = mTokenizer[mIndex]->text;
|
2023-08-07 14:23:57 +08:00
|
|
|
mIndex++;
|
|
|
|
} else if (mIndex+1< tokenCount && mTokenizer[mIndex+1]->text.startsWith(':')){ // enum NAME:int {...};
|
|
|
|
enumName = mTokenizer[mIndex]->text;
|
|
|
|
//skip :
|
|
|
|
mIndex = indexOfNextLeftBrace(mIndex);
|
|
|
|
if (mIndex>tokenCount)
|
|
|
|
return;
|
2021-11-01 23:29:03 +08:00
|
|
|
} else {
|
|
|
|
// enum NAME blahblah
|
|
|
|
// it's an old c-style enum variable definition
|
|
|
|
return;
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add statement for enum name too
|
|
|
|
PStatement enumStatement;
|
2023-08-07 14:23:57 +08:00
|
|
|
if (!isNonameEnum) {
|
|
|
|
if (isEnumClass) {
|
|
|
|
enumStatement=addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"enum class",
|
|
|
|
enumName,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
startLine,
|
|
|
|
StatementKind::skEnumClassType,
|
|
|
|
getScope(),
|
|
|
|
mCurrentMemberAccessibility,
|
|
|
|
StatementProperty::spHasDefinition);
|
|
|
|
} else {
|
|
|
|
enumStatement=addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"enum",
|
|
|
|
enumName,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
startLine,
|
|
|
|
StatementKind::skEnumType,
|
|
|
|
getScope(),
|
|
|
|
mCurrentMemberAccessibility,
|
|
|
|
isAdhocVar?(StatementProperty::spHasDefinition|StatementProperty::spDummyStatement)
|
|
|
|
:StatementProperty::spHasDefinition );
|
|
|
|
}
|
2022-11-01 22:10:54 +08:00
|
|
|
}
|
|
|
|
if (isAdhocVar) {
|
|
|
|
//Ad-hoc var definition
|
|
|
|
// Skip to the closing brace
|
|
|
|
int i = indexOfMatchingBrace(mIndex)+1;
|
|
|
|
QString typeSuffix="";
|
2023-03-12 23:45:03 +08:00
|
|
|
while (i<tokenCount) {
|
2022-11-01 22:10:54 +08:00
|
|
|
QString name=mTokenizer[i]->text;
|
|
|
|
if (isIdentifierOrPointer(name)) {
|
|
|
|
QString suffix;
|
|
|
|
QString args;
|
|
|
|
parseCommandTypeAndArgs(name,suffix,args);
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
enumName+suffix,
|
|
|
|
mTokenizer[i]->text,
|
|
|
|
args,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[i]->line,
|
|
|
|
StatementKind::skVariable,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2022-11-01 22:10:54 +08:00
|
|
|
}
|
|
|
|
} else if (name!=',') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
endIndex=indexOfNextSemicolon(i);
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 22:10:54 +08:00
|
|
|
|
2021-08-18 05:34:04 +08:00
|
|
|
// Skip opening brace
|
|
|
|
mIndex++;
|
|
|
|
|
|
|
|
// Call every member "enum NAME ITEMNAME"
|
2023-08-07 14:23:57 +08:00
|
|
|
QString lastType;
|
|
|
|
if (enumStatement && !isAdhocVar)
|
|
|
|
lastType = "enum " + enumName;
|
2021-08-18 05:34:04 +08:00
|
|
|
QString cmd;
|
|
|
|
QString args;
|
2023-08-07 14:23:57 +08:00
|
|
|
int value=0;
|
|
|
|
bool canCalcValue=true;
|
|
|
|
while ((mIndex < tokenCount) &&
|
|
|
|
mTokenizer[mIndex]->text!='}') {
|
|
|
|
if (tokenIsIdentifier(mTokenizer[mIndex]->text)) {
|
|
|
|
cmd = mTokenizer[mIndex]->text;
|
|
|
|
args = "";
|
|
|
|
if (mIndex+1<tokenCount &&
|
|
|
|
mTokenizer[mIndex+1]->text=="=") {
|
|
|
|
mIndex+=2;
|
|
|
|
if (mIndex<tokenCount) {
|
|
|
|
int endIndex = indexOfNextPeriodOrSemicolon(mIndex);
|
2023-08-07 14:42:42 +08:00
|
|
|
value = evaluateConstExpr(endIndex,canCalcValue);
|
2023-08-07 14:23:57 +08:00
|
|
|
mIndex = endIndex - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isEnumClass) {
|
|
|
|
if (enumStatement) {
|
2021-08-18 05:34:04 +08:00
|
|
|
addStatement(
|
2023-08-07 14:23:57 +08:00
|
|
|
enumStatement,
|
|
|
|
mCurrentFile,
|
|
|
|
lastType,
|
|
|
|
cmd,
|
|
|
|
args,
|
|
|
|
"",
|
|
|
|
canCalcValue?QString("%1").arg(value):"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skEnum,
|
|
|
|
getScope(),
|
|
|
|
mCurrentMemberAccessibility,
|
|
|
|
StatementProperty::spHasDefinition);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QString strValue=canCalcValue?QString("%1").arg(value):"";
|
|
|
|
if (enumStatement) {
|
|
|
|
addStatement(
|
|
|
|
enumStatement,
|
|
|
|
mCurrentFile,
|
|
|
|
lastType,
|
|
|
|
cmd,
|
|
|
|
args,
|
|
|
|
"",
|
|
|
|
strValue,
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skEnum,
|
|
|
|
getScope(),
|
|
|
|
mCurrentMemberAccessibility,
|
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
2023-08-07 14:23:57 +08:00
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
lastType,
|
|
|
|
cmd,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
strValue,
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skEnum,
|
|
|
|
getScope(),
|
|
|
|
mCurrentMemberAccessibility,
|
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
2023-08-07 14:23:57 +08:00
|
|
|
value++;
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
2023-08-07 14:23:57 +08:00
|
|
|
mIndex ++ ;
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
2022-11-01 22:10:54 +08:00
|
|
|
if (mIndex<endIndex)
|
|
|
|
mIndex=endIndex;
|
|
|
|
mIndex = indexOfNextSemicolon(mIndex)+1;
|
2021-08-17 23:30:14 +08:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:42:32 +08:00
|
|
|
void CppParser::handleForBlock()
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
|
|
|
|
2021-08-18 13:42:32 +08:00
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
mIndex++; // skip for/catch;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex >= tokenCount)
|
2021-08-18 13:42:32 +08:00
|
|
|
return;
|
2023-05-14 15:57:07 +08:00
|
|
|
if (mTokenizer[mIndex]->text!="(")
|
|
|
|
return;
|
2022-11-01 09:02:17 +08:00
|
|
|
int i=indexOfNextSemicolon(mIndex);
|
2021-08-18 13:42:32 +08:00
|
|
|
int i2 = i+1; //skip over ';' (tokenizer have change for(;;) to for(;)
|
2023-05-14 15:57:07 +08:00
|
|
|
|
|
|
|
// for(int x:vec)
|
|
|
|
if (i2 > mTokenizer[mIndex]->matchIndex)
|
|
|
|
i2 = mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
if (i2>=tokenCount)
|
2021-08-18 13:42:32 +08:00
|
|
|
return;
|
|
|
|
if (mTokenizer[i2]->text.startsWith('{')) {
|
|
|
|
mBlockBeginSkips.append(i2);
|
2022-11-01 09:02:17 +08:00
|
|
|
i=indexOfMatchingBrace(i2);
|
|
|
|
// tokenizer will handle unbalanced braces, no need check here
|
|
|
|
// if (i==i2)
|
|
|
|
// mBlockEndSkips.append(mTokenizer.tokenCount());
|
|
|
|
// else
|
|
|
|
mBlockEndSkips.append(i);
|
2021-08-18 13:42:32 +08:00
|
|
|
} else {
|
2022-11-01 09:02:17 +08:00
|
|
|
i=indexOfNextSemicolon(i2);
|
2021-08-18 13:42:32 +08:00
|
|
|
mBlockEndSkips.append(i);
|
|
|
|
}
|
|
|
|
// add a block
|
|
|
|
PStatement block = addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-18 13:42:32 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skBlock,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-18 13:42:32 +08:00
|
|
|
|
|
|
|
addSoloScopeLevel(block,startLine);
|
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
void CppParser::handleKeyword(KeywordType skipType)
|
2021-08-18 13:42:32 +08:00
|
|
|
{
|
|
|
|
// Skip
|
|
|
|
switch (skipType) {
|
2022-11-01 09:02:17 +08:00
|
|
|
case KeywordType::SkipItself:
|
2021-08-18 13:42:32 +08:00
|
|
|
// skip it;
|
|
|
|
mIndex++;
|
|
|
|
break;
|
2022-11-04 20:27:35 +08:00
|
|
|
case KeywordType::SkipNextSemicolon:
|
2022-11-01 09:02:17 +08:00
|
|
|
// Skip to ; and over it
|
2022-11-04 20:27:35 +08:00
|
|
|
skipNextSemicolon(mIndex);
|
2021-08-18 13:42:32 +08:00
|
|
|
break;
|
2022-11-04 20:27:35 +08:00
|
|
|
case KeywordType::SkipNextColon:
|
2022-11-01 09:02:17 +08:00
|
|
|
// Skip to : and over it
|
|
|
|
mIndex = indexOfNextColon(mIndex)+1;
|
2021-08-18 13:42:32 +08:00
|
|
|
break;
|
2022-11-04 20:27:35 +08:00
|
|
|
case KeywordType::SkipNextParenthesis:
|
2022-11-01 09:02:17 +08:00
|
|
|
// skip pass ()
|
2022-11-04 20:27:35 +08:00
|
|
|
skipParenthesis(mIndex);
|
2021-08-18 13:42:32 +08:00
|
|
|
break;
|
2022-11-04 20:27:35 +08:00
|
|
|
case KeywordType::MoveToLeftBrace:
|
2021-08-18 13:42:32 +08:00
|
|
|
// Skip to {
|
2022-11-01 09:02:17 +08:00
|
|
|
mIndex = indexOfNextLeftBrace(mIndex);
|
2021-08-18 13:42:32 +08:00
|
|
|
break;
|
2023-07-05 09:01:38 +08:00
|
|
|
// case KeywordType::MoveToRightBrace:
|
|
|
|
// // Skip pass {}
|
|
|
|
// mIndex = indexPassBraces(mIndex);
|
|
|
|
// break;
|
2021-08-18 17:02:57 +08:00
|
|
|
default:
|
|
|
|
break;
|
2021-08-18 13:42:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 18:58:15 +08:00
|
|
|
void CppParser::handleLambda(int index, int endIndex)
|
2022-11-02 22:48:25 +08:00
|
|
|
{
|
2022-11-04 23:44:11 +08:00
|
|
|
Q_ASSERT(mTokenizer[index]->text.startsWith('['));
|
|
|
|
int startLine=mTokenizer[index]->line;
|
2022-11-05 08:42:54 +08:00
|
|
|
int argStart=index+1;
|
2022-11-05 18:58:15 +08:00
|
|
|
if (mTokenizer[argStart]->text!='(')
|
|
|
|
return;
|
2022-11-02 22:48:25 +08:00
|
|
|
int argEnd= mTokenizer[argStart]->matchIndex;
|
|
|
|
//TODO: parse captures
|
|
|
|
int bodyStart=indexOfNextLeftBrace(argEnd+1);
|
2022-11-05 18:58:15 +08:00
|
|
|
if (bodyStart>=endIndex) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int bodyEnd = mTokenizer[bodyStart]->matchIndex;
|
2023-05-24 17:02:59 +08:00
|
|
|
if (bodyEnd>endIndex) {
|
2022-11-02 22:48:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
PStatement lambdaBlock = addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
startLine,
|
|
|
|
StatementKind::skBlock,
|
|
|
|
StatementScope::Local,
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility::None,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2022-11-05 08:42:54 +08:00
|
|
|
scanMethodArgs(lambdaBlock,argStart);
|
2022-11-05 18:58:15 +08:00
|
|
|
addSoloScopeLevel(lambdaBlock,mTokenizer[bodyStart]->line);
|
|
|
|
int i=bodyStart+1; // start after '{';
|
|
|
|
while (i+2<bodyEnd) {
|
|
|
|
if (tokenIsTypeOrNonKeyword(mTokenizer[i]->text)
|
|
|
|
&& !mTokenizer[i]->text.endsWith('.')
|
|
|
|
&& !mTokenizer[i]->text.endsWith("->")
|
|
|
|
&& (mTokenizer[i+1]->text.startsWith('*')
|
|
|
|
|| mTokenizer[i+1]->text.startsWith('&')
|
|
|
|
|| tokenIsTypeOrNonKeyword(mTokenizer[i+1]->text)))
|
|
|
|
{
|
|
|
|
QString sType;
|
|
|
|
QString sName;
|
|
|
|
while (i+1<bodyEnd) {
|
|
|
|
if (mTokenizer[i+1]->text==':'
|
|
|
|
|| mTokenizer[i+1]->text=='('
|
|
|
|
|| mTokenizer[i+1]->text=='='
|
|
|
|
|| mTokenizer[i+1]->text==';'
|
|
|
|
|| mTokenizer[i+1]->text==','
|
|
|
|
|| mTokenizer[i+1]->text=='{'
|
|
|
|
)
|
|
|
|
break;
|
|
|
|
else {
|
|
|
|
if (!sType.isEmpty())
|
|
|
|
sType+=' ';
|
|
|
|
sType+=mTokenizer[i]->text;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
QString tempType;
|
|
|
|
while (i<bodyEnd) {
|
|
|
|
// Skip bit identifiers,
|
|
|
|
// e.g.:
|
|
|
|
// handle
|
|
|
|
// unsigned short bAppReturnCode:8,reserved:6,fBusy:1,fAck:1
|
|
|
|
// as
|
|
|
|
// unsigned short bAppReturnCode,reserved,fBusy,fAck
|
|
|
|
if (mTokenizer[i]->text.front() == ':') {
|
2023-03-12 23:45:03 +08:00
|
|
|
while ( (i < bodyEnd)
|
2022-11-05 18:58:15 +08:00
|
|
|
&& !(
|
|
|
|
mTokenizer[i]->text==','
|
|
|
|
|| mTokenizer[i]->text==';'
|
|
|
|
|| mTokenizer[i]->text=='='
|
|
|
|
))
|
|
|
|
i++;
|
|
|
|
} else if (mTokenizer[i]->text==';') {
|
|
|
|
break;
|
2023-03-01 11:29:30 +08:00
|
|
|
} else if (isIdentChar(mTokenizer[i]->text[0])) {
|
2022-11-05 18:58:15 +08:00
|
|
|
QString cmd=mTokenizer[i]->text;
|
|
|
|
if (cmd=="const") {
|
|
|
|
tempType="const";
|
|
|
|
} else {
|
|
|
|
QString suffix;
|
|
|
|
QString args;
|
|
|
|
parseCommandTypeAndArgs(cmd,suffix,args);
|
|
|
|
if (!cmd.isEmpty()) {
|
|
|
|
addChildStatement(
|
|
|
|
lambdaBlock,
|
|
|
|
mCurrentFile,
|
|
|
|
(sType+' '+tempType+suffix).trimmed(),
|
|
|
|
cmd,
|
|
|
|
args,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skVariable,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition); // TODO: not supported to pass list
|
2022-11-05 18:58:15 +08:00
|
|
|
tempType="";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
} else if (mTokenizer[i]->text=='(') {
|
|
|
|
i=mTokenizer[i]->matchIndex+1;
|
2023-02-06 14:04:38 +08:00
|
|
|
} else if (mTokenizer[i]->text.endsWith('=')) {
|
2023-03-12 23:45:03 +08:00
|
|
|
i = skipAssignment(i, bodyEnd);
|
2022-11-05 18:58:15 +08:00
|
|
|
} else if (mTokenizer[i]->text=='{') {
|
|
|
|
tempType="";
|
|
|
|
i=mTokenizer[i]->matchIndex+1;
|
|
|
|
} else {
|
|
|
|
tempType="";
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 22:22:33 +08:00
|
|
|
i=moveToEndOfStatement(i, true, bodyEnd);
|
2022-11-05 18:58:15 +08:00
|
|
|
}
|
|
|
|
removeScopeLevel(mTokenizer[bodyEnd]->line);
|
2022-11-02 22:48:25 +08:00
|
|
|
}
|
|
|
|
|
2023-03-10 20:50:46 +08:00
|
|
|
void CppParser::handleOperatorOverloading(const QString &sType,
|
|
|
|
//const QString &prefix,
|
|
|
|
int operatorTokenIndex, bool isStatic)
|
2023-02-06 14:04:38 +08:00
|
|
|
{
|
|
|
|
//operatorTokenIndex is the token index of "operator"
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
|
|
|
|
2023-02-06 14:04:38 +08:00
|
|
|
int index=operatorTokenIndex+1;
|
|
|
|
QString op="";
|
2023-03-12 23:45:03 +08:00
|
|
|
if (index>=tokenCount) {
|
2023-02-06 14:04:38 +08:00
|
|
|
mIndex=index;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mTokenizer[index]->text=="(") {
|
|
|
|
op="()";
|
|
|
|
index=mTokenizer[index]->matchIndex+1;
|
|
|
|
} else if (mTokenizer[index]->text=="new"
|
|
|
|
|| mTokenizer[index]->text=="delete") {
|
|
|
|
op=mTokenizer[index]->text;
|
|
|
|
index++;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (index<tokenCount
|
2023-02-06 14:04:38 +08:00
|
|
|
&& mTokenizer[index]->text=="[]") {
|
|
|
|
op+="[]";
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
op=mTokenizer[index]->text;
|
|
|
|
index++;
|
2023-03-10 20:13:52 +08:00
|
|
|
while (index<mTokenizer.tokenCount()
|
|
|
|
&& mTokenizer[index]->text != "(")
|
|
|
|
index++;
|
2023-02-06 14:04:38 +08:00
|
|
|
}
|
2023-03-12 23:45:03 +08:00
|
|
|
while (index<tokenCount
|
2023-02-06 14:04:38 +08:00
|
|
|
&& mTokenizer[index]->text == ")")
|
|
|
|
index++;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (index>=tokenCount
|
2023-02-06 14:04:38 +08:00
|
|
|
|| mTokenizer[index]->text!="(") {
|
|
|
|
mIndex=index;
|
|
|
|
return;
|
|
|
|
}
|
2023-03-10 20:13:52 +08:00
|
|
|
Q_ASSERT(!op.isEmpty());
|
2023-03-10 20:50:46 +08:00
|
|
|
if (isIdentChar(op.front())) {
|
|
|
|
handleMethod(StatementKind::skFunction,
|
|
|
|
sType+" "+op,
|
|
|
|
"operator("+op+")",
|
|
|
|
index,
|
|
|
|
isStatic,
|
|
|
|
false,
|
|
|
|
true);
|
|
|
|
} else {
|
|
|
|
handleMethod(StatementKind::skFunction,
|
2023-02-06 14:04:38 +08:00
|
|
|
sType,
|
2023-03-10 20:50:46 +08:00
|
|
|
"operator"+op,
|
2023-02-06 14:04:38 +08:00
|
|
|
index,
|
|
|
|
isStatic,
|
2023-03-10 20:50:46 +08:00
|
|
|
false,
|
2023-03-10 20:13:52 +08:00
|
|
|
true);
|
2023-03-10 20:50:46 +08:00
|
|
|
}
|
2023-02-06 14:04:38 +08:00
|
|
|
}
|
|
|
|
|
2023-03-10 20:13:52 +08:00
|
|
|
void CppParser::handleMethod(StatementKind functionKind,const QString &sType, const QString &sName, int argStart, bool isStatic, bool isFriend,bool isOperatorOverload)
|
2021-08-18 17:02:57 +08:00
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2021-08-18 17:02:57 +08:00
|
|
|
bool isValid = true;
|
|
|
|
bool isDeclaration = false; // assume it's not a prototype
|
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
2022-11-04 20:27:35 +08:00
|
|
|
int argEnd = mTokenizer[argStart]->matchIndex;
|
2021-08-18 17:02:57 +08:00
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex >= tokenCount) // not finished define, just skip it;
|
2021-08-18 17:02:57 +08:00
|
|
|
return;
|
|
|
|
|
2022-11-04 20:27:35 +08:00
|
|
|
PStatement scopeStatement = getCurrentScope();
|
2022-11-02 22:48:25 +08:00
|
|
|
|
|
|
|
//find start of the function body;
|
|
|
|
bool foundColon=false;
|
2022-11-04 20:27:35 +08:00
|
|
|
mIndex=argEnd+1;
|
2023-03-12 23:45:03 +08:00
|
|
|
while ((mIndex < tokenCount) && !isblockChar(mTokenizer[mIndex]->text.front())) {
|
2022-11-02 22:48:25 +08:00
|
|
|
if (mTokenizer[mIndex]->text=='(') {
|
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
}else if (mTokenizer[mIndex]->text==':') {
|
|
|
|
foundColon=true;
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
if (foundColon) {
|
|
|
|
mIndex++;
|
2023-03-12 23:45:03 +08:00
|
|
|
while ((mIndex < tokenCount) && !isblockChar(mTokenizer[mIndex]->text.front())) {
|
2022-11-02 22:48:25 +08:00
|
|
|
if (isWordChar(mTokenizer[mIndex]->text[0])
|
|
|
|
&& mIndex+1<mTokenizer.tokenCount()
|
|
|
|
&& mTokenizer[mIndex+1]->text=='{') {
|
|
|
|
//skip parent {}intializer
|
|
|
|
mIndex=mTokenizer[mIndex+1]->matchIndex+1;
|
|
|
|
} else if (mTokenizer[mIndex]->text=='(') {
|
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
} else
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex>=tokenCount)
|
2022-11-07 21:44:12 +08:00
|
|
|
return;
|
|
|
|
|
2021-08-18 17:02:57 +08:00
|
|
|
// Check if this is a prototype
|
|
|
|
if (mTokenizer[mIndex]->text.startsWith(';')
|
|
|
|
|| mTokenizer[mIndex]->text.startsWith('}')) {// prototype
|
|
|
|
isDeclaration = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString scopelessName;
|
|
|
|
PStatement functionStatement;
|
2022-11-04 20:27:35 +08:00
|
|
|
if (isFriend && isDeclaration && scopeStatement) {
|
2021-08-18 17:02:57 +08:00
|
|
|
int delimPos = sName.indexOf("::");
|
|
|
|
if (delimPos >= 0) {
|
|
|
|
scopelessName = sName.mid(delimPos+2);
|
|
|
|
} else
|
|
|
|
scopelessName = sName;
|
|
|
|
//TODO : we should check namespace
|
2022-11-04 20:27:35 +08:00
|
|
|
scopeStatement->friends.insert(scopelessName);
|
2021-08-18 17:02:57 +08:00
|
|
|
} else if (isValid) {
|
|
|
|
// Use the class the function belongs to as the parent ID if the function is declared outside of the class body
|
|
|
|
QString scopelessName;
|
|
|
|
QString parentClassName;
|
2023-03-10 20:13:52 +08:00
|
|
|
if (!isOperatorOverload && splitLastMember(sName,scopelessName,parentClassName)) {
|
2021-08-18 17:02:57 +08:00
|
|
|
// Provide Bar instead of Foo::Bar
|
2022-11-04 20:27:35 +08:00
|
|
|
scopeStatement = getIncompleteClass(parentClassName,getCurrentScope());
|
2023-02-06 16:50:24 +08:00
|
|
|
|
|
|
|
//parent not found
|
|
|
|
if (!parentClassName.isEmpty() && !scopeStatement)
|
|
|
|
scopelessName=sName;
|
2021-08-18 17:02:57 +08:00
|
|
|
} else
|
|
|
|
scopelessName = sName;
|
|
|
|
|
2023-02-06 14:04:38 +08:00
|
|
|
// qDebug()<<sName<<scopelessName<<parentClassName;
|
|
|
|
// if (scopeStatement)
|
|
|
|
// qDebug()<<"--"<<scopeStatement->fullName;
|
2021-08-18 17:02:57 +08:00
|
|
|
// For function definitions, the parent class is given. Only use that as a parent
|
|
|
|
if (!isDeclaration) {
|
|
|
|
functionStatement=addStatement(
|
2022-11-04 20:27:35 +08:00
|
|
|
scopeStatement,
|
2021-08-18 17:02:57 +08:00
|
|
|
mCurrentFile,
|
|
|
|
sType,
|
|
|
|
scopelessName,
|
2022-10-31 19:37:24 +08:00
|
|
|
argStart,
|
|
|
|
argEnd,
|
2021-08-18 17:02:57 +08:00
|
|
|
"",
|
|
|
|
//mTokenizer[mIndex - 1]^.Line,
|
|
|
|
startLine,
|
|
|
|
functionKind,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition
|
2023-03-10 20:13:52 +08:00
|
|
|
| (isStatic?StatementProperty::spStatic:StatementProperty::spNone)
|
|
|
|
| (isOperatorOverload?StatementProperty::spOperatorOverloading:StatementProperty::spNone));
|
2022-11-05 08:42:54 +08:00
|
|
|
scanMethodArgs(functionStatement, argStart);
|
2021-08-18 17:02:57 +08:00
|
|
|
// add variable this to the class function
|
2022-11-04 20:27:35 +08:00
|
|
|
if (scopeStatement && scopeStatement->kind == StatementKind::skClass &&
|
2021-08-18 17:02:57 +08:00
|
|
|
!isStatic) {
|
|
|
|
//add this to non-static class member function
|
|
|
|
addStatement(
|
|
|
|
functionStatement,
|
|
|
|
mCurrentFile,
|
2022-11-04 20:27:35 +08:00
|
|
|
scopeStatement->command+"*",
|
2021-08-18 17:02:57 +08:00
|
|
|
"this",
|
|
|
|
"",
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-18 17:02:57 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skVariable,
|
2022-11-01 09:02:17 +08:00
|
|
|
StatementScope::Local,
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility::None,
|
2023-03-10 20:13:52 +08:00
|
|
|
StatementProperty::spHasDefinition
|
|
|
|
| (isOperatorOverload?StatementProperty::spOperatorOverloading:StatementProperty::spNone));
|
2021-08-18 17:02:57 +08:00
|
|
|
}
|
2022-11-04 20:27:35 +08:00
|
|
|
|
2021-11-17 17:18:02 +08:00
|
|
|
// add "__func__ variable"
|
|
|
|
addStatement(
|
|
|
|
functionStatement,
|
|
|
|
mCurrentFile,
|
|
|
|
"static const char ",
|
|
|
|
"__func__",
|
|
|
|
"[]",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-11-17 17:18:02 +08:00
|
|
|
"\""+scopelessName+"\"",
|
|
|
|
startLine+1,
|
|
|
|
StatementKind::skVariable,
|
2022-11-01 09:02:17 +08:00
|
|
|
StatementScope::Local,
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility::None,
|
2023-03-10 20:13:52 +08:00
|
|
|
StatementProperty::spHasDefinition
|
|
|
|
| (isOperatorOverload?StatementProperty::spOperatorOverloading:StatementProperty::spNone));
|
2022-11-04 20:27:35 +08:00
|
|
|
|
2021-08-18 17:02:57 +08:00
|
|
|
} else {
|
|
|
|
functionStatement = addStatement(
|
2022-11-04 20:27:35 +08:00
|
|
|
scopeStatement,
|
2021-08-18 17:02:57 +08:00
|
|
|
mCurrentFile,
|
|
|
|
sType,
|
|
|
|
scopelessName,
|
2022-10-31 19:37:24 +08:00
|
|
|
argStart,
|
|
|
|
argEnd,
|
2021-08-18 17:02:57 +08:00
|
|
|
"",
|
|
|
|
//mTokenizer[mIndex - 1]^.Line,
|
|
|
|
startLine,
|
|
|
|
functionKind,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2023-03-10 20:13:52 +08:00
|
|
|
(isStatic?StatementProperty::spStatic:StatementProperty::spNone)
|
|
|
|
| (isOperatorOverload?StatementProperty::spOperatorOverloading:StatementProperty::spNone));
|
2021-08-18 17:02:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
if ((mIndex < tokenCount) && mTokenizer[mIndex]->text.startsWith('{')) {
|
2021-08-18 17:02:57 +08:00
|
|
|
addSoloScopeLevel(functionStatement,startLine);
|
|
|
|
mIndex++; //skip '{'
|
2023-03-12 23:45:03 +08:00
|
|
|
} else if ((mIndex < tokenCount) && mTokenizer[mIndex]->text.startsWith(';')) {
|
2021-08-18 17:02:57 +08:00
|
|
|
addSoloScopeLevel(functionStatement,startLine);
|
|
|
|
if (mTokenizer[mIndex]->line != startLine)
|
|
|
|
removeScopeLevel(mTokenizer[mIndex]->line+1);
|
|
|
|
else
|
|
|
|
removeScopeLevel(startLine+1);
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
void CppParser::handleNamespace(KeywordType skipType)
|
2021-08-18 17:02:57 +08:00
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2021-08-18 17:02:57 +08:00
|
|
|
bool isInline=false;
|
2022-11-01 00:01:46 +08:00
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
if (skipType==KeywordType::Inline) {
|
2021-08-18 17:02:57 +08:00
|
|
|
isInline = true;
|
|
|
|
mIndex++; //skip 'inline'
|
|
|
|
}
|
|
|
|
|
|
|
|
mIndex++; //skip 'namespace'
|
|
|
|
|
2022-11-04 20:27:35 +08:00
|
|
|
// if (!tokenIsIdentifier(mTokenizer[mIndex]->text))
|
|
|
|
// //wrong namespace define, stop handling
|
|
|
|
// return;
|
2021-08-18 17:02:57 +08:00
|
|
|
QString command = mTokenizer[mIndex]->text;
|
2021-08-28 09:01:40 +08:00
|
|
|
|
|
|
|
QString fullName = getFullStatementName(command,getCurrentScope());
|
|
|
|
if (isInline) {
|
|
|
|
mInlineNamespaces.insert(fullName);
|
|
|
|
} else if (mInlineNamespaces.contains(fullName)) {
|
|
|
|
isInline = true;
|
|
|
|
}
|
|
|
|
// if (command.startsWith("__")) // hack for inline namespaces
|
|
|
|
// isInline = true;
|
2021-08-18 17:02:57 +08:00
|
|
|
mIndex++;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex>=tokenCount)
|
2021-08-18 17:02:57 +08:00
|
|
|
return;
|
|
|
|
QString aliasName;
|
2023-03-12 23:45:03 +08:00
|
|
|
if ((mIndex+2<tokenCount) && (mTokenizer[mIndex]->text == '=')) {
|
2021-08-18 17:02:57 +08:00
|
|
|
aliasName=mTokenizer[mIndex+1]->text;
|
|
|
|
//namespace alias
|
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
aliasName, // name of the alias namespace
|
|
|
|
command, // command
|
|
|
|
"", // args
|
2022-10-31 19:37:24 +08:00
|
|
|
"", // noname args
|
2021-08-18 17:02:57 +08:00
|
|
|
"", // values
|
|
|
|
//mTokenizer[mIndex]^.Line,
|
|
|
|
startLine,
|
|
|
|
StatementKind::skNamespaceAlias,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-18 17:02:57 +08:00
|
|
|
mIndex+=2; //skip ;
|
|
|
|
return;
|
|
|
|
} else if (isInline) {
|
|
|
|
//inline namespace , just skip it
|
|
|
|
// Skip to '{'
|
2023-03-12 23:45:03 +08:00
|
|
|
while ((mIndex<tokenCount) && (mTokenizer[mIndex]->text != '{'))
|
2021-08-18 17:02:57 +08:00
|
|
|
mIndex++;
|
2022-11-01 09:02:17 +08:00
|
|
|
int i =indexOfMatchingBrace(mIndex); //skip '}'
|
2021-08-18 17:02:57 +08:00
|
|
|
if (i==mIndex)
|
2023-03-12 23:45:03 +08:00
|
|
|
mInlineNamespaceEndSkips.append(tokenCount);
|
2021-08-18 17:02:57 +08:00
|
|
|
else
|
|
|
|
mInlineNamespaceEndSkips.append(i);
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex<tokenCount)
|
2021-08-18 17:02:57 +08:00
|
|
|
mIndex++; //skip '{'
|
|
|
|
} else {
|
|
|
|
PStatement namespaceStatement = addStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"", // type
|
|
|
|
command, // command
|
|
|
|
"", // args
|
|
|
|
"", // noname args
|
|
|
|
"", // values
|
|
|
|
startLine,
|
|
|
|
StatementKind::skNamespace,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-18 17:02:57 +08:00
|
|
|
|
2022-11-03 00:49:22 +08:00
|
|
|
// find next '{' or ';'
|
|
|
|
mIndex = indexOfNextSemicolonOrLeftBrace(mIndex);
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex<tokenCount && mTokenizer[mIndex]->text=='{')
|
2022-11-03 00:49:22 +08:00
|
|
|
addSoloScopeLevel(namespaceStatement,startLine);
|
|
|
|
//skip it
|
|
|
|
mIndex++;
|
2021-08-18 17:02:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::handleOtherTypedefs()
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2021-08-18 17:02:57 +08:00
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
// Skip typedef word
|
|
|
|
mIndex++;
|
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex>=tokenCount)
|
2021-08-18 17:02:57 +08:00
|
|
|
return;
|
|
|
|
|
2022-11-01 22:10:54 +08:00
|
|
|
if (mTokenizer[mIndex]->text == '('
|
|
|
|
|| mTokenizer[mIndex]->text == ','
|
|
|
|
|| mTokenizer[mIndex]->text == ';') { // error typedef
|
|
|
|
//skip over next ;
|
|
|
|
mIndex=indexOfNextSemicolon(mIndex)+1;
|
2021-08-18 17:02:57 +08:00
|
|
|
return;
|
|
|
|
}
|
2023-03-12 23:45:03 +08:00
|
|
|
if ((mIndex+1<tokenCount)
|
2021-08-27 08:52:20 +08:00
|
|
|
&& (mTokenizer[mIndex+1]->text == ';')) {
|
2022-11-01 22:10:54 +08:00
|
|
|
//no old type, not valid
|
2021-08-27 08:52:20 +08:00
|
|
|
mIndex+=2; //skip ;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-01 22:10:54 +08:00
|
|
|
QString oldType;
|
2023-06-20 09:57:57 +08:00
|
|
|
QString tempType;
|
2021-08-18 17:02:57 +08:00
|
|
|
// Walk up to first new word (before first comma or ;)
|
|
|
|
while(true) {
|
2023-02-06 20:32:29 +08:00
|
|
|
if (oldType.endsWith("::"))
|
|
|
|
oldType += mTokenizer[mIndex]->text;
|
|
|
|
else if (mTokenizer[mIndex]->text=="::")
|
|
|
|
oldType += "::";
|
2023-06-20 09:57:57 +08:00
|
|
|
else if (mTokenizer[mIndex]->text=="*"
|
|
|
|
|| mTokenizer[mIndex]->text=="&")
|
|
|
|
tempType += mTokenizer[mIndex]->text;
|
|
|
|
else {
|
|
|
|
oldType += tempType + ' ' + mTokenizer[mIndex]->text;
|
|
|
|
tempType="";
|
|
|
|
}
|
2021-08-18 17:02:57 +08:00
|
|
|
mIndex++;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex+1>=tokenCount) {
|
2022-11-01 22:10:54 +08:00
|
|
|
//not valid, just exit
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mTokenizer[mIndex]->text=='(') {
|
2021-08-18 21:57:42 +08:00
|
|
|
break;
|
2022-11-01 22:10:54 +08:00
|
|
|
}
|
2021-08-18 21:57:42 +08:00
|
|
|
if (mTokenizer[mIndex + 1]->text.front() == ','
|
2022-11-01 22:10:54 +08:00
|
|
|
|| mTokenizer[mIndex + 1]->text == ';')
|
2021-08-18 17:02:57 +08:00
|
|
|
break;
|
2022-11-01 22:10:54 +08:00
|
|
|
//typedef function pointer
|
|
|
|
|
2021-08-18 17:02:57 +08:00
|
|
|
}
|
|
|
|
oldType = oldType.trimmed();
|
2022-11-01 22:10:54 +08:00
|
|
|
if (oldType.isEmpty()) {
|
|
|
|
//skip over next ;
|
|
|
|
mIndex=indexOfNextSemicolon(mIndex)+1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString newType;
|
2023-03-12 23:45:03 +08:00
|
|
|
while(mIndex+1<tokenCount) {
|
2022-11-02 13:38:26 +08:00
|
|
|
if (mTokenizer[mIndex]->text == ',' ) {
|
2022-11-02 10:42:55 +08:00
|
|
|
mIndex++;
|
2022-11-02 13:38:26 +08:00
|
|
|
} else if (mTokenizer[mIndex]->text == ';' ) {
|
2022-11-02 10:42:55 +08:00
|
|
|
break;
|
|
|
|
} else if (mTokenizer[mIndex]->text == '(') {
|
2022-11-01 22:10:54 +08:00
|
|
|
int paramStart=mTokenizer[mIndex]->matchIndex+1;
|
2023-03-12 23:45:03 +08:00
|
|
|
if (paramStart>=tokenCount
|
2022-11-01 22:10:54 +08:00
|
|
|
|| mTokenizer[paramStart]->text!='(') {
|
|
|
|
//not valid function pointer (no args)
|
|
|
|
//skip over next ;
|
|
|
|
mIndex=indexOfNextSemicolon(paramStart)+1;
|
|
|
|
return;
|
|
|
|
}
|
2023-03-01 11:29:30 +08:00
|
|
|
QString newType = findFunctionPointerName(mIndex);
|
2022-11-02 13:38:26 +08:00
|
|
|
if (!newType.isEmpty()) {
|
2021-08-18 21:57:42 +08:00
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
2023-06-20 09:57:57 +08:00
|
|
|
oldType+tempType,
|
2021-08-18 21:57:42 +08:00
|
|
|
newType,
|
2022-11-01 22:10:54 +08:00
|
|
|
mergeArgs(paramStart,mTokenizer[paramStart]->matchIndex),
|
|
|
|
"",
|
2021-08-18 21:57:42 +08:00
|
|
|
"",
|
|
|
|
startLine,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2023-06-20 09:57:57 +08:00
|
|
|
tempType="";
|
2022-11-01 22:10:54 +08:00
|
|
|
}
|
2022-11-02 13:38:26 +08:00
|
|
|
mIndex = mTokenizer[paramStart]->matchIndex+1;
|
2022-11-01 22:10:54 +08:00
|
|
|
} else if (mTokenizer[mIndex+1]->text.front() ==','
|
|
|
|
|| mTokenizer[mIndex+1]->text.front() ==';') {
|
2021-08-18 21:57:42 +08:00
|
|
|
newType += mTokenizer[mIndex]->text;
|
2022-11-01 22:10:54 +08:00
|
|
|
QString suffix;
|
|
|
|
QString args;
|
|
|
|
parseCommandTypeAndArgs(newType,suffix,args);
|
|
|
|
|
2021-08-18 21:57:42 +08:00
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
2023-06-20 09:57:57 +08:00
|
|
|
oldType+tempType+suffix,
|
2021-08-18 21:57:42 +08:00
|
|
|
newType,
|
2022-11-01 22:10:54 +08:00
|
|
|
args,
|
2021-08-18 21:57:42 +08:00
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-18 21:57:42 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2023-06-20 09:57:57 +08:00
|
|
|
tempType="";
|
2021-08-18 21:57:42 +08:00
|
|
|
newType = "";
|
|
|
|
mIndex++;
|
2022-11-01 22:10:54 +08:00
|
|
|
} else {
|
|
|
|
newType += mTokenizer[mIndex]->text;
|
|
|
|
mIndex++;
|
2021-08-18 17:02:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:24:39 +08:00
|
|
|
// Step over semicolon (saves one HandleStatement loop)
|
2021-08-18 21:57:42 +08:00
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::handlePreprocessor()
|
|
|
|
{
|
2022-06-01 17:02:03 +08:00
|
|
|
QString text = mTokenizer[mIndex]->text.mid(1).trimmed();
|
|
|
|
if (text.startsWith("include")) { // start of new file
|
2021-08-18 21:57:42 +08:00
|
|
|
// format: #include fullfilename:line
|
|
|
|
// Strip keyword
|
2022-06-01 17:02:03 +08:00
|
|
|
QString s = text.mid(QString("include").length());
|
|
|
|
if (!s.startsWith(" ") && !s.startsWith("\t"))
|
|
|
|
goto handlePreprocessorEnd;
|
2021-08-18 21:57:42 +08:00
|
|
|
int delimPos = s.lastIndexOf(':');
|
|
|
|
if (delimPos>=0) {
|
2022-11-04 23:44:11 +08:00
|
|
|
// qDebug()<<mCurrentScope.size()<<mCurrentFile<<mTokenizer[mIndex]->line<<s.mid(0,delimPos).trimmed();
|
2022-06-01 17:02:03 +08:00
|
|
|
mCurrentFile = s.mid(0,delimPos).trimmed();
|
2021-08-18 21:57:42 +08:00
|
|
|
mIsSystemHeader = isSystemHeaderFile(mCurrentFile) || isProjectHeaderFile(mCurrentFile);
|
2022-10-23 15:22:26 +08:00
|
|
|
mIsProjectFile = mProjectFiles.contains(mCurrentFile);
|
|
|
|
mIsHeader = isHFile(mCurrentFile);
|
2021-08-18 21:57:42 +08:00
|
|
|
|
|
|
|
// Mention progress to user if we enter a NEW file
|
|
|
|
bool ok;
|
2021-08-29 10:14:07 +08:00
|
|
|
int line = s.midRef(delimPos+1).toInt(&ok);
|
2021-08-18 21:57:42 +08:00
|
|
|
if (line == 1) {
|
|
|
|
mFilesScannedCount++;
|
|
|
|
mFilesToScanCount++;
|
2021-08-27 16:38:55 +08:00
|
|
|
emit onProgress(mCurrentFile,mFilesToScanCount,mFilesScannedCount);
|
2021-08-18 21:57:42 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-22 19:33:20 +08:00
|
|
|
} else if (text.startsWith("define")) {
|
2021-08-18 21:57:42 +08:00
|
|
|
|
|
|
|
// format: #define A B, remove define keyword
|
2022-06-01 17:02:03 +08:00
|
|
|
QString s = text.mid(QString("define").length());
|
|
|
|
if (!s.startsWith(" ") && !s.startsWith("\t"))
|
|
|
|
goto handlePreprocessorEnd;
|
|
|
|
s = s.trimmed();
|
2021-08-18 21:57:42 +08:00
|
|
|
// Ask the preprocessor to cut parts up
|
|
|
|
QString name,args,value;
|
2022-10-22 10:44:10 +08:00
|
|
|
mPreprocessor.getDefineParts(s,name,args,value);
|
2021-08-18 21:57:42 +08:00
|
|
|
|
|
|
|
addStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
nullptr, // defines don't belong to any scope
|
|
|
|
mCurrentFile,
|
|
|
|
"", // define has no type
|
|
|
|
name,
|
|
|
|
args,
|
|
|
|
"",// noname args
|
|
|
|
value,
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skPreprocessor,
|
2022-11-01 09:02:17 +08:00
|
|
|
StatementScope::Global,
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility::None,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-18 21:57:42 +08:00
|
|
|
} // TODO: undef ( define has limited scope)
|
2022-06-01 17:02:03 +08:00
|
|
|
handlePreprocessorEnd:
|
2021-08-18 21:57:42 +08:00
|
|
|
mIndex++;
|
2021-08-18 17:02:57 +08:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility CppParser::getClassMemberAccessibility(const QString& text) const {
|
|
|
|
KeywordType type = mCppKeywords.value(text,KeywordType::None);
|
|
|
|
return getClassMemberAccessibility(type);
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility CppParser::getClassMemberAccessibility(KeywordType keywordType) const
|
2021-08-19 12:01:01 +08:00
|
|
|
{
|
2022-11-01 09:02:17 +08:00
|
|
|
switch(keywordType) {
|
|
|
|
case KeywordType::Public:
|
2023-03-12 18:24:58 +08:00
|
|
|
return StatementAccessibility::Public;
|
2022-11-01 09:02:17 +08:00
|
|
|
case KeywordType::Private:
|
2023-03-12 18:24:58 +08:00
|
|
|
return StatementAccessibility::Private;
|
2022-11-01 09:02:17 +08:00
|
|
|
case KeywordType::Protected:
|
2023-03-12 18:24:58 +08:00
|
|
|
return StatementAccessibility::Protected;
|
2022-11-01 09:02:17 +08:00
|
|
|
default:
|
2023-03-12 18:24:58 +08:00
|
|
|
return StatementAccessibility::None;
|
2022-11-01 09:02:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 18:24:58 +08:00
|
|
|
void CppParser::handleAccessibilitySpecifiers(KeywordType keywordType)
|
2022-11-01 09:02:17 +08:00
|
|
|
{
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility = getClassMemberAccessibility(keywordType);
|
|
|
|
mIndex++;
|
|
|
|
|
|
|
|
if (mIndex < mTokenizer.tokenCount()
|
|
|
|
&& mTokenizer[mIndex]->text == ':')
|
|
|
|
mIndex++; // skip ':'
|
2021-08-19 12:01:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::handleStatement()
|
|
|
|
{
|
2022-10-31 19:37:24 +08:00
|
|
|
QString funcType,funcName;
|
2021-08-19 12:01:01 +08:00
|
|
|
int idx=getCurrentBlockEndSkip();
|
|
|
|
int idx2=getCurrentBlockBeginSkip();
|
|
|
|
int idx3=getCurrentInlineNamespaceEndSkip();
|
2022-11-01 09:02:17 +08:00
|
|
|
KeywordType keywordType;
|
2022-11-11 21:20:57 +08:00
|
|
|
#ifdef QT_DEBUG
|
|
|
|
// qDebug()<<lastIndex<<mIndex;
|
2023-02-21 22:40:29 +08:00
|
|
|
Q_ASSERT(mIndex>=mLastIndex);
|
|
|
|
mLastIndex=mIndex;
|
2022-11-11 21:20:57 +08:00
|
|
|
#endif
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2022-11-02 13:38:26 +08:00
|
|
|
|
2021-08-19 12:01:01 +08:00
|
|
|
if (mIndex >= idx2) {
|
|
|
|
//skip (previous handled) block begin
|
|
|
|
mBlockBeginSkips.pop_back();
|
|
|
|
if (mIndex == idx2)
|
|
|
|
mIndex++;
|
2023-03-12 23:45:03 +08:00
|
|
|
else if (mIndex<tokenCount) //error happens, but we must remove an (error) added scope
|
2021-08-19 12:01:01 +08:00
|
|
|
removeScopeLevel(mTokenizer[mIndex]->line);
|
|
|
|
} else if (mIndex >= idx) {
|
|
|
|
//skip (previous handled) block end
|
|
|
|
mBlockEndSkips.pop_back();
|
2023-03-12 23:45:03 +08:00
|
|
|
if (idx+1 < tokenCount)
|
2021-08-19 12:01:01 +08:00
|
|
|
removeScopeLevel(mTokenizer[idx+1]->line);
|
|
|
|
if (mIndex == idx)
|
|
|
|
mIndex++;
|
|
|
|
} else if (mIndex >= idx3) {
|
|
|
|
//skip (previous handled) inline name space end
|
|
|
|
mInlineNamespaceEndSkips.pop_back();
|
|
|
|
if (mIndex == idx3)
|
|
|
|
mIndex++;
|
|
|
|
} else if (mTokenizer[mIndex]->text.startsWith('{')) {
|
|
|
|
PStatement block = addStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
//mTokenizer[mIndex]^.Line,
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skBlock,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2022-11-02 13:38:26 +08:00
|
|
|
addSoloScopeLevel(block,mTokenizer[mIndex]->line,true);
|
2021-08-19 12:01:01 +08:00
|
|
|
mIndex++;
|
2021-08-23 17:27:17 +08:00
|
|
|
} else if (mTokenizer[mIndex]->text[0] == '}') {
|
2021-08-19 12:01:01 +08:00
|
|
|
removeScopeLevel(mTokenizer[mIndex]->line);
|
|
|
|
mIndex++;
|
|
|
|
} else if (checkForPreprocessor()) {
|
|
|
|
handlePreprocessor();
|
2022-11-04 20:27:35 +08:00
|
|
|
// } else if (checkForLambda()) { // is lambda
|
|
|
|
// handleLambda();
|
|
|
|
} else if (mTokenizer[mIndex]->text=='(') {
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex+1<tokenCount &&
|
2023-02-06 14:04:38 +08:00
|
|
|
mTokenizer[mIndex+1]->text=="operator") {
|
2022-11-04 20:27:35 +08:00
|
|
|
// things like (operator int)
|
|
|
|
mIndex++; //just skip '('
|
|
|
|
} else
|
|
|
|
skipParenthesis(mIndex);
|
|
|
|
} else if (mTokenizer[mIndex]->text==')') {
|
2022-11-02 22:48:25 +08:00
|
|
|
mIndex++;
|
2022-11-04 20:27:35 +08:00
|
|
|
} else if (mTokenizer[mIndex]->text.startsWith('~')) {
|
|
|
|
//it should be a destructor
|
2023-03-24 17:40:59 +08:00
|
|
|
if (mIndex+2<tokenCount
|
|
|
|
&& isIdentChar(mTokenizer[mIndex+1]->text[0])
|
|
|
|
&& mTokenizer[mIndex+2]->text=='(') {
|
2022-11-04 20:27:35 +08:00
|
|
|
//dont further check to speed up
|
2023-03-24 17:40:59 +08:00
|
|
|
handleMethod(StatementKind::skDestructor, "", '~'+mTokenizer[mIndex+1]->text, mIndex+2, false, false);
|
2022-11-04 20:27:35 +08:00
|
|
|
} else {
|
2022-11-04 23:44:11 +08:00
|
|
|
//error
|
2022-11-09 22:22:33 +08:00
|
|
|
mIndex=moveToEndOfStatement(mIndex,false);
|
2022-11-04 20:27:35 +08:00
|
|
|
}
|
2023-02-06 14:04:38 +08:00
|
|
|
} else if (mTokenizer[mIndex]->text=="::") {
|
|
|
|
checkAndHandleMethodOrVar(KeywordType::None);
|
2022-11-04 20:27:35 +08:00
|
|
|
} else if (!isIdentChar(mTokenizer[mIndex]->text[0])) {
|
2022-11-09 22:22:33 +08:00
|
|
|
mIndex=moveToEndOfStatement(mIndex,true);
|
2022-11-01 09:02:17 +08:00
|
|
|
} else if (checkForKeyword(keywordType)) { // includes template now
|
|
|
|
handleKeyword(keywordType);
|
|
|
|
} else if (keywordType==KeywordType::For) { // (for/catch)
|
2021-08-19 12:01:01 +08:00
|
|
|
handleForBlock();
|
2022-11-01 09:02:17 +08:00
|
|
|
} else if (keywordType==KeywordType::Catch) { // (for/catch)
|
2021-08-19 12:01:01 +08:00
|
|
|
handleCatchBlock();
|
2023-03-12 18:24:58 +08:00
|
|
|
} else if (checkForAccessibilitySpecifiers(keywordType)) { // public /private/proteced
|
|
|
|
handleAccessibilitySpecifiers(keywordType);
|
2022-11-01 09:02:17 +08:00
|
|
|
} else if (keywordType==KeywordType::Enum) {
|
2022-11-01 22:10:54 +08:00
|
|
|
handleEnum(false);
|
2022-11-01 09:02:17 +08:00
|
|
|
} else if (keywordType==KeywordType::Typedef) {
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex+1 < tokenCount) {
|
2021-08-19 12:01:01 +08:00
|
|
|
if (checkForTypedefStruct()) { // typedef struct something
|
|
|
|
mIndex++; // skip 'typedef'
|
|
|
|
handleStructs(true);
|
|
|
|
} else if (checkForTypedefEnum()) { // typedef enum something
|
|
|
|
mIndex++; // skip 'typedef'
|
2022-11-01 22:10:54 +08:00
|
|
|
handleEnum(true);
|
2021-08-19 12:01:01 +08:00
|
|
|
} else
|
|
|
|
handleOtherTypedefs(); // typedef Foo Bar
|
|
|
|
} else
|
|
|
|
mIndex++;
|
2022-11-01 09:02:17 +08:00
|
|
|
} else if (checkForNamespace(keywordType)) {
|
|
|
|
handleNamespace(keywordType);
|
|
|
|
} else if (checkForUsing(keywordType)) {
|
2021-08-19 12:01:01 +08:00
|
|
|
handleUsing();
|
2022-11-01 09:02:17 +08:00
|
|
|
} else if (checkForStructs(keywordType)) {
|
2021-08-19 12:01:01 +08:00
|
|
|
handleStructs(false);
|
2022-11-11 21:20:57 +08:00
|
|
|
} else if (keywordType == KeywordType::Inline) {
|
|
|
|
mIndex++;
|
|
|
|
}else {
|
2022-11-04 20:27:35 +08:00
|
|
|
// it should be method/constructor/var
|
2022-11-04 23:44:11 +08:00
|
|
|
checkAndHandleMethodOrVar(keywordType);
|
2022-11-04 20:27:35 +08:00
|
|
|
}
|
2022-11-11 21:20:57 +08:00
|
|
|
//Q_ASSERT(mIndex<999999);
|
2022-11-04 23:44:11 +08:00
|
|
|
|
2022-11-05 08:42:54 +08:00
|
|
|
// while (mTokenizer.lambdasCount()>0 && mTokenizer.indexOfFirstLambda()<mIndex) {
|
|
|
|
// handleLambda(mTokenizer.indexOfFirstLambda());
|
|
|
|
// mTokenizer.removeFirstLambda();
|
|
|
|
// }
|
2022-11-04 20:27:35 +08:00
|
|
|
// else if (checkForMethod(funcType, funcName, argStart,argEnd, isStatic, isFriend)) {
|
|
|
|
// handleMethod(funcType, funcName, argStart, argEnd, isStatic, isFriend); // don't recalculate parts
|
|
|
|
// } else if (tryHandleVar()) {
|
|
|
|
// //do nothing
|
|
|
|
// } else
|
|
|
|
// mIndex++;
|
|
|
|
|
|
|
|
//todo: remove mSkipList (we can check '}''s statement type instead)
|
2022-11-05 08:42:54 +08:00
|
|
|
// checkForSkipStatement();
|
2021-08-19 12:01:01 +08:00
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
return mIndex < tokenCount;
|
2021-08-19 12:01:01 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::handleStructs(bool isTypedef)
|
2023-06-30 10:53:58 +08:00
|
|
|
{
|
2021-08-19 12:01:01 +08:00
|
|
|
bool isFriend = false;
|
|
|
|
QString prefix = mTokenizer[mIndex]->text;
|
|
|
|
if (prefix == "friend") {
|
|
|
|
isFriend = true;
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
// Check if were dealing with a struct or union
|
|
|
|
prefix = mTokenizer[mIndex]->text;
|
2023-03-12 19:17:39 +08:00
|
|
|
bool isStruct = ("class" != prefix); //struct/union
|
2021-08-19 12:01:01 +08:00
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2021-08-19 12:01:01 +08:00
|
|
|
|
|
|
|
mIndex++; //skip struct/class/union
|
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex>=tokenCount)
|
2021-08-19 12:01:01 +08:00
|
|
|
return;
|
|
|
|
|
2022-11-04 23:44:11 +08:00
|
|
|
// Do not modifiy index
|
|
|
|
int i=indexOfNextSemicolonOrLeftBrace(mIndex);
|
2023-03-12 23:45:03 +08:00
|
|
|
if (i >= tokenCount) {
|
2022-11-04 23:44:11 +08:00
|
|
|
//error
|
|
|
|
mIndex=i;
|
|
|
|
return;
|
|
|
|
}
|
2021-08-19 12:01:01 +08:00
|
|
|
// Forward class/struct decl *or* typedef, e.g. typedef struct some_struct synonym1, synonym2;
|
2023-03-12 19:17:39 +08:00
|
|
|
if (mTokenizer[i]->text == ";") {
|
2021-08-19 12:01:01 +08:00
|
|
|
// typdef struct Foo Bar
|
|
|
|
if (isTypedef) {
|
2023-06-30 09:29:20 +08:00
|
|
|
QString structTypeName = mTokenizer[mIndex]->text;
|
2023-06-29 21:31:30 +08:00
|
|
|
QString tempType = "";
|
2023-06-30 09:29:20 +08:00
|
|
|
mIndex++; // skip struct/class name
|
|
|
|
while(mIndex+1 < tokenCount) {
|
2021-08-19 12:01:01 +08:00
|
|
|
// Add definition statement for the synonym
|
2023-06-30 09:29:20 +08:00
|
|
|
if ( (mTokenizer[mIndex + 1]->text==","
|
2023-03-12 19:17:39 +08:00
|
|
|
|| mTokenizer[mIndex + 1]->text==";")) {
|
2021-08-19 12:01:01 +08:00
|
|
|
QString newType = mTokenizer[mIndex]->text;
|
2023-06-30 09:29:20 +08:00
|
|
|
QString suffix,tempArgs;
|
|
|
|
parseCommandTypeAndArgs(newType,suffix,tempArgs);
|
2021-08-19 12:01:01 +08:00
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
2023-06-30 09:29:20 +08:00
|
|
|
structTypeName + " "+ tempType + " " + suffix,
|
2021-08-19 12:01:01 +08:00
|
|
|
newType,
|
2023-06-30 09:29:20 +08:00
|
|
|
tempArgs, // args
|
2022-10-31 19:37:24 +08:00
|
|
|
"", // noname args
|
|
|
|
"", // values
|
2023-02-05 21:55:23 +08:00
|
|
|
mTokenizer[mIndex]->line,
|
2021-08-19 12:01:01 +08:00
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2023-06-29 21:31:30 +08:00
|
|
|
tempType="";
|
2023-06-30 09:29:20 +08:00
|
|
|
mIndex++; //skip , ;
|
|
|
|
if (mTokenizer[mIndex]->text.front() == ';')
|
|
|
|
break;
|
|
|
|
} else
|
2023-06-29 21:31:30 +08:00
|
|
|
tempType+= mTokenizer[mIndex]->text;
|
2021-08-19 12:01:01 +08:00
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isFriend) { // friend class
|
|
|
|
PStatement parentStatement = getCurrentScope();
|
|
|
|
if (parentStatement) {
|
|
|
|
parentStatement->friends.insert(mTokenizer[mIndex]->text);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// todo: Forward declaration, struct Foo. Don't mention in class browser
|
|
|
|
}
|
|
|
|
i++; // step over ;
|
|
|
|
mIndex = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normal class/struct decl
|
|
|
|
} else {
|
|
|
|
PStatement firstSynonym;
|
|
|
|
// Add class/struct name BEFORE opening brace
|
2023-03-10 20:13:52 +08:00
|
|
|
if (mTokenizer[mIndex]->text != "{") {
|
2023-03-12 23:45:03 +08:00
|
|
|
while(mIndex < tokenCount) {
|
2023-03-10 20:13:52 +08:00
|
|
|
if (mTokenizer[mIndex]->text == ":"
|
|
|
|
|| mTokenizer[mIndex]->text == "{"
|
|
|
|
|| mTokenizer[mIndex]->text == ";") {
|
2022-11-02 10:42:55 +08:00
|
|
|
break;
|
2023-03-12 23:45:03 +08:00
|
|
|
} else if ((mIndex + 1 < tokenCount)
|
2023-03-10 20:13:52 +08:00
|
|
|
&& (mTokenizer[mIndex + 1]->text == ","
|
|
|
|
|| mTokenizer[mIndex + 1]->text == ";"
|
|
|
|
|| mTokenizer[mIndex + 1]->text == "{"
|
|
|
|
|| mTokenizer[mIndex + 1]->text == ":")) {
|
2021-08-19 12:01:01 +08:00
|
|
|
QString command = mTokenizer[mIndex]->text;
|
2022-11-04 23:44:11 +08:00
|
|
|
|
|
|
|
PStatement scopeStatement=getCurrentScope();
|
|
|
|
QString scopelessName;
|
|
|
|
QString parentName;
|
2023-08-07 14:23:57 +08:00
|
|
|
|
2022-11-04 23:44:11 +08:00
|
|
|
if (splitLastMember(command,scopelessName,parentName)) {
|
|
|
|
scopeStatement = getIncompleteClass(parentName,getCurrentScope());
|
|
|
|
} else {
|
|
|
|
scopelessName=command;
|
|
|
|
}
|
2021-08-19 12:01:01 +08:00
|
|
|
if (!command.isEmpty()) {
|
|
|
|
firstSynonym = addStatement(
|
2022-11-04 23:44:11 +08:00
|
|
|
scopeStatement,
|
2021-08-19 12:01:01 +08:00
|
|
|
mCurrentFile,
|
|
|
|
prefix, // type
|
2022-11-04 23:44:11 +08:00
|
|
|
scopelessName, // command
|
2021-08-19 12:01:01 +08:00
|
|
|
"", // args
|
2022-10-31 19:37:24 +08:00
|
|
|
"", // no name args,
|
2021-08-19 12:01:01 +08:00
|
|
|
"", // values
|
2023-02-05 21:55:23 +08:00
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
//startLine,
|
2021-08-19 12:01:01 +08:00
|
|
|
StatementKind::skClass,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-19 12:01:01 +08:00
|
|
|
command = "";
|
|
|
|
}
|
|
|
|
mIndex++;
|
2022-11-04 20:27:35 +08:00
|
|
|
break;
|
2023-03-12 23:45:03 +08:00
|
|
|
} else if ((mIndex + 2 < tokenCount)
|
2021-08-19 12:01:01 +08:00
|
|
|
&& (mTokenizer[mIndex + 1]->text == "final")
|
2023-03-10 20:13:52 +08:00
|
|
|
&& (mTokenizer[mIndex + 2]->text==","
|
|
|
|
|| mTokenizer[mIndex + 2]->text==":"
|
2021-08-19 12:01:01 +08:00
|
|
|
|| isblockChar(mTokenizer[mIndex + 2]->text.front()))) {
|
|
|
|
QString command = mTokenizer[mIndex]->text;
|
|
|
|
if (!command.isEmpty()) {
|
|
|
|
firstSynonym = addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
prefix, // type
|
|
|
|
command, // command
|
|
|
|
"", // args
|
2022-10-31 19:37:24 +08:00
|
|
|
"", // no name args
|
2021-08-19 12:01:01 +08:00
|
|
|
"", // values
|
2023-02-05 21:55:23 +08:00
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
//startLine,
|
2021-08-19 12:01:01 +08:00
|
|
|
StatementKind::skClass,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-19 12:01:01 +08:00
|
|
|
command="";
|
|
|
|
}
|
|
|
|
mIndex+=2;
|
2022-11-04 20:27:35 +08:00
|
|
|
break;
|
2021-08-19 12:01:01 +08:00
|
|
|
} else
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk to opening brace if we encountered inheritance statements
|
2023-03-12 23:45:03 +08:00
|
|
|
if ((mIndex < tokenCount) && (mTokenizer[mIndex]->text == ":")) {
|
2021-08-19 12:01:01 +08:00
|
|
|
if (firstSynonym)
|
|
|
|
setInheritance(mIndex, firstSynonym, isStruct); // set the _InheritanceList value
|
2022-11-04 20:27:35 +08:00
|
|
|
mIndex=indexOfNextLeftBrace(mIndex);
|
2021-08-19 12:01:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for struct synonyms after close brace
|
|
|
|
if (isStruct) {
|
|
|
|
|
|
|
|
// Walk to closing brace
|
2022-11-01 09:02:17 +08:00
|
|
|
i = indexOfMatchingBrace(mIndex); // step onto closing brace
|
2021-08-19 12:01:01 +08:00
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
if ((i + 1 < tokenCount) && !(
|
2021-08-19 12:01:01 +08:00
|
|
|
mTokenizer[i + 1]->text.front() == ';'
|
|
|
|
|| mTokenizer[i + 1]->text.front() == '}')) {
|
|
|
|
// When encountering names again after struct body scanning, skip it
|
|
|
|
QString command = "";
|
|
|
|
QString args = "";
|
|
|
|
|
|
|
|
// Add synonym before opening brace
|
|
|
|
while(true) {
|
|
|
|
i++;
|
2022-11-01 22:10:54 +08:00
|
|
|
if (mTokenizer[i]->text=='('
|
|
|
|
|| mTokenizer[i]->text==')') {
|
|
|
|
//skip
|
|
|
|
} else if (!(mTokenizer[i]->text == '{'
|
|
|
|
|| mTokenizer[i]->text == ','
|
|
|
|
|| mTokenizer[i]->text == ';')) {
|
|
|
|
if (mTokenizer[i]->text.endsWith(']')) { // cut-off array brackets
|
|
|
|
int pos = mTokenizer[i]->text.indexOf('[');
|
|
|
|
command += mTokenizer[i]->text.mid(0,pos) + ' ';
|
|
|
|
args = mTokenizer[i]->text.mid(pos);
|
|
|
|
} else if (mTokenizer[i]->text.front() == '*'
|
|
|
|
|| mTokenizer[i]->text.front() == '&') { // do not add spaces after pointer operator
|
|
|
|
command += mTokenizer[i]->text;
|
|
|
|
} else {
|
|
|
|
command += mTokenizer[i]->text + ' ';
|
|
|
|
}
|
2021-08-19 12:01:01 +08:00
|
|
|
} else {
|
|
|
|
command = command.trimmed();
|
2023-06-20 09:57:57 +08:00
|
|
|
QString suffix,tempArgs;
|
|
|
|
parseCommandTypeAndArgs(command,suffix,tempArgs);
|
2021-08-19 12:01:01 +08:00
|
|
|
if (!command.isEmpty() &&
|
|
|
|
( !firstSynonym
|
|
|
|
|| command!=firstSynonym->command )) {
|
|
|
|
//not define the struct yet, we define a unamed struct
|
|
|
|
if (!firstSynonym) {
|
|
|
|
firstSynonym = addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
prefix,
|
2023-08-13 22:19:56 +08:00
|
|
|
"___dummy___"+command,
|
2021-08-19 12:01:01 +08:00
|
|
|
"",
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2023-02-05 21:55:23 +08:00
|
|
|
mTokenizer[i]->line,
|
|
|
|
//startLine,
|
2021-08-19 12:01:01 +08:00
|
|
|
StatementKind::skClass,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2023-08-07 14:23:57 +08:00
|
|
|
StatementProperty::spHasDefinition | StatementProperty::spDummyStatement);
|
2021-08-19 12:01:01 +08:00
|
|
|
}
|
|
|
|
if (isTypedef) {
|
|
|
|
//typedef
|
|
|
|
addStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
2023-06-20 09:57:57 +08:00
|
|
|
firstSynonym->command+suffix,
|
2022-10-31 19:37:24 +08:00
|
|
|
command,
|
2023-06-20 09:57:57 +08:00
|
|
|
args+tempArgs,
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition); // typedef
|
2021-08-19 12:01:01 +08:00
|
|
|
} else {
|
|
|
|
//variable define
|
2021-08-19 17:08:01 +08:00
|
|
|
addStatement(
|
2021-08-19 12:01:01 +08:00
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
2023-06-20 09:57:57 +08:00
|
|
|
firstSynonym->command+suffix,
|
2021-08-19 12:01:01 +08:00
|
|
|
command,
|
2023-06-20 09:57:57 +08:00
|
|
|
args+tempArgs,
|
2021-08-19 12:01:01 +08:00
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-19 12:01:01 +08:00
|
|
|
mTokenizer[i]->line,
|
|
|
|
StatementKind::skVariable,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition); // TODO: not supported to pass list
|
2021-08-19 12:01:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
command = "";
|
|
|
|
}
|
2023-03-12 23:45:03 +08:00
|
|
|
if (i >= tokenCount - 1)
|
2021-08-19 12:01:01 +08:00
|
|
|
break;
|
2022-11-01 22:10:54 +08:00
|
|
|
if (mTokenizer[i]->text=='{'
|
|
|
|
|| mTokenizer[i]->text== ';')
|
2021-08-19 12:01:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing worth mentioning after closing brace
|
|
|
|
// Proceed to set first synonym as current class
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 08:52:20 +08:00
|
|
|
if (!firstSynonym) {
|
2023-02-20 17:52:42 +08:00
|
|
|
PStatement scope = getCurrentScope();
|
|
|
|
if (scope && scope->kind == StatementKind::skClass
|
2023-03-12 23:45:03 +08:00
|
|
|
&& mIndex<tokenCount && mTokenizer[mIndex]->text=="{") {
|
2023-02-20 17:52:42 +08:00
|
|
|
//C11 anonymous union/struct
|
|
|
|
addSoloScopeLevel(scope, mTokenizer[mIndex]->line);
|
|
|
|
//skip {
|
|
|
|
mIndex++;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
//anonymous union/struct/class, add as a block
|
|
|
|
firstSynonym=addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skBlock,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2023-02-20 17:52:42 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
|
|
|
}
|
2021-08-27 08:52:20 +08:00
|
|
|
}
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex < tokenCount)
|
2023-02-05 21:55:23 +08:00
|
|
|
addSoloScopeLevel(firstSynonym,mTokenizer[mIndex]->line);
|
|
|
|
else
|
|
|
|
addSoloScopeLevel(firstSynonym,startLine);
|
2021-08-19 12:01:01 +08:00
|
|
|
|
|
|
|
// Step over {
|
2023-03-12 23:45:03 +08:00
|
|
|
if ((mIndex < tokenCount) && (mTokenizer[mIndex]->text == "{"))
|
2021-08-19 12:01:01 +08:00
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:08:01 +08:00
|
|
|
void CppParser::handleUsing()
|
|
|
|
{
|
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
if (mCurrentFile.isEmpty()) {
|
2022-11-01 22:10:54 +08:00
|
|
|
//skip pass next ;
|
|
|
|
mIndex=indexOfNextSemicolon(mIndex)+1;
|
2021-08-19 17:08:01 +08:00
|
|
|
return;
|
|
|
|
}
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2021-08-19 17:08:01 +08:00
|
|
|
|
|
|
|
mIndex++; //skip 'using'
|
|
|
|
|
2021-09-28 10:40:19 +08:00
|
|
|
//handle things like 'using vec = std::vector; '
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex+1 < tokenCount
|
2021-09-28 10:40:19 +08:00
|
|
|
&& mTokenizer[mIndex+1]->text == "=") {
|
|
|
|
QString fullName = mTokenizer[mIndex]->text;
|
|
|
|
QString aliasName;
|
|
|
|
mIndex+=2;
|
2023-03-12 23:45:03 +08:00
|
|
|
while (mIndex<tokenCount &&
|
2021-09-28 10:40:19 +08:00
|
|
|
mTokenizer[mIndex]->text!=';') {
|
|
|
|
aliasName += mTokenizer[mIndex]->text;
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
aliasName, // name of the alias (type)
|
|
|
|
fullName, // command
|
|
|
|
"", // args
|
2022-11-01 22:10:54 +08:00
|
|
|
"", // noname args
|
2021-09-28 10:40:19 +08:00
|
|
|
"", // values
|
|
|
|
startLine,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2021-09-28 10:40:19 +08:00
|
|
|
// skip ;
|
|
|
|
mIndex++;
|
|
|
|
return;
|
|
|
|
}
|
2021-08-19 17:08:01 +08:00
|
|
|
//handle things like 'using std::vector;'
|
2023-03-12 23:45:03 +08:00
|
|
|
if ((mIndex+2>=tokenCount)
|
2021-08-19 17:08:01 +08:00
|
|
|
|| (mTokenizer[mIndex]->text != "namespace")) {
|
2023-02-06 20:32:29 +08:00
|
|
|
QString fullName;
|
|
|
|
QString usingName;
|
2023-03-12 23:45:03 +08:00
|
|
|
while (mIndex<tokenCount &&
|
2023-02-06 20:32:29 +08:00
|
|
|
mTokenizer[mIndex]->text!=';') {
|
|
|
|
fullName += mTokenizer[mIndex]->text;
|
|
|
|
usingName = mTokenizer[mIndex]->text;
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
if (fullName!=usingName) {
|
2021-08-19 17:08:01 +08:00
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
fullName, // name of the alias (type)
|
|
|
|
usingName, // command
|
|
|
|
"", // args
|
2022-11-01 22:10:54 +08:00
|
|
|
"", // noname args
|
2021-08-19 17:08:01 +08:00
|
|
|
"", // values
|
|
|
|
startLine,
|
|
|
|
StatementKind::skAlias,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
2023-02-06 20:32:29 +08:00
|
|
|
//skip ;
|
|
|
|
mIndex++;
|
2021-08-19 17:08:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIndex++; // skip 'namespace'
|
|
|
|
PStatement scopeStatement = getCurrentScope();
|
|
|
|
|
2023-02-06 20:32:29 +08:00
|
|
|
QString usingName;
|
2023-03-12 23:45:03 +08:00
|
|
|
while (mIndex<tokenCount &&
|
2023-02-06 20:32:29 +08:00
|
|
|
mTokenizer[mIndex]->text!=';') {
|
|
|
|
usingName += mTokenizer[mIndex]->text;
|
|
|
|
mIndex++;
|
|
|
|
}
|
2021-08-19 17:08:01 +08:00
|
|
|
|
|
|
|
if (scopeStatement) {
|
|
|
|
QString fullName = scopeStatement->fullName + "::" + usingName;
|
|
|
|
if (!mNamespaces.contains(fullName)) {
|
|
|
|
fullName = usingName;
|
|
|
|
}
|
|
|
|
if (mNamespaces.contains(fullName)) {
|
|
|
|
scopeStatement->usingList.insert(fullName);
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileInfo = mPreprocessor.includesList().value(mCurrentFile);
|
2021-08-19 17:08:01 +08:00
|
|
|
if (!fileInfo)
|
|
|
|
return;
|
|
|
|
if (mNamespaces.contains(usingName)) {
|
|
|
|
fileInfo->usings.insert(usingName);
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 20:32:29 +08:00
|
|
|
//skip ;
|
|
|
|
mIndex++;
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
|
2022-11-04 20:27:35 +08:00
|
|
|
void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
|
2021-08-19 17:08:01 +08:00
|
|
|
{
|
2022-11-01 23:23:21 +08:00
|
|
|
QString lastType;
|
2022-11-04 20:27:35 +08:00
|
|
|
if (typePrefix=="extern") {
|
2022-11-01 23:23:21 +08:00
|
|
|
isExtern=true;
|
2022-11-04 20:27:35 +08:00
|
|
|
} else if (typePrefix=="static") {
|
2022-11-01 23:23:21 +08:00
|
|
|
isStatic=true;
|
2022-11-02 10:42:55 +08:00
|
|
|
} else {
|
2023-05-24 19:30:38 +08:00
|
|
|
if (typePrefix.back()==':')
|
|
|
|
return;
|
2023-02-06 14:04:38 +08:00
|
|
|
lastType=typePrefix.trimmed();
|
2022-11-01 23:23:21 +08:00
|
|
|
}
|
2022-11-04 20:27:35 +08:00
|
|
|
|
2022-11-27 13:32:14 +08:00
|
|
|
PStatement addedVar;
|
2021-08-19 17:08:01 +08:00
|
|
|
|
2022-11-01 23:23:21 +08:00
|
|
|
QString tempType;
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2023-06-20 09:57:57 +08:00
|
|
|
while (lastType.endsWith("*") || lastType.endsWith("&")) {
|
|
|
|
tempType = (lastType.back()+tempType);
|
|
|
|
lastType.truncate(lastType.length()-2);
|
|
|
|
}
|
2023-03-12 23:45:03 +08:00
|
|
|
|
|
|
|
while(mIndex<tokenCount) {
|
2023-03-02 11:28:03 +08:00
|
|
|
switch(mTokenizer[mIndex]->text[0].unicode()) {
|
|
|
|
case ':':
|
|
|
|
if (mTokenizer[mIndex]->text.length()>1) {
|
|
|
|
//handle '::'
|
|
|
|
tempType+=mTokenizer[mIndex]->text;
|
|
|
|
mIndex++;
|
|
|
|
} else {
|
|
|
|
// Skip bit identifiers,
|
|
|
|
// e.g.:
|
|
|
|
// handle
|
|
|
|
// unsigned short bAppReturnCode:8,reserved:6,fBusy:1,fAck:1
|
|
|
|
// as
|
|
|
|
// unsigned short bAppReturnCode,reserved,fBusy,fAck
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex+1<tokenCount
|
2023-03-02 11:28:03 +08:00
|
|
|
&& isIdentifier(mTokenizer[mIndex+1]->text)
|
|
|
|
&& isIdentChar(mTokenizer[mIndex+1]->text.back())
|
|
|
|
&& addedVar
|
|
|
|
&& !(addedVar->properties & StatementProperty::spFunctionPointer)
|
|
|
|
&& AutoTypes.contains(addedVar->type)) {
|
|
|
|
//handle e.g.: for(auto x:vec)
|
|
|
|
QStringList phraseExpression;
|
|
|
|
phraseExpression.append(mTokenizer[mIndex+1]->text);
|
|
|
|
int pos = 0;
|
|
|
|
PEvalStatement aliasStatement = doEvalExpression(mCurrentFile,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
getCurrentScope(),
|
|
|
|
PEvalStatement(),
|
|
|
|
true);
|
2023-03-11 19:56:40 +08:00
|
|
|
if(aliasStatement && aliasStatement->effectiveTypeStatement) {
|
2023-03-02 11:28:03 +08:00
|
|
|
if (STLMaps.contains(aliasStatement->effectiveTypeStatement->fullName)) {
|
|
|
|
addedVar->type = "std::pair"+aliasStatement->templateParams;
|
2023-03-11 19:56:40 +08:00
|
|
|
} else if (STLContainers.contains(aliasStatement->effectiveTypeStatement->fullName)){
|
2023-03-02 11:28:03 +08:00
|
|
|
QString type=doFindFirstTemplateParamOf(mCurrentFile,aliasStatement->templateParams,
|
|
|
|
getCurrentScope());
|
|
|
|
if (!type.isEmpty())
|
|
|
|
addedVar->type = type;
|
|
|
|
}
|
2022-11-29 16:48:40 +08:00
|
|
|
}
|
2022-11-27 13:32:14 +08:00
|
|
|
}
|
2023-03-02 11:28:03 +08:00
|
|
|
addedVar.reset();
|
|
|
|
bool should_exit=false;
|
2023-03-12 23:45:03 +08:00
|
|
|
while (mIndex < tokenCount) {
|
2023-03-02 11:28:03 +08:00
|
|
|
switch(mTokenizer[mIndex]->text[0].unicode()) {
|
|
|
|
case ',':
|
|
|
|
case ';':
|
|
|
|
case '=':
|
|
|
|
should_exit = true;
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
mIndex++;
|
|
|
|
return;
|
|
|
|
case '(':
|
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mIndex++;
|
2022-11-09 22:22:33 +08:00
|
|
|
}
|
2023-03-02 11:28:03 +08:00
|
|
|
if (should_exit)
|
|
|
|
break;
|
2021-08-28 01:20:32 +08:00
|
|
|
}
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
2023-03-02 11:28:03 +08:00
|
|
|
break;
|
|
|
|
case ';':
|
|
|
|
mIndex++;
|
|
|
|
return;
|
|
|
|
case '=':
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex+1<tokenCount
|
2023-06-20 21:07:26 +08:00
|
|
|
&& mTokenizer[mIndex+1]->text!="{"
|
2022-11-27 13:32:14 +08:00
|
|
|
&& addedVar
|
|
|
|
&& !(addedVar->properties & StatementProperty::spFunctionPointer)
|
2022-11-29 16:48:40 +08:00
|
|
|
&& AutoTypes.contains(addedVar->type)) {
|
2023-03-02 11:28:03 +08:00
|
|
|
//handle e.g.: auto x=blahblah;
|
2022-11-27 13:32:14 +08:00
|
|
|
int pos = 0;
|
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
int endIndex = skipAssignment(mIndex, tokenCount);
|
2022-11-27 13:32:14 +08:00
|
|
|
QStringList phraseExpression;
|
|
|
|
for (int i=mIndex+1;i<endIndex;i++) {
|
|
|
|
QString cmd = mTokenizer[i]->text;
|
|
|
|
if (cmd.length()>1 && cmd.endsWith(".")) {
|
|
|
|
phraseExpression.append(cmd.left(cmd.length()-1));
|
|
|
|
phraseExpression.append(".");
|
|
|
|
} else if (cmd.length()>2 && cmd.endsWith("->")) {
|
|
|
|
phraseExpression.append(cmd.left(cmd.length()-2));
|
|
|
|
phraseExpression.append("->");
|
|
|
|
} else if (cmd.length()>2 && cmd.endsWith("::")) {
|
|
|
|
phraseExpression.append(cmd.left(cmd.length()-2));
|
|
|
|
phraseExpression.append("::");
|
|
|
|
} else
|
|
|
|
phraseExpression.append(cmd);
|
|
|
|
}
|
|
|
|
PEvalStatement aliasStatement = doEvalExpression(mCurrentFile,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
getCurrentScope(),
|
|
|
|
PEvalStatement(),
|
|
|
|
true);
|
|
|
|
if(aliasStatement) {
|
2022-11-29 12:06:29 +08:00
|
|
|
if (aliasStatement->effectiveTypeStatement) {
|
2022-11-27 13:32:14 +08:00
|
|
|
addedVar->type = aliasStatement->effectiveTypeStatement->fullName;
|
2022-11-29 12:06:29 +08:00
|
|
|
if (!addedVar->type.endsWith(">"))
|
|
|
|
addedVar->type += aliasStatement->templateParams;
|
2022-11-29 12:17:41 +08:00
|
|
|
if (aliasStatement->typeStatement
|
|
|
|
&& STLIterators.contains(aliasStatement->typeStatement->command)
|
|
|
|
&& !aliasStatement->templateParams.isEmpty()) {
|
|
|
|
PStatement parentStatement = aliasStatement->typeStatement->parentScope.lock();
|
|
|
|
if (parentStatement
|
|
|
|
&& STLContainers.contains(parentStatement->fullName)) {
|
|
|
|
addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command;
|
2023-03-11 19:56:40 +08:00
|
|
|
} else if (parentStatement
|
|
|
|
&& STLMaps.contains(parentStatement->fullName)) {
|
|
|
|
addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command;
|
2022-11-29 12:17:41 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-29 12:06:29 +08:00
|
|
|
} else
|
2022-11-29 15:42:08 +08:00
|
|
|
addedVar->type = aliasStatement->baseType + aliasStatement->templateParams;
|
2022-11-27 13:32:14 +08:00
|
|
|
if (aliasStatement->pointerLevel>0)
|
|
|
|
addedVar->type += QString(aliasStatement->pointerLevel,'*');
|
|
|
|
}
|
|
|
|
mIndex = endIndex;
|
|
|
|
} else
|
2023-03-12 23:45:03 +08:00
|
|
|
mIndex = skipAssignment(mIndex, tokenCount);
|
2022-11-27 13:32:14 +08:00
|
|
|
addedVar.reset();
|
2023-03-02 11:28:03 +08:00
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
case '&':
|
|
|
|
tempType+=mTokenizer[mIndex]->text;
|
|
|
|
mIndex++;
|
|
|
|
break;
|
|
|
|
case '(':
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mTokenizer[mIndex]->matchIndex+1<tokenCount
|
2023-03-02 11:28:03 +08:00
|
|
|
&& mTokenizer[mTokenizer[mIndex]->matchIndex+1]->text=='(') {
|
|
|
|
//function pointer
|
|
|
|
QString cmd = findFunctionPointerName(mIndex);
|
|
|
|
int argStart=mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
int argEnd=mTokenizer[argStart]->matchIndex;
|
|
|
|
|
|
|
|
if (!cmd.isEmpty()) {
|
|
|
|
QString type=lastType;
|
|
|
|
if(type.endsWith("::"))
|
|
|
|
type+=tempType.trimmed();
|
|
|
|
else
|
|
|
|
type+=" "+tempType.trimmed();
|
|
|
|
|
|
|
|
addChildStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
type.trimmed(),
|
|
|
|
cmd,
|
|
|
|
mergeArgs(argStart,argEnd),
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skVariable,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2023-03-02 11:28:03 +08:00
|
|
|
//True,
|
|
|
|
(isExtern?StatementProperty::spNone:StatementProperty::spHasDefinition)
|
|
|
|
| (isStatic?StatementProperty::spStatic:StatementProperty::spNone)
|
|
|
|
| StatementProperty::spFunctionPointer);
|
|
|
|
}
|
|
|
|
addedVar.reset();
|
|
|
|
tempType="";
|
|
|
|
mIndex=indexOfNextPeriodOrSemicolon(argEnd+1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//not function pointer, fall through
|
|
|
|
case '{':
|
2022-11-02 10:42:55 +08:00
|
|
|
tempType="";
|
2023-03-12 23:45:03 +08:00
|
|
|
if (mIndex+1<tokenCount
|
2022-11-27 13:32:14 +08:00
|
|
|
&& isIdentifier(mTokenizer[mIndex+1]->text)
|
|
|
|
&& addedVar
|
|
|
|
&& !(addedVar->properties & StatementProperty::spFunctionPointer)
|
2022-11-29 16:48:40 +08:00
|
|
|
&& AutoTypes.contains(addedVar->type)) {
|
2022-11-27 13:32:14 +08:00
|
|
|
int pos = 0;
|
|
|
|
int endIndex = mTokenizer[mIndex]->matchIndex;
|
|
|
|
QStringList phraseExpression;
|
|
|
|
for (int i=mIndex+1;i<endIndex;i++) {
|
|
|
|
QString cmd = mTokenizer[i]->text;
|
|
|
|
if (cmd.length()>1 && cmd.endsWith(".")) {
|
|
|
|
phraseExpression.append(cmd.left(cmd.length()-1));
|
|
|
|
phraseExpression.append(".");
|
|
|
|
} else if (cmd.length()>2 && cmd.endsWith("->")) {
|
|
|
|
phraseExpression.append(cmd.left(cmd.length()-2));
|
|
|
|
phraseExpression.append("->");
|
|
|
|
} else if (cmd.length()>2 && cmd.endsWith("::")) {
|
|
|
|
phraseExpression.append(cmd.left(cmd.length()-2));
|
|
|
|
phraseExpression.append("::");
|
|
|
|
} else
|
|
|
|
phraseExpression.append(cmd);
|
|
|
|
}
|
|
|
|
PEvalStatement aliasStatement = doEvalExpression(mCurrentFile,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
getCurrentScope(),
|
|
|
|
PEvalStatement(),
|
|
|
|
true);
|
|
|
|
if(aliasStatement && aliasStatement->effectiveTypeStatement) {
|
2022-11-29 12:06:29 +08:00
|
|
|
if (aliasStatement->effectiveTypeStatement) {
|
2022-11-27 13:32:14 +08:00
|
|
|
addedVar->type = aliasStatement->effectiveTypeStatement->fullName;
|
2022-11-29 12:06:29 +08:00
|
|
|
if (!addedVar->type.endsWith(">"))
|
|
|
|
addedVar->type += aliasStatement->templateParams;
|
2022-11-29 12:17:41 +08:00
|
|
|
if (aliasStatement->typeStatement
|
|
|
|
&& STLIterators.contains(aliasStatement->typeStatement->command)) {
|
|
|
|
PStatement parentStatement = aliasStatement->typeStatement->parentScope.lock();
|
|
|
|
if (parentStatement
|
|
|
|
&& STLContainers.contains(parentStatement->fullName)) {
|
|
|
|
addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command;
|
2023-03-11 19:56:40 +08:00
|
|
|
} else if (parentStatement
|
|
|
|
&& STLMaps.contains(parentStatement->fullName)) {
|
|
|
|
addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command;
|
2022-11-29 12:17:41 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-29 12:06:29 +08:00
|
|
|
} else
|
2023-06-20 21:07:26 +08:00
|
|
|
addedVar->type = aliasStatement->baseType + aliasStatement->templateParams;
|
2022-11-27 13:32:14 +08:00
|
|
|
if (aliasStatement->pointerLevel>0)
|
|
|
|
addedVar->type += QString(aliasStatement->pointerLevel,'*');
|
|
|
|
}
|
|
|
|
}
|
2022-11-01 23:23:21 +08:00
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex+1;
|
2022-11-27 13:32:14 +08:00
|
|
|
addedVar.reset();
|
2023-03-02 11:28:03 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isIdentChar(mTokenizer[mIndex]->text[0])) {
|
|
|
|
QString cmd=mTokenizer[mIndex]->text;
|
|
|
|
//normal var
|
|
|
|
if (cmd=="const") {
|
|
|
|
if (tempType.isEmpty()) {
|
|
|
|
tempType = cmd;
|
|
|
|
} else if (tempType.endsWith("*")) {
|
|
|
|
tempType+=cmd;
|
|
|
|
} else {
|
|
|
|
tempType+=" "+cmd;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QString suffix;
|
|
|
|
QString args;
|
|
|
|
cmd=mTokenizer[mIndex]->text;
|
|
|
|
parseCommandTypeAndArgs(cmd,suffix,args);
|
|
|
|
if (!cmd.isEmpty() && !isKeyword(cmd)) {
|
|
|
|
addedVar = addChildStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
(lastType+' '+tempType+suffix).trimmed(),
|
|
|
|
cmd,
|
|
|
|
args,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skVariable,
|
|
|
|
getScope(),
|
2023-03-12 18:24:58 +08:00
|
|
|
mCurrentMemberAccessibility,
|
2023-03-02 11:28:03 +08:00
|
|
|
//True,
|
|
|
|
(isExtern?StatementProperty::spNone:StatementProperty::spHasDefinition)
|
|
|
|
| (isStatic?StatementProperty::spStatic:StatementProperty::spNone));
|
|
|
|
tempType="";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
} else {
|
|
|
|
tempType="";
|
|
|
|
mIndex++;
|
|
|
|
}
|
2022-11-01 23:23:21 +08:00
|
|
|
}
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
2022-11-01 23:23:21 +08:00
|
|
|
// Skip ;
|
|
|
|
mIndex++;
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
void CppParser::internalParse(const QString &fileName)
|
2021-08-19 17:08:01 +08:00
|
|
|
{
|
|
|
|
// Perform some validation before we start
|
|
|
|
if (!mEnabled)
|
|
|
|
return;
|
2022-01-23 23:27:48 +08:00
|
|
|
// if (!isCfile(fileName) && !isHfile(fileName)) // support only known C/C++ files
|
|
|
|
// return;
|
2021-08-19 17:08:01 +08:00
|
|
|
|
2023-03-13 00:13:23 +08:00
|
|
|
//QElapsedTimer timer;
|
2021-08-19 17:08:01 +08:00
|
|
|
// Preprocess the file...
|
2023-02-15 16:24:24 +08:00
|
|
|
auto action = finally([this]{
|
|
|
|
mTokenizer.clear();
|
|
|
|
});
|
2023-03-13 00:13:23 +08:00
|
|
|
//timer.start();
|
2023-02-15 16:24:24 +08:00
|
|
|
// Let the preprocessor augment the include records
|
|
|
|
mPreprocessor.setScanOptions(mParseGlobalHeaders, mParseLocalHeaders);
|
|
|
|
mPreprocessor.preprocess(fileName);
|
|
|
|
|
|
|
|
QStringList preprocessResult = mPreprocessor.result();
|
2021-11-12 02:20:13 +08:00
|
|
|
#ifdef QT_DEBUG
|
2023-08-09 22:36:33 +08:00
|
|
|
// stringsToFile(mPreprocessor.result(),QString("r:\\preprocess-%1.txt").arg(extractFileName(fileName)));
|
2022-10-22 10:44:10 +08:00
|
|
|
// mPreprocessor.dumpDefinesTo("r:\\defines.txt");
|
|
|
|
// mPreprocessor.dumpIncludesListTo("r:\\includes.txt");
|
2021-11-12 02:20:13 +08:00
|
|
|
#endif
|
2023-03-13 00:13:23 +08:00
|
|
|
//qDebug()<<"preprocess"<<timer.elapsed();
|
2023-02-15 16:24:24 +08:00
|
|
|
//reduce memory usage
|
2023-03-13 00:13:23 +08:00
|
|
|
//timer.restart();
|
2023-02-15 16:24:24 +08:00
|
|
|
mPreprocessor.clearTempResults();
|
2023-03-13 00:13:23 +08:00
|
|
|
//qDebug()<<"preprocess clean"<<timer.elapsed();
|
2023-02-15 16:24:24 +08:00
|
|
|
|
2023-03-13 00:13:23 +08:00
|
|
|
//timer.restart();
|
2023-02-15 16:24:24 +08:00
|
|
|
// Tokenize the preprocessed buffer file
|
|
|
|
mTokenizer.tokenize(preprocessResult);
|
|
|
|
//reduce memory usage
|
|
|
|
preprocessResult.clear();
|
2023-03-13 00:13:23 +08:00
|
|
|
//qDebug()<<"tokenize"<<timer.elapsed();
|
2023-02-15 16:24:24 +08:00
|
|
|
if (mTokenizer.tokenCount() == 0)
|
|
|
|
return;
|
2022-11-02 22:48:25 +08:00
|
|
|
#ifdef QT_DEBUG
|
2023-07-03 18:21:33 +08:00
|
|
|
// mTokenizer.dumpTokens(QString("r:\\tokens-%1.txt").arg(extractFileName(fileName)));
|
2022-11-11 21:20:57 +08:00
|
|
|
#endif
|
|
|
|
#ifdef QT_DEBUG
|
2023-02-21 22:40:29 +08:00
|
|
|
mLastIndex = -1;
|
2022-11-02 22:48:25 +08:00
|
|
|
#endif
|
2023-03-13 00:13:23 +08:00
|
|
|
// timer.restart();
|
2023-02-15 16:24:24 +08:00
|
|
|
// Process the token list
|
|
|
|
while(true) {
|
|
|
|
if (!handleStatement())
|
|
|
|
break;
|
|
|
|
}
|
2023-03-13 00:13:23 +08:00
|
|
|
// qDebug()<<"parse"<<timer.elapsed();
|
2021-08-27 23:51:42 +08:00
|
|
|
#ifdef QT_DEBUG
|
2023-08-09 22:36:33 +08:00
|
|
|
// mStatementList.dumpAll(QString("r:\\all-stats-%1.txt").arg(extractFileName(fileName)));
|
|
|
|
// mStatementList.dump(QString("r:\\stats-%1.txt").arg(extractFileName(fileName)));
|
2021-08-27 23:51:42 +08:00
|
|
|
#endif
|
2023-02-15 16:24:24 +08:00
|
|
|
//reduce memory usage
|
|
|
|
internalClear();
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
void CppParser::inheritClassStatement(const PStatement& derived, bool isStruct,
|
2023-03-12 18:24:58 +08:00
|
|
|
const PStatement& base, StatementAccessibility access)
|
2021-08-19 17:08:01 +08:00
|
|
|
{
|
|
|
|
//differentiate class and struct
|
2023-03-12 18:24:58 +08:00
|
|
|
if (access == StatementAccessibility::None) {
|
2021-08-19 17:08:01 +08:00
|
|
|
if (isStruct)
|
2023-03-12 18:24:58 +08:00
|
|
|
access = StatementAccessibility::Public;
|
2021-08-19 17:08:01 +08:00
|
|
|
else
|
2023-03-12 18:24:58 +08:00
|
|
|
access = StatementAccessibility::Private;
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
2021-08-29 10:14:07 +08:00
|
|
|
foreach (const PStatement& statement, base->children) {
|
2023-03-12 18:24:58 +08:00
|
|
|
if (statement->accessibility == StatementAccessibility::Private
|
2021-08-19 17:08:01 +08:00
|
|
|
|| statement->kind == StatementKind::skConstructor
|
|
|
|
|| statement->kind == StatementKind::skDestructor)
|
|
|
|
continue;
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility m_acc;
|
2021-08-19 17:08:01 +08:00
|
|
|
switch(access) {
|
2023-03-12 18:24:58 +08:00
|
|
|
case StatementAccessibility::Public:
|
|
|
|
m_acc = statement->accessibility;
|
2021-08-19 17:08:01 +08:00
|
|
|
break;
|
2023-03-12 18:24:58 +08:00
|
|
|
case StatementAccessibility::Protected:
|
|
|
|
m_acc = StatementAccessibility::Protected;
|
2021-08-19 17:08:01 +08:00
|
|
|
break;
|
2023-03-12 18:24:58 +08:00
|
|
|
case StatementAccessibility::Private:
|
|
|
|
m_acc = StatementAccessibility::Private;
|
2021-08-19 17:08:01 +08:00
|
|
|
break;
|
|
|
|
default:
|
2023-03-12 18:24:58 +08:00
|
|
|
m_acc = StatementAccessibility::Private;
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
//inherit
|
|
|
|
addInheritedStatement(derived,statement,m_acc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
void CppParser::fillListOfFunctions(const QString& fileName, int line,
|
|
|
|
const PStatement& statement,
|
|
|
|
const PStatement& scopeStatement, QStringList &list)
|
2021-08-21 22:15:44 +08:00
|
|
|
{
|
|
|
|
StatementMap children = mStatementList.childrenStatements(scopeStatement);
|
2021-08-29 00:48:23 +08:00
|
|
|
for (const PStatement& child:children) {
|
2021-08-21 22:15:44 +08:00
|
|
|
if ((statement->command == child->command)
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|| (statement->command +'A' == child->command)
|
|
|
|
|| (statement->command +'W' == child->command)
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
if (line < child->line && (child->fileName == fileName))
|
|
|
|
continue;
|
2021-08-29 22:08:43 +08:00
|
|
|
list.append(prettyPrintStatement(child,child->fileName,child->line));
|
2021-08-21 22:15:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
QList<PStatement> CppParser::getListOfFunctions(const QString &fileName, int line, const PStatement &statement, const PStatement &scopeStatement) const
|
2021-09-24 18:02:42 +08:00
|
|
|
{
|
|
|
|
QList<PStatement> result;
|
|
|
|
StatementMap children = mStatementList.childrenStatements(scopeStatement);
|
|
|
|
for (const PStatement& child:children) {
|
2022-01-26 12:17:15 +08:00
|
|
|
if (( (statement->command == child->command)
|
2021-09-24 18:02:42 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|| (statement->command +'A' == child->command)
|
|
|
|
|| (statement->command +'W' == child->command)
|
|
|
|
#endif
|
2022-01-26 12:17:15 +08:00
|
|
|
) ) {
|
2021-09-24 18:02:42 +08:00
|
|
|
if (line < child->line && (child->fileName == fileName))
|
|
|
|
continue;
|
|
|
|
result.append(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement CppParser::findMacro(const QString &phrase, const QString &fileName) const
|
2022-12-11 19:47:43 +08:00
|
|
|
{
|
|
|
|
const StatementMap& statementMap =mStatementList.childrenStatements(nullptr);
|
|
|
|
if (statementMap.isEmpty())
|
|
|
|
return PStatement();
|
|
|
|
StatementList statements = statementMap.values(phrase);
|
|
|
|
PFileIncludes includes = mPreprocessor.includesList().value(fileName,PFileIncludes());
|
|
|
|
foreach (const PStatement& s, statements) {
|
|
|
|
if (s->kind == StatementKind::skPreprocessor) {
|
|
|
|
if (includes && !includes->includeFiles.contains(s->fileName)
|
|
|
|
&& !includes->includeFiles.contains(s->definitionFileName))
|
|
|
|
continue;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::findMemberOfStatement(const QString &phrase,
|
2023-03-11 17:32:57 +08:00
|
|
|
const PStatement& scopeStatement) const
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
|
|
|
const StatementMap& statementMap =mStatementList.childrenStatements(scopeStatement);
|
|
|
|
if (statementMap.isEmpty())
|
|
|
|
return PStatement();
|
|
|
|
|
|
|
|
QString s = phrase;
|
|
|
|
//remove []
|
|
|
|
int p = phrase.indexOf('[');
|
2021-12-04 10:02:07 +08:00
|
|
|
if (p>=0)
|
|
|
|
s.truncate(p);
|
|
|
|
//remove ()
|
|
|
|
p = phrase.indexOf('(');
|
2021-08-16 00:47:35 +08:00
|
|
|
if (p>=0)
|
|
|
|
s.truncate(p);
|
|
|
|
|
|
|
|
//remove <>
|
|
|
|
p =s.indexOf('<');
|
|
|
|
if (p>=0)
|
|
|
|
s.truncate(p);
|
|
|
|
|
|
|
|
return statementMap.value(s,PStatement());
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:44:33 +08:00
|
|
|
|
|
|
|
PStatement CppParser::findMemberOfStatement(const QString& filename,
|
|
|
|
const QString &phrase,
|
|
|
|
const PStatement& scopeStatement) const
|
|
|
|
{
|
|
|
|
const StatementMap& statementMap =mStatementList.childrenStatements(scopeStatement);
|
|
|
|
if (statementMap.isEmpty())
|
|
|
|
return PStatement();
|
|
|
|
|
|
|
|
QString s = phrase;
|
|
|
|
//remove []
|
|
|
|
int p = phrase.indexOf('[');
|
|
|
|
if (p>=0)
|
|
|
|
s.truncate(p);
|
|
|
|
//remove ()
|
|
|
|
p = phrase.indexOf('(');
|
|
|
|
if (p>=0)
|
|
|
|
s.truncate(p);
|
|
|
|
|
|
|
|
//remove <>
|
|
|
|
p =s.indexOf('<');
|
|
|
|
if (p>=0)
|
|
|
|
s.truncate(p);
|
|
|
|
if (scopeStatement) {
|
|
|
|
return statementMap.value(s,PStatement());
|
|
|
|
} else {
|
|
|
|
QList<PStatement> stats = statementMap.values(s);
|
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(filename,PFileIncludes());
|
|
|
|
foreach(const PStatement &s,stats) {
|
|
|
|
if (s->fileName == filename || s->definitionFileName==filename) {
|
|
|
|
return s;
|
|
|
|
} else if (fileIncludes && (fileIncludes->includeFiles.contains(s->fileName)
|
|
|
|
|| fileIncludes->includeFiles.contains(s->definitionFileName))) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
QList<PStatement> CppParser::findMembersOfStatement(const QString &phrase, const PStatement &scopeStatement) const
|
2022-03-16 16:24:39 +08:00
|
|
|
{
|
|
|
|
const StatementMap& statementMap =mStatementList.childrenStatements(scopeStatement);
|
|
|
|
if (statementMap.isEmpty())
|
|
|
|
return QList<PStatement>();
|
|
|
|
|
|
|
|
QString s = phrase;
|
|
|
|
//remove []
|
|
|
|
int p = phrase.indexOf('[');
|
|
|
|
if (p>=0)
|
|
|
|
s.truncate(p);
|
|
|
|
//remove ()
|
|
|
|
p = phrase.indexOf('(');
|
|
|
|
if (p>=0)
|
|
|
|
s.truncate(p);
|
|
|
|
|
|
|
|
//remove <>
|
|
|
|
p =s.indexOf('<');
|
|
|
|
if (p>=0)
|
|
|
|
s.truncate(p);
|
|
|
|
|
|
|
|
return statementMap.values(s);
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::findStatementInScope(const QString &name, const QString &noNameArgs,
|
2023-03-11 17:32:57 +08:00
|
|
|
StatementKind kind, const PStatement& scope) const
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
|
|
|
if (scope && scope->kind == StatementKind::skNamespace) {
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatementList namespaceStatementsList = doFindNamespace(scope->command);
|
2021-08-16 00:47:35 +08:00
|
|
|
if (!namespaceStatementsList)
|
|
|
|
return PStatement();
|
2021-08-29 10:14:07 +08:00
|
|
|
foreach (const PStatement& namespaceStatement, *namespaceStatementsList) {
|
2023-03-11 19:30:56 +08:00
|
|
|
PStatement result=doFindStatementInScope(name,noNameArgs,kind,namespaceStatement);
|
2021-08-16 00:47:35 +08:00
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-11 19:30:56 +08:00
|
|
|
return doFindStatementInScope(name,noNameArgs,kind,scope);
|
2021-08-16 00:47:35 +08:00
|
|
|
}
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement CppParser::findStatementInScope(const QString &name, const PStatement &scope) const
|
2022-03-23 14:13:10 +08:00
|
|
|
{
|
|
|
|
if (!scope)
|
|
|
|
return findMemberOfStatement(name,scope);
|
|
|
|
if (scope->kind == StatementKind::skNamespace) {
|
|
|
|
return findStatementInNamespace(name, scope->fullName);
|
|
|
|
} else {
|
|
|
|
return findMemberOfStatement(name,scope);
|
|
|
|
}
|
|
|
|
}
|
2021-08-22 21:23:58 +08:00
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement CppParser::findStatementInNamespace(const QString &name, const QString &namespaceName) const
|
2021-08-22 21:23:58 +08:00
|
|
|
{
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatementList namespaceStatementsList=doFindNamespace(namespaceName);
|
2021-08-22 21:23:58 +08:00
|
|
|
if (!namespaceStatementsList)
|
|
|
|
return PStatement();
|
2021-08-29 10:14:07 +08:00
|
|
|
foreach (const PStatement& namespaceStatement,*namespaceStatementsList) {
|
2021-08-22 21:23:58 +08:00
|
|
|
PStatement result = findMemberOfStatement(name,namespaceStatement);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement CppParser::doEvalExpression(const QString& fileName,
|
2022-12-11 19:47:43 +08:00
|
|
|
QStringList& phraseExpression,
|
2021-12-06 11:37:37 +08:00
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
|
|
|
const PEvalStatement& previousResult,
|
2023-03-11 17:32:57 +08:00
|
|
|
bool freeScoped) const
|
2021-12-06 11:37:37 +08:00
|
|
|
{
|
|
|
|
//dummy function to easy later upgrades
|
2021-12-08 19:55:15 +08:00
|
|
|
return doEvalPointerArithmetic(fileName,
|
2021-12-06 11:37:37 +08:00
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
2021-12-08 19:55:15 +08:00
|
|
|
freeScoped);
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PEvalStatement CppParser::doEvalPointerArithmetic(const QString &fileName, QStringList &phraseExpression, int &pos, const PStatement &scope, const PEvalStatement &previousResult, bool freeScoped) const
|
2021-12-08 19:55:15 +08:00
|
|
|
{
|
|
|
|
if (pos>=phraseExpression.length())
|
|
|
|
return PEvalStatement();
|
|
|
|
//find the start scope statement
|
|
|
|
PEvalStatement currentResult = doEvalPointerToMembers(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
|
|
|
while (pos < phraseExpression.length()) {
|
|
|
|
if (!currentResult)
|
|
|
|
break;
|
|
|
|
if (currentResult &&
|
|
|
|
(phraseExpression[pos]=="+"
|
|
|
|
|| phraseExpression[pos]=="-")) {
|
|
|
|
if (currentResult->kind == EvalStatementKind::Variable) {
|
|
|
|
pos++;
|
|
|
|
PEvalStatement op2=doEvalPointerToMembers(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
currentResult,
|
|
|
|
false);
|
|
|
|
//todo operator+/- overload
|
|
|
|
} else if (currentResult->kind == EvalStatementKind::Literal
|
|
|
|
&& currentResult->baseType == "int") {
|
|
|
|
pos++;
|
|
|
|
PEvalStatement op2=doEvalPointerToMembers(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
currentResult,
|
|
|
|
false);
|
|
|
|
currentResult = op2;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2021-12-08 21:44:40 +08:00
|
|
|
// qDebug()<<pos<<"pointer add member end";
|
2021-12-08 19:55:15 +08:00
|
|
|
return currentResult;
|
|
|
|
|
2021-12-06 11:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PEvalStatement CppParser::doEvalPointerToMembers(
|
|
|
|
const QString &fileName,
|
2022-12-11 19:47:43 +08:00
|
|
|
QStringList &phraseExpression,
|
2021-12-06 11:37:37 +08:00
|
|
|
int &pos,
|
|
|
|
const PStatement &scope,
|
|
|
|
const PEvalStatement &previousResult,
|
2023-03-11 17:32:57 +08:00
|
|
|
bool freeScoped) const
|
2021-12-06 11:37:37 +08:00
|
|
|
{
|
|
|
|
if (pos>=phraseExpression.length())
|
|
|
|
return PEvalStatement();
|
|
|
|
//find the start scope statement
|
|
|
|
PEvalStatement currentResult = doEvalCCast(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
2021-12-08 19:13:47 +08:00
|
|
|
while (pos < phraseExpression.length()) {
|
2021-12-06 11:37:37 +08:00
|
|
|
if (!currentResult)
|
|
|
|
break;
|
|
|
|
if (currentResult &&
|
|
|
|
(currentResult->kind == EvalStatementKind::Variable)
|
|
|
|
&& (phraseExpression[pos]==".*"
|
|
|
|
|| phraseExpression[pos]=="->*")) {
|
|
|
|
pos++;
|
|
|
|
currentResult =
|
|
|
|
doEvalCCast(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
currentResult,
|
|
|
|
false);
|
|
|
|
if (currentResult) {
|
|
|
|
currentResult->pointerLevel++;
|
|
|
|
}
|
|
|
|
} else
|
2021-12-08 19:13:47 +08:00
|
|
|
break;
|
2021-12-06 11:37:37 +08:00
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
// qDebug()<<pos<<"pointer member end";
|
2021-12-06 11:37:37 +08:00
|
|
|
return currentResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
PEvalStatement CppParser::doEvalCCast(const QString &fileName,
|
2022-12-11 19:47:43 +08:00
|
|
|
QStringList &phraseExpression,
|
2021-12-06 09:02:39 +08:00
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
const PEvalStatement& previousResult,
|
2023-03-11 17:32:57 +08:00
|
|
|
bool freeScoped) const
|
2021-12-04 18:38:54 +08:00
|
|
|
{
|
|
|
|
if (pos>=phraseExpression.length())
|
2021-12-06 11:37:37 +08:00
|
|
|
return PEvalStatement();
|
|
|
|
PEvalStatement result;
|
2021-12-04 18:38:54 +08:00
|
|
|
if (phraseExpression[pos]=="*") {
|
2021-12-05 16:45:48 +08:00
|
|
|
pos++; //skip "*"
|
2021-12-06 11:37:37 +08:00
|
|
|
result = doEvalCCast(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
|
|
|
if (result) {
|
|
|
|
//todo: STL container;
|
2022-11-29 12:06:29 +08:00
|
|
|
|
2021-12-08 21:44:40 +08:00
|
|
|
if (result->pointerLevel==0) {
|
2022-11-29 12:06:29 +08:00
|
|
|
//STL smart pointers
|
|
|
|
if (result->typeStatement
|
|
|
|
&& STLIterators.contains(result->typeStatement->command)
|
|
|
|
) {
|
|
|
|
PStatement parentScope = result->typeStatement->parentScope.lock();
|
|
|
|
if (STLContainers.contains(parentScope->fullName)) {
|
2023-03-11 17:32:57 +08:00
|
|
|
QString typeName=doFindFirstTemplateParamOf(fileName,result->templateParams, parentScope);
|
2023-03-11 19:56:40 +08:00
|
|
|
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
|
|
|
|
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-03-11 19:56:40 +08:00
|
|
|
result->definitionString=typeName;
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
} else {
|
|
|
|
result = PEvalStatement();
|
|
|
|
}
|
|
|
|
} else if (STLMaps.contains(parentScope->fullName)) {
|
|
|
|
QString typeName=doFindTemplateParamOf(fileName,result->templateParams,1,parentScope);
|
2022-11-29 12:06:29 +08:00
|
|
|
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
2022-11-29 12:06:29 +08:00
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-03-11 19:30:56 +08:00
|
|
|
result->definitionString=typeName;
|
2022-11-29 12:06:29 +08:00
|
|
|
result->kind = EvalStatementKind::Variable;
|
2023-03-11 19:30:56 +08:00
|
|
|
} else {
|
|
|
|
result = PEvalStatement();
|
2022-11-29 12:06:29 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-11 19:56:40 +08:00
|
|
|
|
2022-11-29 12:06:29 +08:00
|
|
|
} else {
|
|
|
|
PStatement typeStatement = result->effectiveTypeStatement;
|
|
|
|
if ((typeStatement)
|
2021-12-08 21:44:40 +08:00
|
|
|
&& STLPointers.contains(typeStatement->fullName)
|
|
|
|
&& result->kind == EvalStatementKind::Variable
|
|
|
|
&& result->baseStatement) {
|
2022-11-29 12:06:29 +08:00
|
|
|
PStatement parentScope = result->baseStatement->parentScope.lock();
|
2023-03-11 17:32:57 +08:00
|
|
|
QString typeName;
|
2023-03-11 19:30:56 +08:00
|
|
|
if (!previousResult || previousResult->definitionString.isEmpty())
|
2023-03-11 17:32:57 +08:00
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope);
|
|
|
|
else
|
2023-03-11 19:30:56 +08:00
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,previousResult->definitionString,parentScope);
|
2023-03-11 17:32:57 +08:00
|
|
|
|
2022-11-29 12:06:29 +08:00
|
|
|
// qDebug()<<"typeName"<<typeName;
|
2023-03-11 17:32:57 +08:00
|
|
|
typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
2022-11-29 12:06:29 +08:00
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-03-11 19:30:56 +08:00
|
|
|
result->definitionString=typeName;
|
2022-11-29 12:06:29 +08:00
|
|
|
result->kind = EvalStatementKind::Variable;
|
2023-03-11 19:30:56 +08:00
|
|
|
} else {
|
|
|
|
result = PEvalStatement();
|
2022-11-29 12:06:29 +08:00
|
|
|
}
|
2021-12-08 21:44:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
result->pointerLevel--;
|
2021-12-06 11:37:37 +08:00
|
|
|
}
|
2021-12-04 18:38:54 +08:00
|
|
|
} else if (phraseExpression[pos]=="&") {
|
2021-12-05 16:45:48 +08:00
|
|
|
pos++; //skip "&"
|
2021-12-06 11:37:37 +08:00
|
|
|
result = doEvalCCast(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
|
|
|
if (result) {
|
|
|
|
result->pointerLevel++;
|
|
|
|
}
|
2021-12-05 10:52:17 +08:00
|
|
|
} else if (phraseExpression[pos]=="++"
|
|
|
|
|| phraseExpression[pos]=="--") {
|
2021-12-05 16:45:48 +08:00
|
|
|
pos++; //skip "++" or "--"
|
2021-12-06 11:37:37 +08:00
|
|
|
result = doEvalCCast(
|
2021-12-06 09:02:39 +08:00
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
2021-12-05 16:45:48 +08:00
|
|
|
} else if (phraseExpression[pos]=="(") {
|
|
|
|
//parse
|
2021-12-06 09:02:39 +08:00
|
|
|
int startPos = pos;
|
2021-12-05 16:45:48 +08:00
|
|
|
pos++;
|
2021-12-08 21:44:40 +08:00
|
|
|
// qDebug()<<"parse type cast ()";
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement evalType = doEvalExpression(
|
2021-12-06 09:02:39 +08:00
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement(),
|
2021-12-06 09:02:39 +08:00
|
|
|
true);
|
2021-12-08 21:44:40 +08:00
|
|
|
// qDebug()<<pos;
|
2021-12-05 16:45:48 +08:00
|
|
|
if (pos >= phraseExpression.length() || phraseExpression[pos]!=")") {
|
2021-12-06 11:37:37 +08:00
|
|
|
return PEvalStatement();
|
|
|
|
} else if (evalType &&
|
|
|
|
(evalType->kind == EvalStatementKind::Type)) {
|
2021-12-08 19:13:47 +08:00
|
|
|
pos++; // skip ")"
|
2021-12-08 21:44:40 +08:00
|
|
|
// qDebug()<<"parse type cast exp";
|
2021-12-06 09:02:39 +08:00
|
|
|
//it's a type cast
|
2021-12-06 11:37:37 +08:00
|
|
|
result = doEvalCCast(fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
2021-12-08 19:13:47 +08:00
|
|
|
if (result) {
|
2021-12-08 21:44:40 +08:00
|
|
|
// qDebug()<<"type cast";
|
2021-12-06 11:37:37 +08:00
|
|
|
result->assignType(evalType);
|
2021-12-08 19:13:47 +08:00
|
|
|
}
|
2021-12-06 09:02:39 +08:00
|
|
|
} else //it's not a type cast
|
2021-12-06 11:37:37 +08:00
|
|
|
result = doEvalMemberAccess(
|
2021-12-06 09:02:39 +08:00
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
startPos, //we must reparse it
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
2021-12-06 11:37:37 +08:00
|
|
|
} else
|
|
|
|
result = doEvalMemberAccess(
|
2021-12-06 09:02:39 +08:00
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
2021-12-08 21:44:40 +08:00
|
|
|
// if (result) {
|
|
|
|
// qDebug()<<pos<<(int)result->kind<<result->baseType;
|
|
|
|
// } else {
|
|
|
|
// qDebug()<<"!!!!!!!!!!!not found";
|
|
|
|
// }
|
2021-12-06 11:37:37 +08:00
|
|
|
return result;
|
2021-12-05 10:52:17 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
|
2022-12-11 19:47:43 +08:00
|
|
|
QStringList &phraseExpression,
|
2021-12-06 09:02:39 +08:00
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
const PEvalStatement& previousResult,
|
2023-03-11 17:32:57 +08:00
|
|
|
bool freeScoped) const
|
2021-12-05 10:52:17 +08:00
|
|
|
{
|
2021-12-08 19:13:47 +08:00
|
|
|
// qDebug()<<"eval member access "<<pos<<phraseExpression;
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement result;
|
2021-12-05 10:52:17 +08:00
|
|
|
if (pos>=phraseExpression.length())
|
2021-12-06 11:37:37 +08:00
|
|
|
return result;
|
2021-12-08 21:44:40 +08:00
|
|
|
PEvalStatement lastResult = previousResult;
|
2021-12-06 11:37:37 +08:00
|
|
|
result = doEvalScopeResolution(
|
2021-12-06 09:02:39 +08:00
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
2021-12-06 11:37:37 +08:00
|
|
|
if (!result)
|
|
|
|
return PEvalStatement();
|
2021-12-05 10:52:17 +08:00
|
|
|
while (pos<phraseExpression.length()) {
|
2021-12-06 11:37:37 +08:00
|
|
|
if (!result)
|
2021-12-06 09:02:39 +08:00
|
|
|
break;
|
2021-12-05 10:52:17 +08:00
|
|
|
if (phraseExpression[pos]=="++" || phraseExpression[pos]=="--") {
|
2021-12-06 11:37:37 +08:00
|
|
|
pos++; //just skip it
|
2021-12-05 10:52:17 +08:00
|
|
|
} else if (phraseExpression[pos] == "(") {
|
2021-12-06 11:37:37 +08:00
|
|
|
if (result->kind == EvalStatementKind::Type) {
|
2021-12-06 09:02:39 +08:00
|
|
|
pos++; // skip "("
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement newResult = doEvalExpression(
|
2021-12-06 09:02:39 +08:00
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement(),
|
2021-12-06 09:02:39 +08:00
|
|
|
true);
|
2021-12-06 11:37:37 +08:00
|
|
|
if (newResult)
|
|
|
|
newResult->assignType(result);
|
|
|
|
pos++; // skip ")"
|
|
|
|
result = newResult;
|
2021-12-07 08:23:27 +08:00
|
|
|
} else if (result->kind == EvalStatementKind::Function) {
|
2021-12-08 19:13:47 +08:00
|
|
|
doSkipInExpression(phraseExpression,pos,"(",")");
|
2021-12-08 21:44:40 +08:00
|
|
|
// qDebug()<<"????"<<(result->baseStatement!=nullptr)<<(lastResult!=nullptr);
|
2023-03-11 19:30:56 +08:00
|
|
|
if (result->baseStatement && lastResult) {
|
2021-12-08 21:44:40 +08:00
|
|
|
PStatement parentScope = result->baseStatement->parentScope.lock();
|
|
|
|
if (parentScope
|
|
|
|
&& STLElementMethods.contains(result->baseStatement->command)
|
|
|
|
) {
|
2023-03-11 19:56:40 +08:00
|
|
|
if (STLContainers.contains(parentScope->fullName)) {
|
|
|
|
//stl container methods
|
|
|
|
PStatement typeStatement = result->effectiveTypeStatement;
|
|
|
|
QString typeName;
|
|
|
|
if (!lastResult->definitionString.isEmpty())
|
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,lastResult->definitionString,parentScope);
|
|
|
|
else if (lastResult->baseStatement)
|
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,lastResult->baseStatement->type, parentScope);
|
|
|
|
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
|
|
|
|
if (!typeName.isEmpty())
|
|
|
|
typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-03-11 19:56:40 +08:00
|
|
|
result->definitionString = typeName;
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
lastResult = result;
|
|
|
|
} else {
|
|
|
|
return PEvalStatement();
|
|
|
|
}
|
|
|
|
} else if (STLMaps.contains(parentScope->fullName)) {
|
|
|
|
//stl map methods
|
|
|
|
PStatement typeStatement = result->effectiveTypeStatement;
|
|
|
|
QString typeName;
|
|
|
|
if (!lastResult->definitionString.isEmpty())
|
|
|
|
typeName = doFindTemplateParamOf(fileName,lastResult->definitionString,1,parentScope);
|
|
|
|
else if (lastResult->baseStatement)
|
|
|
|
typeName = doFindTemplateParamOf(fileName,lastResult->baseStatement->type,1, parentScope);
|
|
|
|
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
|
|
|
|
if (!typeName.isEmpty())
|
|
|
|
typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-03-11 19:56:40 +08:00
|
|
|
result->definitionString = typeName;
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
lastResult = result;
|
|
|
|
} else {
|
|
|
|
return PEvalStatement();
|
|
|
|
}
|
2021-12-08 21:44:40 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-11 19:56:40 +08:00
|
|
|
|
2021-12-08 21:44:40 +08:00
|
|
|
}
|
2021-12-06 11:37:37 +08:00
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
} else
|
|
|
|
result = PEvalStatement();
|
2021-12-05 10:52:17 +08:00
|
|
|
} else if (phraseExpression[pos] == "[") {
|
|
|
|
//skip to "]"
|
2021-12-08 19:13:47 +08:00
|
|
|
doSkipInExpression(phraseExpression,pos,"[","]");
|
2021-12-08 21:44:40 +08:00
|
|
|
if (result->pointerLevel>0)
|
|
|
|
result->pointerLevel--;
|
|
|
|
else {
|
|
|
|
PStatement typeStatement = result->effectiveTypeStatement;
|
|
|
|
if (typeStatement
|
|
|
|
&& result->kind == EvalStatementKind::Variable
|
|
|
|
&& result->baseStatement) {
|
2023-03-11 19:56:40 +08:00
|
|
|
if (STLContainers.contains(typeStatement->fullName)) {
|
|
|
|
PStatement parentScope = result->baseStatement->parentScope.lock();
|
|
|
|
QString typeName;
|
|
|
|
if (!lastResult || lastResult->definitionString.isEmpty())
|
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope);
|
|
|
|
else
|
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,lastResult->definitionString,parentScope);
|
|
|
|
typeStatement = doFindTypeDefinitionOf(fileName, typeName,
|
|
|
|
parentScope);
|
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-03-11 19:56:40 +08:00
|
|
|
result->definitionString=typeName;
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
lastResult = result;
|
|
|
|
} else {
|
|
|
|
return PEvalStatement();
|
|
|
|
}
|
|
|
|
} else if (STLMaps.contains(typeStatement->fullName)) {
|
|
|
|
PStatement parentScope = result->baseStatement->parentScope.lock();
|
|
|
|
QString typeName;
|
|
|
|
if (!lastResult || lastResult->definitionString.isEmpty())
|
|
|
|
typeName = doFindTemplateParamOf(fileName,result->baseStatement->type, 1,parentScope);
|
|
|
|
else
|
|
|
|
typeName = doFindTemplateParamOf(fileName,lastResult->definitionString,1,parentScope);
|
|
|
|
typeStatement = doFindTypeDefinitionOf(fileName, typeName,
|
|
|
|
parentScope);
|
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-03-11 19:56:40 +08:00
|
|
|
result->definitionString=typeName;
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
lastResult = result;
|
|
|
|
} else {
|
|
|
|
return PEvalStatement();
|
|
|
|
}
|
2021-12-08 21:44:40 +08:00
|
|
|
}
|
2023-03-11 19:56:40 +08:00
|
|
|
|
2023-03-11 19:30:56 +08:00
|
|
|
} else {
|
|
|
|
return PEvalStatement();
|
2021-12-08 21:44:40 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-05 10:52:17 +08:00
|
|
|
} else if (phraseExpression[pos] == ".") {
|
|
|
|
pos++;
|
2021-12-08 21:44:40 +08:00
|
|
|
lastResult = result;
|
2021-12-06 11:37:37 +08:00
|
|
|
result = doEvalScopeResolution(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
result,
|
|
|
|
false);
|
2021-12-05 10:52:17 +08:00
|
|
|
} else if (phraseExpression[pos] == "->") {
|
|
|
|
pos++;
|
2021-12-08 21:44:40 +08:00
|
|
|
if (result->pointerLevel==0) {
|
2023-06-08 17:15:55 +08:00
|
|
|
// iterator
|
|
|
|
if (result->typeStatement
|
|
|
|
&& STLIterators.contains(result->typeStatement->command)
|
|
|
|
) {
|
|
|
|
PStatement parentScope = result->typeStatement->parentScope.lock();
|
|
|
|
if (STLContainers.contains(parentScope->fullName)) {
|
|
|
|
QString typeName=doFindFirstTemplateParamOf(fileName,result->templateParams, parentScope);
|
|
|
|
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
|
|
|
|
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-06-08 17:15:55 +08:00
|
|
|
result->definitionString=typeName;
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
} else {
|
|
|
|
result = PEvalStatement();
|
|
|
|
}
|
|
|
|
} else if (STLMaps.contains(parentScope->fullName)) {
|
|
|
|
QString typeName=doFindTemplateParamOf(fileName,result->templateParams,1,parentScope);
|
|
|
|
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
|
|
|
|
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-06-08 17:15:55 +08:00
|
|
|
result->definitionString=typeName;
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
} else {
|
|
|
|
result = PEvalStatement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//smart pointer
|
|
|
|
PStatement typeStatement = result->effectiveTypeStatement;
|
|
|
|
if ((typeStatement)
|
|
|
|
&& STLPointers.contains(typeStatement->fullName)
|
|
|
|
&& result->kind == EvalStatementKind::Variable
|
|
|
|
&& result->baseStatement) {
|
|
|
|
PStatement parentScope = result->baseStatement->parentScope.lock();
|
|
|
|
QString typeName;
|
|
|
|
if (!previousResult || previousResult->definitionString.isEmpty())
|
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope);
|
|
|
|
else
|
|
|
|
typeName = doFindFirstTemplateParamOf(fileName,previousResult->definitionString,parentScope);
|
|
|
|
// qDebug()<<"typeName"<<typeName;
|
|
|
|
typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
2023-06-20 10:58:14 +08:00
|
|
|
result = doCreateEvalType(fileName,typeName,parentScope);
|
2023-06-08 17:15:55 +08:00
|
|
|
result->definitionString=typeName;
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
} else {
|
|
|
|
return PEvalStatement();
|
|
|
|
}
|
2021-12-08 21:44:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result->pointerLevel--;
|
|
|
|
}
|
|
|
|
lastResult = result;
|
2021-12-06 11:37:37 +08:00
|
|
|
result = doEvalScopeResolution(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
result,
|
|
|
|
false);
|
2021-12-05 10:52:17 +08:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
return result;
|
2021-12-05 10:52:17 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement CppParser::doEvalScopeResolution(const QString &fileName,
|
2022-12-11 19:47:43 +08:00
|
|
|
QStringList &phraseExpression,
|
2021-12-06 09:02:39 +08:00
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
const PEvalStatement& previousResult,
|
2023-03-11 17:32:57 +08:00
|
|
|
bool freeScoped) const
|
2021-12-05 10:52:17 +08:00
|
|
|
{
|
2021-12-08 19:13:47 +08:00
|
|
|
// qDebug()<<"eval scope res "<<pos<<phraseExpression;
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement result;
|
2021-12-05 10:52:17 +08:00
|
|
|
if (pos>=phraseExpression.length())
|
2021-12-06 11:37:37 +08:00
|
|
|
return result;
|
|
|
|
result = doEvalTerm(
|
|
|
|
fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
previousResult,
|
|
|
|
freeScoped);
|
2021-12-05 10:52:17 +08:00
|
|
|
while (pos<phraseExpression.length()) {
|
|
|
|
if (phraseExpression[pos]=="::" ) {
|
|
|
|
pos++;
|
2021-12-06 11:37:37 +08:00
|
|
|
if (!result) {
|
2021-12-08 19:55:15 +08:00
|
|
|
//global
|
|
|
|
result = doEvalTerm(fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
PStatement(),
|
|
|
|
PEvalStatement(),
|
|
|
|
false);
|
2021-12-06 11:37:37 +08:00
|
|
|
} else if (result->kind == EvalStatementKind::Type) {
|
2021-12-08 19:55:15 +08:00
|
|
|
//class static member
|
|
|
|
result = doEvalTerm(fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
result,
|
|
|
|
false);
|
2021-12-06 11:37:37 +08:00
|
|
|
} else if (result->kind == EvalStatementKind::Namespace) {
|
2021-12-08 19:55:15 +08:00
|
|
|
//namespace
|
|
|
|
result = doEvalTerm(fileName,
|
|
|
|
phraseExpression,
|
|
|
|
pos,
|
|
|
|
scope,
|
|
|
|
result,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
if (!result)
|
|
|
|
break;
|
2021-12-05 10:52:17 +08:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
// qDebug()<<pos<<"scope end";
|
2021-12-06 11:37:37 +08:00
|
|
|
return result;
|
2021-12-05 10:52:17 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement CppParser::doEvalTerm(const QString &fileName,
|
2022-12-11 19:47:43 +08:00
|
|
|
QStringList &phraseExpression,
|
2021-12-06 09:02:39 +08:00
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
const PEvalStatement& previousResult,
|
2023-03-11 17:32:57 +08:00
|
|
|
bool freeScoped) const
|
2021-12-05 10:52:17 +08:00
|
|
|
{
|
2021-12-08 21:44:40 +08:00
|
|
|
// if (previousResult) {
|
|
|
|
// qDebug()<<"eval term "<<pos<<phraseExpression<<previousResult->baseType<<freeScoped;
|
|
|
|
// } else {
|
|
|
|
// qDebug()<<"eval term "<<pos<<phraseExpression<<"no type"<<freeScoped;
|
|
|
|
// }
|
2021-12-06 11:37:37 +08:00
|
|
|
PEvalStatement result;
|
2021-12-05 10:52:17 +08:00
|
|
|
if (pos>=phraseExpression.length())
|
2021-12-06 11:37:37 +08:00
|
|
|
return result;
|
2022-12-11 19:47:43 +08:00
|
|
|
// qDebug()<<"eval term"<<phraseExpression[pos];
|
2021-12-05 10:52:17 +08:00
|
|
|
if (phraseExpression[pos]=="(") {
|
|
|
|
pos++;
|
2021-12-07 08:23:27 +08:00
|
|
|
result = doEvalExpression(fileName,phraseExpression,pos,scope,PEvalStatement(),freeScoped);
|
2021-12-05 10:52:17 +08:00
|
|
|
if (pos >= phraseExpression.length() || phraseExpression[pos]!=")")
|
2021-12-06 11:37:37 +08:00
|
|
|
return PEvalStatement();
|
|
|
|
else {
|
|
|
|
pos++; // skip ")";
|
|
|
|
return result;
|
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
} else {
|
|
|
|
int pointerLevel = 0;
|
|
|
|
//skip "struct", "const", "static", etc
|
|
|
|
while(pos < phraseExpression.length()) {
|
|
|
|
QString token = phraseExpression[pos];
|
|
|
|
if (token=="*") // for expression like (const * char)?
|
|
|
|
pointerLevel++;
|
|
|
|
else if (mCppTypeKeywords.contains(token)
|
|
|
|
|| !mCppKeywords.contains(token))
|
|
|
|
break;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos>=phraseExpression.length() || phraseExpression[pos]==")")
|
|
|
|
return result;
|
|
|
|
if (mCppKeywords.contains(phraseExpression[pos])) {
|
|
|
|
result = doCreateEvalType(phraseExpression[pos]);
|
|
|
|
pos++;
|
|
|
|
} else if (isIdentifier(phraseExpression[pos])) {
|
|
|
|
PStatement statement;
|
|
|
|
if (freeScoped) {
|
|
|
|
if (!previousResult) {
|
|
|
|
statement = findStatementStartingFrom(
|
|
|
|
fileName,
|
|
|
|
phraseExpression[pos],
|
|
|
|
scope);
|
|
|
|
} else {
|
|
|
|
statement = findStatementStartingFrom(
|
|
|
|
fileName,
|
|
|
|
phraseExpression[pos],
|
|
|
|
previousResult->effectiveTypeStatement);
|
|
|
|
}
|
2021-12-07 14:48:20 +08:00
|
|
|
} else {
|
2021-12-08 19:13:47 +08:00
|
|
|
if (!previousResult) {
|
|
|
|
statement = findStatementInScope(phraseExpression[pos],PStatement());
|
|
|
|
} else {
|
|
|
|
statement = findStatementInScope(phraseExpression[pos],previousResult->effectiveTypeStatement);
|
|
|
|
}
|
2022-12-11 19:47:43 +08:00
|
|
|
// if (!statement) {
|
|
|
|
// statement = findMacro(
|
|
|
|
// phraseExpression[pos],
|
|
|
|
// fileName);
|
|
|
|
// }
|
2021-12-07 14:48:20 +08:00
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
pos++;
|
2022-01-25 16:11:16 +08:00
|
|
|
if (statement && statement->kind == StatementKind::skConstructor) {
|
|
|
|
statement = statement->parentScope.lock();
|
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
if (statement) {
|
|
|
|
switch (statement->kind) {
|
|
|
|
case StatementKind::skNamespace:
|
|
|
|
result = doCreateEvalNamespace(statement);
|
|
|
|
break;
|
2021-12-08 21:44:40 +08:00
|
|
|
case StatementKind::skAlias: {
|
2023-03-11 17:32:57 +08:00
|
|
|
statement = doFindAliasedStatement(statement);
|
2021-12-08 21:44:40 +08:00
|
|
|
if (statement)
|
2023-05-31 20:06:52 +08:00
|
|
|
result = doCreateEvalType(fileName,statement);
|
2021-12-08 21:44:40 +08:00
|
|
|
}
|
|
|
|
break;
|
2021-12-08 19:13:47 +08:00
|
|
|
case StatementKind::skVariable:
|
|
|
|
case StatementKind::skParameter:
|
2022-11-29 16:48:40 +08:00
|
|
|
result = doCreateEvalVariable(fileName,statement, previousResult?previousResult->templateParams:"",scope);
|
2021-12-08 19:13:47 +08:00
|
|
|
break;
|
|
|
|
case StatementKind::skEnumType:
|
|
|
|
case StatementKind::skClass:
|
|
|
|
case StatementKind::skEnumClassType:
|
|
|
|
case StatementKind::skTypedef:
|
|
|
|
result = doCreateEvalType(fileName,statement);
|
|
|
|
break;
|
|
|
|
case StatementKind::skFunction:
|
|
|
|
result = doCreateEvalFunction(fileName,statement);
|
|
|
|
break;
|
2022-12-11 19:47:43 +08:00
|
|
|
case StatementKind::skPreprocessor:
|
2023-07-01 13:41:27 +08:00
|
|
|
// qDebug()<<"before"<<phraseExpression;
|
2022-12-11 19:47:43 +08:00
|
|
|
pos--;
|
|
|
|
if (expandMacro(phraseExpression,pos,statement)) {
|
2023-07-01 13:41:27 +08:00
|
|
|
// qDebug()<<"after"<<phraseExpression;
|
2022-12-11 19:47:43 +08:00
|
|
|
result = doEvalExpression(fileName,phraseExpression,pos,scope,previousResult,freeScoped);
|
|
|
|
} else
|
|
|
|
result = PEvalStatement();
|
|
|
|
break;
|
2021-12-08 19:13:47 +08:00
|
|
|
default:
|
|
|
|
result = PEvalStatement();
|
|
|
|
}
|
2022-11-29 12:06:29 +08:00
|
|
|
if (result
|
|
|
|
&& result->typeStatement
|
|
|
|
&& STLIterators.contains(result->typeStatement->command)
|
|
|
|
&& previousResult) {
|
|
|
|
PStatement parentStatement = result->typeStatement->parentScope.lock();
|
|
|
|
if (parentStatement
|
|
|
|
&& STLContainers.contains(parentStatement->fullName)) {
|
|
|
|
result->templateParams = previousResult->templateParams;
|
2023-03-11 19:56:40 +08:00
|
|
|
} else if (parentStatement
|
|
|
|
&& STLMaps.contains(parentStatement->fullName)) {
|
|
|
|
result->templateParams = previousResult->templateParams;
|
2022-11-29 12:06:29 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-07 14:48:20 +08:00
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
} else if (isIntegerLiteral(phraseExpression[pos])) {
|
|
|
|
result = doCreateEvalLiteral("int");
|
2021-12-08 19:55:15 +08:00
|
|
|
pos++;
|
2021-12-08 19:13:47 +08:00
|
|
|
} else if (isFloatLiteral(phraseExpression[pos])) {
|
|
|
|
result = doCreateEvalLiteral("double");
|
2021-12-08 19:55:15 +08:00
|
|
|
pos++;
|
2021-12-08 19:13:47 +08:00
|
|
|
} else if (isStringLiteral(phraseExpression[pos])) {
|
|
|
|
result = doCreateEvalLiteral("char");
|
|
|
|
result->pointerLevel = 1;
|
2021-12-08 19:55:15 +08:00
|
|
|
pos++;
|
2021-12-08 19:13:47 +08:00
|
|
|
} else if (isCharLiteral(phraseExpression[pos])) {
|
|
|
|
result = doCreateEvalLiteral("char");
|
2021-12-08 19:55:15 +08:00
|
|
|
pos++;
|
2021-12-08 19:13:47 +08:00
|
|
|
} else
|
2021-12-08 19:55:15 +08:00
|
|
|
return result;
|
2021-12-08 21:44:40 +08:00
|
|
|
// if (result) {
|
|
|
|
// qDebug()<<"term kind:"<<(int)result->kind;
|
|
|
|
// }
|
2021-12-08 19:13:47 +08:00
|
|
|
if (result && result->kind == EvalStatementKind::Type) {
|
|
|
|
//skip "struct", "const", "static", etc
|
|
|
|
while(pos < phraseExpression.length()) {
|
|
|
|
QString token = phraseExpression[pos];
|
|
|
|
if (token=="*") // for expression like (const * char)?
|
|
|
|
pointerLevel++;
|
|
|
|
else if (mCppTypeKeywords.contains(token)
|
|
|
|
|| !mCppKeywords.contains(token))
|
|
|
|
break;
|
|
|
|
pos++;
|
2021-12-07 14:48:20 +08:00
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
result->pointerLevel = pointerLevel;
|
2021-12-07 14:48:20 +08:00
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
}
|
|
|
|
// qDebug()<<pos<<" term end";
|
|
|
|
// if (!result) {
|
|
|
|
// qDebug()<<"not found !!!!";
|
|
|
|
// }
|
2022-11-29 12:06:29 +08:00
|
|
|
|
2021-12-08 19:13:47 +08:00
|
|
|
return result;
|
2021-12-04 18:38:54 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
bool CppParser::expandMacro(QStringList &phraseExpression, int &pos, const PStatement ¯o) const
|
2022-12-11 19:47:43 +08:00
|
|
|
{
|
|
|
|
QString s;
|
|
|
|
if (macro->args.isEmpty()) {
|
|
|
|
s=macro->value;
|
|
|
|
phraseExpression.removeAt(pos);
|
|
|
|
} else {
|
|
|
|
QString args=macro->args.mid(1,macro->args.length()-2).trimmed(); // remove '(' ')'
|
|
|
|
|
|
|
|
if(args=="")
|
|
|
|
return false;
|
|
|
|
QStringList argList = args.split(',');
|
|
|
|
QList<bool> argUsed;
|
|
|
|
for (int i=0;i<argList.size();i++) {
|
|
|
|
argList[i]=argList[i].trimmed();
|
|
|
|
argUsed.append(false);
|
|
|
|
}
|
|
|
|
QList<PDefineArgToken> tokens = mPreprocessor.tokenizeValue(macro->value);
|
|
|
|
|
|
|
|
QString formatStr = "";
|
|
|
|
DefineArgTokenType lastTokenType=DefineArgTokenType::Other;
|
|
|
|
int index;
|
|
|
|
foreach (const PDefineArgToken& token, tokens) {
|
|
|
|
switch(token->type) {
|
|
|
|
case DefineArgTokenType::Identifier:
|
|
|
|
index = argList.indexOf(token->value);
|
|
|
|
if (index>=0) {
|
|
|
|
argUsed[index] = true;
|
|
|
|
if (lastTokenType == DefineArgTokenType::Sharp) {
|
|
|
|
formatStr+= "\"%"+QString("%1").arg(index+1)+"\"";
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
formatStr+= "%"+QString("%1").arg(index+1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
formatStr += token->value;
|
|
|
|
break;
|
|
|
|
case DefineArgTokenType::DSharp:
|
|
|
|
case DefineArgTokenType::Sharp:
|
|
|
|
break;
|
|
|
|
case DefineArgTokenType::Space:
|
|
|
|
case DefineArgTokenType::Symbol:
|
|
|
|
formatStr+=token->value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastTokenType = token->type;
|
|
|
|
}
|
|
|
|
QStringList values;
|
|
|
|
int i;
|
|
|
|
int level=1;
|
|
|
|
QString current;
|
|
|
|
for (i=pos+2;i<phraseExpression.length();i++) {
|
|
|
|
if (phraseExpression[i]==')') {
|
|
|
|
level--;
|
|
|
|
if (level==0) {
|
|
|
|
values.append(current);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (phraseExpression[i]=='(') {
|
|
|
|
level++;
|
|
|
|
} else {
|
|
|
|
if (level==1 && phraseExpression[i]==',') {
|
|
|
|
values.append(current);
|
|
|
|
current.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current+=phraseExpression[i];
|
|
|
|
}
|
|
|
|
if (level!=0)
|
|
|
|
return false;
|
|
|
|
if (values.length()!=argList.length())
|
|
|
|
return false;
|
|
|
|
s = formatStr;
|
|
|
|
for (int i=0;i<values.length();i++) {
|
|
|
|
if (argUsed[i]) {
|
|
|
|
QString argValue = values[i];
|
|
|
|
s=s.arg(argValue.trimmed());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int j=pos;j<=i;j++) {
|
|
|
|
phraseExpression.removeAt(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSynedit::CppSyntaxer syntaxer;
|
|
|
|
syntaxer.resetState();
|
|
|
|
syntaxer.setLine(s,0);
|
|
|
|
int i=0;
|
|
|
|
while (!syntaxer.eol()) {
|
|
|
|
QString token=syntaxer.getToken();
|
|
|
|
QSynedit::PTokenAttribute attr = syntaxer.getTokenAttribute();
|
|
|
|
switch(attr->tokenType()) {
|
|
|
|
case QSynedit::TokenType::Space:
|
|
|
|
case QSynedit::TokenType::Comment:
|
|
|
|
break;
|
2023-07-01 13:41:27 +08:00
|
|
|
case QSynedit::TokenType::Identifier:
|
|
|
|
if (token!=macro->command)
|
|
|
|
phraseExpression.insert(pos+i,token);
|
|
|
|
else
|
|
|
|
phraseExpression.insert(pos+i,token+"_expanded");
|
|
|
|
i++;
|
|
|
|
break;
|
2022-12-11 19:47:43 +08:00
|
|
|
default:
|
|
|
|
phraseExpression.insert(pos+i,token);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
syntaxer.next();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalNamespace(const PStatement &namespaceStatement) const
|
2021-12-07 14:48:20 +08:00
|
|
|
{
|
|
|
|
if (!namespaceStatement)
|
|
|
|
return PEvalStatement();
|
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
namespaceStatement->fullName,
|
|
|
|
EvalStatementKind::Namespace,
|
2021-12-08 19:13:47 +08:00
|
|
|
PStatement(),
|
2022-11-29 12:06:29 +08:00
|
|
|
namespaceStatement,
|
2021-12-08 19:13:47 +08:00
|
|
|
namespaceStatement);
|
2021-12-07 14:48:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-20 10:58:14 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalType(const QString &fileName, const QString &typeName, const PStatement& parentScope) const
|
|
|
|
{
|
|
|
|
QString baseType;
|
|
|
|
PStatement typeStatement;
|
|
|
|
int pointerLevel=0;
|
|
|
|
QString templateParams;
|
|
|
|
PStatement effectiveTypeStatement = doParseEvalTypeInfo(
|
|
|
|
fileName,
|
|
|
|
parentScope,
|
|
|
|
typeName,
|
|
|
|
baseType,
|
|
|
|
typeStatement,
|
|
|
|
pointerLevel,
|
|
|
|
templateParams);
|
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
baseType,
|
|
|
|
EvalStatementKind::Type,
|
|
|
|
PStatement(),
|
|
|
|
typeStatement,
|
|
|
|
effectiveTypeStatement,
|
|
|
|
pointerLevel,
|
|
|
|
templateParams
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalType(const QString& fileName,const PStatement &typeStatement) const
|
2021-12-07 08:23:27 +08:00
|
|
|
{
|
2021-12-07 14:48:20 +08:00
|
|
|
if (!typeStatement)
|
|
|
|
return PEvalStatement();
|
|
|
|
if (typeStatement->kind == StatementKind::skTypedef) {
|
|
|
|
QString baseType;
|
|
|
|
int pointerLevel=0;
|
2022-11-29 12:06:29 +08:00
|
|
|
QString templateParams;
|
|
|
|
PStatement tempStatement;
|
|
|
|
PStatement effetiveTypeStatement = doParseEvalTypeInfo(
|
2021-12-07 14:48:20 +08:00
|
|
|
fileName,
|
|
|
|
typeStatement->parentScope.lock(),
|
2021-12-08 21:44:40 +08:00
|
|
|
typeStatement->type + typeStatement->args,
|
2021-12-07 14:48:20 +08:00
|
|
|
baseType,
|
2022-11-29 12:06:29 +08:00
|
|
|
tempStatement,
|
|
|
|
pointerLevel,
|
|
|
|
templateParams);
|
2021-12-07 14:48:20 +08:00
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
baseType,
|
|
|
|
EvalStatementKind::Type,
|
|
|
|
PStatement(),
|
2022-11-29 12:06:29 +08:00
|
|
|
typeStatement,
|
|
|
|
effetiveTypeStatement,
|
|
|
|
pointerLevel,
|
|
|
|
templateParams
|
2021-12-07 14:48:20 +08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return std::make_shared<EvalStatement>(
|
2021-12-07 08:23:27 +08:00
|
|
|
typeStatement->fullName,
|
|
|
|
EvalStatementKind::Type,
|
2023-03-11 19:30:56 +08:00
|
|
|
typeStatement,
|
2022-11-29 12:06:29 +08:00
|
|
|
typeStatement,
|
2021-12-08 19:13:47 +08:00
|
|
|
typeStatement);
|
2021-12-07 14:48:20 +08:00
|
|
|
}
|
2021-12-07 08:23:27 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalType(const QString &primitiveType) const
|
2021-12-08 19:13:47 +08:00
|
|
|
{
|
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
primitiveType,
|
|
|
|
EvalStatementKind::Type,
|
|
|
|
PStatement(),
|
2022-11-29 12:06:29 +08:00
|
|
|
PStatement(),
|
2021-12-08 19:13:47 +08:00
|
|
|
PStatement());
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:48:40 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalVariable(
|
|
|
|
const QString &fileName,
|
|
|
|
const PStatement& varStatement,
|
|
|
|
const QString& baseTemplateParams,
|
2023-03-11 17:32:57 +08:00
|
|
|
const PStatement& scope) const
|
2021-12-07 08:23:27 +08:00
|
|
|
{
|
2021-12-07 14:48:20 +08:00
|
|
|
if (!varStatement)
|
|
|
|
return PEvalStatement();
|
2021-12-07 08:23:27 +08:00
|
|
|
QString baseType;
|
|
|
|
int pointerLevel=0;
|
2022-11-29 12:06:29 +08:00
|
|
|
QString templateParams;
|
|
|
|
PStatement typeStatement;
|
2022-11-29 16:48:40 +08:00
|
|
|
PStatement effectiveTypeStatement;
|
|
|
|
//todo: ugly implementation for std::pair
|
|
|
|
if (varStatement->fullName == "std::pair::first") {
|
|
|
|
effectiveTypeStatement = doParseEvalTypeInfo(
|
|
|
|
fileName,
|
|
|
|
scope,
|
|
|
|
doFindFirstTemplateParamOf(fileName,baseTemplateParams,scope),
|
|
|
|
baseType,
|
|
|
|
typeStatement,
|
|
|
|
pointerLevel,
|
|
|
|
templateParams);
|
|
|
|
} else if (varStatement->fullName == "std::pair::second") {
|
|
|
|
effectiveTypeStatement = doParseEvalTypeInfo(
|
|
|
|
fileName,
|
|
|
|
scope,
|
|
|
|
doFindTemplateParamOf(fileName,baseTemplateParams,1,scope),
|
|
|
|
baseType,
|
|
|
|
typeStatement,
|
|
|
|
pointerLevel,
|
|
|
|
templateParams);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
effectiveTypeStatement = doParseEvalTypeInfo(
|
2021-12-07 08:23:27 +08:00
|
|
|
fileName,
|
|
|
|
varStatement->parentScope.lock(),
|
2022-11-29 12:06:29 +08:00
|
|
|
varStatement->type+varStatement->args,
|
2021-12-07 08:23:27 +08:00
|
|
|
baseType,
|
2022-11-29 12:06:29 +08:00
|
|
|
typeStatement,
|
|
|
|
pointerLevel,
|
|
|
|
templateParams);
|
2022-11-29 16:48:40 +08:00
|
|
|
}
|
2023-06-20 19:26:28 +08:00
|
|
|
// if (!varStatement->args.isEmpty()) {
|
|
|
|
// int j = 0;
|
|
|
|
// while ((j = varStatement->args.indexOf("[", j)) != -1) {
|
|
|
|
// ++j;
|
|
|
|
// pointerLevel++;
|
|
|
|
// }
|
|
|
|
// }
|
2021-12-08 21:44:40 +08:00
|
|
|
// qDebug()<<"parse ..."<<baseType<<pointerLevel;
|
2021-12-07 14:48:20 +08:00
|
|
|
return std::make_shared<EvalStatement>(
|
2021-12-07 08:23:27 +08:00
|
|
|
baseType,
|
|
|
|
EvalStatementKind::Variable,
|
|
|
|
varStatement,
|
|
|
|
typeStatement,
|
2022-11-29 16:48:40 +08:00
|
|
|
effectiveTypeStatement,
|
2022-11-29 12:06:29 +08:00
|
|
|
pointerLevel,
|
|
|
|
templateParams
|
2021-12-07 08:23:27 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:48:40 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalFunction(
|
|
|
|
const QString &fileName,
|
2023-03-11 17:32:57 +08:00
|
|
|
const PStatement& funcStatement) const
|
2021-12-07 14:48:20 +08:00
|
|
|
{
|
|
|
|
if (!funcStatement)
|
|
|
|
return PEvalStatement();
|
|
|
|
QString baseType;
|
|
|
|
int pointerLevel=0;
|
2022-11-29 12:06:29 +08:00
|
|
|
QString templateParams;
|
|
|
|
PStatement typeStatement;
|
|
|
|
PStatement effetiveTypeStatement = doParseEvalTypeInfo(
|
2021-12-07 14:48:20 +08:00
|
|
|
fileName,
|
|
|
|
funcStatement->parentScope.lock(),
|
|
|
|
funcStatement->type,
|
|
|
|
baseType,
|
2022-11-29 12:06:29 +08:00
|
|
|
typeStatement,
|
|
|
|
pointerLevel,
|
|
|
|
templateParams);
|
2021-12-07 14:48:20 +08:00
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
baseType,
|
|
|
|
EvalStatementKind::Function,
|
|
|
|
funcStatement,
|
|
|
|
typeStatement,
|
2022-11-29 12:06:29 +08:00
|
|
|
effetiveTypeStatement,
|
|
|
|
pointerLevel,
|
|
|
|
templateParams
|
2021-12-07 14:48:20 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalLiteral(const QString &type) const
|
2021-12-08 19:13:47 +08:00
|
|
|
{
|
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
type,
|
|
|
|
EvalStatementKind::Literal,
|
|
|
|
PStatement(),
|
2022-11-29 12:06:29 +08:00
|
|
|
PStatement(),
|
2021-12-08 19:13:47 +08:00
|
|
|
PStatement());
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
void CppParser::doSkipInExpression(const QStringList &expression, int &pos, const QString &startSymbol, const QString &endSymbol) const
|
2021-12-08 19:13:47 +08:00
|
|
|
{
|
|
|
|
int level = 0;
|
|
|
|
while (pos<expression.length()) {
|
|
|
|
QString token = expression[pos];
|
|
|
|
if (token == startSymbol) {
|
|
|
|
level++;
|
|
|
|
} else if (token == endSymbol) {
|
|
|
|
level--;
|
2021-12-08 21:44:40 +08:00
|
|
|
if (level==0) {
|
|
|
|
pos++;
|
2021-12-08 19:13:47 +08:00
|
|
|
return;
|
2021-12-08 21:44:40 +08:00
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 11:29:30 +08:00
|
|
|
QString CppParser::findFunctionPointerName(int startIdx)
|
|
|
|
{
|
|
|
|
Q_ASSERT(mTokenizer[startIdx]->text=="(");
|
|
|
|
int i=startIdx+1;
|
|
|
|
int endIdx = mTokenizer[startIdx]->matchIndex;
|
|
|
|
while (i<endIdx) {
|
|
|
|
if (isIdentChar(mTokenizer[i]->text[0])) {
|
|
|
|
return mTokenizer[i]->text;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2021-12-07 08:23:27 +08:00
|
|
|
PStatement CppParser::doParseEvalTypeInfo(
|
|
|
|
const QString &fileName,
|
|
|
|
const PStatement &scope,
|
|
|
|
const QString &type,
|
|
|
|
QString &baseType,
|
2022-11-29 12:06:29 +08:00
|
|
|
PStatement& typeStatement,
|
|
|
|
int &pointerLevel,
|
2023-03-11 17:32:57 +08:00
|
|
|
QString& templateParams) const
|
2021-12-07 08:23:27 +08:00
|
|
|
{
|
|
|
|
// Remove pointer stuff from type
|
2021-12-08 21:44:40 +08:00
|
|
|
QString s = type;
|
|
|
|
// qDebug()<<"eval type info"<<type;
|
2021-12-07 08:23:27 +08:00
|
|
|
int position = s.length()-1;
|
2022-12-10 21:23:49 +08:00
|
|
|
QSynedit::CppSyntaxer syntaxer;
|
|
|
|
syntaxer.resetState();
|
|
|
|
syntaxer.setLine(type,0);
|
2021-12-07 08:23:27 +08:00
|
|
|
int bracketLevel = 0;
|
|
|
|
int templateLevel = 0;
|
2022-12-10 21:23:49 +08:00
|
|
|
while(!syntaxer.eol()) {
|
|
|
|
QString token = syntaxer.getToken();
|
2021-12-07 08:23:27 +08:00
|
|
|
if (bracketLevel == 0 && templateLevel ==0) {
|
|
|
|
if (token == "*")
|
|
|
|
pointerLevel++;
|
2022-12-10 21:23:49 +08:00
|
|
|
else if (syntaxer.getTokenAttribute()->tokenType() == QSynedit::TokenType::Identifier) {
|
2022-11-23 12:51:23 +08:00
|
|
|
baseType += token;
|
2021-12-07 08:23:27 +08:00
|
|
|
} else if (token == "[") {
|
2021-12-08 21:44:40 +08:00
|
|
|
pointerLevel++;
|
2021-12-07 08:23:27 +08:00
|
|
|
bracketLevel++;
|
|
|
|
} else if (token == "<") {
|
|
|
|
templateLevel++;
|
2022-11-29 12:06:29 +08:00
|
|
|
templateParams += token;
|
2021-12-08 19:55:15 +08:00
|
|
|
} else if (token == "::") {
|
|
|
|
baseType += token;
|
2021-12-07 08:23:27 +08:00
|
|
|
}
|
|
|
|
} else if (bracketLevel > 0) {
|
|
|
|
if (token == "[") {
|
|
|
|
bracketLevel++;
|
|
|
|
} else if (token == "]") {
|
2021-12-08 21:44:40 +08:00
|
|
|
bracketLevel--;
|
2021-12-07 08:23:27 +08:00
|
|
|
}
|
|
|
|
} else if (templateLevel > 0) {
|
|
|
|
if (token == "<") {
|
|
|
|
templateLevel++;
|
|
|
|
} else if (token == ">") {
|
2021-12-08 21:44:40 +08:00
|
|
|
templateLevel--;
|
2021-12-07 08:23:27 +08:00
|
|
|
}
|
2022-11-29 12:06:29 +08:00
|
|
|
templateParams += token;
|
2021-12-07 08:23:27 +08:00
|
|
|
}
|
2022-12-10 21:23:49 +08:00
|
|
|
syntaxer.next();
|
2021-12-07 08:23:27 +08:00
|
|
|
}
|
|
|
|
while ((position >= 0) && (s[position] == '*'
|
|
|
|
|| s[position] == ' '
|
|
|
|
|| s[position] == '&')) {
|
|
|
|
if (s[position]=='*') {
|
|
|
|
|
|
|
|
}
|
|
|
|
position--;
|
|
|
|
}
|
2023-03-11 17:32:57 +08:00
|
|
|
typeStatement = doFindStatementOf(fileName,baseType,scope);
|
2023-06-20 10:58:14 +08:00
|
|
|
PStatement effectiveTypeStatement = typeStatement;
|
2023-06-20 09:57:57 +08:00
|
|
|
int level=0;
|
2023-06-20 10:58:14 +08:00
|
|
|
while (effectiveTypeStatement && (effectiveTypeStatement->kind == StatementKind::skTypedef
|
|
|
|
|| effectiveTypeStatement->kind == StatementKind::skPreprocessor)) {
|
|
|
|
if (level >20) // prevent infinite loop
|
|
|
|
break;
|
|
|
|
level++;
|
|
|
|
baseType="";
|
|
|
|
syntaxer.resetState();
|
2023-06-20 19:26:28 +08:00
|
|
|
syntaxer.setLine(effectiveTypeStatement->type+effectiveTypeStatement->args,0);
|
2023-06-20 10:58:14 +08:00
|
|
|
int bracketLevel = 0;
|
|
|
|
int templateLevel = 0;
|
|
|
|
while(!syntaxer.eol()) {
|
|
|
|
QString token = syntaxer.getToken();
|
|
|
|
if (bracketLevel == 0 && templateLevel ==0) {
|
|
|
|
if (token == "*")
|
|
|
|
pointerLevel++;
|
|
|
|
else if (syntaxer.getTokenAttribute()->tokenType() == QSynedit::TokenType::Identifier) {
|
|
|
|
baseType += token;
|
|
|
|
} else if (token == "[") {
|
|
|
|
pointerLevel++;
|
|
|
|
bracketLevel++;
|
|
|
|
} else if (token == "<") {
|
|
|
|
templateLevel++;
|
|
|
|
templateParams += token;
|
|
|
|
} else if (token == "::") {
|
|
|
|
baseType += token;
|
|
|
|
}
|
|
|
|
} else if (bracketLevel > 0) {
|
|
|
|
if (token == "[") {
|
|
|
|
bracketLevel++;
|
|
|
|
} else if (token == "]") {
|
|
|
|
bracketLevel--;
|
|
|
|
}
|
|
|
|
} else if (templateLevel > 0) {
|
|
|
|
if (token == "<") {
|
|
|
|
templateLevel++;
|
|
|
|
} else if (token == ">") {
|
|
|
|
templateLevel--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
syntaxer.next();
|
|
|
|
}
|
|
|
|
effectiveTypeStatement = doFindStatementOf(fileName,baseType, effectiveTypeStatement->parentScope.lock());
|
|
|
|
}
|
|
|
|
effectiveTypeStatement = getTypeDef(effectiveTypeStatement,fileName,baseType);
|
|
|
|
return effectiveTypeStatement;
|
2021-12-07 08:23:27 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
int CppParser::getBracketEnd(const QString &s, int startAt) const
|
2021-08-22 21:23:58 +08:00
|
|
|
{
|
|
|
|
int i = startAt;
|
|
|
|
int level = 0; // assume we start on top of [
|
|
|
|
while (i < s.length()) {
|
|
|
|
switch(s[i].unicode()) {
|
|
|
|
case '<':
|
|
|
|
level++;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
level--;
|
|
|
|
if (level == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return startAt;
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::doFindStatementInScope(const QString &name,
|
|
|
|
const QString &noNameArgs,
|
|
|
|
StatementKind kind,
|
2023-03-11 19:30:56 +08:00
|
|
|
const PStatement& scope) const
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
2023-03-11 19:30:56 +08:00
|
|
|
const StatementMap& statementMap = mStatementList.childrenStatements(scope);
|
2021-08-16 00:47:35 +08:00
|
|
|
|
2021-08-29 10:14:07 +08:00
|
|
|
foreach (const PStatement& statement, statementMap.values(name)) {
|
2021-08-16 00:47:35 +08:00
|
|
|
if (statement->kind == kind && statement->noNameArgs == noNameArgs) {
|
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:08:01 +08:00
|
|
|
void CppParser::internalInvalidateFile(const QString &fileName)
|
|
|
|
{
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2022-10-23 10:40:00 +08:00
|
|
|
// remove its include files list
|
|
|
|
PFileIncludes p = findFileIncludes(fileName, true);
|
|
|
|
if (p) {
|
|
|
|
//fPreprocessor.InvalidDefinesInFile(FileName); //we don't need this, since we reset defines after each parse
|
|
|
|
//p->includeFiles.clear();
|
|
|
|
//p->usings.clear();
|
|
|
|
for (PStatement& statement:p->statements) {
|
|
|
|
if (statement->fileName==fileName) {
|
|
|
|
mStatementList.deleteStatement(statement);
|
|
|
|
} else {
|
2022-11-16 10:29:20 +08:00
|
|
|
statement->setHasDefinition(false);
|
2022-10-23 10:40:00 +08:00
|
|
|
statement->definitionFileName = statement->fileName;
|
|
|
|
statement->definitionLine = statement->line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p->statements.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//remove all statements from namespace cache
|
2021-08-29 00:48:23 +08:00
|
|
|
const QList<QString>& keys=mNamespaces.keys();
|
|
|
|
for (const QString& key:keys) {
|
2021-08-19 17:08:01 +08:00
|
|
|
PStatementList statements = mNamespaces.value(key);
|
|
|
|
for (int i=statements->size()-1;i>=0;i--) {
|
|
|
|
PStatement statement = statements->at(i);
|
2022-10-23 10:40:00 +08:00
|
|
|
if (statement->fileName == fileName) {
|
2021-08-19 17:08:01 +08:00
|
|
|
statements->removeAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (statements->isEmpty()) {
|
|
|
|
mNamespaces.remove(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 19:33:20 +08:00
|
|
|
// delete it from scannedfiles
|
|
|
|
mPreprocessor.removeScannedFile(fileName);
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
void CppParser::internalInvalidateFiles(const QSet<QString> &files)
|
2021-08-19 17:08:01 +08:00
|
|
|
{
|
2021-08-27 23:51:42 +08:00
|
|
|
for (const QString& file:files)
|
2021-08-19 17:08:01 +08:00
|
|
|
internalInvalidateFile(file);
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
QSet<QString> CppParser::calculateFilesToBeReparsed(const QString &fileName)
|
2021-08-15 16:49:37 +08:00
|
|
|
{
|
|
|
|
if (fileName.isEmpty())
|
2021-08-22 21:23:58 +08:00
|
|
|
return QSet<QString>();
|
2022-10-22 19:33:20 +08:00
|
|
|
QSet<QString> result;
|
|
|
|
result.insert(fileName);
|
2022-10-23 23:06:55 +08:00
|
|
|
foreach (const QString& file, mProjectFiles) {
|
2022-11-06 22:51:14 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(file,PFileIncludes());
|
|
|
|
if (fileIncludes && fileIncludes->includeFiles.contains(fileName)) {
|
2022-10-22 19:33:20 +08:00
|
|
|
result.insert(file);
|
2022-10-23 23:06:55 +08:00
|
|
|
}
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
2022-10-22 19:33:20 +08:00
|
|
|
return result;
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-03-12 10:50:47 +08:00
|
|
|
//int CppParser::calcKeyLenForStruct(const QString &word)
|
|
|
|
//{
|
|
|
|
// if (word.startsWith("struct"))
|
|
|
|
// return 6;
|
|
|
|
// else if (word.startsWith("class")
|
|
|
|
// || word.startsWith("union"))
|
|
|
|
// return 5;
|
|
|
|
// return -1;
|
|
|
|
//}
|
2021-08-16 00:47:35 +08:00
|
|
|
|
2022-11-05 08:42:54 +08:00
|
|
|
void CppParser::scanMethodArgs(const PStatement& functionStatement, int argStart)
|
2021-08-19 23:49:23 +08:00
|
|
|
{
|
2022-11-05 08:42:54 +08:00
|
|
|
Q_ASSERT(mTokenizer[argStart]->text=='(');
|
|
|
|
int argEnd=mTokenizer[argStart]->matchIndex;
|
2022-11-01 23:23:21 +08:00
|
|
|
int paramStart = argStart+1;
|
|
|
|
int i = paramStart ; // assume it starts with ( and ends with )
|
2022-10-31 19:37:24 +08:00
|
|
|
// Keep going and stop on top of the variable name
|
2023-05-29 09:34:07 +08:00
|
|
|
QStringList words;
|
2022-10-31 19:37:24 +08:00
|
|
|
while (i < argEnd) {
|
2022-11-01 23:23:21 +08:00
|
|
|
if (mTokenizer[i]->text=='('
|
|
|
|
&& mTokenizer[i]->matchIndex+1<argEnd
|
|
|
|
&& mTokenizer[mTokenizer[i]->matchIndex+1]->text=='(') {
|
|
|
|
//function pointer
|
|
|
|
int argStart=mTokenizer[i]->matchIndex+1;
|
|
|
|
int argEnd=mTokenizer[argStart]->matchIndex;
|
2023-03-01 11:29:30 +08:00
|
|
|
QString cmd=findFunctionPointerName(i);
|
2022-11-02 10:42:55 +08:00
|
|
|
QString args=mergeArgs(argStart,argEnd);
|
|
|
|
if (!cmd.isEmpty()) {
|
|
|
|
addStatement(
|
|
|
|
functionStatement,
|
|
|
|
mCurrentFile,
|
2023-05-29 09:34:07 +08:00
|
|
|
words.join(" "), // 'int*'
|
2022-11-02 10:42:55 +08:00
|
|
|
cmd, // a
|
|
|
|
args,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[i+1]->line,
|
|
|
|
StatementKind::skParameter,
|
|
|
|
StatementScope::Local,
|
2023-03-12 18:24:58 +08:00
|
|
|
StatementAccessibility::None,
|
2022-11-16 10:29:20 +08:00
|
|
|
StatementProperty::spHasDefinition);
|
2022-11-02 10:42:55 +08:00
|
|
|
}
|
|
|
|
i=argEnd+1;
|
2023-05-29 09:34:07 +08:00
|
|
|
words.clear();
|
2022-11-01 23:23:21 +08:00
|
|
|
} else if (mTokenizer[i]->text=='{') {
|
|
|
|
i=mTokenizer[i]->matchIndex+1;
|
2023-02-06 14:04:38 +08:00
|
|
|
} else if (mTokenizer[i]->text.endsWith('=')) {
|
2023-05-29 09:34:07 +08:00
|
|
|
addMethodParameterStatement(words,mTokenizer[i]->line,functionStatement);
|
2022-11-05 18:58:15 +08:00
|
|
|
i=skipAssignment(i,argEnd);
|
2023-02-06 14:04:38 +08:00
|
|
|
} else if (mTokenizer[i]->text=="::") {
|
2023-05-29 09:34:07 +08:00
|
|
|
words.append(mTokenizer[i]->text);
|
2023-02-06 14:04:38 +08:00
|
|
|
i++;
|
2023-05-29 09:34:07 +08:00
|
|
|
} else if (mTokenizer[i]->text==',') {
|
|
|
|
addMethodParameterStatement(words,mTokenizer[i]->line,functionStatement);
|
|
|
|
i++;
|
|
|
|
words.clear();
|
2022-11-01 23:23:21 +08:00
|
|
|
} else if (isWordChar(mTokenizer[i]->text[0])) {
|
|
|
|
QString cmd=mTokenizer[i]->text;
|
2023-05-29 09:34:07 +08:00
|
|
|
words.append(cmd);
|
2022-11-02 10:42:55 +08:00
|
|
|
i++;
|
2023-06-21 19:30:02 +08:00
|
|
|
} else if (mTokenizer[i]->text.startsWith("[")) {
|
|
|
|
if (!words.isEmpty()) {
|
|
|
|
int lastIdx=words.count()-1;
|
|
|
|
words[lastIdx]=words[lastIdx]+mTokenizer[i]->text;
|
|
|
|
}
|
|
|
|
i++;
|
2022-11-02 10:42:55 +08:00
|
|
|
} else {
|
|
|
|
i++;
|
2022-10-31 19:37:24 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-29 09:34:07 +08:00
|
|
|
addMethodParameterStatement(words,mTokenizer[i-1]->line,functionStatement);
|
2022-10-31 19:37:24 +08:00
|
|
|
|
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
|
2022-04-17 19:49:04 +08:00
|
|
|
QString CppParser::splitPhrase(const QString &phrase, QString &sClazz,
|
2023-03-11 17:32:57 +08:00
|
|
|
QString &sOperator, QString &sMember) const
|
2021-08-19 23:49:23 +08:00
|
|
|
{
|
|
|
|
sClazz="";
|
|
|
|
sMember="";
|
|
|
|
sOperator="";
|
|
|
|
QString result="";
|
|
|
|
int bracketLevel = 0;
|
2023-03-12 23:45:03 +08:00
|
|
|
int phraseLength = phrase.length();
|
2021-08-19 23:49:23 +08:00
|
|
|
// Obtain stuff before first operator
|
2023-03-12 23:45:03 +08:00
|
|
|
int firstOpStart = phraseLength + 1;
|
|
|
|
int firstOpEnd = phraseLength + 1;
|
|
|
|
for (int i = 0; i<phraseLength;i++) {
|
2021-09-26 19:40:52 +08:00
|
|
|
if ((i+1<phrase.length()) && (phrase[i] == '-') && (phrase[i + 1] == '>') && (bracketLevel==0)) {
|
2021-08-19 23:49:23 +08:00
|
|
|
firstOpStart = i;
|
|
|
|
firstOpEnd = i+2;
|
|
|
|
sOperator = "->";
|
|
|
|
break;
|
2021-09-26 19:40:52 +08:00
|
|
|
} else if ((i+1<phrase.length()) && (phrase[i] == ':') && (phrase[i + 1] == ':') && (bracketLevel==0)) {
|
2021-08-19 23:49:23 +08:00
|
|
|
firstOpStart = i;
|
|
|
|
firstOpEnd = i+2;
|
|
|
|
sOperator = "::";
|
|
|
|
break;
|
2021-08-31 14:40:41 +08:00
|
|
|
} else if ((phrase[i] == '.') && (bracketLevel==0)) {
|
2021-08-19 23:49:23 +08:00
|
|
|
firstOpStart = i;
|
|
|
|
firstOpEnd = i+1;
|
|
|
|
sOperator = ".";
|
|
|
|
break;
|
|
|
|
} else if (phrase[i] == '[') {
|
|
|
|
bracketLevel++;
|
|
|
|
} else if (phrase[i] == ']') {
|
|
|
|
bracketLevel--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sClazz = phrase.mid(0, firstOpStart);
|
|
|
|
if (firstOpStart == 0) {
|
|
|
|
sMember = "";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
result = phrase.mid(firstOpEnd);
|
|
|
|
|
|
|
|
// ... and before second op, if there is one
|
|
|
|
int secondOp = 0;
|
|
|
|
bracketLevel = 0;
|
2023-03-12 23:45:03 +08:00
|
|
|
for (int i = firstOpEnd; i<phraseLength;i++) {
|
|
|
|
if ((i+1<phraseLength) && (phrase[i] == '-') && (phrase[i + 1] == '>') && (bracketLevel=0)) {
|
2021-08-19 23:49:23 +08:00
|
|
|
secondOp = i;
|
|
|
|
break;
|
2023-03-12 23:45:03 +08:00
|
|
|
} else if ((i+1<phraseLength) && (phrase[i] == ':') && (phrase[i + 1] == ':') && (bracketLevel=0)) {
|
2021-08-19 23:49:23 +08:00
|
|
|
secondOp = i;
|
|
|
|
break;
|
|
|
|
} else if ((phrase[i] == '.') && (bracketLevel=0)) {
|
|
|
|
secondOp = i;
|
|
|
|
break;
|
|
|
|
} else if (phrase[i] == '[') {
|
|
|
|
bracketLevel++;
|
|
|
|
} else if (phrase[i] == ']') {
|
|
|
|
bracketLevel--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (secondOp == 0) {
|
|
|
|
sMember = phrase.mid(firstOpEnd);
|
|
|
|
} else {
|
|
|
|
sMember = phrase.mid(firstOpEnd,secondOp-firstOpEnd);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
QString CppParser::removeTemplateParams(const QString &phrase) const
|
2022-11-04 20:27:35 +08:00
|
|
|
{
|
|
|
|
int pos = phrase.indexOf('<');
|
|
|
|
if (pos>=0) {
|
|
|
|
return phrase.left(pos);
|
|
|
|
}
|
|
|
|
return phrase;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::splitLastMember(const QString &token, QString &lastMember, QString &remaining)
|
|
|
|
{
|
2022-11-04 23:44:11 +08:00
|
|
|
int pos = token.length()-1;
|
|
|
|
int level=0;
|
|
|
|
bool found=false;
|
|
|
|
while (pos>=0 && !found) {
|
|
|
|
switch(token[pos].unicode()) {
|
|
|
|
case ']':
|
|
|
|
case '>':
|
|
|
|
case ')':
|
|
|
|
level++;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
case '<':
|
|
|
|
case '(':
|
|
|
|
level--;
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
if (level==0 && pos>0 && token[pos-1]==':') {
|
|
|
|
found=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos--;
|
|
|
|
}
|
2022-11-04 20:27:35 +08:00
|
|
|
if (pos<0)
|
|
|
|
return false;
|
|
|
|
lastMember=token.mid(pos+2);
|
|
|
|
remaining=token.left(pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static void appendArgWord(QString& args, const QString& word) {
|
|
|
|
// QString s=word.trimmed();
|
|
|
|
// if (s.isEmpty())
|
|
|
|
// return;
|
|
|
|
// if (args.isEmpty())
|
|
|
|
// args.append(s);
|
|
|
|
// else if (isIdentChar(args.back()) && isIdentChar(word.front()) ) {
|
|
|
|
// args+=" ";
|
|
|
|
// args+=s;
|
|
|
|
// } else {
|
|
|
|
// args+=s;
|
|
|
|
// }
|
|
|
|
//}
|
2021-08-15 17:52:39 +08:00
|
|
|
|
2022-11-04 20:27:35 +08:00
|
|
|
bool CppParser::isNotFuncArgs(int startIndex)
|
2021-08-15 20:25:54 +08:00
|
|
|
{
|
2022-11-04 20:27:35 +08:00
|
|
|
Q_ASSERT(mTokenizer[startIndex]->text=='(');
|
|
|
|
int endIndex=mTokenizer[startIndex]->matchIndex;
|
2022-10-31 19:37:24 +08:00
|
|
|
//no args, it must be a function
|
|
|
|
if (endIndex-startIndex==1)
|
|
|
|
return false;
|
|
|
|
int i=startIndex+1; //skip '('
|
|
|
|
int endPos = endIndex;
|
2021-08-15 20:25:54 +08:00
|
|
|
QString word = "";
|
|
|
|
while (i<endPos) {
|
2022-10-31 19:37:24 +08:00
|
|
|
QChar ch=mTokenizer[i]->text[0];
|
|
|
|
switch(ch.unicode()) {
|
|
|
|
// args contains a string/char, can't be a func define
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
2022-11-04 20:27:35 +08:00
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '/':
|
|
|
|
case '|':
|
|
|
|
case '!':
|
2022-10-31 19:37:24 +08:00
|
|
|
case '{':
|
2022-11-04 20:27:35 +08:00
|
|
|
return true;
|
|
|
|
case '[': // function args like int f[10]
|
2022-10-31 19:37:24 +08:00
|
|
|
i=mTokenizer[i]->matchIndex+1;
|
2022-11-04 20:27:35 +08:00
|
|
|
if (i<endPos &&
|
|
|
|
(mTokenizer[i]->text=='('
|
|
|
|
|| mTokenizer[i]->text=='{')) //lambda
|
|
|
|
return true;
|
2022-10-31 19:37:24 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isDigitChar(ch))
|
2021-08-15 20:25:54 +08:00
|
|
|
return true;
|
2022-11-04 20:27:35 +08:00
|
|
|
if (isIdentChar(ch)) {
|
2022-10-31 19:37:24 +08:00
|
|
|
QString currentText=mTokenizer[i]->text;
|
2023-02-06 19:37:01 +08:00
|
|
|
// if (mTokenizer[i]->text.endsWith('.'))
|
|
|
|
// return true;
|
|
|
|
// if (mTokenizer[i]->text.endsWith("->"))
|
|
|
|
// return true;
|
2022-11-02 10:42:55 +08:00
|
|
|
if (!mCppTypeKeywords.contains(currentText)) {
|
2022-11-02 22:48:25 +08:00
|
|
|
if (currentText=="true" || currentText=="false" || currentText=="nullptr" ||
|
2022-11-04 23:44:11 +08:00
|
|
|
currentText=="this")
|
2022-11-02 10:42:55 +08:00
|
|
|
return true;
|
2022-11-04 20:27:35 +08:00
|
|
|
if (currentText=="const")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (isCppKeyword(currentText))
|
2022-11-02 13:38:26 +08:00
|
|
|
return false;
|
2022-11-02 10:42:55 +08:00
|
|
|
|
2023-03-11 17:32:57 +08:00
|
|
|
PStatement statement = doFindStatementOf(mCurrentFile,word,getCurrentScope());
|
2023-02-06 19:37:01 +08:00
|
|
|
//template arguments
|
|
|
|
if (!statement)
|
|
|
|
return false;
|
2022-11-02 13:38:26 +08:00
|
|
|
if (statement && isTypeStatement(statement->kind))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-11-02 10:42:55 +08:00
|
|
|
}
|
2022-11-02 13:38:26 +08:00
|
|
|
|
2022-10-31 19:37:24 +08:00
|
|
|
}
|
|
|
|
i++;
|
2021-08-15 20:25:54 +08:00
|
|
|
}
|
|
|
|
//function with no args
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-17 23:30:14 +08:00
|
|
|
|
2021-12-08 19:13:47 +08:00
|
|
|
bool CppParser::isNamedScope(StatementKind kind) const
|
2021-08-17 23:30:14 +08:00
|
|
|
{
|
|
|
|
switch(kind) {
|
|
|
|
case StatementKind::skClass:
|
|
|
|
case StatementKind::skNamespace:
|
|
|
|
case StatementKind::skFunction:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:13:47 +08:00
|
|
|
bool CppParser::isTypeStatement(StatementKind kind) const
|
2021-08-17 23:30:14 +08:00
|
|
|
{
|
|
|
|
switch(kind) {
|
|
|
|
case StatementKind::skClass:
|
|
|
|
case StatementKind::skTypedef:
|
2021-08-18 05:34:04 +08:00
|
|
|
case StatementKind::skEnumClassType:
|
2021-08-17 23:30:14 +08:00
|
|
|
case StatementKind::skEnumType:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 23:49:23 +08:00
|
|
|
|
|
|
|
void CppParser::updateSerialId()
|
|
|
|
{
|
|
|
|
mSerialId = QString("%1 %2").arg(mParserId).arg(mSerialCount);
|
|
|
|
}
|
2021-08-21 22:15:44 +08:00
|
|
|
|
2022-11-05 18:58:15 +08:00
|
|
|
int CppParser::indexOfNextSemicolon(int index, int endIndex)
|
2022-11-01 09:02:17 +08:00
|
|
|
{
|
2022-11-05 18:58:15 +08:00
|
|
|
if (endIndex<0)
|
|
|
|
endIndex=mTokenizer.tokenCount();
|
|
|
|
while (index<endIndex) {
|
2022-11-01 09:02:17 +08:00
|
|
|
switch(mTokenizer[index]->text[0].unicode()) {
|
|
|
|
case ';':
|
|
|
|
return index;
|
|
|
|
case '(':
|
|
|
|
index = mTokenizer[index]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:22:33 +08:00
|
|
|
int CppParser::indexOfNextPeriodOrSemicolon(int index, int endIndex)
|
|
|
|
{
|
|
|
|
if (endIndex<0)
|
|
|
|
endIndex=mTokenizer.tokenCount();
|
|
|
|
while (index<endIndex) {
|
|
|
|
switch(mTokenizer[index]->text[0].unicode()) {
|
|
|
|
case ';':
|
|
|
|
case ',':
|
2023-02-21 20:35:41 +08:00
|
|
|
case '}':
|
2022-11-09 22:22:33 +08:00
|
|
|
return index;
|
|
|
|
case '(':
|
|
|
|
index = mTokenizer[index]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2022-11-03 00:49:22 +08:00
|
|
|
int CppParser::indexOfNextSemicolonOrLeftBrace(int index)
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
|
|
|
while (index<tokenCount) {
|
2022-11-03 00:49:22 +08:00
|
|
|
switch(mTokenizer[index]->text[0].unicode()) {
|
|
|
|
case ';':
|
|
|
|
case '{':
|
|
|
|
return index;
|
|
|
|
case '(':
|
|
|
|
index = mTokenizer[index]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
int CppParser::indexOfNextColon(int index)
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
|
|
|
while (index<tokenCount) {
|
2023-03-10 16:02:47 +08:00
|
|
|
QString s =mTokenizer[index]->text;
|
|
|
|
switch(s[0].unicode()) {
|
2022-11-01 09:02:17 +08:00
|
|
|
case ':':
|
2023-03-10 16:02:47 +08:00
|
|
|
if (s.length()==1)
|
|
|
|
return index;
|
|
|
|
else
|
|
|
|
index++;
|
|
|
|
break;
|
2022-11-01 09:02:17 +08:00
|
|
|
case '(':
|
|
|
|
index = mTokenizer[index]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::indexOfNextLeftBrace(int index)
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
|
|
|
while (index<tokenCount) {
|
2022-11-01 09:02:17 +08:00
|
|
|
switch(mTokenizer[index]->text[0].unicode()) {
|
|
|
|
case '{':
|
|
|
|
return index;
|
|
|
|
case '(':
|
|
|
|
index = mTokenizer[index]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::indexPassParenthesis(int index)
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
|
|
|
while (index<tokenCount) {
|
2022-11-01 09:02:17 +08:00
|
|
|
if (mTokenizer[index]->text=='(') {
|
|
|
|
return mTokenizer[index]->matchIndex+1;
|
|
|
|
}
|
2022-11-02 13:38:26 +08:00
|
|
|
index++;
|
2022-11-01 09:02:17 +08:00
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:01:38 +08:00
|
|
|
//int CppParser::indexPassBraces(int index)
|
|
|
|
//{
|
|
|
|
// int tokenCount = mTokenizer.tokenCount();
|
|
|
|
// while (index<tokenCount) {
|
|
|
|
// switch(mTokenizer[index]->text[0].unicode()) {
|
|
|
|
// case '{':
|
|
|
|
// return mTokenizer[index]->matchIndex+1;
|
|
|
|
// case '(':
|
|
|
|
// index = mTokenizer[index]->matchIndex+1;
|
|
|
|
// break;
|
|
|
|
// default:
|
|
|
|
// index++;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return index;
|
|
|
|
//}
|
2022-11-01 09:02:17 +08:00
|
|
|
|
2022-11-04 20:27:35 +08:00
|
|
|
void CppParser::skipNextSemicolon(int index)
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2022-11-04 20:27:35 +08:00
|
|
|
mIndex=index;
|
2023-03-12 23:45:03 +08:00
|
|
|
while (mIndex<tokenCount) {
|
2022-11-04 20:27:35 +08:00
|
|
|
switch(mTokenizer[mIndex]->text[0].unicode()) {
|
|
|
|
case ';':
|
|
|
|
mIndex++;
|
|
|
|
return;
|
|
|
|
case '{':
|
|
|
|
mIndex = mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
mIndex = mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:22:33 +08:00
|
|
|
int CppParser::moveToEndOfStatement(int index, bool checkLambda, int endIndex)
|
2022-11-04 23:44:11 +08:00
|
|
|
{
|
2022-11-05 18:58:15 +08:00
|
|
|
int startIndex=index;
|
|
|
|
if (endIndex<0)
|
|
|
|
endIndex=mTokenizer.tokenCount();
|
2022-11-09 22:22:33 +08:00
|
|
|
if (index>=endIndex)
|
|
|
|
return index;
|
|
|
|
index--; // compensate for the first loop
|
|
|
|
|
|
|
|
bool skip=true;
|
|
|
|
do {
|
|
|
|
index++;
|
|
|
|
bool stop = false;
|
|
|
|
while (index<endIndex && !stop) {
|
|
|
|
switch(mTokenizer[index]->text[0].unicode()) {
|
|
|
|
case ';':
|
|
|
|
stop=true;
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
stop=true;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
stop=true;
|
|
|
|
skip=false; //don't skip the orphan '}' that we found
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
//move to '}'
|
|
|
|
index=mTokenizer[index]->matchIndex;
|
|
|
|
stop=true;
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
index = mTokenizer[index]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index++;
|
|
|
|
}
|
2022-11-04 23:44:11 +08:00
|
|
|
}
|
2022-11-09 22:22:33 +08:00
|
|
|
} while (index<mTokenizer.tokenCount() && mTokenizer[index]->text=='=');
|
|
|
|
if (index<endIndex && checkLambda) {
|
2022-11-05 18:58:15 +08:00
|
|
|
while (mTokenizer.lambdasCount()>0 && mTokenizer.indexOfFirstLambda()<index) {
|
|
|
|
int i=mTokenizer.indexOfFirstLambda();
|
|
|
|
mTokenizer.removeFirstLambda();
|
|
|
|
if (i>=startIndex) {
|
|
|
|
handleLambda(i,index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 22:22:33 +08:00
|
|
|
if (skip)
|
|
|
|
index++;
|
2022-11-05 18:58:15 +08:00
|
|
|
return index;
|
2022-11-04 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
2022-11-04 20:27:35 +08:00
|
|
|
void CppParser::skipParenthesis(int index)
|
|
|
|
{
|
2023-03-12 23:45:03 +08:00
|
|
|
int tokenCount = mTokenizer.tokenCount();
|
2022-11-04 20:27:35 +08:00
|
|
|
mIndex=index;
|
2023-03-12 23:45:03 +08:00
|
|
|
while (mIndex<tokenCount) {
|
2022-11-04 20:27:35 +08:00
|
|
|
if (mTokenizer[mIndex]->text=='(') {
|
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 18:58:15 +08:00
|
|
|
int CppParser::skipAssignment(int index, int endIndex)
|
|
|
|
{
|
|
|
|
int startIndex=index;
|
|
|
|
bool stop=false;
|
|
|
|
while (index<endIndex && !stop) {
|
|
|
|
switch(mTokenizer[index]->text[0].unicode()) {
|
|
|
|
case ';':
|
|
|
|
case ',':
|
|
|
|
case '{':
|
|
|
|
stop=true;
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
index = mTokenizer[index]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stop) {
|
|
|
|
while (mTokenizer.lambdasCount()>0 && mTokenizer.indexOfFirstLambda()<index) {
|
|
|
|
int i=mTokenizer.indexOfFirstLambda();
|
|
|
|
mTokenizer.removeFirstLambda();
|
|
|
|
if (i>=startIndex) {
|
|
|
|
handleLambda(i,index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (index<endIndex)?index:endIndex;
|
|
|
|
}
|
|
|
|
|
2022-11-01 22:10:54 +08:00
|
|
|
QString CppParser::mergeArgs(int startIndex, int endIndex)
|
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
for (int i=startIndex;i<=endIndex;i++) {
|
|
|
|
if (i>startIndex)
|
|
|
|
result+=' ';
|
|
|
|
result+=mTokenizer[i]->text;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-12 23:45:03 +08:00
|
|
|
void CppParser::parseCommandTypeAndArgs(QString &command, QString &typeSuffix, QString &args) const
|
2022-11-01 22:10:54 +08:00
|
|
|
{
|
2023-03-01 11:29:30 +08:00
|
|
|
int prefix=0;
|
|
|
|
while (prefix<command.length() && (command[prefix]=='*' || command[prefix]=='&')) {
|
|
|
|
prefix++;
|
|
|
|
}
|
|
|
|
if (prefix>0) {
|
|
|
|
typeSuffix=command.left(prefix);
|
|
|
|
command=command.mid(prefix);
|
|
|
|
} else {
|
|
|
|
typeSuffix="";
|
2022-11-01 22:10:54 +08:00
|
|
|
}
|
|
|
|
int pos=command.indexOf('[');
|
|
|
|
if (pos>=0) {
|
|
|
|
args=command.mid(pos);
|
|
|
|
command=command.left(pos);
|
|
|
|
} else {
|
|
|
|
args="";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-07 11:24:23 +08:00
|
|
|
const QSet<QString> &CppParser::projectFiles() const
|
|
|
|
{
|
|
|
|
return mProjectFiles;
|
|
|
|
}
|
|
|
|
|
2022-11-27 13:32:14 +08:00
|
|
|
QList<QString> CppParser::namespaces()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
return mNamespaces.keys();
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:51:38 +08:00
|
|
|
ParserLanguage CppParser::language() const
|
|
|
|
{
|
|
|
|
return mLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::setLanguage(ParserLanguage newLanguage)
|
|
|
|
{
|
2023-08-13 14:46:53 +08:00
|
|
|
if (mLanguage != newLanguage) {
|
|
|
|
mLanguage = newLanguage;
|
2023-08-13 15:41:17 +08:00
|
|
|
mCppKeywords = CppKeywords;
|
|
|
|
mCppTypeKeywords = CppTypeKeywords;
|
|
|
|
#ifdef ENABLE_SDCC
|
2023-08-13 14:46:53 +08:00
|
|
|
if (mLanguage == ParserLanguage::SDCC) {
|
2023-08-17 22:08:31 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
2023-08-13 14:46:53 +08:00
|
|
|
mCppKeywords.insert(SDCCKeywords);
|
2023-08-17 22:08:31 +08:00
|
|
|
#else
|
|
|
|
for (auto &k : SDCCKeywords.keys())
|
|
|
|
mCppKeywords[k] = SDCCKeywords[k];
|
|
|
|
#endif
|
2023-08-13 14:46:53 +08:00
|
|
|
mCppTypeKeywords.unite(SDCCTypeKeywords);
|
|
|
|
}
|
2023-08-13 15:41:17 +08:00
|
|
|
#endif
|
2023-08-13 14:46:53 +08:00
|
|
|
}
|
2022-07-28 13:51:38 +08:00
|
|
|
}
|
|
|
|
|
2021-12-18 23:36:58 +08:00
|
|
|
|
2021-12-04 10:02:07 +08:00
|
|
|
|
2021-08-24 15:05:10 +08:00
|
|
|
const StatementModel &CppParser::statementList() const
|
|
|
|
{
|
|
|
|
return mStatementList;
|
|
|
|
}
|
|
|
|
|
2021-08-23 10:16:06 +08:00
|
|
|
bool CppParser::parseGlobalHeaders() const
|
|
|
|
{
|
|
|
|
return mParseGlobalHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::setParseGlobalHeaders(bool newParseGlobalHeaders)
|
|
|
|
{
|
|
|
|
mParseGlobalHeaders = newParseGlobalHeaders;
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
const QSet<QString> &CppParser::includePaths()
|
|
|
|
{
|
2022-10-22 10:44:10 +08:00
|
|
|
return mPreprocessor.includePaths();
|
2021-08-29 00:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QSet<QString> &CppParser::projectIncludePaths()
|
|
|
|
{
|
2022-10-22 10:44:10 +08:00
|
|
|
return mPreprocessor.projectIncludePaths();
|
2021-08-29 00:48:23 +08:00
|
|
|
}
|
|
|
|
|
2021-08-23 10:16:06 +08:00
|
|
|
bool CppParser::parseLocalHeaders() const
|
|
|
|
{
|
|
|
|
return mParseLocalHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::setParseLocalHeaders(bool newParseLocalHeaders)
|
|
|
|
{
|
|
|
|
mParseLocalHeaders = newParseLocalHeaders;
|
|
|
|
}
|
|
|
|
|
2021-08-23 03:47:28 +08:00
|
|
|
const QString &CppParser::serialId() const
|
|
|
|
{
|
|
|
|
return mSerialId;
|
|
|
|
}
|
|
|
|
|
2021-08-22 23:48:00 +08:00
|
|
|
int CppParser::parserId() const
|
|
|
|
{
|
|
|
|
return mParserId;
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
void CppParser::setOnGetFileStream(const GetFileStreamCallBack &newOnGetFileStream)
|
|
|
|
{
|
2022-10-22 19:33:20 +08:00
|
|
|
mPreprocessor.setOnGetFileStream(newOnGetFileStream);
|
2021-08-22 21:23:58 +08:00
|
|
|
}
|
|
|
|
|
2021-08-21 22:15:44 +08:00
|
|
|
const QSet<QString> &CppParser::filesToScan() const
|
|
|
|
{
|
|
|
|
return mFilesToScan;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::setFilesToScan(const QSet<QString> &newFilesToScan)
|
|
|
|
{
|
|
|
|
mFilesToScan = newFilesToScan;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::enabled() const
|
|
|
|
{
|
|
|
|
return mEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::setEnabled(bool newEnabled)
|
|
|
|
{
|
2022-03-22 19:08:26 +08:00
|
|
|
if (mEnabled!=newEnabled) {
|
|
|
|
mEnabled = newEnabled;
|
|
|
|
if (!mEnabled) {
|
2022-10-18 12:24:59 +08:00
|
|
|
resetParser();
|
2022-03-22 19:08:26 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-21 22:15:44 +08:00
|
|
|
}
|
2021-08-23 10:16:06 +08:00
|
|
|
|
|
|
|
CppFileParserThread::CppFileParserThread(
|
|
|
|
PCppParser parser,
|
|
|
|
QString fileName,
|
|
|
|
bool inProject,
|
|
|
|
bool onlyIfNotParsed,
|
|
|
|
bool updateView,
|
|
|
|
QObject *parent):QThread(parent),
|
|
|
|
mParser(parser),
|
|
|
|
mFileName(fileName),
|
|
|
|
mInProject(inProject),
|
|
|
|
mOnlyIfNotParsed(onlyIfNotParsed),
|
|
|
|
mUpdateView(updateView)
|
|
|
|
{
|
2022-10-10 18:05:18 +08:00
|
|
|
connect(this,&QThread::finished,
|
|
|
|
this,&QObject::deleteLater);
|
2021-08-23 10:16:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CppFileParserThread::run()
|
|
|
|
{
|
|
|
|
if (mParser && !mParser->parsing()) {
|
|
|
|
mParser->parseFile(mFileName,mInProject,mOnlyIfNotParsed,mUpdateView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CppFileListParserThread::CppFileListParserThread(PCppParser parser,
|
|
|
|
bool updateView, QObject *parent):
|
|
|
|
QThread(parent),
|
|
|
|
mParser(parser),
|
|
|
|
mUpdateView(updateView)
|
|
|
|
{
|
2022-10-10 18:05:18 +08:00
|
|
|
connect(this,&QThread::finished,
|
|
|
|
this,&QObject::deleteLater);
|
2021-08-23 10:16:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CppFileListParserThread::run()
|
|
|
|
{
|
|
|
|
if (mParser && !mParser->parsing()) {
|
|
|
|
mParser->parseFileList(mUpdateView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
void parseFile(PCppParser parser, const QString& fileName, bool inProject, bool onlyIfNotParsed, bool updateView)
|
2021-08-23 10:16:06 +08:00
|
|
|
{
|
2021-08-31 14:40:41 +08:00
|
|
|
if (!parser)
|
|
|
|
return;
|
2022-10-09 22:19:18 +08:00
|
|
|
if (!parser->enabled())
|
|
|
|
return;
|
2023-08-11 13:49:09 +08:00
|
|
|
// qDebug()<<"parsing "<<fileName;
|
2022-10-10 18:05:18 +08:00
|
|
|
//delete when finished
|
2021-08-23 10:16:06 +08:00
|
|
|
CppFileParserThread* thread = new CppFileParserThread(parser,fileName,inProject,onlyIfNotParsed,updateView);
|
|
|
|
thread->connect(thread,
|
|
|
|
&QThread::finished,
|
|
|
|
thread,
|
|
|
|
&QThread::deleteLater);
|
|
|
|
thread->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseFileList(PCppParser parser, bool updateView)
|
|
|
|
{
|
2021-08-31 14:40:41 +08:00
|
|
|
if (!parser)
|
|
|
|
return;
|
2022-10-09 22:19:18 +08:00
|
|
|
if (!parser->enabled())
|
|
|
|
return;
|
2022-10-10 18:05:18 +08:00
|
|
|
//delete when finished
|
2021-08-23 10:16:06 +08:00
|
|
|
CppFileListParserThread *thread = new CppFileListParserThread(parser,updateView);
|
|
|
|
thread->connect(thread,
|
|
|
|
&QThread::finished,
|
|
|
|
thread,
|
|
|
|
&QThread::deleteLater);
|
|
|
|
thread->start();
|
|
|
|
}
|