Fixed whole program analysis

This commit is contained in:
Daniel Marjamäki 2022-07-12 22:58:52 +02:00
parent fa5fd9cae0
commit b387ae80f1
8 changed files with 96 additions and 22 deletions

View File

@ -217,6 +217,7 @@ CLIOBJ = cli/cmdlineparser.o \
TESTOBJ = test/options.o \ TESTOBJ = test/options.o \
test/test64bit.o \ test/test64bit.o \
test/testanalyzerinformation.o \
test/testassert.o \ test/testassert.o \
test/testastutils.o \ test/testastutils.o \
test/testautovariables.o \ test/testautovariables.o \
@ -596,6 +597,9 @@ test/options.o: test/options.cpp test/options.h
test/test64bit.o: test/test64bit.cpp lib/check.h lib/check64bit.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/test64bit.o: test/test64bit.cpp lib/check.h lib/check64bit.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/test64bit.o test/test64bit.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/test64bit.o test/test64bit.cpp
test/testanalyzerinformation.o: test/testanalyzerinformation.cpp lib/analyzerinfo.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/platform.h lib/suppressions.h lib/utils.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testanalyzerinformation.o test/testanalyzerinformation.cpp
test/testassert.o: test/testassert.cpp lib/check.h lib/checkassert.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/testassert.o: test/testassert.cpp lib/check.h lib/checkassert.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testassert.o test/testassert.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testassert.o test/testassert.cpp

View File

@ -95,34 +95,36 @@ static bool skipAnalysis(const std::string &analyzerInfoFile, std::size_t hash,
return true; return true;
} }
std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg)
{
std::string line;
const std::string end(':' + cfg + ':' + 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(':'));
}
return "";
}
std::string AnalyzerInformation::getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg) std::string AnalyzerInformation::getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg)
{ {
const std::string files(buildDir + "/files.txt"); std::ifstream fin(Path::join(buildDir, "files.txt"));
std::ifstream fin(files);
if (fin.is_open()) { if (fin.is_open()) {
std::string line; const std::string& ret = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg);
const std::string end(':' + cfg + ':' + sourcefile); if (!ret.empty())
while (std::getline(fin,line)) { return Path::join(buildDir, ret);
if (line.size() <= end.size() + 2U)
continue;
if (!endsWith(line, end.c_str(), end.size()))
continue;
std::ostringstream ostr;
ostr << buildDir << '/' << line.substr(0,line.find(':'));
return ostr.str();
}
} }
std::string filename = Path::fromNativeSeparators(buildDir);
if (!endsWith(filename, '/'))
filename += '/';
const std::string::size_type pos = sourcefile.rfind('/'); const std::string::size_type pos = sourcefile.rfind('/');
if (pos == std::string::npos) std::string filename;
filename += sourcefile; if (pos != std::string::npos)
filename = sourcefile;
else else
filename += sourcefile.substr(pos+1); filename = sourcefile.substr(pos + 1);
filename += ".analyzerinfo"; return Path::join(buildDir, filename) + ".analyzerinfo";
return filename;
} }
bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t hash, std::list<ErrorMessage> *errors) bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t hash, std::list<ErrorMessage> *errors)

View File

@ -58,6 +58,8 @@ public:
void reportErr(const ErrorMessage &msg, bool verbose); void reportErr(const ErrorMessage &msg, bool verbose);
void setFileInfo(const std::string &check, const std::string &fileInfo); void setFileInfo(const std::string &check, const std::string &fileInfo);
static std::string getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg); static std::string getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg);
protected:
static std::string getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg);
private: private:
std::ofstream mOutputStream; std::ofstream mOutputStream;
std::string mAnalyzerInfoFile; std::string mAnalyzerInfoFile;

View File

@ -241,3 +241,11 @@ bool Path::fileExists(const std::string &file)
std::ifstream f(file.c_str()); std::ifstream f(file.c_str());
return f.is_open(); return f.is_open();
} }
std::string Path::join(std::string path1, std::string path2) {
if (path1.empty() || path2.empty())
return path1 + path2;
if (path2.front() == '/')
return path2;
return ((path1.back() == '/') ? path1 : (path1 + "/")) + path2;
}

View File

@ -179,6 +179,11 @@ public:
* @return true if given path is a File * @return true if given path is a File
*/ */
static bool fileExists(const std::string &file); static bool fileExists(const std::string &file);
/**
* join 2 paths with '/' separators
*/
static std::string join(std::string path1, std::string path2);
}; };
/// @} /// @}

View File

@ -0,0 +1,43 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2022 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "analyzerinfo.h"
#include "testsuite.h"
#include <sstream>
class TestAnalyzerInformation : public TestFixture, private AnalyzerInformation {
public:
TestAnalyzerInformation() : TestFixture("TestAnalyzerInformation") {}
private:
void run() override {
TEST_CASE(getAnalyzerInfoFile);
}
void getAnalyzerInfoFile() const {
const char filesTxt[] = "file1.a4::file1.c\n";
std::istringstream f1(filesTxt);
ASSERT_EQUALS("file1.a4", getAnalyzerInfoFileFromFilesTxt(f1, "file1.c", ""));
std::istringstream f2(filesTxt);
ASSERT_EQUALS("file1.a4", getAnalyzerInfoFileFromFilesTxt(f2, "./file1.c", ""));
}
};
REGISTER_TEST(TestAnalyzerInformation)

View File

@ -37,6 +37,7 @@ private:
TEST_CASE(is_c); TEST_CASE(is_c);
TEST_CASE(is_cpp); TEST_CASE(is_cpp);
TEST_CASE(get_path_from_filename); TEST_CASE(get_path_from_filename);
TEST_CASE(join);
} }
void removeQuotationMarks() const { void removeQuotationMarks() const {
@ -141,6 +142,14 @@ private:
ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/index.h")); ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/index.h"));
ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/")); ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/"));
} }
void join() const {
ASSERT_EQUALS("a", Path::join("a", ""));
ASSERT_EQUALS("a", Path::join("", "a"));
ASSERT_EQUALS("a/b", Path::join("a", "b"));
ASSERT_EQUALS("a/b", Path::join("a/", "b"));
ASSERT_EQUALS("/b", Path::join("a", "/b"));
}
}; };
REGISTER_TEST(TestPath) REGISTER_TEST(TestPath)

View File

@ -33,6 +33,7 @@
<ClCompile Include="..\lib\astutils.cpp" /> <ClCompile Include="..\lib\astutils.cpp" />
<ClCompile Include="options.cpp" /> <ClCompile Include="options.cpp" />
<ClCompile Include="test64bit.cpp" /> <ClCompile Include="test64bit.cpp" />
<ClCompile Include="testanalyzerinformation.cpp" />
<ClCompile Include="testassert.cpp" /> <ClCompile Include="testassert.cpp" />
<ClCompile Include="testastutils.cpp" /> <ClCompile Include="testastutils.cpp" />
<ClCompile Include="testautovariables.cpp" /> <ClCompile Include="testautovariables.cpp" />
@ -316,4 +317,4 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" /> <ImportGroup Label="ExtensionTargets" />
</Project> </Project>