-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add feature: Cache check results for future runs: #799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [submodule "externals/cryptopp"] | ||
| path = externals/cryptopp | ||
| url = git@github.com:nirbar/cryptopp.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,301 @@ | ||
| #include "cache.h" | ||
| #include "errorlogger.h" | ||
| #include <cryptopp/sha3.h> | ||
| #include <cryptopp/base64.h> | ||
| #include <cryptopp/files.h> | ||
| #include <fstream> | ||
| using namespace std; | ||
| using namespace tinyxml2; | ||
| using namespace CryptoPP; | ||
|
|
||
| Cache::Cache() | ||
| { | ||
| } | ||
|
|
||
| Cache::Cache(const Cache & other) | ||
| { | ||
| *this = other; | ||
| } | ||
|
|
||
| Cache & Cache::operator=(const Cache & other) | ||
| { | ||
| Clear(); | ||
| if (!_cacheFile.empty()) | ||
| { | ||
| Load(other._cacheFile); | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| int Cache::Load(const string& file) | ||
| { | ||
| XMLError er = XML_SUCCESS; | ||
|
|
||
| if (!_cacheFile.empty()) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| _cacheFile = file; | ||
| er = _cache.LoadFile(file.c_str()); | ||
|
|
||
| return((er == XML_SUCCESS) || (er == XML_NO_ERROR)) ? 0 : (er | XML_SUCCESS); | ||
| } | ||
|
|
||
| int Cache::LoadFromCache(const char* filePath, const char* configuration, size_t* pCachedSize, std::string* pHash, const tinyxml2::XMLElement** ppElem) const | ||
| { | ||
| const XMLElement *elem = NULL; | ||
| const char* sizeStr = NULL; | ||
| const char* hashStr = NULL; | ||
| size_t tmpSize = 0; | ||
| string path; | ||
| (*pCachedSize) = 0; | ||
| (*ppElem) = NULL; | ||
| pHash->clear(); | ||
|
|
||
| path = Normalize(filePath); | ||
|
|
||
| if ((Find(path.c_str(), configuration, &elem) != 0) || (elem == NULL)) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| // Parse size | ||
| if ((sizeStr = elem->Attribute("Size")) == NULL) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| if ((tmpSize = ::stoull(sizeStr, NULL)) == 0) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| // Hash | ||
| if ((hashStr = elem->Attribute("Hash")) == NULL) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| (*pCachedSize) = tmpSize; | ||
| (*pHash) = hashStr; | ||
| (*ppElem) = elem; | ||
| return 0; | ||
| } | ||
|
|
||
| int Cache::Find(const char* filePath, const char* configuration, XMLElement** ppElem) | ||
| { | ||
| return Find(filePath, configuration, (const XMLElement**)ppElem); | ||
| } | ||
|
|
||
| int Cache::Find(const char* filePath, const char* configuration, const XMLElement** ppElem) const | ||
| { | ||
| (*ppElem) = NULL; | ||
|
|
||
| string path = Normalize(filePath); | ||
| if (path.empty() || _cacheFile.empty()) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| const XMLElement* root = _cache.RootElement(); | ||
| if (root == NULL) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| for (const XMLElement* currElem = root->FirstChildElement("File"); currElem != NULL; currElem = currElem->NextSiblingElement("File")) | ||
| { | ||
| if (currElem->Attribute("Path", path.c_str()) && currElem->Attribute("Configuration", configuration)) | ||
| { | ||
| (*ppElem) = currElem; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return ((*ppElem) == NULL); | ||
| } | ||
|
|
||
| string Cache::CalcHash(const char* code) const | ||
| { | ||
| SHA3_512 sha; | ||
| string out; | ||
| Base64Encoder* enc = new Base64Encoder(new StringSink(out), false); | ||
|
|
||
| StringSource(code, true, new HashFilter(sha, enc)); | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| string Cache::Normalize(const char* filePath) const | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should use our Path class to deal with filepaths. There is for instance a Path::fromNativeSeparators() function that might be useful here. |
||
| { | ||
| size_t i; | ||
| string path(filePath); | ||
|
|
||
| while ((i = path.find('\\')) != string::npos) | ||
| { | ||
| path = path.replace(i, 1, "/"); | ||
| } | ||
| while ((i = path.find("//")) != string::npos) | ||
| { | ||
| path = path.replace(i, 2, "/"); | ||
| } | ||
| return path; | ||
| } | ||
|
|
||
| bool Cache::ReportCachedResults(const char* filePath, const char* configuration, const char* code, ErrorLogger* pLogger) const | ||
| { | ||
| string cachedHash; | ||
| size_t cachedSize = 0; | ||
| const XMLElement* pElem = NULL; | ||
|
|
||
| // Cached? | ||
| if ((LoadFromCache(filePath, configuration, &cachedSize, &cachedHash, &pElem) != 0) || (cachedSize == 0) || cachedHash.empty() || (pElem == NULL)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Compare size | ||
| size_t actualSize = strlen(code); | ||
| if (actualSize != cachedSize) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Compare hash | ||
| string actualHash = CalcHash(code); | ||
| if (actualHash.compare(cachedHash) != 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| for (const XMLElement* currChild = pElem->FirstChildElement("Report"); currChild != NULL; currChild = currChild->NextSiblingElement("Report")) | ||
| { | ||
| ErrorLogger::ErrorMessage msg; | ||
| string t = currChild->GetText(); | ||
| if (!msg.deserialize(t)) | ||
| { | ||
| return false; | ||
| } | ||
| if (msg._id.compare("syntaxError") != 0) | ||
| { | ||
| msg.file0 = filePath; | ||
| } | ||
| pLogger->reportErr(msg); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int Cache::CacheFile(const char* filePath, const char* configuration, const char* code, const std::list<std::string>& reports) | ||
| { | ||
| // Cache disabled | ||
| if (_cacheFile.empty()) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| string path = Normalize(filePath); | ||
| if (path.empty()) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| size_t actualSize = strlen(code); | ||
| if (actualSize == 0) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| string actualHash = CalcHash(code); | ||
| if (actualHash.empty()) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| XMLElement* pElem = NULL; | ||
| if ((Find(path.c_str(), configuration, &pElem) != 0) || (pElem == NULL)) | ||
| { | ||
| // Create element | ||
| XMLElement* root = _cache.RootElement(); | ||
| if (root == NULL) | ||
| { | ||
| root = _cache.NewElement("CppCheckCache"); | ||
| if (root == NULL) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| if (_cache.InsertFirstChild(root) == NULL) | ||
| { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| pElem = _cache.NewElement("File"); | ||
| if (pElem == NULL) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| if (root->InsertEndChild(pElem) == NULL) | ||
| { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| pElem->DeleteChildren(); | ||
| pElem->SetAttribute("Configuration", configuration); | ||
| pElem->SetAttribute("Size", actualSize); | ||
| pElem->SetAttribute("Hash", actualHash.c_str()); | ||
| pElem->SetAttribute("Path", path.c_str()); | ||
|
|
||
| // Insert reports | ||
| std::list<std::string>::const_iterator currIt, endIt; | ||
| for (currIt = reports.begin(), endIt = reports.end(); currIt != endIt; ++currIt) | ||
| { | ||
| XMLElement *report = _cache.NewElement("Report"); | ||
| if (report == NULL) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| report->SetText(currIt->c_str()); | ||
| pElem->InsertEndChild(report); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int Cache::Remove(const char* filePath, const char* configuration) | ||
| { | ||
| XMLElement* pElem = NULL; | ||
| if ((Find(filePath, configuration, &pElem) != 0) || (pElem == NULL)) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| XMLElement* root = _cache.RootElement(); | ||
| if (root == NULL) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| root->DeleteChild(pElem); | ||
| return 0; | ||
| } | ||
|
|
||
| void Cache::Clear() | ||
| { | ||
| _cache.Clear(); | ||
| _cacheFile.clear(); | ||
| } | ||
|
|
||
| int Cache::Save() | ||
| { | ||
| XMLError er = XML_SUCCESS; | ||
|
|
||
| er = _cache.SaveFile(_cacheFile.c_str()); | ||
|
|
||
| return((er == XML_SUCCESS) || (er == XML_NO_ERROR)) ? 0 : (er | XML_SUCCESS); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strlen=>sizeof