• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/tclsh
2
3# Copyright JS Foundation and other contributors, http://js.foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17foreach file_name [getSourceFileNames] {
18    set state "normal"
19    set lines {}
20    set cols {}
21    set struct_marks {}
22    set expect_struct_name false
23    set prev_tok ""
24    set def_start false
25    set expect_newline false
26    set check_newline true
27
28    foreach token [getTokens $file_name 1 0 -1 -1 {}] {
29        set tok_val [lindex $token 0]
30        set line_num [lindex $token 1]
31        set col_num [lindex $token 2]
32        set tok_type [lindex $token 3]
33
34        if {$state == "macro"} {
35            if {$col_num == 0} {
36                set state "normal"
37            } else {
38                continue
39            }
40        }
41
42        if {$tok_type in {space ccomment cppcomment newline}} {
43            continue
44        }
45
46        if {$tok_type == "pp_define"} {
47            set state "macro"
48            set prev_tok ""
49            set def_start false
50            continue
51        }
52
53        if {$expect_struct_name == true} {
54            if {$tok_type == "identifier" && $line_num != [lindex $prev_tok 1]} {
55                report $file_name $line_num "type name should be on the same line with the rightbrace"
56            }
57            set expect_struct_name false
58        }
59
60        # check that rightbrace and typename (in struct, union and enum definitons) are on the same line
61        if {$tok_type in {struct enum union}} {
62            set def_start true
63        } elseif {$tok_type == "semicolon"} {
64            set def_start false
65        } elseif {$tok_type == "leftbrace"} {
66            lappend cols $col_num
67            lappend lines $line_num
68            if {$def_start == true} {
69                lappend struct_marks 1
70                set def_start false
71            } elseif {[lindex $prev_tok 3] == "assign"} {
72                lappend struct_marks 2
73                set check_newline false
74            } else {
75                lappend struct_marks 0
76            }
77        } elseif {$tok_type == "rightbrace"} {
78            if {[llength $lines] > 0} {
79                if {[lindex $struct_marks end] == 1} {
80                    set expect_struct_name true
81                    set check_newline false
82                } elseif {[lindex $struct_marks end] == 2} {
83                    set check_newline false
84                }
85                set lines [lreplace $lines end end]
86                set cols [lreplace $cols end end]
87                set struct_marks [lreplace $struct_marks end end]
88            } else {
89                report $file_name $line_num "unmatched brace"
90            }
91        }
92
93        # check that braces are on separate lines
94        if {$check_newline == true} {
95            if {$expect_newline == true} {
96                if {$tok_type == "semicolon"} {
97                    # empty
98                } elseif {[lindex $prev_tok 1] == $line_num} {
99                    report $file_name $line_num "brace should be placed on a separate line"
100                } else {
101                    set expect_newline false
102                }
103            } elseif {$tok_type in {leftbrace rightbrace}} {
104                if {[lindex $prev_tok 1] == $line_num} {
105                    report $file_name $line_num "brace should be placed on a separate line"
106                }
107                set expect_newline true
108            }
109        } else {
110            set check_newline true
111        }
112
113        set prev_tok $token
114    }
115}
116