Coverage Report

Created: 2025-01-24 06:31

/src/cppcheck/oss-fuzz/build/importproject.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "matchcompiler.h"
2
#include <string>
3
#include <cstring>
4
/*
5
 * Cppcheck - A tool for static C/C++ code analysis
6
 * Copyright (C) 2007-2024 Cppcheck team.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "importproject.h"
23
24
#include "path.h"
25
#include "settings.h"
26
#include "standards.h"
27
#include "suppressions.h"
28
#include "token.h"
29
#include "tokenlist.h"
30
#include "utils.h"
31
32
#include <algorithm>
33
#include <cstdlib>
34
#include <cstring>
35
#include <fstream>
36
#include <iostream>
37
#include <iterator>
38
#include <sstream>
39
#include <stack>
40
#include <unordered_set>
41
#include <utility>
42
43
#include "xml.h"
44
45
#include "json.h"
46
47
// TODO: align the exclusion logic with PathMatch
48
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths)
49
0
{
50
0
    for (auto it = fileSettings.cbegin(); it != fileSettings.cend();) {
51
0
        bool ignore = false;
52
0
        for (std::string i : ipaths) {
53
0
            if (it->filename().size() > i.size() && it->filename().compare(0,i.size(),i)==0) {
54
0
                ignore = true;
55
0
                break;
56
0
            }
57
0
            if (isValidGlobPattern(i) && matchglob(i, it->filename())) {
58
0
                ignore = true;
59
0
                break;
60
0
            }
61
0
            if (!Path::isAbsolute(i)) {
62
0
                i = mPath + i;
63
0
                if (it->filename().size() > i.size() && it->filename().compare(0,i.size(),i)==0) {
64
0
                    ignore = true;
65
0
                    break;
66
0
                }
67
0
            }
68
0
        }
69
0
        if (ignore)
70
0
            it = fileSettings.erase(it);
71
0
        else
72
0
            ++it;
73
0
    }
74
0
}
75
76
void ImportProject::ignoreOtherConfigs(const std::string &cfg)
77
0
{
78
0
    for (auto it = fileSettings.cbegin(); it != fileSettings.cend();) {
79
0
        if (it->cfg != cfg)
80
0
            it = fileSettings.erase(it);
81
0
        else
82
0
            ++it;
83
0
    }
84
0
}
85
86
void ImportProject::fsSetDefines(FileSettings& fs, std::string defs)
87
0
{
88
0
    while (defs.find(";%(") != std::string::npos) {
89
0
        const std::string::size_type pos1 = defs.find(";%(");
90
0
        const std::string::size_type pos2 = defs.find(';', pos1+1);
91
0
        defs.erase(pos1, pos2 == std::string::npos ? pos2 : (pos2-pos1));
92
0
    }
93
0
    while (defs.find(";;") != std::string::npos)
94
0
        defs.erase(defs.find(";;"),1);
95
0
    while (!defs.empty() && defs[0] == ';')
96
0
        defs.erase(0, 1);
97
0
    while (!defs.empty() && endsWith(defs,';'))
98
0
        defs.erase(defs.size() - 1U); // TODO: Use std::string::pop_back() as soon as travis supports it
99
0
    bool eq = false;
100
0
    for (std::size_t pos = 0; pos < defs.size(); ++pos) {
101
0
        if (defs[pos] == '(' || defs[pos] == '=')
102
0
            eq = true;
103
0
        else if (defs[pos] == ';') {
104
0
            if (!eq) {
105
0
                defs.insert(pos,"=1");
106
0
                pos += 3;
107
0
            }
108
0
            if (pos < defs.size())
109
0
                eq = false;
110
0
        }
111
0
    }
112
0
    if (!eq && !defs.empty())
113
0
        defs += "=1";
114
0
    fs.defines.swap(defs);
115
0
}
116
117
static bool simplifyPathWithVariables(std::string &s, std::map<std::string, std::string, cppcheck::stricmp> &variables)
118
0
{
119
0
    std::set<std::string, cppcheck::stricmp> expanded;
120
0
    std::string::size_type start = 0;
121
0
    while ((start = s.find("$(")) != std::string::npos) {
122
0
        const std::string::size_type end = s.find(')',start);
123
0
        if (end == std::string::npos)
124
0
            break;
125
0
        const std::string var = s.substr(start+2,end-start-2);
126
0
        if (expanded.find(var) != expanded.end())
127
0
            break;
128
0
        expanded.insert(var);
129
0
        auto it1 = utils::as_const(variables).find(var);
130
        // variable was not found within defined variables
131
0
        if (it1 == variables.end()) {
132
0
            const char *envValue = std::getenv(var.c_str());
133
0
            if (!envValue) {
134
                //! \todo generate a debug/info message about undefined variable
135
0
                break;
136
0
            }
137
0
            variables[var] = std::string(envValue);
138
0
            it1 = variables.find(var);
139
0
        }
140
0
        s.replace(start, end - start + 1, it1->second);
141
0
    }
142
0
    if (s.find("$(") != std::string::npos)
143
0
        return false;
144
0
    s = Path::simplifyPath(std::move(s));
145
0
    return true;
146
0
}
147
148
void ImportProject::fsSetIncludePaths(FileSettings& fs, const std::string &basepath, const std::list<std::string> &in, std::map<std::string, std::string, cppcheck::stricmp> &variables)
149
0
{
150
0
    std::set<std::string> found;
151
    // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
152
0
    const std::list<std::string> copyIn(in);
153
0
    fs.includePaths.clear();
154
0
    for (const std::string &ipath : copyIn) {
155
0
        if (ipath.empty())
156
0
            continue;
157
0
        if (startsWith(ipath,"%("))
158
0
            continue;
159
0
        std::string s(Path::fromNativeSeparators(ipath));
160
0
        if (!found.insert(s).second)
161
0
            continue;
162
0
        if (s[0] == '/' || (s.size() > 1U && s.compare(1,2,":/") == 0)) {
163
0
            if (!endsWith(s,'/'))
164
0
                s += '/';
165
0
            fs.includePaths.push_back(std::move(s));
166
0
            continue;
167
0
        }
168
169
0
        if (endsWith(s,'/')) // this is a temporary hack, simplifyPath can crash if path ends with '/'
170
0
            s.pop_back();
171
172
0
        if (s.find("$(") == std::string::npos) {
173
0
            s = Path::simplifyPath(basepath + s);
174
0
        } else {
175
0
            if (!simplifyPathWithVariables(s, variables))
176
0
                continue;
177
0
        }
178
0
        if (s.empty())
179
0
            continue;
180
0
        fs.includePaths.push_back(s.back() == '/' ? s : (s + '/'));
181
0
    }
182
0
}
183
184
ImportProject::Type ImportProject::import(const std::string &filename, Settings *settings)
185
0
{
186
0
    std::ifstream fin(filename);
187
0
    if (!fin.is_open())
188
0
        return ImportProject::Type::MISSING;
189
190
0
    mPath = Path::getPathFromFilename(Path::fromNativeSeparators(filename));
191
0
    if (!mPath.empty() && !endsWith(mPath,'/'))
192
0
        mPath += '/';
193
194
0
    const std::vector<std::string> fileFilters =
195
0
        settings ? settings->fileFilters : std::vector<std::string>();
196
197
0
    if (endsWith(filename, ".json")) {
198
0
        if (importCompileCommands(fin)) {
199
0
            setRelativePaths(filename);
200
0
            return ImportProject::Type::COMPILE_DB;
201
0
        }
202
0
    } else if (endsWith(filename, ".sln")) {
203
0
        if (importSln(fin, mPath, fileFilters)) {
204
0
            setRelativePaths(filename);
205
0
            return ImportProject::Type::VS_SLN;
206
0
        }
207
0
    } else if (endsWith(filename, ".vcxproj")) {
208
0
        std::map<std::string, std::string, cppcheck::stricmp> variables;
209
0
        std::vector<SharedItemsProject> sharedItemsProjects;
210
0
        if (importVcxproj(filename, variables, "", fileFilters, sharedItemsProjects)) {
211
0
            setRelativePaths(filename);
212
0
            return ImportProject::Type::VS_VCXPROJ;
213
0
        }
214
0
    } else if (endsWith(filename, ".bpr")) {
215
0
        if (importBcb6Prj(filename)) {
216
0
            setRelativePaths(filename);
217
0
            return ImportProject::Type::BORLAND;
218
0
        }
219
0
    } else if (settings && endsWith(filename, ".cppcheck")) {
220
0
        if (importCppcheckGuiProject(fin, settings)) {
221
0
            setRelativePaths(filename);
222
0
            return ImportProject::Type::CPPCHECK_GUI;
223
0
        }
224
0
    } else {
225
0
        return ImportProject::Type::UNKNOWN;
226
0
    }
227
0
    return ImportProject::Type::FAILURE;
228
0
}
229
230
static std::string readUntil(const std::string &command, std::string::size_type *pos, const char until[])
231
0
{
232
0
    std::string ret;
233
0
    bool escapedString = false;
234
0
    bool str = false;
235
0
    bool escape = false;
236
0
    for (; *pos < command.size() && (str || !std::strchr(until, command[*pos])); (*pos)++) {
237
0
        if (escape)
238
0
            escape = false;
239
0
        else if (command[*pos] == '\\') {
240
0
            if (str)
241
0
                escape = true;
242
0
            else if (command[*pos + 1] == '"') {
243
0
                if (escapedString)
244
0
                    return ret + "\\\"";
245
0
                escapedString = true;
246
0
                ret += "\\\"";
247
0
                (*pos)++;
248
0
                continue;
249
0
            }
250
0
        } else if (command[*pos] == '\"')
251
0
            str = !str;
252
0
        ret += command[*pos];
253
0
    }
254
0
    return ret;
255
0
}
256
257
static std::string unescape(const std::string &in)
258
0
{
259
0
    std::string out;
260
0
    bool escape = false;
261
0
    for (const char c: in) {
262
0
        if (escape) {
263
0
            escape = false;
264
0
            if (!std::strchr("\\\"\'",c))
265
0
                out += "\\";
266
0
            out += c;
267
0
        } else if (c == '\\')
268
0
            escape = true;
269
0
        else
270
0
            out += c;
271
0
    }
272
0
    return out;
273
0
}
274
275
void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command)
276
0
{
277
0
    std::string defs;
278
279
    // Parse command..
280
0
    std::string::size_type pos = 0;
281
0
    while (std::string::npos != (pos = command.find(' ',pos))) {
282
0
        while (pos < command.size() && command[pos] == ' ')
283
0
            pos++;
284
0
        if (pos >= command.size())
285
0
            break;
286
0
        if (command[pos] != '/' && command[pos] != '-')
287
0
            continue;
288
0
        pos++;
289
0
        if (pos >= command.size())
290
0
            break;
291
0
        const char F = command[pos++];
292
0
        if (std::strchr("DUI", F)) {
293
0
            while (pos < command.size() && command[pos] == ' ')
294
0
                ++pos;
295
0
        }
296
0
        const std::string fval = readUntil(command, &pos, " =");
297
0
        if (F=='D') {
298
0
            std::string defval = readUntil(command, &pos, " ");
299
0
            defs += fval;
300
0
            if (defval.size() >= 3 && startsWith(defval,"=\"") && defval.back()=='\"')
301
0
                defval = "=" + unescape(defval.substr(2, defval.size() - 3));
302
0
            else if (defval.size() >= 5 && startsWith(defval, "=\\\"") && endsWith(defval, "\\\""))
303
0
                defval = "=\"" + unescape(defval.substr(3, defval.size() - 5)) + "\"";
304
0
            if (!defval.empty())
305
0
                defs += defval;
306
0
            defs += ';';
307
0
        } else if (F=='U')
308
0
            fs.undefs.insert(fval);
309
0
        else if (F=='I') {
310
0
            std::string i = fval;
311
0
            if (i.size() > 1 && i[0] == '\"' && i.back() == '\"')
312
0
                i = unescape(i.substr(1, i.size() - 2));
313
0
            if (std::find(fs.includePaths.cbegin(), fs.includePaths.cend(), i) == fs.includePaths.cend())
314
0
                fs.includePaths.push_back(std::move(i));
315
0
        } else if (F=='s' && startsWith(fval,"td")) {
316
0
            ++pos;
317
0
            fs.standard = readUntil(command, &pos, " ");
318
0
        } else if (F == 'i' && fval == MatchCompiler::makeConstString("system")) {
319
0
            ++pos;
320
0
            std::string isystem = readUntil(command, &pos, " ");
321
0
            fs.systemIncludePaths.push_back(std::move(isystem));
322
0
        } else if (F=='m') {
323
0
            if (fval == MatchCompiler::makeConstString("unicode")) {
324
0
                defs += "UNICODE";
325
0
                defs += ";";
326
0
            }
327
0
        } else if (F=='f') {
328
0
            if (fval == MatchCompiler::makeConstString("pic")) {
329
0
                defs += "__pic__";
330
0
                defs += ";";
331
0
            } else if (fval == MatchCompiler::makeConstString("PIC")) {
332
0
                defs += "__PIC__";
333
0
                defs += ";";
334
0
            } else if (fval == MatchCompiler::makeConstString("pie")) {
335
0
                defs += "__pie__";
336
0
                defs += ";";
337
0
            } else if (fval == MatchCompiler::makeConstString("PIE")) {
338
0
                defs += "__PIE__";
339
0
                defs += ";";
340
0
            }
341
            // TODO: support -fsigned-char and -funsigned-char?
342
            // we can only set it globally but in this context it needs to be treated per file
343
0
        }
344
0
    }
345
0
    fsSetDefines(fs, std::move(defs));
346
0
}
347
348
bool ImportProject::importCompileCommands(std::istream &istr)
349
0
{
350
0
    picojson::value compileCommands;
351
0
    istr >> compileCommands;
352
0
    if (!compileCommands.is<picojson::array>()) {
353
0
        printError("compilation database is not a JSON array");
354
0
        return false;
355
0
    }
356
357
0
    for (const picojson::value &fileInfo : compileCommands.get<picojson::array>()) {
358
0
        picojson::object obj = fileInfo.get<picojson::object>();
359
0
        std::string dirpath = Path::fromNativeSeparators(obj["directory"].get<std::string>());
360
361
        /* CMAKE produces the directory without trailing / so add it if not
362
         * there - it is needed by setIncludePaths() */
