Skip to content

Commit 36c5f0e

Browse files
committed
Removed the regular expressions when searching for documentation nodes.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent c5e5d93 commit 36c5f0e

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

QtSharp/DocGeneration/Documentation.cs

+49-52
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Xml;
99
using CppSharp;
1010
using CppSharp.AST;
11+
using CppSharp.Generators;
1112
using CppSharp.Generators.CSharp;
1213
using HtmlAgilityPack;
1314
using Mono.Data.Sqlite;
@@ -251,64 +252,31 @@ public void DocumentFunction(Function function)
251252
var lineStart = function.LineNumberStart;
252253
var lineEnd = function.LineNumberEnd;
253254
var functions = this.functionNodes[function.OriginalName];
255+
var unit = function.OriginalNamespace.TranslationUnit;
256+
var location = unit.FileName;
254257
var node = functions.Find(
255-
f => f.Location == function.TranslationUnit.FileName &&
256-
(f.LineNumber == lineStart || f.LineNumber == lineEnd));
258+
f => f.Location == location &&
259+
(f.LineNumber == lineStart || f.LineNumber == lineEnd));
257260
var @params = function.Parameters.Where(p => p.Kind == ParameterKind.Regular).ToList();
258-
// HACK: functions in qglobal.h have weird additional definitions just for docs
259-
if ((node == null || node.HRef == null) && function.TranslationUnit.FileName == "qglobal.h")
260-
{
261-
node = functions.Find(
262-
c => c.Location == function.TranslationUnit.FileName &&
263-
(c.FullName == function.QualifiedOriginalName || c.Name == function.OriginalName) &&
264-
c.Access != "private" && c.ParametersModifiers.Count == @params.Count);
265-
}
266-
// HACK: some functions are "located" in a cpp, go figure...
267-
if ((node == null || node.HRef == null) && function.Signature != null)
261+
int realParamsCount = @params.Count(p => !string.IsNullOrWhiteSpace(p.OriginalName) || p.DefaultArgument == null);
262+
// functions can have different line numbers because of #defines
263+
if (node == null || node.HRef == null)
268264
{
269-
var qualifiedOriginalName = function.GetQualifiedName(decl => decl.OriginalName,
270-
decl =>
271-
{
272-
var @class = decl.OriginalNamespace as Class;
273-
return @class != null ? (@class.OriginalClass ?? @class) : decl.OriginalNamespace;
274-
});
275-
var nodes = functions.Where(f => f.FullName == qualifiedOriginalName &&
276-
f.ParametersModifiers.Count == @params.Count).ToList();
277-
if (nodes.Count == 0)
278-
{
279-
@params.RemoveAll(p => string.IsNullOrEmpty(p.OriginalName) && p.DefaultArgument != null);
280-
}
281-
nodes = functions.Where(f => f.FullName == qualifiedOriginalName &&
282-
f.ParametersModifiers.Count == @params.Count).ToList();
283-
if (nodes.Count == 0)
284-
{
285-
var method = function as Method;
286-
if (method != null && !method.IsStatic && function.OriginalFunction == null)
287-
{
288-
return;
289-
}
290-
nodes = functions.Where(f => f.Name == function.OriginalName &&
291-
f.ParametersModifiers.Count == @params.Count).ToList();
292-
}
265+
var nodes = functions.FindAll(
266+
f => CheckLocation(f.Location, location) &&
267+
(f.FullName == function.QualifiedOriginalName || f.Name == function.OriginalName) &&
268+
f.Access != "private" && f.ParametersModifiers.Count == realParamsCount);
269+
// HACK: work around https://bugreports.qt.io/browse/QTBUG-53994
293270
if (nodes.Count == 1)
294271
{
295272
node = nodes[0];
296273
}
297274
else
298275
{
299-
if (function.Signature.Contains('('))
300-
{
301-
var startArgs = function.Signature.IndexOf('(') + 1;
302-
var signature = function.Signature;
303-
if (signature.Contains(": "))
304-
{
305-
signature = signature.Substring(0, signature.IndexOf(": ", StringComparison.Ordinal));
306-
}
307-
signature = signature.Substring(startArgs, signature.LastIndexOf(')') - startArgs);
308-
signature = this.regexSpaceBetweenArgs.Replace(this.regexArgName.Replace(signature, "$1$3$5"), " ");
309-
node = nodes.Find(f => string.Join(", ",
310-
f.ParametersModifiers.Select(m => m.Replace(" *", "*").Replace(" &", "&"))) == signature);
311-
}
276+
Generator.CurrentOutputNamespace = unit.Module.OutputNamespace;
277+
var paramTypes = @params.Select(p => p.Type.ToString()).ToList();
278+
node = nodes.Find(
279+
f => f.ParametersModifiers.SequenceEqual(paramTypes, new TypeInIndexEqualityComparer()));
312280
}
313281
}
314282
if (node != null && node.HRef != null)
@@ -323,7 +291,7 @@ public void DocumentFunction(Function function)
323291
{
324292
var docs = this.membersDocumentation[file][key];
325293
var i = 0;
326-
// HACK: work around bugs of the type of https://bugreports.qt.io/browse/QTBUG-46148
294+
// HACK: work around https://bugreports.qt.io/browse/QTBUG-53941
327295
if (function.Namespace.Name == "QByteArray" &&
328296
((function.OriginalName == "qCompress" && @params.Count == 2) ||
329297
(function.OriginalName == "qUncompress" && @params.Count == 1)))
@@ -351,6 +319,24 @@ public void DocumentFunction(Function function)
351319
}
352320
}
353321

