Skip to content

Commit 37b50df

Browse files
committed
refs #424 - do not store reference to filelist in simplecpp::Location / cleanups
1 parent 8e85885 commit 37b50df

File tree

3 files changed

+33
-40
lines changed

3 files changed

+33
-40
lines changed

simplecpp.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ class FileStream : public simplecpp::TokenList::Stream {
422422
{
423423
if (!file) {
424424
files.push_back(filename);
425-
throw simplecpp::Output(simplecpp::Output::FILE_NOT_FOUND, simplecpp::Location(files), "File is missing: " + filename);
425+
throw simplecpp::Output(simplecpp::Output::FILE_NOT_FOUND, {}, "File is missing: " + filename);
426426
}
427427
init();
428428
}
@@ -564,11 +564,11 @@ void simplecpp::TokenList::dump(bool linenrs) const
564564
std::string simplecpp::TokenList::stringify(bool linenrs) const
565565
{
566566
std::ostringstream ret;
567-
Location loc(files);
567+
Location loc;
568568
bool filechg = true;
569569
for (const Token *tok = cfront(); tok; tok = tok->next) {
570570
if (tok->location.line < loc.line || tok->location.fileIndex != loc.fileIndex) {
571-
ret << "\n#line " << tok->location.line << " \"" << tok->location.file() << "\"\n";
571+
ret << "\n#line " << tok->location.line << " \"" << tok->location.file(files) << "\"\n";
572572
loc = tok->location;
573573
filechg = true;
574574
}
@@ -633,16 +633,16 @@ static bool isStringLiteralPrefix(const std::string &str)
633633
str == "R" || str == "uR" || str == "UR" || str == "LR" || str == "u8R";
634634
}
635635

636-
void simplecpp::TokenList::lineDirective(unsigned int fileIndex, unsigned int line, Location *location)
636+
void simplecpp::TokenList::lineDirective(unsigned int fileIndex, unsigned int line, Location &location)
637637
{
638-
if (fileIndex != location->fileIndex || line >= location->line) {
639-
location->fileIndex = fileIndex;
640-
location->line = line;
638+
if (fileIndex != location.fileIndex || line >= location.line) {
639+
location.fileIndex = fileIndex;
640+
location.line = line;
641641
return;
642642
}
643643

644-
if (line + 2 >= location->line) {
645-
location->line = line;
644+
if (line + 2 >= location.line) {
645+
location.line = line;
646646
while (cback()->op != '#')
647647
deleteToken(back());
648648
deleteToken(back());
@@ -660,10 +660,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
660660

661661
const Token *oldLastToken = nullptr;
662662

663-
Location location(files);
664-
location.fileIndex = fileIndex(filename);
665-
location.line = 1U;
666-
location.col = 1U;
663+
Location location(fileIndex(filename), 1, 1);
667664
while (stream.good()) {
668665
unsigned char ch = stream.readChar();
669666
if (!stream.good())
@@ -731,7 +728,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
731728
while (numtok->comment)
732729
numtok = numtok->previous;
733730
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
734-
std::atol(numtok->str().c_str()), &location);
731+
std::atol(numtok->str().c_str()), location);
735732
}
736733
// #line 3
737734
else if (llNextToken->str() == "line" &&
@@ -740,7 +737,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
740737
const Token *numtok = cback();
741738
while (numtok->comment)
742739
numtok = numtok->previous;
743-
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
740+
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), location);
744741
}
745742
}
746743
// #endfile
@@ -1884,7 +1881,7 @@ namespace simplecpp {
18841881
usageList.push_back(loc);
18851882

18861883
if (nameTokInst->str() == "__FILE__") {
1887-
output.push_back(new Token('\"'+loc.file()+'\"', loc));
1884+
output.push_back(new Token('\"'+loc.file(output.getFiles())+'\"', loc));
18881885
return nameTokInst->next;
18891886
}
18901887
if (nameTokInst->str() == "__LINE__") {
@@ -2636,7 +2633,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI
26362633
}
26372634
}
26382635

2639-
const std::string &sourcefile = tok->location.file();
2636+
const std::string &sourcefile = tok->location.file(expr.getFiles());
26402637
const bool systemheader = (tok1 && tok1->op == '<');
26412638
std::string header;
26422639
if (systemheader) {
@@ -3165,7 +3162,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
31653162
if (outputList) {
31663163
simplecpp::Output err = {
31673164
simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND,
3168-
Location(filenames),
3165+
{},
31693166
"Can not open include file '" + filename + "' that is explicitly included."
31703167
};
31713168
outputList->push_back(std::move(err));
@@ -3198,7 +3195,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
31983195
if (!rawtok || rawtok->str() != INCLUDE)
31993196
continue;
32003197

3201-
const std::string &sourcefile = rawtok->location.file();
3198+
const std::string &sourcefile = rawtok->location.file(filenames);
32023199

32033200
const Token * const htok = rawtok->nextSkipComments();
32043201
if (!sameline(rawtok, htok))
@@ -3355,7 +3352,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33553352
if (outputList) {
33563353
simplecpp::Output err = {
33573354
Output::DUI_ERROR,
3358-
Location(files),
3355+
{},
33593356
"unknown standard specified: '" + dui.std + "'"
33603357
};
33613358
outputList->push_back(std::move(err));
@@ -3525,7 +3522,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35253522

35263523
const bool systemheader = (inctok->str()[0] == '<');
35273524
const std::string header(inctok->str().substr(1U, inctok->str().size() - 2U));
3528-
const FileData *const filedata = cache.get(rawtok->location.file(), header, dui, systemheader, files, outputList).first;
3525+
const FileData *const filedata = cache.get(rawtok->location.file(files), header, dui, systemheader, files, outputList).first;
35293526
if (filedata == nullptr) {
35303527
if (outputList) {
35313528
simplecpp::Output out = {
@@ -3618,7 +3615,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36183615
tok = tok->next;
36193616
bool closingAngularBracket = false;
36203617
if (tok) {
3621-
const std::string &sourcefile = rawtok->location.file();
3618+
const std::string &sourcefile = rawtok->location.file(files);
36223619
const bool systemheader = (tok && tok->op == '<');
36233620
std::string header;
36243621

@@ -3727,7 +3724,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
37273724
macros.erase(tok->str());
37283725
}
37293726
} else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) {
3730-
pragmaOnce.insert(rawtok->location.file());
3727+
pragmaOnce.insert(rawtok->location.file(files));
37313728
}
37323729
if (ifstates.top() != True && rawtok->nextcond)
37333730
rawtok = rawtok->nextcond->previous;
@@ -3783,7 +3780,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
37833780
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
37843781
usage.insert(usage.end(), temp.begin(), temp.end());
37853782
for (std::list<Location>::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
3786-
MacroUsage mu(usageIt->files, macro.valueDefinedInCode());
3783+
MacroUsage mu(macro.valueDefinedInCode());
37873784
mu.macroName = macro.name();
37883785
mu.macroLocation = macro.defineLocation();
37893786
mu.useLocation = *usageIt;

simplecpp.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,15 @@ namespace simplecpp {
7676
*/
7777
class SIMPLECPP_LIB Location {
7878
public:
79-
explicit Location(const std::vector<std::string> &f) : files(f) {}
79+
Location() = default;
80+
Location(unsigned int fileIndex, unsigned int line, unsigned int col)
81+
: fileIndex(fileIndex)
82+
, line(line)
83+
, col(col)
84+
{}
8085

8186
Location(const Location &loc) = default;
82-
83-
Location &operator=(const Location &other) {
84-
if (this != &other) {
85-
fileIndex = other.fileIndex;
86-
line = other.line;
87-
col = other.col;
88-
}
89-
return *this;
90-
}
87+
Location &operator=(const Location &other) = default;
9188

9289
/** increment this location by string */
9390
void adjust(const std::string &str);
@@ -104,11 +101,10 @@ namespace simplecpp {
104101
return fileIndex == other.fileIndex && line == other.line;
105102
}
106103

107-
const std::string& file() const {
108-
return fileIndex < files.size() ? files[fileIndex] : emptyFileName;
104+
const std::string& file(const std::vector<std::string> &f) const {
105+
return fileIndex < f.size() ? f[fileIndex] : emptyFileName;
109106
}
110107

111-
const std::vector<std::string> &files;
112108
unsigned int fileIndex{};
113109
unsigned int line{1};
114110
unsigned int col{};
@@ -356,7 +352,7 @@ namespace simplecpp {
356352
void constFoldQuestionOp(Token **tok1);
357353

358354
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
359-
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);
355+
void lineDirective(unsigned int fileIndex, unsigned int line, Location &location);
360356

361357
const Token* lastLineTok(int maxsize=1000) const;
362358
const Token* isLastLinePreprocessor(int maxsize=1000) const;
@@ -370,7 +366,7 @@ namespace simplecpp {
370366

371367
/** Tracking how macros are used */
372368
struct SIMPLECPP_LIB MacroUsage {
373-
explicit MacroUsage(const std::vector<std::string> &f, bool macroValueKnown_) : macroLocation(f), useLocation(f), macroValueKnown(macroValueKnown_) {}
369+
explicit MacroUsage(bool macroValueKnown_) : macroValueKnown(macroValueKnown_) {}
374370
std::string macroName;
375371
Location macroLocation;
376372
Location useLocation;

test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3125,7 +3125,7 @@ static void stdValid()
31253125
static void assertToken(const std::string& s, bool name, bool number, bool comment, char op, int line)
31263126
{
31273127
const std::vector<std::string> f;
3128-
const simplecpp::Location l(f);
3128+
const simplecpp::Location l;
31293129
const simplecpp::Token t(s, l);
31303130
assertEquals(name, t.name, line);
31313131
assertEquals(number, t.number, line);

0 commit comments

Comments
 (0)