Skip to content

Commit 1e1b7f3

Browse files
committed
Replace reference members with pointers and use default operators where applicable
1 parent 5783afa commit 1e1b7f3

2 files changed

Lines changed: 53 additions & 87 deletions

File tree

simplecpp.cpp

Lines changed: 46 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -346,26 +346,26 @@ class StdIStream : public simplecpp::TokenList::Stream {
346346
public:
347347
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
348348
explicit StdIStream(std::istream &istr)
349-
: istr(istr) {
349+
: istr(&istr) {
350350
assert(istr.good());
351351
init();
352352
}
353353

354354
virtual int get() override {
355-
return istr.get();
355+
return istr->get();
356356
}
357357
virtual int peek() override {
358-
return istr.peek();
358+
return istr->peek();
359359
}
360360
virtual void unget() override {
361-
istr.unget();
361+
istr->unget();
362362
}
363363
virtual bool good() override {
364-
return istr.good();
364+
return istr->good();
365365
}
366366

367367
private:
368-
std::istream &istr;
368+
std::istream *istr;
369369
};
370370

371371
class StdCharBufStream : public simplecpp::TokenList::Stream {
@@ -449,39 +449,36 @@ class FileStream : public simplecpp::TokenList::Stream {
449449
ungetc(ch, file);
450450
}
451451

452-
FileStream(const FileStream&);
453-
FileStream &operator=(const FileStream&);
454-
455452
FILE *file;
456453
int lastCh;
457454
int lastStatus;
458455
};
459456

460-
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames) : frontToken(nullptr), backToken(nullptr), files(filenames) {}
457+
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames) : frontToken(nullptr), backToken(nullptr), files(&filenames) {}
461458

462459
simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
463-
: frontToken(nullptr), backToken(nullptr), files(filenames)
460+
: frontToken(nullptr), backToken(nullptr), files(&filenames)
464461
{
465462
StdIStream stream(istr);
466463
readfile(stream,filename,outputList);
467464
}
468465

469466
simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
470-
: frontToken(nullptr), backToken(nullptr), files(filenames)
467+
: frontToken(nullptr), backToken(nullptr), files(&filenames)
471468
{
472469
StdCharBufStream stream(data, size);
473470
readfile(stream,filename,outputList);
474471
}
475472

476473
simplecpp::TokenList::TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
477-
: frontToken(nullptr), backToken(nullptr), files(filenames)
474+
: frontToken(nullptr), backToken(nullptr), files(&filenames)
478475
{
479476
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(data), size);
480477
readfile(stream,filename,outputList);
481478
}
482479

