Skip to content

Commit a9f76b7

Browse files
authored
Fix #13889 (cached misra results are shown after removing --premium=misra-c-2012 option) (danmar#7594)
Include addon info and premium args in analyzerinfo hash function, fixes #13889.
1 parent 3181f13 commit a9f76b7

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ test/testcondition.o: test/testcondition.cpp lib/addoninfo.h lib/check.h lib/che
752752
test/testconstructors.o: test/testconstructors.cpp lib/addoninfo.h lib/check.h lib/checkclass.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
753753
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testconstructors.cpp
754754

755-
test/testcppcheck.o: test/testcppcheck.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
755+
test/testcppcheck.o: test/testcppcheck.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
756756
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testcppcheck.cpp
757757

758758
test/testerrorlogger.o: test/testerrorlogger.cpp externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/xml.h test/fixture.h test/helpers.h

lib/cppcheck.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -850,19 +850,24 @@ static simplecpp::TokenList createTokenList(const std::string& filename, std::ve
850850
return {filename, files, outputList};
851851
}
852852

853-
static std::size_t calculateHash(const Preprocessor& preprocessor, const simplecpp::TokenList& tokens, const Settings& settings, const Suppressions& supprs)
853+
std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const simplecpp::TokenList& tokens) const
854854
{
855855
std::ostringstream toolinfo;
856-
toolinfo << (settings.cppcheckCfgProductName.empty() ? CPPCHECK_VERSION_STRING : settings.cppcheckCfgProductName);
857-
toolinfo << (settings.severity.isEnabled(Severity::warning) ? 'w' : ' ');
858-
toolinfo << (settings.severity.isEnabled(Severity::style) ? 's' : ' ');
859-
toolinfo << (settings.severity.isEnabled(Severity::performance) ? 'p' : ' ');
860-
toolinfo << (settings.severity.isEnabled(Severity::portability) ? 'p' : ' ');
861-
toolinfo << (settings.severity.isEnabled(Severity::information) ? 'i' : ' ');
862-
toolinfo << settings.userDefines;
863-
toolinfo << std::to_string(static_cast<std::uint8_t>(settings.checkLevel));
856+
toolinfo << (mSettings.cppcheckCfgProductName.empty() ? CPPCHECK_VERSION_STRING : mSettings.cppcheckCfgProductName);
857+
toolinfo << (mSettings.severity.isEnabled(Severity::warning) ? 'w' : ' ');
858+
toolinfo << (mSettings.severity.isEnabled(Severity::style) ? 's' : ' ');
859+
toolinfo << (mSettings.severity.isEnabled(Severity::performance) ? 'p' : ' ');
860+
toolinfo << (mSettings.severity.isEnabled(Severity::portability) ? 'p' : ' ');
861+
toolinfo << (mSettings.severity.isEnabled(Severity::information) ? 'i' : ' ');
862+
toolinfo << mSettings.userDefines;
863+
toolinfo << std::to_string(static_cast<std::uint8_t>(mSettings.checkLevel));
864+
for (const auto &a : mSettings.addonInfos) {
865+
toolinfo << a.name;
866+
toolinfo << a.args;
867+
}
868+
toolinfo << mSettings.premiumArgs;
864869
// TODO: do we need to add more options?
865-
supprs.nomsg.dump(toolinfo);
870+
mSuppressions.nomsg.dump(toolinfo);
866871
return preprocessor.calculateHash(tokens, toolinfo.str());
867872
}
868873

@@ -922,7 +927,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
922927
simplecpp::TokenList tokens(*fileStream, files, file.spath());
923928
if (analyzerInformation) {
924929
const Preprocessor preprocessor(mSettings, mErrorLogger, Standards::Language::C);
925-
hash = calculateHash(preprocessor, tokens, mSettings, mSuppressions);
930+
hash = calculateHash(preprocessor, tokens);
926931
}
927932
tokenlist.createTokens(std::move(tokens));
928933
}
@@ -931,7 +936,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
931936
simplecpp::TokenList tokens(file.spath(), files);
932937
if (analyzerInformation) {
933938
const Preprocessor preprocessor(mSettings, mErrorLogger, file.lang());
934-
hash = calculateHash(preprocessor, tokens, mSettings, mSuppressions);
939+
hash = calculateHash(preprocessor, tokens);
935940
}
936941
tokenlist.createTokens(std::move(tokens));
937942
}
@@ -1015,7 +1020,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
10151020

