This commit is contained in:
Roy Qu 2024-04-09 17:15:27 +08:00
parent 4f1cc3d50d
commit edbe230bf7
5 changed files with 45 additions and 25 deletions

View File

@ -672,7 +672,7 @@ PStatement CppParser::doFindAliasedStatement(const PStatement &statement, QSet<S
if (nsName.isEmpty()) { if (nsName.isEmpty()) {
QList<PStatement> resultList = findMembersOfStatement(name,PStatement()); QList<PStatement> resultList = findMembersOfStatement(name,PStatement());
foreach(const PStatement& resultStatement,resultList) { foreach(const PStatement& resultStatement,resultList) {
if (fileInfo->includeFiles.contains(resultStatement->fileName)) { if (fileInfo->includes.contains(resultStatement->fileName)) {
result = resultStatement; result = resultStatement;
break; break;
} }
@ -685,7 +685,7 @@ PStatement CppParser::doFindAliasedStatement(const PStatement &statement, QSet<S
QList<PStatement> resultList = findMembersOfStatement(name,namespaceStatement); QList<PStatement> resultList = findMembersOfStatement(name,namespaceStatement);
foreach(const PStatement& resultStatement,resultList) { foreach(const PStatement& resultStatement,resultList) {
if (fileInfo->includeFiles.contains(resultStatement->fileName)) { if (fileInfo->includes.contains(resultStatement->fileName)) {
result = resultStatement; result = resultStatement;
break; break;
} }
@ -857,7 +857,7 @@ QSet<QString> CppParser::internalGetIncludedFiles(const QString &filename) const
PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(filename); PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(filename);
if (fileInfo) { if (fileInfo) {
foreach (const QString& file, fileInfo->includeFiles.keys()) { foreach (const QString& file, fileInfo->includes.keys()) {
list.insert(file); list.insert(file);
} }
} }
@ -888,7 +888,7 @@ QSet<QString> CppParser::internalGetFileUsings(const QString &filename) const
foreach (const QString& usingName, fileInfo->usings) { foreach (const QString& usingName, fileInfo->usings) {
result.insert(usingName); result.insert(usingName);
} }
foreach (const QString& subFile,fileInfo->includeFiles.keys()){ foreach (const QString& subFile,fileInfo->includes.keys()){
PParsedFileInfo subIncludes = mPreprocessor.findFileInfo(subFile); PParsedFileInfo subIncludes = mPreprocessor.findFileInfo(subFile);
if (subIncludes) { if (subIncludes) {
foreach (const QString& usingName, subIncludes->usings) { foreach (const QString& usingName, subIncludes->usings) {
@ -1843,7 +1843,7 @@ QStringList CppParser::sortFilesByIncludeRelations(const QSet<QString> &files)
PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(file); PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(file);
bool hasInclude=false; bool hasInclude=false;
if (fileInfo) { if (fileInfo) {
foreach(const QString& inc,fileInfo->includeFiles.keys()) { foreach(const QString& inc,fileInfo->includes.keys()) {
if (fileSet.contains(inc)) { if (fileSet.contains(inc)) {
hasInclude=true; hasInclude=true;
break; break;
@ -3397,7 +3397,7 @@ void CppParser::handlePreprocessor()
mCurrentFile = s.mid(0,delimPos).trimmed(); mCurrentFile = s.mid(0,delimPos).trimmed();
PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(mCurrentFile); PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(mCurrentFile);
if (fileInfo) { if (fileInfo) {
mCurrentFile = fileInfo->baseFile; mCurrentFile = fileInfo->fileName;
} else { } else {
mCurrentFile.squeeze(); mCurrentFile.squeeze();
} }
@ -4486,7 +4486,7 @@ PStatement CppParser::findMacro(const QString &phrase, const QString &fileName)
PParsedFileInfo includes = mPreprocessor.findFileInfo(fileName); PParsedFileInfo includes = mPreprocessor.findFileInfo(fileName);
foreach (const PStatement& s, statements) { foreach (const PStatement& s, statements) {
if (s->kind == StatementKind::Preprocessor) { if (s->kind == StatementKind::Preprocessor) {
if (includes && fileName != s->fileName && !includes->includeFiles.contains(s->fileName)) if (includes && fileName != s->fileName && !includes->includes.contains(s->fileName))
continue; continue;
return s; return s;
} }
@ -4552,8 +4552,8 @@ PStatement CppParser::findMemberOfStatement(const QString& filename,
return s; // hard defines return s; // hard defines
} if (s->fileName == filename || s->definitionFileName==filename) { } if (s->fileName == filename || s->definitionFileName==filename) {
return s; return s;
} else if (fileInfo && (fileInfo->includeFiles.contains(s->fileName) } else if (fileInfo && (fileInfo->includes.contains(s->fileName)
|| fileInfo->includeFiles.contains(s->definitionFileName))) { || fileInfo->includes.contains(s->definitionFileName))) {
return s; return s;
} }
} }
@ -5907,7 +5907,7 @@ void CppParser::internalInvalidateFile(const QString &fileName)
PParsedFileInfo p = findFileIncludes(fileName, true); PParsedFileInfo p = findFileIncludes(fileName, true);
if (p) { if (p) {
//fPreprocessor.InvalidDefinesInFile(FileName); //we don't need this, since we reset defines after each parse //fPreprocessor.InvalidDefinesInFile(FileName); //we don't need this, since we reset defines after each parse
//p->includeFiles.clear(); //p->includes.clear();
//p->usings.clear(); //p->usings.clear();
for (PStatement& statement:p->statements) { for (PStatement& statement:p->statements) {
if (statement->fileName==fileName) { if (statement->fileName==fileName) {
@ -5968,7 +5968,7 @@ QSet<QString> CppParser::calculateFilesToBeReparsed(const QString &fileName)
result.insert(fileName); result.insert(fileName);
foreach (const QString& file, mProjectFiles) { foreach (const QString& file, mProjectFiles) {
PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(file); PParsedFileInfo fileInfo = mPreprocessor.findFileInfo(file);
if (fileInfo && fileInfo->includeFiles.contains(fileName)) { if (fileInfo && fileInfo->includes.contains(fileName)) {
result.insert(file); result.insert(file);
} }
} }

View File

@ -225,7 +225,7 @@ void CppPreprocessor::dumpIncludesListTo(const QString &fileName) const
if (file.open(QIODevice::WriteOnly|QIODevice::Truncate)) { if (file.open(QIODevice::WriteOnly|QIODevice::Truncate)) {
QTextStream stream(&file); QTextStream stream(&file);
for (const PParsedFileInfo& fileInfo:mFileInfos) { for (const PParsedFileInfo& fileInfo:mFileInfos) {
stream<<fileInfo->baseFile<<" : " stream<<fileInfo->fileName<<" : "
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0) #if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
<<Qt::endl; <<Qt::endl;
#else #else
@ -237,7 +237,7 @@ void CppPreprocessor::dumpIncludesListTo(const QString &fileName) const
#else #else
<<endl; <<endl;
#endif #endif
foreach (const QString& s,fileInfo->includeFiles.keys()) { foreach (const QString& s,fileInfo->includes.keys()) {
stream<<"\t--"+s stream<<"\t--"+s
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0) #if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
<<Qt::endl; <<Qt::endl;
@ -762,27 +762,27 @@ void CppPreprocessor::openInclude(QString fileName)
{ {
PParsedFileInfo fileInfo = findFileInfo(fileName); PParsedFileInfo fileInfo = findFileInfo(fileName);
if (fileInfo) { if (fileInfo) {
fileName = fileInfo->baseFile; fileName = fileInfo->fileName;
} else { } else {
fileName.squeeze(); fileName.squeeze();
} }
if (mIncludes.size()>0) { if (mIncludes.size()>0) {
// PParsedFile topFile = mIncludes.front(); // PParsedFile topFile = mIncludes.front();
// if (topFile->fileIncludes->includeFiles.contains(fileName)) { // if (topFile->fileIncludes->includes.contains(fileName)) {
// PParsedFile innerMostFile = mIncludes.back(); // PParsedFile innerMostFile = mIncludes.back();
// innerMostFile->fileIncludes->includeFiles.insert(fileName, false); // innerMostFile->fileIncludes->includes.insert(fileName, false);
// return; //already included // return; //already included
// } // }
bool alreadyIncluded = false; bool alreadyIncluded = false;
for (PParsedFile& parsedFile:mIncludes) { for (PParsedFile& parsedFile:mIncludes) {
if (parsedFile->fileInfo->includeFiles.contains(fileName)) { if (parsedFile->fileInfo->includes.contains(fileName)) {
alreadyIncluded = true; alreadyIncluded = true;
continue; continue;
} }
parsedFile->fileInfo->includeFiles.insert(fileName,false); parsedFile->fileInfo->includes.insert(fileName,false);
} }
PParsedFile innerMostFile = mIncludes.back(); PParsedFile innerMostFile = mIncludes.back();
innerMostFile->fileInfo->includeFiles.insert(fileName,true); innerMostFile->fileInfo->includes.insert(fileName,true);
innerMostFile->fileInfo->directIncludes.append(fileName); innerMostFile->fileInfo->directIncludes.append(fileName);
if (alreadyIncluded) if (alreadyIncluded)
return; return;
@ -805,7 +805,7 @@ void CppPreprocessor::openInclude(QString fileName)
if (!mCurrentFileInfo) { if (!mCurrentFileInfo) {
// do NOT create a new item for a file that's already in the list // do NOT create a new item for a file that's already in the list
mCurrentFileInfo = std::make_shared<ParsedFileInfo>(); mCurrentFileInfo = std::make_shared<ParsedFileInfo>();
mCurrentFileInfo->baseFile = fileName; mCurrentFileInfo->fileName = fileName;
mFileInfos.insert(fileName,mCurrentFileInfo); mFileInfos.insert(fileName,mCurrentFileInfo);
} }
parsedFile->fileInfo = mCurrentFileInfo; parsedFile->fileInfo = mCurrentFileInfo;
@ -832,8 +832,8 @@ void CppPreprocessor::openInclude(QString fileName)
PParsedFileInfo fileInfo = findFileInfo(fileName); PParsedFileInfo fileInfo = findFileInfo(fileName);
if (fileInfo) { if (fileInfo) {
for (PParsedFile& file:mIncludes) { for (PParsedFile& file:mIncludes) {
foreach (const QString& incFile,fileInfo->includeFiles.keys()) { foreach (const QString& incFile,fileInfo->includes.keys()) {
file->fileInfo->includeFiles.insert(incFile,false); file->fileInfo->includes.insert(incFile,false);
} }
} }
} }
@ -917,7 +917,7 @@ void CppPreprocessor::addDefinesInFile(const QString &fileName)
PParsedFileInfo fileInfo = findFileInfo(fileName); PParsedFileInfo fileInfo = findFileInfo(fileName);
if (fileInfo) { if (fileInfo) {
foreach (const QString& file, fileInfo->includeFiles.keys()) { foreach (const QString& file, fileInfo->includes.keys()) {
addDefinesInFile(file); addDefinesInFile(file);
} }
} }

View File

@ -43,6 +43,7 @@ struct ParsedFile {
int branches; //branch levels; int branches; //branch levels;
PParsedFileInfo fileInfo; // info of this file PParsedFileInfo fileInfo; // info of this file
}; };
using PParsedFile = std::shared_ptr<ParsedFile>; using PParsedFile = std::shared_ptr<ParsedFile>;
class CppPreprocessor class CppPreprocessor

View File

@ -778,3 +778,20 @@ bool ParsedFileInfo::isLineVisible(int line)
} }
return lastI<0?true:branches[lastI]; return lastI<0?true:branches[lastI];
} }
void ParsedFileInfo::includeFile(const QString &fileName)
{
int count = includes.value(fileName,0);
count++;
includes.insert(fileName,count);
}
void ParsedFileInfo::unincludeFile(const QString &fileName)
{
int count = includes.value(fileName,0);
count--;
if (count<=0)
includes.remove(fileName);
else
includes.insert(fileName,count);
}

View File

@ -318,8 +318,8 @@ struct ClassInheritanceInfo {
using PClassInheritanceInfo = std::shared_ptr<ClassInheritanceInfo>; using PClassInheritanceInfo = std::shared_ptr<ClassInheritanceInfo>;
struct ParsedFileInfo { struct ParsedFileInfo {
QString baseFile; QString fileName;
QMap<QString, bool> includeFiles; // true means the file is directly included, false means included indirectly QMap<QString, int> includes;
QStringList directIncludes; // QStringList directIncludes; //
QSet<QString> usings; // namespaces it usings QSet<QString> usings; // namespaces it usings
StatementMap statements; // but we don't save temporary statements (full name as key) StatementMap statements; // but we don't save temporary statements (full name as key)
@ -328,6 +328,8 @@ struct ParsedFileInfo {
QMap<int,bool> branches; QMap<int,bool> branches;
QList<std::weak_ptr<ClassInheritanceInfo>> handledInheritances; QList<std::weak_ptr<ClassInheritanceInfo>> handledInheritances;
bool isLineVisible(int line); bool isLineVisible(int line);
void includeFile(const QString &fileName);
void unincludeFile(const QString& fileName);
}; };
using PParsedFileInfo = std::shared_ptr<ParsedFileInfo>; using PParsedFileInfo = std::shared_ptr<ParsedFileInfo>;