363
0
        if (!endsWith(dirpath, '/'))
364
0
            dirpath += '/';
365
366
0
        const std::string directory = std::move(dirpath);
367
368
0
        std::string command;
369
0
        if (obj.count("arguments")) {
370
0
            if (obj["arguments"].is<picojson::array>()) {
371
0
                for (const picojson::value& arg : obj["arguments"].get<picojson::array>()) {
372
0
                    if (arg.is<std::string>()) {
373
0
                        std::string str = arg.get<std::string>();
374
0
                        if (str.find(' ') != std::string::npos)
375
0
                            str = "\"" + str + "\"";
376
0
                        command += str + " ";
377
0
                    }
378
0
                }
379
0
            } else {
380
0
                printError("'arguments' field in compilation database entry is not a JSON array");
381
0
                return false;
382
0
            }
383
0
        } else if (obj.count("command")) {
384
0
            if (obj["command"].is<std::string>()) {
385
0
                command = obj["command"].get<std::string>();
386
0
            } else {
387
0
                printError("'command' field in compilation database entry is not a string");
388
0
                return false;
389
0
            }
390
0
        } else {
391
0
            printError("no 'arguments' or 'command' field found in compilation database entry");
392
0
            return false;
393
0
        }
394
395
0
        if (!obj.count("file") || !obj["file"].is<std::string>()) {
396
0
            printError("skip compilation database entry because it does not have a proper 'file' field");
397
0
            continue;
398
0
        }
399
400
0
        const std::string file = Path::fromNativeSeparators(obj["file"].get<std::string>());
401
402
        // Accept file?
403
0
        if (!Path::acceptFile(file))
404
0
            continue;
405
406
0
        std::string path;
407
0
        if (Path::isAbsolute(file))
408
0
            path = Path::simplifyPath(file);
409
#ifdef _WIN32
410
        else if (file[0] == '/' && directory.size() > 2 && std::isalpha(directory[0]) && directory[1] == ':')
411
            // directory: C:\foo\bar
412
            // file: /xy/z.c
413
            // => c:/xy/z.c
414
            path = Path::simplifyPath(directory.substr(0,2) + file);
415
#endif
416
0
        else
417
0
            path = Path::simplifyPath(directory + file);
418
0
        if (!sourceFileExists(path)) {
419
0
            printError("'" + path + "' from compilation database does not exist");
420
0
            return false;
421
0
        }
422
0
        FileSettings fs{std::move(path)};
423
0
        fsParseCommand(fs, command); // read settings; -D, -I, -U, -std, -m*, -f*
424
0
        std::map<std::string, std::string, cppcheck::stricmp> variables;
425
0
        fsSetIncludePaths(fs, directory, fs.includePaths, variables);
426
0
        fileSettings.push_back(std::move(fs));
427
0
    }
428
429
0
    return true;
