clean up code

This commit is contained in:
Roy Qu 2022-11-16 10:29:20 +08:00
parent 7ab444fc06
commit a9c266c39a
7 changed files with 137 additions and 124 deletions

View File

@ -298,7 +298,7 @@ QPixmap IconsManager::getPixmapForStatement(PStatement statement)
case StatementKind::skDestructor:
if (statement->scope == StatementScope::Global)
return *(pIconsManager->getPixmap(IconsManager::PARSER_GLOBAL_METHOD));
if (statement->isInherited) {
if (statement->isInherited()) {
if (statement->classScope == StatementClassScope::Protected) {
return *(pIconsManager->getPixmap(IconsManager::PARSER_INHERITED_PROTECTED_METHOD));
} else if (statement->classScope == StatementClassScope::Public) {
@ -319,7 +319,7 @@ QPixmap IconsManager::getPixmapForStatement(PStatement statement)
case StatementKind::skLocalVariable:
return *(pIconsManager->getPixmap(IconsManager::PARSER_LOCAL_VAR));
case StatementKind::skVariable:
if (statement->isInherited) {
if (statement->isInherited()) {
if (statement->classScope == StatementClassScope::Protected) {
return *(pIconsManager->getPixmap(IconsManager::PARSER_INHERITED_PROTECTD_VAR));
} else if (statement->classScope == StatementClassScope::Public) {

View File

@ -927,8 +927,7 @@ void CppParser::parseHardDefines()
StatementKind::skPreprocessor,
StatementScope::Global,
StatementClassScope::None,
true,
false);
StatementProperty::spHasDefinition);
}
}
}
@ -1154,9 +1153,7 @@ PStatement CppParser::addInheritedStatement(const PStatement& derived, const PSt
inherit->kind,
inherit->scope,
access,
true,
inherit->isStatic);
statement->isInherited = true;
inherit->properties & StatementProperty::spInherited);
return statement;
}
@ -1166,7 +1163,7 @@ PStatement CppParser::addChildStatement(const PStatement& parent, const QString
const QString& noNameArgs,
const QString &value, int line, StatementKind kind,
const StatementScope& scope, const StatementClassScope& classScope,
bool isDefinition, bool isStatic)
StatementProperties properties)
{
return addStatement(
parent,
@ -1180,8 +1177,7 @@ PStatement CppParser::addChildStatement(const PStatement& parent, const QString
kind,
scope,
classScope,
isDefinition,
isStatic);
properties);
}
PStatement CppParser::addStatement(const PStatement& parent,
@ -1193,7 +1189,8 @@ PStatement CppParser::addStatement(const PStatement& parent,
const QString &value,
int line, StatementKind kind,
const StatementScope& scope,
const StatementClassScope& classScope, bool isDefinition, bool isStatic)
const StatementClassScope& classScope,
StatementProperties properties)
{
// Move '*', '&' to type rather than cmd (it's in the way for code-completion)
QString newType = aType;
@ -1212,10 +1209,10 @@ PStatement CppParser::addStatement(const PStatement& parent,
|| kind == StatementKind::skVariable
) {
//find
if (isDefinition) {
if (properties.testFlag(StatementProperty::spHasDefinition)) {
PStatement oldStatement = findStatementInScope(newCommand,noNameArgs,kind,parent);
if (oldStatement && !oldStatement->hasDefinition) {
oldStatement->hasDefinition = true;
if (oldStatement && !oldStatement->hasDefinition()) {
oldStatement->setHasDefinition(true);
if (oldStatement->fileName!=fileName) {
PFileIncludes fileIncludes=mPreprocessor.includesList().value(fileName);
if (fileIncludes) {
@ -1244,20 +1241,20 @@ PStatement CppParser::addStatement(const PStatement& parent,
result->kind = kind;
result->scope = scope;
result->classScope = classScope;
result->hasDefinition = isDefinition;
result->properties = properties;
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;
if (!fileName.isEmpty()) {
result->setInProject(mIsProjectFile);
result->setInSystemHeader(mIsSystemHeader);
} else {
result->setInProject(false);
result->setInSystemHeader(true);
}
//result->children;
//result->friends;
result->isStatic = isStatic;
result->isInherited = false;
if (scope == StatementScope::Local)
result->fullName = newCommand;
else
@ -1282,7 +1279,15 @@ PStatement CppParser::addStatement(const PStatement& parent,
return result;
}
PStatement CppParser::addStatement(const PStatement &parent, const QString &fileName, const QString &aType, const QString &command, int argStart, int argEnd, const QString &value, int line, StatementKind kind, const StatementScope &scope, const StatementClassScope &classScope, bool isDefinition, bool isStatic)
PStatement CppParser::addStatement(const PStatement &parent,
const QString &fileName,
const QString &aType,
const QString &command,
int argStart, int argEnd,
const QString &value, int line,
StatementKind kind, const StatementScope &scope,
const StatementClassScope &classScope,
StatementProperties properties)
{
Q_ASSERT(mTokenizer[argStart]->text=='(');
QString args;
@ -1354,8 +1359,7 @@ PStatement CppParser::addStatement(const PStatement &parent, const QString &file
kind,
scope,
classScope,
isDefinition,
isStatic);
properties);
}
void CppParser::setInheritance(int index, const PStatement& classStatement, bool isStruct)
@ -2188,8 +2192,7 @@ void CppParser::handleCatchBlock()
StatementKind::skBlock,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
addSoloScopeLevel(block,startLine);
scanMethodArgs(block,mIndex);
mIndex=mTokenizer[mIndex]->matchIndex+1;
@ -2256,8 +2259,7 @@ void CppParser::handleEnum(bool isTypedef)
StatementKind::skEnumClassType,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
} else {
enumStatement=addStatement(
getCurrentScope(),
@ -2271,8 +2273,7 @@ void CppParser::handleEnum(bool isTypedef)
StatementKind::skEnumType,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
if (isAdhocVar) {
//Ad-hoc var definition
@ -2298,8 +2299,7 @@ void CppParser::handleEnum(bool isTypedef)
StatementKind::skVariable,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
} else if (name!=',') {
break;
@ -2339,8 +2339,7 @@ void CppParser::handleEnum(bool isTypedef)
StatementKind::skEnum,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
} else {
if (enumStatement) {
@ -2356,8 +2355,7 @@ void CppParser::handleEnum(bool isTypedef)
StatementKind::skEnum,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
addStatement(
getCurrentScope(),
@ -2371,8 +2369,7 @@ void CppParser::handleEnum(bool isTypedef)
StatementKind::skEnum,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
}
mIndex ++ ;
@ -2418,8 +2415,7 @@ void CppParser::handleForBlock()
StatementKind::skBlock,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
addSoloScopeLevel(block,startLine);
}
@ -2486,8 +2482,7 @@ void CppParser::handleLambda(int index, int endIndex)
StatementKind::skBlock,
StatementScope::Local,
StatementClassScope::None,
true,
false);
StatementProperty::spHasDefinition);
scanMethodArgs(lambdaBlock,argStart);
addSoloScopeLevel(lambdaBlock,mTokenizer[bodyStart]->line);
int i=bodyStart+1; // start after '{';
@ -2560,9 +2555,7 @@ void CppParser::handleLambda(int index, int endIndex)
StatementKind::skVariable,
getScope(),
mClassScope,
//True,
false,
false); // TODO: not supported to pass list
StatementProperty::spHasDefinition); // TODO: not supported to pass list
tempType="";
}
}
@ -2670,8 +2663,8 @@ void CppParser::handleMethod(StatementKind functionKind,const QString &sType, co
functionKind,
getScope(),
mClassScope,
true,
isStatic);
StatementProperty::spHasDefinition
| (isStatic?StatementProperty::spStatic:StatementProperty::spNone));
scanMethodArgs(functionStatement, argStart);
// add variable this to the class function
if (scopeStatement && scopeStatement->kind == StatementKind::skClass &&
@ -2689,8 +2682,7 @@ void CppParser::handleMethod(StatementKind functionKind,const QString &sType, co
StatementKind::skVariable,
StatementScope::Local,
StatementClassScope::None,
true,
false);
StatementProperty::spHasDefinition);
}
// add "__func__ variable"
@ -2706,8 +2698,7 @@ void CppParser::handleMethod(StatementKind functionKind,const QString &sType, co
StatementKind::skVariable,
StatementScope::Local,
StatementClassScope::None,
true,
false);
StatementProperty::spHasDefinition);
} else {
functionStatement = addStatement(
@ -2723,8 +2714,7 @@ void CppParser::handleMethod(StatementKind functionKind,const QString &sType, co
functionKind,
getScope(),
mClassScope,
false,
isStatic);
(isStatic?StatementProperty::spStatic:StatementProperty::spNone));
}
}
@ -2787,8 +2777,7 @@ void CppParser::handleNamespace(KeywordType skipType)
StatementKind::skNamespaceAlias,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
mIndex+=2; //skip ;
return;
} else if (isInline) {
@ -2816,8 +2805,7 @@ void CppParser::handleNamespace(KeywordType skipType)
StatementKind::skNamespace,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
// find next '{' or ';'
mIndex = indexOfNextSemicolonOrLeftBrace(mIndex);
@ -2906,8 +2894,7 @@ void CppParser::handleOtherTypedefs()
StatementKind::skTypedef,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
mIndex = mTokenizer[paramStart]->matchIndex+1;
} else if (mTokenizer[mIndex+1]->text.front() ==','
@ -2929,8 +2916,7 @@ void CppParser::handleOtherTypedefs()
StatementKind::skTypedef,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
newType = "";
mIndex++;
} else {
@ -2992,8 +2978,7 @@ void CppParser::handlePreprocessor()
StatementKind::skPreprocessor,
StatementScope::Global,
StatementClassScope::None,
true,
false);
StatementProperty::spHasDefinition);
} // TODO: undef ( define has limited scope)
handlePreprocessorEnd:
mIndex++;
@ -3082,8 +3067,7 @@ bool CppParser::handleStatement()
StatementKind::skBlock,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
addSoloScopeLevel(block,mTokenizer[mIndex]->line,true);
mIndex++;
} else if (mTokenizer[mIndex]->text[0] == '}') {
@ -3219,8 +3203,7 @@ void CppParser::handleStructs(bool isTypedef)
StatementKind::skTypedef,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
mIndex++;
if (mIndex >= mTokenizer.tokenCount())
@ -3279,8 +3262,7 @@ void CppParser::handleStructs(bool isTypedef)
StatementKind::skClass,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
command = "";
}
mIndex++;
@ -3304,8 +3286,7 @@ void CppParser::handleStructs(bool isTypedef)
StatementKind::skClass,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
command="";
}
mIndex+=2;
@ -3373,8 +3354,7 @@ void CppParser::handleStructs(bool isTypedef)
StatementKind::skClass,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
if (isTypedef) {
//typedef
@ -3390,8 +3370,7 @@ void CppParser::handleStructs(bool isTypedef)
StatementKind::skTypedef,
getScope(),
mClassScope,
true,
false); // typedef
StatementProperty::spHasDefinition); // typedef
} else {
//variable define
addStatement(
@ -3406,8 +3385,7 @@ void CppParser::handleStructs(bool isTypedef)
StatementKind::skVariable,
getScope(),
mClassScope,
true,
false); // TODO: not supported to pass list
StatementProperty::spHasDefinition); // TODO: not supported to pass list
}
}
command = "";
@ -3437,8 +3415,7 @@ void CppParser::handleStructs(bool isTypedef)
StatementKind::skBlock,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
addSoloScopeLevel(firstSynonym,startLine);
@ -3482,8 +3459,7 @@ void CppParser::handleUsing()
StatementKind::skTypedef,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
// skip ;
mIndex++;
return;
@ -3507,8 +3483,7 @@ void CppParser::handleUsing()
StatementKind::skAlias,
getScope(),
mClassScope,
true,
false);
StatementProperty::spHasDefinition);
}
//skip to ; and skip it
mIndex=indexOfNextSemicolon(mIndex)+1;
@ -3642,8 +3617,8 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
getScope(),
mClassScope,
//True,
!isExtern,
isStatic);
(isExtern?StatementProperty::spNone:StatementProperty::spHasDefinition)
| (isStatic?StatementProperty::spStatic:StatementProperty::spNone));
}
tempType="";
mIndex=indexOfNextSemicolonOrLeftBrace(argEnd+1);
@ -3673,8 +3648,8 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
getScope(),
mClassScope,
//True,
!isExtern,
isStatic);
(isExtern?StatementProperty::spNone:StatementProperty::spHasDefinition)
| (isStatic?StatementProperty::spStatic:StatementProperty::spNone));
tempType="";
}
}
@ -4707,7 +4682,7 @@ void CppParser::internalInvalidateFile(const QString &fileName)
if (statement->fileName==fileName) {
mStatementList.deleteStatement(statement);
} else {
statement->hasDefinition=false;
statement->setHasDefinition(false);
statement->definitionFileName = statement->fileName;
statement->definitionLine = statement->line;
}
@ -4797,8 +4772,7 @@ void CppParser::scanMethodArgs(const PStatement& functionStatement, int argStart
StatementKind::skParameter,
StatementScope::Local,
StatementClassScope::None,
true,
false);
StatementProperty::spHasDefinition);
}
i=argEnd+1;
varType="";
@ -4833,8 +4807,7 @@ void CppParser::scanMethodArgs(const PStatement& functionStatement, int argStart
StatementKind::skParameter,
StatementScope::Local,
StatementClassScope::None,
true,
false);
StatementProperty::spHasDefinition);
}
}
}

