Cppcheck
importproject.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 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 importprojectH
21 #define importprojectH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 #include "filesettings.h"
26 #include "platform.h"
27 #include "utils.h"
28 
29 #include <iosfwd>
30 #include <list>
31 #include <map>
32 #include <set>
33 #include <string>
34 #include <vector>
35 
36 /// @addtogroup Core
37 /// @{
38 
39 namespace cppcheck {
40  struct stricmp {
41  bool operator()(const std::string &lhs, const std::string &rhs) const {
42  return caseInsensitiveStringCompare(lhs,rhs) < 0;
43  }
44  };
45 }
46 
47 class Settings;
48 
49 /**
50  * @brief Importing project settings.
51  */
53 public:
54  enum class Type {
55  NONE,
56  UNKNOWN,
57  MISSING,
58  FAILURE,
59  COMPILE_DB,
60  VS_SLN,
61  VS_VCXPROJ,
62  BORLAND,
63  CPPCHECK_GUI
64  };
65 
66  static void fsParseCommand(FileSettings& fs, const std::string& command);
67  static void fsSetDefines(FileSettings& fs, std::string defs);
68  static void fsSetIncludePaths(FileSettings& fs, const std::string &basepath, const std::list<std::string> &in, std::map<std::string, std::string, cppcheck::stricmp> &variables);
69 
70  std::list<FileSettings> fileSettings;
71  Type projectType{Type::NONE};
72 
73  ImportProject() = default;
74  virtual ~ImportProject() = default;
75  ImportProject(const ImportProject&) = default;
77 
78  void selectOneVsConfig(Platform::Type platform);
79  void selectVsConfigurations(Platform::Type platform, const std::vector<std::string> &configurations);
80 
81  std::list<std::string> getVSConfigs();
82 
83  // Cppcheck GUI output
84  struct {
85  std::string analyzeAllVsConfigs;
86  std::vector<std::string> pathNames;
87  std::list<std::string> libraries;
88  std::list<std::string> excludedPaths;
89  std::list<std::string> checkVsConfigs;
90  std::string projectFile;
91  std::string platform;
92  } guiProject;
93 
94  void ignorePaths(const std::vector<std::string> &ipaths);
95  void ignoreOtherConfigs(const std::string &cfg);
96 
97  Type import(const std::string &filename, Settings *settings=nullptr);
98 protected:
99  bool importCompileCommands(std::istream &istr);
100  bool importCppcheckGuiProject(std::istream &istr, Settings *settings);
101  virtual bool sourceFileExists(const std::string &file);
102 private:
103  bool importSln(std::istream &istr, const std::string &path, const std::vector<std::string> &fileFilters);
104  bool importVcxproj(const std::string &filename, std::map<std::string, std::string, cppcheck::stricmp> &variables, const std::string &additionalIncludeDirectories, const std::vector<std::string> &fileFilters);
105  bool importBcb6Prj(const std::string &projectFilename);
106 
107  static void printError(const std::string &message);
108 
109  void setRelativePaths(const std::string &filename);
110 
111  std::string mPath;
112  std::set<std::string> mAllVSConfigs;
113 };
114 
115 
116 namespace CppcheckXml {
117  static constexpr char ProjectElementName[] = "project";
118  static constexpr char ProjectVersionAttrib[] = "version";
119  static constexpr char ProjectFileVersion[] = "1";
120  static constexpr char BuildDirElementName[] = "builddir";
121  static constexpr char ImportProjectElementName[] = "importproject";
122  static constexpr char AnalyzeAllVsConfigsElementName[] = "analyze-all-vs-configs";
123  static constexpr char Parser[] = "parser";
124  static constexpr char IncludeDirElementName[] = "includedir";
125  static constexpr char DirElementName[] = "dir";
126  static constexpr char DirNameAttrib[] = "name";
127  static constexpr char DefinesElementName[] = "defines";
128  static constexpr char DefineName[] = "define";
129  static constexpr char DefineNameAttrib[] = "name";
130  static constexpr char UndefinesElementName[] = "undefines";
131  static constexpr char UndefineName[] = "undefine";
132  static constexpr char PathsElementName[] = "paths";
133  static constexpr char PathName[] = "dir";
134  static constexpr char PathNameAttrib[] = "name";
135  static constexpr char RootPathName[] = "root";
136  static constexpr char RootPathNameAttrib[] = "name";
137  static constexpr char IgnoreElementName[] = "ignore";
138  static constexpr char IgnorePathName[] = "path";
139  static constexpr char IgnorePathNameAttrib[] = "name";
140  static constexpr char ExcludeElementName[] = "exclude";
141  static constexpr char ExcludePathName[] = "path";
142  static constexpr char ExcludePathNameAttrib[] = "name";
143  static constexpr char FunctionContracts[] = "function-contracts";
144  static constexpr char VariableContractsElementName[] = "variable-contracts";
145  static constexpr char LibrariesElementName[] = "libraries";
146  static constexpr char LibraryElementName[] = "library";
147  static constexpr char PlatformElementName[] = "platform";
148  static constexpr char SuppressionsElementName[] = "suppressions";
149  static constexpr char SuppressionElementName[] = "suppression";
150  static constexpr char AddonElementName[] = "addon";
151  static constexpr char AddonsElementName[] = "addons";
152  static constexpr char ToolElementName[] = "tool";
153  static constexpr char ToolsElementName[] = "tools";
154  static constexpr char TagsElementName[] = "tags";
155  static constexpr char TagElementName[] = "tag";
156  static constexpr char TagWarningsElementName[] = "tag-warnings";
157  static constexpr char TagAttributeName[] = "tag";
158  static constexpr char WarningElementName[] = "warning";
159  static constexpr char HashAttributeName[] = "hash";
160  static constexpr char CheckLevelExhaustiveElementName[] = "check-level-exhaustive";
161  static constexpr char CheckHeadersElementName[] = "check-headers";
162  static constexpr char CheckUnusedTemplatesElementName[] = "check-unused-templates";
163  static constexpr char MaxCtuDepthElementName[] = "max-ctu-depth";
164  static constexpr char MaxTemplateRecursionElementName[] = "max-template-recursion";
165  static constexpr char CheckUnknownFunctionReturn[] = "check-unknown-function-return-values";
166  static constexpr char ClangTidy[] = "clang-tidy";
167  static constexpr char Name[] = "name";
168  static constexpr char VSConfigurationElementName[] = "vs-configurations";
169  static constexpr char VSConfigurationName[] = "config";
170  // Cppcheck Premium
171  static constexpr char BughuntingElementName[] = "bug-hunting";
172  static constexpr char CodingStandardsElementName[] = "coding-standards";
173  static constexpr char CodingStandardElementName[] = "coding-standard";
174  static constexpr char CertIntPrecisionElementName[] = "cert-c-int-precision";
175  static constexpr char ProjectNameElementName[] = "project-name";
176 }
177 
178 /// @}
179 //---------------------------------------------------------------------------
180 #endif // importprojectH
Importing project settings.
Definition: importproject.h:52
std::list< std::string > excludedPaths
Definition: importproject.h:88
std::list< FileSettings > fileSettings
Definition: importproject.h:70
ImportProject(const ImportProject &)=default
std::string mPath
ImportProject()=default
ImportProject & operator=(const ImportProject &)=default
std::string analyzeAllVsConfigs
Definition: importproject.h:85
std::string projectFile
Definition: importproject.h:90
std::string platform
Definition: importproject.h:91
std::set< std::string > mAllVSConfigs
std::list< std::string > checkVsConfigs
Definition: importproject.h:89
virtual ~ImportProject()=default
std::vector< std::string > pathNames
Definition: importproject.h:86
std::list< std::string > libraries
Definition: importproject.h:87
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
Information about a class type.
#define WARN_UNUSED
Definition: config.h:110
#define CPPCHECKLIB
Definition: config.h:35
static constexpr char RootPathName[]
static constexpr char VSConfigurationName[]
static constexpr char DirElementName[]
static constexpr char DefineName[]
static constexpr char ExcludePathNameAttrib[]
static constexpr char PathsElementName[]
static constexpr char VariableContractsElementName[]
static constexpr char FunctionContracts[]
static constexpr char DefinesElementName[]
static constexpr char LibrariesElementName[]
static constexpr char ToolsElementName[]
static constexpr char IncludeDirElementName[]
static constexpr char ExcludeElementName[]
static constexpr char ProjectElementName[]
static constexpr char CertIntPrecisionElementName[]
static constexpr char PlatformElementName[]
static constexpr char TagsElementName[]
static constexpr char RootPathNameAttrib[]
static constexpr char CodingStandardElementName[]
static constexpr char IgnorePathNameAttrib[]
static constexpr char CodingStandardsElementName[]
static constexpr char AddonsElementName[]
static constexpr char UndefinesElementName[]
static constexpr char ImportProjectElementName[]
static constexpr char Parser[]
static constexpr char BughuntingElementName[]
static constexpr char AddonElementName[]
static constexpr char ProjectVersionAttrib[]
static constexpr char CheckUnknownFunctionReturn[]
static constexpr char TagWarningsElementName[]
static constexpr char TagAttributeName[]
static constexpr char WarningElementName[]
static constexpr char DefineNameAttrib[]
static constexpr char AnalyzeAllVsConfigsElementName[]
static constexpr char TagElementName[]
static constexpr char ProjectNameElementName[]
static constexpr char BuildDirElementName[]
static constexpr char MaxTemplateRecursionElementName[]
static constexpr char ClangTidy[]
static constexpr char ExcludePathName[]
static constexpr char CheckUnusedTemplatesElementName[]
static constexpr char VSConfigurationElementName[]
static constexpr char CheckLevelExhaustiveElementName[]
static constexpr char DirNameAttrib[]
static constexpr char MaxCtuDepthElementName[]
static constexpr char UndefineName[]
static constexpr char HashAttributeName[]
static constexpr char SuppressionElementName[]
static constexpr char Name[]
static constexpr char IgnoreElementName[]
static constexpr char IgnorePathName[]
static constexpr char ProjectFileVersion[]
static constexpr char ToolElementName[]
static constexpr char SuppressionsElementName[]
static constexpr char CheckHeadersElementName[]
static constexpr char PathName[]
static constexpr char PathNameAttrib[]
static constexpr char LibraryElementName[]
static std::string cfg(const std::vector< std::string > &configs, const std::string &userDefines)
File settings.
Definition: filesettings.h:57
bool operator()(const std::string &lhs, const std::string &rhs) const
Definition: importproject.h:41
int caseInsensitiveStringCompare(const std::string &lhs, const std::string &rhs)
Definition: utils.cpp:28