430
0
}
431
432
bool ImportProject::importSln(std::istream &istr, const std::string &path, const std::vector<std::string> &fileFilters)
433
0
{
434
0
    std::string line;
435
436
0
    if (!std::getline(istr,line)) {
437
0
        printError("Visual Studio solution file is empty");
438
0
        return false;
439
0
    }
440
441
0
    if (!startsWith(line, "Microsoft Visual Studio Solution File")) {
442
        // Skip BOM
443
0
        if (!std::getline(istr, line) || !startsWith(line, "Microsoft Visual Studio Solution File")) {
444
0
            printError("Visual Studio solution file header not found");
445
0
            return false;
446
0
        }
447
0
    }
448
449
0
    std::map<std::string,std::string,cppcheck::stricmp> variables;
450
0
    variables["SolutionDir"] = path;
451
452
0
    bool found = false;
453
0
    std::vector<SharedItemsProject> sharedItemsProjects;
454
0
    while (std::getline(istr,line)) {
455
0
        if (!startsWith(line,"Project("))
456
0
            continue;
457
0
        const std::string::size_type pos = line.find(".vcxproj");
458
0
        if (pos == std::string::npos)
459
0
            continue;
460
0
        const std::string::size_type pos1 = line.rfind('\"',pos);
461
0
        if (pos1 == std::string::npos)
462
0
            continue;
463
0
        std::string vcxproj(line.substr(pos1+1, pos-pos1+7));
464
0
        vcxproj = Path::toNativeSeparators(std::move(vcxproj));
465
0
        if (!Path::isAbsolute(vcxproj))
466
0
            vcxproj = path + vcxproj;
467
0
        vcxproj = Path::fromNativeSeparators(std::move(vcxproj));
468
0
        if (!importVcxproj(vcxproj, variables, "", fileFilters, sharedItemsProjects)) {
469
0
            printError("failed to load '" + vcxproj + "' from Visual Studio solution");
470
0
            return false;
471
0
        }
472
0
        found = true;
473
0
    }
474
475
0
    if (!found) {
476
0
        printError("no projects found in Visual Studio solution file");
477
0
        return false;
478
0
    }
479
480
0
    return true;
481
0
}
482
483
namespace {
484
    struct ProjectConfiguration {
485
0
        explicit ProjectConfiguration(const tinyxml2::XMLElement *cfg) {
486
0
            const char *a = cfg->Attribute("Include");
487
0
            if (a)
488
0
                name = a;
489
0
            for (const tinyxml2::XMLElement *e = cfg->FirstChildElement(); e; e = e->NextSiblingElement()) {
490
0
                const char * const text = e->GetText();
491
0
                if (!text)
492
0
                    continue;
493
0
                const char * ename = e->Name();
494
0
                if (std::strcmp(ename,"Configuration")==0)
495
0
                    configuration = text;
496
0
                else if (std::strcmp(ename,"Platform")==0) {
497
0
                    platformStr = text;
498
0
                    if (platformStr == MatchCompiler::makeConstString("Win32"))
499
0
                        platform = Win32;
500
0
                    else if (platformStr == MatchCompiler::makeConstString("x64"))
501
0
                        platform = x64;
502
0
                    else
503
0
                        platform = Unknown;
504
0
                }
505
0
            }
506
0
        }
507
        std::string name;
508
        std::string configuration;
509
        enum : std::uint8_t { Win32, x64, Unknown } platform = Unknown;
510
        std::string platformStr;
511
    };
512
513
    struct ItemDefinitionGroup {
514
0
        explicit ItemDefinitionGroup(const tinyxml2::XMLElement *idg, std::string includePaths) : additionalIncludePaths(std::move(includePaths)) {
515
0
            const char *condAttr = idg->Attribute("Condition");
516
0
            if (condAttr)
517
0
                condition = condAttr;
518
0
            for (const tinyxml2::XMLElement *e1 = idg->FirstChildElement(); e1; e1 = e1->NextSiblingElement()) {
519
0
                const char* name = e1->Name();
520
0
                if (std::strcmp(name, "ClCompile") == 0) {
521
0
                    enhancedInstructionSet = "StreamingSIMDExtensions2";
522
0
                    for (const tinyxml2::XMLElement *e = e1->FirstChildElement(); e; e = e->NextSiblingElement()) {
523
0
                        const char * const text = e->GetText();
524
0
                        if (!text)
525
0
                            continue;
526
0
                        const char * const ename = e->Name();
527
0
                        if (std::strcmp(ename, "PreprocessorDefinitions") == 0)
528
0
                            preprocessorDefinitions = text;
529
0
                        else if (std::strcmp(ename, "AdditionalIncludeDirectories") == 0) {
530
0
                            if (!additionalIncludePaths.empty())
531
0
                                additionalIncludePaths += ';';
532
0
                            additionalIncludePaths += text;
533
0
                        } else if (std::strcmp(ename, "LanguageStandard") == 0) {
534
0
                            if (std::strcmp(text, "stdcpp14") == 0)
535
0
                                cppstd = Standards::CPP14;
536
0
                            else if (std::strcmp(text, "stdcpp17") == 0)
537
0
                                cppstd = Standards::CPP17;
538
0
                            else if (std::strcmp(text, "stdcpp20") == 0)
539
0
                                cppstd = Standards::CPP20;
540
0
                            else if (std::strcmp(text, "stdcpplatest") == 0)
541
0
                                cppstd = Standards::CPPLatest;
542
0
                        } else if (std::strcmp(ename, "EnableEnhancedInstructionSet") == 0) {
543
0
                            enhancedInstructionSet = text;
544
0
                        }
545
0
                    }
546
0
                }
547
0
                else if (std::strcmp(name, "Link") == 0) {
548
0
                    for (const tinyxml2::XMLElement *e = e1->FirstChildElement(); e; e = e->NextSiblingElement()) {
549
0
                        const char * const text = e->GetText();
550
0
                        if (!text)
551
0
                            continue;
552
0
                        if (std::strcmp(e->Name(), "EntryPointSymbol") == 0) {
553
0
                            entryPointSymbol = text;
554
0
                        }
555
0
                    }
556
0
                }
557
0
            }
558
0
        }
559
560
0
        static void replaceAll(std::string &c, const std::string &from, const std::string &to) {
561
0
            std::string::size_type pos;
562
0
            while ((pos = c.find(from)) != std::string::npos) {
563
0
                c.erase(pos,from.size());
564
0
                c.insert(pos,to);
565
0
            }
566
0
        }
567
568
        // see https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditions
569
        // properties are .NET String objects and you can call any of its members on them
570
0
        bool conditionIsTrue(const ProjectConfiguration &p) const {
571
0
            if (condition.empty())
572
0
                return true;
573
0
            std::string c = '(' + condition + ");";
574
0
            replaceAll(c, "$(Configuration)", p.configuration);
575
0
            replaceAll(c, "$(Platform)", p.platformStr);
576
577
            // TODO: improve evaluation
578
0
            const Settings s;
579
0
            TokenList tokenlist(&s);
580
0
            std::istringstream istr(c);
581
0
            tokenlist.createTokens(istr, Standards::Language::C); // TODO: check result
582
            // TODO: put in a helper
583
            // generate links
584
0
            {
585
0
                std::stack<Token*> lpar;
586
0
                for (Token* tok2 = tokenlist.front(); tok2; tok2 = tok2->next()) {
587
0
                    if (tok2->str() == MatchCompiler::makeConstString("("))
588
0
                        lpar.push(tok2);
589
0
                    else if (tok2->str() == MatchCompiler::makeConstString(")")) {
590
0
                        if (lpar.empty())
591
0
                            break;
592
0
                        Token::createMutualLinks(lpar.top(), tok2);
593
0
                        lpar.pop();
594
0
                    }
595
0
                }
596
0
            }
597
0
            tokenlist.createAst();
598
0
            for (const Token *tok = tokenlist.front(); tok; tok = tok->next()) {
599
0
                if (tok->str() == MatchCompiler::makeConstString("(") && tok->astOperand1() && tok->astOperand2()) {
600
                    // TODO: this is wrong - it is Contains() not Equals()
601
0
                    if (tok->astOperand1()->expressionString() == MatchCompiler::makeConstString("Configuration.Contains"))
602
0
                        return ('\'' + p.configuration + '\'') == tok->astOperand2()->str();
603
0
                }
604
0
                if (tok->str() == MatchCompiler::makeConstString("==") && tok->astOperand1() && tok->astOperand2() && tok->astOperand1()->str() == tok->astOperand2()->str())
605
0
                    return true;
606
0
            }
607
0
            return false;
608
0
        }
609
        std::string condition;
610
        std::string enhancedInstructionSet;
611
        std::string preprocessorDefinitions;
612
        std::string additionalIncludePaths;
613
        std::string entryPointSymbol; // TODO: use this
614
        Standards::cppstd_t cppstd = Standards::CPPLatest;
615
    };
616
}
617
618
static std::list<std::string> toStringList(const std::string &s)
619
0
{
620
0
    std::list<std::string> ret;
621
0
    std::string::size_type pos1 = 0;
622
0
    std::string::size_type pos2;
623
0
    while ((pos2 = s.find(';',pos1)) != std::string::npos) {
624
0
        ret.push_back(s.substr(pos1, pos2-pos1));
625
0
        pos1 = pos2 + 1;
626
0
        if (pos1 >= s.size())
627
0
            break;
628
0
    }
629
0
    if (pos1 < s.size())
630
0
        ret.push_back(s.substr(pos1));
631
0
    return ret;
632
0
}
633
634
static void importPropertyGroup(const tinyxml2::XMLElement *node, std::map<std::string,std::string,cppcheck::stricmp> &variables, std::string &includePath, bool *useOfMfc)
635
0
{
636
0
    if (useOfMfc) {
637
0
        for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
638
0
            if (std::strcmp(e->Name(), "UseOfMfc") == 0) {
639
0
                *useOfMfc = true;
640
0
                break;
641
0
            }
642
0
        }
643
0
    }
644
645
0
    const char* labelAttribute = node->Attribute("Label");
