Skip to content

Commit 8e85885

Browse files
authored
refs #424 - removed filelist parameter from simplecpp::Output constructors / cleanups (#569)
1 parent a766d22 commit 8e85885

File tree

2 files changed

+122
-101
lines changed

2 files changed

+122
-101
lines changed

simplecpp.cpp

Lines changed: 121 additions & 99 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(files, simplecpp::Output::FILE_NOT_FOUND, "File is missing: " + filename);
425+
throw simplecpp::Output(simplecpp::Output::FILE_NOT_FOUND, simplecpp::Location(files), "File is missing: " + filename);
426426
}
427427
init();
428428
}
@@ -615,14 +615,15 @@ static std::string escapeString(const std::string &str)
615615
return ostr.str();
616616
}
617617

618-
static void portabilityBackslash(simplecpp::OutputList *outputList, const std::vector<std::string> &files, const simplecpp::Location &location)
618+
static void portabilityBackslash(simplecpp::OutputList *outputList, const simplecpp::Location &location)
619619
{
620620
if (!outputList)
621621
return;
622-
simplecpp::Output err(files);
623-
err.type = simplecpp::Output::PORTABILITY_BACKSLASH;
624-
err.location = location;
625-
err.msg = "Combination 'backslash space newline' is not portable.";
622+
simplecpp::Output err = {
623+
simplecpp::Output::PORTABILITY_BACKSLASH,
624+
location,
625+
"Combination 'backslash space newline' is not portable."
626+
};
626627
outputList->push_back(std::move(err));
627628
}
628629

@@ -670,13 +671,12 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
670671

671672
if (ch >= 0x80) {
672673
if (outputList) {
673-
simplecpp::Output err(files);
674-
err.type = simplecpp::Output::UNHANDLED_CHAR_ERROR;
675-
err.location = location;
676-
std::ostringstream s;
677-
s << static_cast<int>(ch);
678-
err.msg = "The code contains unhandled character(s) (character code=" + s.str() + "). Neither unicode nor extended ascii is supported.";
679-
outputList->push_back(err);
674+
simplecpp::Output err = {
675+
simplecpp::Output::UNHANDLED_CHAR_ERROR,
676+
location,
677+
"The code contains unhandled character(s) (character code=" + std::to_string(static_cast<int>(ch)) + "). Neither unicode nor extended ascii is supported."
678+
};
679+
outputList->push_back(std::move(err));
680680
}
681681
clear();
682682
return;
@@ -685,7 +685,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
685685
if (ch == '\n') {
686686
if (cback() && cback()->op == '\\') {
687687
if (location.col > cback()->location.col + 1U)
688-
portabilityBackslash(outputList, files, cback()->location);
688+
portabilityBackslash(outputList, cback()->location);
689689
++multiline;
690690
deleteToken(back());
691691
} else {
@@ -812,7 +812,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
812812
const TokenString check_portability = currentToken + tmp;
813813
const std::string::size_type pos = check_portability.find_last_not_of(" \t");
814814
if (pos < check_portability.size() - 1U && check_portability[pos] == '\\')
815-
portabilityBackslash(outputList, files, location);
815+
portabilityBackslash(outputList, location);
816816
++multiline;
817817
tmp_ch = stream.readChar();
818818
currentToken += '\n';
@@ -872,11 +872,12 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
872872
}
873873
if (!stream.good() || ch == '\n') {
874874
if (outputList) {
875-
Output err(files);
876-
err.type = Output::SYNTAX_ERROR;
877-
err.location = location;
878-
err.msg = "Invalid newline in raw string delimiter.";
879-
outputList->push_back(err);
875+
Output err = {
876+
Output::SYNTAX_ERROR,
877+
location,
878+
"Invalid newline in raw string delimiter."
879+
};
880+
outputList->push_back(std::move(err));
880881
}
881882
return;
882883
}
@@ -885,10 +886,11 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
885886
currentToken += stream.readChar();
886887
if (!endsWith(currentToken, endOfRawString)) {
887888
if (outputList) {
888-
Output err(files);
889-
err.type = Output::SYNTAX_ERROR;
890-
err.location = location;
891-
err.msg = "Raw string missing terminating delimiter.";
889+
Output err = {
890+
Output::SYNTAX_ERROR,
891+
location,
892+
"Raw string missing terminating delimiter."
893+
};
892894
outputList->push_back(std::move(err));
893895
}
894896
return;
@@ -1428,10 +1430,11 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
14281430
if (!stream.good() || ch != end) {
14291431
clear();
14301432
if (outputList) {
1431-
Output err(files);
1432-
err.type = Output::SYNTAX_ERROR;
1433-
err.location = location;
1434-
err.msg = std::string("No pair for character (") + start + "). Can't process file. File is either invalid or unicode, which is currently not supported.";
1433+
Output err = {
1434+
Output::SYNTAX_ERROR,
1435+
location,
1436+
std::string("No pair for character (") + start + "). Can't process file. File is either invalid or unicode, which is currently not supported."
1437+
};
14351438
outputList->push_back(std::move(err));
14361439
}
14371440
return "";
@@ -3160,10 +3163,11 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
31603163

31613164
if (filedata == nullptr) {
31623165
if (outputList) {
3163-
simplecpp::Output err(filenames);
3164-
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
3165-
err.location = Location(filenames);
3166-
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3166+
simplecpp::Output err = {
3167+
simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND,
3168+
Location(filenames),
3169+
"Can not open include file '" + filename + "' that is explicitly included."
3170+
};
31673171
outputList->push_back(std::move(err));
31683172
}
31693173
continue;
@@ -3231,12 +3235,13 @@ static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token
32313235
simplecpp::TokenList value(files);
32323236
try {
32333237
*tok1 = it->second.expand(value, tok, macros, files);
3234-
} catch (simplecpp::Macro::Error &err) {
3238+
} catch (const simplecpp::Macro::Error &err) {
32353239
if (outputList) {
3236-
simplecpp::Output out(files);
3237-
out.type = simplecpp::Output::SYNTAX_ERROR;
3238-
out.location = err.location;
3239-
out.msg = "failed to expand \'" + tok->str() + "\', " + err.what;
3240+
simplecpp::Output out = {
3241+
simplecpp::Output::SYNTAX_ERROR,
3242+
err.location,
3243+
"failed to expand \'" + tok->str() + "\', " + err.what
3244+
};
32403245
outputList->push_back(std::move(out));
32413246
}
32423247
return false;
@@ -3348,10 +3353,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33483353
const cppstd_t cpp_std = simplecpp::getCppStd(dui.std);
33493354
if (cpp_std == CPPUnknown) {
33503355
if (outputList) {
3351-
simplecpp::Output err(files);
3352-
err.type = Output::DUI_ERROR;
3353-
err.msg = "unknown standard specified: '" + dui.std + "'";
3354-
outputList->push_back(err);
3356+
simplecpp::Output err = {
3357+
Output::DUI_ERROR,
3358+
Location(files),
3359+
"unknown standard specified: '" + dui.std + "'"
3360+
};
3361+
outputList->push_back(std::move(err));
33553362
}
33563363
output.clear();
33573364
return;
@@ -3403,27 +3410,32 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34033410

34043411
if (ifstates.size() <= 1U && (rawtok->str() == ELIF || rawtok->str() == ELSE || rawtok->str() == ENDIF)) {
34053412
if (outputList) {
3406-
simplecpp::Output err(files);
3407-
err.type = Output::SYNTAX_ERROR;
3408-
err.location = rawtok->location;
3409-
err.msg = "#" + rawtok->str() + " without #if";
3410-
outputList->push_back(err);
3413+
simplecpp::Output err = {
3414+
Output::SYNTAX_ERROR,
3415+
rawtok->location,
3416+
"#" + rawtok->str() + " without #if"
3417+
};
3418+
outputList->push_back(std::move(err));
34113419
}
34123420
output.clear();
34133421
return;
34143422
}
34153423

34163424
if (ifstates.top() == True && (rawtok->str() == ERROR || rawtok->str() == WARNING)) {
34173425
if (outputList) {
3418-
simplecpp::Output err(rawtok->location.files);
3419-
err.type = rawtok->str() == ERROR ? Output::ERROR : Output::WARNING;
3420-
err.location = rawtok->location;
3426+
std::string msg;
34213427
for (const Token *tok = rawtok->next; tok && sameline(rawtok,tok); tok = tok->next) {
3422-
if (!err.msg.empty() && isNameChar(tok->str()[0]))
3423-
err.msg += ' ';
3424-
err.msg += tok->str();
3428+
if (!msg.empty() && isNameChar(tok->str()[0]))
3429+
msg += ' ';
3430+
msg += tok->str();
34253431
}
3426-
err.msg = '#' + rawtok->str() + ' ' + err.msg;
3432+
msg = '#' + rawtok->str() + ' ' + msg;
3433+
simplecpp::Output err = {
3434+
rawtok->str() == ERROR ? Output::ERROR : Output::WARNING,
3435+
rawtok->location,
3436+
std::move(msg)
3437+
};
3438+
34273439
outputList->push_back(std::move(err));
34283440
}
34293441
if (rawtok->str() == ERROR) {
@@ -3446,21 +3458,23 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34463458
}
34473459
} catch (const std::runtime_error &) {
34483460
if (outputList) {
3449-
simplecpp::Output err(files);
3450-
err.type = Output::SYNTAX_ERROR;
3451-
err.location = rawtok->location;
3452-
err.msg = "Failed to parse #define";
3453-
outputList->push_back(err);
3461+
simplecpp::Output err = {
3462+
Output::SYNTAX_ERROR,
3463+
rawtok->location,
3464+
"Failed to parse #define"
3465+
};
3466+
outputList->push_back(std::move(err));
34543467
}
34553468
output.clear();
34563469
return;
3457-
} catch (simplecpp::Macro::Error &err) {
3470+
} catch (const simplecpp::Macro::Error &err) {
34583471
if (outputList) {
3459-
simplecpp::Output out(files);
3460-
out.type = simplecpp::Output::SYNTAX_ERROR;
3461-
out.location = err.location;
3462-
out.msg = "Failed to parse #define, " + err.what;
3463-
outputList->push_back(out);
3472+
simplecpp::Output out = {
3473+
simplecpp::Output::SYNTAX_ERROR,
3474+
err.location,
3475+
"Failed to parse #define, " + err.what
3476+
};
3477+
outputList->push_back(std::move(out));
34643478
}
34653479
output.clear();
34663480
return;
@@ -3496,11 +3510,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34963510

34973511
if (inc2.empty() || inc2.cfront()->str().size() <= 2U) {
34983512
if (outputList) {
3499-
simplecpp::Output err(files);
3500-
err.type = Output::SYNTAX_ERROR;
3501-
err.location = rawtok->location;
3502-
err.msg = "No header in #include";
3503-
outputList->push_back(err);
3513+
simplecpp::Output err = {
3514+
Output::SYNTAX_ERROR,
3515+
rawtok->location,
3516+
"No header in #include"
3517+
};
3518+
outputList->push_back(std::move(err));
35043519
}
35053520
output.clear();
35063521
return;
@@ -3513,19 +3528,21 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35133528
const FileData *const filedata = cache.get(rawtok->location.file(), header, dui, systemheader, files, outputList).first;
35143529
if (filedata == nullptr) {
35153530
if (outputList) {
3516-
simplecpp::Output out(files);
3517-
out.type = Output::MISSING_HEADER;
3518-
out.location = rawtok->location;
3519-
out.msg = "Header not found: " + inctok->str();
3520-
outputList->push_back(out);
3531+
simplecpp::Output out = {
3532+
simplecpp::Output::MISSING_HEADER,
3533+
rawtok->location,
3534+
"Header not found: " + inctok->str()
3535+
};
3536+
outputList->push_back(std::move(out));
35213537
}
35223538
} else if (includetokenstack.size() >= 400) {
35233539
if (outputList) {
3524-
simplecpp::Output out(files);
3525-
out.type = Output::INCLUDE_NESTED_TOO_DEEPLY;
3526-
out.location = rawtok->location;
3527-
out.msg = "#include nested too deeply";
3528-
outputList->push_back(out);
3540+
simplecpp::Output out = {
3541+
simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY,
3542+
rawtok->location,
3543+
"#include nested too deeply"
3544+
};
3545+
outputList->push_back(std::move(out));
35293546
}
35303547
} else if (pragmaOnce.find(filedata->filename) == pragmaOnce.end()) {
35313548
includetokenstack.push(gotoNextLine(rawtok));
@@ -3535,11 +3552,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35353552
} else if (rawtok->str() == IF || rawtok->str() == IFDEF || rawtok->str() == IFNDEF || rawtok->str() == ELIF) {
35363553
if (!sameline(rawtok,rawtok->next)) {
35373554
if (outputList) {
3538-
simplecpp::Output out(files);
3539-
out.type = Output::SYNTAX_ERROR;
3540-
out.location = rawtok->location;
3541-
out.msg = "Syntax error in #" + rawtok->str();
3542-
outputList->push_back(out);
3555+
simplecpp::Output out = {
3556+
simplecpp::Output::SYNTAX_ERROR,
3557+
rawtok->location,
3558+
"Syntax error in #" + rawtok->str()
3559+
};
3560+
outputList->push_back(std::move(out));
35433561
}
35443562
output.clear();
35453563
return;
@@ -3580,10 +3598,11 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35803598
tok = tok ? tok->next : nullptr;
35813599
if (!tok || !sameline(rawtok,tok) || (par && tok->op != ')')) {
35823600
if (outputList) {
3583-
Output out(rawtok->location.files);
3584-
out.type = Output::SYNTAX_ERROR;
3585-
out.location = rawtok->location;
3586-
out.msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
3601+
Output out = {
3602+
Output::SYNTAX_ERROR,
3603+
rawtok->location,
3604+
"failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition"
3605+
};
35873606
outputList->push_back(std::move(out));
35883607
}
35893608
output.clear();
@@ -3622,11 +3641,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36223641
tok = tok ? tok->next : nullptr;
36233642
if (!tok || !sameline(rawtok,tok) || (par && tok->op != ')') || (!closingAngularBracket)) {
36243643
if (outputList) {
3625-
Output out(rawtok->location.files);
3626-
out.type = Output::SYNTAX_ERROR;
3627-
out.location = rawtok->location;
3628-
out.msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
3629-
outputList->push_back(out);
3644+
Output out = {
3645+
Output::SYNTAX_ERROR,
3646+
rawtok->location,
3647+
"failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition"
3648+
};
3649+
outputList->push_back(std::move(out));
36303650
}
36313651
output.clear();
36323652
return;
@@ -3659,13 +3679,15 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36593679
}
36603680
} catch (const std::exception &e) {
36613681
if (outputList) {
3662-
Output out(rawtok->location.files);
3663-
out.type = Output::SYNTAX_ERROR;
3664-
out.location = rawtok->location;
3665-
out.msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
3682+
std::string msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
36663683
if (e.what() && *e.what())
3667-
out.msg += std::string(", ") + e.what();
3668-
outputList->push_back(out);
3684+
msg += std::string(", ") + e.what();
3685+
Output out = {
3686+
Output::SYNTAX_ERROR,
3687+
rawtok->location,
3688+
std::move(msg)
3689+
};
3690+
outputList->push_back(std::move(out));
36693691
}
36703692
output.clear();
36713693
return;

0 commit comments

Comments
 (0)