@@ -1026,25 +1026,6 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
10261026 preprocessor.inlineSuppressions (mSuppressions .nomsg );
10271027 preprocessor.removeComments ();
10281028
1029- if (!mSettings .buildDir .empty ()) {
1030- analyzerInformation.reset (new AnalyzerInformation);
1031- mLogger ->setAnalyzerInfo (analyzerInformation.get ());
1032- }
1033-
1034- if (analyzerInformation) {
1035- // Calculate hash so it can be compared with old hash / future hashes
1036- const std::size_t hash = calculateHash (preprocessor, file.spath ());
1037- std::list<ErrorMessage> errors;
1038- if (!analyzerInformation->analyzeFile (mSettings .buildDir , file.spath (), cfgname, file.fsFileId (), hash, errors, mSettings .debugainfo )) {
1039- while (!errors.empty ()) {
1040- mErrorLogger .reportErr (errors.front ());
1041- errors.pop_front ();
1042- }
1043- mLogger ->setAnalyzerInfo (nullptr );
1044- return mLogger ->exitcode (); // known results => no need to reanalyze file
1045- }
1046- }
1047-
10481029 // Get directives
10491030 std::list<Directive> directives;
10501031 preprocessor.createDirectives (directives);
@@ -1062,7 +1043,13 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
10621043 std::inserter (configDefines, configDefines.end ()),
10631044 getDefineName);
10641045
1046+ // Keep track of all included files when using build dir
1047+ std::set<std::string> includedFiles;
1048+
10651049 preprocessor.setLoadCallback ([&](simplecpp::FileData &data, bool loaded) {
1050+ if (analyzerInformation) {
1051+ includedFiles.insert (data.filename );
1052+ }
10661053 if (loaded) {
10671054 // Do preprocessing on included file
10681055 mLogger ->addRemarkComments (preprocessor.getRemarkComments (data.tokens ));
@@ -1078,12 +1065,37 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
10781065
10791066 preprocessor.setPlatformInfo ();
10801067
1068+ if (!mSettings .buildDir .empty ()) {
1069+ analyzerInformation.reset (new AnalyzerInformation);
1070+ mLogger ->setAnalyzerInfo (analyzerInformation.get ());
1071+ }
1072+
1073+ if (analyzerInformation) {
1074+ // Load all included files to get correct hashes and suppressions
1075+ for (const std::string &filename : analyzerInformation->getIncludes (mSettings .buildDir , file.spath (), cfgname, file.fsFileId ()))
1076+ preprocessor.loadFile (files, filename);
1077+ // Calculate hash so it can be compared with old hash / future hashes
1078+ const std::size_t hash = calculateHash (preprocessor, file.spath ());
1079+ std::list<ErrorMessage> errors;
1080+ if (!analyzerInformation->analyzeFile (mSettings .buildDir , file.spath (), cfgname, file.fsFileId (), hash, errors, mSettings .debugainfo )) {
1081+ while (!errors.empty ()) {
1082+ mErrorLogger .reportErr (errors.front ());
1083+ errors.pop_front ();
1084+ }
1085+ mLogger ->setAnalyzerInfo (nullptr );
1086+ return mLogger ->exitcode (); // known results => no need to reanalyze file
1087+ }
1088+ // Clear included file list; we don't want to keep includes that have been removed from the source
1089+ // Any includes that are still present will be readded
1090+ includedFiles.clear ();
1091+ }
1092+
10811093 // Get configurations..
10821094 if (maxConfigs > 1 ) {
10831095 Timer::run (" Preprocessor::getConfigs" , mTimerResults , [&]() {
10841096 configurations = { " " };
10851097 preprocessor.getConfigs (configDefines, configurations);
1086- preprocessor.loadFiles (files);
1098+ preprocessor.loadAllIncludes (files);
10871099 });
10881100 } else {
10891101 configurations = { mSettings .userDefines };
@@ -1306,6 +1318,11 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
13061318 mLogger ->setPlistFilenames (std::move (files));
13071319 }
13081320
1321+ if (analyzerInformation) {
1322+ analyzerInformation->writeIncludes (includedFiles);
1323+ analyzerInformation->writeHash (calculateHash (preprocessor, file.spath ()));
1324+ }
1325+
13091326 executeAddons (dumpFile, file);
13101327 } catch (const TerminateException &) {
13111328 // Analysis is terminated
0 commit comments