646
0
    if (labelAttribute && std::strcmp(labelAttribute, "UserMacros") == 0) {
647
0
        for (const tinyxml2::XMLElement *propertyGroup = node->FirstChildElement(); propertyGroup; propertyGroup = propertyGroup->NextSiblingElement()) {
648
0
            const char* name = propertyGroup->Name();
649
0
            const char *text = empty_if_null(propertyGroup->GetText());
650
0
            variables[name] = text;
651
0
        }
652
653
0
    } else if (!labelAttribute) {
654
0
        for (const tinyxml2::XMLElement *propertyGroup = node->FirstChildElement(); propertyGroup; propertyGroup = propertyGroup->NextSiblingElement()) {
655
0
            if (std::strcmp(propertyGroup->Name(), "IncludePath") != 0)
656
0
                continue;
657
0
            const char *text = propertyGroup->GetText();
658
0
            if (!text)
659
0
                continue;
660
0
            std::string path(text);
661
0
            const std::string::size_type pos = path.find("$(IncludePath)");
662
0
            if (pos != std::string::npos)
663
0
                path.replace(pos, 14U, includePath);
664
0
            includePath = std::move(path);
665
0
        }
666
0
    }
667
0
}
668
669
static void loadVisualStudioProperties(const std::string &props, std::map<std::string,std::string,cppcheck::stricmp> &variables, std::string &includePath, const std::string &additionalIncludeDirectories, std::list<ItemDefinitionGroup> &itemDefinitionGroupList)
670
0
{
671
0
    std::string filename(props);
672
    // variables can't be resolved
673
0
    if (!simplifyPathWithVariables(filename, variables))
674
0
        return;
675
676
    // prepend project dir (if it exists) to transform relative paths into absolute ones
677
0
    if (!Path::isAbsolute(filename) && variables.count("ProjectDir") > 0)
678
0
        filename = Path::getAbsoluteFilePath(variables.at("ProjectDir") + filename);
679
680
0
    tinyxml2::XMLDocument doc;
681
0
    if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS)
682
0
        return;
683
0
    const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
684
0
    if (rootnode == nullptr)
685
0
        return;
686
0
    for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
687
0
        const char* name = node->Name();
688
0
        if (std::strcmp(name, "ImportGroup") == 0) {
689
0
            const char *labelAttribute = node->Attribute("Label");
690
0
            if (labelAttribute == nullptr || std::strcmp(labelAttribute, "PropertySheets") != 0)
691
0
                continue;
692
0
            for (const tinyxml2::XMLElement *importGroup = node->FirstChildElement(); importGroup; importGroup = importGroup->NextSiblingElement()) {
693
0
                if (std::strcmp(importGroup->Name(), "Import") == 0) {
694
0
                    const char *projectAttribute = importGroup->Attribute("Project");
695
0
                    if (projectAttribute == nullptr)
696
0
                        continue;
697
0
                    std::string loadprj(projectAttribute);
698
0
                    if (loadprj.find('$') == std::string::npos) {
699
0
                        loadprj = Path::getPathFromFilename(filename) + loadprj;
700
0
                    }
701
0
                    loadVisualStudioProperties(loadprj, variables, includePath, additionalIncludeDirectories, itemDefinitionGroupList);
702
0
                }
703
0
            }
704
0
        } else if (std::strcmp(name,"PropertyGroup")==0) {
705
0
            importPropertyGroup(node, variables, includePath, nullptr);
706
0
        } else if (std::strcmp(name,"ItemDefinitionGroup")==0) {
707
0
            itemDefinitionGroupList.emplace_back(node, additionalIncludeDirectories);
708
0
        }
709
0
    }
710
0
}
711
712
bool ImportProject::importVcxproj(const std::string &filename, std::map<std::string, std::string, cppcheck::stricmp> &variables, const std::string &additionalIncludeDirectories, const std::vector<std::string> &fileFilters, std::vector<SharedItemsProject> &cache)
713
0
{
714
0
    variables["ProjectDir"] = Path::simplifyPath(Path::getPathFromFilename(filename));
715
716
0
    std::list<ProjectConfiguration> projectConfigurationList;
717
0
    std::list<std::string> compileList;
718
0
    std::list<ItemDefinitionGroup> itemDefinitionGroupList;
719
0
    std::string includePath;
720
0
    std::vector<SharedItemsProject> sharedItemsProjects;
721
722
0
    bool useOfMfc = false;
723
724
0
    tinyxml2::XMLDocument doc;
725
0
    const tinyxml2::XMLError error = doc.LoadFile(filename.c_str());
726
0
    if (error != tinyxml2::XML_SUCCESS) {
727
0
        printError(std::string("Visual Studio project file is not a valid XML - ") + tinyxml2::XMLDocument::ErrorIDToName(error));
728
0
        return false;
729
0
    }
730
0
    const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
731
0
    if (rootnode == nullptr) {
732
0
        printError("Visual Studio project file has no XML root node");
733
0
        return false;
734
0
    }
735
0
    for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
736
0
        const char* name = node->Name();
737
0
        if (std::strcmp(name, "ItemGroup") == 0) {
738
0
            const char *labelAttribute = node->Attribute("Label");
739
0
            if (labelAttribute && std::strcmp(labelAttribute, "ProjectConfigurations") == 0) {
740
0
                for (const tinyxml2::XMLElement *cfg = node->FirstChildElement(); cfg; cfg = cfg->NextSiblingElement()) {
741
0
                    if (std::strcmp(cfg->Name(), "ProjectConfiguration") == 0) {
742
0
                        const ProjectConfiguration p(cfg);
743
0
                        if (p.platform != ProjectConfiguration::Unknown) {
744
0
                            projectConfigurationList.emplace_back(cfg);
745
0
                            mAllVSConfigs.insert(p.configuration);
746
0
                        }
747
0
                    }
748
0
                }
749
0
            } else {
750
0
                for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
751
0
                    if (std::strcmp(e->Name(), "ClCompile") == 0) {
752
0
                        const char *include = e->Attribute("Include");
753
0
                        if (include && Path::acceptFile(include)) {
754
0
                            std::string toInclude = Path::simplifyPath(Path::isAbsolute(include) ? include : Path::getPathFromFilename(filename) + include);
755
0
                            compileList.emplace_back(toInclude);
756
0
                        }
757
0
                    }
758
0
                }
759
0
            }
760
0
        } else if (std::strcmp(name, "ItemDefinitionGroup") == 0) {
761
0
            itemDefinitionGroupList.emplace_back(node, additionalIncludeDirectories);
762
0
        } else if (std::strcmp(name, "PropertyGroup") == 0) {
763
0
            importPropertyGroup(node, variables, includePath, &useOfMfc);
764
0
        } else if (std::strcmp(name, "ImportGroup") == 0) {
765
0
            const char *labelAttribute = node->Attribute("Label");
766
0
            if (labelAttribute && std::strcmp(labelAttribute, "PropertySheets") == 0) {
767
0
                for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
768
0
                    if (std::strcmp(e->Name(), "Import") == 0) {
769
0
                        const char *projectAttribute = e->Attribute("Project");
770
0
                        if (projectAttribute)
771
0
                            loadVisualStudioProperties(projectAttribute, variables, includePath, additionalIncludeDirectories, itemDefinitionGroupList);
772
0
                    }
773
0
                }
774
0
            } else if (labelAttribute && std::strcmp(labelAttribute, "Shared") == 0) {
775
0
                for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
776
0
                    if (std::strcmp(e->Name(), "Import") == 0) {
777
0
                        const char *projectAttribute = e->Attribute("Project");
778
0
                        if (projectAttribute) {
779
                            // Path to shared items project is relative to current project directory,
780
                            // unless the string starts with $(SolutionDir)
781
0
                            std::string pathToSharedItemsFile;
782
0
                            if (std::string(projectAttribute).rfind("$(SolutionDir)", 0) == 0) {
783
0
                                pathToSharedItemsFile = projectAttribute;
784
0
                            } else {
785
0
                                pathToSharedItemsFile = variables["ProjectDir"] + projectAttribute;
786
0
                            }
787
0
                            if (!simplifyPathWithVariables(pathToSharedItemsFile, variables)) {
788
0
                                printError("Could not simplify path to referenced shared items project");
789
0
                                return false;
790
0
                            }
791
792
0
                            SharedItemsProject toAdd = importVcxitems(pathToSharedItemsFile, fileFilters, cache);
793
0
                            if (!toAdd.successful) {
794
0
                                printError("Could not load shared items project \"" + pathToSharedItemsFile + "\" from original path \"" + std::string(projectAttribute) + "\".");
795
0
                                return false;
796
0
                            }
797
0
                            sharedItemsProjects.emplace_back(toAdd);
798
0
                        }
799
0
                    }
800
0
                }
801
0
            }
802
0
        }
803
0
    }
804
    // # TODO: support signedness of char via /J (and potential XML option for it)?
805
    // we can only set it globally but in this context it needs to be treated per file
806
807
    // Include shared items project files
808
0
    std::vector<std::string> sharedItemsIncludePaths;
809
0
    for (const auto& sharedProject : sharedItemsProjects) {
810
0
        for (const auto &file : sharedProject.sourceFiles) {
811
0
            std::string pathToFile = Path::simplifyPath(Path::getPathFromFilename(sharedProject.pathToProjectFile) + file);
812
0
            compileList.emplace_back(std::move(pathToFile));
813
0
        }
814
0
        for (const auto &p : sharedProject.includePaths) {
815
0
            std::string path = Path::simplifyPath(Path::getPathFromFilename(sharedProject.pathToProjectFile) + p);
816
0
            sharedItemsIncludePaths.emplace_back(std::move(path));
817
0
        }
818
0
    }
819
820
    // Project files
