File: testcppcheck.cpp

package info (click to toggle)
cppcheck 2.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,688 kB
  • sloc: cpp: 272,455; python: 22,408; ansic: 8,088; sh: 1,059; makefile: 1,041; xml: 987; cs: 291
file content (627 lines) | stat: -rw-r--r-- 26,360 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
 * Cppcheck - A tool for static C/C++ code analysis
 * Copyright (C) 2007-2025 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 <http://www.gnu.org/licenses/>.
 */

#include "addoninfo.h"
#include "color.h"
#include "cppcheck.h"
#include "errorlogger.h"
#include "filesettings.h"
#include "fixture.h"
#include "helpers.h"
#include "path.h"
#include "preprocessor.h"
#include "redirect.h"
#include "settings.h"
#include "standards.h"
#include "suppressions.h"

#include <algorithm>
#include <cstdlib>
#include <list>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

#include <simplecpp.h>

class TestCppcheck : public TestFixture {
public:
    TestCppcheck() : TestFixture("TestCppcheck") {}

private:
    const std::string templateFormat{"{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]"};

    class ErrorLogger2 : public ErrorLogger {
    public:
        std::list<std::string> ids;
        std::list<ErrorMessage> errmsgs;

    private:
        void reportOut(const std::string & /*outmsg*/, Color /*c*/ = Color::Reset) override {}

        void reportErr(const ErrorMessage &msg) override {
            ids.push_back(msg.id);
            errmsgs.push_back(msg);
        }

        void reportMetric(const std::string &metric) override {
            (void) metric;
        }
    };

    void run() override {
        TEST_CASE(getErrorMessages);
        TEST_CASE(checkWithFile);
        TEST_CASE(checkWithFileWithTools);
        TEST_CASE(checkWithFileWithToolsNoCommand);
        TEST_CASE(checkWithFS);
        TEST_CASE(checkWithFSWithTools);
        TEST_CASE(checkWithFSWithToolsNoCommand);
        TEST_CASE(suppress_error_library);
        TEST_CASE(unique_errors);
        TEST_CASE(unique_errors_2);
        TEST_CASE(isPremiumCodingStandardId);
        TEST_CASE(getDumpFileContentsRawTokens);
        TEST_CASE(getDumpFileContentsLibrary);
        TEST_CASE(checkPlistOutput);
        TEST_CASE(premiumResultsCache);
        TEST_CASE(purgedConfiguration);
    }

    void getErrorMessages() const {
        ErrorLogger2 errorLogger;
        CppCheck::getErrorMessages(errorLogger);
        ASSERT(!errorLogger.ids.empty());

        // Check if there are duplicate error ids in errorLogger.id
        std::string duplicate;
        for (auto it = errorLogger.ids.cbegin();
             it != errorLogger.ids.cend();
             ++it) {
            if (std::find(errorLogger.ids.cbegin(), it, *it) != it) {
                duplicate = "Duplicate ID: " + *it;
                break;
            }
        }
        ASSERT_EQUALS("", duplicate);

        // Check for error ids from this class.
        bool foundPurgedConfiguration = false;
        bool foundTooManyConfigs = false;
        bool foundMissingInclude = false; // #11984
        bool foundMissingIncludeSystem = false; // #11984
        for (const std::string & it : errorLogger.ids) {
            if (it == "purgedConfiguration")
                foundPurgedConfiguration = true;
            else if (it == "toomanyconfigs")
                foundTooManyConfigs = true;
            else if (it == "missingInclude")
                foundMissingInclude = true;
            else if (it == "missingIncludeSystem")
                foundMissingIncludeSystem = true;
        }
        ASSERT(foundPurgedConfiguration);
        ASSERT(foundTooManyConfigs);
        ASSERT(foundMissingInclude);
        ASSERT(foundMissingIncludeSystem);
    }

    static std::string exename_(const std::string& exe)
    {
#ifdef _WIN32
        return exe + ".exe";
#else
        return exe;
#endif
    }

