Skip to content

Commit

Permalink
Added an option for skipping private declarations.
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Jan 15, 2019
1 parent 7817b52 commit e203463
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/CppParser/Bindings/CLI/CppParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,16 @@ void CppSharp::Parser::CppParserOptions::UnityBuild::set(bool value)
((::CppSharp::CppParser::CppParserOptions*)NativePtr)->unityBuild = value;
}

bool CppSharp::Parser::CppParserOptions::SkipPrivateDeclarations::get()
{
return ((::CppSharp::CppParser::CppParserOptions*)NativePtr)->skipPrivateDeclarations;
}

void CppSharp::Parser::CppParserOptions::SkipPrivateDeclarations::set(bool value)
{
((::CppSharp::CppParser::CppParserOptions*)NativePtr)->skipPrivateDeclarations = value;
}

unsigned int CppSharp::Parser::CppParserOptions::ArgumentsCount::get()
{
auto __ret = ((::CppSharp::CppParser::CppParserOptions*)NativePtr)->getArgumentsCount();
Expand Down
6 changes: 6 additions & 0 deletions src/CppParser/Bindings/CLI/CppParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ namespace CppSharp
void set(bool);
}

property bool SkipPrivateDeclarations
{
bool get();
void set(bool);
}

property unsigned int ArgumentsCount
{
unsigned int get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18041,6 +18041,9 @@ public partial struct __Internal
[FieldOffset(148)]
internal byte unityBuild;

[FieldOffset(149)]
internal byte skipPrivateDeclarations;

[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN8CppSharp9CppParser16CppParserOptionsC2Ev")]
Expand Down Expand Up @@ -18578,6 +18581,19 @@ public bool UnityBuild
}
}

public bool SkipPrivateDeclarations
{
get
{
return ((global::CppSharp.Parser.CppParserOptions.__Internal*) __Instance)->skipPrivateDeclarations != 0;
}

set
{
((global::CppSharp.Parser.CppParserOptions.__Internal*)__Instance)->skipPrivateDeclarations = (byte) (value ? 1 : 0);
}
}

public uint ArgumentsCount
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18041,6 +18041,9 @@ public partial struct __Internal
[FieldOffset(184)]
internal byte unityBuild;

[FieldOffset(185)]
internal byte skipPrivateDeclarations;

[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall,
EntryPoint="??0CppParserOptions@CppParser@CppSharp@@QAE@XZ")]
Expand Down Expand Up @@ -18578,6 +18581,19 @@ public bool UnityBuild
}
}

public bool SkipPrivateDeclarations
{
get
{
return ((global::CppSharp.Parser.CppParserOptions.__Internal*) __Instance)->skipPrivateDeclarations != 0;
}

set
{
((global::CppSharp.Parser.CppParserOptions.__Internal*)__Instance)->skipPrivateDeclarations = (byte) (value ? 1 : 0);
}
}

public uint ArgumentsCount
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18040,6 +18040,9 @@ public partial struct __Internal
[FieldOffset(288)]
internal byte unityBuild;

[FieldOffset(289)]
internal byte skipPrivateDeclarations;

[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN8CppSharp9CppParser16CppParserOptionsC2Ev")]
Expand Down Expand Up @@ -18577,6 +18580,19 @@ public bool UnityBuild
}
}

public bool SkipPrivateDeclarations
{
get
{
return ((global::CppSharp.Parser.CppParserOptions.__Internal*) __Instance)->skipPrivateDeclarations != 0;
}

set
{
((global::CppSharp.Parser.CppParserOptions.__Internal*)__Instance)->skipPrivateDeclarations = (byte) (value ? 1 : 0);
}
}

public uint ArgumentsCount
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18040,6 +18040,9 @@ public partial struct __Internal
[FieldOffset(312)]
internal byte unityBuild;

[FieldOffset(313)]
internal byte skipPrivateDeclarations;

[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN8CppSharp9CppParser16CppParserOptionsC2Ev")]
Expand Down Expand Up @@ -18577,6 +18580,19 @@ public bool UnityBuild
}
}

public bool SkipPrivateDeclarations
{
get
{
return ((global::CppSharp.Parser.CppParserOptions.__Internal*) __Instance)->skipPrivateDeclarations != 0;
}

set
{
((global::CppSharp.Parser.CppParserOptions.__Internal*)__Instance)->skipPrivateDeclarations = (byte) (value ? 1 : 0);
}
}

public uint ArgumentsCount
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18040,6 +18040,9 @@ public partial struct __Internal
[FieldOffset(240)]
internal byte unityBuild;

[FieldOffset(241)]
internal byte skipPrivateDeclarations;

[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN8CppSharp9CppParser16CppParserOptionsC2Ev")]
Expand Down Expand Up @@ -18577,6 +18580,19 @@ public bool UnityBuild
}
}

public bool SkipPrivateDeclarations
{
get
{
return ((global::CppSharp.Parser.CppParserOptions.__Internal*) __Instance)->skipPrivateDeclarations != 0;
}

set
{
((global::CppSharp.Parser.CppParserOptions.__Internal*)__Instance)->skipPrivateDeclarations = (byte) (value ? 1 : 0);
}
}

public uint ArgumentsCount
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18041,6 +18041,9 @@ public partial struct __Internal
[FieldOffset(312)]
internal byte unityBuild;

[FieldOffset(313)]
internal byte skipPrivateDeclarations;