821
0
    for (const std::string &cfilename : compileList) {
822
0
        if (!fileFilters.empty() && !matchglobs(fileFilters, cfilename))
823
0
            continue;
824
825
0
        for (const ProjectConfiguration &p : projectConfigurationList) {
826
827
0
            if (!guiProject.checkVsConfigs.empty()) {
828
0
                const bool doChecking = std::any_of(guiProject.checkVsConfigs.cbegin(), guiProject.checkVsConfigs.cend(), [&](const std::string& c) {
829
0
                    return c == p.configuration;
830
0
                });
831
0
                if (!doChecking)
832
0
                    continue;
833
0
            }
834
835
0
            FileSettings fs{cfilename};
836
0
            fs.cfg = p.name;
837
            // TODO: detect actual MSC version
838
0
            fs.msc = true;
839
0
            fs.useMfc = useOfMfc;
840
0
            fs.defines = "_WIN32=1";
841
0
            if (p.platform == ProjectConfiguration::Win32)
842
0
                fs.platformType = Platform::Type::Win32W;
843
0
            else if (p.platform == ProjectConfiguration::x64) {
844
0
                fs.platformType = Platform::Type::Win64;
845
0
                fs.defines += ";_WIN64=1";
846
0
            }
847
0
            std::string additionalIncludePaths;
848
0
            for (const ItemDefinitionGroup &i : itemDefinitionGroupList) {
849
0
                if (!i.conditionIsTrue(p))
850
0
                    continue;
851
0
                fs.standard = Standards::getCPP(i.cppstd);
852
0
                fs.defines += ';' + i.preprocessorDefinitions;
853
0
                if (i.enhancedInstructionSet == MatchCompiler::makeConstString("StreamingSIMDExtensions"))
854
0
                    fs.defines += ";__SSE__";
855
0
                else if (i.enhancedInstructionSet == MatchCompiler::makeConstString("StreamingSIMDExtensions2"))
856
0
                    fs.defines += ";__SSE2__";
857
0
                else if (i.enhancedInstructionSet == MatchCompiler::makeConstString("AdvancedVectorExtensions"))
858
0
                    fs.defines += ";__AVX__";
859
0
                else if (i.enhancedInstructionSet == MatchCompiler::makeConstString("AdvancedVectorExtensions2"))
860
0
                    fs.defines += ";__AVX2__";
861
0
                else if (i.enhancedInstructionSet == MatchCompiler::makeConstString("AdvancedVectorExtensions512"))
862
0
                    fs.defines += ";__AVX512__";
863
0
                additionalIncludePaths += ';' + i.additionalIncludePaths;
864
0
            }
865
0
            fsSetDefines(fs, fs.defines);
866
0
            fsSetIncludePaths(fs, Path::getPathFromFilename(filename), toStringList(includePath + ';' + additionalIncludePaths), variables);
867
0
            for (const auto &path : sharedItemsIncludePaths) {
868
0
                fs.includePaths.emplace_back(path);
869
0
            }
870
0
            fileSettings.push_back(std::move(fs));
871
0
        }
872
0
    }
873
874
0
    return true;
875
0
}
876
877
ImportProject::SharedItemsProject ImportProject::importVcxitems(const std::string& filename, const std::vector<std::string>& fileFilters, std::vector<SharedItemsProject> &cache)
878
0
{
879
0
    auto isInCacheCheck = [filename](const ImportProject::SharedItemsProject& e) -> bool {
880
0
        return filename == e.pathToProjectFile;
881
0
    };
882
0
    const auto iterator = std::find_if(cache.begin(), cache.end(), isInCacheCheck);
883
0
    if (iterator != std::end(cache)) {
884
0
        return *iterator;
885
0
    }
886
887
0
    SharedItemsProject result;
888
0
    result.pathToProjectFile = filename;
889
890
0
    tinyxml2::XMLDocument doc;
891
0
    const tinyxml2::XMLError error = doc.LoadFile(filename.c_str());
892
0
    if (error != tinyxml2::XML_SUCCESS) {
893
0
        printError(std::string("Visual Studio project file is not a valid XML - ") + tinyxml2::XMLDocument::ErrorIDToName(error));
894
0
        return result;
895
0
    }
896
0
    const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
897
0
    if (rootnode == nullptr) {
898
0
        printError("Visual Studio project file has no XML root node");
899
0
        return result;
900
0
    }
901
0
    for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
902
0
        if (std::strcmp(node->Name(), "ItemGroup") == 0) {
903
0
            for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) {
904
0
                if (std::strcmp(e->Name(), "ClCompile") == 0) {
905
0
                    const char* include = e->Attribute("Include");
906
0
                    if (include && Path::acceptFile(include)) {
907
0
                        std::string file(include);
908
0
                        findAndReplace(file, "$(MSBuildThisFileDirectory)", "./");
909
910
                        // Don't include file if it matches the filter
911
0
                        if (!fileFilters.empty() && !matchglobs(fileFilters, file))
912
0
                            continue;
913
914
0
                        result.sourceFiles.emplace_back(file);
915
0
                    } else {
916
0
                        printError("Could not find shared items source file");
917
0
                        return result;
918
0
                    }
919
0
                }
920
0
            }
921
0
        } else if (std::strcmp(node->Name(), "ItemDefinitionGroup") == 0) {
922
0
            ItemDefinitionGroup temp(node, "");
923
0
            for (const auto& includePath : toStringList(temp.additionalIncludePaths)) {
924
0
                if (includePath == MatchCompiler::makeConstString("%(AdditionalIncludeDirectories)"))
925
0
                    continue;
926
927
0
                std::string toAdd(includePath);
928
0
                findAndReplace(toAdd, "$(MSBuildThisFileDirectory)", "./");
929
0
                result.includePaths.emplace_back(toAdd);
930
0
            }
931
0
        }
932
0
    }
933
934
0
    result.successful = true;
935
0
    cache.emplace_back(result);
936
0
    return result;
937
0
}
938
939
bool ImportProject::importBcb6Prj(const std::string &projectFilename)
940
0
{
941
0
    tinyxml2::XMLDocument doc;
942
0
    const tinyxml2::XMLError error = doc.LoadFile(projectFilename.c_str());
943
0
    if (error != tinyxml2::XML_SUCCESS) {
944
0
        printError(std::string("Borland project file is not a valid XML - ") + tinyxml2::XMLDocument::ErrorIDToName(error));
945
0
        return false;
946
0
    }
947
0
    const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
948
0
    if (rootnode == nullptr) {
949
0
        printError("Borland project file has no XML root node");
950
0
        return false;
951
0
    }
952
953
0
    const std::string& projectDir = Path::simplifyPath(Path::getPathFromFilename(projectFilename));
954
955
0
    std::list<std::string> compileList;
956
0
    std::string includePath;
957
0
    std::string userdefines;
958
0
    std::string sysdefines;
959
0
    std::string cflag1;
960
961
0
    for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
962
0
        const char* name = node->Name();
963
0
        if (std::strcmp(name, "FILELIST") == 0) {
964
0
            for (const tinyxml2::XMLElement *f = node->FirstChildElement(); f; f = f->NextSiblingElement()) {
965
0
                if (std::strcmp(f->Name(), "FILE") == 0) {
966
0
                    const char *filename = f->Attribute("FILENAME");
967
0
                    if (filename && Path::acceptFile(filename))
968
0
                        compileList.emplace_back(filename);
969
0
                }
970
0
            }
971
0
        } else if (std::strcmp(name, "MACROS") == 0) {
972
0
            for (const tinyxml2::XMLElement *m = node->FirstChildElement(); m; m = m->NextSiblingElement()) {
973
0
                const char* mname = m->Name();
974
0
                if (std::strcmp(mname, "INCLUDEPATH") == 0) {
975
0
                    const char *v = m->Attribute("value");
976
0
                    if (v)
977
0
                        includePath = v;
978
0
                } else if (std::strcmp(mname, "USERDEFINES") == 0) {
979
0
                    const char *v = m->Attribute("value");
980
0
                    if (v)
981
0
                        userdefines = v;
982
0
                } else if (std::strcmp(mname, "SYSDEFINES") == 0) {
983
0
                    const char *v = m->Attribute("value");
984
0
                    if (v)
985
0
                        sysdefines = v;
986
0
                }
987
0
            }
988
0
        } else if (std::strcmp(name, "OPTIONS") == 0) {
989
0
            for (const tinyxml2::XMLElement *m = node->FirstChildElement(); m; m = m->NextSiblingElement()) {
990
0
                if (std::strcmp(m->Name(), "CFLAG1") == 0) {
991
0
                    const char *v = m->Attribute("value");
992
0
                    if (v)
993
0
                        cflag1 = v;
994
0
                }
995
0
            }
996
0
        }
997
0
    }
998
999
0
    std::set<std::string> cflags;
1000
1001
    // parse cflag1 and fill the cflags set
