Skip to content

Commit b41401a

Browse files
committed
Refactoring: modernizing fortrancode.l
1 parent dc58153 commit b41401a

File tree

1 file changed

+41
-57
lines changed

1 file changed

+41
-57
lines changed

src/fortrancode.l

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,13 @@
4242
#include <stdio.h>
4343
#include <assert.h>
4444
#include <ctype.h>
45-
#include <qregexp.h>
46-
#include <qdir.h>
47-
#include <qcstringlist.h>
48-
#include "entry.h"
45+
4946
#include "doxygen.h"
5047
#include "message.h"
5148
#include "outputlist.h"
5249
#include "util.h"
5350
#include "membername.h"
54-
#include "searchindex.h"
5551
#include "defargs.h"
56-
#include "memberlist.h"
5752
#include "config.h"
5853
#include "groupdef.h"
5954
#include "classlist.h"
@@ -99,17 +94,15 @@ class UseEntry
9994
{
10095
public:
10196
QCString module; // just for debug
102-
QCStringList onlyNames; /* entries of the ONLY-part */
97+
std::vector<QCString> onlyNames; /* entries of the ONLY-part */
10398
};
10499

105100
/**
106101
module name -> list of ONLY/remote entries
107102
(module name = name of the module, which can be accessed via use-directive)
108103
*/
109-
class UseSDict : public SDict<UseEntry>
104+
class UseMap : public std::map<std::string,UseEntry>
110105
{
111-
public:
112-
UseSDict() : SDict<UseEntry>(17) {}
113106
};
114107

