Browse Source

Simplify ClangParser by making it static

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1391/head
Dimitar Dobrev 5 years ago
parent
commit
926fca6087
  1. 18
      src/Generator/Driver.cs
  2. 29
      src/Parser/Parser.cs

18
src/Generator/Driver.cs

@ -124,19 +124,18 @@ namespace CppSharp @@ -124,19 +124,18 @@ namespace CppSharp
public bool ParseCode()
{
var astContext = new Parser.AST.ASTContext();
var parser = new ClangParser(astContext);
parser.SourcesParsed += OnSourceFileParsed;
ClangParser.SourcesParsed += OnSourceFileParsed;
var sourceFiles = Options.Modules.SelectMany(m => m.Headers);
ParserOptions.BuildForSourceFile(Options.Modules);
using (ParserResult result = parser.ParseSourceFiles(
using (ParserResult result = ClangParser.ParseSourceFiles(
sourceFiles, ParserOptions))
Context.TargetInfo = result.TargetInfo;
Context.ASTContext = ClangParser.ConvertASTContext(astContext);
Context.ASTContext = ClangParser.ConvertASTContext(ParserOptions.ASTContext);
ClangParser.SourcesParsed -= OnSourceFileParsed;
return !hasParsingErrors;
}
@ -161,6 +160,7 @@ namespace CppSharp @@ -161,6 +160,7 @@ namespace CppSharp
public bool ParseLibraries()
{
ClangParser.LibraryParsed += OnFileParsed;
foreach (var module in Options.Modules)
{
foreach (var libraryDir in module.LibraryDirs)
@ -171,10 +171,7 @@ namespace CppSharp @@ -171,10 +171,7 @@ namespace CppSharp
if (Context.Symbols.Libraries.Any(l => l.FileName == library))
continue;
var parser = new ClangParser();
parser.LibraryParsed += OnFileParsed;
using (var res = parser.ParseLibrary(library, ParserOptions))
using (var res = ClangParser.ParseLibrary(library, ParserOptions))
{
if (res.Kind != ParserResultKind.Success)
continue;
@ -183,6 +180,7 @@ namespace CppSharp @@ -183,6 +180,7 @@ namespace CppSharp
}
}
}
ClangParser.LibraryParsed -= OnFileParsed;
Context.Symbols.IndexSymbols();
SortModulesByDependencies();

29
src/Parser/Parser.cs

@ -8,37 +8,22 @@ using NativeLibrary = CppSharp.Parser.AST.NativeLibrary; @@ -8,37 +8,22 @@ using NativeLibrary = CppSharp.Parser.AST.NativeLibrary;
namespace CppSharp
{
public class ClangParser
public static class ClangParser
{
/// <summary>
/// Context with translation units ASTs.
/// </summary>
public ASTContext ASTContext { get; private set; }
/// <summary>
/// Fired when source files are parsed.
/// </summary>
public Action<IEnumerable<string>, ParserResult> SourcesParsed = delegate {};
public static Action<IEnumerable<string>, ParserResult> SourcesParsed = delegate {};
/// <summary>
/// Fired when library files are parsed.
/// </summary>
public Action<string, ParserResult> LibraryParsed = delegate {};
public ClangParser()
{
ASTContext = new ASTContext();
}
public ClangParser(ASTContext context)
{
ASTContext = context;
}
public static Action<string, ParserResult> LibraryParsed = delegate {};
/// <summary>
/// Parses a C++ source file as a translation unit.
/// </summary>
public ParserResult ParseSourceFile(string file, ParserOptions options)
public static ParserResult ParseSourceFile(string file, ParserOptions options)
{
return ParseSourceFiles(new [] { file }, options);
}
@ -46,9 +31,9 @@ namespace CppSharp @@ -46,9 +31,9 @@ namespace CppSharp
/// <summary>
/// Parses a set of C++ source files as a single translation unit.
/// </summary>
public ParserResult ParseSourceFiles(IEnumerable<string> files, ParserOptions options)
public static ParserResult ParseSourceFiles(IEnumerable<string> files, ParserOptions options)
{
options.ASTContext = ASTContext;
options.ASTContext = new ASTContext();
foreach (var file in files)
options.AddSourceFiles(file);
@ -62,7 +47,7 @@ namespace CppSharp @@ -62,7 +47,7 @@ namespace CppSharp
/// <summary>
/// Parses a library file with symbols.
/// </summary>
public ParserResult ParseLibrary(string file, ParserOptions options)
public static ParserResult ParseLibrary(string file, ParserOptions options)
{
options.LibraryFile = file;

Loading…
Cancel
Save