mirror of https://github.com/mono/CppSharp.git
3 changed files with 222 additions and 158 deletions
@ -0,0 +1,216 @@ |
|||||||
|
using System; |
||||||
|
|
||||||
|
namespace CppSharp.Passes |
||||||
|
{ |
||||||
|
public class CheckIgnoredDeclsPass : TranslationUnitPass |
||||||
|
{ |
||||||
|
public CheckIgnoredDeclsPass() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
public override bool VisitDeclaration(Declaration decl) |
||||||
|
{ |
||||||
|
if (decl.IsDependent) |
||||||
|
{ |
||||||
|
decl.ExplicityIgnored = true; |
||||||
|
Console.WriteLine("Decl '{0}' was ignored due to dependent context", |
||||||
|
decl.Name); |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public override bool VisitFieldDecl(Field field) |
||||||
|
{ |
||||||
|
if (!VisitDeclaration(field)) |
||||||
|
return false; |
||||||
|
|
||||||
|
var type = field.Type; |
||||||
|
|
||||||
|
string msg; |
||||||
|
if (!HasInvalidType(type, out msg)) |
||||||
|
return false; |
||||||
|
|
||||||
|
field.ExplicityIgnored = true; |
||||||
|
|
||||||
|
Console.WriteLine("Field '{0}' was ignored due to {1} type", |
||||||
|
field.Name, msg); |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public override bool VisitFunctionDecl(Function function) |
||||||
|
{ |
||||||
|
if (!VisitDeclaration(function)) |
||||||
|
return false; |
||||||
|
|
||||||
|
var ret = function.ReturnType; |
||||||
|
|
||||||
|
string msg; |
||||||
|
if (HasInvalidType(ret.Type, out msg)) |
||||||
|
{ |
||||||
|
function.ExplicityIgnored = true; |
||||||
|
Console.WriteLine("Function '{0}' was ignored due to {1} return decl", |
||||||
|
function.Name, msg); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
foreach (var param in function.Parameters) |
||||||
|
{ |
||||||
|
if (HasInvalidDecl(param, out msg)) |
||||||
|
{ |
||||||
|
function.ExplicityIgnored = true; |
||||||
|
Console.WriteLine("Function '{0}' was ignored due to {1} param", |
||||||
|
function.Name, msg); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (HasInvalidType(param.Type, out msg)) |
||||||
|
{ |
||||||
|
function.ExplicityIgnored = true; |
||||||
|
Console.WriteLine("Function '{0}' was ignored due to {1} param", |
||||||
|
function.Name, msg); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public override bool VisitTypedefDecl(TypedefDecl typedef) |
||||||
|
{ |
||||||
|
if (!VisitDeclaration(typedef)) |
||||||
|
return false; |
||||||
|
|
||||||
|
string msg; |
||||||
|
if (HasInvalidType(typedef.Type, out msg)) |
||||||
|
{ |
||||||
|
typedef.ExplicityIgnored = true; |
||||||
|
Console.WriteLine("Typedef '{0}' was ignored due to {1} type", |
||||||
|
typedef.Name, msg); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public override bool VisitProperty(Property property) |
||||||
|
{ |
||||||
|
if (!VisitDeclaration(property)) |
||||||
|
return false; |
||||||
|
|
||||||
|
string msg; |
||||||
|
if (HasInvalidDecl(property, out msg)) |
||||||
|
{ |
||||||
|
property.ExplicityIgnored = true; |
||||||
|
Console.WriteLine("Property '{0}' was ignored due to {1} decl", |
||||||
|
property.Name, msg); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (HasInvalidType(property.Type, out msg)) |
||||||
|
{ |
||||||
|
property.ExplicityIgnored = true; |
||||||
|
Console.WriteLine("Property '{0}' was ignored due to {1} type", |
||||||
|
property.Name, msg); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
#region Helpers
|
||||||
|
|
||||||
|
/// <remarks>
|
||||||
|
/// Checks if a given type is invalid, which can happen for a number of
|
||||||
|
/// reasons: incomplete definitions, being explicitly ignored, or also
|
||||||
|
/// by being a type we do not know how to handle.
|
||||||
|
/// </remarks>
|
||||||
|
bool HasInvalidType(Type type, out string msg) |
||||||
|
{ |
||||||
|
if (type == null) |
||||||
|
{ |
||||||
|
msg = "null"; |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if (!IsTypeComplete(type)) |
||||||
|
{ |
||||||
|
msg = "incomplete"; |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if (IsTypeIgnored(type)) |
||||||
|
{ |
||||||
|
msg = "ignored"; |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
msg = null; |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
bool HasInvalidDecl(Declaration decl, out string msg) |
||||||
|
{ |
||||||
|
if (decl == null) |
||||||
|
{ |
||||||
|
msg = "null"; |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if (!IsDeclComplete(decl)) |
||||||
|
{ |
||||||
|
msg = "incomplete"; |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if (IsDeclIgnored(decl)) |
||||||
|
{ |
||||||
|
msg = "ignored"; |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
msg = null; |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
static bool IsTypeComplete(Type type) |
||||||
|
{ |
||||||
|
var checker = new TypeCompletionChecker(); |
||||||
|
return type.Visit(checker); |
||||||
|
} |
||||||
|
|
||||||
|
static bool IsDeclComplete(Declaration decl) |
||||||
|
{ |
||||||
|
var checker = new TypeCompletionChecker(); |
||||||
|
return decl.Visit(checker); |
||||||
|
} |
||||||
|
|
||||||
|
bool IsTypeIgnored(Type type) |
||||||
|
{ |
||||||
|
var checker = new TypeIgnoreChecker(Driver.TypeDatabase); |
||||||
|
type.Visit(checker); |
||||||
|
|
||||||
|
return checker.IsIgnored; |
||||||
|
} |
||||||
|
|
||||||
|
bool IsDeclIgnored(Declaration decl) |
||||||
|
{ |
||||||
|
var checker = new TypeIgnoreChecker(Driver.TypeDatabase); |
||||||
|
decl.Visit(checker); |
||||||
|
|
||||||
|
return checker.IsIgnored; |
||||||
|
} |
||||||
|
|
||||||
|
#endregion
|
||||||
|
} |
||||||
|
|
||||||
|
public static class CheckIgnoredDeclsPassExtensions |
||||||
|
{ |
||||||
|
public static void CheckIgnoredDecls(this PassBuilder builder) |
||||||
|
{ |
||||||
|
var pass = new CheckIgnoredDeclsPass(); |
||||||
|
builder.AddPass(pass); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue