work save

This commit is contained in:
royqh1979@gmail.com 2021-08-10 21:27:24 +08:00
parent 2692d77fb1
commit 1050526a2c
4 changed files with 149 additions and 1 deletions

View File

@ -101,10 +101,84 @@ void CppPreprocessor::handleBranch(const QString &line)
} }
} }
void CppPreprocessor::handleDefine(const QString &line)
{
if (getCurrentBranch()) {
addDefineByLine(line, false);
mResult[mPreProcIndex] = '#' + line; // add define to result file so the parser can handle it
}
}
void CppPreprocessor::handleInclude(const QString &line)
{
if (!getCurrentBranch()) // we're skipping due to a branch failure
return;
PParsedFile file = mIncludes.back();
// Get full header file name
QString fileName = getHeaderFileName(file->fileName, line,mIncludePaths,
mProjectIncludePaths);
if (fileName.isEmpty())
return;
mCurrentIncludes->includeFiles.insert(fileName,true);
// And open a new entry
openInclude(fileName);
}
void CppPreprocessor::handlePreprocessor(const QString &value)
{
if (value.startsWith("define"))
handleDefine(value);
else if (value.startsWith("undef"))
handleUndefine(value);
else if (value.startsWith("if")
|| value.startsWith("else") || value.startsWith("elif")
|| value.startsWith("endif"))
handleBranch(value);
else if (value.startsWith("include"))
handleInclude(value);
}
void CppPreprocessor::handleUndefine(const QString &line)
{
// Remove undef
Name := TrimLeft(Copy(Line, Length('undef') + 1, MaxInt));
files:=TStringList.Create;
files.Sorted:=True;
files.Duplicates:=dupIgnore;
try
// may be defined many times
while True do begin
Define := GetDefine(Name, Index);
if Assigned(Define) then begin
fDefineIndex.Remove(Name);
fDefines.objects[index]:=nil;
files.AddObject(Define^.FileName,Pointer(Define));
end else
break;
end;
for i:=0 to files.Count-1 do begin
Define:= PDefine(files.objects[i]);
idx:=FastIndexOf(fFileDefines,files[i]);
if idx>0 then begin
DefineList:=TList(fFileDefines.Objects[idx]);
DefineList.Remove(Pointer(Define));
define^.ArgList.Free;
Dispose(PDefine(Define));
end;
end;
finally
files.Free;
end;
}
QString CppPreprocessor::expandMacros(const QString &line, int depth) QString CppPreprocessor::expandMacros(const QString &line, int depth)
{ {
//prevent infinit recursion //prevent infinit recursion
if (depth > 20) if (depth > MAX_DEFINE_EXPAND_DEPTH)
return line; return line;
QString word; QString word;
QString newLine; QString newLine;

View File

@ -5,6 +5,8 @@
#include <QTextStream> #include <QTextStream>
#include "utils.h" #include "utils.h"
#define MAX_DEFINE_EXPAND_DEPTH 20
struct ParsedFile { struct ParsedFile {
int index; // 0-based for programming convenience int index; // 0-based for programming convenience
QString fileName; // Record filename, but not used now QString fileName; // Record filename, but not used now

View File

@ -1,5 +1,9 @@
#include "utils.h" #include "utils.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
QStringList CppDirectives; QStringList CppDirectives;
QStringList JavadocTags; QStringList JavadocTags;
QMap<QString,SkipType> CppKeywords; QMap<QString,SkipType> CppKeywords;
@ -233,3 +237,64 @@ void initParser()
JavadocTags.append("@value"); JavadocTags.append("@value");
JavadocTags.append("@version"); JavadocTags.append("@version");
} }
QString getHeaderFileName(const QString &relativeTo, const QString &line, const QStringList &includePaths, const QStringList &projectIncludePaths) {
QString result = "";
// Handle <>
int openTokenPos = line.indexOf('<');
if (openTokenPos >= 0) {
int closeTokenPos = line.indexOf('>',openTokenPos+1);
if (closeTokenPos >=0) {
QString fileName = line.mid(openTokenPos + 1, closeTokenPos - openTokenPos - 1);
//project settings is preferred
result = getSystemHeaderFileName(fileName, projectIncludePaths);
if (result.isEmpty()) {
result = getSystemHeaderFileName(fileName, includePaths);
}
}
} else {
// Try ""
openTokenPos = line.indexOf('"');
if (openTokenPos >= 0) {
int closeTokenPos = line.indexOf('"', openTokenPos+1);
if (closeTokenPos >= 0) {
QString fileName = line.mid(openTokenPos + 1, closeTokenPos - openTokenPos - 1);
result = getLocalHeaderFileName(relativeTo, fileName);
//project settings is preferred
if (result.isEmpty()) {
result = getSystemHeaderFileName(fileName, projectIncludePaths);
}
if (result.isEmpty()) {
result = getSystemHeaderFileName(fileName, includePaths);
}
}
}
}
}
QString getLocalHeaderFileName(const QString &relativeTo, const QString &fileName)
{
QFileInfo relativeFile(relativeTo);
QDir dir = relativeFile.dir();
// Search local directory
if (dir.exists(fileName)) {
return dir.absoluteFilePath(fileName);
}
return "";
}
QString getSystemHeaderFileName(const QString &fileName, const QStringList &includePaths)
{
if (includePaths.isEmpty())
return "";
// Search compiler include directories
for (QString path:includePaths) {
QDir dir(path);
if (dir.exists(fileName))
return dir.absoluteFilePath(fileName);
}
//not found
return "";
}

View File

@ -162,4 +162,11 @@ extern QSet<QString> STLContainers;
extern QSet<QString> STLElementMethods; extern QSet<QString> STLElementMethods;
void initParser(); void initParser();
QString getHeaderFileName(const QString& relativeTo, const QString& line,
const QStringList& includePaths, const QStringList &projectIncludePaths);
QString getLocalHeaderFileName(const QString& relativeTo, const QString& fileName);
QString getSystemHeaderFileName(const QString& fileName, const QStringList& includePaths);
#endif // PARSER_UTILS_H #endif // PARSER_UTILS_H