Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ test/options.o: test/options.cpp lib/config.h lib/timer.h test/options.h
test/test64bit.o: test/test64bit.cpp lib/check.h lib/check64bit.h lib/checkers.h lib/checkimpl.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/test64bit.cpp

test/testanalyzerinformation.o: test/testanalyzerinformation.cpp externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/utils.h lib/xml.h test/fixture.h
test/testanalyzerinformation.o: test/testanalyzerinformation.cpp externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/xml.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testanalyzerinformation.cpp

test/testassert.o: test/testassert.cpp lib/check.h lib/checkassert.h lib/checkers.h lib/checkimpl.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
Expand Down
18 changes: 15 additions & 3 deletions lib/analyzerinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@

#include "xml.h"

// Not XMLDocument::LoadFile(): with MSVC it denies writing, and addons inherit the handle (#15207)
static tinyxml2::XMLError loadFile(tinyxml2::XMLDocument &doc, const std::string &filename)
{
std::ifstream fin(filename, std::ios::binary);
if (!fin.is_open())
return tinyxml2::XML_ERROR_FILE_NOT_FOUND;
std::ostringstream ostr;
ostr << fin.rdbuf();
const std::string data = ostr.str();
return doc.Parse(data.data(), data.size());
}

AnalyzerInformation::~AnalyzerInformation()
{
close();
Expand Down Expand Up @@ -81,7 +93,7 @@ std::set<std::string> AnalyzerInformation::getIncludes(const std::string &buildD
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir, sourcefile, cfg, fsFileId);

tinyxml2::XMLDocument analyzerInfoDoc;
if (analyzerInfoDoc.LoadFile(analyzerInfoFile.c_str()) != tinyxml2::XML_SUCCESS)
if (loadFile(analyzerInfoDoc, analyzerInfoFile) != tinyxml2::XML_SUCCESS)
return files;

const tinyxml2::XMLElement *const rootNode = analyzerInfoDoc.FirstChildElement();
Expand Down Expand Up @@ -239,7 +251,7 @@ bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::st

{
tinyxml2::XMLDocument analyzerInfoDoc;
const tinyxml2::XMLError xmlError = analyzerInfoDoc.LoadFile(analyzerInfoFile.c_str());
const tinyxml2::XMLError xmlError = loadFile(analyzerInfoDoc, analyzerInfoFile);
if (xmlError == tinyxml2::XML_SUCCESS) {
const std::string err = skipAnalysis(analyzerInfoDoc, hash, errors);
if (err.empty()) {
Expand Down Expand Up @@ -324,7 +336,7 @@ std::string AnalyzerInformation::processFilesTxt(const std::string& buildDir, co
const std::string xmlfile = buildDir + '/' + filesTxtInfo.afile;

tinyxml2::XMLDocument doc;
const tinyxml2::XMLError error = doc.LoadFile(xmlfile.c_str());
const tinyxml2::XMLError error = loadFile(doc, xmlfile);
if (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND) {
/* FIXME: this can currently not be reported as an error because:
* - --clang does not generate any analyzer information - see #14456
Expand Down
34 changes: 34 additions & 0 deletions test/testanalyzerinformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
#include "errorlogger.h"
#include "filesettings.h"
#include "fixture.h"
#include "helpers.h"
#include "standards.h"

#include <fstream>
#include <list>
#include <set>
#include <sstream>
#include <string>

#include "xml.h"

Expand All @@ -45,6 +49,7 @@ class TestAnalyzerInformation : public TestFixture {
TEST_CASE(filesTextDuplicateFile);
TEST_CASE(parse);
TEST_CASE(skipAnalysis);
TEST_CASE(loadFileOpenForWriting);
}

void getAnalyzerInfoFileFromFilesTxt() const {
Expand Down Expand Up @@ -309,6 +314,35 @@ class TestAnalyzerInformation : public TestFixture {
ASSERT_EQUALS(0, errorList.size());
}
}

void loadFileOpenForWriting() const {
const std::string buildDir = "testanalyzerinfo-builddir";
const ScopedFile file("file1.c.analyzerinfo",
"<?xml version=\"1.0\"?>\n"
"<analyzerinfo>\n"
" <includes>\n"
" <filename>file1.h</filename>\n"
" </includes>\n"
" <hash>123</hash>\n"
"</analyzerinfo>\n",
buildDir);

std::ofstream writer(file.path(), std::ios::app);
ASSERT(writer.is_open());

{
const AnalyzerInformation analyzerInfo;
const std::set<std::string> includes = analyzerInfo.getIncludes(buildDir, "file1.c", "", 0);
ASSERT_EQUALS(1, includes.size());
ASSERT_EQUALS("file1.h", *includes.cbegin());
}
{
AnalyzerInformation analyzerInfo;
std::list<ErrorMessage> errorList;
ASSERT_EQUALS(false, analyzerInfo.analyzeFile(buildDir, "file1.c", "", 0, 123, errorList));
ASSERT_EQUALS(0, errorList.size());
}
}
};

REGISTER_TEST(TestAnalyzerInformation)
Loading