1002
0
    {
1003
0
        std::string arg;
1004
1005
0
        for (const char i : cflag1) {
1006
0
            if (i == ' ' && !arg.empty()) {
1007
0
                cflags.insert(arg);
1008
0
                arg.clear();
1009
0
                continue;
1010
0
            }
1011
0
            arg += i;
1012
0
        }
1013
1014
0
        if (!arg.empty()) {
1015
0
            cflags.insert(arg);
1016
0
        }
1017
1018
        // cleanup: -t is "An alternate name for the -Wxxx switches; there is no difference"
1019
        // -> Remove every known -txxx argument and replace it with its -Wxxx counterpart.
1020
        //    This way, we know what we have to check for later on.
1021
0
        static const std::map<std::string, std::string> synonyms = {
1022
0
            { "-tC","-WC" },
1023
0
            { "-tCDR","-WCDR" },
1024
0
            { "-tCDV","-WCDV" },
1025
0
            { "-tW","-W" },
1026
0
            { "-tWC","-WC" },
1027
0
            { "-tWCDR","-WCDR" },
1028
0
            { "-tWCDV","-WCDV" },
1029
0
            { "-tWD","-WD" },
1030
0
            { "-tWDR","-WDR" },
1031
0
            { "-tWDV","-WDV" },
1032
0
            { "-tWM","-WM" },
1033
0
            { "-tWP","-WP" },
1034
0
            { "-tWR","-WR" },
1035
0
            { "-tWU","-WU" },
1036
0
            { "-tWV","-WV" }
1037
0
        };
1038
1039
0
        for (auto i = synonyms.cbegin(); i != synonyms.cend(); ++i) {
1040
0
            if (cflags.erase(i->first) > 0) {
1041
0
                cflags.insert(i->second);
1042
0
            }
1043
0
        }
1044
0
    }
1045
1046
0
    std::string predefines;
1047
0
    std::string cppPredefines;
1048
1049
    // Collecting predefines. See BCB6 help topic "Predefined macros"
1050
0
    {
1051
0
        cppPredefines +=
1052
            // Defined if you've selected C++ compilation; will increase in later releases.
1053
            // value 0x0560 (but 0x0564 for our BCB6 SP4)
1054
            // @see http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Predefined_Macros#C.2B.2B_Compiler_Versions_in_Predefined_Macros
1055
0
            ";__BCPLUSPLUS__=0x0560"
1056
1057
            // Defined if in C++ mode; otherwise, undefined.
1058
0
            ";__cplusplus=1"
1059
1060
            // Defined as 1 for C++ files(meaning that templates are supported); otherwise, it is undefined.
1061
0
            ";__TEMPLATES__=1"
1062
1063
            // Defined only for C++ programs to indicate that wchar_t is an intrinsically defined data type.
1064
0
            ";_WCHAR_T"
1065
1066
            // Defined only for C++ programs to indicate that wchar_t is an intrinsically defined data type.
1067
0
            ";_WCHAR_T_DEFINED"
1068
1069
            // Defined in any compiler that has an optimizer.
1070
0
            ";__BCOPT__=1"
1071
1072
            // Version number.
1073
            // BCB6 is 0x056X (SP4 is 0x0564)
1074
            // @see http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Predefined_Macros#C.2B.2B_Compiler_Versions_in_Predefined_Macros
1075
0
            ";__BORLANDC__=0x0560"
1076
0
            ";__TCPLUSPLUS__=0x0560"
1077
0
            ";__TURBOC__=0x0560";
1078
1079
        // Defined if Calling Convention is set to cdecl; otherwise undefined.
1080
0
        const bool useCdecl = (cflags.find("-p") == cflags.end()
1081
0
                               && cflags.find("-pm") == cflags.end()
1082
0
                               && cflags.find("-pr") == cflags.end()
1083
0
                               && cflags.find("-ps") == cflags.end());
1084
0
        if (useCdecl)
1085
0
            predefines += ";__CDECL=1";
1086
1087
        // Defined by default indicating that the default char is unsigned char. Use the -K compiler option to undefine this macro.
1088
0
        const bool treatCharAsUnsignedChar = (cflags.find("-K") != cflags.end());
1089
0
        if (treatCharAsUnsignedChar)
1090
0
            predefines += ";_CHAR_UNSIGNED=1";
1091
1092
        // Defined whenever one of the CodeGuard compiler options is used; otherwise it is undefined.
1093
0
        const bool codeguardUsed = (cflags.find("-vGd") != cflags.end()
1094
0
                                    || cflags.find("-vGt") != cflags.end()
1095
0
                                    || cflags.find("-vGc") != cflags.end());
1096
0
        if (codeguardUsed)
1097
0
            predefines += ";__CODEGUARD__";
1098
1099
        // When defined, the macro indicates that the program is a console application.
1100
0
        const bool isConsoleApp = (cflags.find("-WC") != cflags.end());
1101
0
        if (isConsoleApp)
1102
0
            predefines += ";__CONSOLE__=1";
1103
1104
        // Enable stack unwinding. This is true by default; use -xd- to disable.
1105
0
        const bool enableStackUnwinding = (cflags.find("-xd-") == cflags.end());
1106
0
        if (enableStackUnwinding)
1107
0
            predefines += ";_CPPUNWIND=1";
1108
1109
        // Defined whenever the -WD compiler option is used; otherwise it is undefined.
1110
0
        const bool isDLL = (cflags.find("-WD") != cflags.end());
1111
0
        if (isDLL)
1112
0
            predefines += ";__DLL__=1";
1113
1114
        // Defined when compiling in 32-bit flat memory model.
1115
        // TODO: not sure how to switch to another memory model or how to read configuration from project file
1116
0
        predefines += ";__FLAT__=1";
1117
1118
        // Always defined. The default value is 300. You can change the value to 400 or 500 by using the /4 or /5 compiler options.
1119
0
        if (cflags.find("-6") != cflags.end())
1120
0
            predefines += ";_M_IX86=600";
1121
0
        else if (cflags.find("-5") != cflags.end())
1122
0
            predefines += ";_M_IX86=500";
1123
0
        else if (cflags.find("-4") != cflags.end())
1124
0
            predefines += ";_M_IX86=400";
1125
0
        else
1126
0
            predefines += ";_M_IX86=300";
1127
1128
        // Defined only if the -WM option is used. It specifies that the multithread library is to be linked.
1129
0
        const bool linkMtLib = (cflags.find("-WM") != cflags.end());
1130
0
        if (linkMtLib)
1131
0
            predefines += ";__MT__=1";
1132
1133
        // Defined if Calling Convention is set to Pascal; otherwise undefined.
1134
0
        const bool usePascalCallingConvention = (cflags.find("-p") != cflags.end());
1135
0
        if (usePascalCallingConvention)
1136
0
            predefines += ";__PASCAL__=1";
1137
1138
        // Defined if you compile with the -A compiler option; otherwise, it is undefined.
1139
0
        const bool useAnsiKeywordExtensions = (cflags.find("-A") != cflags.end());
1140
0
        if (useAnsiKeywordExtensions)
1141
0
            predefines += ";__STDC__=1";
1142
1143
        // Thread Local Storage. Always true in C++Builder.
1144
0
        predefines += ";__TLC__=1";
1145
1146
        // Defined for Windows-only code.
1147
0
        const bool isWindowsTarget = (cflags.find("-WC") != cflags.end()
1148
0
                                      || cflags.find("-WCDR") != cflags.end()
1149
0
                                      || cflags.find("-WCDV") != cflags.end()
1150
0
                                      || cflags.find("-WD") != cflags.end()
1151
0
                                      || cflags.find("-WDR") != cflags.end()
1152
0
                                      || cflags.find("-WDV") != cflags.end()
1153
0
                                      || cflags.find("-WM") != cflags.end()
1154
0
                                      || cflags.find("-WP") != cflags.end()
1155
0
                                      || cflags.find("-WR") != cflags.end()
1156
0
                                      || cflags.find("-WU") != cflags.end()
1157
0
                                      || cflags.find("-WV") != cflags.end());
1158
0
        if (isWindowsTarget)
1159
0
            predefines += ";_Windows";
1160
1161
        // Defined for console and GUI applications.
1162
        // TODO: I'm not sure about the difference to define "_Windows".
1163
        //       From description, I would assume __WIN32__ is only defined for
1164
        //       executables, while _Windows would also be defined for DLLs, etc.
1165
        //       However, in a newly created DLL project, both __WIN32__ and
1166
        //       _Windows are defined. -> treating them the same for now.
1167
        //       Also boost uses __WIN32__ for OS identification.
1168
0
        const bool isConsoleOrGuiApp = isWindowsTarget;
1169
0
        if (isConsoleOrGuiApp)
1170
0
            predefines += ";__WIN32__=1";
1171
0
    }
1172
1173
    // Include paths may contain variables like "$(BCB)\include" or "$(BCB)\include\vcl".
1174
    // Those get resolved by ImportProject::FileSettings::setIncludePaths by
1175
    // 1. checking the provided variables map ("BCB" => "C:\\Program Files (x86)\\Borland\\CBuilder6")
1176
    // 2. checking env variables as a fallback
1177
    // Setting env is always possible. Configuring the variables via cli might be an addition.
1178
    // Reading the BCB6 install location from registry in windows environments would also be possible,
1179
    // but I didn't see any such functionality around the source. Not in favor of adding it only
1180
    // for the BCB6 project loading.
1181
0
    std::map<std::string, std::string, cppcheck::stricmp> variables;
1182
0
    const std::string defines = predefines + ";" + sysdefines + ";" + userdefines;
1183
0
    const std::string cppDefines  = cppPredefines + ";" + defines;
1184
0
    const bool forceCppMode = (cflags.find("-P") != cflags.end());
