@@ -799,10 +799,9 @@ unsigned int CppCheck::check(const FileWithDetails &file)
799799 return returnValue;
800800}
801801
802- unsigned int CppCheck::check (const FileWithDetails &file, const std::string &content )
802+ unsigned int CppCheck::checkBuffer (const FileWithDetails &file, const uint8_t * data, std::size_t size )
803803{
804- std::istringstream iss (content);
805- return checkFile (file, " " , 0 , &iss);
804+ return checkBuffer (file, " " , 0 , data, size);
806805}
807806
808807unsigned int CppCheck::check (const FileSettings &fs)
@@ -851,14 +850,6 @@ unsigned int CppCheck::check(const FileSettings &fs)
851850 return returnValue;
852851}
853852
854- static simplecpp::TokenList createTokenList (const std::string& filename, std::vector<std::string>& files, simplecpp::OutputList* outputList, std::istream* fileStream)
855- {
856- if (fileStream)
857- return {*fileStream, files, filename, outputList};
858-
859- return {filename, files, outputList};
860- }
861-
862853std::size_t CppCheck::calculateHash (const Preprocessor& preprocessor, const simplecpp::TokenList& tokens, const std::string& filePath) const
863854{
864855 std::ostringstream toolinfo;
@@ -880,7 +871,23 @@ std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const simp
880871 return preprocessor.calculateHash (tokens, toolinfo.str ());
881872}
882873
883- unsigned int CppCheck::checkFile (const FileWithDetails& file, const std::string &cfgname, int fileIndex, std::istream* fileStream)
874+ unsigned int CppCheck::checkBuffer (const FileWithDetails &file, const std::string &cfgname, int fileIndex, const uint8_t * data, std::size_t size)
875+ {
876+ const auto f = [&file, data, size](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
877+ return simplecpp::TokenList{data, size, files, file.spath (), outputList};
878+ };
879+ return checkInternal (file, cfgname, fileIndex, f);
880+ }
881+
882+ unsigned int CppCheck::checkFile (const FileWithDetails& file, const std::string &cfgname, int fileIndex)
883+ {
884+ const auto f = [&file](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
885+ return simplecpp::TokenList{file.spath (), files, outputList};
886+ };
887+ return checkInternal (file, cfgname, fileIndex, f);
888+ }
889+
890+ unsigned int CppCheck::checkInternal (const FileWithDetails& file, const std::string &cfgname, int fileIndex, const CreateTokenListFn& createTokenList)
884891{
885892 // TODO: move to constructor when CppCheck no longer owns the settings
886893 if (mSettings .checks .isEnabled (Checks::unusedFunction) && !mUnusedFunctionsCheck )
@@ -931,24 +938,13 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
931938 std::size_t hash = 0 ;
932939 // markup files are special and do not adhere to the enforced language
933940 TokenList tokenlist{mSettings , Standards::Language::C};
934- if (fileStream) {
935- std::vector<std::string> files;
936- simplecpp::TokenList tokens (*fileStream, files, file.spath ());
937- if (analyzerInformation) {
938- const Preprocessor preprocessor (mSettings , mErrorLogger , Standards::Language::C);
939- hash = calculateHash (preprocessor, tokens);
940- }
941- tokenlist.createTokens (std::move (tokens));
942- }
943- else {
944- std::vector<std::string> files;
945- simplecpp::TokenList tokens (file.spath (), files);
946- if (analyzerInformation) {
947- const Preprocessor preprocessor (mSettings , mErrorLogger , file.lang ());
948- hash = calculateHash (preprocessor, tokens);
949- }
950- tokenlist.createTokens (std::move (tokens));
941+ std::vector<std::string> files;
942+ simplecpp::TokenList tokens = createTokenList (files, nullptr );
943+ if (analyzerInformation) {
944+ const Preprocessor preprocessor (mSettings , mErrorLogger , file.lang ());
945+ hash = calculateHash (preprocessor, tokens);
951946 }
947+ tokenlist.createTokens (std::move (tokens));
952948 // this is not a real source file - we just want to tokenize it. treat it as C anyways as the language needs to be determined.
953949 Tokenizer tokenizer (std::move (tokenlist), mErrorLogger );
954950 mUnusedFunctionsCheck ->parseTokens (tokenizer, mSettings );
@@ -967,7 +963,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
967963
968964 simplecpp::OutputList outputList;
969965 std::vector<std::string> files;
970- simplecpp::TokenList tokens1 = createTokenList (file. spath (), files, &outputList, fileStream );
966+ simplecpp::TokenList tokens1 = createTokenList (files, &outputList);
971967
972968 // If there is a syntax error, report it and stop
973969 const auto output_it = std::find_if (outputList.cbegin (), outputList.cend (), [](const simplecpp::Output &output){
@@ -1075,8 +1071,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
10751071 code += " #line " + std::to_string (dir.linenr ) + " \" " + dir.file + " \"\n " + dir.str + ' \n ' ;
10761072 }
10771073 TokenList tokenlist (mSettings , file.lang ());
1078- std::istringstream istr2 (code);
1079- tokenlist.createTokens (istr2); // TODO: check result?
1074+ tokenlist.createTokensFromBuffer (code.data (), code.size ()); // TODO: check result?
10801075 executeRules (" define" , tokenlist);
10811076 }
10821077#endif
0 commit comments