diff --git a/Makefile b/Makefile index f7b7daeec..4109e10a2 100644 --- a/Makefile +++ b/Makefile @@ -163,6 +163,7 @@ TESTOBJ = test/options.o \ test/testinternal.o \ test/testio.o \ test/testleakautovar.o \ + test/testlibrary.o \ test/testmathlib.o \ test/testmemleak.o \ test/testnonreentrantfunctions.o \ @@ -434,6 +435,9 @@ test/testio.o: test/testio.cpp lib/checkio.h lib/check.h lib/config.h lib/token. test/testleakautovar.o: test/testleakautovar.cpp lib/tokenize.h lib/errorlogger.h lib/config.h lib/suppressions.h lib/tokenlist.h lib/checkleakautovar.h lib/check.h lib/token.h lib/settings.h lib/library.h lib/path.h lib/standards.h test/testsuite.h test/redirect.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o test/testleakautovar.o test/testleakautovar.cpp +test/testlibrary.o: test/testlibrary.cpp lib/library.h lib/config.h lib/path.h test/testsuite.h lib/errorlogger.h lib/suppressions.h test/redirect.h + $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o test/testlibrary.o test/testlibrary.cpp + test/testmathlib.o: test/testmathlib.cpp lib/mathlib.h lib/config.h test/testsuite.h lib/errorlogger.h lib/suppressions.h test/redirect.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o test/testmathlib.o test/testmathlib.cpp diff --git a/lib/library.cpp b/lib/library.cpp index b87da030b..5576e5daa 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -29,8 +29,6 @@ Library::Library() : allocid(0) bool Library::load(const char exename[], const char path[]) { - tinyxml2::XMLDocument doc; - if (std::strchr(path,',') != NULL) { bool ret = true; std::string p(path); @@ -65,9 +63,15 @@ bool Library::load(const char exename[], const char path[]) return false; } + tinyxml2::XMLDocument doc; if (doc.LoadFile(fp) != tinyxml2::XML_NO_ERROR) return false; + return load(doc); +} + +bool Library::load(const tinyxml2::XMLDocument &doc) +{ const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement(); if (strcmp(rootnode->Name(),"def") != 0) return false; diff --git a/lib/library.h b/lib/library.h index 0e86d909a..94a860d44 100644 --- a/lib/library.h +++ b/lib/library.h @@ -22,14 +22,15 @@ //--------------------------------------------------------------------------- #include "config.h" +#include "path.h" + +#include #include #include #include #include #include -#include "path.h" - /// @addtogroup Core /// @{ @@ -41,6 +42,7 @@ public: Library(); bool load(const char exename [], const char path []); + bool load(const tinyxml2::XMLDocument &doc); /** get allocation id for function (by name) */ int alloc(const std::string &name) const { diff --git a/test/testfiles.pri b/test/testfiles.pri index b4b56ec99..4afb160cf 100644 --- a/test/testfiles.pri +++ b/test/testfiles.pri @@ -23,6 +23,7 @@ SOURCES += $${BASEPATH}/test64bit.cpp \ $${BASEPATH}/testinternal.cpp \ $${BASEPATH}/testio.cpp \ $${BASEPATH}/testleakautovar.cpp \ + $${BASEPATH}/testlibrary.cpp \ $${BASEPATH}/testmathlib.cpp \ $${BASEPATH}/testmemleak.cpp \ $${BASEPATH}/testnonreentrantfunctions.cpp \ diff --git a/test/testlibrary.cpp b/test/testlibrary.cpp new file mode 100644 index 000000000..9b742f689 --- /dev/null +++ b/test/testlibrary.cpp @@ -0,0 +1,86 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2013 Daniel Marjamäki and 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 . + */ + +#include "library.h" +#include "testsuite.h" + +class TestLibrary : public TestFixture { +public: + TestLibrary() : TestFixture("TestLibrary") { } + +private: + + void run() { + TEST_CASE(empty); + TEST_CASE(function); + TEST_CASE(memory); + } + + void empty() { + const char xmldata[] = "\n"; + tinyxml2::XMLDocument doc; + doc.Parse(xmldata, sizeof(xmldata)); + + Library library; + library.load(doc); + ASSERT(library.use.empty()); + ASSERT(library.leakignore.empty()); + ASSERT(library.argumentChecks.empty()); + } + + void function() { + const char xmldata[] = "\n" + "\n" + " \n" + " false\n" + " \n" + ""; + tinyxml2::XMLDocument doc; + doc.Parse(xmldata, sizeof(xmldata)); + + Library library; + library.load(doc); + ASSERT(library.use.empty()); + ASSERT(library.leakignore.empty()); + ASSERT(library.argumentChecks.empty()); + ASSERT(library.isnotnoreturn("foo")); + } + + void memory() { + const char xmldata[] = "\n" + "\n" + " \n" + " CreateX\n" + " DeleteX\n" + " \n" + ""; + tinyxml2::XMLDocument doc; + doc.Parse(xmldata, sizeof(xmldata)); + + Library library; + library.load(doc); + ASSERT(library.use.empty()); + ASSERT(library.leakignore.empty()); + ASSERT(library.argumentChecks.empty()); + + ASSERT(Library::ismemory(library.alloc("CreateX"))); + ASSERT_EQUALS(library.alloc("CreateX"), library.dealloc("DeleteX")); + } +}; + +REGISTER_TEST(TestLibrary)