[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="??0CppParserOptions@CppParser@CppSharp@@QEAA@XZ")]
Expand Down Expand Up @@ -18578,6 +18581,19 @@ public bool UnityBuild
}
}

public bool SkipPrivateDeclarations
{
get
{
return ((global::CppSharp.Parser.CppParserOptions.__Internal*) __Instance)->skipPrivateDeclarations != 0;
}

set
{
((global::CppSharp.Parser.CppParserOptions.__Internal*)__Instance)->skipPrivateDeclarations = (byte) (value ? 1 : 0);
}
}

public uint ArgumentsCount
{
get
Expand Down
1 change: 1 addition & 0 deletions src/CppParser/CppParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ CppParserOptions::CppParserOptions()
, microsoftMode(false)
, verbose(false)
, unityBuild(false)
, skipPrivateDeclarations(true)
{
}

Expand Down
1 change: 1 addition & 0 deletions src/CppParser/CppParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct CS_API CppParserOptions
bool microsoftMode;
bool verbose;
bool unityBuild;
bool skipPrivateDeclarations;
};

enum class ParserDiagnosticLevel
Expand Down
32 changes: 18 additions & 14 deletions src/CppParser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,8 @@ void Parser::ReadClassLayout(Class* Class, const clang::RecordDecl* RD,

// Dump fields.
uint64_t FieldNo = 0;
for (RecordDecl::field_iterator I = RD->field_begin(),
E = RD->field_end(); I != E; ++I, ++FieldNo) {
auto Field = *I;
uint64_t LocalFieldOffsetInBits = Layout.getFieldOffset(FieldNo);
for (const clang::FieldDecl* Field : RD->fields()) {
uint64_t LocalFieldOffsetInBits = Layout.getFieldOffset(FieldNo++);
CharUnits FieldOffset =
Offset + c->getASTContext().toCharUnitsFromBits(LocalFieldOffsetInBits);

Expand Down Expand Up @@ -966,7 +964,7 @@ void Parser::WalkRecord(const clang::RecordDecl* Record, Class* RC)
{
auto MD = cast<CXXMethodDecl>(D);
if (IsSupported(MD))
WalkMethodCXX(MD);
WalkDeclaration(MD);
break;
}
}
Expand All @@ -975,19 +973,14 @@ void Parser::WalkRecord(const clang::RecordDecl* Record, Class* RC)
return;
}

if (opts->skipPrivateDeclarations &&
Record->getAccess() == clang::AccessSpecifier::AS_private)
return;

for (auto D : Record->decls())
{
switch (D->getKind())
{
case Decl::CXXConstructor:
case Decl::CXXDestructor:
case Decl::CXXConversion:
case Decl::CXXMethod:
{
auto MD = cast<CXXMethodDecl>(D);
WalkMethodCXX(MD);
break;
}
case Decl::AccessSpec:
{
AccessSpecDecl* AS = cast<AccessSpecDecl>(D);
Expand Down Expand Up @@ -1514,6 +1507,10 @@ TypeAliasTemplate* Parser::WalkTypeAliasTemplate(

FunctionTemplate* Parser::WalkFunctionTemplate(const clang::FunctionTemplateDecl* TD)
{
if (opts->skipPrivateDeclarations &&
TD->getAccess() == clang::AccessSpecifier::AS_private)
return nullptr;

using namespace clang;

auto NS = GetNamespace(TD);
Expand Down Expand Up @@ -1729,6 +1726,11 @@ static CXXOperatorKind GetOperatorKindFromDecl(clang::DeclarationName Name)

Method* Parser::WalkMethodCXX(const clang::CXXMethodDecl* MD)
{
if (opts->skipPrivateDeclarations &&
MD->getAccess() == clang::AccessSpecifier::AS_private &&
!MD->isVirtual())
return nullptr;

using namespace clang;

// We could be in a redeclaration, so process the primary context.
Expand Down Expand Up @@ -3930,6 +3932,8 @@ Declaration* Parser::WalkDeclaration(const clang::Decl* D)
{
auto MD = cast<CXXMethodDecl>(D);
Decl = WalkMethodCXX(MD);
if (Decl == nullptr)
return Decl;

auto NS = GetNamespace(MD);
Decl->_namespace = NS;
Expand Down
1 change: 1 addition & 0 deletions src/Generator.Tests/ASTTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected void ParseLibrary(params string[] files)

var testsPath = GeneratorTest.GetTestsDirectory("Native");
ParserOptions.AddIncludeDirs(testsPath);
ParserOptions.SkipPrivateDeclarations = true;

var module = Options.AddModule("Test");
module.Headers.AddRange(files);
Expand Down
12 changes: 3 additions & 9 deletions src/Generator.Tests/Passes/TestPasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,11 @@ public void TestUniqueNamesAcrossTranslationUnits()
}

[Test]
public void TestStructInheritance()
{

}

[Test]
public void TestIgnoringMethod()
public void TestSkippedPrivateMethod()
{
AstContext.IgnoreClassMethodWithName("Foo", "toIgnore");
Assert.IsFalse(AstContext.FindClass("Foo").First().Methods.Find(
m => m.Name == "toIgnore").IsGenerated);
Assert.That(AstContext.FindClass("Foo").First().Methods.Find(
m => m.Name == "toIgnore"), Is.Null);
}

[Test]
Expand Down

0 comments on commit e203463

Please sign in to comment.