1185
1186
0
    for (const std::string &c : compileList) {
1187
        // C++ compilation is selected by file extension by default, so these
1188
        // defines have to be configured on a per-file base.
1189
        //
1190
        // > Files with the .CPP extension compile as C++ files. Files with a .C
1191
        // > extension, with no extension, or with extensions other than .CPP,
1192
        // > .OBJ, .LIB, or .ASM compile as C files.
1193
        // (http://docwiki.embarcadero.com/RADStudio/Tokyo/en/BCC32.EXE,_the_C%2B%2B_32-bit_Command-Line_Compiler)
1194
        //
1195
        // We can also force C++ compilation for all files using the -P command line switch.
1196
0
        const bool cppMode = forceCppMode || Path::getFilenameExtensionInLowerCase(c) == MatchCompiler::makeConstString(".cpp");
1197
0
        FileSettings fs{Path::simplifyPath(Path::isAbsolute(c) ? c : projectDir + c)};
1198
0
        fsSetIncludePaths(fs, projectDir, toStringList(includePath), variables);
1199
0
        fsSetDefines(fs, cppMode ? cppDefines : defines);
1200
0
        fileSettings.push_back(std::move(fs));
1201
0
    }
1202
1203
0
    return true;
1204
0
}
1205
1206
static std::string joinRelativePath(const std::string &path1, const std::string &path2)
1207
0
{
1208
0
    if (!path1.empty() && !Path::isAbsolute(path2))
1209
0
        return path1 + path2;
1210
0
    return path2;
1211
0
}
1212
1213
static std::list<std::string> readXmlStringList(const tinyxml2::XMLElement *node, const std::string &path, const char name[], const char attribute[])
1214
0
{
1215
0
    std::list<std::string> ret;
1216
0
    for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
1217
0
        if (strcmp(child->Name(), name) != 0)
1218
0
            continue;
1219
0
        const char *attr = attribute ? child->Attribute(attribute) : child->GetText();
1220
0
        if (attr)
1221
0
            ret.push_back(joinRelativePath(path, attr));
1222
0
    }
1223
0
    return ret;
1224
0
}
1225
1226
static std::string join(const std::list<std::string> &strlist, const char *sep)
1227
0
{
1228
0
    std::string ret;
1229
0
    for (const std::string &s : strlist) {
1230
0
        ret += (ret.empty() ? "" : sep) + s;
1231
0
    }
1232
0
    return ret;
1233
0
}
1234
1235
static std::string istream_to_string(std::istream &istr)
1236
0
{
1237
0
    std::istreambuf_iterator<char> eos;
1238
0
    return std::string(std::istreambuf_iterator<char>(istr), eos);
1239
0
}
1240
1241
bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *settings)
1242
0
{
1243
0
    tinyxml2::XMLDocument doc;
1244
0
    const std::string xmldata = istream_to_string(istr);
1245
0
    const tinyxml2::XMLError error = doc.Parse(xmldata.data(), xmldata.size());
1246
0
    if (error != tinyxml2::XML_SUCCESS) {
1247
0
        printError(std::string("Cppcheck GUI project file is not a valid XML - ") + tinyxml2::XMLDocument::ErrorIDToName(error));
1248
0
        return false;
1249
0
    }
1250
0
    const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
1251
0
    if (rootnode == nullptr || strcmp(rootnode->Name(), CppcheckXml::ProjectElementName) != 0) {
1252
0
        printError("Cppcheck GUI project file has no XML root node");
1253
0
        return false;
1254
0
    }
1255
1256
0
    const std::string &path = mPath;
1257
1258
0
    std::list<std::string> paths;
1259
0
    std::list<SuppressionList::Suppression> suppressions;
1260
0
    Settings temp;
1261
1262
    // default to --check-level=normal for import for now
1263
0
    temp.setCheckLevel(Settings::CheckLevel::normal);
1264
1265
0
    guiProject.analyzeAllVsConfigs.clear();
1266
1267
    // TODO: this should support all available command-line options
1268
0
    for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
1269
0
        const char* name = node->Name();
1270
0
        if (strcmp(name, CppcheckXml::RootPathName) == 0) {
1271
0
            const char* attr = node->Attribute(CppcheckXml::RootPathNameAttrib);
1272
0
            if (attr) {
1273
0
                temp.basePaths.push_back(Path::fromNativeSeparators(joinRelativePath(path, attr)));
1274
0
                temp.relativePaths = true;
1275
0
            }
1276
0
        } else if (strcmp(name, CppcheckXml::BuildDirElementName) == 0)
1277
0
            temp.buildDir = joinRelativePath(path, empty_if_null(node->GetText()));
1278
0
        else if (strcmp(name, CppcheckXml::IncludeDirElementName) == 0)
1279
0
            temp.includePaths = readXmlStringList(node, path, CppcheckXml::DirElementName, CppcheckXml::DirNameAttrib);
1280
0
        else if (strcmp(name, CppcheckXml::DefinesElementName) == 0)
1281
0
            temp.userDefines = join(readXmlStringList(node, "", CppcheckXml::DefineName, CppcheckXml::DefineNameAttrib), ";");
1282
0
        else if (strcmp(name, CppcheckXml::UndefinesElementName) == 0) {
1283
0
            for (const std::string &u : readXmlStringList(node, "", CppcheckXml::UndefineName, nullptr))
1284
0
                temp.userUndefs.insert(u);
1285
0
        } else if (strcmp(name, CppcheckXml::ImportProjectElementName) == 0) {
1286
0
            const std::string t_str = empty_if_null(node->GetText());
1287
0
            if (!t_str.empty())
1288
0
                guiProject.projectFile = path + t_str;
1289
0
        }
1290
0
        else if (strcmp(name, CppcheckXml::PathsElementName) == 0)
1291
0
            paths = readXmlStringList(node, path, CppcheckXml::PathName, CppcheckXml::PathNameAttrib);
1292
0
        else if (strcmp(name, CppcheckXml::ExcludeElementName) == 0)
1293
0
            guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::ExcludePathName, CppcheckXml::ExcludePathNameAttrib);
1294
0
        else if (strcmp(name, CppcheckXml::FunctionContracts) == 0)
1295
0
            ;
1296
0
        else if (strcmp(name, CppcheckXml::VariableContractsElementName) == 0)
1297
0
            ;
1298
0
        else if (strcmp(name, CppcheckXml::IgnoreElementName) == 0)
1299
0
            guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::IgnorePathName, CppcheckXml::IgnorePathNameAttrib);
1300
0
        else if (strcmp(name, CppcheckXml::LibrariesElementName) == 0)
1301
0
            guiProject.libraries = readXmlStringList(node, "", CppcheckXml::LibraryElementName, nullptr);
1302
0
        else if (strcmp(name, CppcheckXml::SuppressionsElementName) == 0) {
1303
0
            for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
1304
0
                if (strcmp(child->Name(), CppcheckXml::SuppressionElementName) != 0)
1305
0
                    continue;
1306
0
                SuppressionList::Suppression s;
1307
0
                s.errorId = empty_if_null(child->GetText());
1308
0
                s.fileName = empty_if_null(child->Attribute("fileName"));
1309
0
                if (!s.fileName.empty())
1310
0
                    s.fileName = joinRelativePath(path, s.fileName);
1311
0
                s.lineNumber = child->IntAttribute("lineNumber", SuppressionList::Suppression::NO_LINE);
1312
0
                s.symbolName = empty_if_null(child->Attribute("symbolName"));
1313
0
                s.hash = strToInt<std::size_t>(default_if_null(child->Attribute("hash"), "0"));
1314
0
                suppressions.push_back(std::move(s));
1315
0
            }
1316
0
        } else if (strcmp(name, CppcheckXml::VSConfigurationElementName) == 0)
1317
0
            guiProject.checkVsConfigs = readXmlStringList(node, "", CppcheckXml::VSConfigurationName, nullptr);
1318
0
        else if (strcmp(name, CppcheckXml::PlatformElementName) == 0)
1319
0
            guiProject.platform = empty_if_null(node->GetText());
1320
0
        else if (strcmp(name, CppcheckXml::AnalyzeAllVsConfigsElementName) == 0)
1321
0
            guiProject.analyzeAllVsConfigs = empty_if_null(node->GetText());
1322
0
        else if (strcmp(name, CppcheckXml::Parser) == 0)
1323
0
            temp.clang = true;
1324
0
        else if (strcmp(name, CppcheckXml::AddonsElementName) == 0) {
1325
0
            const auto& addons = readXmlStringList(node, "", CppcheckXml::AddonElementName, nullptr);
1326
0
            temp.addons.insert(addons.cbegin(), addons.cend());
1327
0
        }
1328
0
        else if (strcmp(name, CppcheckXml::TagsElementName) == 0)
1329
0
            node->Attribute(CppcheckXml::TagElementName); // FIXME: Write some warning
1330
0
        else if (strcmp(name, CppcheckXml::ToolsElementName) == 0) {
1331
0
            const std::list<std::string> toolList = readXmlStringList(node, "", CppcheckXml::ToolElementName, nullptr);
1332
0
            for (const std::string &toolName : toolList) {
1333
0
                if (toolName == CppcheckXml::ClangTidy)
1334
0
                    temp.clangTidy = true;
1335
0
            }
1336
0
        } else if (strcmp(name, CppcheckXml::CheckHeadersElementName) == 0)
