|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2025 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 | +#include <filesettings.h> |
| 20 | + |
| 21 | +#include "fixture.h" |
| 22 | +#include "frontend.h" |
| 23 | +#include "settings.h" |
| 24 | +#include "standards.h" |
| 25 | + |
| 26 | +#include <list> |
| 27 | +#include <string> |
| 28 | + |
| 29 | +class TestFrontend : public TestFixture { |
| 30 | +public: |
| 31 | + TestFrontend() : TestFixture("TestFrontend") {} |
| 32 | + |
| 33 | +private: |
| 34 | + void run() override { |
| 35 | + TEST_CASE(applyLangFS); |
| 36 | + } |
| 37 | + |
| 38 | + void applyLangFS() const { |
| 39 | + const char xmldata[] = R"(<def format="2"><markup ext=".ml" reporterrors="false"/></def>)"; |
| 40 | + const Settings s = settingsBuilder().libraryxml(xmldata).build(); |
| 41 | + |
| 42 | + const std::list<FileSettings> fs = { |
| 43 | + {"nolang", Standards::Language::None, 0 }, |
| 44 | + {"c", Standards::Language::None, 0 }, // TODO: should be C - FileSettings are currently expected to not be pre-identified |
| 45 | + {"cpp", Standards::Language::None, 0 }, // TODO: should be CPP - FileSettings arecurrently expected to not be pre-identified |
| 46 | + {"nolang.c", Standards::Language::None, 0 }, |
| 47 | + {"nolang.cpp", Standards::Language::None, 0 }, |
| 48 | + {"nolang.ml", Standards::Language::None, 0 } |
| 49 | + }; |
| 50 | + |
| 51 | + // no language to enforce - identify only |
| 52 | + { |
| 53 | + std::list<FileSettings> fs1 = fs; |
| 54 | + frontend::applyLang(fs1, s, Standards::Language::None); |
| 55 | + auto it = fs1.cbegin(); |
| 56 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::CPP); // unknown defaults to C++ |
| 57 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::CPP); // unknown defaults to C++ |
| 58 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::CPP); // unknown defaults to C++ |
| 59 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::C); |
| 60 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::CPP); |
| 61 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::C); // markup files are always C |
| 62 | + } |
| 63 | + |
| 64 | + // language to enforce (C) |
| 65 | + { |
| 66 | + std::list<FileSettings> fs1 = fs; |
| 67 | + frontend::applyLang(fs1, s, Standards::Language::C); |
| 68 | + auto it = fs1.cbegin(); |
| 69 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::C); |
| 70 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::C); |
| 71 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::C); |
| 72 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::C); |
| 73 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::C); |
| 74 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::C); // markup files are always C |
| 75 | + } |
| 76 | + |
| 77 | + // language to enforce (C++) |
| 78 | + { |
| 79 | + std::list<FileSettings> fs1 = fs; |
| 80 | + frontend::applyLang(fs1, s, Standards::Language::CPP); |
| 81 | + auto it = fs1.cbegin(); |
| 82 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::CPP); |
| 83 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::CPP); |
| 84 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::CPP); |
| 85 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::CPP); |
| 86 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::CPP); |
| 87 | + ASSERT_EQUALS_ENUM((it++)->file.lang(), Standards::Language::C); // markup files are always C |
| 88 | + } |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +REGISTER_TEST(TestFrontend) |
0 commit comments