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-05 23:13:21 +08:00
|
|
|
#include "statementmodel.h"
|
|
|
|
|
2021-08-08 17:22:37 +08:00
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
2021-08-05 23:13:21 +08:00
|
|
|
StatementModel::StatementModel(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2021-08-07 14:08:51 +08:00
|
|
|
mCount = 0;
|
2021-08-07 18:02:57 +08:00
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
void StatementModel::add(const PStatement& statement)
|
2021-08-07 18:02:57 +08:00
|
|
|
{
|
|
|
|
if (!statement) {
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
PStatement parent = statement->parentScope.lock();
|
|
|
|
if (parent) {
|
|
|
|
addMember(parent->children,statement);
|
|
|
|
} else {
|
|
|
|
addMember(mGlobalStatements,statement);
|
|
|
|
}
|
|
|
|
mCount++;
|
2021-08-27 08:52:20 +08:00
|
|
|
#ifdef QT_DEBUG
|
|
|
|
mAllStatements.append(statement);
|
|
|
|
#endif
|
|
|
|
|
2021-08-07 18:02:57 +08:00
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
void StatementModel::deleteStatement(const PStatement& statement)
|
2021-08-07 18:02:57 +08:00
|
|
|
{
|
|
|
|
if (!statement) {
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
PStatement parent = statement->parentScope.lock();
|
|
|
|
int count = 0;
|
|
|
|
if (parent) {
|
|
|
|
count = deleteMember(parent->children,statement);
|
|
|
|
} else {
|
|
|
|
count = deleteMember(mGlobalStatements,statement);
|
|
|
|
}
|
|
|
|
mCount -= count;
|
2021-08-27 08:52:20 +08:00
|
|
|
#ifdef QT_DEBUG
|
|
|
|
mAllStatements.removeOne(statement);
|
|
|
|
#endif
|
|
|
|
|
2021-08-07 14:08:51 +08:00
|
|
|
}
|
2021-08-05 23:13:21 +08:00
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
const StatementMap &StatementModel::childrenStatements(const PStatement& statement) const
|
2021-08-07 14:08:51 +08:00
|
|
|
{
|
|
|
|
if (!statement) {
|
|
|
|
return mGlobalStatements;
|
|
|
|
} else {
|
|
|
|
return statement->children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 15:05:10 +08:00
|
|
|
const StatementMap &StatementModel::childrenStatements(std::weak_ptr<Statement> statement) const
|
2021-08-07 14:08:51 +08:00
|
|
|
{
|
|
|
|
PStatement s = statement.lock();
|
|
|
|
return childrenStatements(s);
|
2021-08-05 23:13:21 +08:00
|
|
|
}
|
2021-08-07 18:02:57 +08:00
|
|
|
|
|
|
|
void StatementModel::clear() {
|
|
|
|
mCount=0;
|
|
|
|
mGlobalStatements.clear();
|
|
|
|
}
|
|
|
|
|
2021-08-08 17:22:37 +08:00
|
|
|
void StatementModel::dump(const QString &logFile)
|
|
|
|
{
|
|
|
|
QFile file(logFile);
|
|
|
|
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
|
|
|
|
QTextStream out(&file);
|
|
|
|
dumpStatementMap(mGlobalStatements,out,0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 08:52:20 +08:00
|
|
|
#ifdef QT_DEBUG
|
|
|
|
void StatementModel::dumpAll(const QString &logFile)
|
|
|
|
{
|
|
|
|
QFile file(logFile);
|
|
|
|
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
|
|
|
|
QTextStream out(&file);
|
|
|
|
for (PStatement statement:mAllStatements) {
|
|
|
|
out<<QString("%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12")
|
|
|
|
.arg(statement->command).arg(int(statement->kind))
|
|
|
|
.arg(statement->type).arg(statement->fullName)
|
|
|
|
.arg((size_t)(statement->parentScope.lock().get()))
|
|
|
|
.arg((int)statement->classScope)
|
|
|
|
.arg(statement->fileName)
|
|
|
|
.arg(statement->line)
|
|
|
|
.arg(statement->definitionFileName)
|
2022-03-23 14:45:10 +08:00
|
|
|
.arg(statement->definitionLine)<<endl;
|
2021-08-27 08:52:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
void StatementModel::addMember(StatementMap &map, const PStatement& statement)
|
2021-08-07 18:02:57 +08:00
|
|
|
{
|
|
|
|
if (!statement)
|
|
|
|
return ;
|
2021-08-11 11:47:47 +08:00
|
|
|
map.insert(statement->command,statement);
|
|
|
|
// QList<PStatement> lst = map.values(statement->command);
|
|
|
|
// if (!lst) {
|
|
|
|
// lst=std::make_shared<StatementList>();
|
|
|
|
// map.insert(,lst);
|
|
|
|
// }
|
|
|
|
// lst->append(statement);
|
2021-08-07 18:02:57 +08:00
|
|
|
}
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
int StatementModel::deleteMember(StatementMap &map, const PStatement& statement)
|
2021-08-07 18:02:57 +08:00
|
|
|
{
|
|
|
|
if (!statement)
|
|
|
|
return 0;
|
2021-08-27 16:38:55 +08:00
|
|
|
return map.remove(statement->command,statement);
|
2021-08-07 18:02:57 +08:00
|
|
|
}
|
2021-08-08 17:22:37 +08:00
|
|
|
|
|
|
|
void StatementModel::dumpStatementMap(StatementMap &map, QTextStream &out, int level)
|
|
|
|
{
|
2021-08-27 08:52:20 +08:00
|
|
|
QString indent(level,'\t');
|
2021-08-29 10:14:07 +08:00
|
|
|
foreach (const PStatement& statement,map) {
|
2021-08-11 11:47:47 +08:00
|
|
|
out<<indent<<QString("%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12")
|
|
|
|
.arg(statement->command).arg(int(statement->kind))
|
2021-08-29 10:14:07 +08:00
|
|
|
.arg(statement->type,statement->fullName)
|
2022-04-18 16:56:31 +08:00
|
|
|
.arg(statement->noNameArgs)
|
|
|
|
.arg(statement->args)
|
2021-08-11 11:47:47 +08:00
|
|
|
.arg((size_t)(statement->parentScope.lock().get()))
|
|
|
|
.arg((int)statement->classScope)
|
|
|
|
.arg(statement->fileName)
|
|
|
|
.arg(statement->line)
|
|
|
|
.arg(statement->definitionFileName)
|
2022-03-23 14:45:10 +08:00
|
|
|
.arg(statement->definitionLine);
|
2022-07-04 11:39:06 +08:00
|
|
|
out
|
2022-07-24 11:19:11 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
|
2022-07-04 11:39:06 +08:00
|
|
|
<<Qt::endl;
|
|
|
|
#else
|
|
|
|
<<endl;
|
|
|
|
#endif
|
2021-08-11 11:47:47 +08:00
|
|
|
if (statement->children.isEmpty())
|
|
|
|
continue;
|
2022-07-04 11:39:06 +08:00
|
|
|
out<<indent<<statement->command<<" {"
|
2022-07-24 11:19:11 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
|
2022-07-04 11:39:06 +08:00
|
|
|
<<Qt::endl;
|
|
|
|
#else
|
|
|
|
<<endl;
|
|
|
|
#endif
|
2021-08-11 11:47:47 +08:00
|
|
|
dumpStatementMap(statement->children,out,level+1);
|
2022-07-04 11:39:06 +08:00
|
|
|
out<<indent<<"}"
|
2022-07-24 11:19:11 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
|
2022-07-04 11:39:06 +08:00
|
|
|
<<Qt::endl;
|
|
|
|
#else
|
|
|
|
<<endl;
|
|
|
|
#endif
|
|
|
|
|
2021-08-08 17:22:37 +08:00
|
|
|
}
|
|
|
|
}
|