public static bool ParseSourceFile(string file) { // Lets setup the options for parsing the file. var parserOptions = new ParserOptions { LanguageVersion = LanguageVersion.CPP11, // Verbose here will make sure the parser outputs some extra debugging // information regarding include directories, which can be helpful when // tracking down parsing issues. Verbose = true }; // This will setup the necessary system include paths and arguments for parsing. // It will probe into the registry (on Windows) and filesystem to find the paths // of the system toolchains and necessary include directories. parserOptions.Setup(); // We create the Clang parser and parse the source code. var parser = new ClangParser(); var parserResult = parser.ParseSourceFile(file, parserOptions); // If there was some kind of error parsing, then lets print some diagnostics. if (parserResult.Kind != ParserResultKind.Success) { if (parserResult.Kind == ParserResultKind.FileNotFound) { Console.Error.WriteLine($"{file} was not found."); } for (uint i = 0; i < parserResult.DiagnosticsCount; i++) { var diag = parserResult.GetDiagnostics(i); Console.WriteLine("{0}({1},{2}): {3}: {4}", diag.FileName, diag.LineNumber, diag.ColumnNumber, diag.Level.ToString().ToLower(), diag.Message); } parserResult.Dispose(); return(false); } // Now we can consume the output of the parser (syntax tree). // First we will convert the output, bindings for the native Clang AST, // to CppSharp's managed AST representation. var astContext = ClangParser.ConvertASTContext(parserResult.ASTContext); // After its converted, we can dispose of the native AST bindings. parserResult.Dispose(); // Now we can finally do what we please with the syntax tree. foreach (var sourceUnit in astContext.TranslationUnits) { Console.WriteLine(sourceUnit.FileName); } return(true); }
public bool ParseCode() { var astContext = new Parser.AST.ASTContext(); var parser = new ClangParser(astContext); parser.SourcesParsed += OnSourceFileParsed; var sourceFiles = Options.Modules.SelectMany(m => m.Headers); if (Options.UnityBuild) { using (var parserOptions = ParserOptions.BuildForSourceFile( Options.Modules)) { using (var result = parser.ParseSourceFiles( sourceFiles, parserOptions)) Context.TargetInfo = result.TargetInfo; if (string.IsNullOrEmpty(ParserOptions.TargetTriple)) { ParserOptions.TargetTriple = parserOptions.TargetTriple; } } } else { foreach (var sourceFile in sourceFiles) { using (var parserOptions = ParserOptions.BuildForSourceFile( Options.Modules, sourceFile)) { using (ParserResult result = parser.ParseSourceFile( sourceFile, parserOptions)) if (Context.TargetInfo == null) { Context.TargetInfo = result.TargetInfo; } else if (result.TargetInfo != null) { result.TargetInfo.Dispose(); } if (string.IsNullOrEmpty(ParserOptions.TargetTriple)) { ParserOptions.TargetTriple = parserOptions.TargetTriple; } } } } Context.ASTContext = ClangParser.ConvertASTContext(astContext); return(!hasParsingErrors); }
public bool ParseCode() { var astContext = new Parser.AST.ASTContext(); var parser = new ClangParser(astContext); parser.SourcesParsed += OnSourceFileParsed; var sourceFiles = Options.Modules.SelectMany(m => m.Headers); if (Options.UnityBuild) { var parserOptions = BuildParserOptions(); var result = parser.ParseSourceFiles(sourceFiles, parserOptions); result.Dispose(); } else { var results = new List <ParserResult>(); foreach (var sourceFile in sourceFiles) { var parserOptions = BuildParserOptions(sourceFile); results.Add(parser.ParseSourceFile(sourceFile, parserOptions)); } foreach (var result in results) { result.Dispose(); } } Context.TargetInfo = parser.GetTargetInfo(ParserOptions); Context.ASTContext = ClangParser.ConvertASTContext(astContext); return(!hasParsingErrors); }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine(help); return; } for (int i = 0; i < args.Length; i++) { if (args[i] == "--include") { if (args.Length == i) { Console.WriteLine(help); return; } string includePath = args[i + 1]; DirectoryInfo d = new DirectoryInfo(includePath); includes.Add(d.FullName); includePath = Environment.CurrentDirectory + "\\" + includePath; i++; } if (args[i] == "--namespace") { if (args.Length == i) { Console.WriteLine(help); return; } nameSpace = args[i + 1]; i++; } if (args[i] == "--class") { if (args.Length == i) { Console.WriteLine(help); return; } className = args[i + 1]; i++; } if (args[i] == "--dll") { if (args.Length == i) { Console.WriteLine(help); return; } dllName = args[i + 1]; i++; } } sourceFile = args[args.Length - 1]; if (includes.Count == 0 || string.IsNullOrEmpty(nameSpace) || string.IsNullOrEmpty(className) || string.IsNullOrEmpty(dllName)) { Console.WriteLine(help); return; } string srcFile = Environment.CurrentDirectory + "\\" + sourceFile; if(!File.Exists(srcFile)) { Console.WriteLine("Can not find file {0}, working directory {1}", srcFile, Environment.CurrentDirectory); } ParserOptions options = new ParserOptions(); options.IncludeDirs.AddRange(includes); options.MicrosoftMode = true; options.ASTContext = new CppSharp.AST.ASTContext(); options.FileName = srcFile; options.Defines.Add("__STDC_CONSTANT_MACROS"); SourceFile source = new SourceFile(options.FileName); parser = new ClangParser(); //parser.ASTContext = new CppSharp.AST.ASTContext(); result = parser.ParseSourceFile(source, options); if (result.Kind != ParserResultKind.Success) { Console.WriteLine("Error: {0}", result.Kind.ToString()); foreach (var diag in result.Diagnostics) Console.WriteLine(diag.FileName + "(" + diag.LineNumber.ToString() + "):" + diag.Message); } GenerateInteropFiles(result); }