From 36c5f0e42ac40069b5fbeb8660c9a27ed34bd55d Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Fri, 10 Jun 2016 18:04:24 +0300 Subject: [PATCH] Removed the regular expressions when searching for documentation nodes. Signed-off-by: Dimitar Dobrev --- QtSharp/DocGeneration/Documentation.cs | 101 ++++++++++++------------- QtSharp/GetCommentsFromQtDocsPass.cs | 14 +++- 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/QtSharp/DocGeneration/Documentation.cs b/QtSharp/DocGeneration/Documentation.cs index b169a587..d20aea1d 100644 --- a/QtSharp/DocGeneration/Documentation.cs +++ b/QtSharp/DocGeneration/Documentation.cs @@ -8,6 +8,7 @@ using System.Xml; using CppSharp; using CppSharp.AST; +using CppSharp.Generators; using CppSharp.Generators.CSharp; using HtmlAgilityPack; using Mono.Data.Sqlite; @@ -251,64 +252,31 @@ public void DocumentFunction(Function function) var lineStart = function.LineNumberStart; var lineEnd = function.LineNumberEnd; var functions = this.functionNodes[function.OriginalName]; + var unit = function.OriginalNamespace.TranslationUnit; + var location = unit.FileName; var node = functions.Find( - f => f.Location == function.TranslationUnit.FileName && - (f.LineNumber == lineStart || f.LineNumber == lineEnd)); + f => f.Location == location && + (f.LineNumber == lineStart || f.LineNumber == lineEnd)); var @params = function.Parameters.Where(p => p.Kind == ParameterKind.Regular).ToList(); - // HACK: functions in qglobal.h have weird additional definitions just for docs - if ((node == null || node.HRef == null) && function.TranslationUnit.FileName == "qglobal.h") - { - node = functions.Find( - c => c.Location == function.TranslationUnit.FileName && - (c.FullName == function.QualifiedOriginalName || c.Name == function.OriginalName) && - c.Access != "private" && c.ParametersModifiers.Count == @params.Count); - } - // HACK: some functions are "located" in a cpp, go figure... - if ((node == null || node.HRef == null) && function.Signature != null) + int realParamsCount = @params.Count(p => !string.IsNullOrWhiteSpace(p.OriginalName) || p.DefaultArgument == null); + // functions can have different line numbers because of #defines + if (node == null || node.HRef == null) { - var qualifiedOriginalName = function.GetQualifiedName(decl => decl.OriginalName, - decl => - { - var @class = decl.OriginalNamespace as Class; - return @class != null ? (@class.OriginalClass ?? @class) : decl.OriginalNamespace; - }); - var nodes = functions.Where(f => f.FullName == qualifiedOriginalName && - f.ParametersModifiers.Count == @params.Count).ToList(); - if (nodes.Count == 0) - { - @params.RemoveAll(p => string.IsNullOrEmpty(p.OriginalName) && p.DefaultArgument != null); - } - nodes = functions.Where(f => f.FullName == qualifiedOriginalName && - f.ParametersModifiers.Count == @params.Count).ToList(); - if (nodes.Count == 0) - { - var method = function as Method; - if (method != null && !method.IsStatic && function.OriginalFunction == null) - { - return; - } - nodes = functions.Where(f => f.Name == function.OriginalName && - f.ParametersModifiers.Count == @params.Count).ToList(); - } + var nodes = functions.FindAll( + f => CheckLocation(f.Location, location) && + (f.FullName == function.QualifiedOriginalName || f.Name == function.OriginalName) && + f.Access != "private" && f.ParametersModifiers.Count == realParamsCount); + // HACK: work around https://bugreports.qt.io/browse/QTBUG-53994 if (nodes.Count == 1) { node = nodes[0]; } else { - if (function.Signature.Contains('(')) - { - var startArgs = function.Signature.IndexOf('(') + 1; - var signature = function.Signature; - if (signature.Contains(": ")) - { - signature = signature.Substring(0, signature.IndexOf(": ", StringComparison.Ordinal)); - } - signature = signature.Substring(startArgs, signature.LastIndexOf(')') - startArgs); - signature = this.regexSpaceBetweenArgs.Replace(this.regexArgName.Replace(signature, "$1$3$5"), " "); - node = nodes.Find(f => string.Join(", ", - f.ParametersModifiers.Select(m => m.Replace(" *", "*").Replace(" &", "&"))) == signature); - } + Generator.CurrentOutputNamespace = unit.Module.OutputNamespace; + var paramTypes = @params.Select(p => p.Type.ToString()).ToList(); + node = nodes.Find( + f => f.ParametersModifiers.SequenceEqual(paramTypes, new TypeInIndexEqualityComparer())); } } if (node != null && node.HRef != null) @@ -323,7 +291,7 @@ public void DocumentFunction(Function function) { var docs = this.membersDocumentation[file][key]; var i = 0; - // HACK: work around bugs of the type of https://bugreports.qt.io/browse/QTBUG-46148 + // HACK: work around https://bugreports.qt.io/browse/QTBUG-53941 if (function.Namespace.Name == "QByteArray" && ((function.OriginalName == "qCompress" && @params.Count == 2) || (function.OriginalName == "qUncompress" && @params.Count == 1))) @@ -351,6 +319,24 @@ public void DocumentFunction(Function function) } } + private static bool CheckLocation(string indexLocation, string location) + { + if (indexLocation == location) + { + return true; + } + // work around https://bugreports.qt.io/browse/QTBUG-53946 + if (Path.GetExtension(indexLocation) != ".h" && indexLocation.Contains('_')) + { + var i = indexLocation.IndexOf('_'); + if (i >= 0) + { + return indexLocation.Substring(0, i) + ".h" == location; + } + } + return false; + } + private static string EscapeId(string link) { var idBuilder = new StringBuilder(); @@ -787,8 +773,6 @@ private static void AddObsoleteAttribute(Declaration function) private readonly Dictionary> typesDocumentation = new Dictionary>(); private readonly Dictionary>> membersDocumentation = new Dictionary>>(); - private readonly Regex regexArgName = new Regex(@"((unsigned\s*)?[\w<>]+)\s*(\*|&)?\s*\w*(\s*=\s*[^=,]+?)?(,|$)", RegexOptions.Compiled); - private readonly Regex regexSpaceBetweenArgs = new Regex(@"\r?\n\s+", RegexOptions.Compiled); private readonly Dictionary> functionNodes = new Dictionary>(); private readonly Dictionary> propertyNodes = new Dictionary>(); @@ -797,5 +781,18 @@ private static void AddObsoleteAttribute(Declaration function) private readonly List variableNodes = new List(); private Regex regexParameters = new Regex(@"\s*(.+?)\s*", RegexOptions.Compiled); + + private class TypeInIndexEqualityComparer : IEqualityComparer + { + public bool Equals(string x, string y) + { + return x.Contains(y); + } + + public int GetHashCode(string obj) + { + return obj.GetHashCode(); + } + } } } diff --git a/QtSharp/GetCommentsFromQtDocsPass.cs b/QtSharp/GetCommentsFromQtDocsPass.cs index 3dfdbfad..1615a14b 100644 --- a/QtSharp/GetCommentsFromQtDocsPass.cs +++ b/QtSharp/GetCommentsFromQtDocsPass.cs @@ -13,6 +13,7 @@ public GetCommentsFromQtDocsPass(string docsPath, IEnumerable modules) this.documentation = new Documentation(docsPath, modules); this.Options.VisitFunctionReturnType = false; this.Options.VisitFunctionParameters = false; + this.Options.VisitEventParameters = false; this.Options.VisitClassBases = false; this.Options.VisitTemplateArguments = false; this.Options.VisitClassFields = false; @@ -23,6 +24,11 @@ public override bool VisitLibrary(ASTContext context) return this.documentation.Exists && base.VisitLibrary(context); } + public override bool VisitTranslationUnit(TranslationUnit unit) + { + return !unit.IsSystemHeader && unit.IsGenerated && base.VisitTranslationUnit(unit); + } + public override bool VisitClassDecl(Class @class) { if (!@class.IsIncomplete && base.VisitClassDecl(@class)) @@ -58,7 +64,7 @@ public override bool VisitClassDecl(Class @class) public override bool VisitDeclarationContext(DeclarationContext context) { - return context.IsGenerated && base.VisitDeclarationContext(context); + return context.IsGenerated && !context.TranslationUnit.IsSystemHeader && base.VisitDeclarationContext(context); } public override bool VisitEnumDecl(Enumeration @enum) @@ -77,7 +83,7 @@ public override bool VisitEnumDecl(Enumeration @enum) public override bool VisitFunctionDecl(Function function) { - if (!base.VisitFunctionDecl(function)) + if (!base.VisitFunctionDecl(function) || function.TranslationUnit.IsSystemHeader) { return false; } @@ -102,7 +108,7 @@ public override bool VisitFunctionDecl(Function function) public override bool VisitProperty(Property property) { - if (!base.VisitProperty(property)) + if (!base.VisitProperty(property) || property.TranslationUnit.IsSystemHeader) { return false; } @@ -167,7 +173,7 @@ public override bool VisitEvent(Event @event) public override bool VisitVariableDecl(Variable variable) { // HACK: it doesn't work to call the base as everywhere else because the type of the variable is visited too - if (this.AlreadyVisited(variable)) + if (this.AlreadyVisited(variable) || variable.TranslationUnit.IsSystemHeader) { return false; }