-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Tokenizer: provide TokenList
in constructor
#7468
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
Open
firewave
wants to merge
2
commits into
danmar:main
Choose a base branch
from
firewave:toklist-123-x
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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 |
---|---|---|
|
@@ -715,8 +715,9 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file) | |
} | ||
|
||
try { | ||
Tokenizer tokenizer(mSettings, mErrorLogger); | ||
tokenizer.list.appendFileIfNew(file.spath()); | ||
TokenList tokenlist{&mSettings}; | ||
tokenlist.appendFileIfNew(file.spath()); | ||
Tokenizer tokenizer(std::move(tokenlist), mSettings, mErrorLogger); | ||
std::istringstream ast(output2); | ||
clangimport::parseClangAstDump(tokenizer, ast); | ||
ValueFlow::setValues(tokenizer.list, | ||
|
@@ -902,18 +903,17 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string | |
|
||
if (mUnusedFunctionsCheck && (mSettings.useSingleJob() || analyzerInformation)) { | ||
std::size_t hash = 0; | ||
// 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. | ||
Tokenizer tokenizer(mSettings, mErrorLogger); | ||
TokenList tokenlist{&mSettings}; | ||
// enforce the language since markup files are special and do not adhere to the enforced language | ||
tokenizer.list.setLang(Standards::Language::C, true); | ||
tokenlist.setLang(Standards::Language::C, true); | ||
if (fileStream) { | ||
std::vector<std::string> files; | ||
simplecpp::TokenList tokens(*fileStream, files, file.spath()); | ||
if (analyzerInformation) { | ||
const Preprocessor preprocessor(mSettings, mErrorLogger); | ||
hash = calculateHash(preprocessor, tokens, mSettings, mSuppressions); | ||
} | ||
tokenizer.list.createTokens(std::move(tokens)); | ||
tokenlist.createTokens(std::move(tokens)); | ||
} | ||
else { | ||
std::vector<std::string> files; | ||
|
@@ -922,8 +922,10 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string | |
const Preprocessor preprocessor(mSettings, mErrorLogger); | ||
hash = calculateHash(preprocessor, tokens, mSettings, mSuppressions); | ||
} | ||
tokenizer.list.createTokens(std::move(tokens)); | ||
tokenlist.createTokens(std::move(tokens)); | ||
} | ||
// 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. | ||
Tokenizer tokenizer(std::move(tokenlist), mSettings, mErrorLogger); | ||
mUnusedFunctionsCheck->parseTokens(tokenizer, mSettings); | ||
|
||
if (analyzerInformation) { | ||
|
@@ -1123,75 +1125,82 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string | |
continue; | ||
} | ||
|
||
Tokenizer tokenizer(mSettings, mErrorLogger); | ||
if (mSettings.showtime != SHOWTIME_MODES::SHOWTIME_NONE) | ||
tokenizer.setTimerResults(&s_timerResults); | ||
tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies? | ||
|
||
try { | ||
TokenList tokenlist{&mSettings}; | ||
|
||
// Create tokens, skip rest of iteration if failed | ||
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() { | ||
simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, mCurrentConfig, files, true); | ||
tokenizer.list.createTokens(std::move(tokensP)); | ||
tokenlist.createTokens(std::move(tokensP)); | ||
}); | ||
hasValidConfig = true; | ||
|
||
// locations macros | ||
mLogger->setLocationMacros(tokenizer.tokens(), files); | ||
Tokenizer tokenizer(std::move(tokenlist), mSettings, mErrorLogger); | ||
try { | ||
if (mSettings.showtime != SHOWTIME_MODES::SHOWTIME_NONE) | ||
tokenizer.setTimerResults(&s_timerResults); | ||
tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies? | ||
|
||
// If only errors are printed, print filename after the check | ||
if (!mSettings.quiet && (!mCurrentConfig.empty() || checkCount > 1)) { | ||
std::string fixedpath = Path::toNativeSeparators(file.spath()); | ||
mErrorLogger.reportOut("Checking " + fixedpath + ": " + mCurrentConfig + "...", Color::FgGreen); | ||
} | ||
// locations macros | ||
mLogger->setLocationMacros(tokenizer.tokens(), files); | ||
|
||
if (!tokenizer.tokens()) | ||
continue; | ||
// If only errors are printed, print filename after the check | ||
if (!mSettings.quiet && (!mCurrentConfig.empty() || checkCount > 1)) { | ||
std::string fixedpath = Path::toNativeSeparators(file.spath()); | ||
mErrorLogger.reportOut("Checking " + fixedpath + ": " + mCurrentConfig + "...", Color::FgGreen); | ||
} | ||
|
||
// skip rest of iteration if just checking configuration | ||
if (mSettings.checkConfiguration) | ||
continue; | ||
if (!tokenizer.tokens()) | ||
continue; | ||
|
||
// skip rest of iteration if just checking configuration | ||
if (mSettings.checkConfiguration) | ||
continue; | ||
|
||
#ifdef HAVE_RULES | ||
// Execute rules for "raw" code | ||
executeRules("raw", tokenizer.list); | ||
// Execute rules for "raw" code | ||
executeRules("raw", tokenizer.list); | ||
#endif | ||
|
||
// Simplify tokens into normal form, skip rest of iteration if failed | ||
if (!tokenizer.simplifyTokens1(mCurrentConfig)) | ||
continue; | ||
// Simplify tokens into normal form, skip rest of iteration if failed | ||
if (!tokenizer.simplifyTokens1(mCurrentConfig)) | ||
continue; | ||
|
||
// dump xml if --dump | ||
if ((mSettings.dump || !mSettings.addons.empty()) && fdump.is_open()) { | ||
fdump << "<dump cfg=\"" << ErrorLogger::toxml(mCurrentConfig) << "\">" << std::endl; | ||
fdump << " <standards>" << std::endl; | ||
fdump << " <c version=\"" << mSettings.standards.getC() << "\"/>" << std::endl; | ||
fdump << " <cpp version=\"" << mSettings.standards.getCPP() << "\"/>" << std::endl; | ||
fdump << " </standards>" << std::endl; | ||
fdump << getLibraryDumpData(); | ||
preprocessor.dump(fdump); | ||
tokenizer.dump(fdump); | ||
fdump << "</dump>" << std::endl; | ||
} | ||
// dump xml if --dump | ||
if ((mSettings.dump || !mSettings.addons.empty()) && fdump.is_open()) { | ||
fdump << "<dump cfg=\"" << ErrorLogger::toxml(mCurrentConfig) << "\">" << std::endl; | ||
fdump << " <standards>" << std::endl; | ||
fdump << " <c version=\"" << mSettings.standards.getC() << "\"/>" << std::endl; | ||
fdump << " <cpp version=\"" << mSettings.standards.getCPP() << "\"/>" << std::endl; | ||
fdump << " </standards>" << std::endl; | ||
fdump << getLibraryDumpData(); | ||
preprocessor.dump(fdump); | ||
tokenizer.dump(fdump); | ||
fdump << "</dump>" << std::endl; | ||
} | ||
|
||
if (mSettings.inlineSuppressions) { | ||
// Need to call this even if the hash will skip this configuration | ||
mSuppressions.nomsg.markUnmatchedInlineSuppressionsAsChecked(tokenizer); | ||
} | ||
if (mSettings.inlineSuppressions) { | ||
// Need to call this even if the hash will skip this configuration | ||
mSuppressions.nomsg.markUnmatchedInlineSuppressionsAsChecked(tokenizer); | ||
} | ||
|
||
// Skip if we already met the same simplified token list | ||
if (mSettings.force || mSettings.maxConfigs > 1) { | ||
const std::size_t hash = tokenizer.list.calculateHash(); | ||
if (hashes.find(hash) != hashes.end()) { | ||
if (mSettings.debugwarnings) | ||
purgedConfigurationMessage(file.spath(), mCurrentConfig); | ||
continue; | ||
// Skip if we already met the same simplified token list | ||
if (mSettings.force || mSettings.maxConfigs > 1) { | ||
const std::size_t hash = tokenizer.list.calculateHash(); | ||
if (hashes.find(hash) != hashes.end()) { | ||
if (mSettings.debugwarnings) | ||
purgedConfigurationMessage(file.spath(), mCurrentConfig); | ||
continue; | ||
} | ||
hashes.insert(hash); | ||
} | ||
hashes.insert(hash); | ||
} | ||
|
||
// Check normal tokens | ||
checkNormalTokens(tokenizer, analyzerInformation.get()); | ||
// Check normal tokens | ||
checkNormalTokens(tokenizer, analyzerInformation.get()); | ||
} catch (const InternalError &e) { | ||
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenizer.list, file.spath()); | ||
mErrorLogger.reportErr(errmsg); | ||
} | ||
} catch (const simplecpp::Output &o) { | ||
// #error etc during preprocessing | ||
configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg); | ||
|
@@ -1221,7 +1230,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string | |
mLogger->setAnalyzerInfo(nullptr); | ||
return mLogger->exitcode(); | ||
} catch (const InternalError &e) { | ||
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenizer.list, file.spath()); | ||
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, file.spath()); | ||
Comment on lines
-1224
to
+1233
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. Since |
||
mErrorLogger.reportErr(errmsg); | ||
} | ||
} | ||
|
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
Oops, something went wrong.
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.
The multiple layers of exception handling seem excessive. I think it is possible to clean this up a bit but that requires test coverage first.