xref: /aosp_15_r20/external/gmmlib/Source/GmmLib/Scripts/GmmCheckCodingStandard.wsf (revision 35ffd701415c9e32e53136d61a677a8d0a8fc4a5)
1//============================================================================
2// Copyright(c) 2017 Intel Corporation
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files(the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and / or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20// OTHER DEALINGS IN THE SOFTWARE.
21//============================================================================
22<job>
23<script>
24
25// Globals
26var fso = WScript.CreateObject("Scripting.FileSystemObject");
27var err_count = 0;
28var in_block_comment = false;
29
30// Main function
31function main() {
32    try {
33        var dir;
34
35        // Get the directory to parse from the command-line
36        {
37            dir = WScript.Arguments.Unnamed.Item(0);
38            if (!fso.folderExists(dir)) {
39                print("ERROR: Cannot find directory " + dir);
40                err_count++;
41                return;
42            }
43            dir = fso.getAbsolutePathName(dir);
44        }
45
46        print("Checking coding standard in " + dir);
47
48        // Loop thru each line of each source file and look for coding standard violations
49        {
50            foreach_line(dir, function(file, line, linenum) {
51                var is_comment_line = false;
52
53                if (line.match(/\/\*/)) {
54                    in_block_comment = true;
55                }
56                if (line.match(/\*\//)) {
57                    in_block_comment = false;
58                }
59                if (line.match(/^\s*\/\//) || in_block_comment) {
60                    is_comment_line = true;
61                }
62                if (line.match(/\t/)) {
63                    report_error(file, linenum, "Found tabs. Check your IDE settings and replace tabs with spaces.");
64                };
65            })
66        }
67    } catch (e) {
68        print("ERROR: Exception caught in main(). " + e.message);
69        err_count++;
70        return;
71    }
72}
73
74// Loops thru each file recursively and applies a function to
75// every line
76function foreach_line(dir, apply) {
77    try {
78        var folder;
79
80        // Parse all source files in this directory
81        {
82            folder = fso.getFolder(dir);
83            var files = new Enumerator(folder.Files);
84            for (; !files.atEnd(); files.moveNext()) {
85                var path = files.item().path;
86                var path_l = path.toLowerCase();
87                if (path_l.match(/(\.cpp|\.hpp|\.h)$/)) {
88                    parse_file(path, apply);
89                }
90            }
91        }
92
93        // Recurse through all subfolders
94        {
95            var subfolders = new Enumerator(folder.SubFolders);
96            for (; !subfolders.atEnd(); subfolders.moveNext()) {
97                foreach_line(subfolders.item().path, apply);
98            }
99        }
100    }
101    catch (e) {
102        print("ERROR: Failed to parse directory " + dir + ". " + e.message);
103        err_count++;
104        return;
105    }
106}
107
108// Parse every line in the file and applies the
109// given function.
110function parse_file(path, apply) {
111    try {
112        in_block_comment = false;
113        var f = fso.openTextFile(path, 1);
114        var i = 0;
115        while (!f.atEndOfStream) {
116            i++;
117            var line = f.readLine();
118            try {
119                apply(path, line, i);
120            } catch(e) {
121                report_error(path, i, "Failed to parse line. " + e.message);
122            }
123        }
124        f.close();
125    } catch (e) {
126        print("ERROR: Failed to open " + path + ". " + e.message);
127        err_count++;
128    }
129}
130
131// Prints a message
132function print(msg) {
133    WScript.echo(msg);
134}
135
136// Prints an error message in a VisualStudio-friendly format
137function report_error(file, linenum, msg) {
138    print(file + "(" + linenum + ") : error : " + msg);
139    err_count++;
140}
141
142main();
143print("\nFound " + err_count + " error(s)\n");
144WScript.quit(err_count);
145</script>
146</job>