    CppCheck::ExecuteCmdFn getExecuteCommand(const std::string& fname, int& called) const
    {
        // cppcheck-suppress passedByValue - used as callback so we need to preserve the signature
        // NOLINTNEXTLINE(performance-unnecessary-value-param) - used as callback so we need to preserve the signature
        return [&](std::string exe, std::vector<std::string> args, std::string redirect, std::string& /*output*/) -> int {
            ++called;
            if (exe == exename_("clang-tidy"))
            {
                ASSERT_EQUALS(4, args.size());
                ASSERT_EQUALS("-quiet", args[0]);
                ASSERT_EQUALS("-checks=*,-clang-analyzer-*,-llvm*", args[1]);
                ASSERT_EQUALS(fname, args[2]);
                ASSERT_EQUALS("--", args[3]);
                ASSERT_EQUALS("2>&1", redirect);
                return EXIT_SUCCESS;
            }
            if (exe == exename_("python3"))
            {
                ASSERT_EQUALS(1, args.size());
                ASSERT_EQUALS("--version", args[0]);
                ASSERT_EQUALS("2>&1", redirect);
                return EXIT_SUCCESS;
            }
            if (exe == exename_("python"))
            {
                ASSERT_EQUALS(1, args.size());
                ASSERT_EQUALS("--version", args[0]);
                ASSERT_EQUALS("2>&1", redirect);
                return EXIT_SUCCESS;
            }
            ASSERT_MSG(false, "unhandled exe: " + exe);
            return EXIT_FAILURE;
        };
    }

