Exemplo n.º 1
0
        public AST ParseWithClangArgs(CppConfig config)
        {
            if (config.Sources.Count == 0)
            {
                Console.WriteLine("No input sources or includes");
                return(null);
            }

            // the index object
            CXIndex Index = clang.createIndex(0, 0);

            // prepare some vars for parse
            uint option = clang.defaultEditingTranslationUnitOptions()
                          | (uint)CXTranslationUnit_Flags.CXTranslationUnit_SkipFunctionBodies;
            CXUnsavedFile unsavedFile = new CXUnsavedFile();

            CXTranslationUnit TU;
            var error = clang.parseTranslationUnit2(Index, config.Sources[0], config.Extras, config.Extras.Length, out unsavedFile, 0,
                                                    option, out TU);

            if (error != CXErrorCode.CXError_Success)
            {
                Console.WriteLine("Failed to parse Translation Unit!");
                return(null);
            }

            bool fatal          = false;
            var  numDiagnostics = clang.getNumDiagnostics(TU);

            for (uint loop = 0; loop < numDiagnostics; ++loop)
            {
                fatal |= DealingWithDiagnostic(clang.getDiagnostic(TU, loop));
            }
            if (fatal)
            {
                return(null);
            }

            ASTVisitor visitor = new ASTVisitor();
            AST        ast     = visitor.Visit(TU);

            clang.disposeIndex(Index);
            return(ast);
        }
Exemplo n.º 2
0
        public static CppConfig GetCppConfig(string configFile)
        {
            string fileContent = File.ReadAllText(configFile);

            var deserializer = new DeserializerBuilder()
                               .WithNamingConvention(new CamelCaseNamingConvention())
                               .Build();

            CppConfig cppConfig = deserializer.Deserialize <CppConfig>(fileContent);

            cppConfig.Sources = ToAbsolutPath(cppConfig.Sources);


            //test

            Dictionary <string, UserTest111> dict = new Dictionary <string, UserTest111>();

            dict.Add("1", new UserTest111 {
                Name = "123", Email = "*****@*****.**"
            });
            dict.Add("2", new UserTest111 {
                Name = "234", Email = "*****@*****.**"
            });
            dict.Add("3", new UserTest111 {
                Name = "345", Email = "*****@*****.**"
            });

            var    deserializer1 = new DeserializerBuilder().Build();
            var    serializer    = new SerializerBuilder().Build();
            string yaml          = serializer.Serialize(dict);

            yaml = @"1:
  Name: 123
  Email: [email protected]
2: 
3:
  Name: 345
  Email: [email protected]";

            Dictionary <string, UserTest111> ddd = deserializer1.Deserialize <Dictionary <string, UserTest111> >(yaml);


            return(cppConfig);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("usage: clangen.exe <config_file>");
                return;
            }

            if (args.Length > 1)
            {
                Console.WriteLine("Warnning: Arguments exceed the first one will be ignored!");
            }

            string filePath = args[0];

            if (!File.Exists(filePath))
            {
                string error = string.Format("Configure file: {0} not found!", filePath);
                Console.WriteLine(error);
                return;
            }

            string libclang_path = Environment.CurrentDirectory;
            int    index         = libclang_path.LastIndexOf('\\');

            libclang_path = libclang_path.Substring(0, index);
            Parser    p         = new Parser(libclang_path);
            CppConfig cppConfig = Configure.GetCppConfig(filePath);
            AST       ast       = p.ParseWithClangArgs(cppConfig);

            //NativeType type = ast.GetType(
            //    "std::map<int, double, std::less<int>, std::allocator<std::pair<const int, double>>");

            //ASTTraits.IsInstanceOf(type, "std::map");
            //NativeType keyType = ASTTraits.KeyType(type);
            //NativeType mappedType = ASTTraits.MappedType(type);
            //Debug.Assert(ASTTraits.IsSameType(keyType, mappedType));
        }