work save

This commit is contained in:
royqh1979@gmail.com 2021-08-15 16:49:37 +08:00
parent 5174823cb8
commit 2376a09d73
5 changed files with 407 additions and 72 deletions

View File

@ -1,6 +1,301 @@
#include "cppparser.h"
#include "parserutils.h"
#include <QHash>
#include <QQueue>
CppParser::CppParser(QObject *parent) : QObject(parent)
{
}
PStatement CppParser::addInheritedStatement(PStatement derived, PStatement inherit, StatementClassScope access)
{
PStatement statement = addStatement(
derived,
inherit->fileName,
inherit->hintText,
inherit->type, // "Type" is already in use
inherit->command,
inherit->args,
inherit->value,
inherit->line,
inherit->kind,
inherit->scope,
access,
true,
inherit->inheritanceList,
inherit->isStatic);
statement->isInherited = true;
return statement;
}
PStatement CppParser::addChildStatement(PStatement parent, const QString &fileName, const QString &hintText, const QString &aType, const QString &command, const QString &args, const QString &value, int line, StatementKind kind, StatementScope scope, StatementClassScope classScope, bool isDefinition, bool isStatic)
{
return addStatement(
parent,
fileName,
hintText,
aType,
command,
args,
value,
line,
kind,
scope,
classScope,
isDefinition,
QList<std::weak_ptr<Statement>>(),
isStatic);
}
PStatement CppParser::addStatement(PStatement parent, const QString &fileName, const QString &hintText, const QString &aType, const QString &command, const QString &args, const QString &value, int line, StatementKind kind, StatementScope scope, StatementClassScope classScope, bool isDefinition, const QList<std::weak_ptr<Statement> > &inheritanceList, bool isStatic)
{
// Move '*', '&' to type rather than cmd (it's in the way for code-completion)
QString newType = aType;
QString newCommand = command;
while (!newCommand.isEmpty() && (newCommand.front() == '*' || newCommand.front() == '&')) {
newType += newCommand.front();
newCommand.remove(0,1); // remove first
}
QString noNameArgs = "";
if (kind == StatementKind::skConstructor
|| kind == StatementKind::skFunction
|| kind == StatementKind::skDestructor
|| kind == StatementKind::skVariable) {
noNameArgs = removeArgNames(args);
//find
PStatement oldStatement = findStatementInScope(command,noNameArgs,kind,parent);
if (oldStatement && isDefinition && !oldStatement->hasDefinition) {
oldStatement->hasDefinition = true;
if (oldStatement->fileName!=fileName) {
PFileIncludes fileIncludes1=findFileIncludes(fileName);
if (fileIncludes1) {
fileIncludes1->statements.insert(oldStatement->command,
oldStatement);
fileIncludes1->dependingFiles.insert(oldStatement->fileName);
PFileIncludes fileIncludes2=findFileIncludes(oldStatement->fileName);
if (fileIncludes2) {
fileIncludes2->dependedFiles.insert(fileName);
}
}
}
oldStatement->definitionLine = line;
oldStatement->definitionFileName = fileName;
return oldStatement;
}
}
PStatement result = std::make_shared<Statement>();
result->parentScope = parent;
result->hintText = hintText;
result->type = newType;
if (!newCommand.isEmpty())
result->command = newCommand;
else {
mUniqId++;
result->command = QString("__STATEMENT__%1").arg(mUniqId);
}
result->args = args;
result->noNameArgs = noNameArgs;
result->value = value;
result->kind = kind;
result->inheritanceList = inheritanceList;
result->scope = scope;
result->classScope = classScope;
result->hasDefinition = isDefinition;
result->line = line;
result->definitionLine = line;
result->fileName = fileName;
result->definitionFileName = fileName;
if (!fileName.isEmpty())
result->inProject = mIsProjectFile;
else
result->inProject = false;
result->inSystemHeader = mIsSystemHeader;
//result->children;
//result->friends;
result->isStatic = isStatic;
result->isInherited = false;
if (scope == StatementScope::ssLocal)
result->fullName = newCommand;
else
result->fullName = getFullStatementName(newCommand, parent);
result->usageCount = 0;
result->freqTop = 0;
if (result->kind == StatementKind::skNamespace) {
PStatementList namespaceList = mNamespaces.value(result->fullName,PStatementList());
if (!namespaceList) {
namespaceList=std::make_shared<StatementList>();
mNamespaces.insert(result->fullName,namespaceList);
}
namespaceList->append(result);
}
return result;
}
void CppParser::addSoloScopeLevel(PStatement statement, int line)
{
// Add class list
PStatement parentScope;
if (statement && (statement->kind == StatementKind::skBlock)) {
parentScope = statement->parentScope.lock();
while (parentScope && (parentScope->kind == StatementKind::skBlock)) {
parentScope = parentScope->parentScope.lock();
}
if (!parentScope)
statement.reset();
}
if (mCurrentClassScope.count()>0) {
mCurrentClassScope.back() = mClassScope;
}
mCurrentScope.append(statement);
PFileIncludes fileIncludes = findFileIncludes(mCurrentFile);
if (fileIncludes) {
fileIncludes->scopes.insert(line,statement);
}
// Set new scope
if (!statement)
mClassScope = StatementClassScope::scsNone; // {}, namespace or class that doesn't exist
else if (statement->kind == StatementKind::skNamespace)
mClassScope = StatementClassScope::scsNone;
else if (statement->type == "class")
mClassScope = StatementClassScope::scsPrivate; // classes are private by default
else
mClassScope = StatementClassScope::scsPublic; // structs are public by default
mCurrentClassScope.append(mClassScope);
}
bool CppParser::checkForCatchBlock()
{
return mIndex < mTokenizer.tokenCount() &&
mTokenizer[mIndex]->text == "catch";
}
bool CppParser::checkForEnum()
{
return mIndex < mTokenizer.tokenCount() &&
mTokenizer[mIndex]->text == "enum";
}
bool CppParser::checkForForBlock()
{
return mIndex < mTokenizer.tokenCount() &&
mTokenizer[mIndex]->text == "for";
}
bool CppParser::checkForKeyword()
{
SkipType st = CppKeywords.value(mTokenizer[mIndex]->text,SkipType::skNone);
return st!=SkipType::skNone;
}
bool CppParser::checkForMethod(QString &sType, QString &sName, QString &sArgs, bool &isStatic, bool &isFriend)
{
}
void CppParser::calculateFilesToBeReparsed(const QString &fileName, QStringList &files)
{
if (fileName.isEmpty())
return;
files.clear();
QQueue<QString> queue;
QSet<QString> processed;
queue.enqueue(fileName);
while (!queue.isEmpty()) {
QString name = queue.dequeue();
files.append(name);
processed.insert(name);
PFileIncludes p=findFileIncludes(name);
if (!p)
continue;
for (QString s:p->dependedFiles) {
if (!processed.contains(s)) {
queue.enqueue(s);
}
}
}
}
QString CppParser::removeArgNames(const QString &args)
{
QString result = "";
int argsLen = args.length();
if (argsLen < 2)
return "";
int i=1; // skip start '('
QString currentArg;
QString word;
int brackLevel = 0;
bool typeGetted = false;
while (i<argsLen-1) { //skip end ')'
switch(args[i].unicode()) {
case ',':
if (brackLevel >0) {
word+=args[i];
} else {
if (!typeGetted) {
currentArg += ' ' + word;
} else {
if (isKeyword(word)) {
currentArg += ' ' + word;
}
}
word = "";
result += currentArg.trimmed() + ',';
currentArg = "";
typeGetted = false;
}
break;
case '<':
case '[':
case '(':
brackLevel++;
word+=args[i];
break;
case '>':
case ']':
case ')':
brackLevel--;
word+=args[i];
break;
case ' ':
case '\t':
if ((brackLevel >0) && !isSpaceChar(args[i-1])) {
word+=args[i];
} else if (!word.trimmed().isEmpty()) {
if (!typeGetted) {
currentArg += ' ' + word;
if (CppTypeKeywords.contains(word) || !isKeyword(word))
typeGetted = true;
} else {
if (isKeyword(word))
currentArg += ' ' + word;
}
word = "";
}
break;
default:
if (isLetterChar(args[i]))
word+=args[i];
}
i++;
}
if (!typeGetted) {
currentArg += ' ' + word;
} else {
if (isKeyword(word)) {
currentArg += ' ' + word;
}
}
result += currentArg.trimmed();
return result;
}

