Skip to content

Commit 1bf054e

Browse files
committed
got rid of duplicated error reporting code in CppCheck::checkInternal() / cleanups
1 parent 70dc246 commit 1bf054e

File tree

6 files changed

+22
-47
lines changed

6 files changed

+22
-47
lines changed

lib/cppcheck.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -977,30 +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-
// If there is a syntax error, report it and stop
981-
const auto output_it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
982-
return Preprocessor::hasErrors(output);
983-
});
984-
if (output_it != outputList.cend()) {
985-
const simplecpp::Output &output = *output_it;
986-
std::string locfile = Path::fromNativeSeparators(output.location.file());
987-
if (mSettings.relativePaths)
988-
locfile = Path::getRelativePath(locfile, mSettings.basePaths);
989-
990-
ErrorMessage::FileLocation loc1(locfile, output.location.line, output.location.col);
991-
992-
ErrorMessage errmsg({std::move(loc1)},
993-
"", // TODO: is this correct?
994-
Severity::error,
995-
output.msg,
996-
"syntaxError",
997-
Certainty::normal);
998-
mErrorLogger.reportErr(errmsg);
999-
return mLogger->exitcode();
1000-
}
1001-
1002980
Preprocessor preprocessor(tokens1, mSettings, mErrorLogger, file.lang());
1003981

982+
if (preprocessor.reportOutput(outputList, true))
983+
return mLogger->exitcode();
984+
1004985
if (!preprocessor.loadFiles(files))
1005986
return mLogger->exitcode();
1006987

lib/preprocessor.h

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

143-
static bool hasErrors(const simplecpp::Output &output);
144-
145-
protected:
146143
bool reportOutput(const simplecpp::OutputList &outputList, bool showerror);
147144

148145
private:
146+
static bool hasErrors(const simplecpp::Output &output);
147+
149148
bool handleErrors(const simplecpp::OutputList &outputList, bool throwError);
150149

151150
static void simplifyPragmaAsmPrivate(simplecpp::TokenList &tokenList);

test/cli/other_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3888,8 +3888,9 @@ def test_simplecpp_unhandled_char(tmp_path):
38883888
assert exitcode == 0, stdout
38893889
assert stdout.splitlines() == []
38903890
assert stderr.splitlines() == [
3891+
# TODO: lacks column information
38913892
# TODO: should report another ID
3892-
'{}:2:5: error: The code contains unhandled character(s) (character code=228). Neither unicode nor extended ascii is supported. [syntaxError]'.format(test_file)
3893+
'{}:2:0: error: The code contains unhandled character(s) (character code=228). Neither unicode nor extended ascii is supported. [preprocessorErrorDirective]'.format(test_file)
38933894
]
38943895

38953896

test/cli/project_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ def test_json_entry_file_not_found(tmpdir):
5959
with open(project_file, 'w') as f:
6060
f.write(json.dumps(compilation_db))
6161

62-
ret, _, stderr = cppcheck(["--project=" + str(project_file)])
62+
ret, _, stderr = cppcheck([
63+
'--template=simple',
64+
"--project=" + str(project_file)
65+
])
6366
assert 0 == ret
64-
assert f"{missing_file}:1:0: error: File is missing: {missing_file_posix} [syntaxError]\n\n^\n" == stderr
67+
assert stderr == f"nofile:0:0: error: File is missing: {missing_file_posix} [preprocessorErrorDirective]\n"
6568

6669

6770
def test_json_no_arguments(tmpdir):

