- fix: Parser: invalidating file may lost class inheritance infos.

This commit is contained in:
Roy Qu 2024-02-28 09:17:30 +08:00
parent 1728e953bf
commit 2743e6f682
6 changed files with 43 additions and 45 deletions

View File

@ -1613,6 +1613,8 @@ void CppParser::setInheritance(int index, const PStatement& classStatement, bool
inheritanceInfo->handled = false; inheritanceInfo->handled = false;
mClassInheritances.append(inheritanceInfo); mClassInheritances.append(inheritanceInfo);
handleInheritance(classStatement, inheritanceInfo);
} }
} }
@ -4373,6 +4375,29 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
mIndex++; mIndex++;
} }
void CppParser::handleInheritance(PStatement derivedStatement, PClassInheritanceInfo inheritanceInfo)
{
if (inheritanceInfo->handled)
return;
PStatement statement = doFindStatementOf(
inheritanceInfo->file,
inheritanceInfo->parentClassName,
inheritanceInfo->isGlobal?PStatement():derivedStatement->parentScope.lock());
if (statement && statement->kind == StatementKind::skClass) {
inheritClassStatement(derivedStatement,
inheritanceInfo->isStruct,
statement,
inheritanceInfo->visibility);
// inheritanceInfo->parentClassFilename = statement->fileName;
inheritanceInfo->handled = true;
PFileIncludes fileIncludes = mPreprocessor.findFileIncludes(statement->fileName);
Q_ASSERT(fileIncludes!=nullptr);
fileIncludes->handledInheritances.append(inheritanceInfo);
}
}
void CppParser::handleInheritances() void CppParser::handleInheritances()
{ {
for (int i=mClassInheritances.length()-1;i>=0;i--) { for (int i=mClassInheritances.length()-1;i>=0;i--) {
@ -4387,21 +4412,7 @@ void CppParser::handleInheritances()
} }
continue; continue;
} }
if (inheritanceInfo->handled) handleInheritance(derivedStatement, inheritanceInfo);
continue;
PStatement statement = doFindStatementOf(
inheritanceInfo->file,
inheritanceInfo->parentClassName,
inheritanceInfo->isGlobal?PStatement():derivedStatement->parentScope.lock());
if (statement && statement->kind == StatementKind::skClass) {
inheritClassStatement(derivedStatement,
inheritanceInfo->isStruct,
statement,
inheritanceInfo->visibility);
inheritanceInfo->parentClassFilename = statement->fileName;
inheritanceInfo->handled = true;
}
} }
//mClassInheritances.clear(); //mClassInheritances.clear();
} }
@ -5982,6 +5993,15 @@ void CppParser::internalInvalidateFile(const QString &fileName)
} }
} }
p->statements.clear(); p->statements.clear();
//invalidate all handledInheritances
for (std::weak_ptr<ClassInheritanceInfo> &pWeakInfo: p->handledInheritances) {
PClassInheritanceInfo info = pWeakInfo.lock();
if (info) {
info->handled = false;
}
}
p->handledInheritances.clear();
} }
//remove all statements from namespace cache //remove all statements from namespace cache
@ -6000,10 +6020,10 @@ void CppParser::internalInvalidateFile(const QString &fileName)
} }
// class inheritance // class inheritance
// invalid class inheritance infos (derived class is not valid) whould be auto removed in handleInheritances() // invalid class inheritance infos (derived class is not valid) whould be auto removed in handleInheritances()
foreach (const PClassInheritanceInfo& info, mClassInheritances) { // foreach (const PClassInheritanceInfo& info, mClassInheritances) {
if (info->handled && info->parentClassFilename == fileName) // if (info->handled && info->parentClassFilename == fileName)
info->handled = false; // info->handled = false;
} // }
// delete it from scannedfiles // delete it from scannedfiles
mPreprocessor.removeScannedFile(fileName); mPreprocessor.removeScannedFile(fileName);
} }

View File

@ -516,6 +516,7 @@ private:
void handleStructs(bool isTypedef = false); void handleStructs(bool isTypedef = false);
void handleUsing(); void handleUsing();
void handleVar(const QString& typePrefix,bool isExtern,bool isStatic); void handleVar(const QString& typePrefix,bool isExtern,bool isStatic);
void handleInheritance(PStatement derivedClass, PClassInheritanceInfo pInfo);
void handleInheritances(); void handleInheritances();
void skipRequires(); void skipRequires();
void internalParse(const QString& fileName); void internalParse(const QString& fileName);

View File

@ -295,6 +295,7 @@ private:
QVector<PCppScope> mScopes; QVector<PCppScope> mScopes;
}; };
struct ClassInheritanceInfo;
struct FileIncludes { struct FileIncludes {
QString baseFile; QString baseFile;
QMap<QString, bool> includeFiles; // true means the file is directly included, false means included indirectly QMap<QString, bool> includeFiles; // true means the file is directly included, false means included indirectly
@ -304,6 +305,7 @@ struct FileIncludes {
StatementMap declaredStatements; // statements declared in this file (full name as key) StatementMap declaredStatements; // statements declared in this file (full name as key)
CppScopes scopes; // int is start line of the statement scope CppScopes scopes; // int is start line of the statement scope
QMap<int,bool> branches; QMap<int,bool> branches;
QList<std::weak_ptr<ClassInheritanceInfo>> handledInheritances;
bool isLineVisible(int line); bool isLineVisible(int line);
}; };
using PFileIncludes = std::shared_ptr<FileIncludes>; using PFileIncludes = std::shared_ptr<FileIncludes>;

View File

@ -60,7 +60,7 @@ struct ClassInheritanceInfo {
bool isGlobal; bool isGlobal;
bool isStruct; bool isStruct;
StatementAccessibility visibility; StatementAccessibility visibility;
QString parentClassFilename; // QString parentClassFilename;
bool handled; bool handled;
}; };

View File

@ -22,29 +22,6 @@
namespace QSynedit { namespace QSynedit {
QSet<QString> QSynEditPainter::OperatorGlyphs {
"-",
"+",
"*",
"/",
"\\",
"~",
"!",
"@",
"#",
"$",
"%",
"^",
"&",
"|",
"=",
"<",
">",
"?",
":",
};
QSynEditPainter::QSynEditPainter(QSynEdit *edit, QPainter *painter, int firstRow, int lastRow, int left, int right): QSynEditPainter::QSynEditPainter(QSynEdit *edit, QPainter *painter, int firstRow, int lastRow, int left, int right):
mEdit{edit}, mEdit{edit},
mPainter{painter}, mPainter{painter},

View File

@ -109,8 +109,6 @@ private:
QRect mClip; QRect mClip;
int mFirstRow, mLastRow, mLeft, mRight; int mFirstRow, mLastRow, mLeft, mRight;
SynTokenAccu mTokenAccu; SynTokenAccu mTokenAccu;
static QSet<QString> OperatorGlyphs;
}; };
} }