Skip to content

Commit b387ae8

Browse files
committed
Fixed whole program analysis
1 parent fa5fd9c commit b387ae8

File tree

8 files changed

+96
-22
lines changed

8 files changed

+96
-22
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ CLIOBJ = cli/cmdlineparser.o \
217217

218218
TESTOBJ = test/options.o \
219219
test/test64bit.o \
220+
test/testanalyzerinformation.o \
220221
test/testassert.o \
221222
test/testastutils.o \
222223
test/testautovariables.o \
@@ -596,6 +597,9 @@ test/options.o: test/options.cpp test/options.h
596597
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
597598
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/test64bit.o test/test64bit.cpp
598599

600+
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
601+
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testanalyzerinformation.o test/testanalyzerinformation.cpp
602+
599603
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
600604
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testassert.o test/testassert.cpp
601605

lib/analyzerinfo.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,36 @@ static bool skipAnalysis(const std::string &analyzerInfoFile, std::size_t hash,
9595
return true;
9696
}
9797

98+
std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg)
99+
{
100+
std::string line;
101+
const std::string end(':' + cfg + ':' + Path::simplifyPath(sourcefile));
102+
while (std::getline(filesTxt,line)) {
103+
if (line.size() <= end.size() + 2U)
104+
continue;
105+
if (!endsWith(line, end.c_str(), end.size()))
106+
continue;
107+
return line.substr(0,line.find(':'));
108+
}
109+
return "";
110+
}
111+
98112
std::string AnalyzerInformation::getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg)
99113
{
100-
const std::string files(buildDir + "/files.txt");
101-
std::ifstream fin(files);
114+
std::ifstream fin(Path::join(buildDir, "files.txt"));
102115
if (fin.is_open()) {
103-
std::string line;
104-
const std::string end(':' + cfg + ':' + sourcefile);
105-
while (std::getline(fin,line)) {
106-
if (line.size() <= end.size() + 2U)
107-
continue;
108-
if (!endsWith(line, end.c_str(), end.size()))
109-
continue;
110-
std::ostringstream ostr;
111-
ostr << buildDir << '/' << line.substr(0,line.find(':'));
112-
return ostr.str();
113-
}
116+
const std::string& ret = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg);
117+
if (!ret.empty())
118+
return Path::join(buildDir, ret);
114119
}
115120

116-
std::string filename = Path::fromNativeSeparators(buildDir);
117-
if (!endsWith(filename, '/'))
118-
filename += '/';
119121
const std::string::size_type pos = sourcefile.rfind('/');
120-
if (pos == std::string::npos)
121-
filename += sourcefile;
122+
std::string filename;
123+
if (pos != std::string::npos)
124+
filename = sourcefile;
122125
else
123-
filename += sourcefile.substr(pos+1);
124-
filename += ".analyzerinfo";
125-
return filename;
126+
filename = sourcefile.substr(pos + 1);
127+
return Path::join(buildDir, filename) + ".analyzerinfo";
126128
}
127129

128130
bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t hash, std::list<ErrorMessage> *errors)

lib/analyzerinfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class CPPCHECKLIB AnalyzerInformation {
5858
void reportErr(const ErrorMessage &msg, bool verbose);
5959
void setFileInfo(const std::string &check, const std::string &fileInfo);
6060
static std::string getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg);
61+
protected:
62+
static std::string getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg);
6163
private:
6264
std::ofstream mOutputStream;
6365
std::string mAnalyzerInfoFile;

lib/path.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,11 @@ bool Path::fileExists(const std::string &file)
241241
std::ifstream f(file.c_str());
242242
return f.is_open();
243243
}
244+
245+
std::string Path::join(std::string path1, std::string path2) {
246+
if (path1.empty() || path2.empty())
247+
return path1 + path2;
248+
if (path2.front() == '/')
249+
return path2;
250+
return ((path1.back() == '/') ? path1 : (path1 + "/")) + path2;
251+
}

lib/path.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ class CPPCHECKLIB Path {
179179
* @return true if given path is a File
180180
*/
181181
static bool fileExists(const std::string &file);
182+
183+
/**
184+
* join 2 paths with '/' separators
185+
*/
186+
static std::string join(std::string path1, std::string path2);
182187
};
183188

184189
/// @}

test/testanalyzerinformation.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2022 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
#include "analyzerinfo.h"
21+
#include "testsuite.h"
22+
#include <sstream>
23+
24+
class TestAnalyzerInformation : public TestFixture, private AnalyzerInformation {
25+
public:
26+
TestAnalyzerInformation() : TestFixture("TestAnalyzerInformation") {}
27+
28+
private:
29+
30+
void run() override {
31+
TEST_CASE(getAnalyzerInfoFile);
32+
}
33+
34+
void getAnalyzerInfoFile() const {
35+
const char filesTxt[] = "file1.a4::file1.c\n";
36+
std::istringstream f1(filesTxt);
37+
ASSERT_EQUALS("file1.a4", getAnalyzerInfoFileFromFilesTxt(f1, "file1.c", ""));
38+
std::istringstream f2(filesTxt);
39+
ASSERT_EQUALS("file1.a4", getAnalyzerInfoFileFromFilesTxt(f2, "./file1.c", ""));
40+
}
41+
};
42+
43+
REGISTER_TEST(TestAnalyzerInformation)

test/testpath.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class TestPath : public TestFixture {
3737
TEST_CASE(is_c);
3838
TEST_CASE(is_cpp);
3939
TEST_CASE(get_path_from_filename);
40+
TEST_CASE(join);
4041
}
4142

4243
void removeQuotationMarks() const {
@@ -141,6 +142,14 @@ class TestPath : public TestFixture {
141142
ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/index.h"));
142143
ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/"));
143144
}
145+
146+
void join() const {
147+
ASSERT_EQUALS("a", Path::join("a", ""));
148+
ASSERT_EQUALS("a", Path::join("", "a"));
149+
ASSERT_EQUALS("a/b", Path::join("a", "b"));
150+
ASSERT_EQUALS("a/b", Path::join("a/", "b"));
151+
ASSERT_EQUALS("/b", Path::join("a", "/b"));
152+
}
144153
};
145154

146155
REGISTER_TEST(TestPath)

test/testrunner.vcxproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<ClCompile Include="..\lib\astutils.cpp" />
3434
<ClCompile Include="options.cpp" />
3535
<ClCompile Include="test64bit.cpp" />
36+
<ClCompile Include="testanalyzerinformation.cpp" />
3637
<ClCompile Include="testassert.cpp" />
3738
<ClCompile Include="testastutils.cpp" />
3839
<ClCompile Include="testautovariables.cpp" />
@@ -316,4 +317,4 @@
316317
</ItemDefinitionGroup>
317318
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
318319
<ImportGroup Label="ExtensionTargets" />
319-
</Project>
320+
</Project>

0 commit comments

Comments
 (0)