483480
simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList)
484-
: frontToken(nullptr), backToken(nullptr), files(filenames)
481+
: frontToken(nullptr), backToken(nullptr), files(&filenames)
485482
{
486483
try {
487484
FileStream stream(filename, filenames);
@@ -561,7 +558,7 @@ void simplecpp::TokenList::dump() const
561558
std::string simplecpp::TokenList::stringify() const
562559
{
563560
std::ostringstream ret;
564-
Location loc(files);
561+
Location loc(*files);
565562
for (const Token *tok = cfront(); tok; tok = tok->next) {
566563
if (tok->location.line < loc.line || tok->location.fileIndex != loc.fileIndex) {
567564
ret << "\n#line " << tok->location.line << " \"" << tok->location.file() << "\"\n";
@@ -647,7 +644,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
647644

648645
const Token *oldLastToken = nullptr;
649646

650-
Location location(files);
647+
Location location(*files);
651648
location.fileIndex = fileIndex(filename);
652649
location.line = 1U;
653650
location.col = 1U;
@@ -658,7 +655,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
658655

659656
if (ch >= 0x80) {
660657
if (outputList) {
661-
simplecpp::Output err(files);
658+
simplecpp::Output err(*files);
662659
err.type = simplecpp::Output::UNHANDLED_CHAR_ERROR;
663660
err.location = location;
664661
std::ostringstream s;
@@ -673,7 +670,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
673670
if (ch == '\n') {
674671
if (cback() && cback()->op == '\\') {
675672
if (location.col > cback()->location.col + 1U)
676-
portabilityBackslash(outputList, files, cback()->location);
673+
portabilityBackslash(outputList, *files, cback()->location);
677674
++multiline;
678675
deleteToken(back());
679676
} else {
@@ -778,7 +775,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
778775
const TokenString check_portability = currentToken + tmp;
779776
const std::string::size_type pos = check_portability.find_last_not_of(" \t");
780777
if (pos < check_portability.size() - 1U && check_portability[pos] == '\\')
781-
portabilityBackslash(outputList, files, location);
778+
portabilityBackslash(outputList, *files, location);
782779
++multiline;
783780
tmp_ch = stream.readChar();
784781
currentToken += '\n';
@@ -838,7 +835,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
838835
}
839836
if (!stream.good() || ch == '\n') {
840837
if (outputList) {
841-
Output err(files);
838+
Output err(*files);
842839
err.type = Output::SYNTAX_ERROR;
843840
err.location = location;
844841
err.msg = "Invalid newline in raw string delimiter.";
@@ -851,7 +848,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
851848
currentToken += stream.readChar();
852849
if (!endsWith(currentToken, endOfRawString)) {
853850
if (outputList) {
854-
Output err(files);
851+
Output err(*files);
855852
err.type = Output::SYNTAX_ERROR;
856853
err.location = location;
857854
err.msg = "Raw string missing terminating delimiter.";
@@ -1393,7 +1390,7 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
13931390
if (!stream.good() || ch != end) {
13941391
clear();
13951392
if (outputList) {
1396-
Output err(files);
1393+
Output err(*files);
13971394
err.type = Output::SYNTAX_ERROR;
13981395
err.location = location;
13991396
err.msg = std::string("No pair for character (") + start + "). Can't process file. File is either invalid or unicode, which is currently not supported.";
@@ -1457,12 +1454,12 @@ bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
14571454

14581455
unsigned int simplecpp::TokenList::fileIndex(const std::string &filename)
14591456
{
1460-
for (unsigned int i = 0; i < files.size(); ++i) {
1461-
if (files[i] == filename)
1457+
for (unsigned int i = 0; i < files->size(); ++i) {
1458+
if ((*files)[i] == filename)
14621459
return i;
14631460
}
1464-
files.push_back(filename);
1465-
return files.size() - 1U;
1461+
files->push_back(filename);
1462+
return files->size() - 1U;
14661463
}
14671464

14681465

@@ -1472,9 +1469,9 @@ namespace simplecpp {
14721469

14731470
class Macro {
14741471
public:
1475-
explicit Macro(std::vector<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), optExpandValue(nullptr), optNoExpandValue(nullptr), valueDefinedInCode_(false) {}
1472+
explicit Macro(std::vector<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(&f), tokenListDefine(f), variadic(false), variadicOpt(false), optExpandValue(nullptr), optNoExpandValue(nullptr), valueDefinedInCode_(false) {}
14761473

1477-
Macro(const Token *tok, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(true) {
1474+
Macro(const Token *tok, std::vector<std::string> &f) : nameTokDef(nullptr), files(&f), tokenListDefine(f), valueDefinedInCode_(true) {
14781475
if (sameline(tok->previousSkipComments(), tok))
14791476
throw std::runtime_error("bad macro syntax");
14801477
if (tok->op != '#')
@@ -1490,38 +1487,19 @@ namespace simplecpp {
14901487
throw std::runtime_error("bad macro syntax");
14911488
}
14921489

1493-
Macro(const std::string &name, const std::string &value, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(false) {
1490+
Macro(const std::string &name, const std::string &value, std::vector<std::string> &f) : nameTokDef(nullptr), files(&f), tokenListDefine(f), valueDefinedInCode_(false) {
14941491
const std::string def(name + ' ' + value);
14951492
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(def.data()), def.size());
14961493
tokenListDefine.readfile(stream);
14971494
if (!parseDefine(tokenListDefine.cfront()))
14981495
throw std::runtime_error("bad macro syntax. macroname=" + name + " value=" + value);
14991496
}
15001497

1501-
Macro(const Macro &other) : nameTokDef(nullptr), files(other.files), tokenListDefine(other.files), valueDefinedInCode_(other.valueDefinedInCode_) {
1502-
*this = other;
1503-
}
1504-
15051498
~Macro() {
15061499
delete optExpandValue;
15071500
delete optNoExpandValue;
15081501
}
15091502

1510-
Macro &operator=(const Macro &other) {
1511-
if (this != &other) {
1512-
files = other.files;
1513-
valueDefinedInCode_ = other.valueDefinedInCode_;
1514-
if (other.tokenListDefine.empty())
1515-
parseDefine(other.nameTokDef);
1516-
else {
1517-
tokenListDefine = other.tokenListDefine;
1518-
parseDefine(tokenListDefine.cfront());
1519-
}
1520-
usageList = other.usageList;
1521-
}
1522-
return *this;
1523-
}
1524-
15251503
bool valueDefinedInCode() const {
15261504
return valueDefinedInCode_;
15271505
}
@@ -1746,8 +1724,8 @@ namespace simplecpp {
17461724
}
17471725

17481726
if (variadicOpt) {
1749-
TokenList expandValue(files);
1750-
TokenList noExpandValue(files);
1727+
TokenList expandValue(*files);
1728+
TokenList noExpandValue(*files);
17511729
for (const Token *tok = valueToken; tok && tok != endToken;) {
17521730
if (tok->str() == "__VA_OPT__") {
17531731
if (!sameline(tok, tok->next) || tok->next->op != '(')
@@ -1901,7 +1879,7 @@ namespace simplecpp {
19011879
}
19021880

19031881
// If macro call uses __COUNTER__ then expand that first
1904-
TokenList tokensparams(files);
1882+
TokenList tokensparams(*files);
19051883
std::vector<const Token *> parametertokens2;
19061884
if (!parametertokens1.empty()) {
19071885
bool counter = false;
@@ -1965,7 +1943,7 @@ namespace simplecpp {
19651943
output->deleteToken(comma);
19661944
continue;
19671945
}
1968-
TokenList new_output(files);
1946+
TokenList new_output(*files);
19691947
if (!expandArg(&new_output, tok, parametertokens2))
19701948
output->push_back(newMacroToken(tok->str(), loc, isReplaced(expandedmacros), tok));
19711949
else if (new_output.empty()) // placemarker token
@@ -2048,7 +2026,7 @@ namespace simplecpp {
20482026
return tok->next;
20492027
}
20502028

2051-
TokenList temp2(files);
2029+
TokenList temp2(*files);
20522030
temp2.push_back(new Token(temp.cback()->str(), tok->location));
20532031

20542032
const Token * const tok2 = appendTokens(&temp2, loc, tok->next, macros, expandedmacros, parametertokens);
@@ -2069,7 +2047,7 @@ namespace simplecpp {
20692047

20702048
// Macro parameter..
20712049
{
2072-
TokenList temp(files);
2050+
TokenList temp(*files);
20732051
if (expandArg(&temp, tok, loc, macros, expandedmacros, parametertokens)) {
20742052
if (tok->str() == "__VA_ARGS__" && temp.empty() && output->cback() && output->cback()->str() == "," &&
20752053
tok->nextSkipComments() && tok->nextSkipComments()->str() == ")")
@@ -2086,15 +2064,15 @@ namespace simplecpp {
20862064

20872065
const Macro &calledMacro = it->second;
20882066
if (!calledMacro.functionLike()) {
2089-
TokenList temp(files);
2067+
TokenList temp(*files);
20902068
calledMacro.expand(&temp, loc, tok, macros, expandedmacros);
20912069
return recursiveExpandToken(output, temp, loc, tok, macros, expandedmacros2, parametertokens);
20922070
}
20932071
if (!sameline(tok, tok->next)) {
20942072
output->push_back(newMacroToken(tok->str(), loc, true, tok));
20952073
return tok->next;
20962074
}
2097-
TokenList tokens(files);
2075+
TokenList tokens(*files);
20982076
tokens.push_back(new Token(*tok));
20992077
const Token * tok2 = nullptr;
21002078
if (tok->next->op == '(')
@@ -2108,7 +2086,7 @@ namespace simplecpp {
21082086
output->push_back(newMacroToken(tok->str(), loc, true, tok));
21092087
return tok->next;
21102088
}
2111-
TokenList temp(files);
2089+
TokenList temp(*files);
21122090
calledMacro.expand(&temp, loc, tokens.cfront(), macros, expandedmacros);
21132091
return recursiveExpandToken(output, temp, loc, tok2, macros, expandedmacros, parametertokens);
21142092
}
@@ -2128,7 +2106,7 @@ namespace simplecpp {
21282106
if (defToken) {
21292107
std::string macroName = defToken->str();
21302108
if (defToken->next && defToken->next->op == '#' && defToken->next->next && defToken->next->next->op == '#' && defToken->next->next->next && defToken->next->next->next->name && sameline(defToken,defToken->next->next->next)) {
2131-
TokenList temp(files);
2109+
TokenList temp(*files);
21322110
if (expandArg(&temp, defToken, parametertokens))
21332111
macroName = temp.cback()->str();
21342112
if (expandArg(&temp, defToken->next->next->next, parametertokens))
@@ -2200,7 +2178,7 @@ namespace simplecpp {
22002178
* @return token after the X
22012179
*/
22022180
const Token *expandHash(TokenList *output, const Location &loc, const Token *tok, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
2203-
TokenList tokenListHash(files);
2181+
TokenList tokenListHash(*files);
22042182
const MacroMap macros2; // temporarily bypass macro expansion
22052183
tok = expandToken(&tokenListHash, loc, tok->next, macros2, expandedmacros, parametertokens);
22062184
std::ostringstream ostr;
@@ -2251,7 +2229,7 @@ namespace simplecpp {
22512229
if (canBeConcatenatedStringOrChar && (B->number || !B->name))
22522230
throw invalidHashHash::cannotCombine(tok->location, name(), A, B);
22532231

2254-
TokenList tokensB(files);
2232+
TokenList tokensB(*files);
22552233
const Token *nextTok = B->next;
22562234

22572235
if (canBeConcatenatedStringOrChar) {
@@ -2306,14 +2284,14 @@ namespace simplecpp {
23062284
b->location = loc;
23072285
output->takeTokens(tokensB);
23082286
} else if (sameline(B, nextTok) && sameline(B, nextTok->next) && nextTok->op == '#' && nextTok->next->op == '#') {
2309-
TokenList output2(files);
2287+
TokenList output2(*files);
23102288
output2.push_back(new Token(strAB, tok->location));
23112289
nextTok = expandHashHash(&output2, loc, nextTok, macros, expandedmacros, parametertokens);
23122290
output->deleteToken(A);
23132291
output->takeTokens(output2);
23142292
} else {
23152293
output->deleteToken(A);
2316-
TokenList tokens(files);
2294+
TokenList tokens(*files);
23172295
tokens.push_back(new Token(strAB, tok->location));
23182296
// for function like macros, push the (...)
23192297
if (tokensB.empty() && sameline(B,B->next) && B->next->op=='(') {
@@ -2359,7 +2337,7 @@ namespace simplecpp {
23592337
const Token *endToken;
23602338

23612339
/** files */
2362-
std::vector<std::string> &files;
2340+
std::vector<std::string> *files;
23632341

23642342
/** this is used for -D where the definition is not seen anywhere in code */
23652343
TokenList tokenListDefine;
@@ -3379,7 +3357,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33793357

33803358
if (ifstates.top() == True && (rawtok->str() == ERROR || rawtok->str() == WARNING)) {
33813359
if (outputList) {
3382-
simplecpp::Output err(rawtok->location.files);
3360+
simplecpp::Output err(*rawtok->location.files);
33833361
err.type = rawtok->str() == ERROR ? Output::ERROR : Output::WARNING;
33843362
err.location = rawtok->location;
33853363
for (const Token *tok = rawtok->next; tok && sameline(rawtok,tok); tok = tok->next) {
@@ -3544,7 +3522,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35443522
tok = tok ? tok->next : nullptr;
35453523
if (!tok || !sameline(rawtok,tok) || (par && tok->op != ')')) {
35463524
if (outputList) {
3547-
Output out(rawtok->location.files);
3525+
Output out(*rawtok->location.files);
35483526
out.type = Output::SYNTAX_ERROR;
35493527
out.location = rawtok->location;
35503528
out.msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
@@ -3584,7 +3562,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35843562
tok = tok ? tok->next : nullptr;
35853563
if (!tok || !sameline(rawtok,tok) || (par && tok->op != ')') || (!closingAngularBracket)) {
35863564
if (outputList) {
3587-
Output out(rawtok->location.files);
3565+
Output out(*rawtok->location.files);
35883566
out.type = Output::SYNTAX_ERROR;
35893567
out.location = rawtok->location;
35903568
out.msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
@@ -3621,7 +3599,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36213599
}
36223600
} catch (const std::exception &e) {
36233601
if (outputList) {
3624-
Output out(rawtok->location.files);
3602+
Output out(*rawtok->location.files);
36253603
out.type = Output::SYNTAX_ERROR;
36263604
out.location = rawtok->location;
36273605
out.msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
@@ -3723,7 +3701,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
37233701
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
37243702
usage.insert(usage.end(), temp.begin(), temp.end());
37253703
for (std::list<Location>::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
3726-
MacroUsage mu(usageIt->files, macro.valueDefinedInCode());
3704+
MacroUsage mu(*usageIt->files, macro.valueDefinedInCode());
37273705
mu.macroName = macro.name();
37283706
mu.macroLocation = macro.defineLocation();
37293707
mu.useLocation = *usageIt;

0 commit comments

Comments
 (0)