    void checkWithFileInternal(const std::string& fname, bool tools, bool nocmd = false) const
    {
        REDIRECT;
        ScopedFile file(fname,
                        "void f()\n"
                        "{\n"
                        "  (void)(*((int*)0));\n"
                        "}");

        int called = 0;
        std::unordered_set<std::string> addons;
        std::vector<AddonInfo> addonInfo;
        if (tools)
        {
            addons.emplace("testcppcheck");
            addonInfo.emplace_back(/*AddonInfo()*/);
        }
        const auto s = dinit(Settings,
                             $.templateFormat = templateFormat,
                                 $.clangTidy = tools,
                                 $.addons = std::move (addons),
                                 $.addonInfos = std::move (addonInfo));
        Suppressions supprs;
        ErrorLogger2 errorLogger;
        CppCheck::ExecuteCmdFn f;
        if (tools && !nocmd) {
            f = getExecuteCommand(fname, called);
        }
        CppCheck cppcheck(s, supprs, errorLogger, false, f);
        ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(file.path(), Path::identify(file.path(), false), 0)));
        // TODO: how to properly disable these warnings?
        errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
            return id == "logChecker";
        }), errorLogger.ids.end());
        errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
            return msg.id == "logChecker";
        }), errorLogger.errmsgs.end());
        if (tools)
        {
            ASSERT_EQUALS(2, errorLogger.ids.size());
            auto it = errorLogger.errmsgs.cbegin();
            ASSERT_EQUALS("nullPointer", it->id);
            ++it;

            if (nocmd)
            {
                ASSERT_EQUALS("internalError", it->id);
                ASSERT_EQUALS("Bailing out from analysis: Checking file failed: Failed to execute addon - no command callback provided", it->shortMessage()); // TODO: add addon name

                // TODO: clang-tidy is currently not invoked for file inputs - see #12053
                // TODO: needs to become a proper error
                TODO_ASSERT_EQUALS("Failed to execute '" + exename_("clang-tidy") + "' (no command callback provided)\n", "", GET_REDIRECT_ERROUT);

                ASSERT_EQUALS(0, called); // not called because we check if the callback exists
            }
            else
            {
                ASSERT_EQUALS("internalError", it->id);
                ASSERT_EQUALS("Bailing out from analysis: Checking file failed: Failed to auto detect python", it->shortMessage()); // TODO: clarify what python is used for

                // TODO: we cannot check this because the python detection is cached globally so this result will different dependent on how the test is called
                //ASSERT_EQUALS(2, called);
            }
        }
        else
        {
            ASSERT_EQUALS(0, called);
            ASSERT_EQUALS(1, errorLogger.ids.size());
            ASSERT_EQUALS("nullPointer", *errorLogger.ids.cbegin());
        }
    }

    void checkWithFile() const {
        checkWithFileInternal("file.c", false);
    }

    void checkWithFileWithTools() const {
        checkWithFileInternal("file_tools.c", true);
    }

    void checkWithFileWithToolsNoCommand() const {
        checkWithFileInternal("file_tools_nocmd.c", true, true);
    }

    void checkWithFSInternal(const std::string& fname, bool tools, bool nocmd = false) const
    {
        REDIRECT;
        ScopedFile file(fname,
                        "void f()\n"
                        "{\n"
                        "  (void)(*((int*)0));\n"
                        "}");

        int called = 0;
        std::unordered_set<std::string> addons;
        std::vector<AddonInfo> addonInfo;
        if (tools)
        {
            addons.emplace("testcppcheck");
            addonInfo.emplace_back(/*AddonInfo()*/);
        }
        const auto s = dinit(Settings,
                             $.templateFormat = templateFormat,
                                 $.clangTidy = tools,
                                 $.addons = std::move (addons),
                                 $.addonInfos = std::move (addonInfo));
        Suppressions supprs;
        ErrorLogger2 errorLogger;
        CppCheck::ExecuteCmdFn f;
        if (tools && !nocmd) {
            f = getExecuteCommand(fname, called);
        }
        CppCheck cppcheck(s, supprs, errorLogger, false, f);
        FileSettings fs{file.path(), Path::identify(file.path(), false), 0};
        ASSERT_EQUALS(1, cppcheck.check(fs));
        // TODO: how to properly disable these warnings?
        errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
            return id == "logChecker";
        }), errorLogger.ids.end());
        errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
            return msg.id == "logChecker";
        }), errorLogger.errmsgs.end());
        if (tools)
        {
            ASSERT_EQUALS(2, errorLogger.ids.size());
            auto it = errorLogger.errmsgs.cbegin();
            ASSERT_EQUALS("nullPointer", it->id);
            ++it;

            if (nocmd)
            {
                ASSERT_EQUALS("internalError", it->id);
                ASSERT_EQUALS("Bailing out from analysis: Checking file failed: Failed to execute addon - no command callback provided", it->shortMessage()); // TODO: add addon name

                // TODO: needs to become a proper error
                ASSERT_EQUALS("Failed to execute '" + exename_("clang-tidy") + "' (no command callback provided)\n", GET_REDIRECT_ERROUT);

                ASSERT_EQUALS(0, called); // not called because we check if the callback exists
            }
            else
            {
                ASSERT_EQUALS("internalError", it->id);
                ASSERT_EQUALS("Bailing out from analysis: Checking file failed: Failed to auto detect python", it->shortMessage()); // TODO: clarify what python is used for

                // TODO: we cannot check this because the python detection is cached globally so this result will different dependent on how the test is called
                //ASSERT_EQUALS(3, called);
            }
        }
        else
        {
            ASSERT_EQUALS(0, called);
            ASSERT_EQUALS(1, errorLogger.ids.size());
            ASSERT_EQUALS("nullPointer", *errorLogger.ids.cbegin());
        }
    }

    void checkWithFS() const {
        checkWithFSInternal("fs.c", false);
    }

    void checkWithFSWithTools() const {
        checkWithFSInternal("fs_tools.c", true);
    }

    void checkWithFSWithToolsNoCommand() const {
        checkWithFSInternal("fs_tools_nocmd.c", true, true);
    }

    void suppress_error_library() const
    {
        ScopedFile file("suppr_err_lib.c",
                        "void f()\n"
                        "{\n"
                        "  (void)(*((int*)0));\n"
                        "}");

        const char xmldata[] = R"(<def format="2"><markup ext=".c" reporterrors="false"/></def>)";
        const Settings s = settingsBuilder().libraryxml(xmldata).build();
        Suppressions supprs;
        ErrorLogger2 errorLogger;
        CppCheck cppcheck(s, supprs, errorLogger, false, {});
        ASSERT_EQUALS(0, cppcheck.check(FileWithDetails(file.path(), Path::identify(file.path(), false), 0)));
        // TODO: how to properly disable these warnings?
        errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
            return id == "logChecker";
        }), errorLogger.ids.end());
        ASSERT_EQUALS(0, errorLogger.ids.size());
    }

    // TODO: how to actually get duplicated findings
    void unique_errors() const
    {
        ScopedFile file("inc.h",
                        "inline void f()\n"
                        "{\n"
                        "  (void)(*((int*)0));\n"
                        "}");
        ScopedFile test_file_a("a.c",
                               "#include \"inc.h\"");
        ScopedFile test_file_b("b.c",
                               "#include \"inc.h\"");

        // this is the "simple" format
        const auto s = dinit(Settings, $.templateFormat = templateFormat); // TODO: remove when we only longer rely on toString() in unique message handling
        Suppressions supprs;
        ErrorLogger2 errorLogger;
        CppCheck cppcheck(s, supprs, errorLogger, false, {});
        ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(test_file_a.path(), Path::identify(test_file_a.path(), false), 0)));
        ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(test_file_b.path(), Path::identify(test_file_b.path(), false), 0)));
        // TODO: how to properly disable these warnings?
        errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
            return msg.id == "logChecker";
        }), errorLogger.errmsgs.end());
        // the internal errorlist is cleared after each check() call
        ASSERT_EQUALS(2, errorLogger.errmsgs.size());
        auto it = errorLogger.errmsgs.cbegin();
        ASSERT_EQUALS("a.c", it->file0);
        ASSERT_EQUALS("nullPointer", it->id);
        ++it;
        ASSERT_EQUALS("b.c", it->file0);
        ASSERT_EQUALS("nullPointer", it->id);
    }

    void unique_errors_2() const
    {
        ScopedFile test_file("c.c",
                             "void f()\n"
                             "{\n"
                             "const long m[9] = {};\n"
                             "long a=m[9], b=m[9];\n"
                             "(void)a;\n"
                             "(void)b;\n"
                             "}");

        // this is the "simple" format
        const auto s = dinit(Settings, $.templateFormat = templateFormat); // TODO: remove when we only longer rely on toString() in unique message handling?
        Suppressions supprs;
        ErrorLogger2 errorLogger;
        CppCheck cppcheck(s, supprs, errorLogger, false, {});
        ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(test_file.path(), Path::identify(test_file.path(), false), 0)));
        // TODO: how to properly disable these warnings?
        errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
            return msg.id == "logChecker";
        }), errorLogger.errmsgs.end());
        // the internal errorlist is cleared after each check() call
        ASSERT_EQUALS(2, errorLogger.errmsgs.size());
        auto it = errorLogger.errmsgs.cbegin();
        ASSERT_EQUALS("c.c", it->file0);
        ASSERT_EQUALS(1, it->callStack.size());
        {
            auto stack = it->callStack.cbegin();
            ASSERT_EQUALS(4, stack->line);
            ASSERT_EQUALS(9, stack->column);
        }
        ASSERT_EQUALS("arrayIndexOutOfBounds", it->id);
        ++it;
        ASSERT_EQUALS("c.c", it->file0);
        ASSERT_EQUALS(1, it->callStack.size());
        {
            auto stack = it->callStack.cbegin();
            ASSERT_EQUALS(4, stack->line);
            ASSERT_EQUALS(17, stack->column);
        }
        ASSERT_EQUALS("arrayIndexOutOfBounds", it->id);
    }

    void isPremiumCodingStandardId() const {
        Suppressions supprs;
        ErrorLogger2 errorLogger;

        {
            const auto s = dinit(Settings, $.premiumArgs = "");
            CppCheck cppcheck(s, supprs, errorLogger, false, {});

            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("misra-c2012-0.0"));
            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("misra-c2023-0.0"));
            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c-2012-0.0"));
            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c-2023-0.0"));
            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c-2025-0.0"));
            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c-2025-dir-0.0"));
            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c++-2008-0-0-0"));
            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c++-2023-0.0.0"));
            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-cert-int50-cpp"));
            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-autosar-0-0-0"));
        }

        {
            const auto s = dinit(Settings, $.premiumArgs = "--misra-c-2012 --cert-c++-2016 --autosar");

            CppCheck cppcheck(s, supprs, errorLogger, false, {});

            ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("misra-c2012-0.0"));
            ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c-2012-0.0"));
            ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c-2023-0.0"));
            ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c-2025-0.0"));
            ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c-2025-dir-0.0"));
            ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c++-2008-0-0-0"));
            ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c++-2023-0.0.0"));
            ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-cert-int50-cpp"));
            ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-autosar-0-0-0"));
        }
    }

    void getDumpFileContentsRawTokens() const {
        Settings s;
        s.relativePaths = true;
        s.basePaths.emplace_back("/some/path");
        Suppressions supprs;
        ErrorLogger2 errorLogger;
        CppCheck cppcheck(s, supprs, errorLogger, false, {});
        std::vector<std::string> files{"/some/path/test.c"};
        simplecpp::TokenList tokens1(files);
        const std::string expected = "  <rawtokens>\n"
                                     "    <file index=\"0\" name=\"test.c\"/>\n"
                                     "  </rawtokens>\n";
        ASSERT_EQUALS(expected, cppcheck.getDumpFileContentsRawTokens(files, tokens1));

        const char code[] = "//x \\ \n"
                            "y\n"
                            ";\n";

        simplecpp::OutputList outputList;
        const simplecpp::TokenList tokens2(code, files, "", &outputList);
        const std::string expected2 = "  <rawtokens>\n"
                                      "    <file index=\"0\" name=\"test.c\"/>\n"
                                      "    <file index=\"1\" name=\"\"/>\n"
                                      "    <tok fileIndex=\"1\" linenr=\"1\" column=\"1\" str=\"//x &#10;y\"/>\n"
                                      "    <tok fileIndex=\"1\" linenr=\"3\" column=\"1\" str=\";\"/>\n"
                                      "  </rawtokens>\n";
        ASSERT_EQUALS(expected2, cppcheck.getDumpFileContentsRawTokens(files, tokens2));

    }

    void getDumpFileContentsLibrary() const {
        Suppressions supprs;
        ErrorLogger2 errorLogger;

        {
            Settings s;
            s.libraries.emplace_back("std.cfg");
            CppCheck cppcheck(s, supprs, errorLogger, false, {});
            //std::vector<std::string> files{ "/some/path/test.c" };
            const std::string expected = "  <library lib=\"std.cfg\"/>\n";
            ASSERT_EQUALS(expected, cppcheck.getLibraryDumpData());
        }

        {
            Settings s;
            s.libraries.emplace_back("std.cfg");
            s.libraries.emplace_back("posix.cfg");
            CppCheck cppcheck(s, supprs, errorLogger, false, {});
            const std::string expected = "  <library lib=\"std.cfg\"/>\n  <library lib=\"posix.cfg\"/>\n";
            ASSERT_EQUALS(expected, cppcheck.getLibraryDumpData());
        }
    }

    void checkPlistOutput() const {
        Suppressions supprs;
        ErrorLogger2 errorLogger;
        std::vector<std::string> files = {"textfile.txt"};

        {
            const auto s = dinit(Settings, $.templateFormat = templateFormat, $.plistOutput = "output");
            const ScopedFile file("file", "");
            CppCheck cppcheck(s, supprs, errorLogger, false, {});
            const FileWithDetails fileWithDetails {file.path(), Path::identify(file.path(), false), 0};

            cppcheck.checkPlistOutput(fileWithDetails, files);
            const std::string outputFile {"outputfile_" + std::to_string(std::hash<std::string> {}(fileWithDetails.spath())) + ".plist"};
            ASSERT(Path::exists(outputFile));
            std::remove(outputFile.c_str());
        }

        {
            const auto s = dinit(Settings, $.plistOutput = "output");
            const ScopedFile file("file.c", "");
            CppCheck cppcheck(s, supprs, errorLogger, false, {});
            const FileWithDetails fileWithDetails {file.path(), Path::identify(file.path(), false), 0};

            cppcheck.checkPlistOutput(fileWithDetails, files);
            const std::string outputFile {"outputfile_" + std::to_string(std::hash<std::string> {}(fileWithDetails.spath())) + ".plist"};
            ASSERT(Path::exists(outputFile));
            std::remove(outputFile.c_str());
        }

        {
            Settings s;
            const ScopedFile file("file.c", "");
            CppCheck cppcheck(s, supprs, errorLogger, false, {});
            cppcheck.checkPlistOutput(FileWithDetails(file.path(), Path::identify(file.path(), false), 0), files);
        }
    }

    void premiumResultsCache() const {
        // Trac #13889 - cached misra results are shown after removing --premium=misra-c-2012 option

        Settings settings;
        Suppressions supprs;
        ErrorLogger2 errorLogger;

        std::vector<std::string> files;

        const char code[] = "void f();\nint x;\n";
        simplecpp::TokenList tokens(code, files, "m1.c");

        Preprocessor preprocessor(tokens, settings, errorLogger, Standards::Language::C);
        ASSERT(preprocessor.loadFiles(files));

        AddonInfo premiumaddon;
        premiumaddon.name = "premiumaddon.json";
        premiumaddon.executable = "premiumaddon";

        settings.cppcheckCfgProductName = "Cppcheck Premium 0.0.0";
        settings.addons.insert(premiumaddon.name);
        settings.addonInfos.push_back(premiumaddon);

        settings.premiumArgs = "misra-c-2012";
        CppCheck check(settings, supprs, errorLogger, false, {});
        const size_t hash1 = check.calculateHash(preprocessor);

        settings.premiumArgs = "";
        const size_t hash2 = check.calculateHash(preprocessor);

        // cppcheck-suppress knownConditionTrueFalse
        ASSERT(hash1 != hash2);
    }

    void purgedConfiguration() const
    {
        ScopedFile test_file("test.cpp",
                             "#ifdef X\n"
                             "#endif\n"
                             "int main() {}\n");

        // this is the "simple" format
        const auto s = dinit(Settings,
                             $.templateFormat = templateFormat, // TODO: remove when we only longer rely on toString() in unique message handling
                                 $.severity.enable (Severity::information);
                             $.debugwarnings = true);
        Suppressions supprs;
        ErrorLogger2 errorLogger;
        CppCheck cppcheck(s, supprs, errorLogger, false, {});
        ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(test_file.path(), Path::identify(test_file.path(), false), 0)));
        // TODO: how to properly disable these warnings?
        errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
            return msg.id == "logChecker";
        }), errorLogger.errmsgs.end());
        // the internal errorlist is cleared after each check() call
        ASSERT_EQUALS(1, errorLogger.errmsgs.size());
        auto it = errorLogger.errmsgs.cbegin();
        ASSERT_EQUALS("test.cpp:0:0: information: The configuration 'X' was not checked because its code equals another one. [purgedConfiguration]",
                      it->toString(false, templateFormat, ""));
    }

    // TODO: test suppressions
    // TODO: test all with FS
};

REGISTER_TEST(TestCppcheck)