Visitar URL original
Refactorizations: - Return string reference in Token::file() - Moved … · x29a/simplecpp@7adf7c8 · GitHub
Skip to content

Commit 7adf7c8

Browse files
PKEuSdanmar
authored andcommitted
Refactorizations: - Return string reference in Token::file() - Moved some code into conditional scopes
1 parent 4abf3cd commit 7adf7c8

2 files changed

Lines changed: 51 additions & 42 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,12 +2025,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
20252025
}
20262026

20272027
if (ifstates.size() <= 1U && (rawtok->str == ELIF || rawtok->str == ELSE || rawtok->str == ENDIF)) {
2028-
simplecpp::Output err(files);
2029-
err.type = Output::SYNTAX_ERROR;
2030-
err.location = rawtok->location;
2031-
err.msg = "#" + rawtok->str + " without #if";
2032-
if (outputList)
2028+
if (outputList) {
2029+
simplecpp::Output err(files);
2030+
err.type = Output::SYNTAX_ERROR;
2031+
err.location = rawtok->location;
2032+
err.msg = "#" + rawtok->str + " without #if";
20332033
outputList->push_back(err);
2034+
}
20342035
output.clear();
20352036
return;
20362037
}
@@ -2067,12 +2068,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
20672068
it->second = macro;
20682069
}
20692070
} catch (const std::runtime_error &) {
2070-
simplecpp::Output err(files);
2071-
err.type = Output::SYNTAX_ERROR;
2072-
err.location = rawtok->location;
2073-
err.msg = "Failed to parse #define";
2074-
if (outputList)
2071+
if (outputList) {
2072+
simplecpp::Output err(files);
2073+
err.type = Output::SYNTAX_ERROR;
2074+
err.location = rawtok->location;
2075+
err.msg = "Failed to parse #define";
20752076
outputList->push_back(err);
2077+
}
20762078
output.clear();
20772079
return;
20782080
}
@@ -2094,12 +2096,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
20942096
}
20952097

20962098
if (inc2.empty() || inc2.cfront()->str.size() <= 2U) {
2097-
simplecpp::Output err(files);
2098-
err.type = Output::SYNTAX_ERROR;
2099-
err.location = rawtok->location;
2100-
err.msg = "No header in #include";
2101-
if (outputList)
2099+
if (outputList) {
2100+
simplecpp::Output err(files);
2101+
err.type = Output::SYNTAX_ERROR;
2102+
err.location = rawtok->location;
2103+
err.msg = "No header in #include";
21022104
outputList->push_back(err);
2105+
}
21032106
output.clear();
21042107
return;
21052108
}
@@ -2119,19 +2122,21 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
21192122
}
21202123
}
21212124
if (header2.empty()) {
2122-
simplecpp::Output out(files);
2123-
out.type = Output::MISSING_HEADER;
2124-
out.location = rawtok->location;
2125-
out.msg = "Header not found: " + rawtok->next->str;
2126-
if (outputList)
2125+
if (outputList) {
2126+
simplecpp::Output out(files);
2127+
out.type = Output::MISSING_HEADER;
2128+
out.location = rawtok->location;
2129+
out.msg = "Header not found: " + rawtok->next->str;
21272130
outputList->push_back(out);
2131+
}
21282132
} else if (includetokenstack.size() >= 400) {
2129-
simplecpp::Output out(files);
2130-
out.type = Output::INCLUDE_NESTED_TOO_DEEPLY;
2131-
out.location = rawtok->location;
2132-
out.msg = "#include nested too deeply";
2133-
if (outputList)
2133+
if (outputList) {
2134+
simplecpp::Output out(files);
2135+
out.type = Output::INCLUDE_NESTED_TOO_DEEPLY;
2136+
out.location = rawtok->location;
2137+
out.msg = "#include nested too deeply";
21342138
outputList->push_back(out);
2139+
}
21352140
} else if (pragmaOnce.find(header2) == pragmaOnce.end()) {
21362141
includetokenstack.push(gotoNextLine(rawtok));
21372142
const TokenList *includetokens = filedata.find(header2)->second;
@@ -2140,12 +2145,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
21402145
}
21412146
} else if (rawtok->str == IF || rawtok->str == IFDEF || rawtok->str == IFNDEF || rawtok->str == ELIF) {
21422147
if (!sameline(rawtok,rawtok->next)) {
2143-
simplecpp::Output out(files);
2144-
out.type = Output::SYNTAX_ERROR;
2145-
out.location = rawtok->location;
2146-
out.msg = "Syntax error in #" + rawtok->str;
2147-
if (outputList)
2148+
if (outputList) {
2149+
simplecpp::Output out(files);
2150+
out.type = Output::SYNTAX_ERROR;
2151+
out.location = rawtok->location;
2152+
out.msg = "Syntax error in #" + rawtok->str;
21482153
outputList->push_back(out);
2154+
}
21492155
output.clear();
21502156
return;
21512157
}
@@ -2179,12 +2185,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
21792185
if (par)
21802186
tok = tok ? tok->next : NULL;
21812187
if (!tok || !sameline(rawtok,tok) || (par && tok->op != ')')) {
2182-
Output out(rawtok->location.files);
2183-
out.type = Output::SYNTAX_ERROR;
2184-
out.location = rawtok->location;
2185-
out.msg = "failed to evaluate " + std::string(rawtok->str == IF ? "#if" : "#elif") + " condition";
2186-
if (outputList)
2188+
if (outputList) {
2189+
Output out(rawtok->location.files);
2190+
out.type = Output::SYNTAX_ERROR;
2191+
out.location = rawtok->location;
2192+
out.msg = "failed to evaluate " + std::string(rawtok->str == IF ? "#if" : "#elif") + " condition";
21872193
outputList->push_back(out);
2194+
}
21882195
output.clear();
21892196
return;
21902197
}
@@ -2200,12 +2207,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
22002207
try {
22012208
conditionIsTrue = (evaluate(expr, sizeOfType) != 0);
22022209
} catch (const std::exception &) {
2203-
Output out(rawtok->location.files);
2204-
out.type = Output::SYNTAX_ERROR;
2205-
out.location = rawtok->location;
2206-
out.msg = "failed to evaluate " + std::string(rawtok->str == IF ? "#if" : "#elif") + " condition";
2207-
if (outputList)
2210+
if (outputList) {
2211+
Output out(rawtok->location.files);
2212+
out.type = Output::SYNTAX_ERROR;
2213+
out.location = rawtok->location;
2214+
out.msg = "failed to evaluate " + std::string(rawtok->str == IF ? "#if" : "#elif") + " condition";
22082215
outputList->push_back(out);
2216+
}
22092217
output.clear();
22102218
return;
22112219
}

‎simplecpp.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ namespace simplecpp {
7676
return fileIndex == other.fileIndex && line == other.line;
7777
}
7878

79-
std::string file() const {
80-
return fileIndex < files.size() ? files[fileIndex] : std::string("");
79+
const std::string& file() const {
80+
static const std::string temp;
81+
return fileIndex < files.size() ? files[fileIndex] : temp;
8182
}
8283

8384
const std::vector<std::string> &files;

0 commit comments

Comments
 (0)