1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2021 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 "platform.h"
26 #include "utils.h"
27 
28 #include <list>
29 #include <map>
30 #include <set>
31 #include <string>
32 #include <vector>
33 
34 /// @addtogroup Core
35 /// @{
36 
37 namespace cppcheck {
38     struct stricmp {
operatorstricmp39         bool operator()(const std::string &lhs, const std::string &rhs) const {
40             return caseInsensitiveStringCompare(lhs,rhs) < 0;
41         }
42     };
43 }
44 
45 class Settings;
46 
47 /**
48  * @brief Importing project settings.
49  */
50 class CPPCHECKLIB ImportProject {
51 public:
52     enum class Type {
53         UNKNOWN,
54         MISSING,
55         COMPILE_DB,
56         VS_SLN,
57         VS_VCXPROJ,
58         BORLAND,
59         CPPCHECK_GUI
60     };
61 
62     /** File settings. Multiple configurations for a file is allowed. */
63     struct CPPCHECKLIB FileSettings {
FileSettingsFileSettings64         FileSettings() : platformType(cppcheck::Platform::Unspecified), msc(false), useMfc(false) {}
65         std::string cfg;
66         std::string filename;
67         std::string defines;
cppcheckDefinesFileSettings68         std::string cppcheckDefines() const {
69             return defines + (msc ? ";_MSC_VER=1900" : "") + (useMfc ? ";__AFXWIN_H__=1" : "");
70         }
71         std::set<std::string> undefs;
72         std::list<std::string> includePaths;
73         std::list<std::string> systemIncludePaths;
74         std::string standard;
75         cppcheck::Platform::PlatformType platformType;
76         bool msc;
77         bool useMfc;
78 
79         void parseCommand(std::string command);
80         void setDefines(std::string defs);
81         void setIncludePaths(const std::string &basepath, const std::list<std::string> &in, std::map<std::string, std::string, cppcheck::stricmp> &variables);
82     };
83     std::list<FileSettings> fileSettings;
84     Type projectType;
85 
86     ImportProject();
87 
88     void selectOneVsConfig(cppcheck::Platform::PlatformType platform);
89 
90     std::list<std::string> getVSConfigs();
91 
92     // Cppcheck GUI output
93     struct {
94         std::string analyzeAllVsConfigs;
95         std::vector<std::string> pathNames;
96         std::list<std::string> libraries;
97         std::list<std::string> excludedPaths;
98         std::list<std::string> checkVsConfigs;
99         std::string projectFile;
100         std::string platform;
101     } guiProject;
102 
103     void ignorePaths(const std::vector<std::string> &ipaths);
104     void ignoreOtherConfigs(const std::string &cfg);
105     void ignoreOtherPlatforms(cppcheck::Platform::PlatformType platformType);
106 
107     Type import(const std::string &filename, Settings *settings=nullptr);
108 protected:
109     void importCompileCommands(std::istream &istr);
110     bool importCppcheckGuiProject(std::istream &istr, Settings *settings);
111 private:
112     void importSln(std::istream &istr, const std::string &path, const std::string &fileFilter);
113     void importVcxproj(const std::string &filename, std::map<std::string, std::string, cppcheck::stricmp> &variables, const std::string &additionalIncludeDirectories, const std::string &fileFilter);
114     void importBcb6Prj(const std::string &projectFilename);
115 
116     void setRelativePaths(const std::string &filename);
117 
118     std::string mPath;
119     std::set<std::string> mAllVSConfigs;
120 };
121 
122 
123 namespace CppcheckXml {
124     const char ProjectElementName[] = "project";
125     const char ProjectVersionAttrib[] = "version";
126     const char ProjectFileVersion[] = "1";
127     const char BuildDirElementName[] = "builddir";
128     const char ImportProjectElementName[] = "importproject";
129     const char AnalyzeAllVsConfigsElementName[] = "analyze-all-vs-configs";
130     const char Parser[] = "parser";
131     const char BugHunting[] = "bug-hunting";
132     const char IncludeDirElementName[] = "includedir";
133     const char DirElementName[] = "dir";
134     const char DirNameAttrib[] = "name";
135     const char DefinesElementName[] = "defines";
136     const char DefineName[] = "define";
137     const char DefineNameAttrib[] = "name";
138     const char UndefinesElementName[] = "undefines";
139     const char UndefineName[] = "undefine";
140     const char PathsElementName[] = "paths";
141     const char PathName[] = "dir";
142     const char PathNameAttrib[] = "name";
143     const char RootPathName[] = "root";
144     const char RootPathNameAttrib[] = "name";
145     const char IgnoreElementName[] = "ignore";
146     const char IgnorePathName[] = "path";
147     const char IgnorePathNameAttrib[] = "name";
148     const char ExcludeElementName[] = "exclude";
149     const char ExcludePathName[] = "path";
150     const char ExcludePathNameAttrib[] = "name";
151     const char FunctionContracts[] = "function-contracts";
152     const char FunctionContract[] = "contract";
153     const char ContractFunction[] = "function";
154     const char ContractExpects[] = "expects";
155     const char VariableContractsElementName[] = "variable-contracts";
156     const char VariableContractItemElementName[] = "var";
157     const char VariableContractVarName[] = "name";
158     const char VariableContractMin[] = "min";
159     const char VariableContractMax[] = "max";
160     const char LibrariesElementName[] = "libraries";
161     const char LibraryElementName[] = "library";
162     const char PlatformElementName[] = "platform";
163     const char SuppressionsElementName[] = "suppressions";
164     const char SuppressionElementName[] = "suppression";
165     const char AddonElementName[] = "addon";
166     const char AddonsElementName[] = "addons";
167     const char ToolElementName[] = "tool";
168     const char ToolsElementName[] = "tools";
169     const char TagsElementName[] = "tags";
170     const char TagElementName[] = "tag";
171     const char TagWarningsElementName[] = "tag-warnings";
172     const char TagAttributeName[] = "tag";
173     const char WarningElementName[] = "warning";
174     const char HashAttributeName[] = "hash";
175     const char CheckHeadersElementName[] = "check-headers";
176     const char CheckUnusedTemplatesElementName[] = "check-unused-templates";
177     const char MaxCtuDepthElementName[] = "max-ctu-depth";
178     const char MaxTemplateRecursionElementName[] = "max-template-recursion";
179     const char CheckUnknownFunctionReturn[] = "check-unknown-function-return-values";
180     const char ClangTidy[] = "clang-tidy";
181     const char Name[] = "name";
182     const char VSConfigurationElementName[] = "vs-configurations";
183     const char VSConfigurationName[] = "config";
184 }
185 
186 /// @}
187 //---------------------------------------------------------------------------
188 #endif // importprojectH
189