Coverage Report

Created: 2025-02-17 06:38

/src/cppcheck/lib/checkunusedfunctions.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- C++ -*-
2
 * Cppcheck - A tool for static C/C++ code analysis
3
 * Copyright (C) 2007-2024 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
20
//---------------------------------------------------------------------------
21
#ifndef checkunusedfunctionsH
22
#define checkunusedfunctionsH
23
//---------------------------------------------------------------------------
24
25
#include "config.h"
26
27
#include <list>
28
#include <set>
29
#include <string>
30
#include <unordered_map>
31
32
class ErrorLogger;
33
class Function;
34
class Settings;
35
class Tokenizer;
36
37
/** @brief Check for functions never called */
38
/// @{
39
40
class CPPCHECKLIB CheckUnusedFunctions {
41
    friend class TestSuppressions;
42
    friend class TestSingleExecutorBase;
43
    friend class TestProcessExecutorBase;
44
    friend class TestThreadExecutorBase;
45
    friend class TestUnusedFunctions;
46
47
public:
48
1.76k
    CheckUnusedFunctions() = default;
49
50
    // Parse current tokens and determine..
51
    // * Check what functions are used
52
    // * What functions are declared
53
    void parseTokens(const Tokenizer &tokenizer, const Settings &settings);
54
55
    std::string analyzerInfo() const;
56
57
    static void analyseWholeProgram(const Settings &settings, ErrorLogger& errorLogger, const std::string &buildDir);
58
59
0
    static void getErrorMessages(ErrorLogger &errorLogger) {
60
0
        unusedFunctionError(errorLogger, "", 0, 0, "funcName");
61
0
    }
62
63
    // Return true if an error is reported.
64
    bool check(const Settings& settings, ErrorLogger& errorLogger) const;
65
66
    void updateFunctionData(const CheckUnusedFunctions& check);
67
68
private:
69
    static void unusedFunctionError(ErrorLogger& errorLogger,
70
                                    const std::string &filename, unsigned int fileIndex, unsigned int lineNumber,
71
                                    const std::string &funcname);
72
73
    struct CPPCHECKLIB FunctionUsage {
74
        std::string filename;
75
        unsigned int lineNumber{};
76
        unsigned int fileIndex{};
77
        bool usedSameFile{};
78
        bool usedOtherFile{};
79
        bool isC{};
80
        bool isStatic{};
81
    };
82
83
    std::unordered_map<std::string, FunctionUsage> mFunctions;
84
85
    class CPPCHECKLIB FunctionDecl {
86
    public:
87
        explicit FunctionDecl(const Function *f);
88
        std::string functionName;
89
        std::string fileName;
90
        unsigned int lineNumber;
91
    };
92
    std::list<FunctionDecl> mFunctionDecl;
93
    std::set<std::string> mFunctionCalls;
94
};
95
/// @}
96
//---------------------------------------------------------------------------
97
#endif // checkunusedfunctionsH