⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testpreprocessor.cpp

📁 cppcheck is a static C/C++ code analyzer that checks for memory leaks, mismatching allocation-deallo
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * Cppcheck - A tool for static C/C++ code analysis * Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam, * Leandro Penz, Kimmo Varis, Vesa Pikki * * 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/ */// The preprocessor that c++check uses is a bit special. Instead of generating// the code for a known configuration, it generates the code for each configuration.#include "testsuite.h"#include "../src/preprocessor.h"#include "../src/tokenize.h"#include <map>#include <string>class TestPreprocessor : public TestFixture{public:    TestPreprocessor() : TestFixture("TestPreprocessor")    { }    class OurPreprocessor : public Preprocessor    {    public:        static std::string replaceIfDefined(const std::string &str)        {            return Preprocessor::replaceIfDefined(str);        }        static std::string expandMacros(std::string code)        {            return Preprocessor::expandMacros(code);        }    };private:    void run()    {        // Just read the code into a string. Perform simple cleanup of the code        TEST_CASE(readCode);        // The bug that started the whole work with the new preprocessor        TEST_CASE(Bug2190219);        TEST_CASE(test1);        TEST_CASE(test2);        TEST_CASE(test3);        TEST_CASE(test4);        TEST_CASE(test5);        TEST_CASE(test6);        // Handling include guards (don't create extra configuration for it)        TEST_CASE(includeguard);        TEST_CASE(newlines);        TEST_CASE(comments1);        TEST_CASE(if0);        TEST_CASE(if1);        TEST_CASE(elif);        TEST_CASE(if_cond1);        TEST_CASE(multiline1);        TEST_CASE(multiline2);        TEST_CASE(multiline3);        TEST_CASE(if_defined);      // "#if defined(AAA)" => "#ifdef AAA"        // Macros..        TEST_CASE(macro_simple1);        TEST_CASE(macro_simple2);        TEST_CASE(macro_simple3);        TEST_CASE(macro_simple4);        TEST_CASE(macro_simple5);        TEST_CASE(macro_simple6);        TEST_CASE(macro_simple7);        TEST_CASE(macro_simple8);        TEST_CASE(macro_mismatch);        TEST_CASE(string1);        TEST_CASE(string2);        TEST_CASE(preprocessor_undef);        TEST_CASE(defdef);  // Defined multiple times        TEST_CASE(preprocessor_doublesharp);        TEST_CASE(preprocessor_include_in_str);        TEST_CASE(fmt1);        TEST_CASE(fmt2);        TEST_CASE(multi_character_character);        TEST_CASE(stringify);        TEST_CASE(ifdefwithfile);    }    void readCode()    {        const char code[] = " \t a //\n"                            "  #aa\t /* remove this */\tb  \r\n";        Preprocessor p;        std::istringstream istr(code);        std::string codestr(p.read(istr));        ASSERT_EQUALS("a \n#aa b \n", codestr);    }    void Bug2190219()    {        const char filedata[] = "int main()\n"                                "{\n"                                "#ifdef __cplusplus\n"                                "    int* flags = new int[10];\n"                                "#else\n"                                "    int* flags = (int*)malloc((10)*sizeof(int));\n"                                "#endif\n"                                "\n"                                "#ifdef __cplusplus\n"                                "    delete [] flags;\n"                                "#else\n"                                "    free(flags);\n"                                "#endif\n"                                "}\n";        // Expected result..        std::map<std::string, std::string> expected;        expected[""]          = "int main()\n"                                "{\n"                                "\n"                                "\n"                                "\n"                                "int* flags = (int*)malloc((10)*sizeof(int));\n"                                "\n"                                "\n"                                "\n"                                "\n"                                "\n"                                "free(flags);\n"                                "\n"                                "}\n";        expected["__cplusplus"] = "int main()\n"                                  "{\n"                                  "\n"                                  "int* flags = new int[10];\n"                                  "\n"                                  "\n"                                  "\n"                                  "\n"                                  "\n"                                  "delete [] flags;\n"                                  "\n"                                  "\n"                                  "\n"                                  "}\n";        // Preprocess => actual result..        std::istringstream istr(filedata);        std::map<std::string, std::string> actual;        Preprocessor preprocessor;        preprocessor.preprocess(istr, actual, "file.c");        // Compare results..        ASSERT_EQUALS(expected[""], actual[""]);        ASSERT_EQUALS(expected["__cplusplus"], actual["__cplusplus"]);        ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));    }    void test1()    {        const char filedata[] = "#ifdef  WIN32 \n"                                "    abcdef\n"                                "#else  \n"                                "    qwerty\n"                                "#endif  \n";        // Preprocess => actual result..        std::istringstream istr(filedata);        std::map<std::string, std::string> actual;        Preprocessor preprocessor;        preprocessor.preprocess(istr, actual, "file.c");        // Compare results..        ASSERT_EQUALS("\n\n\nqwerty\n\n", actual[""]);        ASSERT_EQUALS("\nabcdef\n\n\n\n", actual["WIN32"]);        ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));    }    void test2()    {        const char filedata[] = "# ifndef WIN32\n"                                "    \" # ifdef WIN32\" // a comment\n"                                "   #   else  \n"                                "    qwerty\n"                                "  # endif  \n";        // Preprocess => actual result..        std::istringstream istr(filedata);        std::map<std::string, std::string> actual;        Preprocessor preprocessor;        preprocessor.preprocess(istr, actual, "file.c");        // Compare results..        ASSERT_EQUALS("\n\" # ifdef WIN32\"\n\n\n\n", actual[""]);        ASSERT_EQUALS("\n\n\nqwerty\n\n", actual["WIN32"]);        ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));    }    void test3()    {        const char filedata[] = "#ifdef ABC\n"                                "a\n"                                "#ifdef DEF\n"                                "b\n"                                "#endif\n"                                "c\n"                                "#endif\n";        // Preprocess => actual result..        std::istringstream istr(filedata);        std::map<std::string, std::string> actual;        Preprocessor preprocessor;        preprocessor.preprocess(istr, actual, "file.c");        // Compare results..        ASSERT_EQUALS("\n\n\n\n\n\n\n", actual[""]);        ASSERT_EQUALS("\na\n\n\n\nc\n\n", actual["ABC"]);        ASSERT_EQUALS("\na\n\nb\n\nc\n\n", actual["ABC;DEF"]);        ASSERT_EQUALS(3, static_cast<unsigned int>(actual.size()));    }    void test4()    {        const char filedata[] = "#ifdef ABC\n"                                "A\n"                                "#endif\t\n"                                "#ifdef ABC\n"                                "A\n"                                "#endif\n";        // Preprocess => actual result..        std::istringstream istr(filedata);        std::map<std::string, std::string> actual;        Preprocessor preprocessor;        preprocessor.preprocess(istr, actual, "file.c");        // Compare results..        ASSERT_EQUALS("\n\n\n\n\n\n", actual[""]);        ASSERT_EQUALS("\nA\n\n\nA\n\n", actual["ABC"]);        ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));    }    void test5()    {        const char filedata[] = "#ifdef ABC\n"                                "A\n"                                "#else\n"                                "B\n"                                "#ifdef DEF\n"                                "C\n"                                "#endif\n"                                "#endif\n";        // Preprocess => actual result..        std::istringstream istr(filedata);        std::map<std::string, std::string> actual;        Preprocessor preprocessor;        preprocessor.preprocess(istr, actual, "file.c");        // Compare results..        ASSERT_EQUALS("\n\n\nB\n\n\n\n\n", actual[""]);        ASSERT_EQUALS("\nA\n\n\n\n\n\n\n", actual["ABC"]);        ASSERT_EQUALS("\n\n\nB\n\nC\n\n\n", actual["DEF"]);        ASSERT_EQUALS(3, static_cast<unsigned int>(actual.size()));    }    void test6()    {        const char filedata[] = "#if(AAA)\n"                                "#if(AAA)\n";        std::istringstream istr(filedata);        const std::string actual(Preprocessor::read(istr));        // Compare results..        ASSERT_EQUALS("#if (AAA)\n#if (AAA)\n", actual);    }    void includeguard()    {        // Handling include guards..        const char filedata[] = "#file \"abc.h\"\n"                                "#ifndef abcH\n"                                "#define abcH\n"                                "#endif\n"                                "#endfile\n"                                "#ifdef ABC\n"                                "#endif";        // Preprocess => actual result..        std::istringstream istr(filedata);        std::map<std::string, std::string> actual;        Preprocessor preprocessor;        preprocessor.preprocess(istr, actual, "file.c");        // Expected configurations: "" and "ABC"        ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));    }    void ifdefwithfile()    {        // Handling include guards..        const char filedata[] = "#ifdef ABC\n"                                "#file \"abc.h\"\n"                                "class A{};/*\n\n\n\n\n\n\n*/\n"                                "#endfile\n"                                "#endif\n"                                "int main() {}\n";        // Preprocess => actual result..        std::istringstream istr(filedata);        std::map<std::string, std::string> actual;        Preprocessor preprocessor;        preprocessor.preprocess(istr, actual, "file.c");        Tokenizer tok;        std::istringstream codeStream(actual[""]);        tok.tokenize(codeStream, "main.cpp");        ASSERT_EQUALS("\n\n##file 0\n1:\n2:\n3:\n4: int main ( ) { }\n", tok.tokens()->stringifyList());        // Expected configurations: "" and "ABC"        ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));        ASSERT_EQUALS("\n#file \"abc.h\"\n\n\n\n\n\n\n\n\n#endfile\n\nint main() {}\n", actual[""]);        ASSERT_EQUALS("\n#file \"abc.h\"\nclass A{};\n\n\n\n\n\n\n\n#endfile\n\nint main() {}\n", actual["ABC"]);    }    void newlines()    {        const char filedata[] = "\r\r\n\n";        // Preprocess        std::istringstream istr(filedata);        Preprocessor preprocessor;        ASSERT_EQUALS("\n\n\n", preprocessor.read(istr));    }    void comments1()    {        const char filedata[] = "/*\n"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -