mirror of https://github.com/mono/CppSharp.git
3 changed files with 157 additions and 117 deletions
@ -0,0 +1,94 @@ |
|||||||
|
using System; |
||||||
|
|
||||||
|
#if !OLD_PARSER
|
||||||
|
using CppSharp.Parser; |
||||||
|
using CppSharp.Parser.AST; |
||||||
|
#else
|
||||||
|
using CppSharp.AST; |
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace CppSharp |
||||||
|
{ |
||||||
|
public 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<SourceFile, ParserResult> SourceParsed = delegate {}; |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fire when library files are parsed.
|
||||||
|
/// </summary>
|
||||||
|
public Action<string, ParserResult> LibraryParsed = delegate {}; |
||||||
|
|
||||||
|
public ClangParser() |
||||||
|
{ |
||||||
|
ASTContext = new ASTContext(); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parses a C++ source file to a translation unit.
|
||||||
|
/// </summary>
|
||||||
|
public ParserResult ParseSourceFile(SourceFile file, ParserOptions options) |
||||||
|
{ |
||||||
|
//if (options.ASTContext == null)
|
||||||
|
options.ASTContext = ASTContext; |
||||||
|
|
||||||
|
options.FileName = file.Path; |
||||||
|
var result = Parser.ClangParser.ParseHeader(options); |
||||||
|
SourceParsed(file, result); |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parses the project source files.
|
||||||
|
/// </summary>
|
||||||
|
public void ParseProject(Project project, ParserOptions options) |
||||||
|
{ |
||||||
|
// TODO: Search for cached AST trees on disk
|
||||||
|
// TODO: Do multi-threaded parsing of source files
|
||||||
|
|
||||||
|
foreach (var source in project.Sources) |
||||||
|
ParseSourceFile(source, source.Options); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parses a library file with symbols.
|
||||||
|
/// </summary>
|
||||||
|
public ParserResult ParseLibrary(string file, ParserOptions options) |
||||||
|
{ |
||||||
|
options.FileName = file; |
||||||
|
var result = Parser.ClangParser.ParseLibrary(options); |
||||||
|
LibraryParsed(file, result); |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
#if !OLD_PARSER
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a native parser AST to a managed AST.
|
||||||
|
/// </summary>
|
||||||
|
static public AST.ASTContext ConvertASTContext(ASTContext context) |
||||||
|
{ |
||||||
|
var converter = new ASTConverter(context); |
||||||
|
return converter.Convert(); |
||||||
|
} |
||||||
|
|
||||||
|
public static AST.NativeLibrary ConvertLibrary(NativeLibrary library) |
||||||
|
{ |
||||||
|
var newLibrary = new AST.NativeLibrary { FileName = library.FileName }; |
||||||
|
|
||||||
|
foreach (var symbol in library.Symbols) |
||||||
|
newLibrary.Symbols.Add(symbol); |
||||||
|
|
||||||
|
return newLibrary; |
||||||
|
} |
||||||
|
#endif
|
||||||
|
} |
||||||
|
} |
@ -1,109 +0,0 @@ |
|||||||
using System; |
|
||||||
using System.Collections.Generic; |
|
||||||
using CppSharp.AST; |
|
||||||
|
|
||||||
namespace CppSharp |
|
||||||
{ |
|
||||||
public class Parser |
|
||||||
{ |
|
||||||
public Library Library { get; private set; } |
|
||||||
private readonly DriverOptions options; |
|
||||||
|
|
||||||
public Parser(DriverOptions options) |
|
||||||
{ |
|
||||||
this.options = options; |
|
||||||
Library = new Library(); |
|
||||||
} |
|
||||||
|
|
||||||
public bool ParseHeaders(IEnumerable<string> headers) |
|
||||||
{ |
|
||||||
var hasErrors = false; |
|
||||||
|
|
||||||
foreach (var header in headers) |
|
||||||
{ |
|
||||||
var result = ParseHeader(header); |
|
||||||
|
|
||||||
// If we have some error, report to end-user.
|
|
||||||
if (!options.IgnoreParseErrors) |
|
||||||
{ |
|
||||||
foreach (var diag in result.Diagnostics) |
|
||||||
{ |
|
||||||
if (diag.Level == ParserDiagnosticLevel.Error || |
|
||||||
diag.Level == ParserDiagnosticLevel.Fatal) |
|
||||||
hasErrors = true; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return !hasErrors; |
|
||||||
} |
|
||||||
|
|
||||||
public ParserResult ParseHeader(string file) |
|
||||||
{ |
|
||||||
var parserOptions = new ParserOptions |
|
||||||
{ |
|
||||||
Library = Library, |
|
||||||
FileName = file, |
|
||||||
Verbose = options.Verbose, |
|
||||||
IncludeDirs = options.IncludeDirs, |
|
||||||
SystemIncludeDirs = options.SystemIncludeDirs, |
|
||||||
Defines = options.Defines, |
|
||||||
NoStandardIncludes = options.NoStandardIncludes, |
|
||||||
NoBuiltinIncludes = options.NoBuiltinIncludes, |
|
||||||
MicrosoftMode = options.MicrosoftMode, |
|
||||||
ToolSetToUse = options.ToolsetToUse, |
|
||||||
TargetTriple = options.TargetTriple, |
|
||||||
Abi = options.Abi |
|
||||||
}; |
|
||||||
|
|
||||||
var result = ClangParser.ParseHeader(parserOptions); |
|
||||||
OnHeaderParsed(file, result); |
|
||||||
|
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
public bool ParseLibraries(IEnumerable<string> libraries) |
|
||||||
{ |
|
||||||
var hasErrors = false; |
|
||||||
|
|
||||||
foreach (var lib in libraries) |
|
||||||
{ |
|
||||||
var result = ParseLibrary(lib); |
|
||||||
|
|
||||||
// If we have some error, report to end-user.
|
|
||||||
if (!options.IgnoreParseErrors) |
|
||||||
{ |
|
||||||
foreach (var diag in result.Diagnostics) |
|
||||||
{ |
|
||||||
if (diag.Level == ParserDiagnosticLevel.Error || |
|
||||||
diag.Level == ParserDiagnosticLevel.Fatal) |
|
||||||
hasErrors = true; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return !hasErrors; |
|
||||||
} |
|
||||||
|
|
||||||
public ParserResult ParseLibrary(string file) |
|
||||||
{ |
|
||||||
var parserOptions = new ParserOptions |
|
||||||
{ |
|
||||||
Library = Library, |
|
||||||
FileName = file, |
|
||||||
Verbose = false, |
|
||||||
LibraryDirs = options.LibraryDirs, |
|
||||||
ToolSetToUse = options.ToolsetToUse |
|
||||||
}; |
|
||||||
|
|
||||||
var result = ClangParser.ParseLibrary(parserOptions); |
|
||||||
OnLibraryParsed(file, result); |
|
||||||
|
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
public Action<string, ParserResult> OnHeaderParsed = delegate {}; |
|
||||||
public Action<string, ParserResult> OnLibraryParsed = delegate { }; |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue