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-09-26 12:01:45 +08:00
|
|
|
#include "qsynedit/highlighter/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>
|
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);
|
2022-01-04 16:50:54 +08:00
|
|
|
CppParser::CppParser(QObject *parent) : QObject(parent),
|
|
|
|
mMutex(QMutex::Recursive)
|
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()
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
//wait for all methods finishes running
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (!mParsing && (mLockCount == 0)) {
|
|
|
|
mParsing = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-08-21 22:15:44 +08:00
|
|
|
PStatement statement = findStatementOf(fileName,phrase, line);
|
|
|
|
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) {
|
|
|
|
PStatementList namespaceStatementsList = findNamespace(parentScope->command);
|
|
|
|
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-03-23 14:13:10 +08:00
|
|
|
PStatement CppParser::findAndScanBlockAt(const QString &filename, int line)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing) {
|
|
|
|
return PStatement();
|
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
PStatement statement = fileIncludes->scopes.findScopeAtLine(line);
|
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 "";
|
|
|
|
// Remove pointer stuff from type
|
|
|
|
QString s = phrase; // 'Type' is a keyword
|
|
|
|
int i = s.indexOf('<');
|
|
|
|
if (i>=0) {
|
|
|
|
int t=getFirstTemplateParamEnd(s,i);
|
|
|
|
return s.mid(i+1,t-i-1);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
PStatement statement = findStatementOf(fileName,s,currentScope);
|
|
|
|
return getFirstTemplateParam(statement,fileName, phrase, 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
|
|
|
|
{
|
|
|
|
int i = phrase.length()-1;
|
|
|
|
|
|
|
|
// Obtain stuff after first operator
|
|
|
|
while (i>=0) {
|
|
|
|
if ((i+1<phrase.length()) &&
|
|
|
|
(phrase[i + 1] == '>') && (phrase[i] == '-'))
|
|
|
|
return i;
|
|
|
|
else if ((i+1<phrase.length()) &&
|
|
|
|
(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);
|
2021-08-22 16:08:46 +08:00
|
|
|
return mNamespaces.value(name,PStatementList());
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:44:23 +08:00
|
|
|
PStatement CppParser::findStatement(const QString &fullname)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (fullname.isEmpty())
|
|
|
|
return PStatement();
|
|
|
|
QStringList phrases = fullname.split("::");
|
|
|
|
if (phrases.isEmpty())
|
|
|
|
return PStatement();
|
|
|
|
PStatement parentStatement;
|
|
|
|
PStatement statement;
|
|
|
|
foreach (const QString& phrase, phrases) {
|
|
|
|
if (parentStatement && parentStatement->kind == StatementKind::skNamespace) {
|
|
|
|
PStatementList lst = findNamespace(parentStatement->fullName);
|
|
|
|
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();
|
2021-08-22 16:08:46 +08:00
|
|
|
return findStatementOf(fileName,phrase,findAndScanBlockAt(fileName,line));
|
|
|
|
}
|
|
|
|
|
2021-12-05 10:52:17 +08:00
|
|
|
PStatement CppParser::findStatementOf(const QString &fileName,
|
|
|
|
const QString &phrase,
|
|
|
|
const PStatement& currentScope,
|
|
|
|
PStatement &parentScopeType,
|
|
|
|
bool force)
|
2021-08-22 16:08:46 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
PStatement result;
|
2021-08-25 00:20:07 +08:00
|
|
|
parentScopeType = currentScope;
|
2021-08-22 16:08:46 +08:00
|
|
|
if (mParsing && !force)
|
|
|
|
return PStatement();
|
|
|
|
|
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)) {
|
2021-08-25 00:20:07 +08:00
|
|
|
PStatement typeStatement = findTypeDefinitionOf(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) {
|
|
|
|
statement = findAliasedStatement(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();
|
|
|
|
typeName=findFirstTemplateParamOf(fileName,lastScopeStatement->type,
|
|
|
|
lastScopeParent );
|
|
|
|
typeStatement=findTypeDefinitionOf(fileName, typeName,
|
|
|
|
lastScopeParent );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isSTLContainerFunctions)
|
2021-08-25 00:20:07 +08:00
|
|
|
typeStatement = findTypeDefinitionOf(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();
|
|
|
|
typeName=findFirstTemplateParamOf(fileName,statement->type, parentScope);
|
|
|
|
typeStatement=findTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
} else if ((typeStatement)
|
|
|
|
&& STLContainers.contains(typeStatement->fullName)
|
|
|
|
&& nextScopeWord.endsWith(']')) {
|
|
|
|
//it's a std container
|
|
|
|
PStatement parentScope = statement->parentScope.lock();
|
|
|
|
typeName = findFirstTemplateParamOf(fileName,statement->type,
|
|
|
|
parentScope);
|
|
|
|
typeStatement = findTypeDefinitionOf(fileName, typeName,
|
|
|
|
parentScope);
|
|
|
|
}
|
|
|
|
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)) {
|
2021-08-25 00:20:07 +08:00
|
|
|
PStatement typeStatement = findTypeDefinitionOf(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,
|
|
|
|
const QStringList &phraseExpression,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::findStatementOf(const QString &fileName, const QString &phrase, const PStatement& currentClass, bool force)
|
2021-08-22 16:08:46 +08:00
|
|
|
{
|
|
|
|
PStatement statementParentType;
|
|
|
|
return findStatementOf(fileName,phrase,currentClass,statementParentType,force);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
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()) {
|
|
|
|
return findMemberOfStatement(phrase,PStatement());
|
|
|
|
} 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) {
|
|
|
|
PStatementList lst = findNamespace(ownerEvalStatement->effectiveTypeStatement->fullName);
|
|
|
|
foreach (const PStatement& namespaceStatement, *lst) {
|
|
|
|
PStatement statement = findMemberOfStatement(phrase,namespaceStatement);
|
|
|
|
if (statement)
|
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
return PStatement();
|
|
|
|
}
|
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();
|
|
|
|
return findStatementOf(fileName,expression,findAndScanBlockAt(fileName,line));
|
|
|
|
}
|
|
|
|
|
2022-03-15 21:33:27 +08:00
|
|
|
PStatement CppParser::findAliasedStatement(const PStatement &statement)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
if (mParsing)
|
|
|
|
return PStatement();
|
|
|
|
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);
|
|
|
|
PStatementList namespaceStatements = findNamespace(nsName);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-07 08:23:27 +08:00
|
|
|
PStatement CppParser::findStatementStartingFrom(const QString &fileName, const QString &phrase, const PStatement& startScope)
|
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
|
|
|
|
result = findMemberOfStatement(phrase,PStatement());
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
//Find in all global usings
|
|
|
|
const QSet<QString>& fileUsings = getFileUsings(fileName);
|
|
|
|
// 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
|
|
|
|
2021-08-22 21:23:58 +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 = findStatementOf(fileName,s,currentClass);
|
|
|
|
return getTypeDef(statement,fileName,aType);
|
|
|
|
}
|
|
|
|
|
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);
|
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;
|
2021-08-22 21:23:58 +08:00
|
|
|
if (mParsing)
|
2021-09-04 00:13:42 +08:00
|
|
|
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,
|
|
|
|
projectIncludes);
|
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,
|
|
|
|
StatementClassScope::None,
|
2022-10-31 19:37:24 +08:00
|
|
|
true,
|
|
|
|
false);
|
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();
|
|
|
|
mCurrentClassScope.clear();
|
2022-10-18 12:24:59 +08:00
|
|
|
mSkipList.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
|
|
|
}
|
|
|
|
|
2021-08-29 22:08:43 +08:00
|
|
|
QString CppParser::getScopePrefix(const PStatement& statement){
|
|
|
|
switch (statement->classScope) {
|
2022-11-01 09:02:17 +08:00
|
|
|
case StatementClassScope::Public:
|
2021-08-29 22:08:43 +08:00
|
|
|
return "public";
|
2022-11-01 09:02:17 +08:00
|
|
|
case StatementClassScope::Private:
|
2021-08-29 22:08:43 +08:00
|
|
|
return "private";
|
2022-11-01 09:02:17 +08:00
|
|
|
case StatementClassScope::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:
|
|
|
|
result = statement->type + "::" + statement->command;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
QString CppParser::getFirstTemplateParam(const PStatement& statement,
|
|
|
|
const QString& filename,
|
|
|
|
const QString& phrase,
|
|
|
|
const PStatement& currentScope)
|
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 "";
|
|
|
|
return findFirstTemplateParamOf(filename,statement->type, currentScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::getFirstTemplateParamEnd(const QString &s, int startAt)
|
|
|
|
{
|
|
|
|
int i = startAt;
|
|
|
|
int level = 0; // assume we start on top of '<'
|
|
|
|
while (i < s.length()) {
|
|
|
|
switch (s[i].unicode()) {
|
|
|
|
case '<':
|
|
|
|
level++;
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
if (level == 1)
|
|
|
|
return i;
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
void CppParser::addFileToScan(const QString& value, bool inProject)
|
2021-08-19 23:49:23 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mMutex);
|
|
|
|
//value.replace('/','\\'); // only accept full file names
|
|
|
|
|
|
|
|
// Update project listing
|
|
|
|
if (inProject)
|
|
|
|
mProjectFiles.insert(value);
|
|
|
|
|
|
|
|
// Only parse given file
|
2022-10-22 10:44:10 +08:00
|
|
|
if (!mPreprocessor.scannedFiles().contains(value)) {
|
2021-08-19 23:49:23 +08:00
|
|
|
mFilesToScan.insert(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::addInheritedStatement(const PStatement& derived, const PStatement& inherit, StatementClassScope 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,
|
|
|
|
true,
|
|
|
|
inherit->isStatic);
|
2021-08-15 16:49:37 +08:00
|
|
|
statement->isInherited = true;
|
|
|
|
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,
|
|
|
|
const StatementScope& scope, const StatementClassScope& classScope,
|
|
|
|
bool isDefinition, bool isStatic)
|
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,
|
|
|
|
isDefinition,
|
2021-08-15 16:49:37 +08:00
|
|
|
isStatic);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
const StatementClassScope& classScope, bool isDefinition, bool isStatic)
|
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-04-18 16:56:31 +08:00
|
|
|
if (isDefinition) {
|
|
|
|
PStatement oldStatement = findStatementInScope(newCommand,noNameArgs,kind,parent);
|
|
|
|
if (oldStatement && !oldStatement->hasDefinition) {
|
|
|
|
oldStatement->hasDefinition = true;
|
|
|
|
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;
|
|
|
|
result->classScope = classScope;
|
|
|
|
result->hasDefinition = isDefinition;
|
|
|
|
result->line = line;
|
|
|
|
result->definitionLine = line;
|
|
|
|
result->fileName = fileName;
|
|
|
|
result->definitionFileName = fileName;
|
|
|
|
if (!fileName.isEmpty())
|
|
|
|
result->inProject = mIsProjectFile;
|
|
|
|
else
|
|
|
|
result->inProject = false;
|
|
|
|
result->inSystemHeader = mIsSystemHeader;
|
|
|
|
//result->children;
|
|
|
|
//result->friends;
|
|
|
|
result->isStatic = isStatic;
|
|
|
|
result->isInherited = false;
|
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);
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 16:49:37 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:37:24 +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, const StatementClassScope &classScope, bool isDefinition, bool isStatic)
|
|
|
|
{
|
|
|
|
QString args("(");
|
|
|
|
QString noNameArgs("(");
|
|
|
|
|
|
|
|
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];
|
|
|
|
if (this->isLetterChar(ch)) {
|
|
|
|
QString spaces=(i>argStart)?" ":"";
|
|
|
|
args+=spaces;
|
|
|
|
word += mTokenizer[i]->text[0];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.push_back(")");
|
|
|
|
noNameArgs.push_back(")");
|
|
|
|
return addStatement(
|
|
|
|
parent,
|
|
|
|
fileName,
|
|
|
|
aType,
|
|
|
|
command,
|
|
|
|
args,
|
|
|
|
noNameArgs,
|
|
|
|
value,
|
|
|
|
line,
|
|
|
|
kind,
|
|
|
|
scope,
|
|
|
|
classScope,
|
|
|
|
isDefinition,
|
|
|
|
isStatic);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// Clear it. Assume it is assigned
|
2022-11-01 09:02:17 +08:00
|
|
|
StatementClassScope lastInheritScopeType = StatementClassScope::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
|
|
|
StatementClassScope inheritScopeType = getClassScope(mTokenizer[index]->text);
|
|
|
|
QString currentText = mTokenizer[index]->text;
|
|
|
|
if (currentText=='(') {
|
|
|
|
//skip to matching ')'
|
|
|
|
index=mTokenizer[index]->matchIndex;
|
2022-11-01 09:02:17 +08:00
|
|
|
} else if (inheritScopeType == StatementClassScope::None) {
|
2022-10-31 19:37:24 +08:00
|
|
|
if (currentText.front()!=','
|
|
|
|
&& currentText.front()!=':') {
|
|
|
|
QString basename = currentText;
|
2021-08-19 23:49:23 +08:00
|
|
|
//remove template staff
|
|
|
|
if (basename.endsWith('>')) {
|
|
|
|
int pBegin = basename.indexOf('<');
|
|
|
|
if (pBegin>=0)
|
|
|
|
basename.truncate(pBegin);
|
|
|
|
}
|
|
|
|
// Find the corresponding PStatement
|
|
|
|
PStatement statement = findStatementOf(mCurrentFile,basename,
|
|
|
|
classStatement->parentScope.lock(),true);
|
|
|
|
if (statement && statement->kind == StatementKind::skClass) {
|
|
|
|
inheritClassStatement(classStatement,isStruct,statement,lastInheritScopeType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
lastInheritScopeType = inheritScopeType;
|
|
|
|
if (index >= mTokenizer.tokenCount())
|
|
|
|
break;
|
|
|
|
if (mTokenizer[index]->text.front() == '{'
|
|
|
|
|| mTokenizer[index]->text.front() == ';')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::isCurrentScope(const QString &command)
|
|
|
|
{
|
|
|
|
PStatement statement = getCurrentScope();
|
|
|
|
if (!statement)
|
|
|
|
return false;
|
|
|
|
QString s = command;
|
|
|
|
// remove template staff
|
|
|
|
int i= command.indexOf('<');
|
|
|
|
if (i>=0) {
|
|
|
|
s.truncate(i);
|
|
|
|
}
|
|
|
|
return (statement->command == s);
|
|
|
|
}
|
|
|
|
|
2022-02-09 20:50:01 +08:00
|
|
|
void CppParser::addSoloScopeLevel(PStatement& statement, int line, bool shouldResetBlock)
|
2021-08-15 16:49:37 +08:00
|
|
|
{
|
|
|
|
// Add class list
|
|
|
|
|
|
|
|
PStatement parentScope;
|
2022-02-09 20:50:01 +08:00
|
|
|
if (shouldResetBlock && statement && (statement->kind == StatementKind::skBlock)) {
|
2021-08-15 16:49:37 +08:00
|
|
|
parentScope = statement->parentScope.lock();
|
|
|
|
while (parentScope && (parentScope->kind == StatementKind::skBlock)) {
|
|
|
|
parentScope = parentScope->parentScope.lock();
|
|
|
|
}
|
|
|
|
if (!parentScope)
|
|
|
|
statement.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCurrentClassScope.count()>0) {
|
|
|
|
mCurrentClassScope.back() = mClassScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-11-01 09:02:17 +08:00
|
|
|
mClassScope = StatementClassScope::None; // {}, namespace or class that doesn't exist
|
2021-08-15 16:49:37 +08:00
|
|
|
else if (statement->kind == StatementKind::skNamespace)
|
2022-11-01 09:02:17 +08:00
|
|
|
mClassScope = StatementClassScope::None;
|
2021-08-15 16:49:37 +08:00
|
|
|
else if (statement->type == "class")
|
2022-11-01 09:02:17 +08:00
|
|
|
mClassScope = StatementClassScope::Private; // classes are private by default
|
2021-08-15 16:49:37 +08:00
|
|
|
else
|
2022-11-01 09:02:17 +08:00
|
|
|
mClassScope = StatementClassScope::Public; // structs are public by default
|
2021-08-15 16:49:37 +08:00
|
|
|
mCurrentClassScope.append(mClassScope);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
PStatement currentScope = mCurrentScope.back();;
|
2022-10-22 10:44:10 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList().value(mCurrentFile);
|
2021-08-19 23:49:23 +08:00
|
|
|
if (currentScope && (currentScope->kind == StatementKind::skBlock)) {
|
|
|
|
if (currentScope->children.isEmpty()) {
|
|
|
|
// remove no children block
|
2021-08-22 05:50:26 +08:00
|
|
|
if (fileIncludes) {
|
|
|
|
fileIncludes->scopes.removeLastScope();
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
mStatementList.deleteStatement(currentScope);
|
|
|
|
} else {
|
|
|
|
fileIncludes->statements.insert(currentScope->fullName,currentScope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mCurrentScope.pop_back();
|
|
|
|
mCurrentClassScope.pop_back();
|
|
|
|
|
|
|
|
// 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) {
|
2022-11-01 09:02:17 +08:00
|
|
|
mClassScope = StatementClassScope::None;
|
2021-08-19 23:49:23 +08:00
|
|
|
} else {
|
|
|
|
mClassScope = mCurrentClassScope.back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 07:26:13 +08:00
|
|
|
void CppParser::internalClear()
|
|
|
|
{
|
|
|
|
mCurrentScope.clear();
|
|
|
|
mCurrentClassScope.clear();
|
|
|
|
mIndex = 0;
|
2022-11-01 09:02:17 +08:00
|
|
|
mClassScope = StatementClassScope::None;
|
2021-11-12 07:26:13 +08:00
|
|
|
mSkipList.clear();
|
|
|
|
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);
|
|
|
|
QStringList buffer;
|
|
|
|
if (mOnGetFileStream) {
|
|
|
|
mOnGetFileStream(file,buffer);
|
|
|
|
}
|
2022-10-23 23:06:55 +08:00
|
|
|
//we only use local include relations
|
|
|
|
mPreprocessor.setScanOptions(false, true);
|
2022-10-23 21:42:11 +08:00
|
|
|
mPreprocessor.preprocess(file,buffer);
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
bool CppParser::checkForKeyword(KeywordType& keywordType)
|
|
|
|
{
|
|
|
|
keywordType = mCppKeywords.value(mTokenizer[mIndex]->text,KeywordType::None);
|
|
|
|
switch(keywordType) {
|
|
|
|
case KeywordType::Catch:
|
|
|
|
case KeywordType::For:
|
|
|
|
case KeywordType::None:
|
|
|
|
case KeywordType::Public:
|
|
|
|
case KeywordType::Private:
|
|
|
|
case KeywordType::Enum:
|
|
|
|
case KeywordType::Inline:
|
|
|
|
case KeywordType::Namespace:
|
|
|
|
case KeywordType::Typedef:
|
|
|
|
case KeywordType::Using:
|
|
|
|
case KeywordType::Friend:
|
|
|
|
case KeywordType::Protected:
|
2022-11-01 00:01:46 +08:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-10-31 19:37:24 +08:00
|
|
|
bool CppParser::checkForMethod(QString &sType, QString &sName, int &argStartIndex,
|
2022-11-01 09:02:17 +08:00
|
|
|
int &argEndIndex, bool &isStatic, bool &isFriend)
|
2021-08-15 16:49:37 +08:00
|
|
|
{
|
2021-08-15 17:52:39 +08:00
|
|
|
PStatement scope = getCurrentScope();
|
2021-08-15 16:49:37 +08:00
|
|
|
|
2021-08-15 17:52:39 +08:00
|
|
|
if (scope && !(scope->kind == StatementKind::skNamespace
|
|
|
|
|| scope->kind == StatementKind::skClass)) { //don't care function declaration in the function's
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function template:
|
|
|
|
// compiler directives (>= 0 words), added to type
|
|
|
|
// type (>= 1 words)
|
|
|
|
// name (1 word)
|
|
|
|
// (argument list)
|
|
|
|
// ; or {
|
|
|
|
|
|
|
|
isStatic = false;
|
|
|
|
isFriend = false;
|
|
|
|
|
|
|
|
sType = ""; // should contain type "int"
|
|
|
|
sName = ""; // should contain function name "foo::function"
|
|
|
|
|
|
|
|
bool bTypeOK = false;
|
|
|
|
bool bNameOK = false;
|
|
|
|
bool bArgsOK = false;
|
|
|
|
|
|
|
|
// Don't modify index
|
|
|
|
int indexBackup = mIndex;
|
|
|
|
|
|
|
|
// Gather data for the string parts
|
|
|
|
while ((mIndex < mTokenizer.tokenCount()) && !isSeperator(mTokenizer[mIndex]->text[0])) {
|
|
|
|
if ((mIndex + 1 < mTokenizer.tokenCount())
|
2022-10-31 19:37:24 +08:00
|
|
|
&& (mTokenizer[mIndex + 1]->text == '(')) { // and start of a function
|
|
|
|
int indexAfter = mTokenizer[mIndex + 1]->matchIndex+1;
|
2021-08-15 17:52:39 +08:00
|
|
|
//it's not a function define
|
2022-10-31 19:37:24 +08:00
|
|
|
if ((indexAfter < mTokenizer.tokenCount()) && (mTokenizer[indexAfter]->text[0] == ','))
|
2021-08-15 17:52:39 +08:00
|
|
|
break;
|
|
|
|
|
2022-10-31 19:37:24 +08:00
|
|
|
if ((indexAfter < mTokenizer.tokenCount()) && (mTokenizer[indexAfter]->text[0] == ';')) {
|
|
|
|
//function can only be defined in global/namespaces/classes
|
|
|
|
if (!mCurrentScope.isEmpty()) {
|
|
|
|
PStatement currentScope=mCurrentScope.last();
|
|
|
|
//in namespace, it might be function or object initilization
|
|
|
|
if (currentScope->kind == StatementKind::skNamespace
|
|
|
|
&& isNotFuncArgs(mIndex + 1,mTokenizer[mIndex + 1]->matchIndex)) {
|
|
|
|
break;
|
|
|
|
//not in class, it can't be a valid function definition
|
|
|
|
} else if (currentScope->kind != StatementKind::skClass)
|
|
|
|
break;
|
|
|
|
//variable can't be initialized in class definition, it must be a function
|
|
|
|
} else if (isNotFuncArgs(mIndex + 1,mTokenizer[mIndex + 1]->matchIndex))
|
2021-08-15 17:52:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
sName = mTokenizer[mIndex]->text;
|
2022-10-31 19:37:24 +08:00
|
|
|
argStartIndex = mIndex+1;
|
|
|
|
argEndIndex = mTokenizer[mIndex + 1]->matchIndex;
|
2021-08-15 17:52:39 +08:00
|
|
|
bTypeOK = !sType.isEmpty();
|
|
|
|
bNameOK = !sName.isEmpty();
|
2022-10-31 19:37:24 +08:00
|
|
|
bArgsOK = true;
|
2022-11-01 09:02:17 +08:00
|
|
|
mIndex = argEndIndex+1;
|
2021-08-15 17:52:39 +08:00
|
|
|
|
|
|
|
// Allow constructor/destructor too
|
|
|
|
if (!bTypeOK) {
|
|
|
|
// Check for constructor/destructor outside class body
|
|
|
|
int delimPos = sName.indexOf("::");
|
|
|
|
if (delimPos >= 0) {
|
|
|
|
bTypeOK = true;
|
|
|
|
sType = sName.mid(0, delimPos);
|
|
|
|
|
|
|
|
// remove template staff
|
|
|
|
int pos1 = sType.indexOf('<');
|
|
|
|
if (pos1>=0) {
|
|
|
|
sType.truncate(pos1);
|
|
|
|
sName = sType+sName.mid(delimPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we inside a class body?
|
|
|
|
if (!bTypeOK) {
|
|
|
|
sType = mTokenizer[mIndex]->text;
|
|
|
|
if (sType[0] == '~')
|
|
|
|
sType.remove(0,1);
|
2021-08-19 23:49:23 +08:00
|
|
|
bTypeOK = isCurrentScope(sType); // constructor/destructor
|
2021-08-15 17:52:39 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else {
|
2021-08-18 17:02:57 +08:00
|
|
|
//if IsValidIdentifier(mTokenizer[mIndex]->text) then
|
2021-08-15 17:52:39 +08:00
|
|
|
// Still walking through type
|
2022-11-01 09:02:17 +08:00
|
|
|
QString s = mTokenizer[mIndex]->text;
|
2021-08-15 17:52:39 +08:00
|
|
|
if (s == "static")
|
|
|
|
isStatic = true;
|
|
|
|
if (s == "friend")
|
|
|
|
isFriend = true;
|
|
|
|
if (!s.isEmpty() && !(s=="extern"))
|
|
|
|
sType = sType + ' '+ s;
|
|
|
|
bTypeOK = !sType.isEmpty();
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correct function, don't jump over
|
|
|
|
if (bTypeOK && bNameOK && bArgsOK) {
|
|
|
|
sType = sType.trimmed(); // should contain type "int"
|
|
|
|
sName = sName.trimmed(); // should contain function name "foo::function"
|
|
|
|
return true;
|
2022-11-01 09:02:17 +08:00
|
|
|
} else {
|
|
|
|
mIndex = indexBackup;
|
2021-08-15 17:52:39 +08:00
|
|
|
return false;
|
2022-11-01 09:02:17 +08:00
|
|
|
}
|
2021-08-15 17:52:39 +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-01 09:02:17 +08:00
|
|
|
bool CppParser::checkForScope(KeywordType keywordType)
|
2021-08-15 17:52:39 +08:00
|
|
|
{
|
2022-11-01 09:02:17 +08:00
|
|
|
return ( (keywordType == KeywordType::Public || keywordType == KeywordType::Protected
|
|
|
|
|| keywordType == KeywordType::Private)
|
2022-11-01 00:01:46 +08:00
|
|
|
&& mIndex+1 < mTokenizer.tokenCount()
|
|
|
|
&& mTokenizer[mIndex + 1]->text == ':'
|
2021-08-15 17:52:39 +08:00
|
|
|
);
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
|
2021-08-15 20:25:54 +08:00
|
|
|
void CppParser::checkForSkipStatement()
|
|
|
|
{
|
|
|
|
if ((mSkipList.count()>0) && (mIndex == mSkipList.back())) { // skip to next ';'
|
|
|
|
do {
|
2022-11-01 09:02:17 +08:00
|
|
|
if (isLeftParenthesis(mTokenizer[mIndex]->text))
|
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
else
|
|
|
|
mIndex++;
|
2021-08-15 20:25:54 +08:00
|
|
|
} while ((mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text[0] != ';'));
|
|
|
|
mIndex++; //skip ';'
|
|
|
|
mSkipList.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
bool CppParser::checkForStructs(KeywordType keywordType)
|
2021-08-15 20:25:54 +08:00
|
|
|
{
|
|
|
|
int dis = 0;
|
2022-11-01 09:02:17 +08:00
|
|
|
if (keywordType == KeywordType::Friend
|
|
|
|
|| keywordType == KeywordType::Scope)
|
2021-08-15 20:25:54 +08:00
|
|
|
dis = 1;
|
|
|
|
if (mIndex >= mTokenizer.tokenCount() - 2 - dis)
|
|
|
|
return false;
|
|
|
|
QString word = mTokenizer[mIndex+dis]->text;
|
2021-08-16 00:47:35 +08:00
|
|
|
int keyLen = calcKeyLenForStruct(word);
|
2021-08-15 20:25:54 +08:00
|
|
|
if (keyLen<0)
|
|
|
|
return false;
|
2022-02-07 09:53:02 +08:00
|
|
|
bool result = (word.length() == keyLen) || isSpaceChar(word[keyLen])
|
2021-08-15 20:25:54 +08:00
|
|
|
|| (word[keyLen] == '[');
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
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'},
|
|
|
|
// ...
|
|
|
|
// };
|
|
|
|
while (i < mTokenizer.tokenCount()) {
|
|
|
|
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;
|
|
|
|
QString word = mTokenizer[mIndex + 1]->text;
|
|
|
|
int keyLen = calcKeyLenForStruct(word);
|
|
|
|
if (keyLen<0)
|
|
|
|
return false;
|
|
|
|
return (word.length() == keyLen) || isSpaceChar(word[keyLen]) || word[keyLen]=='[';
|
|
|
|
}
|
|
|
|
|
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-01 00:01:46 +08:00
|
|
|
bool CppParser::checkForVar(bool& isFunctionPointer)
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
|
|
|
// Be pessimistic
|
|
|
|
bool result = false;
|
2022-11-01 00:01:46 +08:00
|
|
|
isFunctionPointer = false;
|
2021-08-16 00:47:35 +08:00
|
|
|
|
|
|
|
// Store old index
|
|
|
|
int indexBackup = mIndex;
|
2022-11-01 09:02:17 +08:00
|
|
|
KeywordType keywordType;
|
2021-08-16 00:47:35 +08:00
|
|
|
|
|
|
|
// Use mIndex so we can reuse checking functions
|
|
|
|
if (mIndex + 1 < mTokenizer.tokenCount()) {
|
|
|
|
// Check the current and the next token
|
|
|
|
for (int i = 0; i<=1; i++) {
|
2022-11-01 09:02:17 +08:00
|
|
|
if (checkForKeyword(keywordType)
|
2021-08-16 00:47:35 +08:00
|
|
|
|| isInvalidVarPrefixChar(mTokenizer[mIndex]->text.front())
|
|
|
|
|| (mTokenizer[mIndex]->text.back() == '.')
|
|
|
|
|| (
|
|
|
|
(mTokenizer[mIndex]->text.length() > 1) &&
|
|
|
|
(mTokenizer[mIndex]->text[mTokenizer[mIndex]->text.length() - 2] == '-') &&
|
|
|
|
(mTokenizer[mIndex]->text[mTokenizer[mIndex]->text.length() - 1] == '>'))
|
|
|
|
) {
|
|
|
|
// Reset index and fail
|
|
|
|
mIndex = indexBackup;
|
|
|
|
return false;
|
|
|
|
} // Could be a function pointer?
|
2022-11-01 00:01:46 +08:00
|
|
|
else if (mTokenizer[mIndex]->text == '(') {
|
|
|
|
int nextIndex = mTokenizer[mIndex]->matchIndex+1;
|
|
|
|
if ( (nextIndex >= mTokenizer.tokenCount())
|
|
|
|
|| (mTokenizer[nextIndex]->text != '(')
|
|
|
|
|| !mTokenizer[mIndex+1]->text.startsWith('*')) {
|
2021-08-16 00:47:35 +08:00
|
|
|
// Reset index and fail
|
|
|
|
mIndex = indexBackup;
|
|
|
|
return false;
|
2022-11-01 00:01:46 +08:00
|
|
|
} else {
|
|
|
|
nextIndex = mTokenizer[nextIndex]->matchIndex+1;
|
|
|
|
if (mTokenizer[mIndex]->text!=';') {
|
|
|
|
mIndex = indexBackup;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
isFunctionPointer=true;
|
|
|
|
mIndex = indexBackup;
|
|
|
|
return true;
|
2021-08-16 00:47:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if we do not find a comma or a semicolon or a ( (inline constructor)
|
|
|
|
while (mIndex < mTokenizer.tokenCount()) {
|
|
|
|
if (mTokenizer[mIndex]->text.front() == '#'
|
2022-11-01 00:01:46 +08:00
|
|
|
|| mTokenizer[mIndex]->text == '}'
|
2022-11-01 09:02:17 +08:00
|
|
|
|| checkForKeyword(keywordType)) {
|
2021-08-16 00:47:35 +08:00
|
|
|
break; // fail
|
|
|
|
} else if (mTokenizer[mIndex]->text.front() == ','
|
|
|
|
|| mTokenizer[mIndex]->text.front() == ';'
|
|
|
|
|| mTokenizer[mIndex]->text.front() == '{') {
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-11-01 00:01:46 +08:00
|
|
|
//skip '(' ')'
|
|
|
|
if (mTokenizer[mIndex]->text=='(' || mTokenizer[mIndex]->text=='[')
|
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex;
|
|
|
|
else
|
|
|
|
mIndex++;
|
2021-08-16 00:47:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Revert to the point we started at
|
|
|
|
mIndex = indexBackup;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::getCurrentBlockEndSkip()
|
|
|
|
{
|
|
|
|
if (mBlockEndSkips.isEmpty())
|
|
|
|
return mTokenizer.tokenCount()+1;
|
|
|
|
return mBlockEndSkips.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::getCurrentBlockBeginSkip()
|
|
|
|
{
|
|
|
|
if (mBlockBeginSkips.isEmpty())
|
|
|
|
return mTokenizer.tokenCount()+1;
|
|
|
|
return mBlockBeginSkips.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::getCurrentInlineNamespaceEndSkip()
|
|
|
|
{
|
|
|
|
if (mInlineNamespaceEndSkips.isEmpty())
|
|
|
|
return mTokenizer.tokenCount()+1;
|
|
|
|
return mInlineNamespaceEndSkips.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
PStatement CppParser::getCurrentScope()
|
|
|
|
{
|
|
|
|
if (mCurrentScope.isEmpty()) {
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
return mCurrentScope.back();
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:08:46 +08:00
|
|
|
void CppParser::getFullNamespace(const QString &phrase, QString &sNamespace, QString &member)
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
QString CppParser::getFullStatementName(const QString &command, const PStatement& parent)
|
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);
|
|
|
|
}
|
|
|
|
PStatement result = findStatementOf(mCurrentFile,s,parentScope,true);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
QString CppParser::getStatementKey(const QString &sName, const QString &sType, const QString &sNoNameArgs)
|
|
|
|
{
|
|
|
|
return sName + "--" + sType + "--" + sNoNameArgs;
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::getTypeDef(const PStatement& statement,
|
|
|
|
const QString& fileName, const QString& aType)
|
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;
|
|
|
|
PStatement result = findTypeDefinitionOf(fileName,statement->type, statement->parentScope.lock());
|
|
|
|
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) {
|
|
|
|
PStatement result = findAliasedStatement(statement);
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
int startLine= mTokenizer[mIndex]->line;
|
|
|
|
mIndex++; // skip for/catch;
|
2021-08-18 05:34:04 +08:00
|
|
|
if (!((mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text.startsWith('('))))
|
|
|
|
return;
|
|
|
|
//skip params
|
2022-11-01 09:02:17 +08:00
|
|
|
int i2=mTokenizer[mIndex]->matchIndex+1;
|
2021-08-18 05:34:04 +08:00
|
|
|
if (i2>=mTokenizer.tokenCount())
|
|
|
|
return;
|
|
|
|
if (mTokenizer[i2]->text.startsWith('{')) {
|
|
|
|
mBlockBeginSkips.append(i2);
|
2022-11-01 09:02:17 +08:00
|
|
|
int i = indexOfMatchingBrace(i2);
|
|
|
|
// if (i==i2) {
|
|
|
|
// mBlockEndSkips.append(mTokenizer.tokenCount());
|
|
|
|
// } else {
|
|
|
|
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(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
2022-02-09 20:50:01 +08:00
|
|
|
addSoloScopeLevel(block,startLine,false);
|
2022-11-01 09:02:17 +08:00
|
|
|
if (!mTokenizer[mIndex+1]->text.contains("..."))
|
|
|
|
scanMethodArgs(block,mIndex, mTokenizer[mIndex]->matchIndex);
|
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex+1;
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::handleEnum()
|
|
|
|
{
|
|
|
|
//todo : handle enum class
|
|
|
|
QString enumName = "";
|
|
|
|
bool isEnumClass = false;
|
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
mIndex++; //skip 'enum'
|
2021-08-27 00:49:50 +08:00
|
|
|
|
|
|
|
if (mIndex < mTokenizer.tokenCount() && mTokenizer[mIndex]->text == "class") {
|
|
|
|
//enum class
|
|
|
|
isEnumClass = true;
|
|
|
|
mIndex++; //skip class
|
|
|
|
|
|
|
|
}
|
|
|
|
if ((mIndex< mTokenizer.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?
|
2021-08-27 00:49:50 +08:00
|
|
|
if ((i + 1 < mTokenizer.tokenCount()) && mTokenizer[i]->text.startsWith('}')) {
|
|
|
|
if (!mTokenizer[i + 1]->text.startsWith(';'))
|
|
|
|
enumName = mTokenizer[i + 1]->text.trimmed();
|
|
|
|
}
|
2021-11-01 23:29:03 +08:00
|
|
|
} else if (mIndex+1< mTokenizer.tokenCount() && mTokenizer[mIndex+1]->text.startsWith('{')){ // enum NAME {...};
|
2021-08-18 05:34:04 +08:00
|
|
|
if ( (mIndex< mTokenizer.tokenCount()) && mTokenizer[mIndex]->text == "class") {
|
|
|
|
//enum class {...} NAME
|
|
|
|
isEnumClass = true;
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
while ((mIndex < mTokenizer.tokenCount()) &&
|
|
|
|
!(mTokenizer[mIndex]->text.startsWith('{')
|
|
|
|
|| mTokenizer[mIndex]->text.startsWith(';'))) {
|
|
|
|
enumName += mTokenizer[mIndex]->text + ' ';
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
enumName = enumName.trimmed();
|
|
|
|
// An opening brace must be present after NAME
|
|
|
|
if ((mIndex >= mTokenizer.tokenCount()) || !mTokenizer[mIndex]->text.startsWith('{'))
|
|
|
|
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;
|
|
|
|
if (!enumName.isEmpty()) {
|
|
|
|
if (isEnumClass) {
|
|
|
|
enumStatement=addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"enum class",
|
|
|
|
enumName,
|
|
|
|
"",
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-18 05:34:04 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skEnumClassType,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
} else {
|
|
|
|
enumStatement=addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"enum",
|
|
|
|
enumName,
|
|
|
|
"",
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-18 05:34:04 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skEnumType,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
}
|
2021-08-27 00:49:50 +08:00
|
|
|
} else {
|
|
|
|
enumStatement = getCurrentScope();
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip opening brace
|
|
|
|
mIndex++;
|
|
|
|
|
|
|
|
// Call every member "enum NAME ITEMNAME"
|
|
|
|
QString lastType("enum");
|
|
|
|
if (!enumName.isEmpty())
|
|
|
|
lastType += ' ' + enumName;
|
|
|
|
QString cmd;
|
|
|
|
QString args;
|
|
|
|
if (!mTokenizer[mIndex]->text.startsWith('}')) {
|
|
|
|
while ((mIndex < mTokenizer.tokenCount()) &&
|
|
|
|
!isblockChar(mTokenizer[mIndex]->text[0])) {
|
|
|
|
if (!mTokenizer[mIndex]->text.startsWith(',')) {
|
|
|
|
if (mTokenizer[mIndex]->text.endsWith(']')) { //array; break args
|
|
|
|
int p = mTokenizer[mIndex]->text.indexOf('[');
|
|
|
|
cmd = mTokenizer[mIndex]->text.mid(0,p);
|
|
|
|
args = mTokenizer[mIndex]->text.mid(p);
|
|
|
|
} else {
|
|
|
|
cmd = mTokenizer[mIndex]->text;
|
|
|
|
args = "";
|
|
|
|
}
|
2021-09-26 19:40:52 +08:00
|
|
|
if (isEnumClass) {
|
2021-08-18 05:34:04 +08:00
|
|
|
if (enumStatement) {
|
|
|
|
addStatement(
|
|
|
|
enumStatement,
|
|
|
|
mCurrentFile,
|
|
|
|
lastType,
|
|
|
|
cmd,
|
|
|
|
args,
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2022-01-22 21:07:41 +08:00
|
|
|
mTokenizer[mIndex]->line,
|
2021-08-18 05:34:04 +08:00
|
|
|
StatementKind::skEnum,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-26 19:40:52 +08:00
|
|
|
if (enumStatement) {
|
|
|
|
addStatement(
|
|
|
|
enumStatement,
|
|
|
|
mCurrentFile,
|
|
|
|
lastType,
|
|
|
|
cmd,
|
|
|
|
args,
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2022-01-22 21:07:41 +08:00
|
|
|
mTokenizer[mIndex]->line,
|
2021-09-26 19:40:52 +08:00
|
|
|
StatementKind::skEnum,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
}
|
2021-08-18 05:34:04 +08:00
|
|
|
addStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
lastType,
|
|
|
|
cmd,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skEnum,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
2021-08-18 05:34:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mIndex ++ ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Step over closing brace
|
|
|
|
if ((mIndex < mTokenizer.tokenCount()) && mTokenizer[mIndex]->text.startsWith('}'))
|
|
|
|
mIndex++;
|
2021-08-17 23:30:14 +08:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:42:32 +08:00
|
|
|
void CppParser::handleForBlock()
|
|
|
|
{
|
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
mIndex++; // skip for/catch;
|
|
|
|
if (!(mIndex < mTokenizer.tokenCount()))
|
|
|
|
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(;)
|
|
|
|
if (i2>=mTokenizer.tokenCount())
|
|
|
|
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(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
|
|
|
|
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-01 09:02:17 +08:00
|
|
|
case KeywordType::SkipAfterSemicolon:
|
|
|
|
// Skip to ; and over it
|
|
|
|
mIndex = indexOfNextSemicolon(mIndex)+1;
|
2021-08-18 13:42:32 +08:00
|
|
|
break;
|
2022-11-01 09:02:17 +08:00
|
|
|
case KeywordType::SkipAfterColon:
|
|
|
|
// Skip to : and over it
|
|
|
|
mIndex = indexOfNextColon(mIndex)+1;
|
2021-08-18 13:42:32 +08:00
|
|
|
break;
|
2022-11-01 09:02:17 +08:00
|
|
|
case KeywordType::SkipAfterParenthesis:
|
|
|
|
// skip pass ()
|
|
|
|
mIndex = indexPassParenthesis(mIndex);
|
2021-08-18 13:42:32 +08:00
|
|
|
break;
|
2022-11-01 09:02:17 +08:00
|
|
|
case KeywordType::SkipToLeftBrace:
|
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;
|
2022-11-01 09:02:17 +08:00
|
|
|
case KeywordType::SkipAfterBrace:
|
|
|
|
// Skip pass {}
|
|
|
|
mIndex = indexPassBraces(mIndex);
|
2021-08-18 13:42:32 +08:00
|
|
|
break;
|
2021-08-18 17:02:57 +08:00
|
|
|
default:
|
|
|
|
break;
|
2021-08-18 13:42:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:37:24 +08:00
|
|
|
void CppParser::handleMethod(const QString &sType, const QString &sName, int argStart, int argEnd, bool isStatic, bool isFriend)
|
2021-08-18 17:02:57 +08:00
|
|
|
{
|
|
|
|
bool isValid = true;
|
|
|
|
bool isDeclaration = false; // assume it's not a prototype
|
|
|
|
int i = mIndex;
|
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
|
|
|
|
if (mIndex >= mTokenizer.tokenCount()) // not finished define, just skip it;
|
|
|
|
return;
|
|
|
|
|
|
|
|
PStatement functionClass = getCurrentScope();
|
|
|
|
// Check if this is a prototype
|
|
|
|
if (mTokenizer[mIndex]->text.startsWith(';')
|
|
|
|
|| mTokenizer[mIndex]->text.startsWith('}')) {// prototype
|
|
|
|
isDeclaration = true;
|
|
|
|
} else {
|
|
|
|
// Find the function body start after the inherited constructor
|
|
|
|
if ((mIndex < mTokenizer.tokenCount()) && mTokenizer[mIndex]->text.startsWith(':')) {
|
|
|
|
while ((mIndex < mTokenizer.tokenCount()) && !isblockChar(mTokenizer[mIndex]->text.front()))
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Still a prototype
|
|
|
|
if ((mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text.startsWith(';')
|
|
|
|
|| mTokenizer[mIndex]->text.startsWith('}'))) {// prototype
|
|
|
|
isDeclaration = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString scopelessName;
|
|
|
|
PStatement functionStatement;
|
|
|
|
if (isFriend && isDeclaration && functionClass) {
|
|
|
|
int delimPos = sName.indexOf("::");
|
|
|
|
if (delimPos >= 0) {
|
|
|
|
scopelessName = sName.mid(delimPos+2);
|
|
|
|
} else
|
|
|
|
scopelessName = sName;
|
|
|
|
//TODO : we should check namespace
|
|
|
|
functionClass->friends.insert(scopelessName);
|
|
|
|
} else if (isValid) {
|
|
|
|
// Use the class the function belongs to as the parent ID if the function is declared outside of the class body
|
|
|
|
int delimPos = sName.indexOf("::");
|
|
|
|
QString scopelessName;
|
|
|
|
QString parentClassName;
|
|
|
|
if (delimPos >= 0) {
|
|
|
|
// Provide Bar instead of Foo::Bar
|
2022-01-25 13:53:55 +08:00
|
|
|
scopelessName = sName.mid(delimPos+2);
|
2021-08-18 17:02:57 +08:00
|
|
|
|
|
|
|
// Check what class this function belongs to
|
|
|
|
parentClassName = sName.mid(0, delimPos);
|
|
|
|
functionClass = getIncompleteClass(parentClassName,getCurrentScope());
|
|
|
|
} else
|
|
|
|
scopelessName = sName;
|
|
|
|
|
|
|
|
StatementKind functionKind;
|
|
|
|
// Determine function type
|
|
|
|
if (scopelessName == sType) {
|
|
|
|
functionKind = StatementKind::skConstructor;
|
|
|
|
} else if (scopelessName == '~' + sType) {
|
|
|
|
functionKind = StatementKind::skDestructor;
|
|
|
|
} else {
|
|
|
|
functionKind = StatementKind::skFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For function definitions, the parent class is given. Only use that as a parent
|
|
|
|
if (!isDeclaration) {
|
|
|
|
functionStatement=addStatement(
|
|
|
|
functionClass,
|
|
|
|
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(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
isStatic);
|
2021-10-23 16:35:36 +08:00
|
|
|
scanMethodArgs(functionStatement, sArgs);
|
2021-08-18 17:02:57 +08:00
|
|
|
// add variable this to the class function
|
|
|
|
if (functionClass && functionClass->kind == StatementKind::skClass &&
|
|
|
|
!isStatic) {
|
|
|
|
//add this to non-static class member function
|
|
|
|
addStatement(
|
|
|
|
functionStatement,
|
|
|
|
mCurrentFile,
|
|
|
|
functionClass->command,
|
|
|
|
"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,
|
|
|
|
StatementClassScope::None,
|
2021-08-18 17:02:57 +08:00
|
|
|
true,
|
|
|
|
false);
|
|
|
|
}
|
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,
|
|
|
|
StatementClassScope::None,
|
2021-11-17 17:18:02 +08:00
|
|
|
true,
|
|
|
|
false);
|
2021-08-18 17:02:57 +08:00
|
|
|
} else {
|
|
|
|
functionStatement = addStatement(
|
2021-08-27 00:49:50 +08:00
|
|
|
functionClass,
|
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(),
|
|
|
|
mClassScope,
|
|
|
|
false,
|
|
|
|
isStatic);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((mIndex < mTokenizer.tokenCount()) && mTokenizer[mIndex]->text.startsWith('{')) {
|
|
|
|
addSoloScopeLevel(functionStatement,startLine);
|
|
|
|
mIndex++; //skip '{'
|
|
|
|
} else if ((mIndex < mTokenizer.tokenCount()) && mTokenizer[mIndex]->text.startsWith(';')) {
|
|
|
|
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
|
|
|
{
|
|
|
|
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'
|
|
|
|
|
|
|
|
if (!isLetterChar(mTokenizer[mIndex]->text.front()))
|
|
|
|
//wrong namespace define, stop handling
|
|
|
|
return;
|
|
|
|
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++;
|
|
|
|
if (mIndex>=mTokenizer.tokenCount())
|
|
|
|
return;
|
|
|
|
QString aliasName;
|
|
|
|
if ((mIndex+2<mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text.front() == '=')) {
|
|
|
|
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(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
mIndex+=2; //skip ;
|
|
|
|
return;
|
|
|
|
} else if (isInline) {
|
|
|
|
//inline namespace , just skip it
|
|
|
|
// Skip to '{'
|
|
|
|
while ((mIndex<mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text.front() != '{'))
|
|
|
|
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)
|
|
|
|
mInlineNamespaceEndSkips.append(mTokenizer.tokenCount());
|
|
|
|
else
|
|
|
|
mInlineNamespaceEndSkips.append(i);
|
|
|
|
if (mIndex<mTokenizer.tokenCount())
|
|
|
|
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(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
2021-08-18 17:02:57 +08:00
|
|
|
addSoloScopeLevel(namespaceStatement,startLine);
|
|
|
|
|
|
|
|
// Skip to '{'
|
|
|
|
while ((mIndex<mTokenizer.tokenCount()) && !mTokenizer[mIndex]->text.startsWith('{'))
|
|
|
|
mIndex++;
|
|
|
|
if (mIndex<mTokenizer.tokenCount())
|
|
|
|
mIndex++; //skip '{'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::handleOtherTypedefs()
|
|
|
|
{
|
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
// Skip typedef word
|
|
|
|
mIndex++;
|
|
|
|
|
|
|
|
if (mIndex>=mTokenizer.tokenCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mTokenizer[mIndex]->text.front() == '('
|
|
|
|
|| mTokenizer[mIndex]->text.front() == ','
|
|
|
|
|| mTokenizer[mIndex]->text.front() == ';') { // error typedef
|
|
|
|
//skip to ;
|
|
|
|
while ((mIndex< mTokenizer.tokenCount()) && !mTokenizer[mIndex]->text.startsWith(';'))
|
|
|
|
mIndex++;
|
|
|
|
//skip ;
|
2021-08-26 17:48:23 +08:00
|
|
|
if ((mIndex< mTokenizer.tokenCount()) && mTokenizer[mIndex]->text.startsWith(';'))
|
2021-08-18 17:02:57 +08:00
|
|
|
mIndex++;
|
|
|
|
return;
|
|
|
|
}
|
2021-08-27 08:52:20 +08:00
|
|
|
if ((mIndex+1<mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex+1]->text == ';')) {
|
|
|
|
//no old type
|
|
|
|
QString newType = mTokenizer[mIndex]->text.trimmed();
|
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"",
|
|
|
|
newType,
|
|
|
|
"",
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-27 08:52:20 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
mIndex+=2; //skip ;
|
|
|
|
return;
|
|
|
|
}
|
2021-08-18 17:02:57 +08:00
|
|
|
QString oldType;
|
2021-08-27 08:52:20 +08:00
|
|
|
|
2021-08-18 17:02:57 +08:00
|
|
|
// Walk up to first new word (before first comma or ;)
|
|
|
|
while(true) {
|
|
|
|
oldType += mTokenizer[mIndex]->text + ' ';
|
|
|
|
mIndex++;
|
2021-08-18 21:57:42 +08:00
|
|
|
if (mIndex+1>=mTokenizer.tokenCount())
|
|
|
|
break;
|
|
|
|
if (mTokenizer[mIndex + 1]->text.front() == ','
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front() == ';')
|
2021-08-18 17:02:57 +08:00
|
|
|
break;
|
|
|
|
if ((mIndex + 2 < mTokenizer.tokenCount())
|
2021-08-18 21:57:42 +08:00
|
|
|
&& (mTokenizer[mIndex + 2]->text.front() == ','
|
|
|
|
|| mTokenizer[mIndex + 2]->text.front() == ';')
|
|
|
|
&& (mTokenizer[mIndex + 1]->text.front() == '('))
|
2021-08-18 17:02:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
oldType = oldType.trimmed();
|
|
|
|
|
|
|
|
// Add synonyms for old
|
|
|
|
if ((mIndex+1 < mTokenizer.tokenCount()) && !oldType.isEmpty()) {
|
2021-08-18 21:57:42 +08:00
|
|
|
QString newType;
|
2021-08-18 17:02:57 +08:00
|
|
|
while(true) {
|
|
|
|
// Support multiword typedefs
|
2021-08-18 21:57:42 +08:00
|
|
|
if ((mIndex + 2 < mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex + 2]->text.front() == ','
|
|
|
|
|| mTokenizer[mIndex + 2]->text.front() == ';')
|
|
|
|
&& (mTokenizer[mIndex + 1]->text.front() == '(')) {
|
2021-08-18 17:02:57 +08:00
|
|
|
//valid function define
|
2021-08-18 21:57:42 +08:00
|
|
|
newType = mTokenizer[mIndex]->text.trimmed();
|
|
|
|
newType = newType.mid(1,newType.length()-2); //remove '(' and ')';
|
|
|
|
newType = newType.trimmed();
|
|
|
|
int p = newType.lastIndexOf(' ');
|
|
|
|
if (p>=0)
|
|
|
|
newType.truncate(p+1);
|
2022-10-31 19:37:24 +08:00
|
|
|
QString args=mTokenizer[mIndex + 1]->text;
|
2021-08-18 21:57:42 +08:00
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
oldType,
|
|
|
|
newType,
|
2022-10-31 19:37:24 +08:00
|
|
|
args,
|
|
|
|
removeArgNames(args),
|
2021-08-18 21:57:42 +08:00
|
|
|
"",
|
|
|
|
startLine,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
newType = "";
|
|
|
|
//skip to ',' or ';'
|
|
|
|
mIndex+=2;
|
|
|
|
} else if (mTokenizer[mIndex+1]->text.front() ==','
|
|
|
|
|| mTokenizer[mIndex+1]->text.front() ==';'
|
|
|
|
|| mTokenizer[mIndex+1]->text.front() =='(') {
|
|
|
|
newType += mTokenizer[mIndex]->text;
|
|
|
|
newType = newType.trimmed();
|
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
oldType,
|
|
|
|
newType,
|
|
|
|
"",
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-18 21:57:42 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
newType = "";
|
|
|
|
mIndex++;
|
|
|
|
} else {
|
|
|
|
newType += mTokenizer[mIndex]->text + ' ';
|
|
|
|
mIndex++;
|
|
|
|
}
|
2022-03-16 16:24:39 +08:00
|
|
|
if (mIndex < mTokenizer.tokenCount() && mTokenizer[mIndex]->text[0] == ',' )
|
|
|
|
mIndex++;
|
2021-08-26 17:48:23 +08:00
|
|
|
if ((mIndex>= mTokenizer.tokenCount()) || (mTokenizer[mIndex]->text[0] == ';'))
|
2021-08-18 21:57:42 +08:00
|
|
|
break;
|
2021-08-18 17:02:57 +08:00
|
|
|
if (mIndex+1 >= mTokenizer.tokenCount())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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,
|
|
|
|
StatementClassScope::None,
|
2022-10-31 19:37:24 +08:00
|
|
|
true,
|
|
|
|
false);
|
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
|
|
|
}
|
|
|
|
|
2022-10-31 19:37:24 +08:00
|
|
|
StatementClassScope CppParser::getClassScope(const QString& text) {
|
|
|
|
if (!text.isEmpty() && text[0]=='p') {
|
|
|
|
if (text=="public")
|
2022-11-01 09:02:17 +08:00
|
|
|
return StatementClassScope::Public;
|
2022-10-31 19:37:24 +08:00
|
|
|
else if (text=="private")
|
2022-11-01 09:02:17 +08:00
|
|
|
return StatementClassScope::Private;
|
2022-10-31 19:37:24 +08:00
|
|
|
else if (text=="protected")
|
2022-11-01 09:02:17 +08:00
|
|
|
return StatementClassScope::Protected;
|
2022-10-31 19:37:24 +08:00
|
|
|
}
|
2022-11-01 09:02:17 +08:00
|
|
|
return StatementClassScope::None;
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 09:02:17 +08:00
|
|
|
StatementClassScope CppParser::getClassScope(KeywordType keywordType)
|
2021-08-19 12:01:01 +08:00
|
|
|
{
|
2022-11-01 09:02:17 +08:00
|
|
|
switch(keywordType) {
|
|
|
|
case KeywordType::Public:
|
|
|
|
return StatementClassScope::Public;
|
|
|
|
case KeywordType::Private:
|
|
|
|
return StatementClassScope::Private;
|
|
|
|
case KeywordType::Protected:
|
|
|
|
return StatementClassScope::Protected;
|
|
|
|
default:
|
|
|
|
return StatementClassScope::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::handleScope(KeywordType keywordType)
|
|
|
|
{
|
|
|
|
mClassScope = getClassScope(keywordType);
|
2021-08-19 12:01:01 +08:00
|
|
|
mIndex+=2; // the scope is followed by a ':'
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CppParser::handleStatement()
|
|
|
|
{
|
2022-10-31 19:37:24 +08:00
|
|
|
QString funcType,funcName;
|
|
|
|
int argStart,argEnd;
|
2021-08-19 12:01:01 +08:00
|
|
|
bool isStatic, isFriend;
|
|
|
|
int idx=getCurrentBlockEndSkip();
|
|
|
|
int idx2=getCurrentBlockBeginSkip();
|
|
|
|
int idx3=getCurrentInlineNamespaceEndSkip();
|
2022-11-01 09:02:17 +08:00
|
|
|
KeywordType keywordType;
|
2022-11-01 00:01:46 +08:00
|
|
|
bool isFunctionPointer;
|
2021-08-19 12:01:01 +08:00
|
|
|
if (mIndex >= idx2) {
|
|
|
|
//skip (previous handled) block begin
|
|
|
|
mBlockBeginSkips.pop_back();
|
|
|
|
if (mIndex == idx2)
|
|
|
|
mIndex++;
|
|
|
|
else if (mIndex<mTokenizer.tokenCount()) //error happens, but we must remove an (error) added scope
|
|
|
|
removeScopeLevel(mTokenizer[mIndex]->line);
|
|
|
|
} else if (mIndex >= idx) {
|
|
|
|
//skip (previous handled) block end
|
|
|
|
mBlockEndSkips.pop_back();
|
|
|
|
if (idx+1 < mTokenizer.tokenCount())
|
|
|
|
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(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
2021-08-19 12:01:01 +08:00
|
|
|
addSoloScopeLevel(block,mTokenizer[mIndex]->line);
|
|
|
|
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-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();
|
2022-11-01 09:02:17 +08:00
|
|
|
} else if (checkForScope(keywordType)) { // public /private/proteced
|
|
|
|
handleScope(keywordType);
|
|
|
|
} else if (keywordType==KeywordType::Enum) {
|
2021-08-19 12:01:01 +08:00
|
|
|
handleEnum();
|
2022-11-01 09:02:17 +08:00
|
|
|
} else if (keywordType==KeywordType::Typedef) {
|
2021-08-19 12:01:01 +08:00
|
|
|
if (mIndex+1 < mTokenizer.tokenCount()) {
|
|
|
|
if (checkForTypedefStruct()) { // typedef struct something
|
|
|
|
mIndex++; // skip 'typedef'
|
|
|
|
handleStructs(true);
|
|
|
|
} else if (checkForTypedefEnum()) { // typedef enum something
|
|
|
|
mIndex++; // skip 'typedef'
|
|
|
|
handleEnum();
|
|
|
|
} 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-10-31 19:37:24 +08:00
|
|
|
} else if (checkForMethod(funcType, funcName, argStart,argEnd, isStatic, isFriend)) {
|
|
|
|
handleMethod(funcType, funcName, argStart, argEnd, isStatic, isFriend); // don't recalculate parts
|
2022-11-01 00:01:46 +08:00
|
|
|
} else if (checkForVar(isFunctionPointer)) {
|
|
|
|
handleVar(isFunctionPointer);
|
2021-08-19 12:01:01 +08:00
|
|
|
} else
|
|
|
|
mIndex++;
|
|
|
|
|
|
|
|
checkForSkipStatement();
|
|
|
|
|
|
|
|
return mIndex < mTokenizer.tokenCount();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::handleStructs(bool isTypedef)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
bool isStruct = ("struct" == prefix) || ("union"==prefix);
|
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
|
|
|
|
mIndex++; //skip struct/class/union
|
|
|
|
|
|
|
|
if (mIndex>=mTokenizer.tokenCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Do not modifiy index initially
|
|
|
|
int i = mIndex;
|
|
|
|
|
|
|
|
// Skip until the struct body starts
|
|
|
|
while ((i < mTokenizer.tokenCount()) && ! (
|
|
|
|
mTokenizer[i]->text.front() ==';'
|
|
|
|
|| mTokenizer[i]->text.front() =='{'))
|
|
|
|
i++;
|
|
|
|
|
|
|
|
// Forward class/struct decl *or* typedef, e.g. typedef struct some_struct synonym1, synonym2;
|
|
|
|
if ((i < mTokenizer.tokenCount()) && (mTokenizer[i]->text.front() == ';')) {
|
|
|
|
// typdef struct Foo Bar
|
|
|
|
if (isTypedef) {
|
|
|
|
QString oldType = mTokenizer[mIndex]->text;
|
|
|
|
while(true) {
|
|
|
|
// Add definition statement for the synonym
|
|
|
|
if ((mIndex + 1 < mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex + 1]->text.front()==','
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front()==';')) {
|
|
|
|
QString newType = mTokenizer[mIndex]->text;
|
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
oldType,
|
|
|
|
newType,
|
2022-10-31 19:37:24 +08:00
|
|
|
"", // args
|
|
|
|
"", // noname args
|
|
|
|
"", // values
|
2021-08-19 12:01:01 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
if (mIndex >= mTokenizer.tokenCount())
|
|
|
|
break;
|
|
|
|
if (mTokenizer[mIndex]->text.front() == ';')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} 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
|
|
|
|
if (mTokenizer[mIndex]->text.front() != '{') {
|
|
|
|
while(true) {
|
|
|
|
if ((mIndex + 1 < mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex + 1]->text.front() == ','
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front() == ';'
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front() == '{'
|
|
|
|
|| mTokenizer[mIndex + 1]->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
|
|
|
|
startLine,
|
|
|
|
StatementKind::skClass,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
command = "";
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
} else if ((mIndex + 2 < mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex + 1]->text == "final")
|
|
|
|
&& (mTokenizer[mIndex + 2]->text.front()==','
|
2022-05-06 19:20:26 +08:00
|
|
|
|| mTokenizer[mIndex + 2]->text.front()==':'
|
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
|
|
|
|
startLine,
|
|
|
|
StatementKind::skClass,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
command="";
|
|
|
|
}
|
|
|
|
mIndex+=2;
|
|
|
|
} else
|
|
|
|
mIndex++;
|
|
|
|
if (mIndex >= mTokenizer.tokenCount())
|
|
|
|
break;
|
|
|
|
if (mTokenizer[mIndex]->text.front() == ':'
|
|
|
|
|| mTokenizer[mIndex]->text.front() == '{'
|
|
|
|
|| mTokenizer[mIndex]->text.front() == ';')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk to opening brace if we encountered inheritance statements
|
|
|
|
if ((mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text.front() == ':')) {
|
|
|
|
if (firstSynonym)
|
|
|
|
setInheritance(mIndex, firstSynonym, isStruct); // set the _InheritanceList value
|
|
|
|
while ((mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text.front() != '{'))
|
|
|
|
mIndex++; // skip decl after ':'
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
if ((i + 1 < mTokenizer.tokenCount()) && !(
|
|
|
|
mTokenizer[i + 1]->text.front() == ';'
|
|
|
|
|| mTokenizer[i + 1]->text.front() == '}')) {
|
|
|
|
// When encountering names again after struct body scanning, skip it
|
|
|
|
mSkipList.append(i+1); // add first name to skip statement so that we can skip it until the next ;
|
|
|
|
QString command = "";
|
|
|
|
QString args = "";
|
|
|
|
|
|
|
|
// Add synonym before opening brace
|
|
|
|
while(true) {
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (!(mTokenizer[i]->text.front() == '{'
|
|
|
|
|| mTokenizer[i]->text.front() == ','
|
|
|
|
|| mTokenizer[i]->text.front() == ';')) {
|
2021-08-26 17:48:23 +08:00
|
|
|
// if ((mTokenizer[i]->text.front() == '_')
|
|
|
|
// && (mTokenizer[i]->text.back() == '_')) {
|
|
|
|
// // skip possible gcc attributes
|
|
|
|
// // start and end with 2 underscores (i.e. __attribute__)
|
|
|
|
// // so, to avoid slow checks of strings, we just check the first and last letter of the token
|
|
|
|
// // if both are underscores, we split
|
|
|
|
// break;
|
|
|
|
// } else {
|
2021-08-19 12:01:01 +08:00
|
|
|
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-26 17:48:23 +08:00
|
|
|
// }
|
2021-08-19 12:01:01 +08:00
|
|
|
} else {
|
|
|
|
command = command.trimmed();
|
|
|
|
if (!command.isEmpty() &&
|
|
|
|
( !firstSynonym
|
|
|
|
|| command!=firstSynonym->command )) {
|
|
|
|
//not define the struct yet, we define a unamed struct
|
|
|
|
if (!firstSynonym) {
|
|
|
|
firstSynonym = addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
prefix,
|
|
|
|
"__"+command,
|
|
|
|
"",
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-19 12:01:01 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skClass,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
if (isTypedef) {
|
|
|
|
//typedef
|
|
|
|
addStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
firstSynonym->command,
|
|
|
|
command,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false); // 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,
|
|
|
|
firstSynonym->command,
|
|
|
|
command,
|
|
|
|
args,
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-19 12:01:01 +08:00
|
|
|
mTokenizer[i]->line,
|
|
|
|
StatementKind::skVariable,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false); // TODO: not supported to pass list
|
|
|
|
}
|
|
|
|
}
|
|
|
|
command = "";
|
|
|
|
}
|
|
|
|
if (i >= mTokenizer.tokenCount() - 1)
|
|
|
|
break;
|
|
|
|
if (mTokenizer[i]->text.front()=='{'
|
|
|
|
|| mTokenizer[i]->text.front()== ';')
|
|
|
|
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) {
|
|
|
|
//anonymous union/struct/class, add ast a block
|
|
|
|
firstSynonym=addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
2021-08-27 08:52:20 +08:00
|
|
|
startLine,
|
|
|
|
StatementKind::skBlock,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
}
|
2021-08-19 12:01:01 +08:00
|
|
|
addSoloScopeLevel(firstSynonym,startLine);
|
|
|
|
|
|
|
|
// Step over {
|
|
|
|
if ((mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text.front() == '{'))
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:08:01 +08:00
|
|
|
void CppParser::handleUsing()
|
|
|
|
{
|
|
|
|
int startLine = mTokenizer[mIndex]->line;
|
|
|
|
if (mCurrentFile.isEmpty()) {
|
|
|
|
//skip to ;
|
|
|
|
while ((mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text!=';'))
|
|
|
|
mIndex++;
|
|
|
|
mIndex++; //skip ;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIndex++; //skip 'using'
|
|
|
|
|
2021-09-28 10:40:19 +08:00
|
|
|
//handle things like 'using vec = std::vector; '
|
|
|
|
if (mIndex+1 < mTokenizer.tokenCount()
|
|
|
|
&& mTokenizer[mIndex+1]->text == "=") {
|
|
|
|
QString fullName = mTokenizer[mIndex]->text;
|
|
|
|
QString aliasName;
|
|
|
|
mIndex+=2;
|
|
|
|
while (mIndex<mTokenizer.tokenCount() &&
|
|
|
|
mTokenizer[mIndex]->text!=';') {
|
|
|
|
aliasName += mTokenizer[mIndex]->text;
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
aliasName, // name of the alias (type)
|
|
|
|
fullName, // command
|
|
|
|
"", // args
|
2022-10-31 19:37:24 +08:00
|
|
|
"",
|
|
|
|
"",
|
2021-09-28 10:40:19 +08:00
|
|
|
"", // values
|
|
|
|
startLine,
|
|
|
|
StatementKind::skTypedef,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
// skip ;
|
|
|
|
mIndex++;
|
|
|
|
return;
|
|
|
|
}
|
2021-08-19 17:08:01 +08:00
|
|
|
//handle things like 'using std::vector;'
|
|
|
|
if ((mIndex+2>=mTokenizer.tokenCount())
|
|
|
|
|| (mTokenizer[mIndex]->text != "namespace")) {
|
|
|
|
int i= mTokenizer[mIndex]->text.lastIndexOf("::");
|
|
|
|
if (i>=0) {
|
|
|
|
QString fullName = mTokenizer[mIndex]->text;
|
|
|
|
QString usingName = fullName.mid(i+2);
|
|
|
|
addStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
fullName, // name of the alias (type)
|
|
|
|
usingName, // command
|
|
|
|
"", // args
|
|
|
|
"", // values
|
|
|
|
startLine,
|
|
|
|
StatementKind::skAlias,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
//skip to ;
|
|
|
|
while ((mIndex<mTokenizer.tokenCount()) &&
|
|
|
|
(mTokenizer[mIndex]->text!=";"))
|
|
|
|
mIndex++;
|
|
|
|
mIndex++; //and skip it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIndex++; // skip 'namespace'
|
|
|
|
PStatement scopeStatement = getCurrentScope();
|
|
|
|
|
|
|
|
QString usingName = mTokenizer[mIndex]->text;
|
|
|
|
mIndex++;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 00:01:46 +08:00
|
|
|
void CppParser::handleVar(bool isFunctionPointer)
|
2021-08-19 17:08:01 +08:00
|
|
|
{
|
|
|
|
// Keep going and stop on top of the variable name
|
|
|
|
QString lastType = "";
|
|
|
|
bool isExtern = false;
|
|
|
|
bool isStatic = false;
|
|
|
|
bool varAdded = false;
|
|
|
|
|
2022-11-01 00:01:46 +08:00
|
|
|
if (!isFunctionPointer) {
|
|
|
|
while (true) {
|
|
|
|
if ((mIndex + 1 < mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex + 1]->text=='('
|
|
|
|
|| mTokenizer[mIndex + 1]->text==','
|
|
|
|
|| mTokenizer[mIndex + 1]->text==';'
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front()==':'
|
|
|
|
|| mTokenizer[mIndex + 1]->text=='}'
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front()=='#'
|
|
|
|
|| mTokenizer[mIndex + 1]->text=='{')) {
|
|
|
|
break;
|
|
|
|
}
|
2021-08-19 17:08:01 +08:00
|
|
|
|
2022-11-01 00:01:46 +08:00
|
|
|
// struct/class/union is part of the type signature
|
|
|
|
// but we dont store it in the type cache, so must trim it to find the type info
|
|
|
|
if (mTokenizer[mIndex]->text!="struct"
|
|
|
|
&& mTokenizer[mIndex]->text!="class"
|
|
|
|
&& mTokenizer[mIndex]->text!="union") {
|
|
|
|
if (mTokenizer[mIndex]->text == ':') {
|
|
|
|
lastType += ':';
|
2021-08-19 17:08:01 +08:00
|
|
|
} else {
|
2022-11-01 00:01:46 +08:00
|
|
|
QString s=mTokenizer[mIndex]->text;
|
|
|
|
if (s == "extern") {
|
|
|
|
isExtern = true;
|
|
|
|
} else {
|
|
|
|
if (!s.isEmpty())
|
|
|
|
lastType += ' '+s;
|
|
|
|
if (s == "static")
|
|
|
|
isStatic = true;
|
|
|
|
}
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-01 00:01:46 +08:00
|
|
|
mIndex++;
|
|
|
|
if(mIndex >= mTokenizer.tokenCount())
|
|
|
|
break;
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
2022-11-01 00:01:46 +08:00
|
|
|
lastType = lastType.trimmed();
|
|
|
|
} else {
|
|
|
|
QString s=mTokenizer[mIndex]->text;
|
|
|
|
if (s == "extern") {
|
|
|
|
isExtern = true;
|
|
|
|
} else if (s=="static") {
|
|
|
|
isStatic = true;
|
|
|
|
} else
|
|
|
|
lastType=s;
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother entering the scanning loop when we have failed
|
|
|
|
if (mIndex >= mTokenizer.tokenCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find the variable name
|
|
|
|
while (true) {
|
|
|
|
// Skip bit identifiers,
|
|
|
|
// e.g.:
|
|
|
|
// handle
|
|
|
|
// unsigned short bAppReturnCode:8,reserved:6,fBusy:1,fAck:1
|
|
|
|
// as
|
|
|
|
// unsigned short bAppReturnCode,reserved,fBusy,fAck
|
|
|
|
if ( (mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text.front() == ':')) {
|
|
|
|
while ( (mIndex < mTokenizer.tokenCount())
|
|
|
|
&& !(
|
2022-11-01 00:01:46 +08:00
|
|
|
mTokenizer[mIndex]->text.startsWith(',')
|
|
|
|
|| mTokenizer[mIndex]->text.startsWith(';')
|
2021-08-19 17:08:01 +08:00
|
|
|
))
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
// Skip inline constructors,
|
|
|
|
// e.g.:
|
|
|
|
// handle
|
|
|
|
// int a(3)
|
|
|
|
// as
|
|
|
|
// int a
|
|
|
|
if (!isFunctionPointer &&
|
|
|
|
mIndex < mTokenizer.tokenCount() &&
|
2022-11-01 00:01:46 +08:00
|
|
|
mTokenizer[mIndex]->text == '(') {
|
|
|
|
mIndex=mTokenizer[mIndex]->matchIndex+1;
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Did we stop on top of the variable name?
|
|
|
|
if (mIndex < mTokenizer.tokenCount()) {
|
|
|
|
if (mTokenizer[mIndex]->text.front()!=','
|
|
|
|
&& mTokenizer[mIndex]->text.front()!=';') {
|
|
|
|
QString cmd;
|
|
|
|
QString args;
|
2022-11-01 00:01:46 +08:00
|
|
|
if (isFunctionPointer) {
|
|
|
|
QString s = mTokenizer[mIndex+1]->text;
|
|
|
|
cmd = s.mid(1); // (*foo) -> foo
|
|
|
|
//TODO: parse args
|
2021-08-19 17:08:01 +08:00
|
|
|
args = mTokenizer[mIndex + 1]->text; // (int a,int b)
|
|
|
|
lastType += "(*)" + args; // void(int a,int b)
|
|
|
|
mIndex++;
|
|
|
|
} else if (mTokenizer[mIndex]->text.back() == ']') { //array; break args
|
|
|
|
int pos = mTokenizer[mIndex]->text.indexOf('[');
|
|
|
|
cmd = mTokenizer[mIndex]->text.mid(0,pos);
|
|
|
|
args = mTokenizer[mIndex]->text.mid(pos);
|
|
|
|
} else {
|
|
|
|
cmd = mTokenizer[mIndex]->text;
|
|
|
|
args = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a statement for every struct we are in
|
2021-08-28 01:20:32 +08:00
|
|
|
if (!lastType.isEmpty()) {
|
|
|
|
addChildStatement(
|
2022-10-31 19:37:24 +08:00
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
lastType,
|
|
|
|
cmd,
|
|
|
|
args,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skVariable,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
//True,
|
|
|
|
!isExtern,
|
|
|
|
isStatic); // TODO: not supported to pass list
|
2021-08-28 01:20:32 +08:00
|
|
|
varAdded = true;
|
|
|
|
}
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step over the variable name
|
|
|
|
if (isblockChar(mTokenizer[mIndex]->text.front())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
if (mIndex >= mTokenizer.tokenCount())
|
|
|
|
break;
|
|
|
|
if (isblockChar(mTokenizer[mIndex]->text.front()))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (varAdded && (mIndex < mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex]->text == '{')) {
|
|
|
|
// skip { } like A x {new A};
|
2022-11-01 09:02:17 +08:00
|
|
|
int i=indexOfMatchingBrace(mIndex);
|
2021-08-19 17:08:01 +08:00
|
|
|
if (i!=mIndex)
|
|
|
|
mIndex = i+1;
|
|
|
|
}
|
|
|
|
// Skip ; and ,
|
|
|
|
if ( (mIndex < mTokenizer.tokenCount()) &&
|
|
|
|
(mTokenizer[mIndex]->text.front() == ';'
|
|
|
|
|| mTokenizer[mIndex]->text.front() == ','))
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-08-23 17:27:17 +08:00
|
|
|
QStringList buffer;
|
2021-08-19 17:08:01 +08:00
|
|
|
if (mOnGetFileStream) {
|
2021-08-23 17:27:17 +08:00
|
|
|
mOnGetFileStream(fileName,buffer);
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Preprocess the file...
|
|
|
|
{
|
|
|
|
auto action = finally([this]{
|
2022-10-22 10:59:39 +08:00
|
|
|
mTokenizer.clear();
|
2021-08-19 17:08:01 +08:00
|
|
|
});
|
|
|
|
// Let the preprocessor augment the include records
|
2022-10-22 10:44:10 +08:00
|
|
|
// mPreprocessor.setIncludesList(mIncludesList);
|
|
|
|
// mPreprocessor.setScannedFileList(mScannedFiles);
|
|
|
|
// mPreprocessor.setIncludePaths(mIncludePaths);
|
|
|
|
// mPreprocessor.setProjectIncludePaths(mProjectIncludePaths);
|
|
|
|
mPreprocessor.setScanOptions(mParseGlobalHeaders, mParseLocalHeaders);
|
|
|
|
mPreprocessor.preprocess(fileName, buffer);
|
|
|
|
|
|
|
|
QStringList preprocessResult = mPreprocessor.result();
|
2021-11-12 02:20:13 +08:00
|
|
|
#ifdef QT_DEBUG
|
2022-10-22 19:33:20 +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
|
2022-10-22 19:33:20 +08:00
|
|
|
//reduce memory usage
|
|
|
|
mPreprocessor.clearTempResults();
|
2021-08-27 08:52:20 +08:00
|
|
|
|
2021-08-19 17:08:01 +08:00
|
|
|
// Tokenize the preprocessed buffer file
|
2021-11-12 07:26:13 +08:00
|
|
|
mTokenizer.tokenize(preprocessResult);
|
|
|
|
//reduce memory usage
|
|
|
|
preprocessResult.clear();
|
2021-08-19 17:08:01 +08:00
|
|
|
if (mTokenizer.tokenCount() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Process the token list
|
|
|
|
while(true) {
|
|
|
|
if (!handleStatement())
|
|
|
|
break;
|
|
|
|
}
|
2022-10-31 19:37:24 +08:00
|
|
|
mTokenizer.dumpTokens(QString("r:\\tokens-%1.txt").arg(extractFileName(fileName)));
|
2021-08-27 23:51:42 +08:00
|
|
|
#ifdef QT_DEBUG
|
2022-10-28 09:47:34 +08:00
|
|
|
// mTokenizer.dumpTokens(QString("r:\\tokens-%1.txt").arg(extractFileName(fileName)));
|
2022-04-18 16:56:31 +08:00
|
|
|
//
|
|
|
|
// mStatementList.dumpAll("r:\\all-stats.txt");
|
2021-08-27 23:51:42 +08:00
|
|
|
#endif
|
2022-10-28 09:47:34 +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,
|
|
|
|
const PStatement& base, StatementClassScope access)
|
2021-08-19 17:08:01 +08:00
|
|
|
{
|
|
|
|
//differentiate class and struct
|
2022-11-01 09:02:17 +08:00
|
|
|
if (access == StatementClassScope::None) {
|
2021-08-19 17:08:01 +08:00
|
|
|
if (isStruct)
|
2022-11-01 09:02:17 +08:00
|
|
|
access = StatementClassScope::Public;
|
2021-08-19 17:08:01 +08:00
|
|
|
else
|
2022-11-01 09:02:17 +08:00
|
|
|
access = StatementClassScope::Private;
|
2021-08-19 17:08:01 +08:00
|
|
|
}
|
2021-08-29 10:14:07 +08:00
|
|
|
foreach (const PStatement& statement, base->children) {
|
2022-11-01 09:02:17 +08:00
|
|
|
if (statement->classScope == StatementClassScope::Private
|
2021-08-19 17:08:01 +08:00
|
|
|
|| statement->kind == StatementKind::skConstructor
|
|
|
|
|| statement->kind == StatementKind::skDestructor)
|
|
|
|
continue;
|
|
|
|
StatementClassScope m_acc;
|
|
|
|
switch(access) {
|
2022-11-01 09:02:17 +08:00
|
|
|
case StatementClassScope::Public:
|
2021-08-19 17:08:01 +08:00
|
|
|
m_acc = statement->classScope;
|
|
|
|
break;
|
2022-11-01 09:02:17 +08:00
|
|
|
case StatementClassScope::Protected:
|
|
|
|
m_acc = StatementClassScope::Protected;
|
2021-08-19 17:08:01 +08:00
|
|
|
break;
|
2022-11-01 09:02:17 +08:00
|
|
|
case StatementClassScope::Private:
|
|
|
|
m_acc = StatementClassScope::Private;
|
2021-08-19 17:08:01 +08:00
|
|
|
break;
|
|
|
|
default:
|
2022-11-01 09:02:17 +08:00
|
|
|
m_acc = StatementClassScope::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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 18:02:42 +08:00
|
|
|
QList<PStatement> CppParser::getListOfFunctions(const QString &fileName, int line, const PStatement &statement, const PStatement &scopeStatement)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
PStatement CppParser::findMemberOfStatement(const QString &phrase,
|
|
|
|
const PStatement& scopeStatement)
|
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());
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:24:39 +08:00
|
|
|
QList<PStatement> CppParser::findMembersOfStatement(const QString &phrase, const PStatement &scopeStatement)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
StatementKind kind, const PStatement& scope)
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
|
|
|
if (scope && scope->kind == StatementKind::skNamespace) {
|
|
|
|
PStatementList namespaceStatementsList = findNamespace(scope->command);
|
|
|
|
if (!namespaceStatementsList)
|
|
|
|
return PStatement();
|
2021-08-29 10:14:07 +08:00
|
|
|
foreach (const PStatement& namespaceStatement, *namespaceStatementsList) {
|
2021-08-16 00:47:35 +08:00
|
|
|
PStatement result=doFindStatementInScope(name,noNameArgs,kind,namespaceStatement);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return doFindStatementInScope(name,noNameArgs,kind,scope);
|
|
|
|
}
|
|
|
|
return PStatement();
|
|
|
|
}
|
|
|
|
|
2022-03-23 14:13:10 +08:00
|
|
|
PStatement CppParser::findStatementInScope(const QString &name, const PStatement &scope)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
PStatement CppParser::findStatementInNamespace(const QString &name, const QString &namespaceName)
|
|
|
|
{
|
|
|
|
PStatementList namespaceStatementsList=findNamespace(namespaceName);
|
|
|
|
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,
|
|
|
|
const QStringList& phraseExpression,
|
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
|
|
|
const PEvalStatement& previousResult,
|
|
|
|
bool freeScoped)
|
|
|
|
{
|
|
|
|
//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);
|
|
|
|
}
|
|
|
|
|
|
|
|
PEvalStatement CppParser::doEvalPointerArithmetic(const QString &fileName, const QStringList &phraseExpression, int &pos, const PStatement &scope, const PEvalStatement &previousResult, bool freeScoped)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
const QStringList &phraseExpression,
|
|
|
|
int &pos,
|
|
|
|
const PStatement &scope,
|
|
|
|
const PEvalStatement &previousResult,
|
|
|
|
bool freeScoped)
|
|
|
|
{
|
|
|
|
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,
|
2021-12-06 09:02:39 +08:00
|
|
|
const QStringList &phraseExpression,
|
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
const PEvalStatement& previousResult,
|
2021-12-06 09:02:39 +08:00
|
|
|
bool freeScoped)
|
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;
|
2021-12-08 21:44:40 +08:00
|
|
|
if (result->pointerLevel==0) {
|
|
|
|
PStatement typeStatement = result->effectiveTypeStatement;
|
|
|
|
if ((typeStatement)
|
|
|
|
&& STLPointers.contains(typeStatement->fullName)
|
|
|
|
&& result->kind == EvalStatementKind::Variable
|
|
|
|
&& result->baseStatement) {
|
|
|
|
PStatement parentScope = result->baseStatement->parentScope.lock();
|
|
|
|
QString typeName=findFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope);
|
|
|
|
// qDebug()<<"typeName"<<typeName;
|
|
|
|
typeStatement=findTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
|
|
|
result = doCreateEvalType(fileName,typeStatement);
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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,
|
2021-12-06 09:02:39 +08:00
|
|
|
const QStringList &phraseExpression,
|
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
const PEvalStatement& previousResult,
|
2021-12-06 09:02:39 +08:00
|
|
|
bool freeScoped)
|
|
|
|
|
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);
|
|
|
|
if (result->baseStatement && lastResult && lastResult->baseStatement) {
|
|
|
|
PStatement parentScope = result->baseStatement->parentScope.lock();
|
|
|
|
if (parentScope
|
|
|
|
&& STLContainers.contains(parentScope->fullName)
|
|
|
|
&& STLElementMethods.contains(result->baseStatement->command)
|
|
|
|
) {
|
|
|
|
//stl container methods
|
|
|
|
PStatement typeStatement = result->effectiveTypeStatement;
|
|
|
|
QString typeName=findFirstTemplateParamOf(fileName,lastResult->baseStatement->type, parentScope);
|
|
|
|
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
|
|
|
|
typeStatement=findTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
|
|
|
result = doCreateEvalType(fileName,typeStatement);
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
&& STLContainers.contains(typeStatement->fullName)
|
|
|
|
&& result->kind == EvalStatementKind::Variable
|
|
|
|
&& result->baseStatement) {
|
|
|
|
PStatement parentScope = result->baseStatement->parentScope.lock();
|
|
|
|
QString typeName = findFirstTemplateParamOf(fileName,result->baseStatement->type,
|
|
|
|
parentScope);
|
|
|
|
typeStatement = findTypeDefinitionOf(fileName, typeName,
|
|
|
|
parentScope);
|
|
|
|
if (typeStatement) {
|
|
|
|
result = doCreateEvalType(fileName,typeStatement);
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-08 21:44:40 +08:00
|
|
|
// qDebug()<<(result!=nullptr)<<pos<<"after .";
|
2021-12-05 10:52:17 +08:00
|
|
|
} else if (phraseExpression[pos] == "->") {
|
|
|
|
pos++;
|
2021-12-08 21:44:40 +08:00
|
|
|
// qDebug()<<"pointer level"<<result->pointerLevel;
|
|
|
|
if (result->pointerLevel==0) {
|
|
|
|
PStatement typeStatement = result->effectiveTypeStatement;
|
|
|
|
if ((typeStatement)
|
|
|
|
&& STLPointers.contains(typeStatement->fullName)
|
|
|
|
&& result->kind == EvalStatementKind::Variable
|
|
|
|
&& result->baseStatement) {
|
|
|
|
PStatement parentScope = result->baseStatement->parentScope.lock();
|
|
|
|
QString typeName=findFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope);
|
|
|
|
// qDebug()<<"typeName"<<typeName;
|
|
|
|
typeStatement=findTypeDefinitionOf(fileName, typeName,parentScope);
|
|
|
|
if (typeStatement) {
|
|
|
|
result = doCreateEvalType(fileName,typeStatement);
|
|
|
|
result->kind = EvalStatementKind::Variable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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,
|
2021-12-06 09:02:39 +08:00
|
|
|
const QStringList &phraseExpression,
|
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
const PEvalStatement& previousResult,
|
2021-12-06 09:02:39 +08:00
|
|
|
bool freeScoped)
|
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,
|
2021-12-06 09:02:39 +08:00
|
|
|
const QStringList &phraseExpression,
|
|
|
|
int &pos,
|
|
|
|
const PStatement& scope,
|
2021-12-06 11:37:37 +08:00
|
|
|
const PEvalStatement& previousResult,
|
2021-12-06 09:02:39 +08:00
|
|
|
bool freeScoped)
|
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;
|
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 {
|
2021-12-08 21:44:40 +08:00
|
|
|
// if (previousResult->effectiveTypeStatement) {
|
|
|
|
// qDebug()<<phraseExpression[pos]<<previousResult->effectiveTypeStatement->fullName;
|
|
|
|
// } else {
|
|
|
|
// qDebug()<<phraseExpression[pos]<<"no type";
|
|
|
|
// }
|
2021-12-08 19:13:47 +08:00
|
|
|
statement = findStatementInScope(phraseExpression[pos],previousResult->effectiveTypeStatement);
|
|
|
|
// if (!statement) {
|
|
|
|
// qDebug()<<"not found!";
|
|
|
|
// } else {
|
|
|
|
// qDebug()<<statement->fullName;
|
|
|
|
// qDebug()<<statement->kind;
|
|
|
|
// }
|
|
|
|
}
|
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: {
|
2022-03-16 16:36:25 +08:00
|
|
|
statement = findAliasedStatement(statement);
|
2021-12-08 21:44:40 +08:00
|
|
|
if (statement)
|
|
|
|
result = doCreateEvalNamespace(statement);
|
|
|
|
}
|
|
|
|
break;
|
2021-12-08 19:13:47 +08:00
|
|
|
case StatementKind::skVariable:
|
|
|
|
case StatementKind::skParameter:
|
|
|
|
result = doCreateEvalVariable(fileName,statement);
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
result = PEvalStatement();
|
|
|
|
}
|
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 !!!!";
|
|
|
|
// }
|
|
|
|
return result;
|
2021-12-04 18:38:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-07 14:48:20 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalNamespace(const PStatement &namespaceStatement)
|
|
|
|
{
|
|
|
|
if (!namespaceStatement)
|
|
|
|
return PEvalStatement();
|
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
namespaceStatement->fullName,
|
|
|
|
EvalStatementKind::Namespace,
|
2021-12-08 19:13:47 +08:00
|
|
|
PStatement(),
|
|
|
|
namespaceStatement);
|
2021-12-07 14:48:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PEvalStatement CppParser::doCreateEvalType(const QString& fileName,const PStatement &typeStatement)
|
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;
|
|
|
|
PStatement statement = doParseEvalTypeInfo(
|
|
|
|
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,
|
|
|
|
pointerLevel);
|
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
baseType,
|
|
|
|
EvalStatementKind::Type,
|
|
|
|
PStatement(),
|
2021-12-08 19:13:47 +08:00
|
|
|
typeStatement,
|
2021-12-07 14:48:20 +08:00
|
|
|
pointerLevel
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return std::make_shared<EvalStatement>(
|
2021-12-07 08:23:27 +08:00
|
|
|
typeStatement->fullName,
|
|
|
|
EvalStatementKind::Type,
|
2021-12-08 19:13:47 +08:00
|
|
|
PStatement(),
|
|
|
|
typeStatement);
|
2021-12-07 14:48:20 +08:00
|
|
|
}
|
2021-12-07 08:23:27 +08:00
|
|
|
}
|
|
|
|
|
2021-12-08 19:13:47 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalType(const QString &primitiveType)
|
|
|
|
{
|
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
primitiveType,
|
|
|
|
EvalStatementKind::Type,
|
|
|
|
PStatement(),
|
|
|
|
PStatement());
|
|
|
|
}
|
|
|
|
|
2021-12-07 14:48:20 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalVariable(const QString &fileName, PStatement varStatement)
|
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;
|
|
|
|
PStatement typeStatement = doParseEvalTypeInfo(
|
|
|
|
fileName,
|
|
|
|
varStatement->parentScope.lock(),
|
2021-12-08 21:44:40 +08:00
|
|
|
varStatement->type+ varStatement->args,
|
2021-12-07 08:23:27 +08:00
|
|
|
baseType,
|
|
|
|
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,
|
|
|
|
pointerLevel
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-07 14:48:20 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalFunction(const QString &fileName, PStatement funcStatement)
|
|
|
|
{
|
|
|
|
if (!funcStatement)
|
|
|
|
return PEvalStatement();
|
|
|
|
QString baseType;
|
|
|
|
int pointerLevel=0;
|
|
|
|
PStatement typeStatement = doParseEvalTypeInfo(
|
|
|
|
fileName,
|
|
|
|
funcStatement->parentScope.lock(),
|
|
|
|
funcStatement->type,
|
|
|
|
baseType,
|
|
|
|
pointerLevel);
|
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
baseType,
|
|
|
|
EvalStatementKind::Function,
|
|
|
|
funcStatement,
|
|
|
|
typeStatement,
|
|
|
|
pointerLevel
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:13:47 +08:00
|
|
|
PEvalStatement CppParser::doCreateEvalLiteral(const QString &type)
|
|
|
|
{
|
|
|
|
return std::make_shared<EvalStatement>(
|
|
|
|
type,
|
|
|
|
EvalStatementKind::Literal,
|
|
|
|
PStatement(),
|
|
|
|
PStatement());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::doSkipInExpression(const QStringList &expression, int &pos, const QString &startSymbol, const QString &endSymbol)
|
|
|
|
{
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 08:23:27 +08:00
|
|
|
PStatement CppParser::doParseEvalTypeInfo(
|
|
|
|
const QString &fileName,
|
|
|
|
const PStatement &scope,
|
|
|
|
const QString &type,
|
|
|
|
QString &baseType,
|
|
|
|
int &pointerLevel)
|
|
|
|
{
|
|
|
|
// 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-09-27 14:01:38 +08:00
|
|
|
QSynedit::CppHighlighter highlighter;
|
2021-12-07 08:23:27 +08:00
|
|
|
highlighter.resetState();
|
|
|
|
highlighter.setLine(type,0);
|
|
|
|
int bracketLevel = 0;
|
|
|
|
int templateLevel = 0;
|
|
|
|
while(!highlighter.eol()) {
|
|
|
|
QString token = highlighter.getToken();
|
|
|
|
if (bracketLevel == 0 && templateLevel ==0) {
|
|
|
|
if (token == "*")
|
|
|
|
pointerLevel++;
|
|
|
|
else if (token == "&")
|
|
|
|
pointerLevel--;
|
|
|
|
else if (highlighter.getTokenAttribute() == highlighter.identifierAttribute()) {
|
|
|
|
if (token!= "const")
|
2021-12-08 19:55:15 +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++;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
highlighter.next();
|
|
|
|
}
|
|
|
|
while ((position >= 0) && (s[position] == '*'
|
|
|
|
|| s[position] == ' '
|
|
|
|
|| s[position] == '&')) {
|
|
|
|
if (s[position]=='*') {
|
|
|
|
|
|
|
|
}
|
|
|
|
position--;
|
|
|
|
}
|
2021-12-08 19:13:47 +08:00
|
|
|
PStatement statement = findStatementOf(fileName,baseType,scope);
|
|
|
|
return getTypeDef(statement,fileName,baseType);
|
2021-12-07 08:23:27 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:58 +08:00
|
|
|
int CppParser::getBracketEnd(const QString &s, int startAt)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
const PStatement& scope)
|
2021-08-16 00:47:35 +08:00
|
|
|
{
|
|
|
|
const StatementMap& statementMap =mStatementList.childrenStatements(scope);
|
|
|
|
|
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 {
|
|
|
|
statement->hasDefinition=false;
|
|
|
|
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-10-22 19:33:20 +08:00
|
|
|
PFileIncludes fileIncludes = mPreprocessor.includesList()[file];
|
2022-10-23 23:06:55 +08:00
|
|
|
if (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
|
|
|
}
|
|
|
|
|
2021-08-16 00:47:35 +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;
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:37:24 +08:00
|
|
|
void CppParser::scanMethodArgs(const PStatement& functionStatement, int argStart, int argEnd)
|
2021-08-19 23:49:23 +08:00
|
|
|
{
|
|
|
|
// Split up argument string by ,
|
2022-10-31 19:37:24 +08:00
|
|
|
int i = argStart+1; // assume it starts with ( and ends with )
|
2021-08-19 23:49:23 +08:00
|
|
|
int paramStart = i;
|
2022-10-31 19:37:24 +08:00
|
|
|
// Keep going and stop on top of the variable name
|
|
|
|
QString lastType = "";
|
|
|
|
bool isFunctionPointer = false;
|
|
|
|
bool varAdded = false;
|
|
|
|
while (i < argEnd) {
|
2021-08-19 23:49:23 +08:00
|
|
|
if ((argStr[i] == ',') ||
|
|
|
|
((i == argStr.length()-1) && (argStr[i] == ')'))) {
|
|
|
|
// We've found "int* a" for example
|
|
|
|
QString s = argStr.mid(paramStart,i-paramStart);
|
|
|
|
|
|
|
|
//remove default value
|
|
|
|
int assignPos = s.indexOf('=');
|
|
|
|
if (assignPos >= 0) {
|
|
|
|
s.truncate(assignPos);
|
|
|
|
s = s.trimmed();
|
|
|
|
}
|
2022-10-31 19:37:24 +08:00
|
|
|
//todo: we don't support function pointer parameters now, till we can tokenize function parameters
|
|
|
|
|
2022-01-12 20:59:28 +08:00
|
|
|
//skip []
|
|
|
|
int varEndPos = s.length()-1;
|
|
|
|
int bracketLevel = 0;
|
|
|
|
while (varEndPos>=0) {
|
|
|
|
switch(s[varEndPos].unicode()) {
|
|
|
|
case ']':
|
|
|
|
bracketLevel++;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
bracketLevel--;
|
|
|
|
varEndPos--;
|
|
|
|
break;
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
2022-01-12 20:59:28 +08:00
|
|
|
if (bracketLevel==0)
|
|
|
|
break;
|
|
|
|
varEndPos--;
|
|
|
|
}
|
|
|
|
int varStartPos = varEndPos;
|
|
|
|
if (varEndPos>=0) {
|
|
|
|
while (varStartPos-1>=0) {
|
|
|
|
if (!mTokenizer.isIdentChar(s[varStartPos-1]))
|
|
|
|
break;
|
|
|
|
varStartPos--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (varStartPos>=0) {
|
|
|
|
if (varEndPos+1<s.length())
|
|
|
|
args=s.mid(varEndPos+1);
|
2021-08-19 23:49:23 +08:00
|
|
|
addStatement(
|
|
|
|
functionStatement,
|
|
|
|
mCurrentFile,
|
2022-01-12 20:59:28 +08:00
|
|
|
s.mid(0,varStartPos), // 'int*'
|
|
|
|
s.mid(varStartPos,varEndPos-varStartPos+1), // a
|
2021-08-19 23:49:23 +08:00
|
|
|
args,
|
|
|
|
"",
|
|
|
|
functionStatement->definitionLine,
|
|
|
|
StatementKind::skParameter,
|
2022-11-01 09:02:17 +08:00
|
|
|
StatementScope::Local,
|
|
|
|
StatementClassScope::None,
|
2021-08-19 23:49:23 +08:00
|
|
|
true,
|
|
|
|
false);
|
|
|
|
}
|
2022-01-12 20:59:28 +08:00
|
|
|
if (varStartPos<s.length()) {
|
|
|
|
args = "";
|
|
|
|
int bracketPos = s.indexOf('[');
|
|
|
|
if (bracketPos >= 0) {
|
|
|
|
args = s.mid(bracketPos);
|
|
|
|
s.truncate(bracketPos);
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 23:49:23 +08:00
|
|
|
paramStart = i + 1; // step over ,
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2022-10-31 19:37:24 +08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if ((mIndex + 2 < mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex + 1]->text.front() == '(')
|
|
|
|
&& (mTokenizer[mIndex + 2]->text.front() == '(')) {
|
|
|
|
isFunctionPointer = mTokenizer[mIndex + 1]->text.indexOf('*') >= 0;
|
|
|
|
if (!isFunctionPointer)
|
|
|
|
break; // inline constructor
|
|
|
|
} else if ((mIndex + 1 < mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex + 1]->text.front()=='('
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front()==','
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front()==';'
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front()==':'
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front()=='}'
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front()=='#'
|
|
|
|
|| mTokenizer[mIndex + 1]->text.front()=='{')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// we've made a mistake, this is a typedef , not a variable definition.
|
|
|
|
if (mTokenizer[mIndex]->text == "typedef")
|
|
|
|
return;
|
|
|
|
|
|
|
|
// struct/class/union is part of the type signature
|
|
|
|
// but we dont store it in the type cache, so must trim it to find the type info
|
|
|
|
if (mTokenizer[mIndex]->text!="struct"
|
|
|
|
&& mTokenizer[mIndex]->text!="class"
|
|
|
|
&& mTokenizer[mIndex]->text!="union") {
|
|
|
|
if (mTokenizer[mIndex]->text == ':') {
|
|
|
|
lastType += ':';
|
|
|
|
} else {
|
2022-11-01 00:01:46 +08:00
|
|
|
QString s=mTokenizer[mIndex]->text;
|
2022-10-31 19:37:24 +08:00
|
|
|
if (s == "extern") {
|
|
|
|
isExtern = true;
|
|
|
|
} else {
|
|
|
|
if (!s.isEmpty())
|
|
|
|
lastType += ' '+s;
|
|
|
|
if (s == "static")
|
|
|
|
isStatic = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
if(mIndex >= mTokenizer.tokenCount())
|
|
|
|
break;
|
|
|
|
if (isFunctionPointer)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastType = lastType.trimmed();
|
|
|
|
|
|
|
|
// Don't bother entering the scanning loop when we have failed
|
|
|
|
if (mIndex >= mTokenizer.tokenCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find the variable name
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
// Skip bit identifiers,
|
|
|
|
// e.g.:
|
|
|
|
// handle
|
|
|
|
// unsigned short bAppReturnCode:8,reserved:6,fBusy:1,fAck:1
|
|
|
|
// as
|
|
|
|
// unsigned short bAppReturnCode,reserved,fBusy,fAck
|
|
|
|
if ( (mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text.front() == ':')) {
|
|
|
|
while ( (mIndex < mTokenizer.tokenCount())
|
|
|
|
&& !(
|
|
|
|
mTokenizer[mIndex]->text.front() == ','
|
|
|
|
|| isblockChar(';')
|
|
|
|
))
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip inline constructors,
|
|
|
|
// e.g.:
|
|
|
|
// handle
|
|
|
|
// int a(3)
|
|
|
|
// as
|
|
|
|
// int a
|
|
|
|
if (!isFunctionPointer &&
|
|
|
|
mIndex < mTokenizer.tokenCount() &&
|
|
|
|
mTokenizer[mIndex]->text.front() == '(') {
|
|
|
|
while ((mIndex < mTokenizer.tokenCount())
|
|
|
|
&& !(
|
|
|
|
mTokenizer[mIndex]->text.front() == ','
|
|
|
|
|| isblockChar(mTokenizer[mIndex]->text.front())
|
|
|
|
))
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did we stop on top of the variable name?
|
|
|
|
if (mIndex < mTokenizer.tokenCount()) {
|
|
|
|
if (mTokenizer[mIndex]->text.front()!=','
|
|
|
|
&& mTokenizer[mIndex]->text.front()!=';') {
|
|
|
|
QString cmd;
|
|
|
|
QString args;
|
|
|
|
if (isFunctionPointer && (mIndex + 1 < mTokenizer.tokenCount())) {
|
|
|
|
QString s = mTokenizer[mIndex]->text;
|
|
|
|
cmd = s.mid(2,s.length()-3).trimmed(); // (*foo) -> foo
|
|
|
|
args = mTokenizer[mIndex + 1]->text; // (int a,int b)
|
|
|
|
lastType += "(*)" + args; // void(int a,int b)
|
|
|
|
mIndex++;
|
|
|
|
} else if (mTokenizer[mIndex]->text.back() == ']') { //array; break args
|
|
|
|
int pos = mTokenizer[mIndex]->text.indexOf('[');
|
|
|
|
cmd = mTokenizer[mIndex]->text.mid(0,pos);
|
|
|
|
args = mTokenizer[mIndex]->text.mid(pos);
|
|
|
|
} else {
|
|
|
|
cmd = mTokenizer[mIndex]->text;
|
|
|
|
args = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a statement for every struct we are in
|
|
|
|
if (!lastType.isEmpty()) {
|
|
|
|
addChildStatement(
|
|
|
|
getCurrentScope(),
|
|
|
|
mCurrentFile,
|
|
|
|
lastType,
|
|
|
|
cmd,
|
|
|
|
args,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
mTokenizer[mIndex]->line,
|
|
|
|
StatementKind::skVariable,
|
|
|
|
getScope(),
|
|
|
|
mClassScope,
|
|
|
|
//True,
|
|
|
|
!isExtern,
|
|
|
|
isStatic); // TODO: not supported to pass list
|
|
|
|
varAdded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step over the variable name
|
|
|
|
if (isblockChar(mTokenizer[mIndex]->text.front())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
if (mIndex >= mTokenizer.tokenCount())
|
|
|
|
break;
|
|
|
|
if (isblockChar(mTokenizer[mIndex]->text.front()))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (varAdded && (mIndex < mTokenizer.tokenCount())
|
|
|
|
&& (mTokenizer[mIndex]->text == '{')) {
|
|
|
|
// skip { } like A x {new A};
|
2022-11-01 09:02:17 +08:00
|
|
|
int i=indexOfMatchingBrace(mIndex);
|
2022-10-31 19:37:24 +08:00
|
|
|
if (i!=mIndex)
|
|
|
|
mIndex = i+1;
|
|
|
|
}
|
|
|
|
// Skip ; and ,
|
|
|
|
if ( (mIndex < mTokenizer.tokenCount()) &&
|
|
|
|
(mTokenizer[mIndex]->text.front() == ';'
|
|
|
|
|| mTokenizer[mIndex]->text.front() == ','))
|
|
|
|
mIndex++;
|
|
|
|
|
2021-08-19 23:49:23 +08:00
|
|
|
}
|
|
|
|
|
2022-04-17 19:49:04 +08:00
|
|
|
QString CppParser::splitPhrase(const QString &phrase, QString &sClazz,
|
|
|
|
QString &sOperator, QString &sMember)
|
2021-08-19 23:49:23 +08:00
|
|
|
{
|
|
|
|
sClazz="";
|
|
|
|
sMember="";
|
|
|
|
sOperator="";
|
|
|
|
QString result="";
|
|
|
|
int bracketLevel = 0;
|
|
|
|
// Obtain stuff before first operator
|
|
|
|
int firstOpStart = phrase.length() + 1;
|
|
|
|
int firstOpEnd = phrase.length() + 1;
|
|
|
|
for (int i = 0; i<phrase.length();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;
|
|
|
|
for (int i = firstOpEnd; i<phrase.length();i++) {
|
|
|
|
if ((i+1<phrase.length()) && (phrase[i] == '-') && (phrase[i + 1] == '>') && (bracketLevel=0)) {
|
|
|
|
secondOp = i;
|
|
|
|
break;
|
|
|
|
} else if ((i+1<phrase.length()) && (phrase[i] == ':') && (phrase[i + 1] == ':') && (bracketLevel=0)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-19 19:17:49 +08:00
|
|
|
static bool isIdentChar(const QChar& ch) {
|
|
|
|
return ch.isLetter()
|
|
|
|
|| ch == '_'
|
|
|
|
|| ch.isDigit();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:49:37 +08:00
|
|
|
QString CppParser::removeArgNames(const QString &args)
|
|
|
|
{
|
|
|
|
QString result = "";
|
|
|
|
int argsLen = args.length();
|
|
|
|
if (argsLen < 2)
|
|
|
|
return "";
|
|
|
|
int i=1; // skip start '('
|
|
|
|
QString currentArg;
|
|
|
|
QString word;
|
|
|
|
int brackLevel = 0;
|
|
|
|
bool typeGetted = false;
|
|
|
|
while (i<argsLen-1) { //skip end ')'
|
|
|
|
switch(args[i].unicode()) {
|
|
|
|
case ',':
|
|
|
|
if (brackLevel >0) {
|
|
|
|
word+=args[i];
|
|
|
|
} else {
|
|
|
|
if (!typeGetted) {
|
2022-04-19 19:17:49 +08:00
|
|
|
appendArgWord(currentArg,word);
|
2021-08-15 16:49:37 +08:00
|
|
|
} else {
|
2022-01-26 12:17:15 +08:00
|
|
|
if (isCppKeyword(word)) {
|
2022-04-19 19:17:49 +08:00
|
|
|
appendArgWord(currentArg,word);
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
word = "";
|
|
|
|
result += currentArg.trimmed() + ',';
|
|
|
|
currentArg = "";
|
|
|
|
typeGetted = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
case '[':
|
|
|
|
case '(':
|
|
|
|
brackLevel++;
|
|
|
|
word+=args[i];
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
case ']':
|
|
|
|
case ')':
|
|
|
|
brackLevel--;
|
|
|
|
word+=args[i];
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
if ((brackLevel >0) && !isSpaceChar(args[i-1])) {
|
|
|
|
word+=args[i];
|
2022-04-19 19:17:49 +08:00
|
|
|
} else if (!word.isEmpty()) {
|
|
|
|
if (!typeGetted) {
|
|
|
|
appendArgWord(currentArg,word);
|
|
|
|
if (mCppTypeKeywords.contains(word) || !isCppKeyword(word))
|
|
|
|
typeGetted = true;
|
|
|
|
} else {
|
|
|
|
if (isCppKeyword(word))
|
|
|
|
appendArgWord(currentArg,word);
|
|
|
|
}
|
|
|
|
word = "";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
case '*':
|
|
|
|
if (!word.isEmpty()) {
|
2021-08-15 16:49:37 +08:00
|
|
|
if (!typeGetted) {
|
2022-04-19 19:17:49 +08:00
|
|
|
appendArgWord(currentArg,word);
|
2022-01-26 12:17:15 +08:00
|
|
|
if (mCppTypeKeywords.contains(word) || !isCppKeyword(word))
|
2021-08-15 16:49:37 +08:00
|
|
|
typeGetted = true;
|
|
|
|
} else {
|
2022-01-26 12:17:15 +08:00
|
|
|
if (isCppKeyword(word))
|
2022-04-19 19:17:49 +08:00
|
|
|
appendArgWord(currentArg,word);
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
word = "";
|
|
|
|
}
|
2022-04-19 19:17:49 +08:00
|
|
|
currentArg+=args[i];
|
2021-08-15 16:49:37 +08:00
|
|
|
break;
|
|
|
|
default:
|
2022-04-19 19:17:49 +08:00
|
|
|
if (isIdentChar(args[i])) {
|
2021-08-15 16:49:37 +08:00
|
|
|
word+=args[i];
|
2022-04-19 19:17:49 +08:00
|
|
|
}
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (!typeGetted) {
|
2022-04-19 19:17:49 +08:00
|
|
|
appendArgWord(currentArg,word);
|
2021-08-15 16:49:37 +08:00
|
|
|
} else {
|
2022-01-26 12:17:15 +08:00
|
|
|
if (isCppKeyword(word)) {
|
2022-04-19 19:17:49 +08:00
|
|
|
appendArgWord(currentArg,word);
|
2021-08-15 16:49:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result += currentArg.trimmed();
|
|
|
|
return result;
|
|
|
|
}
|
2021-08-15 17:52:39 +08:00
|
|
|
|
2022-10-31 19:37:24 +08:00
|
|
|
bool CppParser::isNotFuncArgs(int startIndex, int endIndex)
|
2021-08-15 20:25:54 +08:00
|
|
|
{
|
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 '\'':
|
2021-08-15 20:25:54 +08:00
|
|
|
return true;
|
2022-10-31 19:37:24 +08:00
|
|
|
case '(':
|
|
|
|
case '[':
|
|
|
|
case '{':
|
|
|
|
i=mTokenizer[i]->matchIndex+1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isDigitChar(ch))
|
2021-08-15 20:25:54 +08:00
|
|
|
return true;
|
2022-10-31 19:37:24 +08:00
|
|
|
if (isLetterChar(ch)) {
|
|
|
|
QString currentText=mTokenizer[i]->text;
|
|
|
|
if (currentText=="true" || currentText=="false" || currentText=="nullptr"
|
|
|
|
|| currentText=="const")
|
|
|
|
return true;
|
|
|
|
PStatement statement =findStatementOf(mCurrentFile,word,getCurrentScope(),true);
|
|
|
|
if (statement &&
|
|
|
|
!isTypeStatement(statement->kind))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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-01 09:02:17 +08:00
|
|
|
int CppParser::indexOfNextSemicolon(int index)
|
|
|
|
{
|
|
|
|
while (index<mTokenizer.tokenCount()) {
|
|
|
|
switch(mTokenizer[index]->text[0].unicode()) {
|
|
|
|
case ';':
|
|
|
|
return index;
|
|
|
|
case '(':
|
|
|
|
index = mTokenizer[index]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::indexOfNextColon(int index)
|
|
|
|
{
|
|
|
|
while (index<mTokenizer.tokenCount()) {
|
|
|
|
switch(mTokenizer[index]->text[0].unicode()) {
|
|
|
|
case ':':
|
|
|
|
return index;
|
|
|
|
case '(':
|
|
|
|
index = mTokenizer[index]->matchIndex+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::indexOfNextLeftBrace(int index)
|
|
|
|
{
|
|
|
|
while (index<mTokenizer.tokenCount()) {
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
while (index<mTokenizer.tokenCount()) {
|
|
|
|
if (mTokenizer[index]->text=='(') {
|
|
|
|
return mTokenizer[index]->matchIndex+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CppParser::indexPassBraces(int index)
|
|
|
|
{
|
|
|
|
while (index<mTokenizer.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-07-28 13:51:38 +08:00
|
|
|
ParserLanguage CppParser::language() const
|
|
|
|
{
|
|
|
|
return mLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppParser::setLanguage(ParserLanguage newLanguage)
|
|
|
|
{
|
|
|
|
mLanguage = newLanguage;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
mOnGetFileStream = 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;
|
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();
|
|
|
|
}
|