Skip to content

Commit

Permalink
Get rid of duplicate guid constants, and fix test to be able to notic…
Browse files Browse the repository at this point in the history
…e them when they happen

Fixes #355
  • Loading branch information
Steve Otteson committed Mar 16, 2021
1 parent d5d45c8 commit c3b4b04
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
1 change: 1 addition & 0 deletions generation/scraper/ConstantsScraper.rsp
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ PROC_THREAD_ATTRIBUTE_CHILD_PROCESS_POLICY
PROC_THREAD_ATTRIBUTE_DESKTOP_APP_POLICY
CSBFT_LOG
CSBFT_PATCH_FILE
WinUSB_TestGuid
66 changes: 40 additions & 26 deletions sources/PartitionUtilsLib/ConstantsScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private class ConstantsScraperImpl : IDisposable
private Dictionary<string, string> withTypes;

private Dictionary<string, List<EnumObject>> enumMemberNameToEnumObj;
private Dictionary<string, string> exclusionNamesToPartitions;

public ConstantsScraperImpl()
{
Expand All @@ -62,12 +63,13 @@ public void ScrapeConstants(
{
this.requiredNamespaces = new WildcardDictionary(requiredNamespaces);
this.withTypes = withTypes;
this.exclusionNamesToPartitions = exclusionNamesToPartitions;

this.repoRoot = Path.GetFullPath(repoRoot);

this.LoadEnumObjectsFromJsonFiles(enumJsonFiles, renames);

this.ScrapeConstantsFromTraversedFiles(exclusionNamesToPartitions);
this.ScrapeConstantsFromTraversedFiles();

this.WriteEnumsAndRemaps(remaps);
}
Expand Down Expand Up @@ -233,7 +235,7 @@ private void AddConstantValue(string originalNamespace, string type, string name
this.writtenConstants[name] = valueText;
}

private void AddConstantGuid(string defineGuidKeyword, string originalNamespace, string line)
private void AddConstantGuid(string defineGuidKeyword, string originalNamespace, string line, string partitionName)
{
int firstComma = line.IndexOf(',');
string name = line.Substring(0, firstComma).Trim();
Expand All @@ -242,6 +244,11 @@ private void AddConstantGuid(string defineGuidKeyword, string originalNamespace,
return;
}

if (this.ShouldExclude(name, partitionName))
{
return;
}

string args = line.Substring(firstComma + 1).Trim();
int closeParen = args.IndexOf(')');
args = args.Substring(0, closeParen);
Expand All @@ -256,6 +263,8 @@ private void AddConstantGuid(string defineGuidKeyword, string originalNamespace,
{
writer.AddGuid(name, args);
}

this.writtenConstants[name] = args;
}

private void AddConstantInteger(string originalNamespace, string nativeTypeName, string name, string valueText)
Expand Down Expand Up @@ -335,8 +344,31 @@ private void LoadEnumObjectsFromJsonFiles(string[] enumJsonFiles, Dictionary<str
this.enumMemberNameToEnumObj = LoadMemberNameToEnumObjMap(enumObjectsFromJsons);
}

private void ScrapeConstantsFromTraversedFiles(
Dictionary<string, string> exclusionNamesToPartitions)
private bool ShouldExclude(string constName, string currentPartitionName)
{
// If it's in the exclusion list see if we should ignore it based on the
// current partition
if (exclusionNamesToPartitions.TryGetValue(constName, out var partitions))
{
if (partitions == null)
{
return true;
}

string[] parts = partitions.Split(';');
foreach (var part in parts)
{
if (StringComparer.OrdinalIgnoreCase.Equals(part, currentPartitionName))
{
return true;
}
}
}

return false;
}

private void ScrapeConstantsFromTraversedFiles()
{
var autoReplacements = GetAutoValueReplacements();

Expand Down Expand Up @@ -397,7 +429,7 @@ private void ScrapeConstantsFromTraversedFiles(
if (continuation.EndsWith(';'))
{
processingGuidMultiLine = false;
this.AddConstantGuid(defineGuidKeyword, currentNamespace, continuation);
this.AddConstantGuid(defineGuidKeyword, currentNamespace, continuation, partInfo.Name);
continuation = null;
}

Expand All @@ -414,7 +446,7 @@ private void ScrapeConstantsFromTraversedFiles(
line = StripComments(line).Trim();
if (line.EndsWith(';'))
{
this.AddConstantGuid(defineGuidKeyword, currentNamespace, line);
this.AddConstantGuid(defineGuidKeyword, currentNamespace, line, partInfo.Name);
}
else
{
Expand All @@ -435,27 +467,9 @@ private void ScrapeConstantsFromTraversedFiles(

string name = defineMatch.Groups[1].Value;

// If it's in the exclusion list see if we should ignore it based on the
// current partition
if (exclusionNamesToPartitions.TryGetValue(name, out var partitions))
if (this.ShouldExclude(name, partInfo.Name))
{
bool shouldIgnore = partitions == null;
if (!shouldIgnore)
{
string[] parts = partitions.Split(';');
foreach (var part in parts)
{
if (StringComparer.OrdinalIgnoreCase.Equals(part, partInfo.Name))
{
shouldIgnore = true;
}
}
}

if (shouldIgnore)
{
continue;
}
continue;
}

// Get rid of trailing comments
Expand Down
15 changes: 9 additions & 6 deletions sources/WinmdUtils/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,18 @@ public static int ShowDuplicateConstants(FileInfo winmd, IConsole console)
{
using WinmdUtils w1 = WinmdUtils.LoadFromFile(winmd.FullName);
Dictionary<string, List<string>> nameToNamespace = new Dictionary<string, List<string>>();
foreach (var constant in w1.GetConstants())
foreach (ClassInfo apiClass in w1.GetTypes().Where(t => t is ClassInfo && t.Name == "Apis"))
{
if (!nameToNamespace.TryGetValue(constant.Name, out var namespaces))
foreach (var field in apiClass.Fields)
{
namespaces = new List<string>();
nameToNamespace[constant.Name] = namespaces;
}
if (!nameToNamespace.TryGetValue(field.Name, out var namespaces))
{
namespaces = new List<string>();
nameToNamespace[field.Name] = namespaces;
}

namespaces.Add(constant.Namespace);
namespaces.Add(apiClass.Namespace);
}
}

bool dupsFound = false;
Expand Down

0 comments on commit c3b4b04

Please sign in to comment.