Skip to content

Commit 14f9697

Browse files
committed
s [skip ci]
1 parent 6be46e3 commit 14f9697

File tree

6 files changed

+62
-53
lines changed

6 files changed

+62
-53
lines changed

lib/cppcheck.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,11 +1133,37 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
11331133
try {
11341134
TokenList tokenlist{mSettings, file.lang()};
11351135

1136-
// Create tokens, skip rest of iteration if failed
1137-
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() {
1138-
simplecpp::TokenList tokensP = preprocessor.preprocess(currentConfig, files, true);
1139-
tokenlist.createTokens(std::move(tokensP));
1140-
});
1136+
try {
1137+
// Create tokens, skip rest of iteration if failed
1138+
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() {
1139+
simplecpp::OutputList outputList;
1140+
simplecpp::TokenList tokensP = preprocessor.preprocess(currentConfig, files, outputList);
1141+
Preprocessor::throwFirstError(outputList);
1142+
tokenlist.createTokens(std::move(tokensP));
1143+
});
1144+
} catch (const simplecpp::Output &o) {
1145+
// #error etc during preprocessing
1146+
configurationError.push_back((currentConfig.empty() ? "\'\'" : currentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
1147+
--checkCount; // don't count invalid configurations
1148+
1149+
if (!hasValidConfig && currCfg == *configurations.rbegin()) {
1150+
// If there is no valid configuration then report error..
1151+
std::string locfile = Path::fromNativeSeparators(o.location.file());
1152+
if (mSettings.relativePaths)
1153+
locfile = Path::getRelativePath(locfile, mSettings.basePaths);
1154+
1155+
ErrorMessage::FileLocation loc1(locfile, o.location.line, o.location.col);
1156+
1157+
ErrorMessage errmsg({std::move(loc1)},
1158+
file.spath(),
1159+
Severity::error,
1160+
o.msg,
1161+
"preprocessorErrorDirective",
1162+
Certainty::normal);
1163+
mErrorLogger.reportErr(errmsg);
1164+
}
1165+
continue;
1166+
}
11411167
hasValidConfig = true;
11421168

11431169
Tokenizer tokenizer(std::move(tokenlist), mErrorLogger);
@@ -1206,29 +1232,6 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
12061232
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenizer.list, file.spath());
12071233
mErrorLogger.reportErr(errmsg);
12081234
}
1209-
} catch (const simplecpp::Output &o) {
1210-
// #error etc during preprocessing
1211-
configurationError.push_back((currentConfig.empty() ? "\'\'" : currentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
1212-
--checkCount; // don't count invalid configurations
1213-
1214-
if (!hasValidConfig && currCfg == *configurations.rbegin()) {
1215-
// If there is no valid configuration then report error..
1216-
std::string locfile = Path::fromNativeSeparators(o.location.file());
1217-
if (mSettings.relativePaths)
1218-
locfile = Path::getRelativePath(locfile, mSettings.basePaths);
1219-
1220-
ErrorMessage::FileLocation loc1(locfile, o.location.line, o.location.col);
1221-
1222-
ErrorMessage errmsg({std::move(loc1)},
1223-
file.spath(),
1224-
Severity::error,
1225-
o.msg,
1226-
"preprocessorErrorDirective",
1227-
Certainty::normal);
1228-
mErrorLogger.reportErr(errmsg);
1229-
}
1230-
continue;
1231-
12321235
} catch (const TerminateException &) {
12331236
// Analysis is terminated
12341237
if (analyzerInformation)

lib/preprocessor.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -767,28 +767,30 @@ bool Preprocessor::hasErrors(const simplecpp::Output &output)
767767
return false;
768768
}
769769

770-
bool Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool throwError)
770+
bool Preprocessor::handleErrors(const simplecpp::OutputList& outputList)
771771
{
772772
const bool showerror = (!mSettings.userDefines.empty() && !mSettings.force);
773773
const bool hasError = reportOutput(mSettings, mErrorLogger, outputList, showerror);
774-
if (throwError) {
775-
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
776-
return hasErrors(output);
777-
});
778-
if (it != outputList.cend()) {
779-
throw *it;
780-
}
781-
}
782774
return hasError;
783775
}
784776

777+
void Preprocessor::getFirstError(const simplecpp::OutputList& outputList)
778+
{
779+
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
780+
return hasErrors(output);
781+
});
782+
if (it != outputList.cend()) {
783+
throw *it;
784+
}
785+
}
786+
785787
bool Preprocessor::loadFiles(std::vector<std::string> &files)
786788
{
787789
const simplecpp::DUI dui = createDUI(mSettings, "", mLang);
788790

789791
simplecpp::OutputList outputList;
790792
mFileCache = simplecpp::load(mTokens, files, dui, &outputList);
791-
return !handleErrors(outputList, false);
793+
return !handleErrors(outputList);
792794
}
793795

794796
void Preprocessor::removeComments()
@@ -819,19 +821,18 @@ void Preprocessor::setPlatformInfo()
819821
mTokens.sizeOfType["long double *"] = mSettings.platform.sizeof_pointer;
820822
}
821823

822-
simplecpp::TokenList Preprocessor::preprocess(const std::string &cfg, std::vector<std::string> &files, bool throwError)
824+
simplecpp::TokenList Preprocessor::preprocess(const std::string &cfg, std::vector<std::string> &files, simplecpp::OutputList& outputList)
823825
{
824826
const simplecpp::DUI dui = createDUI(mSettings, cfg, mLang);
825827

826-
simplecpp::OutputList outputList;
827828
std::list<simplecpp::MacroUsage> macroUsage;
828829
std::list<simplecpp::IfCond> ifCond;
829830
simplecpp::TokenList tokens2(files);
830831
simplecpp::preprocess(tokens2, mTokens, files, mFileCache, dui, &outputList, &macroUsage, &ifCond);
831832
mMacroUsage = std::move(macroUsage);
832833
mIfCond = std::move(ifCond);
833834

834-
(void)handleErrors(outputList, throwError);
835+
(void)handleErrors(outputList);
835836

836837
tokens2.removeComments();
837838

@@ -840,7 +841,8 @@ simplecpp::TokenList Preprocessor::preprocess(const std::string &cfg, std::vecto
840841

841842
std::string Preprocessor::getcode(const std::string &cfg, std::vector<std::string> &files, const bool writeLocations)
842843
{
843-
simplecpp::TokenList tokens2 = preprocess(cfg, files, false);
844+
simplecpp::OutputList outputList;
845+
simplecpp::TokenList tokens2 = preprocess(cfg, files, outputList);
844846
unsigned int prevfile = 0;
845847
unsigned int line = 1;
846848
std::ostringstream ret;

lib/preprocessor.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
119119

120120
void setPlatformInfo();
121121

122-
/**
123-
* @throws simplecpp::Output thrown in case of preprocessing error
124-
*/
125-
simplecpp::TokenList preprocess(const std::string &cfg, std::vector<std::string> &files, bool throwError = false);
122+
simplecpp::TokenList preprocess(const std::string &cfg, std::vector<std::string> &files, simplecpp::OutputList& outputList);
123+
124+
// used in tests only
125+
static void throwFirstError(const simplecpp::OutputList& outputList);
126126

127127
std::string getcode(const std::string &cfg, std::vector<std::string> &files, bool writeLocations);
128128

@@ -148,10 +148,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
148148
private:
149149
static bool hasErrors(const simplecpp::Output &output);
150150

151-
/**
152-
* @throws simplecpp::Output thrown in case of preprocessing error
153-
*/
154-
bool handleErrors(const simplecpp::OutputList &outputList, bool throwError);
151+
bool handleErrors(const simplecpp::OutputList &outputList);
155152

156153
static void simplifyPragmaAsmPrivate(simplecpp::TokenList &tokenList);
157154

test/helpers.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ void SimpleTokenizer2::preprocess(const char* code, std::size_t size, std::vecto
116116
simplecpp::TokenList tokens1(code, size, files, file0);
117117

118118
Preprocessor preprocessor(tokens1, tokenizer.getSettings(), errorlogger, Path::identify(tokens1.getFiles()[0], false));
119-
simplecpp::TokenList tokens2 = preprocessor.preprocess("", files, true);
119+
120+
simplecpp::OutputList outputList;
121+
simplecpp::TokenList tokens2 = preprocessor.preprocess("", files, outputList);
122+
Preprocessor::throwFirstError(outputList);
120123

121124
// Tokenizer..
122125
tokenizer.list.createTokens(std::move(tokens2));

test/testpreprocessor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ class TestPreprocessor : public TestFixture {
5656
simplecpp::TokenList tokens1 = simplecpp::TokenList(code, files, "file.cpp", &outputList);
5757
(void)Preprocessor::reportOutput(settingsDefault, errorLogger, outputList, true);
5858
Preprocessor p(tokens1, settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
59-
simplecpp::TokenList tokens2 = p.preprocess("", files, true);
59+
simplecpp::OutputList outputList_pp;
60+
simplecpp::TokenList tokens2 = p.preprocess("", files, outputList_pp);
61+
Preprocessor::throwFirstError(outputList_pp);
6062
return tokens2.stringify();
6163
}
6264

test/testtokenlist.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ class TestTokenList : public TestFixture {
158158
std::vector<std::string> files;
159159
simplecpp::TokenList tokens1(code, files, "poll.h", nullptr);
160160
Preprocessor preprocessor(tokens1, settingsDefault, *this, Path::identify(tokens1.getFiles()[0], false));
161-
simplecpp::TokenList tokensP = preprocessor.preprocess("", files, true);
161+
simplecpp::OutputList outputList;
162+
simplecpp::TokenList tokensP = preprocessor.preprocess("", files, outputList);
163+
Preprocessor::throwFirstError(outputList);
162164
TokenList tokenlist(settingsDefault, Standards::Language::C); // headers are treated as C files
163165
tokenlist.createTokens(std::move(tokensP)); // do not assert
164166
}

0 commit comments

Comments
 (0)