1337
0
            temp.checkHeaders = (strcmp(default_if_null(node->GetText(), ""), "true") == 0);
1338
0
        else if (strcmp(name, CppcheckXml::CheckLevelReducedElementName) == 0)
1339
0
            temp.setCheckLevel(Settings::CheckLevel::reduced);
1340
0
        else if (strcmp(name, CppcheckXml::CheckLevelNormalElementName) == 0)
1341
0
            temp.setCheckLevel(Settings::CheckLevel::normal);
1342
0
        else if (strcmp(name, CppcheckXml::CheckLevelExhaustiveElementName) == 0)
1343
0
            temp.setCheckLevel(Settings::CheckLevel::exhaustive);
1344
0
        else if (strcmp(name, CppcheckXml::CheckUnusedTemplatesElementName) == 0)
1345
0
            temp.checkUnusedTemplates = (strcmp(default_if_null(node->GetText(), ""), "true") == 0);
1346
0
        else if (strcmp(name, CppcheckXml::InlineSuppression) == 0)
1347
0
            temp.inlineSuppressions = (strcmp(default_if_null(node->GetText(), ""), "true") == 0);
1348
0
        else if (strcmp(name, CppcheckXml::MaxCtuDepthElementName) == 0)
1349
0
            temp.maxCtuDepth = strToInt<int>(default_if_null(node->GetText(), "2")); // TODO: bail out when missing?
1350
0
        else if (strcmp(name, CppcheckXml::MaxTemplateRecursionElementName) == 0)
1351
0
            temp.maxTemplateRecursion = strToInt<int>(default_if_null(node->GetText(), "100")); // TODO: bail out when missing?
1352
0
        else if (strcmp(name, CppcheckXml::CheckUnknownFunctionReturn) == 0)
1353
0
            ; // TODO
1354
0
        else if (strcmp(name, Settings::SafeChecks::XmlRootName) == 0) {
1355
0
            for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
1356
0
                const char* childname = child->Name();
1357
0
                if (strcmp(childname, Settings::SafeChecks::XmlClasses) == 0)
1358
0
                    temp.safeChecks.classes = true;
1359
0
                else if (strcmp(childname, Settings::SafeChecks::XmlExternalFunctions) == 0)
1360
0
                    temp.safeChecks.externalFunctions = true;
1361
0
                else if (strcmp(childname, Settings::SafeChecks::XmlInternalFunctions) == 0)
1362
0
                    temp.safeChecks.internalFunctions = true;
1363
0
                else if (strcmp(childname, Settings::SafeChecks::XmlExternalVariables) == 0)
1364
0
                    temp.safeChecks.externalVariables = true;
1365
0
                else {
1366
0
                    printError("Unknown '" + std::string(Settings::SafeChecks::XmlRootName) + "' element '" + childname + "' in Cppcheck project file");
1367
0
                    return false;
1368
0
                }
1369
0
            }
1370
0
        } else if (strcmp(name, CppcheckXml::TagWarningsElementName) == 0)
1371
0
            ; // TODO
1372
        // Cppcheck Premium features
1373
0
        else if (strcmp(name, CppcheckXml::BughuntingElementName) == 0)
1374
0
            temp.premiumArgs += " --bughunting";
1375
0
        else if (strcmp(name, CppcheckXml::CertIntPrecisionElementName) == 0)
1376
0
            temp.premiumArgs += std::string(" --cert-c-int-precision=") + default_if_null(node->GetText(), "0");
1377
0
        else if (strcmp(name, CppcheckXml::CodingStandardsElementName) == 0) {
1378
0
            for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
1379
0
                if (strcmp(child->Name(), CppcheckXml::CodingStandardElementName) == 0) {
1380
0
                    const char* text = child->GetText();
1381
0
                    if (text)
1382
0
                        temp.premiumArgs += std::string(" --") + text;
1383
0
                }
1384
0
            }
1385
0
        }
1386
0
        else if (strcmp(name, CppcheckXml::ProjectNameElementName) == 0)
1387
0
            ; // no-op
1388
0
        else {
1389
0
            printError("Unknown element '" + std::string(name) + "' in Cppcheck project file");
1390
0
            return false;
1391
0
        }
1392
0
    }
1393
0
    settings->basePaths = temp.basePaths;
1394
0
    settings->relativePaths |= temp.relativePaths;
1395
0
    settings->buildDir = temp.buildDir;
1396
0
    settings->includePaths = temp.includePaths;
1397
0
    settings->userDefines = temp.userDefines;
1398
0
    settings->userUndefs = temp.userUndefs;
1399
0
    settings->addons = temp.addons;
1400
0
    settings->clang = temp.clang;
1401
0
    settings->clangTidy = temp.clangTidy;
1402
1403
0
    if (!settings->premiumArgs.empty())
1404
0
        settings->premiumArgs += temp.premiumArgs;
1405
0
    else if (!temp.premiumArgs.empty())
1406
0
        settings->premiumArgs = temp.premiumArgs.substr(1);
1407
1408
0
    for (const std::string &p : paths)
1409
0
        guiProject.pathNames.push_back(Path::fromNativeSeparators(p));
1410
0
    settings->supprs.nomsg.addSuppressions(std::move(suppressions)); // TODO: check result
1411
0
    settings->checkHeaders = temp.checkHeaders;
1412
0
    settings->checkUnusedTemplates = temp.checkUnusedTemplates;
1413
0
    settings->maxCtuDepth = temp.maxCtuDepth;
1414
0
    settings->maxTemplateRecursion = temp.maxTemplateRecursion;
1415
0
    settings->inlineSuppressions |= temp.inlineSuppressions;
1416
0
    settings->safeChecks = temp.safeChecks;
1417
0
    settings->setCheckLevel(temp.checkLevel);
1418
1419
0
    return true;
1420
0
}
1421
1422
void ImportProject::selectOneVsConfig(Platform::Type platform)
1423
0
{
1424
0
    std::set<std::string> filenames;
1425
0
    for (auto it = fileSettings.cbegin(); it != fileSettings.cend();) {
1426
0
        if (it->cfg.empty()) {
1427
0
            ++it;
1428
0
            continue;
1429
0
        }
1430
0
        const FileSettings &fs = *it;
1431
0
        bool remove = false;
1432
0
        if (!startsWith(fs.cfg,"Debug"))
1433
0
            remove = true;
1434
0
        if (platform == Platform::Type::Win64 && fs.platformType != platform)
1435
0
            remove = true;
1436
0
        else if ((platform == Platform::Type::Win32A || platform == Platform::Type::Win32W) && fs.platformType == Platform::Type::Win64)
1437
0
            remove = true;
1438
0
        else if (filenames.find(fs.filename()) != filenames.end())
1439
0
            remove = true;
1440
0
        if (remove) {
1441
0
            it = fileSettings.erase(it);
1442
0
        } else {
1443
0
            filenames.insert(fs.filename());
1444
0
            ++it;
1445
0
        }
1446
0
    }
1447
0
}
1448
1449
// cppcheck-suppress unusedFunction - used by GUI only
1450
void ImportProject::selectVsConfigurations(Platform::Type platform, const std::vector<std::string> &configurations)
1451
0
{
1452
0
    for (auto it = fileSettings.cbegin(); it != fileSettings.cend();) {
1453
0
        if (it->cfg.empty()) {
1454
0
            ++it;
1455
0
            continue;
1456
0
        }
1457
0
        const FileSettings &fs = *it;
1458
0
        const auto config = fs.cfg.substr(0, fs.cfg.find('|'));
1459
0
        bool remove = false;
1460
0
        if (std::find(configurations.begin(), configurations.end(), config) == configurations.end())
1461
0
            remove = true;
1462
0
        if (platform == Platform::Type::Win64 && fs.platformType != platform)
1463
0
            remove = true;
1464
0
        else if ((platform == Platform::Type::Win32A || platform == Platform::Type::Win32W) && fs.platformType == Platform::Type::Win64)
1465
0
            remove = true;
1466
0
        if (remove) {
1467
0
            it = fileSettings.erase(it);
1468
0
        } else {
1469
0
            ++it;
1470
0
        }
1471
0
    }
1472
0
}
1473
1474
// cppcheck-suppress unusedFunction - used by GUI only
1475
std::list<std::string> ImportProject::getVSConfigs()
1476
0
{
1477
0
    return std::list<std::string>(mAllVSConfigs.cbegin(), mAllVSConfigs.cend());
1478
0
}
1479
1480
void ImportProject::setRelativePaths(const std::string &filename)
1481
0
{
1482
0
    if (Path::isAbsolute(filename))
1483
0
        return;
1484
0
    const std::vector<std::string> basePaths{Path::fromNativeSeparators(Path::getCurrentPath())};
1485
0
    for (auto &fs: fileSettings) {
1486
0
        fs.file = FileWithDetails{Path::getRelativePath(fs.filename(), basePaths)};
1487
0
        for (auto &includePath: fs.includePaths)
1488
0
            includePath = Path::getRelativePath(includePath, basePaths);
1489
0
    }
1490
0
}
1491
1492
void ImportProject::printError(const std::string &message)
1493
0
{
1494
0
    std::cout << "cppcheck: error: " << message << std::endl;
1495
0
}
1496
1497
bool ImportProject::sourceFileExists(const std::string &file)
1498
0
{
1499
0
    return Path::isFile(file);
1500
0
}