Skip to content

Commit 2b824e7

Browse files
committed
made some Preprocessor methods static [skip ci]
1 parent b662546 commit 2b824e7

File tree

4 files changed

+36
-41
lines changed

4 files changed

+36
-41
lines changed

lib/cppcheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -977,11 +977,11 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
977977
std::vector<std::string> files;
978978
simplecpp::TokenList tokens1 = createTokenList(files, &outputList);
979979

980-
Preprocessor preprocessor(tokens1, mSettings, mErrorLogger, file.lang());
981-
982-
if (preprocessor.reportOutput(outputList, true))
980+
if (Preprocessor::reportOutput(mSettings, mErrorLogger,outputList, true))
983981
return mLogger->exitcode();
984982

983+
Preprocessor preprocessor(tokens1, mSettings, mErrorLogger, file.lang());
984+
985985
if (!preprocessor.loadFiles(files))
986986
return mLogger->exitcode();
987987

lib/preprocessor.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ void Preprocessor::inlineSuppressions(SuppressionList &suppressions)
326326
::addInlineSuppressions(filedata->tokens, mSettings, suppressions, err);
327327
}
328328
for (const BadInlineSuppression &bad : err) {
329-
error(bad.location, bad.errmsg, simplecpp::Output::ERROR);
329+
error(mSettings, mErrorLogger, bad.location, bad.errmsg, simplecpp::Output::ERROR);
330330
}
331331
}
332332

@@ -770,7 +770,7 @@ bool Preprocessor::hasErrors(const simplecpp::Output &output)
770770
bool Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool throwError)
771771
{
772772
const bool showerror = (!mSettings.userDefines.empty() && !mSettings.force);
773-
const bool hasError = reportOutput(outputList, showerror);
773+
const bool hasError = reportOutput(mSettings, mErrorLogger, outputList, showerror);
774774
if (throwError) {
775775
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
776776
return hasErrors(output);
@@ -865,7 +865,7 @@ std::string Preprocessor::getcode(const std::string &cfg, std::vector<std::strin
865865
return ret.str();
866866
}
867867

868-
bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool showerror)
868+
bool Preprocessor::reportOutput(const Settings& settings, ErrorLogger& errorLogger, const simplecpp::OutputList &outputList, bool showerror)
869869
{
870870
bool hasError = false;
871871

@@ -874,7 +874,7 @@ bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh
874874
case simplecpp::Output::ERROR:
875875
hasError = true;
876876
if (!startsWith(out.msg,"#error") || showerror)
877-
error(out.location, out.msg, out.type);
877+
error(settings, errorLogger, out.location, out.msg, out.type);
878878
break;
879879
case simplecpp::Output::WARNING:
880880
case simplecpp::Output::PORTABILITY_BACKSLASH:
@@ -883,22 +883,22 @@ bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh
883883
const std::string::size_type pos1 = out.msg.find_first_of("<\"");
884884
const std::string::size_type pos2 = out.msg.find_first_of(">\"", pos1 + 1U);
885885
if (pos1 < pos2 && pos2 != std::string::npos)
886-
missingInclude(out.location, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
886+
missingInclude(settings, errorLogger, out.location, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
887887
}
888888
break;
889889
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
890890
case simplecpp::Output::SYNTAX_ERROR:
891891
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
892892
hasError = true;
893-
error(out.location, out.msg, out.type);
893+
error(settings, errorLogger, out.location, out.msg, out.type);
894894
break;
895895
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
896896
case simplecpp::Output::FILE_NOT_FOUND:
897897
case simplecpp::Output::DUI_ERROR:
898898
hasError = true;
899899
std::vector<std::string> f;
900900
simplecpp::Location loc(f);
901-
error(loc, out.msg, out.type);
901+
error(settings, errorLogger, loc, out.msg, out.type);
902902
break;
903903
}
904904
}
@@ -929,55 +929,53 @@ static std::string simplecppErrToId(simplecpp::Output::Type type)
929929
}
930930
}
931931

932-
void Preprocessor::error(const simplecpp::Location& loc, const std::string &msg, simplecpp::Output::Type type)
932+
void Preprocessor::error(const Settings& settings, ErrorLogger& errorLogger, const simplecpp::Location& loc, const std::string &msg, simplecpp::Output::Type type)
933933
{
934934
std::list<ErrorMessage::FileLocation> locationList;
935935
if (!loc.file().empty()) {
936936
std::string file = Path::fromNativeSeparators(loc.file());
937-
if (mSettings.relativePaths)
938-
file = Path::getRelativePath(file, mSettings.basePaths);
937+
if (settings.relativePaths)
938+
file = Path::getRelativePath(file, settings.basePaths);
939939

940940
locationList.emplace_back(file, loc.line, loc.col);
941941
}
942-
mErrorLogger.reportErr(ErrorMessage(std::move(locationList),
943-
mFile0,
944-
Severity::error,
945-
msg,
946-
simplecppErrToId(type),
947-
Certainty::normal));
942+
errorLogger.reportErr(ErrorMessage(std::move(locationList),
943+
"",
944+
Severity::error,
945+
msg,
946+
simplecppErrToId(type),
947+
Certainty::normal));
948948
}
949949

