private void ParseSkiaHeaders() { Log?.LogVerbose("Parsing skia headers..."); var options = new CppParserOptions(); foreach (var header in config.IncludeDirs) { var path = Path.Combine(SkiaRoot, header); options.IncludeFolders.Add(path); } var headers = new List <string>(); foreach (var header in config.Headers) { var path = Path.Combine(SkiaRoot, header.Key); options.IncludeFolders.Add(path); foreach (var filter in header.Value) { headers.AddRange(Directory.EnumerateFiles(path, filter)); } } compilation = CppParser.ParseFiles(headers, options); if (compilation == null || compilation.HasErrors) { Log?.LogError("Parsing headers failed."); throw new Exception("Parsing headers failed."); } }
protected CppModule ParseCpp(ConfigFile config, string[] additionalArguments = null) { var loaded = ConfigFile.Load(config, new string[0], Logger); var(filesWithIncludes, filesWithExtensionHeaders) = loaded.GetFilesWithIncludesAndExtensionHeaders(); var configsWithIncludes = new HashSet <ConfigFile>(); foreach (var cfg in loaded.ConfigFilesLoaded) { if (filesWithIncludes.Contains(cfg.Id)) { configsWithIncludes.Add(cfg); } } var cppHeaderGenerator = new CppHeaderGenerator(Logger, true, TestDirectory.FullName); var(updated, _) = cppHeaderGenerator.GenerateCppHeaders(loaded, configsWithIncludes, filesWithExtensionHeaders); var castXml = GetCastXml(loaded); var extensionGenerator = new CppExtensionHeaderGenerator(new MacroManager(castXml)); var skeleton = extensionGenerator.GenerateExtensionHeaders(loaded, TestDirectory.FullName, filesWithExtensionHeaders, updated); var parser = new CppParser(Logger, castXml) { OutputPath = TestDirectory.FullName }; parser.Initialize(loaded); return(parser.Run(skeleton)); }
static void Main(string[] args) { var headerFile = Path.Combine(AppContext.BaseDirectory, "Headers", "renderdoc_app.h"); var options = new CppParserOptions { ParseMacros = true, Defines = { "_WIN32", } }; var compilation = CppParser.ParseFile(headerFile, options); // Print diagnostic messages if (compilation.HasErrors) { foreach (var message in compilation.Diagnostics.Messages) { Debug.WriteLine(message); } } else { string outputPath = "..\\..\\..\\..\\WaveEngine.Bindings.RenderDoc\\Generated"; CsCodeGenerator.Instance.Generate(compilation, outputPath); } }
static void Run(Config config) { var compilation = CppParser.ParseFiles(config.GetFiles(), config.ToParserOptions()); VGenerator.Generate(config, compilation); // compilation.Dump(); }
static void Main(string[] args) { var consoleLogger = new ConsoleLogger(); var logger = new Logger(consoleLogger, consoleLogger); string executablePath = null; // if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // executablePath = Path.GetFullPath("../../../../../bin/CastXML/bin/castxml.exe"); var castXml = new CastXml(logger, new IncludeDirectoryResolver(logger), executablePath); var cppParser = new CppParser(logger, castXml); var config = new ContextConfig() { Id = Path.GetFullPath("test.c"), Process = new string[] { Path.GetFullPath("test.c"), // Path.GetFullPath("test.h"), }.ToList(), Include = new string[] { Path.GetFullPath("test.h"), }.ToList(), Macros = new Dictionary <string, string>() { { "ARRSIZE", "42 * 3" } } }; cppParser.Initialize(config); var module = cppParser.Run(new CppModel.CppModule()); foreach (var inc in module.Includes) { Console.WriteLine(inc.Name); foreach (var struct_ in inc.Structs) { Console.WriteLine(" " + struct_.Name + " " + struct_.Size); foreach (var field in struct_.Fields) { Console.WriteLine(" " + field.Name + " " + field.Offset); } } } Console.WriteLine(JsonConvert.SerializeObject(module, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling = TypeNameHandling.All })); foreach (var f in typeof(CastXml).Assembly.GetManifestResourceNames()) { Console.WriteLine(f); } Console.WriteLine(module); Environment.Exit(module is null ? 1 : 0); }
private CppModule ParseCpp(CastXml castXml, CppModule group) { // Run the parser var parser = new CppParser(Logger, castXml) { OutputPath = IntermediateOutputPath }; parser.Initialize(Config); if (Logger.HasErrors) { Logger.Fatal("Initializing parser failed"); } // Run the parser group = parser.Run(group); if (Logger.HasErrors) { Logger.Fatal("Parsing C++ failed."); } else { Logger.Message("Parsing C++ finished"); } // Print statistics PrintStatistics(parser); return(group); }
/// <summary> /// Run CodeGenerator /// </summary> public void Run() { log.Info("Starting code generation..."); // Initialize the parser CppParser parser = new CppParser(CastXmlExecutablePath, VcToolsPath); parser.ForceParsing = configHasChanged || forceParsing; parser.Initialize(config); // Run the parser CppModule mainModule = parser.Run(); // Run the code generation process NodeJSGenerator generator = new NodeJSGenerator(config, new DriverOptions(), mainModule); // Setup rules and process code generator.SetupPasses(); generator.ProcessCode(); // Generate wrapper code List <GeneratorOutput> outputs = generator.GenerateCode(); SaveCode(outputs); }
protected CppModule ParseCpp(ConfigFile config) { var loaded = ConfigFile.Load(config, new string[0], Logger); loaded.GetFilesWithIncludesAndExtensionHeaders(out var configsWithIncludes, out var configsWithExtensionHeaders); var cppHeaderGenerator = new CppHeaderGenerator(Logger, TestDirectory.FullName); var updated = cppHeaderGenerator .GenerateCppHeaders(loaded, configsWithIncludes, configsWithExtensionHeaders) .UpdatedConfigs; var castXml = GetCastXml(loaded); var macro = new MacroManager(castXml); var extensionGenerator = new CppExtensionHeaderGenerator(); var skeleton = loaded.CreateSkeletonModule(); macro.Parse(Path.Combine(TestDirectory.FullName, loaded.HeaderFileName), skeleton); extensionGenerator.GenerateExtensionHeaders( loaded, TestDirectory.FullName, skeleton, configsWithExtensionHeaders, updated ); var parser = new CppParser(Logger, loaded) { OutputPath = TestDirectory.FullName }; using var xmlReader = castXml.Process(parser.RootConfigHeaderFileName); return(parser.Run(skeleton, xmlReader)); }
static void Main(string[] args) { var flavors = new string[] { "Dawn", "Browser" }; foreach (var flavor in flavors) { var headerFile = Path.Combine(AppContext.BaseDirectory, "Headers", flavor, "webgpu.h"); var options = new CppParserOptions { ParseMacros = true, Defines = { "WGPU_SHARED_LIBRARY", "_WIN32", "WGPU_SKIP_PROCS" } }; var compilation = CppParser.ParseFile(headerFile, options); // Print diagnostic messages if (compilation.HasErrors) { foreach (var message in compilation.Diagnostics.Messages) { Debug.WriteLine(message); } } else { string basePath = "..\\..\\..\\..\\WaveEngine.Bindings.WebGPU"; CsCodeGenerator.Instance.Generate(compilation, basePath, flavor); } } }
public void ParseAssert(string text, Action <CppCompilation> assertCompilation, CppParserOptions options = null, bool asFile = true) { if (assertCompilation == null) { throw new ArgumentNullException(nameof(assertCompilation)); } options = options ?? new CppParserOptions(); var currentDirectory = Environment.CurrentDirectory; var headerFilename = $"{TestContext.CurrentContext.Test.FullName}-{TestContext.CurrentContext.Test.ID}.h"; var headerFile = Path.Combine(currentDirectory, headerFilename); // Parse in memory var compilation = CppParser.Parse(text, options, headerFilename); foreach (var diagnosticsMessage in compilation.Diagnostics.Messages) { Console.WriteLine(diagnosticsMessage); } assertCompilation(compilation); if (asFile) { // Parse single file from disk File.WriteAllText(headerFile, text); compilation = CppParser.ParseFile(headerFile, options); assertCompilation(compilation); } }
protected CppModule ParseCpp(ConfigFile config, string[] additionalArguments = null) { var loaded = ConfigFile.Load(config, new string[0], Logger); loaded.GetFilesWithIncludesAndExtensionHeaders(out var configsWithIncludes, out var filesWithExtensionHeaders); var cppHeaderGenerator = new CppHeaderGenerator(Logger, TestDirectory.FullName); var updated = cppHeaderGenerator.GenerateCppHeaders(loaded, configsWithIncludes, filesWithExtensionHeaders) .UpdatedConfigs; var castXml = GetCastXml(loaded, additionalArguments); var extensionGenerator = new CppExtensionHeaderGenerator(new MacroManager(castXml)); var skeleton = extensionGenerator.GenerateExtensionHeaders(loaded, TestDirectory.FullName, filesWithExtensionHeaders, updated); var parser = new CppParser(Logger, loaded) { OutputPath = TestDirectory.FullName }; using var xmlReader = castXml.Process(parser.RootConfigHeaderFileName); return(parser.Run(skeleton, xmlReader)); }
/// <summary> /// Remove includes, comments and strings from ioContents. /// </summary> /// <param name="ioContents"></param> private void StripContents(ref string ioContents) { CppParser parser = new CppParser(); //TODO: do the removes in one pass parser.RemoveIncludes(ref ioContents); parser.RemoveComments(ref ioContents); parser.RemoveStrings(ref ioContents); }
private bool CheckRemoveStrings(string inInput, string inExpectedOutput) { string contents = inInput; CppParser parser = new CppParser(); parser.RemoveStrings(ref contents); Assert.AreEqual(inExpectedOutput, contents); return(inExpectedOutput == contents); }
private static CppCompilation Compile(GenerateConfig config, string directory) { Environment.CurrentDirectory = directory; Regex[] sourcePatterns = config.CompileConfig.Sources .Select(StringExtension.MakeWildcardPattern) .ToArray(); Regex[] sourceExcludePatterns = config.CompileConfig.SourceExcludes .Select(StringExtension.MakeWildcardPattern) .ToArray(); List <string> sources = Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories) .Select(f => Path.GetRelativePath(directory, f).Replace("\\", "/")) .Where(f => { if (!sourcePatterns.Any(p => p.IsMatch(f))) { return(false); } if (sourceExcludePatterns.Any(p => p.IsMatch(f))) { return(false); } return(true); }) .ToList(); var parserOptions = new CppParserOptions { ParseAsCpp = true, ParseMacros = true, ParseAttributes = true, ParseSystemIncludes = true, AdditionalArguments = { "--target=x86_64-apple-darwin19.5.0", "-Wall", "-Wno-unused-function", "-Wno-unused-value", "-Wno-unused-variable", "-fno-strict-aliasing", "-fwrapv", "-fPIC", "-Wno-nullability-completeness" } }; parserOptions.IncludeFolders.AddRange(GetGnuIncludes()); parserOptions.IncludeFolders.Add("./"); parserOptions.IncludeFolders.AddRange(config.CompileConfig.IncludeFolders); return(CppParser.ParseFiles(sources, parserOptions)); }
private static CppCompilation Parse(string vlcDir) { var parserOptions = new CppParserOptions { ParseComments = NoComment }; parserOptions.IncludeFolders.Add(Path.Combine(vlcDir, include)); return(CppParser.ParseFile(BuildPath(vlcDir, vlch), parserOptions)); }
/// <summary> /// Prints the statistics. /// </summary> private void PrintStatistics(CppParser parser) { var keys = parser.IncludeMacroCounts.Keys.ToList(); keys.Sort(StringComparer.CurrentCultureIgnoreCase); Logger.Message("Macro Statistics"); foreach (var key in keys) { Logger.Message("\t{0}\t{1}", key, parser.IncludeMacroCounts[key]); } Logger.Message("\n"); }
public static CSharpCompilation Convert(List <string> files, CSharpConverterOptions options) { if (files == null) { throw new ArgumentNullException(nameof(files)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } var converter = new CSharpConverter(options); return(converter.Run(parserOptions => CppParser.ParseFiles(files, parserOptions))); }
public static CSharpCompilation Convert(string text, CSharpConverterOptions options) { if (text == null) { throw new ArgumentNullException(nameof(text)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } var converter = new CSharpConverter(options); return(converter.Run(parserOptions => CppParser.Parse(text, parserOptions))); }
public void ParseAsync() { WorkAsync((s, e) => { SetStatus(WrapperStatus.ParsingHeaders); try { GlobalNamespaceCpp = new CppParser().ParseRootFolder(RootFolder); } catch (Exception ex) { //WrapperEvent.Invoke(this, new WrapperProjectEventArgs(WrapperProjectEvent.LogMessage, ex.ToString())); } SetStatus(WrapperStatus.ParsingHeadersDone); }); }
private CppParser BuildParser(string sourceCode) { // Parse the file var inputStream = new AntlrInputStream(sourceCode); var lexer = new CppLexer(inputStream); var tokenStream = new CommonTokenStream(lexer); var parser = new CppParser(tokenStream); // Setup error handling lexer.RemoveErrorListeners(); lexer.AddErrorListener(new LexerExceptionErrorListener()); parser.RemoveErrorListeners(); parser.AddErrorListener(new ParserExceptionErrorListener()); return(parser); }
public void CodeModelParse() { string path = Assembly.GetExecutingAssembly().Location; path = Path.GetDirectoryName(path); path = Path.Combine(path, "CppTestProject"); var rootFolder = new RootFolderDefinition(path); var header = new HeaderDefinition("header1.h"); rootFolder.AddChild(header); NamespaceDefinition globalNamespace = new CppParser().ParseRootFolder(rootFolder); Assert.AreEqual(1, globalNamespace.Children.Count); }
public void TestRemoveIncludes() { CppParser parser = new CppParser(); string contents = @"// #include ""hello.h"" #include ""hello.h"" class hello { }; "; parser.RemoveIncludes(ref contents); string expected_contents = @"// #include ""hello.h"" class hello { }; "; Assert.AreEqual(expected_contents.Replace("\r", ""), contents.Replace("\r", "")); }
static CppCompilation Do(String file, ClConfig cfg) { var options = new CppParserOptions() { // Pass the defines -DMYDEFINE to the C++ parser Defines = { }, IncludeFolders = { }, TargetCpu = CppTargetCpu.X86_64, }; options.ParseSystemIncludes = false; options.ParseComments = false; options.AdditionalArguments.Add("-fno-complete-member-pointers"); options.AdditionalArguments.Add("/TC"); options.AdditionalArguments.Add("-std c99"); options.AdditionalArguments.Add("/c"); options.AdditionalArguments.Add("-fms-extensions"); options.AdditionalArguments.Add("-fms-compatibility"); options.AdditionalArguments.Add(" -fms-compatibility-version=19"); options.ParseAsCpp = false; options.EnableFunctionBodies(); foreach (KeyValuePair <string, string> kv in cfg.define) { String d = kv.Value == null ? (kv.Key) : (kv.Key + "=" + kv.Value); options.Defines.Add(d); } options.Defines.Add("CPP_AST_FIXED"); foreach (String include in cfg.include) { options.IncludeFolders.Add(include); } options.ConfigureForWindowsMsvc(); var compilation = CppParser.ParseFile(file, options); return(compilation); }
protected override bool Execute(ConfigFile config) { var resolver = new IncludeDirectoryResolver(SharpGenLogger); resolver.Configure(config); var castXml = new CastXmlRunner(SharpGenLogger, resolver, CastXmlExecutablePath, CastXmlArguments) { OutputPath = OutputPath }; // Run the parser var parser = new CppParser(SharpGenLogger, config) { OutputPath = OutputPath }; if (SharpGenLogger.HasErrors) { return(false); } var module = CppModule.Read(PartialCppModuleCache.ItemSpec); // Run the C++ parser CppModule group; using (var xmlReader = castXml.Process(parser.RootConfigHeaderFileName)) { group = parser.Run(module, xmlReader); } if (SharpGenLogger.HasErrors) { return(false); } group.Write(ParsedCppModule.ItemSpec); return(true); }
public void TestSimple() { // Parse a C++ files var compilation = CppParser.Parse(@" enum MyEnum { MyEnum_0, MyEnum_1 }; void function0(int a, int b); struct MyStruct { int field0; int field1;}; typedef MyStruct* MyStructPtr; " ); // Print diagnostic messages foreach (var message in compilation.Diagnostics.Messages) { Console.WriteLine(message); } // Print All enums foreach (var cppEnum in compilation.Enums) { Console.WriteLine(cppEnum); } // Print All functions foreach (var cppFunction in compilation.Functions) { Console.WriteLine(cppFunction); } // Print All classes, structs foreach (var cppClass in compilation.Classes) { Console.WriteLine(cppClass); } // Print All typedefs foreach (var cppTypedef in compilation.Typedefs) { Console.WriteLine(cppTypedef); } }
public void Process(string[] args) { var input_file = "test.cpp"; var input_text = File.ReadAllText(input_file, encoding: Encoding.UTF8); var options = new CppParserOptions(); //options.ParseAttributes = true; var compilation = CppParser.Parse(input_text, options); Context.Instance = new Context(); foreach (var cpp_class in compilation.Classes) { HandleClass(cpp_class); } }
protected override bool Execute(ConfigFile config) { var resolver = new IncludeDirectoryResolver(SharpGenLogger); resolver.Configure(config); var castXml = new CastXml(SharpGenLogger, resolver, CastXmlExecutablePath) { OutputPath = OutputPath }; // Run the parser var parser = new CppParser(SharpGenLogger, castXml) { OutputPath = OutputPath }; parser.Initialize(config); if (SharpGenLogger.HasErrors) { return(false); } var module = CppModule.Read(PartialCppModuleCache.ItemSpec); // Run the parser var group = parser.Run(module, CastXmlArguments ?? Array.Empty <string>()); if (SharpGenLogger.HasErrors) { return(false); } group.Write(ParsedCppModule.ItemSpec); return(true); }
public static TranslationUnit GenerateAST(AntlrInputStream inputStream) { // Parse the file var lexer = new CppLexer(inputStream); var tokenStream = new CommonTokenStream(lexer); var parser = new CppParser(tokenStream); // var tokens = lexer.GetAllTokens().Select(token => lexer.Vocabulary.GetSymbolicName(token.Type)).ToList(); // Setup error handling lexer.RemoveErrorListeners(); lexer.AddErrorListener(new LexerExceptionErrorListener()); parser.RemoveErrorListeners(); parser.AddErrorListener(new ParserExceptionErrorListener()); // Parse the concrete syntax tree var translationUnit = parser.translationUnit(); // Convert the the abstract syntax tree var visitor = new ASTVisitor(); var ast = (TranslationUnit)visitor.Visit(translationUnit); return(ast); }
static void Main(string[] args) { var headerFile = Path.Combine(AppContext.BaseDirectory, "ImGUI", "cimgui.h"); var options = new CppParserOptions { ParseMacros = true, Defines = { "CIMGUI_DEFINE_ENUMS_AND_STRUCTS" } }; var compilation = CppParser.ParseFile(headerFile, options); if (compilation.HasErrors) { foreach (var message in compilation.Diagnostics.Messages) { Console.WriteLine(message); } } CsCodeGenerator.Generate(compilation, "../../../../src/SharpImGUI/Generated/"); }
public static void Main(string[] args) { // In this sample we are going to bind a few C++ classes from https://github.com/lemire/simdjson // the library has "singleheader" file so we don't have to collect all needed files + includes // see https://github.com/lemire/simdjson/tree/master/singleheader string outputFolder = "../../../Output/"; if (!Directory.Exists(outputFolder)) { Directory.CreateDirectory(outputFolder); } Console.WriteLine("Downloading simdjson sources..."); HttpClient httpClient = new HttpClient(); string simdJsonSingleHeader = httpClient.GetStringAsync("https://raw.githubusercontent.com/lemire/simdjson/master/singleheader/simdjson.h").Result; File.WriteAllText(Path.Combine(outputFolder, @"simdjson.h"), simdJsonSingleHeader); Console.WriteLine("Downloaded! Parsing..."); var options = new CppParserOptions(); // TODO: test on macOS options.ConfigureForWindowsMsvc(CppTargetCpu.X86_64); options.AdditionalArguments.Add("-std=c++17"); CppCompilation compilation = CppParser.ParseFile(Path.Combine(outputFolder, @"simdjson.h"), options); if (compilation.DumpErrorsIfAny()) { Console.ReadKey(); return; } var mapper = new TypeMapper(compilation); // Register native types we don't want to bind (or any method with them in parameters) mapper.RegisterUnsupportedTypes( "simdjson", // it's empty - we don't need it "basic_string", // TODO: "basic_string_view", // TODO "basic_ostream"); // TODO: var templateManager = new TemplateManager(); // Add additional stuff we want to see in the bindings.c templateManager .AddToCHeader(@"#include ""simdjson.h""") .SetGlobalFunctionsClassName("GlobalFunctions"); PinvokeGenerator.Generate(mapper, templateManager, @namespace: "MyNamespace", dllImportPath: @"""simdjson.dll""", outCFile: Path.Combine(outputFolder, "Bindings.c"), outCsFile: Path.Combine(outputFolder, "Bindings.cs")); Console.WriteLine("Done. See Output folder."); }
protected CompilationUnit ParseStub (string continuation, bool appendSemicolon = true, string afterContinuation = null) { var mt = GetMemberTextToCaret (); if (mt == null) return null; string memberText = mt.Item1; bool wrapInClass = mt.Item2; var wrapper = new StringBuilder (); if (wrapInClass) { /* foreach (var child in Unit.Children) { if (child is UsingDeclaration) { var offset = document.GetOffset (child.StartLocation); wrapper.Append (document.GetText (offset, document.GetOffset (child.EndLocation) - offset)); } }*/ wrapper.Append ("class Stub {"); wrapper.AppendLine (); } wrapper.Append (memberText); wrapper.Append (continuation); AppendMissingClosingBrackets (wrapper, memberText, appendSemicolon); wrapper.Append (afterContinuation); if (wrapInClass) wrapper.Append ('}'); TextLocation memberLocation; if (currentMember != null && currentType != null && currentType.Kind != TypeKind.Enum) { memberLocation = currentMember.Region.Begin; } else if (currentType != null) { memberLocation = currentType.Region.Begin; } else { memberLocation = new TextLocation (1, 1); } using (var stream = new System.IO.StringReader (wrapper.ToString ())) { try { var parser = new CppParser (); return parser.Parse (stream, "stub.cs", wrapInClass ? memberLocation.Line - 2 : 0); } catch (Exception) { Console.WriteLine ("------"); Console.WriteLine (wrapper); throw; } } }
Tuple<CppParsedFile, AstNode, CompilationUnit> GetNewExpressionAt (int offset) { var parser = new CppParser (); string text = this.document.GetText (0, this.offset); var sb = new StringBuilder (text); sb.Append ("a ();"); AppendMissingClosingBrackets (sb, text, false); var stream = new System.IO.StringReader (sb.ToString ()); var completionUnit = parser.Parse (stream, CSharpParsedFile.FileName, 0); stream.Close (); var loc = document.GetLocation (offset); var expr = completionUnit.GetNodeAt (loc, n => n is Expression); if (expr == null) { // try without ";" sb = new StringBuilder (text); sb.Append ("a ()"); AppendMissingClosingBrackets (sb, text, false); stream = new System.IO.StringReader (sb.ToString ()); completionUnit = parser.Parse (stream, CSharpParsedFile.FileName, 0); stream.Close (); loc = document.GetLocation (offset); expr = completionUnit.GetNodeAt (loc, n => n is Expression); if (expr == null) return null; } var tsvisitor = new TypeSystemConvertVisitor (CSharpParsedFile.FileName); completionUnit.AcceptVisitor (tsvisitor, null); return Tuple.Create (tsvisitor.ParsedFile, expr, completionUnit); }