Cppcheck
checkunusedfunctions.h
Go to the documentation of this file.
1 /*
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 
41  friend class TestSuppressions;
42  friend class TestSingleExecutorBase;
43  friend class TestProcessExecutorBase;
44  friend class TestThreadExecutorBase;
45  friend class TestUnusedFunctions;
46 
47 public:
48  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  static void getErrorMessages(ErrorLogger &errorLogger) {
60  unusedFunctionError(errorLogger, emptyString, 0, 0, "funcName");
61  }
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 
74  std::string filename;
75  unsigned int lineNumber{};
76  unsigned int fileIndex{};
77  bool usedSameFile{};
78  bool usedOtherFile{};
79  };
80 
81  std::unordered_map<std::string, FunctionUsage> mFunctions;
82 
84  public:
85  explicit FunctionDecl(const Function *f);
86  std::string functionName;
87  unsigned int lineNumber;
88  };
89  std::list<FunctionDecl> mFunctionDecl;
90  std::set<std::string> mFunctionCalls;
91 };
92 /// @}
93 //---------------------------------------------------------------------------
94 #endif // checkunusedfunctionsH
static const std::string FunctionDecl
Definition: clangimport.cpp:93
Check for functions never called.
std::unordered_map< std::string, FunctionUsage > mFunctions
static void getErrorMessages(ErrorLogger &errorLogger)
CheckUnusedFunctions()=default
std::list< FunctionDecl > mFunctionDecl
std::set< std::string > mFunctionCalls
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
The main purpose is to tokenize the source code.
Definition: tokenize.h:46
static const std::string emptyString
Definition: config.h:127
#define CPPCHECKLIB
Definition: config.h:35