View File

@ -169,8 +169,7 @@ private:
StatementKind kind,
const StatementScope& scope,
const StatementClassScope& classScope,
bool isDefinition,
bool isStatic); // TODO: InheritanceList not supported
StatementProperties properties); // TODO: InheritanceList not supported
PStatement addStatement(
const PStatement& parent,
const QString &fileName,
@ -183,8 +182,7 @@ private:
StatementKind kind,
const StatementScope& scope,
const StatementClassScope& classScope,
bool isDefinition,
bool isStatic);
StatementProperties properties);
PStatement addStatement(
const PStatement& parent,
const QString &fileName,
@ -197,8 +195,7 @@ private:
StatementKind kind,
const StatementScope& scope,
const StatementClassScope& classScope,
bool isDefinition,
bool isStatic);
StatementProperties properties);
void setInheritance(int index, const PStatement& classStatement, bool isStruct);
bool isCurrentScope(const QString& command);
void addSoloScopeLevel(PStatement& statement, int line, bool shouldResetBlock=false); // adds new solo level

View File

@ -140,6 +140,24 @@ struct StatementMatchPosition{
int end;
};
enum StatementProperty {
spNone = 0x0,
spStatic = 0x0001,
spHasDefinition = 0x0002,
spInProject = 0x0004,
spInSystemHeader = 0x0008,
spInherited = 0x0010,
spVirtual = 0x0020,
spOverride = 0x0040,
spConstexpr = 0x0080
};
Q_DECLARE_FLAGS(StatementProperties, StatementProperty)
Q_DECLARE_OPERATORS_FOR_FLAGS(StatementProperties)
using PStatementMathPosition = std::shared_ptr<StatementMatchPosition>;
struct Statement;
@ -158,20 +176,16 @@ struct Statement {
StatementKind kind; // kind of statement class/variable/function/etc
StatementScope scope; // global/local/classlocal
StatementClassScope classScope; // protected/private/public
bool hasDefinition; // definiton line/filename is valid
int line; // declaration
int definitionLine; // definition
QString fileName; // declaration
QString definitionFileName; // definition
bool inProject; // statement in project
bool inSystemHeader; // statement in system header (#include <>)
StatementMap children; // functions can be overloaded,so we use list to save children with the same name
QSet<QString> friends; // friend class / functions
bool isStatic; // static function / variable
bool isInherited; // inherted member;
QString fullName; // fullname(including class and namespace), ClassA::foo
QSet<QString> usingList; // using namespaces
QString noNameArgs;// Args without name
StatementProperties properties;
// fields for code completion
int usageCount; //Usage Count
@ -180,6 +194,38 @@ struct Statement {
int firstMatchLength; // length of first match;
int caseMatched; // if match with case
QList<PStatementMathPosition> matchPositions;
// definiton line/filename is valid
bool hasDefinition() {
return properties.testFlag(StatementProperty::spHasDefinition);
};
void setHasDefinition(bool on) {
properties.setFlag(StatementProperty::spHasDefinition,on);
}
// statement in project
bool inProject() {
return properties.testFlag(StatementProperty::spInProject);
}
void setInProject(bool on) {
properties.setFlag(StatementProperty::spInProject, on);
}
// statement in system header (#include <>)
bool inSystemHeader() {
return properties.testFlag(StatementProperty::spInSystemHeader);
};
void setInSystemHeader(bool on) {
properties.setFlag(StatementProperty::spInSystemHeader, on);
}
bool isStatic() {
return properties.testFlag(StatementProperty::spStatic);
} // static function / variable
void setIsStatic(bool on) {
properties.setFlag(StatementProperty::spStatic, on);
}
bool isInherited() {
return properties.testFlag(StatementProperty::spInherited);
}; // inherted member;
};
struct EvalStatement;

View File

@ -328,7 +328,7 @@ void ClassBrowserModel::filterChildren(ClassBrowserNode *node, const StatementMa
{
for (PStatement statement:statements) {
if (mClassBrowserType==ProjectClassBrowserType::WholeProject
&& !statement->inProject)
&& !statement->inProject())
continue;
if (mProcessedStatements.contains(statement.get()))
@ -336,7 +336,7 @@ void ClassBrowserModel::filterChildren(ClassBrowserNode *node, const StatementMa
if (statement->kind == StatementKind::skBlock)
continue;
if (statement->isInherited && !pSettings->ui().classBrowserShowInherited())
if (statement->isInherited() && !pSettings->ui().classBrowserShowInherited())
continue;
if (statement == node->statement) // prevent infinite recursion
@ -400,10 +400,7 @@ PStatement ClassBrowserModel::createDummy(const PStatement& statement)
result->value = statement->value;
result->scope = statement->scope;
result->classScope = statement->classScope;
result->inProject = statement->inProject;
result->inSystemHeader = statement->inSystemHeader;
result->isStatic = statement->isStatic;
result->isInherited = statement->isInherited;
result->properties = statement->properties;
result->fileName= statement->fileName;
result->line = statement->line;
result->definitionFileName = statement->fileName;

View File

@ -212,7 +212,7 @@ void CodeCompletionPopup::addFunctionWithoutDefinitionChildren(PStatement scopeS
return;
for (const PStatement& childStatement: children) {
if (childStatement->inSystemHeader)
if (childStatement->inSystemHeader())
continue;
if (childStatement->fileName.isEmpty()) {
// hard defines, do nothing
@ -222,7 +222,7 @@ void CodeCompletionPopup::addFunctionWithoutDefinitionChildren(PStatement scopeS
case StatementKind::skConstructor:
case StatementKind::skFunction:
case StatementKind::skDestructor:
if (!childStatement->hasDefinition)
if (!childStatement->hasDefinition())
addStatement(childStatement,fileName,line);
break;
case StatementKind::skClass:
@ -306,17 +306,17 @@ static bool sortByScopeComparator(PStatement statement1,PStatement statement2){
if (statement2->kind != StatementKind::skKeyword) {
//s1 keyword / s2 system defines, s1 < s2, should return true
//s1 keyword / s2 not system defines, s2 < s1, should return false;
return statement2->inSystemHeader;
return statement2->inSystemHeader();
} else
return statement1->command < statement2->command;
} else if (statement2->kind == StatementKind::skKeyword) {
//s1 system defines / s2 keyword, s2 < s1, should return false;
//s1 not system defines / s2 keyword, s1 < s2, should return true;
return (!statement1->inSystemHeader);
return (!statement1->inSystemHeader());
}
// Show stuff from local headers first
if (statement1->inSystemHeader != statement2->inSystemHeader)
return !(statement1->inSystemHeader);
if (statement1->inSystemHeader() != statement2->inSystemHeader())
return !(statement1->inSystemHeader());
// Show local statements first
if (statement1->scope != StatementScope::Global
&& statement2->scope == StatementScope::Global ) {
@ -387,17 +387,17 @@ static bool sortByScopeWithUsageComparator(PStatement statement1,PStatement stat
if (statement2->kind != StatementKind::skKeyword) {
//s1 keyword / s2 system defines, s1 < s2, should return true
//s1 keyword / s2 not system defines, s2 < s1, should return false;
return statement2->inSystemHeader;
return statement2->inSystemHeader();
} else
return statement1->command < statement2->command;
} else if (statement2->kind == StatementKind::skKeyword) {
//s1 system defines / s2 keyword, s2 < s1, should return false;
//s1 not system defines / s2 keyword, s1 < s2, should return true;
return (!statement1->inSystemHeader);
return (!statement1->inSystemHeader());
}
// Show stuff from local headers first
if (statement1->inSystemHeader != statement2->inSystemHeader)
return !(statement1->inSystemHeader);
if (statement1->inSystemHeader() != statement2->inSystemHeader())
return !(statement1->inSystemHeader());
// Show local statements first
if (statement1->scope != StatementScope::Global
&& statement2->scope == StatementScope::Global ) {
@ -770,7 +770,7 @@ void CodeCompletionPopup::getCompletionFor(
mParser->statementList().childrenStatements(classTypeStatement);
foreach (const PStatement& childStatement, children) {
if (
(childStatement->isStatic)
(childStatement->isStatic())
|| (childStatement->kind == StatementKind::skTypedef
|| childStatement->kind == StatementKind::skClass
|| childStatement->kind == StatementKind::skEnum
@ -786,7 +786,7 @@ void CodeCompletionPopup::getCompletionFor(
mParser->statementList().childrenStatements(classTypeStatement);
foreach (const PStatement& childStatement,children) {
if (
(childStatement->isStatic)
(childStatement->isStatic())
|| (childStatement->kind == StatementKind::skTypedef
|| childStatement->kind == StatementKind::skClass
|| childStatement->kind == StatementKind::skEnum

View File

@ -132,7 +132,7 @@ void NewClassCandidatesModel::fillClasses()
return;
foreach( const PStatement& s, mParser->statementList().childrenStatements()) {
if (s->kind==StatementKind::skClass
&& s->inProject
&& s->inProject()
&& !s->command.startsWith("_")
&& !s->command.contains("<")
&& !mClassNames.contains(s->fullName)) {
@ -158,7 +158,7 @@ void NewClassCandidatesModel::fillClassesInNamespace(PStatement ns)
{
foreach( const PStatement& s, mParser->statementList().childrenStatements(ns)) {
if (s->kind==StatementKind::skClass
&& s->inProject
&& s->inProject()
&& !s->command.startsWith("_")
&& !s->command.contains("<")
&& !mClassNames.contains(s->fullName)) {