@@ -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::check (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,12 +850,26 @@ 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 )
853+ unsigned int CppCheck::checkBuffer (const FileWithDetails &file, const std::string &cfgname, int fileIndex, const uint8_t * data , std::size_t size )
855854{
856- if (fileStream)
857- return {*fileStream, files, filename, outputList};
855+ return checkInternal (file, cfgname, fileIndex,
856+ [&file, data, size](std::vector<std::string>& files) {
857+ return simplecpp::TokenList{data, size, files, file.spath ()};
858+ },
859+ [&file, data, size](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
860+ return simplecpp::TokenList{data, size, files, file.spath (), outputList};
861+ });
862+ }
858863
859- return {filename, files, outputList};
864+ unsigned int CppCheck::checkStream (const FileWithDetails &file, const std::string &cfgname, int fileIndex, std::istream& fileStream)
865+ {
866+ return checkInternal (file, cfgname, fileIndex,
867+ [&fileStream](std::vector<std::string>& files) {
868+ return simplecpp::TokenList{fileStream, files};
869+ },
870+ [&file, &fileStream](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
871+ return simplecpp::TokenList{fileStream, files, file.spath (), outputList};
872+ });
860873}
861874
862875std::size_t CppCheck::calculateHash (const Preprocessor& preprocessor, const simplecpp::TokenList& tokens, const std::string& filePath) const
@@ -880,7 +893,18 @@ std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const simp
880893 return preprocessor.calculateHash (tokens, toolinfo.str ());
881894}
882895
883- unsigned int CppCheck::checkFile (const FileWithDetails& file, const std::string &cfgname, int fileIndex, std::istream* fileStream)
896+ unsigned int CppCheck::checkFile (const FileWithDetails& file, const std::string &cfgname, int fileIndex)
897+ {
898+ return checkInternal (file, cfgname, fileIndex,
899+ [&file](std::vector<std::string>& files) {
900+ return simplecpp::TokenList{file.spath (), files};
901+ },
902+ [&file](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
903+ return simplecpp::TokenList{file.spath (), files, outputList};
904+ });
905+ }
906+
907+ unsigned int CppCheck::checkInternal (const FileWithDetails& file, const std::string &cfgname, int fileIndex, const CreateTokensFn& createTokens, const CreateTokenListFn& createTokenList)
884908{
885909 // TODO: move to constructor when CppCheck no longer owns the settings
886910 if (mSettings .checks .isEnabled (Checks::unusedFunction) && !mUnusedFunctionsCheck )
@@ -931,24 +955,13 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
931955 std::size_t hash = 0 ;
932956 // markup files are special and do not adhere to the enforced language
933957 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));
958+ std::vector<std::string> files;
959+ simplecpp::TokenList tokens = createTokens (files);
960+ if (analyzerInformation) {
961+ const Preprocessor preprocessor (mSettings , mErrorLogger , file.lang ());
962+ hash = calculateHash (preprocessor, tokens);
951963 }
964+ tokenlist.createTokens (std::move (tokens));
952965 // 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.
953966 Tokenizer tokenizer (std::move (tokenlist), mErrorLogger );
954967 mUnusedFunctionsCheck ->parseTokens (tokenizer, mSettings );
@@ -967,7 +980,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
967980
968981 simplecpp::OutputList outputList;
969982 std::vector<std::string> files;
970- simplecpp::TokenList tokens1 = createTokenList (file. spath (), files, &outputList, fileStream );
983+ simplecpp::TokenList tokens1 = createTokenList (files, &outputList);
971984
972985 // If there is a syntax error, report it and stop
973986 const auto output_it = std::find_if (outputList.cbegin (), outputList.cend (), [](const simplecpp::Output &output){
0 commit comments