View File

@ -16,64 +16,86 @@ public:
explicit CppParser(QObject *parent = nullptr);
void parseHardDefines();
function FindFileIncludes(const Filename: AnsiString; DeleteIt: boolean = False): PFileIncludes;
procedure AddHardDefineByLine(const Line: AnsiString);
procedure InvalidateFile(const FileName: AnsiString);
procedure GetFileDirectIncludes(const Filename: AnsiString; var List: TStringList);
procedure GetFileIncludes(const Filename: AnsiString; var List: TStringList);
procedure GetFileUsings(const Filename: AnsiString; var List: TDevStringList);
PFileIncludes findFileIncludes(const QString &filename, bool deleteIt = false);
void addHardDefineByLine(const QString& line);
void invalidateFile(const QString& fileName);
QStringList getFileDirectIncludes(const QString& filename) const;
const QList<QString>& getFileIncludes(const QString& filename) const;
const QSet<QString>& getFileUsings(const QString& filename) &;
function IsSystemHeaderFile(const FileName: AnsiString): boolean;
function IsProjectHeaderFile(const FileName: AnsiString): boolean;
procedure GetSourcePair(const FName: AnsiString; var CFile, HFile: AnsiString);
procedure GetClassesList(var List: TStringList);
function SuggestMemberInsertionLine(ParentStatement: PStatement; Scope: TStatementClassScope; var AddScopeStr:
boolean):
integer;
{
function GetSystemHeaderFileName(const FileName: AnsiString): AnsiString; // <file.h>
function GetProjectHeaderFileName(const FileName: AnsiString): AnsiString; // <file.h>
function GetLocalHeaderFileName(const RelativeTo, FileName: AnsiString): AnsiString; // "file.h"
}
function GetHeaderFileName(const RelativeTo, Line: AnsiString): AnsiString; // both
function IsIncludeLine(const Line: AnsiString): boolean;
constructor Create(wnd:HWND);
destructor Destroy; override;
procedure ParseFileList(UpdateView:boolean = True);
procedure ParseFile(const FileName: AnsiString; InProject: boolean; OnlyIfNotParsed: boolean = False; UpdateView:
boolean = True);
function StatementKindStr(Value: TStatementKind): AnsiString;
function StatementClassScopeStr(Value: TStatementClassScope): AnsiString;
procedure Reset;
procedure ClearIncludePaths;
procedure ClearProjectIncludePaths;
procedure ClearProjectFiles;
procedure AddIncludePath(const Value: AnsiString);
procedure AddProjectIncludePath(const Value: AnsiString);
procedure AddFileToScan(Value: AnsiString; InProject: boolean = False);
function PrettyPrintStatement(Statement: PStatement; line:integer = -1): AnsiString;
procedure FillListOfFunctions(const FileName, Phrase: AnsiString; const Line: integer; List: TStringList);
function FindAndScanBlockAt(const Filename: AnsiString; Line: integer): PStatement;
function FindStatementOf(FileName, Phrase: AnsiString; Line: integer): PStatement; overload;
function FindStatementOf(FileName, Phrase: AnsiString; CurrentClass: PStatement;
var CurrentClassType: PStatement ; force:boolean = False): PStatement; overload;
function FindStatementOf(FileName, Phrase: AnsiString; CurrentClass: PStatement; force:boolean = False): PStatement; overload;
bool isSystemHeaderFile(const QString& fileName);
bool isProjectHeaderFile(const QString& fileName);
void getSourcePair(const QString& fName, QString& CFile, QString& HFile);
void getClassesList(QStringList& list);
int suggestMemberInsertionLine(PStatement parentStatement,
StatementClassScope Scope,
bool addScopeStr);
// {
// function GetSystemHeaderFileName(const FileName: AnsiString): AnsiString; // <file.h>
// function GetProjectHeaderFileName(const FileName: AnsiString): AnsiString; // <file.h>
// function GetLocalHeaderFileName(const RelativeTo, FileName: AnsiString): AnsiString; // "file.h"
// }
QString getHeaderFileName(const QString& relativeTo, const QString& line) const;// both
bool isIncludeLine(const QString &line);
void parseFileList(bool updateView = true);
void parseFile(const QString& fileName, bool inProject,
bool onlyIfNotParsed = false, bool updateView = true);
QString statementKindStr(StatementKind value);
QString statementClassScopeStr(StatementClassScope value);
void reset();
void clearIncludePaths();
void clearProjectIncludePaths();
void clearProjectFiles();
void addIncludePath(const QString& value);
void addProjectIncludePath(const QString& value);
void addFileToScan(const QString& value, bool inProject = false);
QString prettyPrintStatement(PStatement statement, int line = -1);
void fillListOfFunctions(const QString& fileName,
const QString& phrase,
int line,
QStringList& list);
PStatement findAndScanBlockAt(const QString& filename, int line);
PStatement findStatementOf(const QString& fileName,
const QString& phrase,
int line);
PStatement findStatementOf(const QString& fileName,
const QString& phrase,
PStatement currentClass,
PStatement& currentClassType,
bool force = false);
PStatement findStatementOf(const QString& fileName,
const QString& phrase,
PStatement currentClass,
bool force = false);
function FindKindOfStatementOf(FileName, Phrase: AnsiString; Line: integer): TStatementKind;
function GetHintFromStatement(FileName, Phrase: AnsiString; Line: integer):AnsiString;
{Find statement starting from startScope}
function FindStatementStartingFrom(const FileName, Phrase: AnsiString; startScope: PStatement; force:boolean = False): PStatement;
function FindTypeDefinitionOf(const FileName: AnsiString;const aType: AnsiString; CurrentClass: PStatement): PStatement;
function FindFirstTemplateParamOf(const FileName: AnsiString;const aPhrase: AnsiString; currentClass: PStatement): String;
function FindLastOperator(const Phrase: AnsiString): integer;
function FindNamespace(const name:AnsiString):TList; // return a list of PSTATEMENTS (of the namespace)
function Freeze:boolean; overload; // Freeze/Lock (stop reparse while searching)
function Freeze(serialId:String):boolean; overload; // Freeze/Lock (stop reparse while searching)
procedure UnFreeze; // UnFree/UnLock (reparse while searching)
function GetParsing: boolean;
StatementKind findKindOfStatementOf(const QString& fileName,
const QString& phrase,
int line);
QString getHintFromStatement(const QString& fileName,
const QString& phrase,
int line);
//{Find statement starting from startScope}
PStatement findStatementStartingFrom(const QString& fileName,
const QString& phrase,
PStatement startScope,
bool force = false);
PStatement findTypeDefinitionOf(const QString& fileName,
const QString& phrase,
PStatement currentClass);
QString FindFirstTemplateParamOf(const QString& fileName,
const QString& phrase,
PStatement currentClass);
int findLastOperator(const QString& phrase) const;
QList<PStatement> findNamespace(const QString& name); // return a list of PSTATEMENTS (of the namespace)
bool freeze(); // Freeze/Lock (stop reparse while searching)
bool freeze(const QString& serialId); // Freeze/Lock (stop reparse while searching)
void unFreeze(); // UnFree/UnLock (reparse while searching)
bool getParsing();
function FindFunctionDoc(const FileName:AnsiString; const Line: integer;
params:TStringList; var isVoid:boolean): AnsiString;
QString findFunctionDoc(const QString& fileName,
int line,
QStringList& params,
bool &isVoid);
signals:
private:
PStatement addInheritedStatement(
@ -83,7 +105,7 @@ private:
PStatement addChildStatement(
// support for multiple parents (only typedef struct/union use multiple parents)
PStatement Parent,
PStatement parent,
const QString& fileName,
const QString& hintText,
const QString& aType, // "Type" is already in use
@ -109,7 +131,7 @@ private:
StatementScope scope,
StatementClassScope classScope,
bool isDefinition,
const QStringList& InheritanceList,
const QList<std::weak_ptr<Statement>>& inheritanceList,
bool isStatic);
void setInheritance(int index, PStatement classStatement, bool isStruct);
PStatement getCurrentScope(); // gets last item from last level
@ -119,22 +141,21 @@ private:
void checkForSkipStatement();
int skipBraces(int startAt);
int skipBracket(int startAt);
bool checkForPreprocessor();
bool checkForCatchBlock();
bool checkForEnum();
bool checkForForBlock();
bool checkForKeyword();
bool checkForMethod(QString &sType, QString &sName, QString &sArgs,
bool &isStatic, bool &isFriend); // caching of results
bool checkForNamespace();
bool checkForPreprocessor();
bool checkForUsing();
bool checkForScope();
bool CheckForStructs();
bool checkForTypedef();
bool checkForTypedefEnum();
bool checkForTypedefStruct();
bool CheckForStructs();
bool checkForMethod(QString &sType, QString &sName, QString &sArgs,
bool &isStatic, bool &isFriend); // caching of results
bool checkForScope();
bool checkForVar();
bool checkForEnum();
bool checkForForBlock();
bool checkForCatchBlock();
StatementScope getScope();
int getCurrentBlockEndSkip();
int getCurrentBlockBeginSkip();
@ -204,6 +225,8 @@ private:
QString getStatementKey(const QString& sName,
const QString& sType,
const QString& sNoNameArgs);
QString removeArgNames(const QString& args);
void onProgress(const QString& fileName, int total, int current);
void onBusy();
void onStartParsing();

View File

@ -57,6 +57,21 @@ void CppTokenizer::dumpTokens(const QString &fileName)
}
}
const CppTokenizer::TokenList &CppTokenizer::tokens()
{
return mTokenList;
}
CppTokenizer::PToken CppTokenizer::operator[](int i)
{
return mTokenList[i];
}
int CppTokenizer::tokenCount()
{
return mTokenList.count();
}
void CppTokenizer::addToken(const QString &sText, int iLine)
{
PToken token = std::make_shared<Token>();

View File

@ -18,6 +18,9 @@ public:
void reset();
void tokenize(const QStringList& buffer);
void dumpTokens(const QString& fileName);
const TokenList& tokens();
PToken operator[](int i);
int tokenCount();
signals:
private:
void addToken(const QString& sText, int iLine);

View File

@ -90,7 +90,7 @@ struct Statement;
using PStatement = std::shared_ptr<Statement>;
using StatementList = QList<PStatement>;
using PStatementList = std::shared_ptr<StatementList>;
using StatementMap = QMultiHash<QString, PStatement>;
using StatementMap = QMultiMap<QString, PStatement>;
struct Statement {
std::weak_ptr<Statement> parentScope; // parent class/struct/namespace scope, don't use auto pointer to prevent circular reference
QString hintText; // text to force display when using PrettyPrintStatement
@ -133,7 +133,7 @@ struct UsingNamespace {
using PUsingNamespace = std::shared_ptr<UsingNamespace>;
struct IncompleteClass {
std::weak_ptr<Statement> statement;
PStatement statement;
int count;
};
using PIncompleteClass = std::shared_ptr<IncompleteClass>;
@ -142,10 +142,9 @@ struct FileIncludes {
QString baseFile;
QMap<QString,bool> includeFiles; // true means the file is directly included, false means included indirectly
QSet<QString> usings; // namespaces it usings
QVector<std::weak_ptr<Statement>> statements; // but we don't save temporary statements
//StatementsIndex: TDevStringHash;
QVector<std::weak_ptr<Statement>> declaredStatements; // statements declared in this file
QMap<int, std::weak_ptr<Statement>> scopes; // int is start line of the statement scope
StatementMap statements; // but we don't save temporary statements
StatementMap declaredStatements; // statements declared in this file
QMap<int, PStatement> scopes; // int is start line of the statement scope
QSet<QString> dependingFiles; // The files I depeneds on
QSet<QString> dependedFiles; // the files depends on me
};