diff --git a/lib/analyzerinfo.cpp b/lib/analyzerinfo.cpp index c7045252f3d..6c353a9684a 100644 --- a/lib/analyzerinfo.cpp +++ b/lib/analyzerinfo.cpp @@ -114,15 +114,13 @@ bool AnalyzerInformation::skipAnalysis(const tinyxml2::XMLDocument &analyzerInfo std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg, int fileIndex) { - const std::string id = (fileIndex > 0) ? std::to_string(fileIndex) : ""; std::string line; - const std::string end(sep + cfg + sep + id + sep + Path::simplifyPath(sourcefile)); while (std::getline(filesTxt,line)) { - if (line.size() <= end.size() + 2U) - continue; - if (!endsWith(line, end.c_str(), end.size())) - continue; - return line.substr(0,line.find(sep)); + AnalyzerInformation::Info filesTxtInfo; + if (!filesTxtInfo.parse(line)) + continue; // TODO: report error? + if (endsWith(sourcefile, filesTxtInfo.sourceFile) && filesTxtInfo.cfg == cfg && filesTxtInfo.fileIndex == fileIndex) + return filesTxtInfo.afile; } return ""; } diff --git a/lib/utils.h b/lib/utils.h index c59e4d3a32c..c84c72236c1 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -124,6 +124,11 @@ bool endsWith(const std::string& str, const char (&end)[N]) return endsWith(str, end, N - 1); } +inline bool endsWith(const std::string& str, const std::string& end) +{ + return endsWith(str, end.c_str(), end.length()); +} + inline static bool isPrefixStringCharLiteral(const std::string &str, char q, const std::string& p) { // str must be at least the prefix plus the start and end quote diff --git a/test/testanalyzerinformation.cpp b/test/testanalyzerinformation.cpp index 13dd721f0a4..7fd520a2327 100644 --- a/test/testanalyzerinformation.cpp +++ b/test/testanalyzerinformation.cpp @@ -39,6 +39,7 @@ class TestAnalyzerInformation : public TestFixture { }; void run() override { + TEST_CASE(getAnalyzerInfoFileFromFilesTxt); TEST_CASE(getAnalyzerInfoFile); TEST_CASE(duplicateFile); TEST_CASE(filesTextDuplicateFile); @@ -46,12 +47,15 @@ class TestAnalyzerInformation : public TestFixture { TEST_CASE(skipAnalysis); } - void getAnalyzerInfoFile() const { + void getAnalyzerInfoFileFromFilesTxt() const { constexpr char filesTxt[] = "file1.a4:::file1.c\n"; - std::istringstream f1(filesTxt); - ASSERT_EQUALS("file1.a4", AnalyzerInformationTest::getAnalyzerInfoFileFromFilesTxt(f1, "file1.c", "", 0)); - std::istringstream f2(filesTxt); - ASSERT_EQUALS("file1.a4", AnalyzerInformationTest::getAnalyzerInfoFileFromFilesTxt(f2, "./file1.c", "", 0)); + std::istringstream f(filesTxt); + ASSERT_EQUALS("file1.a4", AnalyzerInformationTest::getAnalyzerInfoFileFromFilesTxt(f, "file1.c", "", 0)); + f.str(filesTxt); + ASSERT_EQUALS("file1.a4", AnalyzerInformationTest::getAnalyzerInfoFileFromFilesTxt(f, "./file1.c", "", 0)); + } + + void getAnalyzerInfoFile() const { ASSERT_EQUALS("builddir/file1.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile("builddir", "file1.c", "", 0)); ASSERT_EQUALS("builddir/file1.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile("builddir", "some/path/file1.c", "", 0)); } diff --git a/test/testutils.cpp b/test/testutils.cpp index 37c1030000d..0aad2c447a1 100644 --- a/test/testutils.cpp +++ b/test/testutils.cpp @@ -44,6 +44,7 @@ class TestUtils : public TestFixture { TEST_CASE(splitString); TEST_CASE(as_const); TEST_CASE(memoize); + TEST_CASE(endsWith); } void isValidGlobPattern() const { @@ -563,6 +564,17 @@ class TestUtils : public TestFixture { ASSERT_EQUALS(1, callF()); ASSERT_EQUALS(1, count); } + + void endsWith() const + { + ASSERT(::endsWith("test", "test")); + ASSERT(::endsWith("test2", "2")); + ASSERT(::endsWith("test test", "test")); + ASSERT(::endsWith("test", "t")); + ASSERT(!::endsWith("", "test")); + ASSERT(!::endsWith("tes", "test")); + ASSERT(!::endsWith("2test", "2")); + } }; REGISTER_TEST(TestUtils)