-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCompileCommandDeduplicator.cs
More file actions
439 lines (375 loc) · 16.9 KB
/
Copy pathCompileCommandDeduplicator.cs
File metadata and controls
439 lines (375 loc) · 16.9 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
namespace MSBuild.CompileCommands.Extractor
{
/// <summary>
/// Merges duplicate compile command entries (same source file) into a single
/// best-compromise entry suitable for IntelliSense tools like clangd/cpptools.
/// </summary>
public static class CompileCommandDeduplicator
{
private static readonly StringComparer PathComparer = StringComparer.OrdinalIgnoreCase;
// Language standard ranking, higher index = newer standard
private static readonly string[] CppStandardRank = ["c++14", "c++17", "c++20", "c++23", "c++latest"];
private static readonly string[] CStandardRank = ["c11", "c17", "clatest"];
/// <summary>
/// Deduplicate compile commands so there is at most one entry per source file.
/// </summary>
public static List<CompileCommand> Deduplicate(List<CompileCommand> commands, string preferConfig = "Debug", string preferPlatform = "x64")
{
var groups = commands.GroupBy(c => c.File, PathComparer).ToList();
var result = new List<CompileCommand>(groups.Count);
foreach (var group in groups)
{
var entries = group.ToList();
if (entries.Count == 1)
{
// Single entry, still strip warnings/optimization flags
var cleaned = CleanSingleEntry(entries[0]);
result.Add(cleaned);
}
else
{
result.Add(MergeEntries(entries, preferConfig, preferPlatform));
}
}
return result;
}
private static CompileCommand CleanSingleEntry(CompileCommand entry)
{
var args = entry.Arguments.ToList();
args = StripWarningFlags(args);
args = StripOptimizationFlags(args);
return entry with { Arguments = args.ToArray() };
}
private static CompileCommand MergeEntries(List<CompileCommand> entries, string preferConfig, string preferPlatform)
{
// Sort entries so preferred config/platform comes first
var sorted = entries
.OrderByDescending(e => IsPreferred(e, preferConfig, preferPlatform))
.ToList();
var preferred = sorted[0];
// Parse arguments from all entries
var parsedAll = sorted.Select(ParseArguments).ToList();
// The cl.exe path and source file come from the preferred entry;
// the source file is identical across every entry in the group,
// since that's the grouping key.
string clExe = parsedAll[0].ClExe;
string sourceFile = parsedAll[0].SourceFile ?? preferred.File;
// Include paths are unioned with case-insensitive, order-preserving dedup.
var includes = UnionOrderPreserving(parsedAll.SelectMany(p => p.IncludePaths), PathComparer);
// Defines need a smart merge to handle conflicting values across entries.
var defines = MergeDefines(parsedAll.Select(p => p.Defines).ToList());
// For language standard, keep the highest seen across the group.
string? langStd = PickHighestStandard(parsedAll.Select(p => p.LanguageStandard).Where(s => s != null).Cast<string>());
// For the runtime library, prefer the debug variant when present.
string? runtimeLib = PickDebugRuntime(parsedAll.Select(p => p.RuntimeLibrary).Where(s => s != null).Cast<string>());
// Remaining flags are unioned permissively.
var otherFlags = UnionOrderPreserving(parsedAll.SelectMany(p => p.OtherFlags), StringComparer.OrdinalIgnoreCase);
// The working directory comes from the preferred entry.
string directory = preferred.Directory;
// Reconstruct arguments
var args = new List<string>();
args.Add(clExe);
foreach (var inc in includes)
args.Add($"/I{inc}");
foreach (var def in defines)
args.Add($"/D{def}");
if (langStd != null)
args.Add($"/std:{langStd}");
if (runtimeLib != null)
args.Add($"/{runtimeLib}");
args.AddRange(otherFlags);
args.Add(sourceFile);
return new CompileCommand(
File: preferred.File,
Arguments: args.ToArray(),
Directory: directory,
ProjectPath: preferred.ProjectPath,
ProjectName: preferred.ProjectName,
Configuration: preferred.Configuration,
Platform: preferred.Platform);
}
private static int IsPreferred(CompileCommand entry, string preferConfig, string preferPlatform)
{
int score = 0;
if (entry.Configuration != null &&
entry.Configuration.Equals(preferConfig, StringComparison.OrdinalIgnoreCase))
score += 2;
if (entry.Platform != null &&
entry.Platform.Equals(preferPlatform, StringComparison.OrdinalIgnoreCase))
score += 1;
return score;
}
#region Argument Parsing
private record ParsedArgs(
string ClExe,
string? SourceFile,
List<string> IncludePaths,
List<string> Defines,
string? LanguageStandard,
string? RuntimeLibrary,
List<string> OtherFlags);
private static ParsedArgs ParseArguments(CompileCommand cmd)
{
var args = cmd.Arguments;
string clExe = args.Length > 0 ? args[0] : "cl.exe";
string? sourceFile = null;
var includes = new List<string>();
var defines = new List<string>();
string? langStd = null;
string? runtimeLib = null;
var other = new List<string>();
for (int i = 1; i < args.Length; i++)
{
var arg = args[i];
if (arg.StartsWith("/external:I", StringComparison.OrdinalIgnoreCase) ||
arg.StartsWith("-external:I", StringComparison.OrdinalIgnoreCase))
{
var path = arg.Substring(11); // "/external:I".Length
if (string.IsNullOrEmpty(path) && i + 1 < args.Length)
path = args[++i];
if (!string.IsNullOrEmpty(path))
includes.Add(path);
}
else if (arg.StartsWith("/I", StringComparison.OrdinalIgnoreCase) ||
arg.StartsWith("-I", StringComparison.OrdinalIgnoreCase))
{
var path = arg.Substring(2);
if (string.IsNullOrEmpty(path) && i + 1 < args.Length)
path = args[++i];
if (!string.IsNullOrEmpty(path))
includes.Add(path);
}
else if (arg.StartsWith("/D", StringComparison.OrdinalIgnoreCase) ||
arg.StartsWith("-D", StringComparison.OrdinalIgnoreCase))
{
var def = arg.Substring(2);
if (string.IsNullOrEmpty(def) && i + 1 < args.Length)
def = args[++i];
if (!string.IsNullOrEmpty(def))
defines.Add(def);
}
else if (arg.StartsWith("/std:", StringComparison.OrdinalIgnoreCase) ||
arg.StartsWith("-std:", StringComparison.OrdinalIgnoreCase))
{
langStd = arg.Substring(5);
}
else if (IsRuntimeLibFlag(arg))
{
runtimeLib = arg.TrimStart('/').TrimStart('-');
}
else if (IsWarningFlag(arg) || IsOptimizationFlag(arg))
{
// Drop warning and optimization flags
}
else if (!arg.StartsWith("/") && !arg.StartsWith("-") &&
(arg.EndsWith(".cpp", StringComparison.OrdinalIgnoreCase) ||
arg.EndsWith(".c", StringComparison.OrdinalIgnoreCase) ||
arg.EndsWith(".cc", StringComparison.OrdinalIgnoreCase) ||
arg.EndsWith(".cxx", StringComparison.OrdinalIgnoreCase)))
{
sourceFile = arg;
}
else
{
other.Add(arg);
}
}
return new ParsedArgs(clExe, sourceFile, includes, defines, langStd, runtimeLib, other);
}
private static bool IsWarningFlag(string arg)
{
var a = arg.TrimStart('/').TrimStart('-');
// /W0-4, /Wall, /WX, /wd*, /we*, /wo*, /external:*
if (a.StartsWith("W", StringComparison.OrdinalIgnoreCase) && a.Length >= 2)
return true;
if (a.StartsWith("wd", StringComparison.OrdinalIgnoreCase) ||
a.StartsWith("we", StringComparison.OrdinalIgnoreCase) ||
a.StartsWith("wo", StringComparison.OrdinalIgnoreCase))
return true;
if (a.StartsWith("external:W", StringComparison.OrdinalIgnoreCase))
return true;
return false;
}
private static bool IsOptimizationFlag(string arg)
{
var a = arg.TrimStart('/').TrimStart('-');
// /O1, /O2, /Od, /Ox, /Ob*, /GL, /Gw, /Oi, /Ot, /Os, /Oy
if (a.Equals("GL", StringComparison.OrdinalIgnoreCase) ||
a.Equals("Gw", StringComparison.OrdinalIgnoreCase))
return true;
if (a.StartsWith("O", StringComparison.OrdinalIgnoreCase) && a.Length >= 2)
{
char second = a[1];
// /O1, /O2, /Od, /Ox, /Ob0-3, /Oi, /Ot, /Os, /Oy
if (char.IsLetterOrDigit(second))
return true;
}
return false;
}
private static bool IsRuntimeLibFlag(string arg)
{
var a = arg.TrimStart('/').TrimStart('-');
return a.Equals("MT", StringComparison.OrdinalIgnoreCase) ||
a.Equals("MTd", StringComparison.OrdinalIgnoreCase) ||
a.Equals("MD", StringComparison.OrdinalIgnoreCase) ||
a.Equals("MDd", StringComparison.OrdinalIgnoreCase);
}
#endregion
#region Merge Strategies
private static List<string> MergeDefines(List<List<string>> allDefines)
{
// Parse each define into name=value pairs
var allParsed = allDefines
.Select(defs => defs.Select(ParseDefine).ToList())
.ToList();
// Collect all define names
var allNames = new LinkedHashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var parsed in allParsed)
foreach (var (name, _) in parsed)
allNames.Add(name);
var result = new List<string>();
bool hasDebug = false;
foreach (var name in allNames)
{
// Skip _DEBUG and NDEBUG, we'll add _DEBUG at the end
if (name.Equals("_DEBUG", StringComparison.OrdinalIgnoreCase) ||
name.Equals("NDEBUG", StringComparison.OrdinalIgnoreCase))
{
hasDebug = true;
continue;
}
// Collect all values for this define across entries
var values = new List<string?>();
foreach (var parsed in allParsed)
{
var match = parsed.FirstOrDefault(d =>
d.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (match.Name != null)
values.Add(match.Value);
}
if (values.Count == 0) continue;
// All same? Keep that value. Conflict? Keep the define (permissive).
var distinct = values.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
if (distinct.Count == 1)
{
result.Add(FormatDefine(name, distinct[0]));
}
else
{
// Conflict: keep define (first non-null value, permissive)
var firstValue = values.FirstOrDefault(v => v != null) ?? values[0];
result.Add(FormatDefine(name, firstValue));
}
}
// Add _DEBUG or NDEBUG based on the preferred (first) entry's defines
if (hasDebug || allParsed.Any(p => p.Any(d =>
d.Name.Equals("_DEBUG", StringComparison.OrdinalIgnoreCase) ||
d.Name.Equals("NDEBUG", StringComparison.OrdinalIgnoreCase))))
{
var preferredDefines = allParsed[0];
bool preferredHasDebug = preferredDefines.Any(d =>
d.Name.Equals("_DEBUG", StringComparison.OrdinalIgnoreCase));
if (preferredHasDebug)
result.Add("_DEBUG");
else
result.Add("NDEBUG");
}
return result;
}
private static (string Name, string? Value) ParseDefine(string define)
{
var eq = define.IndexOf('=');
if (eq < 0) return (define, null);
return (define.Substring(0, eq), define.Substring(eq + 1));
}
private static string FormatDefine(string name, string? value)
{
return value != null ? $"{name}={value}" : name;
}
private static string? PickHighestStandard(IEnumerable<string> standards)
{
string? best = null;
int bestRank = -1;
foreach (var std in standards)
{
int rank = GetStandardRank(std);
if (rank > bestRank)
{
bestRank = rank;
best = std;
}
}
return best;
}
private static int GetStandardRank(string std)
{
for (int i = 0; i < CppStandardRank.Length; i++)
if (CppStandardRank[i].Equals(std, StringComparison.OrdinalIgnoreCase))
return i + 100; // C++ standards ranked higher than C
for (int i = 0; i < CStandardRank.Length; i++)
if (CStandardRank[i].Equals(std, StringComparison.OrdinalIgnoreCase))
return i;
return -1; // unknown standard
}
private static string? PickDebugRuntime(IEnumerable<string> runtimes)
{
var all = runtimes.ToList();
if (all.Count == 0) return null;
// Prefer debug variants
if (all.Any(r => r.Equals("MDd", StringComparison.OrdinalIgnoreCase)))
return "MDd";
if (all.Any(r => r.Equals("MTd", StringComparison.OrdinalIgnoreCase)))
return "MTd";
if (all.Any(r => r.Equals("MD", StringComparison.OrdinalIgnoreCase)))
return "MD";
if (all.Any(r => r.Equals("MT", StringComparison.OrdinalIgnoreCase)))
return "MT";
return all[0];
}
private static List<string> UnionOrderPreserving(
IEnumerable<string> items,
StringComparer comparer)
{
var seen = new HashSet<string>(comparer);
var result = new List<string>();
foreach (var item in items)
{
if (seen.Add(item))
result.Add(item);
}
return result;
}
private static List<string> StripWarningFlags(List<string> args)
{
return args.Where(a => !IsWarningFlag(a)).ToList();
}
private static List<string> StripOptimizationFlags(List<string> args)
{
return args.Where(a => !IsOptimizationFlag(a)).ToList();
}
#endregion
/// <summary>
/// A simple order-preserving set backed by a HashSet + List.
/// </summary>
private class LinkedHashSet<T> : IEnumerable<T>
{
private readonly HashSet<T> _set;
private readonly List<T> _list = new();
public LinkedHashSet(IEqualityComparer<T>? comparer = null)
{
_set = new HashSet<T>(comparer);
}
public bool Add(T item)
{
if (_set.Add(item))
{
_list.Add(item);
return true;
}
return false;
}
public IEnumerator<T> GetEnumerator() => _list.GetEnumerator();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}