Skip to content

parse files with simplecpp without providing a stream #4955

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

Merged
merged 1 commit into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,14 +583,13 @@ unsigned int CppCheck::check(const std::string &path)
return mExitCode;
}

std::ifstream fin(path);
return checkFile(Path::simplifyPath(path), emptyString, fin);
return checkFile(Path::simplifyPath(path), emptyString);
}

unsigned int CppCheck::check(const std::string &path, const std::string &content)
{
std::istringstream iss(content);
return checkFile(Path::simplifyPath(path), emptyString, iss);
return checkFile(Path::simplifyPath(path), emptyString, &iss);
}

unsigned int CppCheck::check(const ImportProject::FileSettings &fs)
Expand All @@ -615,13 +614,20 @@ unsigned int CppCheck::check(const ImportProject::FileSettings &fs)
temp.mSettings.includePaths.insert(temp.mSettings.includePaths.end(), fs.systemIncludePaths.cbegin(), fs.systemIncludePaths.cend());
return temp.check(Path::simplifyPath(fs.filename));
}
std::ifstream fin(fs.filename);
const unsigned int returnValue = temp.checkFile(Path::simplifyPath(fs.filename), fs.cfg, fin);
const unsigned int returnValue = temp.checkFile(Path::simplifyPath(fs.filename), fs.cfg);
mSettings.nomsg.addSuppressions(temp.mSettings.nomsg.getSuppressions());
return returnValue;
}

unsigned int CppCheck::checkFile(const std::string& filename, const std::string &cfgname, std::istream& fileStream)
static simplecpp::TokenList createTokenList(const std::string& filename, std::vector<std::string>& files, simplecpp::OutputList* outputList, std::istream* fileStream)
{
if (fileStream)
return {*fileStream, files, filename, outputList};

return {filename, files, outputList};
}

unsigned int CppCheck::checkFile(const std::string& filename, const std::string &cfgname, std::istream* fileStream)
{
mExitCode = 0;

Expand Down Expand Up @@ -663,7 +669,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string

simplecpp::OutputList outputList;
std::vector<std::string> files;
simplecpp::TokenList tokens1(fileStream, files, filename, &outputList);
simplecpp::TokenList tokens1 = createTokenList(filename, files, &outputList, fileStream);

// If there is a syntax error, report it and stop
for (const simplecpp::Output &output : outputList) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
* @param fileStream stream the file content can be read from
* @return number of errors found
*/
unsigned int checkFile(const std::string& filename, const std::string &cfgname, std::istream& fileStream);
unsigned int checkFile(const std::string& filename, const std::string &cfgname, std::istream* fileStream = nullptr);

/**
* @brief Check raw tokens
Expand Down