Skip to content

Commit ee3d11c

Browse files
committed
refs #424 - added simplecpp::TokenList::file() to look up file from location
1 parent 519259b commit ee3d11c

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ int main(int argc, char **argv)
207207
std::cout << outputTokens.stringify(linenrs) << std::endl;
208208

209209
for (const simplecpp::Output &output : outputList) {
210-
std::cerr << output.location.file() << ':' << output.location.line << ": ";
210+
std::cerr << outputTokens.file(output.location) << ':' << output.location.line << ": ";
211211
switch (output.type) {
212212
case simplecpp::Output::ERROR:
213213
std::cerr << "#error: ";

simplecpp.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ static std::string replaceAll(std::string s, const std::string& from, const std:
182182
return s;
183183
}
184184

185-
const std::string simplecpp::Location::emptyFileName;
186-
187185
void simplecpp::Location::adjust(const std::string &str)
188186
{
189187
if (strpbrk(str.c_str(), "\r\n") == nullptr) {
@@ -568,7 +566,7 @@ std::string simplecpp::TokenList::stringify(bool linenrs) const
568566
bool filechg = true;
569567
for (const Token *tok = cfront(); tok; tok = tok->next) {
570568
if (tok->location.line < loc.line || tok->location.fileIndex != loc.fileIndex) {
571-
ret << "\n#line " << tok->location.line << " \"" << tok->location.file(files) << "\"\n";
569+
ret << "\n#line " << tok->location.line << " \"" << file(tok->location) << "\"\n";
572570
loc = tok->location;
573571
filechg = true;
574572
}
@@ -1474,6 +1472,12 @@ unsigned int simplecpp::TokenList::fileIndex(const std::string &filename)
14741472
return files.size() - 1U;
14751473
}
14761474

1475+
const std::string& simplecpp::TokenList::file(const Location& loc) const
1476+
{
1477+
static const std::string s_emptyFileName;
1478+
return loc.fileIndex < files.size() ? files[loc.fileIndex] : s_emptyFileName;
1479+
}
1480+
14771481

14781482
namespace simplecpp {
14791483
class Macro;
@@ -1881,7 +1885,7 @@ namespace simplecpp {
18811885
usageList.push_back(loc);
18821886

18831887
if (nameTokInst->str() == "__FILE__") {
1884-
output.push_back(new Token('\"'+loc.file(output.getFiles())+'\"', loc));
1888+
output.push_back(new Token('\"'+output.file(loc)+'\"', loc));
18851889
return nameTokInst->next;
18861890
}
18871891
if (nameTokInst->str() == "__LINE__") {
@@ -2633,7 +2637,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI
26332637
}
26342638
}
26352639

2636-
const std::string &sourcefile = tok->location.file(expr.getFiles());
2640+
const std::string &sourcefile = expr.file(tok->location);
26372641
const bool systemheader = (tok1 && tok1->op == '<');
26382642
std::string header;
26392643
if (systemheader) {
@@ -3195,7 +3199,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
31953199
if (!rawtok || rawtok->str() != INCLUDE)
31963200
continue;
31973201

3198-
const std::string &sourcefile = rawtok->location.file(filenames);
3202+
const std::string &sourcefile = rawtokens.file(rawtok->location);
31993203

32003204
const Token * const htok = rawtok->nextSkipComments();
32013205
if (!sameline(rawtok, htok))
@@ -3522,7 +3526,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35223526

35233527
const bool systemheader = (inctok->str()[0] == '<');
35243528
const std::string header(inctok->str().substr(1U, inctok->str().size() - 2U));
3525-
const FileData *const filedata = cache.get(rawtok->location.file(files), header, dui, systemheader, files, outputList).first;
3529+
const FileData *const filedata = cache.get(rawtokens.file(rawtok->location), header, dui, systemheader, files, outputList).first;
35263530
if (filedata == nullptr) {
35273531
if (outputList) {
35283532
simplecpp::Output out = {
@@ -3615,7 +3619,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36153619
tok = tok->next;
36163620
bool closingAngularBracket = false;
36173621
if (tok) {
3618-
const std::string &sourcefile = rawtok->location.file(files);
3622+
const std::string &sourcefile = rawtokens.file(rawtok->location);
36193623
const bool systemheader = (tok && tok->op == '<');
36203624
std::string header;
36213625

@@ -3724,7 +3728,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
37243728
macros.erase(tok->str());
37253729
}
37263730
} else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) {
3727-
pragmaOnce.insert(rawtok->location.file(files));
3731+
pragmaOnce.insert(rawtokens.file(rawtok->location));
37283732
}
37293733
if (ifstates.top() != True && rawtok->nextcond)
37303734
rawtok = rawtok->nextcond->previous;

simplecpp.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,9 @@ namespace simplecpp {
101101
return fileIndex == other.fileIndex && line == other.line;
102102
}
103103

104-
const std::string& file(const std::vector<std::string> &f) const {
105-
return fileIndex < f.size() ? f[fileIndex] : emptyFileName;
106-
}
107-
108104
unsigned int fileIndex{};
109105
unsigned int line{1};
110106
unsigned int col{};
111-
private:
112-
static const std::string emptyFileName;
113107
};
114108

115109
/**
@@ -337,6 +331,8 @@ namespace simplecpp {
337331
return files;
338332
}
339333

334+
const std::string& file(const Location& loc) const;
335+
340336
private:
341337
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList, int unused);
342338

0 commit comments

Comments
 (0)