Skip to content

Commit 1984d6f

Browse files
committed
Fix #10039 (integrate --check-config include findings with normal analysis)
1 parent ca9747c commit 1984d6f

6 files changed

Lines changed: 9 additions & 99 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,8 +1077,7 @@ void CmdLineParser::printHelp()
10771077
" to only enable this when the whole program is\n"
10781078
" scanned.\n"
10791079
" * missingInclude\n"
1080-
" Warn if there are missing includes. For\n"
1081-
" detailed information, use '--check-config'.\n"
1080+
" Warn if there are missing includes.\n"
10821081
" Several ids can be given if you separate them with\n"
10831082
" commas. See also --std\n"
10841083
" --error-exitcode=<n> If errors are found, integer [n] is returned instead of\n"

cli/cppcheckexecutor.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
200200

201201
int CppCheckExecutor::check(int argc, const char* const argv[])
202202
{
203-
Preprocessor::missingIncludeFlag = false;
204-
Preprocessor::missingSystemIncludeFlag = false;
205-
206203
CheckUnusedFunctions::clear();
207204

208205
CppCheck cppCheck(*this, true, executeCommand);
@@ -388,29 +385,6 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
388385

389386
if (!settings.checkConfiguration) {
390387
cppcheck.tooManyConfigsError(emptyString,0U);
391-
392-
if (settings.checks.isEnabled(Checks::missingInclude) && (Preprocessor::missingIncludeFlag || Preprocessor::missingSystemIncludeFlag)) {
393-
const std::list<ErrorMessage::FileLocation> callStack;
394-
ErrorMessage msg(callStack,
395-
emptyString,
396-
Severity::information,
397-
"Cppcheck cannot find all the include files (use --check-config for details)\n"
398-
"Cppcheck cannot find all the include files. Cppcheck can check the code without the "
399-
"include files found. But the results will probably be more accurate if all the include "
400-
"files are found. Please check your project's include directories and add all of them "
401-
"as include directories for Cppcheck. To see what files Cppcheck cannot find use "
402-
"--check-config.",
403-
"",
404-
Certainty::normal);
405-
if (Preprocessor::missingIncludeFlag) {
406-
msg.id = "missingInclude";
407-
reportInfo(msg);
408-
}
409-
if (Preprocessor::missingSystemIncludeFlag) {
410-
msg.id = "missingIncludeSystem";
411-
reportInfo(msg);
412-
}
413-
}
414388
}
415389

416390
if (settings.xml) {

lib/preprocessor.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ Directive::Directive(std::string _file, const int _linenr, const std::string &_s
6262
str(trim(_str))
6363
{}
6464

65-
std::atomic<bool> Preprocessor::missingIncludeFlag;
66-
std::atomic<bool> Preprocessor::missingSystemIncludeFlag;
67-
6865
char Preprocessor::macroChar = char(1);
6966

7067
Preprocessor::Preprocessor(Settings& settings, ErrorLogger *errorLogger) : mSettings(settings), mErrorLogger(errorLogger)
@@ -840,26 +837,19 @@ void Preprocessor::error(const std::string &filename, unsigned int linenr, const
840837
// Report that include is missing
841838
void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType)
842839
{
843-
if (!mSettings.checks.isEnabled(Checks::missingInclude) && !mSettings.checkConfiguration)
840+
if (!mSettings.checks.isEnabled(Checks::missingInclude))
844841
return;
845842

846843
const std::string fname = Path::fromNativeSeparators(filename);
844+
const std::string errorId = (headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude";
847845
Suppressions::ErrorMessage errorMessage;
848-
errorMessage.errorId = "missingInclude";
846+
errorMessage.errorId = errorId;
849847
errorMessage.setFileName(fname);
850848
errorMessage.lineNumber = linenr;
851849
if (mSettings.nomsg.isSuppressed(errorMessage))
852850
return;
853-
errorMessage.errorId = "missingIncludeSystem";
854-
if (headerType == SystemHeader && mSettings.nomsg.isSuppressed(errorMessage))
855-
return;
856-
857-
if (headerType == SystemHeader)
858-
missingSystemIncludeFlag = true;
859-
else
860-
missingIncludeFlag = true;
861851

862-
if (mErrorLogger && mSettings.checkConfiguration) {
852+
if (mErrorLogger) {
863853

864854
std::list<ErrorMessage::FileLocation> locationList;
865855
if (!filename.empty()) {
@@ -872,7 +862,7 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
872862
(headerType==SystemHeader) ?
873863
"Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results." :
874864
"Include file: \"" + header + "\" not found.",
875-
(headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude",
865+
errorId,
876866
Certainty::normal);
877867
mErrorLogger->reportInfo(errmsg);
878868
}
@@ -920,7 +910,6 @@ void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *se
920910
{
921911
Settings settings2(*settings);
922912
Preprocessor preprocessor(settings2, errorLogger);
923-
settings2.checkConfiguration = true;
924913
preprocessor.missingInclude(emptyString, 1, emptyString, UserHeader);
925914
preprocessor.missingInclude(emptyString, 1, emptyString, SystemHeader);
926915
preprocessor.validateCfgError(emptyString, 1, "X", "X");

lib/preprocessor.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ class CPPCHECKLIB Preprocessor {
8787
explicit Preprocessor(Settings& settings, ErrorLogger *errorLogger = nullptr);
8888
virtual ~Preprocessor();
8989

90-
static std::atomic<bool> missingIncludeFlag;
91-
static std::atomic<bool> missingSystemIncludeFlag;
92-
9390
void inlineSuppressions(const simplecpp::TokenList &tokens);
9491

9592
void setDirectives(const simplecpp::TokenList &tokens);

man/cppcheck.1.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Example: '-UDEBUG'</para>
283283
enables unusedFunction.</para></glossdef></glossentry><glossentry><glossterm>warning</glossterm><glossdef><para>Enable warning messages</para></glossdef></glossentry><glossentry><glossterm>style</glossterm><glossdef><para>Enable all coding style checks. All messages with the
284284
severities 'style', 'performance' and 'portability' are
285285
enabled.</para></glossdef></glossentry><glossentry><glossterm>performance</glossterm><glossdef><para>Enable performance messages</para></glossdef></glossentry><glossentry><glossterm>portability</glossterm><glossdef><para>Enable portability messages</para></glossdef></glossentry><glossentry><glossterm>information</glossterm><glossdef><para>Enable information messages</para></glossdef></glossentry><glossentry><glossterm>unusedFunction</glossterm><glossdef><para>Check for unused functions. It is recommend to only
286-
enable this when the whole program is scanned</para></glossdef></glossentry><glossentry><glossterm>missingInclude</glossterm><glossdef><para>Warn if there are missing includes. For detailed information use --check-config</para></glossdef></glossentry></glosslist>
286+
enable this when the whole program is scanned</para></glossdef></glossentry><glossentry><glossterm>missingInclude</glossterm><glossdef><para>Warn if there are missing includes</para></glossdef></glossentry></glosslist>
287287
By default none of the additional checks are enabled. Several ids can be given if you separate them with commas, e.g. --enable=style,unusedFunction. See also --std
288288
</para>
289289
</listitem>

test/testpreprocessor.cpp

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ class TestPreprocessor : public TestFixture {
259259
TEST_CASE(testDirectiveIncludeComments);
260260

261261
TEST_CASE(testMissingInclude);
262-
TEST_CASE(testMissingIncludeCheckConfig);
263262
}
264263

265264
void preprocess(const char* code, std::map<std::string, std::string>& actual, const char filename[] = "file.c") {
@@ -1937,16 +1936,11 @@ class TestPreprocessor : public TestFixture {
19371936
preprocess("#define () {(int f(x) }\n", actual); // don't hang
19381937
}
19391938

1940-
void inline_suppression_for_missing_include_internal(bool checkConfig) {
1941-
Preprocessor::missingIncludeFlag = false;
1942-
Preprocessor::missingSystemIncludeFlag = false;
1939+
void inline_suppression_for_missing_include_internal() {
19431940
Settings settings;
1944-
settings.checkConfiguration = checkConfig;
19451941
settings.inlineSuppressions = true;
19461942
settings.severity.clear();
1947-
// --check-config needs to report this regardless of the emanled checks
1948-
if (!checkConfig)
1949-
settings.checks.enable(Checks::missingInclude);
1943+
settings.checks.enable(Checks::missingInclude);
19501944
Preprocessor preprocessor(settings, this);
19511945

19521946
const std::string code("// cppcheck-suppress missingInclude\n"
@@ -1957,8 +1951,6 @@ class TestPreprocessor : public TestFixture {
19571951
errout.str("");
19581952
preprocessor.getcode(code, "", "test.c");
19591953
ASSERT_EQUALS("", errout.str());
1960-
ASSERT_EQUALS(false, Preprocessor::missingIncludeFlag);
1961-
ASSERT_EQUALS(false, Preprocessor::missingSystemIncludeFlag);
19621954

19631955
auto suppressions = settings.nomsg.getSuppressions();
19641956
ASSERT_EQUALS(2, suppressions.size());
@@ -1984,10 +1976,6 @@ class TestPreprocessor : public TestFixture {
19841976
inline_suppression_for_missing_include_internal(false);
19851977
}
19861978

1987-
void inline_suppression_for_missing_include_check_config() {
1988-
inline_suppression_for_missing_include_internal(true);
1989-
}
1990-
19911979
void predefine1() {
19921980
const std::string src("#if defined X || Y\n"
19931981
"Fred & Wilma\n"
@@ -2433,9 +2421,6 @@ class TestPreprocessor : public TestFixture {
24332421
}
24342422

24352423
void testMissingInclude() {
2436-
Preprocessor::missingIncludeFlag = false;
2437-
Preprocessor::missingSystemIncludeFlag = false;
2438-
24392424
Settings settings;
24402425
settings.severity.clear();
24412426
settings.checks.enable(Checks::missingInclude);
@@ -2450,43 +2435,9 @@ class TestPreprocessor : public TestFixture {
24502435
"#include \"header2.h\"");
24512436
errout.str("");
24522437
preprocessor.getcode(code, "", "test.c");
2453-
ASSERT_EQUALS(true, Preprocessor::missingIncludeFlag);
2454-
ASSERT_EQUALS(true, Preprocessor::missingSystemIncludeFlag);
24552438

24562439
// the expected messages are emited outside of the Preprocessor
24572440
ASSERT_EQUALS("", errout.str());
2458-
2459-
Preprocessor::missingIncludeFlag = false;
2460-
Preprocessor::missingSystemIncludeFlag = false;
2461-
}
2462-
2463-
void testMissingIncludeCheckConfig() {
2464-
Preprocessor::missingIncludeFlag = false;
2465-
Preprocessor::missingSystemIncludeFlag = false;
2466-
2467-
Settings settings;
2468-
settings.checkConfiguration = true;
2469-
settings.severity.clear();
2470-
// needs to be reported regardless of enabled checks
2471-
Preprocessor preprocessor(settings, this);
2472-
2473-
ScopedFile header("header.h", "");
2474-
ScopedFile header2("header2.h", "");
2475-
2476-
std::string code("#include \"missing.h\"\n"
2477-
"#include <header.h>\n"
2478-
"#include <missing2.h>\n"
2479-
"#include \"header2.h\"");
2480-
errout.str("");
2481-
preprocessor.getcode(code, "", "test.c");
2482-
ASSERT_EQUALS(true, Preprocessor::missingIncludeFlag);
2483-
ASSERT_EQUALS(true, Preprocessor::missingSystemIncludeFlag);
2484-
2485-
ASSERT_EQUALS("[test.c:1]: (information) Include file: \"missing.h\" not found.\n"
2486-
"[test.c:3]: (information) Include file: <missing2.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.\n", errout.str());
2487-
2488-
Preprocessor::missingIncludeFlag = false;
2489-
Preprocessor::missingSystemIncludeFlag = false;
24902441
}
24912442
};
24922443

0 commit comments

Comments
 (0)