refactor: openInclude

This commit is contained in:
Roy Qu 2024-04-09 19:39:35 +08:00
parent 35dccca91e
commit 37c7405311
8 changed files with 34 additions and 68 deletions

View File

@ -2276,7 +2276,7 @@ bool Editor::notParsed()
{ {
if (!mParser) if (!mParser)
return true; return true;
return mParser->findFileIncludes(mFilename)==nullptr; return mParser->findFileInfo(mFilename)==nullptr;
} }
void Editor::insertLine() void Editor::insertLine()

View File

@ -207,12 +207,10 @@ PStatement CppParser::doFindScopeStatement(const QString &filename, int line) co
return fileInfo->findScopeAtLine(line); return fileInfo->findScopeAtLine(line);
} }
PParsedFileInfo CppParser::findFileIncludes(const QString &filename, bool deleteIt) PParsedFileInfo CppParser::findFileInfo(const QString &filename)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(filename); PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(filename);
if (deleteIt && fileInfo)
mPreprocessor.removeFileIncludes(filename);
return fileInfo; return fileInfo;
} }
QString CppParser::findFirstTemplateParamOf(const QString &fileName, const QString &phrase, const PStatement& currentScope) QString CppParser::findFirstTemplateParamOf(const QString &fileName, const QString &phrase, const PStatement& currentScope)
@ -5903,11 +5901,9 @@ void CppParser::internalInvalidateFile(const QString &fileName)
return; return;
// remove its include files list // remove its include files list
PParsedFileInfo p = findFileIncludes(fileName, true); PParsedFileInfo p = mPreprocessor.findFileInfo(fileName);
if (p) { if (p) {
//fPreprocessor.InvalidDefinesInFile(FileName); //we don't need this, since we reset defines after each parse //invalidDefinesInFile(FileName); //we don't need this, since we reset defines after each parse
//p->includes.clear();
//p->usings.clear();
for(PStatement statement:p->statements()) { for(PStatement statement:p->statements()) {
if (statement->fileName==fileName) { if (statement->fileName==fileName) {
mStatementList.deleteStatement(statement); mStatementList.deleteStatement(statement);
@ -5917,7 +5913,6 @@ void CppParser::internalInvalidateFile(const QString &fileName)
statement->definitionLine = statement->line; statement->definitionLine = statement->line;
} }
} }
p->clearStatements();
//invalidate all handledInheritances //invalidate all handledInheritances
for (std::weak_ptr<ClassInheritanceInfo> pWeakInfo: p->handledInheritances()) { for (std::weak_ptr<ClassInheritanceInfo> pWeakInfo: p->handledInheritances()) {
@ -5926,7 +5921,8 @@ void CppParser::internalInvalidateFile(const QString &fileName)
info->handled = false; info->handled = false;
} }
} }
p->clearHandledInheritances();
mPreprocessor.removeFileInfo(fileName);
} }
//remove all statements from namespace cache //remove all statements from namespace cache
@ -5965,7 +5961,7 @@ QSet<QString> CppParser::calculateFilesToBeReparsed(const QString &fileName)
return QSet<QString>(); return QSet<QString>();
QSet<QString> result; QSet<QString> result;
result.insert(fileName); result.insert(fileName);
foreach (const QString& file, mProjectFiles) { foreach (const QString& file, mPreprocessor.scannedFiles()) {
PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(file); PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(file);
if (fileInfo && fileInfo->including(fileName)) { if (fileInfo && fileInfo->including(fileName)) {
result.insert(file); result.insert(file);

View File

@ -48,7 +48,7 @@ public:
const QString& phrase, const QString& phrase,
int line); int line);
PStatement findScopeStatement(const QString& filename, int line); PStatement findScopeStatement(const QString& filename, int line);
PParsedFileInfo findFileIncludes(const QString &filename, bool deleteIt = false); PParsedFileInfo findFileInfo(const QString &filename);
QString findFirstTemplateParamOf(const QString& fileName, QString findFirstTemplateParamOf(const QString& fileName,
const QString& phrase, const QString& phrase,
const PStatement& currentScope); const PStatement& currentScope);

View File

@ -58,7 +58,7 @@ void CppPreprocessor::clearTempResults()
mBuffer.clear(); mBuffer.clear();
mResult.clear(); mResult.clear();
mCurrentFileInfo=nullptr; mCurrentFileInfo=nullptr;
mIncludes.clear(); // stack of files we've stepped into. last one is current file, first one is source file mIncludeStack.clear(); // stack of files we've stepped into. last one is current file, first one is source file
mBranchResults.clear();// stack of branch results (boolean). last one is current branch, first one is outermost branch mBranchResults.clear();// stack of branch results (boolean). last one is current branch, first one is outermost branch
//mDefines.clear(); // working set, editable //mDefines.clear(); // working set, editable
mProcessed.clear(); // dictionary to save filename already processed mProcessed.clear(); // dictionary to save filename already processed
@ -397,7 +397,7 @@ void CppPreprocessor::handleInclude(const QString &line, bool fromNext)
if (getCurrentBranch()!=BranchResult::isTrue) // we're skipping due to a branch failure if (getCurrentBranch()!=BranchResult::isTrue) // we're skipping due to a branch failure
return; return;
PParsedFile file = mIncludes.back(); PParsedFile file = mIncludeStack.back();
QString fileName; QString fileName;
// Get full header file name // Get full header file name
QString currentDir = includeTrailingPathDelimiter(extractFileDir(file->fileName)); QString currentDir = includeTrailingPathDelimiter(extractFileDir(file->fileName));
@ -765,23 +765,20 @@ void CppPreprocessor::openInclude(QString fileName)
fileName = fileInfo->fileName(); fileName = fileInfo->fileName();
} else { } else {
fileName.squeeze(); fileName.squeeze();
fileInfo = std::make_shared<ParsedFileInfo>(fileName);
} }
if (mIncludes.size()>0) { if (mIncludeStack.size()>0) {
// PParsedFile topFile = mIncludes.front();
// if (topFile->fileIncludes->includes.contains(fileName)) {
// PParsedFile innerMostFile = mIncludes.back();
// innerMostFile->fileIncludes->includes.insert(fileName, false);
// return; //already included
// }
bool alreadyIncluded = false; bool alreadyIncluded = false;
for (PParsedFile& parsedFile:mIncludes) { for (PParsedFile& parsedFile:mIncludeStack) {
if (parsedFile->fileInfo->including(fileName)) { if (parsedFile->fileInfo->including(fileName)) {
alreadyIncluded = true; alreadyIncluded = true;
} }
parsedFile->fileInfo->addInclude(fileName); parsedFile->fileInfo->addInclude(fileName);
foreach (const QString& includedFileName,fileInfo->includes()) {
parsedFile->fileInfo->addInclude(includedFileName);
} }
PParsedFile innerMostFile = mIncludes.back(); }
innerMostFile->fileInfo->addInclude(fileName); PParsedFile innerMostFile = mIncludeStack.back();
innerMostFile->fileInfo->addDirectInclude(fileName); innerMostFile->fileInfo->addDirectInclude(fileName);
if (alreadyIncluded) if (alreadyIncluded)
return; return;
@ -800,12 +797,8 @@ void CppPreprocessor::openInclude(QString fileName)
// Keep track of files we include here // Keep track of files we include here
// Only create new items for files we have NOT scanned yet // Only create new items for files we have NOT scanned yet
mCurrentFileInfo = findFileInfo(fileName); mCurrentFileInfo = fileInfo;
if (!mCurrentFileInfo) {
// do NOT create a new item for a file that's already in the list
mCurrentFileInfo = std::make_shared<ParsedFileInfo>(fileName);
mFileInfos.insert(fileName,mCurrentFileInfo); mFileInfos.insert(fileName,mCurrentFileInfo);
}
parsedFile->fileInfo = mCurrentFileInfo; parsedFile->fileInfo = mCurrentFileInfo;
// Don't parse stuff we have already parsed // Don't parse stuff we have already parsed
@ -827,16 +820,8 @@ void CppPreprocessor::openInclude(QString fileName)
} else { } else {
//add defines of already parsed including headers; //add defines of already parsed including headers;
addDefinesInFile(fileName); addDefinesInFile(fileName);
PParsedFileInfo fileInfo = findFileInfo(fileName);
if (fileInfo) {
for (PParsedFile& file:mIncludes) {
foreach (const QString& incFile,fileInfo->includes()) {
file->fileInfo->addInclude(incFile);
} }
} mIncludeStack.append(parsedFile);
}
}
mIncludes.append(parsedFile);
// Process it // Process it
mIndex = parsedFile->index; mIndex = parsedFile->index;
@ -850,7 +835,7 @@ void CppPreprocessor::openInclude(QString fileName)
// Update result file // Update result file
QString includeLine = "#include " + fileName + ":1"; QString includeLine = "#include " + fileName + ":1";
if (mIncludes.count()>1) { // include from within a file if (mIncludeStack.count()>1) { // include from within a file
mResult[mPreProcIndex] = includeLine; mResult[mPreProcIndex] = includeLine;
} else { } else {
mResult.append(includeLine); mResult.append(includeLine);
@ -860,13 +845,13 @@ void CppPreprocessor::openInclude(QString fileName)
void CppPreprocessor::closeInclude() void CppPreprocessor::closeInclude()
{ {
if (mIncludes.isEmpty()) if (mIncludeStack.isEmpty())
return; return;
mIncludes.pop_back(); mIncludeStack.pop_back();
if (mIncludes.isEmpty()) if (mIncludeStack.isEmpty())
return; return;
PParsedFile parsedFile = mIncludes.back(); PParsedFile parsedFile = mIncludeStack.back();
// Continue where we left off // Continue where we left off
mIndex = parsedFile->index; mIndex = parsedFile->index;
@ -1177,7 +1162,7 @@ QStringList CppPreprocessor::removeComments(const QStringList &text)
void CppPreprocessor::preprocessBuffer() void CppPreprocessor::preprocessBuffer()
{ {
while (mIncludes.count() > 0) { while (mIncludeStack.count() > 0) {
QString s; QString s;
do { do {
s = getNextPreprocessor(); s = getNextPreprocessor();

View File

@ -96,7 +96,7 @@ public:
return mFileInfos.value(fileName); return mFileInfos.value(fileName);
} }
void removeFileIncludes(const QString& fileName) { void removeFileInfo(const QString& fileName) {
mFileInfos.remove(fileName); mFileInfos.remove(fileName);
} }
@ -151,7 +151,7 @@ private:
// current file stuff // current file stuff
PParsedFile getInclude(int index) const { PParsedFile getInclude(int index) const {
return mIncludes[index]; return mIncludeStack[index];
} }
void openInclude(QString fileName); void openInclude(QString fileName);
void closeInclude(); void closeInclude();
@ -261,7 +261,7 @@ private:
QStringList mResult; QStringList mResult;
PParsedFileInfo mCurrentFileInfo; PParsedFileInfo mCurrentFileInfo;
int mPreProcIndex; int mPreProcIndex;
QList<PParsedFile> mIncludes; // stack of files we've stepped into. last one is current file, first one is source file QList<PParsedFile> mIncludeStack; // stack of files we've stepped into. last one is current file, first one is source file
QList<BranchResult> mBranchResults;// stack of branch results (boolean). last one is current branch, first one is outermost branch QList<BranchResult> mBranchResults;// stack of branch results (boolean). last one is current branch, first one is outermost branch
DefineMap mDefines; // working set, editable DefineMap mDefines; // working set, editable
QSet<QString> mProcessed; // dictionary to save filename already processed QSet<QString> mProcessed; // dictionary to save filename already processed

View File

@ -792,23 +792,9 @@ bool ParsedFileInfo::isLineVisible(int line) const
void ParsedFileInfo::addInclude(const QString &fileName) void ParsedFileInfo::addInclude(const QString &fileName)
{ {
int count = mIncludeCounts.value(fileName,0);
count++;
mIncludeCounts.insert(fileName,count);
mIncludes.insert(fileName); mIncludes.insert(fileName);
} }
void ParsedFileInfo::removeInclude(const QString &fileName)
{
int count = mIncludeCounts.value(fileName,0);
count--;
if (count<=0) {
mIncludeCounts.remove(fileName);
mIncludes.remove(fileName);
} else
mIncludeCounts.insert(fileName,count);
}
void ParsedFileInfo::addDirectInclude(const QString &fileName) void ParsedFileInfo::addDirectInclude(const QString &fileName)
{ {
mDirectIncludes.append(fileName); mDirectIncludes.append(fileName);

View File

@ -325,7 +325,6 @@ public:
void insertBranch(int level, bool branchTrue); void insertBranch(int level, bool branchTrue);
bool isLineVisible(int line) const; bool isLineVisible(int line) const;
void addInclude(const QString &fileName); void addInclude(const QString &fileName);
void removeInclude(const QString &fileName);
void addDirectInclude(const QString &fileName); void addDirectInclude(const QString &fileName);
bool including(const QString &fileName) const; bool including(const QString &fileName) const;
PStatement findScopeAtLine(int line) const; PStatement findScopeAtLine(int line) const;
@ -344,20 +343,20 @@ public:
const QStringList& directIncludes() const; const QStringList& directIncludes() const;
const QSet<QString>& includes() const; const QSet<QString>& includes() const;
const QList<std::weak_ptr<ClassInheritanceInfo> >& handledInheritances() const; const QList<std::weak_ptr<ClassInheritanceInfo> >& handledInheritances() const;
const QSet<QString> &includedBySet() const;
int includedByCount(const QString &fileName) const;
private: private:
QString mFileName; QString mFileName;
QMap<QString, int> mIncludeCounts;
QSet<QString> mIncludes; QSet<QString> mIncludes;
QStringList mDirectIncludes; //We need order here. QStringList mDirectIncludes; //We need order here.
QSet<QString> mUsings; // namespaces it usings QSet<QString> mUsings; // namespaces it usings
StatementMap mStatements; // but we don't save temporary statements (full name as key) StatementMap mStatements; // but we don't save temporary statements (full name as key)
StatementMap mDeclaredStatements; // statements declared in this file (full name as key)
CppScopes mScopes; // int is start line of the statement scope CppScopes mScopes; // int is start line of the statement scope
QMap<int,bool> mBranches; QMap<int,bool> mBranches;
QList<std::weak_ptr<ClassInheritanceInfo>> mHandledInheritances; QList<std::weak_ptr<ClassInheritanceInfo>> mHandledInheritances;
}; };
using PParsedFileInfo = std::shared_ptr<ParsedFileInfo>; using PParsedFileInfo = std::shared_ptr<ParsedFileInfo>;
extern QStringList CppDirectives; extern QStringList CppDirectives;

View File

@ -283,7 +283,7 @@ void ClassBrowserModel::addMembers()
if (mCurrentFile.isEmpty()) if (mCurrentFile.isEmpty())
return; return;
// show statements in the file // show statements in the file
PParsedFileInfo p = mParser->findFileIncludes(mCurrentFile); PParsedFileInfo p = mParser->findFileInfo(mCurrentFile);
if (!p) if (!p)
return; return;
filterChildren(mRoot,p->statements()); filterChildren(mRoot,p->statements());
@ -291,7 +291,7 @@ void ClassBrowserModel::addMembers()
if (mParser->projectFiles().isEmpty()) if (mParser->projectFiles().isEmpty())
return; return;
foreach(const QString& file,mParser->projectFiles()) { foreach(const QString& file,mParser->projectFiles()) {
PParsedFileInfo p = mParser->findFileIncludes(file); PParsedFileInfo p = mParser->findFileInfo(file);
if (!p) if (!p)
return; return;
filterChildren(mRoot,p->statements()); filterChildren(mRoot,p->statements());