DllMapVerifier.cs 8.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Collections.Generic;

public static class DllMapVerifier
{
    private struct DllImportRef
    {
        public DllImportRef (string name, int line, int column)
        {
            Name = name;
            Line = line;
            Column = column;
        }

        public string Name;
        public int Line;
        public int Column;
    }

    private static Dictionary<string, List<DllImportRef>> dll_imports 
        = new Dictionary<string, List<DllImportRef>> ();
    private static List<string> ignore_dlls = new List<string> ();
    private static List<string> config_dlls = null;
    
    public static int Main (string [] args)
    {
        LoadConfigDlls (args[0]);
        foreach (string file in args) {
            LoadDllImports (file);
        }

        return VerifyDllImports (args[0]) ? 0 : 1;
    }

    private static bool VerifyDllImports (string configFile)
    {
        int total_unmapped_count = 0;

        foreach (KeyValuePair<string, List<DllImportRef>> dll_import in dll_imports) {
            int file_unmapped_count = 0;
            foreach (DllImportRef dll_import_ref in dll_import.Value) {
                if (config_dlls != null && config_dlls.Contains (dll_import_ref.Name)) {
                    continue;
                }

                if (file_unmapped_count++ == 0) {
                    Console.Error.WriteLine ("Unmapped DLLs in file: {0}", dll_import.Key);
                }

                Console.Error.WriteLine ("  + {0} : {1},{2}", dll_import_ref.Name, 
                    dll_import_ref.Line, dll_import_ref.Column);
            }

            total_unmapped_count += file_unmapped_count;
        }
59 60

        if (total_unmapped_count > 0) {
61
            Console.Error.WriteLine ();
62 63 64
            Console.Error.WriteLine ("  If any DllImport above is explicitly allowed to be unmapped,");
            Console.Error.WriteLine ("  add an 'willfully unmapped' comment to the inside of the attribute:");
            Console.Error.WriteLine ();
65
            Console.Error.WriteLine ("      [DllImport (\"libX11.so.6\") /* willfully unmapped */]");
66 67 68
            Console.Error.WriteLine ();
        }

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        if (total_unmapped_count > 0 && config_dlls == null) {
            Console.Error.WriteLine ("No config file for DLL mapping was found ({0})", configFile);
        }

        return total_unmapped_count == 0;
    }
    
    private static void LoadDllImports (string csFile)
    {
        if (csFile.StartsWith ("-i")) {
            ignore_dlls.Add (csFile.Substring (2));
            return;
        }

        if (Path.GetExtension (csFile) == ".cs" && File.Exists (csFile)) {
            List<DllImportRef> dll_import_refs = null;

            foreach (DllImportRef dll_import in ParseFileForDllImports (csFile)) {
                if (ignore_dlls.Contains (dll_import.Name)) {
                    continue;
                }
            
                if (dll_import_refs == null) {
                    dll_import_refs = new List<DllImportRef> ();
                }

                dll_import_refs.Add (dll_import);
            }

            if (dll_import_refs != null) {
                dll_imports.Add (csFile, dll_import_refs);
            }
        }
    }

    private static void LoadConfigDlls (string configFile)
    {
        try {
            XmlTextReader config = new XmlTextReader (configFile);
            config_dlls = new List<string> ();
            while (config.Read ()) {
                if (config.NodeType == XmlNodeType.Element && 
                    config.Name == "dllmap") {
                    string dll = config.GetAttribute ("dll");
                    if (!config_dlls.Contains (dll)) {
                        config_dlls.Add (dll);
                    }
                }
            }
        } catch {
        }
    }
    
#region DllImport parser

    private static StreamReader reader;
    private static int reader_line;
    private static int reader_col;
    
    private static IEnumerable<DllImportRef> ParseFileForDllImports (string file)
    {
        reader_line = 1;
        reader_col = 1;

        using (reader = new StreamReader (file)) {
            char c;
            bool in_paren = false;
            bool in_attr = false;
            bool in_dll_attr = false;
            bool in_string = false;
139
            bool in_comment = false;
140
            int dll_line = 1, dll_col = 1;
141 142
            string dll_string = null;
            string dll_comment = null;
143 144 145 146 147 148 149
            
            while ((c = (char)reader.Peek ()) != Char.MaxValue) {
                switch (c) {
                    case ' ':
                    case '\t': Read (); break;
                    case '[': 
                        in_attr = true;
150 151
                        dll_string = null;
                        dll_comment = null;
152 153 154 155 156
                        dll_line = reader_line;
                        dll_col = reader_col;
                        Read ();
                        break;
                    case '(': Read (); in_paren = true; break;
157
                    case ')': Read (); in_paren = false; break;
158 159
                    case '"':
                        Read ();
160
                        if (dll_string == null && in_dll_attr && in_paren && !in_string) {
161 162 163
                            in_string = true;
                        }
                        break;
164 165 166 167
                    case '/':
                        Read ();
                        if ((char)reader.Peek () == '*') {
                            Read ();
168
                            if (in_dll_attr && !in_comment) {
169 170 171 172
                                in_comment = true;
                            }
                        }
                        break;
173
                    case ']':
174 175 176
                        if (in_dll_attr && dll_string != null && dll_comment != "willfully unmapped") {
                            yield return new DllImportRef (dll_string, dll_line, dll_col);
                        }
177 178 179 180
                        in_attr = false;
                        in_dll_attr = false;
                        Read ();
                        break;
181 182 183 184
                    default:
                        if (!in_dll_attr && in_attr && ReadDllAttribute ()) {
                            in_dll_attr = true;
                        } else if (in_dll_attr && in_string) {
185
                            dll_string = ReadDllString ();
186
                            in_string = false;
187 188 189
                        } else if (in_dll_attr && in_comment) {
                            dll_comment = ReadDllComment ();
                            in_comment = false;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
                        } else {
                            Read ();
                        }
                        break;
                }
            }
        }
    }

    private static bool ReadDllAttribute () 
    {
        return
            Read () == 'D' && 
            Read () == 'l' &&
            Read () == 'l' &&
            Read () == 'I' &&
            Read () == 'm' &&
            Read () == 'p' &&
            Read () == 'o' &&
            Read () == 'r' &&
            Read () == 't';
    }

    private static string ReadDllString ()
    {
        StringBuilder builder = new StringBuilder (32);
        while (true) {
            char c = Read ();
            if (Char.IsLetterOrDigit (c) || c == '.' || c == '-' || c == '_') {
                builder.Append (c);
            } else {
                break;
            }
        }
        return builder.ToString ();
    }
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

    private static string ReadDllComment ()
    {
        StringBuilder builder = new StringBuilder ();
        char lc = Char.MaxValue;
        while (true) {
            char c = Read ();
            if (c == Char.MaxValue || (c == '/' && lc == '*')) {
                break;
            } else if (lc != Char.MaxValue) {
                builder.Append (lc);
            }
            lc = c;
        }
        return builder.ToString ().Trim ();
    }
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    
    private static char Read ()
    {
        char c = (char)reader.Read ();
        if (c == '\n') {
            reader_line++;
            reader_col = 1;
        } else {
            reader_col++;
        }
        return c;
    }
    
#endregion

}