From 09c7b5e791632c0407124f0ced5e748210f24b59 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sun, 23 Oct 2022 23:06:55 +0800 Subject: [PATCH] fix: "." and ".." in included header paths not correctly handled --- RedPandaIDE/parser/cppparser.cpp | 52 ++++++++++++++++---------- RedPandaIDE/parser/cpppreprocessor.cpp | 12 +----- RedPandaIDE/parser/parserutils.cpp | 8 ++-- RedPandaIDE/parser/parserutils.h | 4 +- 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/RedPandaIDE/parser/cppparser.cpp b/RedPandaIDE/parser/cppparser.cpp index 33d4370a..c02280ee 100644 --- a/RedPandaIDE/parser/cppparser.cpp +++ b/RedPandaIDE/parser/cppparser.cpp @@ -814,28 +814,40 @@ void CppParser::parseFile(const QString &fileName, bool inProject, bool onlyIfNo if (onlyIfNotParsed && mPreprocessor.scannedFiles().contains(fName)) return; - QSet filesToReparsed = calculateFilesToBeReparsed(fileName); - internalInvalidateFiles(filesToReparsed); + if (inProject) { + QSet filesToReparsed = calculateFilesToBeReparsed(fileName); + internalInvalidateFiles(filesToReparsed); - QStringList files = sortFilesByIncludeRelations(filesToReparsed); + QStringList files = sortFilesByIncludeRelations(filesToReparsed); - if (inProject) - mProjectFiles.insert(fileName); - else { - mProjectFiles.remove(fileName); + mFilesToScanCount = files.count(); + mFilesScannedCount = 0; + + foreach (const QString& file,files) { + mFilesScannedCount++; + emit onProgress(file,mFilesToScanCount,mFilesScannedCount); + if (!mPreprocessor.scannedFiles().contains(file)) { + internalParse(file); + } + } + } else { + internalInvalidateFile(fileName); + mFilesToScanCount = 1; + mFilesScannedCount = 0; + + mFilesScannedCount++; + emit onProgress(fileName,mFilesToScanCount,mFilesScannedCount); + internalParse(fileName); } +// if (inProject) +// mProjectFiles.insert(fileName); +// else { +// mProjectFiles.remove(fileName); +// } + // Parse from disk or stream - mFilesToScanCount = files.count(); - mFilesScannedCount = 0; - foreach (const QString& file,files) { - mFilesScannedCount++; - emit onProgress(file,mFilesToScanCount,mFilesScannedCount); - if (!mPreprocessor.scannedFiles().contains(file)) { - internalParse(file); - } - } } } @@ -1442,7 +1454,8 @@ QStringList CppParser::sortFilesByIncludeRelations(const QSet &files) if (mOnGetFileStream) { mOnGetFileStream(file,buffer); } - mPreprocessor.setScanOptions(mParseGlobalHeaders, mParseLocalHeaders); + //we only use local include relations + mPreprocessor.setScanOptions(false, true); mPreprocessor.preprocess(file,buffer); mPreprocessor.clearTempResults(); } @@ -4324,10 +4337,11 @@ QSet CppParser::calculateFilesToBeReparsed(const QString &fileName) return QSet(); QSet result; result.insert(fileName); - foreach (const QString& file,mPreprocessor.includesList().keys()) { + foreach (const QString& file, mProjectFiles) { PFileIncludes fileIncludes = mPreprocessor.includesList()[file]; - if (fileIncludes->includeFiles.contains(fileName)) + if (fileIncludes->includeFiles.contains(fileName)) { result.insert(file); + } } return result; } diff --git a/RedPandaIDE/parser/cpppreprocessor.cpp b/RedPandaIDE/parser/cpppreprocessor.cpp index ca893eda..1512a875 100644 --- a/RedPandaIDE/parser/cpppreprocessor.cpp +++ b/RedPandaIDE/parser/cpppreprocessor.cpp @@ -479,7 +479,6 @@ void CppPreprocessor::handleInclude(const QString &line, bool fromNext) mOnGetFileStream(fileName,buffer); } openInclude(fileName, buffer); - //oldCurrentIncludes->includeFiles.insert(fileName,true); } void CppPreprocessor::handlePreprocessor(const QString &value) @@ -730,12 +729,6 @@ void CppPreprocessor::openInclude(const QString &fileName, QStringList bufferedT // do NOT create a new item for a file that's already in the list mCurrentIncludes = std::make_shared(); mCurrentIncludes->baseFile = fileName; - //mCurrentIncludes->includeFiles; - //mCurrentIncludes->usings; - //mCurrentIncludes->statements; - //mCurrentIncludes->scopes; - //mCurrentIncludes->dependedFiles; - //mCurrentIncludes->dependingFiles; mIncludesList.insert(fileName,mCurrentIncludes); } @@ -748,7 +741,7 @@ void CppPreprocessor::openInclude(const QString &fileName, QStringList bufferedT mScannedFiles.insert(fileName); // Only load up the file if we are allowed to parse it - bool isSystemFile = isSystemHeaderFile(fileName, mIncludePaths); + bool isSystemFile = isSystemHeaderFile(fileName, mIncludePaths) || isSystemHeaderFile(fileName, mProjectIncludePaths); if ((mParseSystem && isSystemFile) || (mParseLocal && !isSystemFile)) { if (!bufferedText.isEmpty()) { parsedFile->buffer = bufferedText; @@ -764,9 +757,6 @@ void CppPreprocessor::openInclude(const QString &fileName, QStringList bufferedT foreach (const QString& incFile,fileIncludes->includeFiles.keys()) { file->fileIncludes->includeFiles.insert(incFile,false); } -// file->fileIncludes->includeFiles = -// file->fileIncludes->includeFiles.unite(fileIncludes->includeFiles); - // file->fileIncludes->includeFiles.insert(fileIncludes->includeFiles); } } mIncludes.append(parsedFile); diff --git a/RedPandaIDE/parser/parserutils.cpp b/RedPandaIDE/parser/parserutils.cpp index 21362c10..59070c75 100644 --- a/RedPandaIDE/parser/parserutils.cpp +++ b/RedPandaIDE/parser/parserutils.cpp @@ -391,6 +391,7 @@ QString getHeaderFilename(const QString &relativeTo, const QString &line, } } } + return result; } @@ -400,7 +401,7 @@ QString getLocalHeaderFilename(const QString &relativeTo, const QString &fileNam QDir dir = relativeFile.dir(); // Search local directory if (dir.exists(fileName)) { - return dir.absoluteFilePath(fileName); + return QDir::cleanPath(dir.absoluteFilePath(fileName)); } return ""; } @@ -411,8 +412,9 @@ QString getSystemHeaderFilename(const QString &fileName, const QStringList& incl // Search compiler include directories for (const QString& path:includePaths) { QDir dir(path); - if (dir.exists(fileName)) - return dir.absoluteFilePath(fileName); + if (dir.exists(fileName)) { + return QDir::cleanPath(dir.absoluteFilePath(fileName)); + } } //not found return ""; diff --git a/RedPandaIDE/parser/parserutils.h b/RedPandaIDE/parser/parserutils.h index 21d8dea6..70766bfe 100644 --- a/RedPandaIDE/parser/parserutils.h +++ b/RedPandaIDE/parser/parserutils.h @@ -76,14 +76,14 @@ enum StatementKind { skEnumType, skEnumClassType, skTypedef, + skConstructor, + skDestructor, skFunction, skVariable, skGlobalVariable, skLocalVariable, skEnum, skOperator, - skConstructor, - skDestructor, skParameter, skBlock, skUserCodeSnippet, // user code template