Skip to content

Commit 08fb0fc

Browse files
authored
AnalyzerInformation: use Info::parse() in getAnalyzerInfoFileFromFilesTxt() (#8176)
1 parent d92c579 commit 08fb0fc

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

lib/analyzerinfo.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,15 @@ bool AnalyzerInformation::skipAnalysis(const tinyxml2::XMLDocument &analyzerInfo
110110
return true;
111111
}
112112

113-
std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg, int fileIndex)
113+
std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg, int fsFileId)
114114
{
115-
const std::string id = (fileIndex > 0) ? std::to_string(fileIndex) : "";
116115
std::string line;
117-
const std::string end(sep + cfg + sep + id + sep + Path::simplifyPath(sourcefile));
118116
while (std::getline(filesTxt,line)) {
119-
if (line.size() <= end.size() + 2U)
120-
continue;
121-
if (!endsWith(line, end.c_str(), end.size()))
122-
continue;
123-
return line.substr(0,line.find(sep));
117+
AnalyzerInformation::Info filesTxtInfo;
118+
if (!filesTxtInfo.parse(line))
119+
continue; // TODO: report error?
120+
if (endsWith(sourcefile, filesTxtInfo.sourceFile) && filesTxtInfo.cfg == cfg && filesTxtInfo.fsFileId == fsFileId)
121+
return filesTxtInfo.afile;
124122
}
125123
return "";
126124
}

lib/analyzerinfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class CPPCHECKLIB AnalyzerInformation {
8282
protected:
8383
static std::string getFilesTxt(const std::list<std::string> &sourcefiles, const std::list<FileSettings> &fileSettings);
8484

85-
static std::string getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg, int fileIndex);
85+
static std::string getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg, int fsFileId);
8686

8787
static bool skipAnalysis(const tinyxml2::XMLDocument &analyzerInfoDoc, std::size_t hash, std::list<ErrorMessage> &errors);
8888

lib/utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ bool endsWith(const std::string& str, const char (&end)[N])
124124
return endsWith(str, end, N - 1);
125125
}
126126

127+
inline bool endsWith(const std::string& str, const std::string& end)
128+
{
129+
return endsWith(str, end.c_str(), end.length());
130+
}
131+
127132
inline static bool isPrefixStringCharLiteral(const std::string &str, char q, const std::string& p)
128133
{
129134
// str must be at least the prefix plus the start and end quote

test/testanalyzerinformation.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,23 @@ class TestAnalyzerInformation : public TestFixture {
3939
};
4040

4141
void run() override {
42+
TEST_CASE(getAnalyzerInfoFileFromFilesTxt);
4243
TEST_CASE(getAnalyzerInfoFile);
4344
TEST_CASE(duplicateFile);
4445
TEST_CASE(filesTextDuplicateFile);
4546
TEST_CASE(parse);
4647
TEST_CASE(skipAnalysis);
4748
}
4849

49-
void getAnalyzerInfoFile() const {
50+
void getAnalyzerInfoFileFromFilesTxt() const {
5051
constexpr char filesTxt[] = "file1.a4:::file1.c\n";
51-
std::istringstream f1(filesTxt);
52-
ASSERT_EQUALS("file1.a4", AnalyzerInformationTest::getAnalyzerInfoFileFromFilesTxt(f1, "file1.c", "", 0));
53-
std::istringstream f2(filesTxt);
54-
ASSERT_EQUALS("file1.a4", AnalyzerInformationTest::getAnalyzerInfoFileFromFilesTxt(f2, "./file1.c", "", 0));
52+
std::istringstream f(filesTxt);
53+
ASSERT_EQUALS("file1.a4", AnalyzerInformationTest::getAnalyzerInfoFileFromFilesTxt(f, "file1.c", "", 0));
54+
f.str(filesTxt);
55+
ASSERT_EQUALS("file1.a4", AnalyzerInformationTest::getAnalyzerInfoFileFromFilesTxt(f, "./file1.c", "", 0));
56+
}
57+
58+
void getAnalyzerInfoFile() const {
5559
ASSERT_EQUALS("builddir/file1.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile("builddir", "file1.c", "", 0));
5660
ASSERT_EQUALS("builddir/file1.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile("builddir", "some/path/file1.c", "", 0));
5761
}

test/testutils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class TestUtils : public TestFixture {
4444
TEST_CASE(splitString);
4545
TEST_CASE(as_const);
4646
TEST_CASE(memoize);
47+
TEST_CASE(endsWith);
4748
}
4849

4950
void isValidGlobPattern() const {
@@ -563,6 +564,17 @@ class TestUtils : public TestFixture {
563564
ASSERT_EQUALS(1, callF());
564565
ASSERT_EQUALS(1, count);
565566
}
567+
568+
void endsWith() const
569+
{
570+
ASSERT(::endsWith("test", "test"));
571+
ASSERT(::endsWith("test2", "2"));
572+
ASSERT(::endsWith("test test", "test"));
573+
ASSERT(::endsWith("test", "t"));
574+
ASSERT(!::endsWith("", "test"));
575+
ASSERT(!::endsWith("tes", "test"));
576+
ASSERT(!::endsWith("2test", "2"));
577+
}
566578
};
567579

568580
REGISTER_TEST(TestUtils)

0 commit comments

Comments
 (0)