Skip to content

Commit

Permalink
Removed the regular expressions when searching for documentation nodes.
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Jun 10, 2016
1 parent c5e5d93 commit 36c5f0e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 56 deletions.
101 changes: 49 additions & 52 deletions QtSharp/DocGeneration/Documentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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)))
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -787,8 +773,6 @@ private static void AddObsoleteAttribute(Declaration function)

private readonly Dictionary<string, List<DocumentationNode>> typesDocumentation = new Dictionary<string, List<DocumentationNode>>();
private readonly Dictionary<string, Dictionary<string, List<MemberDocumentationNode>>> membersDocumentation = new Dictionary<string, Dictionary<string, List<MemberDocumentationNode>>>();
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<string, List<FunctionDocIndexNode>> functionNodes = new Dictionary<string, List<FunctionDocIndexNode>>();
private readonly Dictionary<string, List<FullNameDocIndexNode>> propertyNodes = new Dictionary<string, List<FullNameDocIndexNode>>();
Expand All @@ -797,5 +781,18 @@ private static void AddObsoleteAttribute(Declaration function)
private readonly List<DocIndexNode> variableNodes = new List<DocIndexNode>();

private Regex regexParameters = new Regex(@"<i>\s*(.+?)\s*</i>", RegexOptions.Compiled);

private class TypeInIndexEqualityComparer : IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
return x.Contains(y);
}

public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
}
}
14 changes: 10 additions & 4 deletions QtSharp/GetCommentsFromQtDocsPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public GetCommentsFromQtDocsPass(string docsPath, IEnumerable<string> 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;
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 36c5f0e

Please sign in to comment.