3333
3434#include " xml.h"
3535
36+ static AnalyzerInformation::FilesTxt readFilesTxt (std::istream& filesTxt)
37+ {
38+ AnalyzerInformation::FilesTxt index;
39+ std::string line;
40+ while (std::getline (filesTxt, line)) {
41+ AnalyzerInformation::Info filesTxtInfo;
42+ if (!filesTxtInfo.parse (line))
43+ continue ; // TODO: report error?
44+ index.emplace (std::make_tuple (filesTxtInfo.sourceFile , filesTxtInfo.cfg , filesTxtInfo.fsFileId ), filesTxtInfo.afile );
45+ }
46+ return index;
47+ }
48+
49+ static std::string findAfile (const AnalyzerInformation::FilesTxt& index, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId)
50+ {
51+ const auto it = index.find (std::make_tuple (Path::simplifyPath (sourcefile), cfg, fsFileId));
52+ return it == index.end () ? " " : it->second ;
53+ }
54+
3655// Not XMLDocument::LoadFile(): with MSVC it denies writing, and addons inherit the handle (#15207)
3756static tinyxml2::XMLError loadFile (tinyxml2::XMLDocument &doc, const std::string &filename)
3857{
@@ -69,6 +88,18 @@ void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::
6988 fout << getFilesTxt (sourcefiles, fileSettings);
7089}
7190
91+ AnalyzerInformation::FilesTxt AnalyzerInformation::getFilesTxtIndex (const std::list<std::string> &sourcefiles, const std::list<FileSettings> &fileSettings)
92+ {
93+ std::istringstream istr (getFilesTxt (sourcefiles, fileSettings));
94+ return readFilesTxt (istr);
95+ }
96+
97+ const AnalyzerInformation::FilesTxt &AnalyzerInformation::filesTxt () const
98+ {
99+ static const FilesTxt notRead;
100+ return mFilesTxt ? *mFilesTxt : notRead;
101+ }
102+
72103void AnalyzerInformation::writeIncludes (const std::set<std::string> &files)
73104{
74105 if (!files.empty () && mOutputStream .is_open ()) {
@@ -90,7 +121,7 @@ std::set<std::string> AnalyzerInformation::getIncludes(const std::string &buildD
90121 if (buildDir.empty () || sourcefile.empty ())
91122 return files;
92123
93- const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile (buildDir, sourcefile, cfg, fsFileId);
124+ const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile (filesTxt (), buildDir, sourcefile, cfg, fsFileId);
94125
95126 tinyxml2::XMLDocument analyzerInfoDoc;
96127 if (loadFile (analyzerInfoDoc, analyzerInfoFile) != tinyxml2::XML_SUCCESS )
@@ -222,12 +253,21 @@ std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& f
222253
223254std::string AnalyzerInformation::getAnalyzerInfoFile (const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId)
224255{
225- std::ifstream fin (Path::join (buildDir, " files.txt" ));
226- if (fin.is_open ()) {
227- const std::string& ret = getAnalyzerInfoFileFromFilesTxt (fin, sourcefile, cfg, fsFileId);
228- if (!ret.empty ())
229- return Path::join (buildDir, ret);
256+ return getAnalyzerInfoFile (FilesTxt (), buildDir, sourcefile, cfg, fsFileId);
257+ }
258+
259+ std::string AnalyzerInformation::getAnalyzerInfoFile (const FilesTxt &filesTxt, const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId)
260+ {
261+ std::string afile;
262+ if (filesTxt.empty ()) {
263+ std::ifstream fin (Path::join (buildDir, " files.txt" ));
264+ if (fin.is_open ())
265+ afile = getAnalyzerInfoFileFromFilesTxt (fin, sourcefile, cfg, fsFileId);
266+ } else {
267+ afile = findAfile (filesTxt, sourcefile, cfg, fsFileId);
230268 }
269+ if (!afile.empty ())
270+ return Path::join (buildDir, afile);
231271
232272 const std::string::size_type pos = sourcefile.rfind (' /' );
233273 std::string filename;
@@ -247,7 +287,7 @@ bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::st
247287 if (buildDir.empty () || sourcefile.empty ())
248288 return true ;
249289
250- const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile (buildDir,sourcefile,cfg,fsFileId);
290+ const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile (filesTxt (), buildDir, sourcefile, cfg, fsFileId);
251291
252292 {
253293 tinyxml2::XMLDocument analyzerInfoDoc;
@@ -380,7 +420,7 @@ void AnalyzerInformation::reopen(const std::string &buildDir, const std::string
380420 if (buildDir.empty () || sourcefile.empty ())
381421 return ;
382422
383- const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile (buildDir,sourcefile,cfg,fsFileId);
423+ const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile (filesTxt (), buildDir, sourcefile, cfg, fsFileId);
384424 std::ifstream ifs (analyzerInfoFile);
385425 if (!ifs.is_open ())
386426 return ;
0 commit comments