Skip to content

Commit 83b8dd8

Browse files
committed
skip warnings for generated code
1 parent de3a856 commit 83b8dd8

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

src/InheritDoc/CecilExtensions.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
internal static class CecilExtensions
1010
{
11-
private const string compilerGeneratedAttribute = "System.Runtime.CompilerServices.CompilerGeneratedAttribute";
11+
private static readonly string[] generatedCodeAttributes = [
12+
"System.CodeDom.Compiler.GeneratedCodeAttribute",
13+
"System.Diagnostics.DebuggerNonUserCodeAttribute",
14+
"System.Runtime.CompilerServices.CompilerGeneratedAttribute"
15+
];
1216

1317
// These are ignored as the base for types, defaulting the doc inheritance target to an implemented interface instead.
1418
private static readonly string[] ignoredBaseTypes = [
@@ -66,9 +70,25 @@ public static IEnumerable<string> GetDocID(this MethodDefinition m)
6670
return docID;
6771
}
6872

69-
public static bool IsCompilerGenerated(this TypeDefinition t) => (t.HasCustomAttributes && t.CustomAttributes.Any(a => a.AttributeType.FullName == compilerGeneratedAttribute)) || (t.IsNested && t.DeclaringType.IsCompilerGenerated());
73+
public static bool IsGeneratedCode(this TypeDefinition t) => (t.HasCustomAttributes && t.CustomAttributes.Any(a => generatedCodeAttributes.Contains(a.AttributeType.FullName))) || (t.IsNested && t.DeclaringType.IsGeneratedCode());
74+
75+
public static bool IsGeneratedCode(this MethodDefinition m)
76+
{
77+
if (m.HasCustomAttributes && m.CustomAttributes.Any(a => generatedCodeAttributes.Contains(a.AttributeType.FullName)))
78+
return true;
79+
80+
if (m.IsPropertyMethod())
81+
return getPropertyForMethod(m).IsGeneratedCode();
82+
83+
if (m.IsEventMethod())
84+
return getEventForMethod(m).IsGeneratedCode();
85+
86+
return m.DeclaringType.IsGeneratedCode();
87+
}
88+
89+
public static bool IsGeneratedCode(this EventDefinition e) => e.HasCustomAttributes && e.CustomAttributes.Any(a => generatedCodeAttributes.Contains(a.AttributeType.FullName)) || e.DeclaringType.IsGeneratedCode();
7090

71-
public static bool IsCompilerGenerated(this MethodDefinition m) => m.HasCustomAttributes && m.CustomAttributes.Any(a => a.AttributeType.FullName == compilerGeneratedAttribute);
91+
public static bool IsGeneratedCode(this PropertyDefinition p) => p.HasCustomAttributes && p.CustomAttributes.Any(a => generatedCodeAttributes.Contains(a.AttributeType.FullName)) || p.DeclaringType.IsGeneratedCode();
7292

7393
public static bool IsNamespaceDocPlaceholder(this TypeDefinition t) => t.Name == "NamespaceDoc";
7494

src/InheritDoc/InheritDocProcessor.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private static class DocAttributeNames
3535
{
3636
public static readonly XName _visited = XName.Get(nameof(_visited));
3737
public static readonly XName _trimmed = XName.Get(nameof(_trimmed));
38+
public static readonly XName _gencode = XName.Get(nameof(_gencode));
3839
public static readonly XName Cref = XName.Get("cref");
3940
public static readonly XName Name = XName.Get("name");
4041
public static readonly XName Path = XName.Get("path");
@@ -89,7 +90,6 @@ static XDocument loadDoc(string path)
8990
asm.Modules
9091
.SelectMany(m => m.Types)
9192
.SelectManyRecursive(t => t.NestedTypes)
92-
.Where(t => !t.IsCompilerGenerated() || t.IsNamespaceDocPlaceholder())
9393
.ToList()
9494
;
9595

@@ -107,8 +107,14 @@ static XDocument loadDoc(string path)
107107
while ((mem = docMembers.Elements(DocElementNames.Member).FirstOrDefault(isInheritDocCandidate)) is not null)
108108
replaceInheritDoc(docPath, mem, docMap, docMembers, refDocs, logger);
109109

110-
foreach (var md in docMembers.Elements(DocElementNames.Member).Where(m => m.HasAttribute(DocAttributeNames._visited)))
111-
md.SetAttributeValue(DocAttributeNames._visited, null);
110+
foreach (var md in docMembers.Elements(DocElementNames.Member))
111+
{
112+
if (md.HasAttribute(DocAttributeNames._visited))
113+
md.SetAttributeValue(DocAttributeNames._visited, null);
114+
115+
if (md.HasAttribute(DocAttributeNames._gencode))
116+
md.SetAttributeValue(DocAttributeNames._gencode, null);
117+
}
112118

113119
if (trimLevel > ApiLevel.None)
114120
{
@@ -143,14 +149,20 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
143149
string typeID = t.GetDocID();
144150
var memDocs = findDocsByID(docMembers, typeID);
145151

146-
// Several tools include this hack to output namespace documentation
147-
// https://stackoverflow.com/questions/793210/xml-documentation-for-a-namespace
148-
if (t.IsCompilerGenerated() && t.IsNamespaceDocPlaceholder())
152+
if (t.IsGeneratedCode())
149153
{
150-
foreach (var md in memDocs)
151-
md.SetAttributeValue(DocAttributeNames.Name, "N:" + t.Namespace);
154+
// Several tools include this hack to output namespace documentation
155+
// https://stackoverflow.com/questions/793210/xml-documentation-for-a-namespace
156+
if (t.IsNamespaceDocPlaceholder())
157+
{
158+
foreach (var md in memDocs)
159+
md.SetAttributeValue(DocAttributeNames.Name, "N:" + t.Namespace);
152160

153-
continue;
161+
continue;
162+
}
163+
164+
foreach (var md in memDocs)
165+
md.SetAttributeValue(DocAttributeNames._gencode, true);
154166
}
155167

156168
if (t.GetApiLevel() <= trimLevel)
@@ -183,7 +195,7 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
183195
dml.Add(new DocMatch(cref, t));
184196
}
185197

186-
foreach (var (m, idx, memID) in t.Methods.Where(m => !m.IsCompilerGenerated() || m.IsEventMethod() || m.IsPropertyMethod()).SelectMany(m => m.GetDocID().Select((d, i) => (m, i, d))))
198+
foreach (var (m, idx, memID) in t.Methods.SelectMany(m => m.GetDocID().Select((d, i) => (m, i, d))))
187199
{
188200
if (docMap.ContainsKey(memID))
189201
continue;
@@ -209,6 +221,12 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
209221
md.SetAttributeValue(DocAttributeNames._trimmed, true);
210222
}
211223

224+
if (m.IsGeneratedCode())
225+
{
226+
foreach (var md in methDocs)
227+
md.SetAttributeValue(DocAttributeNames._gencode, true);
228+
}
229+
212230
if (methDocs.Descendants(DocElementNames.InheritDoc).Any())
213231
{
214232
logger.Write(ILogger.Severity.Diag, "Processing DocID: " + memID);
@@ -257,7 +275,7 @@ private static void replaceInheritDoc(string file, XElement mem, IDictionary<str
257275
string? cref = (string)inh.Attribute(DocAttributeNames.Cref) ?? dml?.FirstOrDefault()?.Cref;
258276
if (string.IsNullOrEmpty(cref))
259277
{
260-
if (!mem.HasAttribute(DocAttributeNames._trimmed))
278+
if (!mem.HasAttribute(DocAttributeNames._trimmed) && !mem.HasAttribute(DocAttributeNames._gencode))
261279
logger.Warn(ErrorCodes.NoBase, file, mem.SourceLine(), mem.SourceColumn(), "Cref not present and no base could be found for: " + memID);
262280

263281
continue;
@@ -275,7 +293,7 @@ private static void replaceInheritDoc(string file, XElement mem, IDictionary<str
275293
doc = findDocsByID(members, cref!).FirstOrDefault();
276294
if (doc is null)
277295
{
278-
if (!mem.HasAttribute(DocAttributeNames._trimmed))
296+
if (!mem.HasAttribute(DocAttributeNames._trimmed) && !mem.HasAttribute(DocAttributeNames._gencode))
279297
logger.Warn(ErrorCodes.NoDocs, file, mem.SourceLine(), mem.SourceColumn(), "No matching documentation could be found for: " + memID + ", which attempts to inherit from: " + cref);
280298

281299
continue;
@@ -364,7 +382,7 @@ static void removeDoc(List<XNode> nodes, int pos)
364382
if (nodes.Count > 0 && nodes[0].IsWhiteSpace())
365383
nodes.RemoveAt(0);
366384

367-
if (nodes.Count == 0 && !inh.Ancestors(DocElementNames.Member).Any(m => m.HasAttribute(DocAttributeNames._trimmed)))
385+
if (nodes.Count == 0 && !inh.Ancestors(DocElementNames.Member).Any(m => m.HasAttribute(DocAttributeNames._trimmed) || m.HasAttribute(DocAttributeNames._gencode)))
368386
logger.Warn(ErrorCodes.NoNode, file, inh.SourceLine(), inh.SourceColumn(), "No matching non-duplicate nodes found for: " + memID + ", which attempts to inherit from: " + dm.Cref + " path=\"" + xpath + "\"");
369387
else
370388
inh.ReplaceWith(nodes);

0 commit comments

Comments
 (0)