1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "compilationdatabaseutils.h"
#include <projectexplorer/headerpath.h>
#include <projectexplorer/projectmacro.h>
#include <utils/algorithm.h>
#include <utils/hostosinfo.h>
#include <utils/stringutils.h>
#include <QDir>
#include <QRegularExpression>
#include <QSet>
#include <optional>
using namespace ProjectExplorer;
using namespace Utils;
namespace CompilationDatabaseProjectManager {
namespace Internal {
static CppEditor::ProjectFile::Kind fileKindFromString(QString flag)
{
using namespace CppEditor;
if (flag.startsWith("-x"))
flag = flag.mid(2);
if (flag == "c++-header")
return ProjectFile::CXXHeader;
if (flag == "c-header")
return ProjectFile::CHeader;
if (flag == "c++" || flag == "/TP" || flag.startsWith("/Tp"))
return ProjectFile::CXXSource;
if (flag == "c" || flag == "/TC" || flag.startsWith("/Tc"))
return ProjectFile::CSource;
if (flag == "objective-c++")
return ProjectFile::ObjCXXSource;
if (flag == "objective-c++-header")
return ProjectFile::ObjCXXHeader;
if (flag == "objective-c")
return ProjectFile::ObjCSource;
if (flag == "objective-c-header")
return ProjectFile::ObjCHeader;
if (flag == "cl")
return ProjectFile::OpenCLSource;
if (flag == "cuda")
return ProjectFile::CudaSource;
return ProjectFile::Unclassified;
}
QStringList filterFromFileName(const QStringList &flags, QString fileName)
{
QStringList result;
result.reserve(flags.size());
bool skipNext = false;
for (int i = 0; i < flags.size(); ++i) {
const QString &flag = flags.at(i);
if (skipNext) {
skipNext = false;
continue;
}
if (FilePath::fromUserInput(flag).fileName() == fileName)
continue;
if (flag == "-o" || flag.startsWith("/Fo")) {
skipNext = true;
continue;
}
result.push_back(flag);
}
return result;
}
void filteredFlags(const FilePath &filePath,
const FilePath &workingDir,
QStringList &flags,
HeaderPaths &headerPaths,
Macros ¯os,
CppEditor::ProjectFile::Kind &fileKind,
FilePath &sysRoot)
{
if (flags.empty())
return;
// Skip compiler call if present.
bool skipNext = Utils::HostOsInfo::isWindowsHost()
? (!flags.front().startsWith('/') && !flags.front().startsWith('-'))
: (!flags.front().startsWith('-'));
std::optional<HeaderPathType> includePathType;
std::optional<MacroType> macroType;
bool fileKindIsNext = false;
QStringList filtered;
for (const QString &flag : flags) {
if (skipNext) {
skipNext = false;
continue;
}
if (includePathType) {
const QString pathStr = workingDir.resolvePath(flag).toString();
headerPaths.append({pathStr, includePathType.value()});
includePathType.reset();
continue;
}
if (macroType) {
Macro macro = Macro::fromKeyValue(flag);
macro.type = macroType.value();
macros.append(macro);
macroType.reset();
continue;
}
if (flag != "-x"
&& (fileKindIsNext || flag == "/TC" || flag == "/TP"
|| flag.startsWith("/Tc") || flag.startsWith("/Tp") || flag.startsWith("-x"))) {
fileKindIsNext = false;
fileKind = fileKindFromString(flag);
continue;
}
if (flag == "-x") {
fileKindIsNext = true;
continue;
}
if (flag == "-o" || flag == "-MF" || flag == "-c" || flag == "-pedantic"
|| flag.startsWith("-O") || flag.startsWith("-W") || flag.startsWith("-w")) {
continue;
}
const QStringList userIncludeFlags{"-I", "/I"};
const QStringList systemIncludeFlags{"-isystem", "-imsvc", "/imsvc"};
const QStringList allIncludeFlags = QStringList(userIncludeFlags) << systemIncludeFlags;
const QString includeOpt = Utils::findOrDefault(allIncludeFlags, [flag](const QString &opt) {
return flag.startsWith(opt) && flag != opt;
});
if (!includeOpt.isEmpty()) {
const QString pathStr = workingDir.resolvePath(flag.mid(includeOpt.length())).toString();
headerPaths.append({pathStr, userIncludeFlags.contains(includeOpt)
? HeaderPathType::User : HeaderPathType::System});
continue;
}
if ((flag.startsWith("-D") || flag.startsWith("-U") || flag.startsWith("/D") || flag.startsWith("/U"))
&& flag != "-D" && flag != "-U" && flag != "/D" && flag != "/U") {
Macro macro = Macro::fromKeyValue(flag.mid(2));
macro.type = (flag.startsWith("-D") || flag.startsWith("/D")) ? MacroType::Define : MacroType::Undefine;
macros.append(macro);
continue;
}
if (userIncludeFlags.contains(flag)) {
includePathType = HeaderPathType::User;
continue;
}
if (systemIncludeFlags.contains(flag)) {
includePathType = HeaderPathType::System;
continue;
}
if (flag == "-D" || flag == "-U" || flag == "/D" || flag == "/U") {
macroType = (flag == "-D" || flag == "/D") ? MacroType::Define : MacroType::Undefine;
continue;
}
if (flag.startsWith("--sysroot=")) {
if (sysRoot.isEmpty())
sysRoot = workingDir.resolvePath(flag.mid(10));
continue;
}
if ((flag.startsWith("-std=") || flag.startsWith("/std:"))
&& fileKind == CppEditor::ProjectFile::Unclassified) {
const bool cpp = (flag.contains("c++") || flag.contains("gnu++"));
if (CppEditor::ProjectFile::isHeader(CppEditor::ProjectFile::classify(filePath.path())))
fileKind = cpp ? CppEditor::ProjectFile::CXXHeader : CppEditor::ProjectFile::CHeader;
else
fileKind = cpp ? CppEditor::ProjectFile::CXXSource : CppEditor::ProjectFile::CSource;
}
// Skip all remaining Windows flags except feature flags.
if (Utils::HostOsInfo::isWindowsHost() && flag.startsWith("/") && !flag.startsWith("/Z"))
continue;
filtered.push_back(flag);
}
if (fileKind == CppEditor::ProjectFile::Unclassified)
fileKind = CppEditor::ProjectFile::classify(filePath.path());
flags = filtered;
}
QStringList splitCommandLine(QString commandLine, QSet<QString> &flagsCache)
{
QStringList result;
bool insideQuotes = false;
// Remove escaped quotes.
commandLine.replace("\\\"", "'");
for (const QString &part : commandLine.split(QRegularExpression("\""))) {
if (insideQuotes) {
const QString quotedPart = "\"" + part + "\"";
if (result.last().endsWith("=")) {
auto flagIt = flagsCache.insert(result.last() + quotedPart);
result.last() = *flagIt;
} else {
auto flagIt = flagsCache.insert(quotedPart);
result.append(*flagIt);
}
} else { // If 's' is outside quotes ...
for (const QString &flag :
part.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts)) {
auto flagIt = flagsCache.insert(flag);
result.append(*flagIt);
}
}
insideQuotes = !insideQuotes;
}
return result;
}
} // namespace Internal
} // namespace CompilationDatabaseProjectManager
|