322+
private static bool CheckLocation(string indexLocation, string location)
323+
{
324+
if (indexLocation == location)
325+
{
326+
return true;
327+
}
328+
// work around https://bugreports.qt.io/browse/QTBUG-53946
329+
if (Path.GetExtension(indexLocation) != ".h" && indexLocation.Contains('_'))
330+
{
331+
var i = indexLocation.IndexOf('_');
332+
if (i >= 0)
333+
{
334+
return indexLocation.Substring(0, i) + ".h" == location;
335+
}
336+
}
337+
return false;
338+
}
339+
354340
private static string EscapeId(string link)
355341
{
356342
var idBuilder = new StringBuilder();
@@ -787,8 +773,6 @@ private static void AddObsoleteAttribute(Declaration function)
787773

788774
private readonly Dictionary<string, List<DocumentationNode>> typesDocumentation = new Dictionary<string, List<DocumentationNode>>();
789775
private readonly Dictionary<string, Dictionary<string, List<MemberDocumentationNode>>> membersDocumentation = new Dictionary<string, Dictionary<string, List<MemberDocumentationNode>>>();
790-
private readonly Regex regexArgName = new Regex(@"((unsigned\s*)?[\w<>]+)\s*(\*|&)?\s*\w*(\s*=\s*[^=,]+?)?(,|$)", RegexOptions.Compiled);
791-
private readonly Regex regexSpaceBetweenArgs = new Regex(@"\r?\n\s+", RegexOptions.Compiled);
792776

793777
private readonly Dictionary<string, List<FunctionDocIndexNode>> functionNodes = new Dictionary<string, List<FunctionDocIndexNode>>();
794778
private readonly Dictionary<string, List<FullNameDocIndexNode>> propertyNodes = new Dictionary<string, List<FullNameDocIndexNode>>();
@@ -797,5 +781,18 @@ private static void AddObsoleteAttribute(Declaration function)
797781
private readonly List<DocIndexNode> variableNodes = new List<DocIndexNode>();
798782

799783
private Regex regexParameters = new Regex(@"<i>\s*(.+?)\s*</i>", RegexOptions.Compiled);
784+
785+
private class TypeInIndexEqualityComparer : IEqualityComparer<string>
786+
{
787+
public bool Equals(string x, string y)
788+
{
789+
return x.Contains(y);
790+
}
791+
792+
public int GetHashCode(string obj)
793+
{
794+
return obj.GetHashCode();
795+
}
796+
}
800797
}
801798
}

QtSharp/GetCommentsFromQtDocsPass.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public GetCommentsFromQtDocsPass(string docsPath, IEnumerable<string> modules)
1313
this.documentation = new Documentation(docsPath, modules);
1414
this.Options.VisitFunctionReturnType = false;
1515
this.Options.VisitFunctionParameters = false;
16+
this.Options.VisitEventParameters = false;
1617
this.Options.VisitClassBases = false;
1718
this.Options.VisitTemplateArguments = false;
1819
this.Options.VisitClassFields = false;
@@ -23,6 +24,11 @@ public override bool VisitLibrary(ASTContext context)
2324
return this.documentation.Exists && base.VisitLibrary(context);
2425
}
2526

27+
public override bool VisitTranslationUnit(TranslationUnit unit)
28+
{
29+
return !unit.IsSystemHeader && unit.IsGenerated && base.VisitTranslationUnit(unit);
30+
}
31+
2632
public override bool VisitClassDecl(Class @class)
2733
{
2834
if (!@class.IsIncomplete && base.VisitClassDecl(@class))
@@ -58,7 +64,7 @@ public override bool VisitClassDecl(Class @class)
5864

5965
public override bool VisitDeclarationContext(DeclarationContext context)
6066
{
61-
return context.IsGenerated && base.VisitDeclarationContext(context);
67+
return context.IsGenerated && !context.TranslationUnit.IsSystemHeader && base.VisitDeclarationContext(context);
6268
}
6369

6470
public override bool VisitEnumDecl(Enumeration @enum)
@@ -77,7 +83,7 @@ public override bool VisitEnumDecl(Enumeration @enum)
7783

7884
public override bool VisitFunctionDecl(Function function)
7985
{
80-
if (!base.VisitFunctionDecl(function))
86+
if (!base.VisitFunctionDecl(function) || function.TranslationUnit.IsSystemHeader)
8187
{
8288
return false;
8389
}
@@ -102,7 +108,7 @@ public override bool VisitFunctionDecl(Function function)
102108

103109
public override bool VisitProperty(Property property)
104110
{
105-
if (!base.VisitProperty(property))
111+
if (!base.VisitProperty(property) || property.TranslationUnit.IsSystemHeader)
106112
{
107113
return false;
108114
}
@@ -167,7 +173,7 @@ public override bool VisitEvent(Event @event)
167173
public override bool VisitVariableDecl(Variable variable)
168174
{
169175
// HACK: it doesn't work to call the base as everywhere else because the type of the variable is visited too
170-
if (this.AlreadyVisited(variable))
176+
if (this.AlreadyVisited(variable) || variable.TranslationUnit.IsSystemHeader)
171177
{
172178
return false;
173179
}

0 commit comments

Comments
 (0)