115108
/**
@@ -118,7 +111,7 @@ class UseSDict : public SDict<UseEntry>
118111
class Scope
119112
{
120113
public:
121-
QCStringList useNames; //!< contains names of used modules
114+
std::vector<QCString> useNames; //!< contains names of used modules
122115
StringUnorderedSet localVars; //!< contains names of local variables
123116
StringUnorderedSet externalVars; //!< contains names of external entities
124117
};
@@ -132,9 +125,9 @@ struct fortrancodeYY_state
132125
{
133126
QCString docBlock; //!< contents of all lines of a documentation block
134127
QCString currentModule=0; //!< name of the current enclosing module
135-
UseSDict * useMembers= 0; //!< info about used modules
136-
UseEntry * useEntry = 0; //!< current use statement info
137-
QList<Scope> scopeStack;
128+
UseMap useMembers; //!< info about used modules
129+
UseEntry useEntry; //!< current use statement info
130+
std::vector<Scope> scopeStack;
138131
bool isExternal = false;
139132
QCString str=""; //!> contents of fortran string
140133

@@ -181,7 +174,7 @@ static const char *stateToString(int state);
181174
static bool getFortranNamespaceDefs(const QCString &mname,
182175
NamespaceDef *&cd);
183176
static bool getFortranTypeDefs(const QCString &tname, const QCString &moduleName,
184-
ClassDef *&cd, UseSDict *usedict=0);
177+
ClassDef *&cd, const UseMap &useMap);
185178

186179
//----------------------------------------------------------------------------
187180

@@ -198,7 +191,7 @@ static void writeMultiLineCodeLink(yyscan_t yyscanner,CodeOutputInterface &ol,
198191
static bool getGenericProcedureLink(yyscan_t yyscanner,const ClassDef *cd,
199192
const char *memberText,
200193
CodeOutputInterface &ol);
201-
static bool getLink(yyscan_t yyscanner,UseSDict *usedict, // dictionary with used modules
194+
static bool getLink(yyscan_t yyscanner,const UseMap &useMap, // map with used modules
202195
const char *memberText, // exact member text
203196
CodeOutputInterface &ol,
204197
const char *text);
@@ -209,7 +202,7 @@ static void endScope(yyscan_t yyscanner);
209202
static void addUse(yyscan_t yyscanner,const QCString &moduleName);
210203
static void addLocalVar(yyscan_t yyscanner,const QCString &varName);
211204
static MemberDef *getFortranDefs(yyscan_t yyscanner,const QCString &memberName, const QCString &moduleName,
212-
UseSDict *usedict=0);
205+
const UseMap &useMap);
213206
static yy_size_t yyread(yyscan_t yyscanner,char *buf,yy_size_t max_size);
214207

215208

@@ -356,12 +349,9 @@ LANGUAGE_BIND_SPEC BIND{BS}"("{BS}C{BS}(,{BS}NAME{BS}"="{BS}"\""(.*)"\""{BS})?")
356349
yyextra->insideBody=FALSE;
357350

358351
/* append module name to use dict */
359-
yyextra->useEntry = new UseEntry();
360-
//yyextra->useEntry->module = yytext;
361-
//yyextra->useMembers->append(yytext, yyextra->useEntry);
362-
//addUse(yytext);
363-
yyextra->useEntry->module = tmp;
364-
yyextra->useMembers->append(tmp, yyextra->useEntry);
352+
yyextra->useEntry = UseEntry();
353+
yyextra->useEntry.module = tmp;
354+
yyextra->useMembers.insert(std::make_pair(tmp.str(), yyextra->useEntry));
365355
addUse(yyscanner,tmp);
366356
}
367357
<Use,UseOnly,Import>{BS},{BS} { codifyLines(yyscanner,yytext); }
@@ -371,7 +361,7 @@ LANGUAGE_BIND_SPEC BIND{BS}"("{BS}C{BS}(,{BS}NAME{BS}"="{BS}"\""(.*)"\""{BS})?")
371361
<UseOnly>{ID} {
372362
QCString tmp = yytext;
373363
tmp = tmp.lower();
374-
yyextra->useEntry->onlyNames.append(tmp);
364+
yyextra->useEntry.onlyNames.push_back(tmp);
375365
yyextra->insideBody=TRUE;
376366
generateLink(yyscanner,*yyextra->code, yytext);
377367
yyextra->insideBody=FALSE;
@@ -1081,7 +1071,7 @@ static bool getFortranNamespaceDefs(const QCString &mname,
10811071
@returns true, if type is found
10821072
*/
10831073
static bool getFortranTypeDefs(const QCString &tname, const QCString &moduleName,
1084-
ClassDef *&cd, UseSDict *usedict)
1074+
ClassDef *&cd, const UseMap &useMap)
10851075
{
10861076
if (tname.isEmpty()) return FALSE; /* empty name => nothing to link */
10871077

@@ -1100,10 +1090,9 @@ static bool getFortranTypeDefs(const QCString &tname, const QCString &moduleName
11001090
}
11011091
else
11021092
{
1103-
UseEntry *use;
1104-
for (UseSDict::Iterator di(*usedict); (use=di.current()); ++di)
1093+
for (const auto &kv : useMap)
11051094
{
1106-
if ((cd= Doxygen::classLinkedMap->find(use->module+"::"+tname)))
1095+
if ((cd= Doxygen::classLinkedMap->find(kv.second.module+"::"+tname)))
11071096
{
11081097
//cout << "=== type found in used module" << endl;
11091098
return TRUE;
@@ -1122,19 +1111,18 @@ static bool getFortranTypeDefs(const QCString &tname, const QCString &moduleName
11221111
@returns MemberDef pointer, if found, or nullptr otherwise
11231112
*/
11241113
static MemberDef *getFortranDefs(yyscan_t yyscanner,const QCString &memberName, const QCString &moduleName,
1125-
UseSDict *usedict)
1114+
const UseMap &useMap)
11261115
{
11271116
struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
11281117
if (memberName.isEmpty()) return nullptr; /* empty name => nothing to link */
11291118

11301119
// look in local variables
1131-
QListIterator<Scope> it(yyextra->scopeStack);
1132-
Scope *scope;
1133-
for (it.toLast();(scope=it.current());--it)
1120+
for (auto it = yyextra->scopeStack.rbegin(); it!=yyextra->scopeStack.rend(); ++it)
11341121
{
1122+
const Scope &scope = *it;
11351123
std::string lowMemName = memberName.lower().str();
1136-
if (scope->localVars.find(lowMemName)!=std::end(scope->localVars) && // local var
1137-
scope->externalVars.find(lowMemName)==std::end(scope->externalVars)) // and not external
1124+
if (scope.localVars .find(lowMemName)!=std::end(scope.localVars) && // local var
1125+
scope.externalVars.find(lowMemName)==std::end(scope.externalVars)) // and not external
11381126
{
11391127
return nullptr;
11401128
}
@@ -1177,22 +1165,22 @@ static MemberDef *getFortranDefs(yyscan_t yyscanner,const QCString &memberName,
11771165
else
11781166
{ // else search in used modules
11791167
QCString usedModuleName= nspace->name();
1180-
UseEntry *ue= usedict->find(usedModuleName);
1181-
if (ue)
1168+
auto use_it = useMap.find(usedModuleName.str());
1169+
if (use_it!=useMap.end())
11821170
{
1171+
const UseEntry &ue = use_it->second;
11831172
// check if only-list exists and if current entry exists is this list
1184-
QCStringList &only= ue->onlyNames;
1185-
if (only.isEmpty())
1173+
if (ue.onlyNames.empty())
11861174
{
11871175
//cout << " found in module " << usedModuleName << " entry " << memberName << endl;
11881176
return md.get(); // whole module used
11891177
}
11901178
else
11911179
{
1192-
for ( QCStringList::Iterator lit = only.begin(); lit != only.end(); ++lit)
1180+
for ( const auto &name : ue.onlyNames)
11931181
{
11941182
//cout << " search in only: " << usedModuleName << ":: " << memberName << "==" << (*it)<< endl;
1195-
if (memberName == *lit)
1183+
if (memberName == name)
11961184
{
11971185
return md.get(); // found in ONLY-part of use list
11981186
}
@@ -1220,7 +1208,7 @@ static bool getGenericProcedureLink(yyscan_t yyscanner,const ClassDef *cd,
12201208
return FALSE;
12211209
}
12221210

1223-
static bool getLink(yyscan_t yyscanner,UseSDict *usedict, // dictionary with used modules
1211+
static bool getLink(yyscan_t yyscanner,const UseMap &useMap, // dictionary with used modules
12241212
const char *memberText, // exact member text
12251213
CodeOutputInterface &ol,
12261214
const char *text)
@@ -1229,7 +1217,7 @@ static bool getLink(yyscan_t yyscanner,UseSDict *usedict, // dictionary with use
12291217
MemberDef *md=0;
12301218
QCString memberName= removeRedundantWhiteSpace(memberText);
12311219

1232-
if ((md=getFortranDefs(yyscanner,memberName, yyextra->currentModule, usedict)) && md->isLinkable())
1220+
if ((md=getFortranDefs(yyscanner,memberName, yyextra->currentModule, useMap)) && md->isLinkable())
12331221
{
12341222
if (md->isVariable() && (md->getLanguage()!=SrcLangExt_Fortran)) return FALSE; // Non Fortran variables aren't handled yet,
12351223
// see also linkifyText in util.cpp
@@ -1324,45 +1312,43 @@ static void startScope(yyscan_t yyscanner)
13241312
{
13251313
struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
13261314
DBG_CTX((stderr, "===> startScope %s",yytext));
1327-
Scope *scope = new Scope;
1328-
yyextra->scopeStack.append(scope);
1315+
yyextra->scopeStack.push_back(Scope());
13291316
}
13301317

13311318
/** end scope */
13321319
static void endScope(yyscan_t yyscanner)
13331320
{
13341321
struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
13351322
DBG_CTX((stderr,"===> endScope %s",yytext));
1336-
if (yyextra->scopeStack.isEmpty())
1323+
if (yyextra->scopeStack.empty())
13371324
{
13381325
DBG_CTX((stderr,"WARNING: fortrancode.l: stack empty!\n"));
13391326
return;
13401327
}
13411328

1342-
Scope *scope = yyextra->scopeStack.getLast();
1343-
yyextra->scopeStack.removeLast();
1344-
for ( QCStringList::Iterator it = scope->useNames.begin(); it != scope->useNames.end(); ++it)
1329+
Scope &scope = yyextra->scopeStack.back();
1330+
for ( const auto &name : scope.useNames)
13451331
{
1346-
yyextra->useMembers->remove(*it);
1332+
yyextra->useMembers.erase(name.str());
13471333
}
1348-
delete scope;
1334+
yyextra->scopeStack.pop_back();
13491335
}
13501336

13511337
static void addUse(yyscan_t yyscanner,const QCString &moduleName)
13521338
{
13531339
struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1354-
if (!yyextra->scopeStack.isEmpty())
1355-
yyextra->scopeStack.getLast()->useNames.append(moduleName);
1340+
if (!yyextra->scopeStack.empty())
1341+
yyextra->scopeStack.back().useNames.push_back(moduleName);
13561342
}
13571343

13581344
static void addLocalVar(yyscan_t yyscanner,const QCString &varName)
13591345
{
13601346
struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1361-
if (!yyextra->scopeStack.isEmpty())
1347+
if (!yyextra->scopeStack.empty())
13621348
{
13631349
std::string lowVarName = varName.lower().str();
1364-
yyextra->scopeStack.getLast()->localVars.insert(lowVarName);
1365-
if (yyextra->isExternal) yyextra->scopeStack.getLast()->externalVars.insert(lowVarName);
1350+
yyextra->scopeStack.back().localVars.insert(lowVarName);
1351+
if (yyextra->isExternal) yyextra->scopeStack.back().externalVars.insert(lowVarName);
13661352
}
13671353
}
13681354

@@ -1418,12 +1404,10 @@ FortranCodeParser::FortranCodeParser(FortranFormat format) : p(std::make_unique<
14181404
fortrancodeYYset_debug(1,p->yyscanner);
14191405
#endif
14201406
resetCodeParserState();
1421-
p->state.useMembers = new UseSDict;
14221407
}
14231408

14241409
FortranCodeParser::~FortranCodeParser()
14251410
{
1426-
delete p->state.useMembers;
14271411
fortrancodeYYlex_destroy(p->yyscanner);
14281412
}
14291413

0 commit comments

Comments
 (0)