Skip to content

Commit 1322092

Browse files
committed
Add test
1 parent 5ff2826 commit 1322092

3 files changed

Lines changed: 77 additions & 15 deletions

File tree

lib/cppcheck.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -850,24 +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)
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));
864-
for (const auto &a : settings.addonInfos) {
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) {
865865
toolinfo << a.name;
866866
toolinfo << a.args;
867867
}
868-
toolinfo << settings.premiumArgs;
868+
toolinfo << mSettings.premiumArgs;
869869
// TODO: do we need to add more options?
870-
supprs.nomsg.dump(toolinfo);
870+
mSuppressions.nomsg.dump(toolinfo);
871871
return preprocessor.calculateHash(tokens, toolinfo.str());
872872
}
873873

@@ -927,7 +927,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
927927
simplecpp::TokenList tokens(*fileStream, files, file.spath());
928928
if (analyzerInformation) {
929929
const Preprocessor preprocessor(mSettings, mErrorLogger, Standards::Language::C);
930-
hash = calculateHash(preprocessor, tokens, mSettings, mSuppressions);
930+
hash = calculateHash(preprocessor, tokens);
931931
}
932932
tokenlist.createTokens(std::move(tokens));
933933
}
@@ -936,7 +936,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
936936
simplecpp::TokenList tokens(file.spath(), files);
937937
if (analyzerInformation) {
938938
const Preprocessor preprocessor(mSettings, mErrorLogger, file.lang());
939-
hash = calculateHash(preprocessor, tokens, mSettings, mSuppressions);
939+
hash = calculateHash(preprocessor, tokens);
940940
}
941941
tokenlist.createTokens(std::move(tokens));
942942
}
@@ -1020,7 +1020,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
10201020

10211021
if (analyzerInformation) {
10221022
// Calculate hash so it can be compared with old hash / future hashes
1023-
const std::size_t hash = calculateHash(preprocessor, tokens1, mSettings, mSuppressions);
1023+
const std::size_t hash = calculateHash(preprocessor, tokens1);
10241024
std::list<ErrorMessage> errors;
10251025
if (!analyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, fileIndex, hash, errors)) {
10261026
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 toolinfo Token list from preprocessed file.
171+
* @return hash
172+
*/
173+
std::size_t calculateHash(const Preprocessor &preprocessor, const simplecpp::TokenList &tokens);
174+
165175
/**
166176
* @brief Check a file using stream
167177
* @param file the file

test/testcppcheck.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
#include "cmdlineparser.h"
1920
#include "color.h"
2021
#include "cppcheck.h"
2122
#include "errorlogger.h"
2223
#include "filesettings.h"
2324
#include "fixture.h"
2425
#include "helpers.h"
2526
#include "path.h"
27+
#include "preprocessor.h"
2628
#include "settings.h"
2729
#include "suppressions.h"
2830

@@ -58,6 +60,22 @@ class TestCppcheck : public TestFixture {
5860
}
5961
};
6062

63+
class NullCmdLineLogger : public CmdLineLogger
64+
{
65+
public:
66+
void printMessage(const std::string &) override
67+
{
68+
}
69+
70+
void printError(const std::string &) override
71+
{
72+
}
73+
74+
void printRaw(const std::string &) override
75+
{
76+
}
77+
};
78+
6179
void run() override {
6280
TEST_CASE(getErrorMessages);
6381
TEST_CASE(checkWithFile);
@@ -68,6 +86,7 @@ class TestCppcheck : public TestFixture {
6886
TEST_CASE(isPremiumCodingStandardId);
6987
TEST_CASE(getDumpFileContentsRawTokens);
7088
TEST_CASE(getDumpFileContentsLibrary);
89+
TEST_CASE(premiumResultsCache);
7190
}
7291

7392
void getErrorMessages() const {
@@ -324,6 +343,39 @@ class TestCppcheck : public TestFixture {
324343
}
325344
}
326345

346+
void premiumResultsCache() const {
347+
// Trac #13889 - cached misra results are shown after removing --premium=misra-c-2012 option
348+
349+
Settings settings;
350+
Suppressions supprs;
351+
ErrorLogger2 errorLogger;
352+
353+
std::vector<std::string> files;
354+
355+
std::istringstream istr("void f();\nint x;\n");
356+
simplecpp::TokenList tokens(istr, files, "m1.c");
357+
358+
Preprocessor preprocessor(settings, errorLogger, Standards::Language::C);
359+
ASSERT(preprocessor.loadFiles(tokens, files));
360+
361+
AddonInfo premiumaddon;
362+
premiumaddon.name = "premiumaddon.json";
363+
premiumaddon.executable = "premiumaddon";
364+
365+
settings.cppcheckCfgProductName = "Cppcheck Premium 0.0.0";
366+
settings.addons.insert(premiumaddon.name);
367+
settings.addonInfos.push_back(premiumaddon);
368+
369+
settings.premiumArgs = "misra-c-2012";
370+
CppCheck check(settings, supprs, errorLogger, false, {});
371+
size_t hash1 = check.calculateHash(preprocessor, tokens);
372+
373+
settings.premiumArgs = "";
374+
size_t hash2 = check.calculateHash(preprocessor, tokens);
375+
376+
ASSERT(hash1 != hash2);
377+
}
378+
327379
// TODO: test suppressions
328380
// TODO: test all with FS
329381
};

0 commit comments

Comments
 (0)