test/testpreprocessor.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,12 @@ class TestPreprocessor : public TestFixture {
4949
TestPreprocessor() : TestFixture("TestPreprocessor") {}
5050

5151
private:
52-
class PreprocessorTest : public Preprocessor
53-
{
54-
friend class TestPreprocessor;
55-
public:
56-
PreprocessorTest(simplecpp::TokenList& tokens, const Settings& settings, ErrorLogger &errorLogger, Standards::Language lang)
57-
: Preprocessor(tokens, settings, errorLogger, lang)
58-
{}
59-
};
60-
6152
template<size_t size>
6253
std::string expandMacros(const char (&code)[size], ErrorLogger &errorLogger) const {
6354
simplecpp::OutputList outputList;
6455
std::vector<std::string> files;
6556
simplecpp::TokenList tokens1 = simplecpp::TokenList(code, files, "file.cpp", &outputList);
66-
PreprocessorTest p(tokens1, settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
57+
Preprocessor p(tokens1, settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
6758
simplecpp::TokenList tokens2 = p.preprocess("", files, true);
6859
(void)p.reportOutput(outputList, true);
6960
return tokens2.stringify();
@@ -128,7 +119,7 @@ class TestPreprocessor : public TestFixture {
128119

129120
simplecpp::TokenList tokens(code, size, files, Path::simplifyPath(filename), &outputList);
130121
// TODO: we should be using the actual Preprocessor implementation
131-
PreprocessorTest preprocessor(tokens, settings, errorlogger, Path::identify(tokens.getFiles()[0], false));
122+
Preprocessor preprocessor(tokens, settings, errorlogger, Path::identify(tokens.getFiles()[0], false));
132123

133124
// TODO: should be possible without a Preprocessor instance
134125
if (preprocessor.reportOutput(outputList, true))

test/testsuppressions.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class TestSuppressions : public TestFixture {
8888
TEST_CASE(suppressingSyntaxErrorsFS); // #7076
8989
TEST_CASE(suppressingSyntaxErrorsInlineFiles); // #5917
9090
TEST_CASE(suppressingSyntaxErrorsInlineFS); // #5917
91-
TEST_CASE(suppressingSyntaxErrorsWhileFileReadFiles); // PR #1333
92-
TEST_CASE(suppressingSyntaxErrorsWhileFileReadFS); // PR #1333
91+
TEST_CASE(suppressingSimplecppErrorsWhileFileReadFiles); // PR #1333
92+
TEST_CASE(suppressingSimplecppErrorsWhileFileReadFS); // PR #1333
9393
TEST_CASE(symbol);
9494

9595
TEST_CASE(unusedFunctionFiles);
@@ -1341,7 +1341,7 @@ class TestSuppressions : public TestFixture {
13411341
suppressingSyntaxErrorsInlineInternal(&TestSuppressions::checkSuppressionFS);
13421342
}
13431343

1344-
void suppressingSyntaxErrorsWhileFileReadInternal(unsigned int (TestSuppressions::*check)(const char[], const std::string &)) { // syntaxError while file read should be suppressible (PR #1333)
1344+
void suppressingSimplecppErrorsWhileFileReadInternal(unsigned int (TestSuppressions::*check)(const char[], const std::string &)) { // syntaxError while file read should be suppressible (PR #1333)
13451345
const char code[] = "CONST (genType, KS_CONST) genService[KS_CFG_NR_OF_NVM_BLOCKS] =\n"
13461346
"{\n"
13471347
"[!VAR \"BC\" = \"$BC + 1\"!][!//\n"
@@ -1355,16 +1355,16 @@ class TestSuppressions : public TestFixture {
13551355
"[!VAR \"BC\" = \"$BC + 1\"!][!//\n"
13561356
"[!ENDIF!][!//\n"
13571357
"};";
1358-
ASSERT_EQUALS(0, (this->*check)(code, "syntaxError:test.cpp:4"));
1358+
ASSERT_EQUALS(0, (this->*check)(code, "preprocessorErrorDirective:test.cpp:4"));
13591359
ASSERT_EQUALS("", errout_str());
13601360
}
13611361

1362-
void suppressingSyntaxErrorsWhileFileReadFiles() {
1363-
suppressingSyntaxErrorsWhileFileReadInternal(&TestSuppressions::checkSuppressionFiles);
1362+
void suppressingSimplecppErrorsWhileFileReadFiles() {
1363+
suppressingSimplecppErrorsWhileFileReadInternal(&TestSuppressions::checkSuppressionFiles);
13641364
}
13651365

1366-
void suppressingSyntaxErrorsWhileFileReadFS() {
1367-
suppressingSyntaxErrorsWhileFileReadInternal(&TestSuppressions::checkSuppressionFiles);
1366+
void suppressingSimplecppErrorsWhileFileReadFS() {
1367+
suppressingSimplecppErrorsWhileFileReadInternal(&TestSuppressions::checkSuppressionFiles);
13681368
}
13691369

13701370
// TODO: this tests an internal function - should it be private?

0 commit comments

Comments
 (0)