10161021
if (analyzerInformation) {
10171022
// Calculate hash so it can be compared with old hash / future hashes
1018-
const std::size_t hash = calculateHash(preprocessor, tokens1, mSettings, mSuppressions);
1023+
const std::size_t hash = calculateHash(preprocessor, tokens1);
10191024
std::list<ErrorMessage> errors;
10201025
if (!analyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, fileIndex, hash, errors)) {
10211026
while (!errors.empty()) {

lib/cppcheck.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class AnalyzerInformation;
4242
class ErrorLogger;
4343
class Settings;
4444
struct Suppressions;
45+
class Preprocessor;
4546

4647
namespace simplecpp { class TokenList; }
4748

@@ -162,6 +163,15 @@ class CPPCHECKLIB CppCheck {
162163
/** @brief There has been an internal error => Report information message */
163164
void internalError(const std::string &filename, const std::string &msg);
164165

166+
/**
167+
* @brief Calculate hash used to detect when a file needs to be reanalyzed.
168+
*
169+
* @param preprocessor Preprocessor used to calculate the hash.
170+
* @param tokens Token list from preprocessed file.
171+
* @return hash
172+
*/
173+
std::size_t calculateHash(const Preprocessor &preprocessor, const simplecpp::TokenList &tokens) const;
174+
165175
/**
166176
* @brief Check a file using stream
167177
* @param file the file

test/testcppcheck.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "fixture.h"
2424
#include "helpers.h"
2525
#include "path.h"
26+
#include "preprocessor.h"
2627
#include "settings.h"
2728
#include "suppressions.h"
2829

@@ -68,6 +69,7 @@ class TestCppcheck : public TestFixture {
6869
TEST_CASE(isPremiumCodingStandardId);
6970
TEST_CASE(getDumpFileContentsRawTokens);
7071
TEST_CASE(getDumpFileContentsLibrary);
72+
TEST_CASE(premiumResultsCache);
7173
}
7274

7375
void getErrorMessages() const {
@@ -327,6 +329,40 @@ class TestCppcheck : public TestFixture {
327329
}
328330
}
329331

332+
void premiumResultsCache() const {
333+
// Trac #13889 - cached misra results are shown after removing --premium=misra-c-2012 option
334+
335+
Settings settings;
336+
Suppressions supprs;
337+
ErrorLogger2 errorLogger;
338+
339+
std::vector<std::string> files;
340+
341+
std::istringstream istr("void f();\nint x;\n");
342+
const simplecpp::TokenList tokens(istr, files, "m1.c");
343+
344+
Preprocessor preprocessor(settings, errorLogger, Standards::Language::C);
345+
ASSERT(preprocessor.loadFiles(tokens, files));
346+
347+
AddonInfo premiumaddon;
348+
premiumaddon.name = "premiumaddon.json";
349+
premiumaddon.executable = "premiumaddon";
350+
351+
settings.cppcheckCfgProductName = "Cppcheck Premium 0.0.0";
352+
settings.addons.insert(premiumaddon.name);
353+
settings.addonInfos.push_back(premiumaddon);
354+
355+
settings.premiumArgs = "misra-c-2012";
356+
CppCheck check(settings, supprs, errorLogger, false, {});
357+
const size_t hash1 = check.calculateHash(preprocessor, tokens);
358+
359+
settings.premiumArgs = "";
360+
const size_t hash2 = check.calculateHash(preprocessor, tokens);
361+
362+
// cppcheck-suppress knownConditionTrueFalse
363+
ASSERT(hash1 != hash2);
364+
}
365+
330366
// TODO: test suppressions
331367
// TODO: test all with FS
332368
};

0 commit comments

Comments
 (0)