Cppcheck
preprocessor.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 #ifndef preprocessorH
21 #define preprocessorH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 
26 #include <cstddef>
27 #include <istream>
28 #include <list>
29 #include <map>
30 #include <set>
31 #include <string>
32 #include <vector>
33 
34 #include <simplecpp.h>
35 
36 class ErrorLogger;
37 class Settings;
38 class SuppressionList;
39 
40 /**
41  * @brief A preprocessor directive
42  * Each preprocessor directive (\#include, \#define, \#undef, \#if, \#ifdef, \#else, \#endif)
43  * will be recorded as an instance of this class.
44  *
45  * file and linenr denote the location where where the directive is defined.
46  *
47  */
48 
50  /** name of (possibly included) file where directive is defined */
51  std::string file;
52 
53  /** line number in (possibly included) file where directive is defined */
54  unsigned int linenr;
55 
56  /** the actual directive text */
57  std::string str;
58 
59  /** record a directive (possibly filtering src) */
60  Directive(std::string _file, const int _linenr, const std::string &_str);
61 };
62 
63 /// @addtogroup Core
64 /// @{
65 
66 /**
67  * @brief The cppcheck preprocessor.
68  * The preprocessor has special functionality for extracting the various ifdef
69  * configurations that exist in a source file.
70  */
72  // TODO: get rid of this
73  friend class PreprocessorHelper;
74  friend class TestPreprocessor;
75  friend class TestUnusedVar;
76 
77 public:
78 
79  /**
80  * Include file types.
81  */
82  enum HeaderTypes {
83  UserHeader = 1,
84  SystemHeader
85  };
86 
87  /** character that is inserted in expanded macros */
88  static char macroChar;
89 
90  explicit Preprocessor(const Settings& settings, ErrorLogger &errorLogger);
91  virtual ~Preprocessor();
92 
93  void inlineSuppressions(const simplecpp::TokenList &tokens, SuppressionList &suppressions);
94 
95  std::list<Directive> createDirectives(const simplecpp::TokenList &tokens) const;
96 
97  std::set<std::string> getConfigs(const simplecpp::TokenList &tokens) const;
98 
99  void handleErrors(const simplecpp::OutputList &outputList, bool throwError);
100 
101  bool loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files);
102 
103  void removeComments();
104 
105  void setPlatformInfo(simplecpp::TokenList *tokens) const;
106 
107  simplecpp::TokenList preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, bool throwError = false);
108 
109  std::string getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, const bool writeLocations);
110 
111  /**
112  * Calculate HASH. Using toolinfo, tokens1, filedata.
113  *
114  * @param tokens1 Sourcefile tokens
115  * @param toolinfo Arbitrary extra toolinfo
116  * @return HASH
117  */
118  std::size_t calculateHash(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const;
119 
120  void simplifyPragmaAsm(simplecpp::TokenList *tokenList) const;
121 
122  static void getErrorMessages(ErrorLogger &errorLogger, const Settings &settings);
123 
124  /**
125  * dump all directives present in source file
126  */
127  void dump(std::ostream &out) const;
128 
129  void reportOutput(const simplecpp::OutputList &outputList, bool showerror);
130 
131  static bool hasErrors(const simplecpp::Output &output);
132 
133 private:
134  static void simplifyPragmaAsmPrivate(simplecpp::TokenList *tokenList);
135 
136  void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
137  void error(const std::string &filename, unsigned int linenr, const std::string &msg);
138 
139  static bool hasErrors(const simplecpp::OutputList &outputList);
140 
143 
144  /** list of all directives met while preprocessing file */
145 
146  std::map<std::string, simplecpp::TokenList *> mTokenLists;
147 
148  /** filename for cpp/c file - useful when reporting errors */
149  std::string mFile0;
150 
151  /** simplecpp tracking info */
152  std::list<simplecpp::MacroUsage> mMacroUsage;
153  std::list<simplecpp::IfCond> mIfCond;
154 };
155 
156 /// @}
157 //---------------------------------------------------------------------------
158 #endif // preprocessorH
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
The cppcheck preprocessor.
Definition: preprocessor.h:71
HeaderTypes
Include file types.
Definition: preprocessor.h:82
std::list< simplecpp::MacroUsage > mMacroUsage
simplecpp tracking info
Definition: preprocessor.h:152
std::list< simplecpp::IfCond > mIfCond
Definition: preprocessor.h:153
std::string mFile0
filename for cpp/c file - useful when reporting errors
Definition: preprocessor.h:149
const Settings & mSettings
Definition: preprocessor.h:141
std::map< std::string, simplecpp::TokenList * > mTokenLists
list of all directives met while preprocessing file
Definition: preprocessor.h:146
static char macroChar
character that is inserted in expanded macros
Definition: preprocessor.h:88
ErrorLogger & mErrorLogger
Definition: preprocessor.h:142
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
class for handling suppressions
Definition: suppressions.h:42
#define WARN_UNUSED
Definition: config.h:110
#define CPPCHECKLIB
Definition: config.h:35
@ error
Programming error.
@ missingInclude
static std::string cfg(const std::vector< std::string > &configs, const std::string &userDefines)
static void getConfigs(const simplecpp::TokenList &tokens, std::set< std::string > &defined, const std::string &userDefines, const std::set< std::string > &undefined, std::set< std::string > &ret)
A preprocessor directive Each preprocessor directive (#include, #define, #undef, #if,...
Definition: preprocessor.h:49
std::string file
name of (possibly included) file where directive is defined
Definition: preprocessor.h:51
std::string str
the actual directive text
Definition: preprocessor.h:57
unsigned int linenr
line number in (possibly included) file where directive is defined
Definition: preprocessor.h:54