950950
// Report that include is missing
951-
void Preprocessor::missingInclude(const simplecpp::Location& loc, const std::string &header, HeaderTypes headerType)
951+
void Preprocessor::missingInclude(const Settings& settings, ErrorLogger& errorLogger, const simplecpp::Location& loc, const std::string &header, HeaderTypes headerType)
952952
{
953-
if (!mSettings.checks.isEnabled(Checks::missingInclude))
953+
if (!settings.checks.isEnabled(Checks::missingInclude))
954954
return;
955955

956956
std::list<ErrorMessage::FileLocation> locationList;
957957
if (!loc.file().empty()) {
958958
locationList.emplace_back(loc.file(), loc.line, loc.col);
959959
}
960-
ErrorMessage errmsg(std::move(locationList), mFile0, Severity::information,
960+
ErrorMessage errmsg(std::move(locationList), "", Severity::information,
961961
(headerType==SystemHeader) ?
962962
"Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results." :
963963
"Include file: \"" + header + "\" not found.",
964964
(headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude",
965965
Certainty::normal);
966-
mErrorLogger.reportErr(errmsg);
966+
errorLogger.reportErr(errmsg);
967967
}
968968

969969
void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &settings)
970970
{
971971
std::vector<std::string> files;
972-
simplecpp::TokenList tokens(files);
973-
Preprocessor preprocessor(tokens, settings, errorLogger, Standards::Language::CPP);
974972
simplecpp::Location loc(files);
975-
preprocessor.missingInclude(loc, "", UserHeader);
976-
preprocessor.missingInclude(loc, "", SystemHeader);
977-
preprocessor.error(loc, "message", simplecpp::Output::ERROR);
978-
preprocessor.error(loc, "message", simplecpp::Output::SYNTAX_ERROR);
979-
preprocessor.error(loc, "message", simplecpp::Output::UNHANDLED_CHAR_ERROR);
980-
preprocessor.error(loc, "message", simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY);
973+
missingInclude(settings, errorLogger, loc, "", UserHeader);
974+
missingInclude(settings, errorLogger, loc, "", SystemHeader);
975+
error(settings, errorLogger, loc, "message", simplecpp::Output::ERROR);
976+
error(settings, errorLogger, loc, "message", simplecpp::Output::SYNTAX_ERROR);
977+
error(settings, errorLogger, loc, "message", simplecpp::Output::UNHANDLED_CHAR_ERROR);
978+
error(settings, errorLogger, loc, "message", simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY);
981979
}
982980

983981
void Preprocessor::dump(std::ostream &out) const

lib/preprocessor.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
140140
*/
141141
void dump(std::ostream &out) const;
142142

143-
bool reportOutput(const simplecpp::OutputList &outputList, bool showerror);
143+
static bool reportOutput(const Settings& settings, ErrorLogger& errorLogger, const simplecpp::OutputList &outputList, bool showerror);
144144

145145
private:
146146
static bool hasErrors(const simplecpp::Output &output);
@@ -157,8 +157,8 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
157157
SystemHeader
158158
};
159159

160-
void missingInclude(const simplecpp::Location& loc, const std::string &header, HeaderTypes headerType);
161-
void error(const simplecpp::Location& loc, const std::string &msg, simplecpp::Output::Type type);
160+
static void missingInclude(const Settings& settings, ErrorLogger& errorLogger, const simplecpp::Location& loc, const std::string &header, HeaderTypes headerType);
161+
static void error(const Settings& settings, ErrorLogger& errorLogger, const simplecpp::Location& loc, const std::string &msg, simplecpp::Output::Type type);
162162

163163
void addRemarkComments(const simplecpp::TokenList &tokens, std::vector<RemarkComment> &remarkComments) const;
164164

@@ -171,8 +171,6 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
171171

172172
simplecpp::FileDataCache mFileCache;
173173

174-
/** filename for cpp/c file - useful when reporting errors */
175-
std::string mFile0; // TODO: this is never set
176174
Standards::Language mLang{Standards::Language::None};
177175

178176
/** simplecpp tracking info */

test/testpreprocessor.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class TestPreprocessor : public TestFixture {
5454
simplecpp::OutputList outputList;
5555
std::vector<std::string> files;
5656
simplecpp::TokenList tokens1 = simplecpp::TokenList(code, files, "file.cpp", &outputList);
57+
(void)Preprocessor::reportOutput(settingsDefault, errorLogger, outputList, true);
5758
Preprocessor p(tokens1, settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
5859
simplecpp::TokenList tokens2 = p.preprocess("", files, true);
59-
(void)p.reportOutput(outputList, true);
6060
return tokens2.stringify();
6161
}
6262

@@ -118,13 +118,12 @@ class TestPreprocessor : public TestFixture {
118118
std::vector<std::string> files;
119119

120120
simplecpp::TokenList tokens(code, size, files, Path::simplifyPath(filename), &outputList);
121+
if (Preprocessor::reportOutput(settings, errorlogger, outputList, true))
122+
return {};
123+
121124
// TODO: we should be using the actual Preprocessor implementation
122125
Preprocessor preprocessor(tokens, settings, errorlogger, Path::identify(tokens.getFiles()[0], false));
123126

124-
// TODO: should be possible without a Preprocessor instance
125-
if (preprocessor.reportOutput(outputList, true))
126-
return {};
127-
128127
if (inlineSuppression)
129128
preprocessor.inlineSuppressions(*inlineSuppression);
130129
preprocessor.removeComments();

0 commit comments

Comments
 (0)