Skip to content

Commit 1557546

Browse files
committed
fixed #14275 - map simplecpp errors to individual IDs
1 parent 6bd4d6e commit 1557546

File tree

5 files changed

+42
-24
lines changed

5 files changed

+42
-24
lines changed

lib/cppcheck.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,19 +1213,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
12131213

12141214
if (!hasValidConfig && currCfg == *configurations.rbegin()) {
12151215
// 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);
1216+
preprocessor.error(file.spath(), o.location.line, o.msg, o.type);
12291217
}
12301218
continue;
12311219

lib/errorlogger.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
const std::set<std::string> ErrorLogger::mCriticalErrorIds{
4646
"cppcheckError",
4747
"cppcheckLimit",
48+
"includeNestedTooDeeply",
4849
"internalAstError",
4950
"instantiationError",
5051
"internalError",
@@ -53,6 +54,7 @@ const std::set<std::string> ErrorLogger::mCriticalErrorIds{
5354
"premium-invalidLicense",
5455
"preprocessorErrorDirective",
5556
"syntaxError",
57+
"unhandledChar",
5658
"unknownMacro"
5759
};
5860

lib/preprocessor.cpp

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void Preprocessor::inlineSuppressions(SuppressionList &suppressions)
312312
::addInlineSuppressions(filedata->tokens, mSettings, suppressions, err);
313313
}
314314
for (const BadInlineSuppression &bad : err) {
315-
error(bad.file, bad.line, bad.errmsg);
315+
error(bad.file, bad.line, bad.errmsg, simplecpp::Output::ERROR); // TODO: use individual (non-fatal) ID
316316
}
317317
}
318318

@@ -860,7 +860,7 @@ bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh
860860
case simplecpp::Output::ERROR:
861861
hasError = true;
862862
if (!startsWith(out.msg,"#error") || showerror)
863-
error(out.location.file(), out.location.line, out.msg);
863+
error(out.location.file(), out.location.line, out.msg, out.type);
864864
break;
865865
case simplecpp::Output::WARNING:
866866
case simplecpp::Output::PORTABILITY_BACKSLASH:
@@ -877,35 +877,59 @@ bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh
877877
case simplecpp::Output::SYNTAX_ERROR:
878878
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
879879
hasError = true;
880-
error(out.location.file(), out.location.line, out.msg);
880+
error(out.location.file(), out.location.line, out.msg, out.type);
881881
break;
882882
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
883883
case simplecpp::Output::FILE_NOT_FOUND:
884884
case simplecpp::Output::DUI_ERROR:
885885
hasError = true;
886-
error("", 0, out.msg);
886+
error("", 0, out.msg, out.type);
887887
break;
888888
}
889889
}
890890

891891
return hasError;
892892
}
893893

894-
void Preprocessor::error(const std::string &filename, unsigned int linenr, const std::string &msg)
894+
static std::string simplecppErrToId(simplecpp::Output::Type type)
895+
{
896+
switch (type) {
897+
case simplecpp::Output::ERROR:
898+
return "preprocessorErrorDirective";
899+
case simplecpp::Output::SYNTAX_ERROR:
900+
return "syntaxError";
901+
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
902+
return "unhandledChar";
903+
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
904+
return "includeNestedTooDeeply";
905+
// should never occur
906+
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
907+
case simplecpp::Output::FILE_NOT_FOUND:
908+
case simplecpp::Output::DUI_ERROR:
909+
// handled separately
910+
case simplecpp::Output::MISSING_HEADER:
911+
// no handled at all (warnings)
912+
case simplecpp::Output::WARNING:
913+
case simplecpp::Output::PORTABILITY_BACKSLASH:
914+
throw std::runtime_error("unexpected simplecpp::Output type");
915+
}
916+
}
917+
918+
void Preprocessor::error(const std::string &filename, unsigned int linenr, const std::string &msg, simplecpp::Output::Type type)
895919
{
896920
std::list<ErrorMessage::FileLocation> locationList;
897921
if (!filename.empty()) {
898922
std::string file = Path::fromNativeSeparators(filename);
899923
if (mSettings.relativePaths)
900924
file = Path::getRelativePath(file, mSettings.basePaths);
901925

902-
locationList.emplace_back(file, linenr, 0);
926+
locationList.emplace_back(file, linenr, 0); // TODO: set column
903927
}
904928
mErrorLogger.reportErr(ErrorMessage(std::move(locationList),
905929
mFile0,
906930
Severity::error,
907931
msg,
908-
"preprocessorErrorDirective",
932+
simplecppErrToId(type),
909933
Certainty::normal));
910934
}
911935

@@ -935,7 +959,10 @@ void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &se
935959
Preprocessor preprocessor(tokens, settings, errorLogger, Standards::Language::CPP);
936960
preprocessor.missingInclude("", 1, 2, "", UserHeader);
937961
preprocessor.missingInclude("", 1, 2, "", SystemHeader);
938-
preprocessor.error("", 1, "#error message"); // #error ..
962+
preprocessor.error("", 1, "message", simplecpp::Output::ERROR);
963+
preprocessor.error("", 1, "message", simplecpp::Output::SYNTAX_ERROR);
964+
preprocessor.error("", 1, "message", simplecpp::Output::UNHANDLED_CHAR_ERROR);
965+
preprocessor.error("", 1, "message", simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY);
939966
}
940967

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

lib/preprocessor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
142142

143143
bool reportOutput(const simplecpp::OutputList &outputList, bool showerror);
144144

145+
void error(const std::string &filename, unsigned int linenr, const std::string &msg, simplecpp::Output::Type type);
146+
145147
private:
146148
static bool hasErrors(const simplecpp::Output &output);
147149

@@ -158,7 +160,6 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
158160
};
159161

160162
void missingInclude(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &header, HeaderTypes headerType);
161-
void error(const std::string &filename, unsigned int linenr, const std::string &msg);
162163

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

@@ -172,7 +173,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
172173
simplecpp::FileDataCache mFileCache;
173174

174175
/** filename for cpp/c file - useful when reporting errors */
175-
std::string mFile0;
176+
std::string mFile0; // TODO: this is never set
176177
Standards::Language mLang{Standards::Language::None};
177178

178179
/** simplecpp tracking info */

test/testsuppressions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ class TestSuppressions : public TestFixture {
13551355
"[!VAR \"BC\" = \"$BC + 1\"!][!//\n"
13561356
"[!ENDIF!][!//\n"
13571357
"};";
1358-
ASSERT_EQUALS(0, (this->*check)(code, "preprocessorErrorDirective:test.cpp:4"));
1358+
ASSERT_EQUALS(0, (this->*check)(code, "syntaxError:test.cpp:4"));
13591359
ASSERT_EQUALS("", errout_str());
13601360
}
13611361

0 commit comments

Comments
 (0)