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)
return true;
return mParser->findFileIncludes(mFilename)==nullptr;
return mParser->findFileInfo(mFilename)==nullptr;
}
void Editor::insertLine()

View File

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

View File

@ -48,7 +48,7 @@ public:
const QString& phrase,
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,
const QString& phrase,
const PStatement& currentScope);

View File

@ -58,7 +58,7 @@ void CppPreprocessor::clearTempResults()
mBuffer.clear();
mResult.clear();
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
//mDefines.clear(); // working set, editable
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
return;
PParsedFile file = mIncludes.back();
PParsedFile file = mIncludeStack.back();
QString fileName;
// Get full header file name
QString currentDir = includeTrailingPathDelimiter(extractFileDir(file->fileName));
@ -765,23 +765,20 @@ void CppPreprocessor::openInclude(QString fileName)
fileName = fileInfo->fileName();
} else {
fileName.squeeze();
fileInfo = std::make_shared<ParsedFileInfo>(fileName);
}
if (mIncludes.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
// }
if (mIncludeStack.size()>0) {
bool alreadyIncluded = false;
for (PParsedFile& parsedFile:mIncludes) {
for (PParsedFile& parsedFile:mIncludeStack) {
if (parsedFile->fileInfo->including(fileName)) {
alreadyIncluded = true;
}
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);
if (alreadyIncluded)
return;
@ -800,12 +797,8 @@ void CppPreprocessor::openInclude(QString fileName)
// Keep track of files we include here
// Only create new items for files we have NOT scanned yet
mCurrentFileInfo = findFileInfo(fileName);
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);
}
mCurrentFileInfo = fileInfo;
mFileInfos.insert(fileName,mCurrentFileInfo);
parsedFile->fileInfo = mCurrentFileInfo;
// Don't parse stuff we have already parsed
@ -827,16 +820,8 @@ void CppPreprocessor::openInclude(QString fileName)
} else {
//add defines of already parsed including headers;
addDefinesInFile(fileName);
PParsedFileInfo fileInfo = findFileInfo(fileName);
if (fileInfo) {
for (PParsedFile& file:mIncludes) {
foreach (const QString& incFile,fileInfo->includes()) {
file->fileInfo->addInclude(incFile);
}
}
}
}
mIncludes.append(parsedFile);
mIncludeStack.append(parsedFile);
// Process it
mIndex = parsedFile->index;
@ -850,7 +835,7 @@ void CppPreprocessor::openInclude(QString fileName)
// Update result file
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;
} else {
mResult.append(includeLine);
@ -860,13 +845,13 @@ void CppPreprocessor::openInclude(QString fileName)
void CppPreprocessor::closeInclude()
{
if (mIncludes.isEmpty())
if (mIncludeStack.isEmpty())
return;
mIncludes.pop_back();
mIncludeStack.pop_back();
if (mIncludes.isEmpty())
if (mIncludeStack.isEmpty())
return;
PParsedFile parsedFile = mIncludes.back();
PParsedFile parsedFile = mIncludeStack.back();
// Continue where we left off
mIndex = parsedFile->index;
@ -1177,7 +1162,7 @@ QStringList CppPreprocessor::removeComments(const QStringList &text)
void CppPreprocessor::preprocessBuffer()
{
while (mIncludes.count() > 0) {
while (mIncludeStack.count() > 0) {
QString s;
do {
s = getNextPreprocessor();

View File

@ -96,7 +96,7 @@ public:
return mFileInfos.value(fileName);
}
void removeFileIncludes(const QString& fileName) {
void removeFileInfo(const QString& fileName) {
mFileInfos.remove(fileName);
}
@ -151,7 +151,7 @@ private:
// current file stuff
PParsedFile getInclude(int index) const {
return mIncludes[index];
return mIncludeStack[index];
}
void openInclude(QString fileName);
void closeInclude();
@ -261,7 +261,7 @@ private:
QStringList mResult;
PParsedFileInfo mCurrentFileInfo;
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
DefineMap mDefines; // working set, editable
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)
{
int count = mIncludeCounts.value(fileName,0);
count++;
mIncludeCounts.insert(fileName,count);
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)
{
mDirectIncludes.append(fileName);

View File

@ -325,7 +325,6 @@ public:
void insertBranch(int level, bool branchTrue);
bool isLineVisible(int line) const;
void addInclude(const QString &fileName);
void removeInclude(const QString &fileName);
void addDirectInclude(const QString &fileName);
bool including(const QString &fileName) const;
PStatement findScopeAtLine(int line) const;
@ -344,20 +343,20 @@ public:
const QStringList& directIncludes() const;
const QSet<QString>& includes() const;
const QList<std::weak_ptr<ClassInheritanceInfo> >& handledInheritances() const;
const QSet<QString> &includedBySet() const;
int includedByCount(const QString &fileName) const;
private:
QString mFileName;
QMap<QString, int> mIncludeCounts;
QSet<QString> mIncludes;
QStringList mDirectIncludes; //We need order here.
QSet<QString> mUsings; // namespaces it usings
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
QMap<int,bool> mBranches;
QList<std::weak_ptr<ClassInheritanceInfo>> mHandledInheritances;
};
using PParsedFileInfo = std::shared_ptr<ParsedFileInfo>;
extern QStringList CppDirectives;

View File

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