1425bb815Sopenharmony_ci#!/usr/bin/tclsh
2425bb815Sopenharmony_ci
3425bb815Sopenharmony_ci# Copyright JS Foundation and other contributors, http://js.foundation
4425bb815Sopenharmony_ci#
5425bb815Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6425bb815Sopenharmony_ci# you may not use this file except in compliance with the License.
7425bb815Sopenharmony_ci# You may obtain a copy of the License at
8425bb815Sopenharmony_ci#
9425bb815Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10425bb815Sopenharmony_ci#
11425bb815Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12425bb815Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS
13425bb815Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14425bb815Sopenharmony_ci# See the License for the specific language governing permissions and
15425bb815Sopenharmony_ci# limitations under the License.
16425bb815Sopenharmony_ci
17425bb815Sopenharmony_ci# Indentation
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ciforeach fileName [getSourceFileNames] {
20425bb815Sopenharmony_ci    set indent 0
21425bb815Sopenharmony_ci    set lastCheckedLineNumber -1
22425bb815Sopenharmony_ci    set is_in_comment "no"
23425bb815Sopenharmony_ci    set is_in_pp_define "no"
24425bb815Sopenharmony_ci    set is_in_class "no"
25425bb815Sopenharmony_ci    set is_in_template "no"
26425bb815Sopenharmony_ci    set parentheses_level 0
27425bb815Sopenharmony_ci    set template_brackets_level 0
28425bb815Sopenharmony_ci
29425bb815Sopenharmony_ci    foreach token [getTokens $fileName 1 0 -1 -1 {}] {
30425bb815Sopenharmony_ci        set type [lindex $token 3]
31425bb815Sopenharmony_ci        set lineNumber [lindex $token 1]
32425bb815Sopenharmony_ci
33425bb815Sopenharmony_ci        if {$is_in_comment == "yes"} {
34425bb815Sopenharmony_ci            set is_in_comment "no"
35425bb815Sopenharmony_ci        }
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci        if {$type == "newline"} {
38425bb815Sopenharmony_ci            set is_in_pp_define "no"
39425bb815Sopenharmony_ci        } elseif {$type == "class"} {
40425bb815Sopenharmony_ci            set is_in_class "yes"
41425bb815Sopenharmony_ci        } elseif {$type == "template"} {
42425bb815Sopenharmony_ci            set is_in_template "yes"
43425bb815Sopenharmony_ci        } elseif {$is_in_class == "yes" && $type == "semicolon" && $indent == 0} {
44425bb815Sopenharmony_ci            set is_in_class "no"
45425bb815Sopenharmony_ci        } elseif {$type == "ccomment"} {
46425bb815Sopenharmony_ci            set is_in_comment "yes"
47425bb815Sopenharmony_ci        } elseif {[string first "pp_" $type] == 0} {
48425bb815Sopenharmony_ci            if {$type == "pp_define"} {
49425bb815Sopenharmony_ci                set is_in_pp_define "yes"
50425bb815Sopenharmony_ci            }
51425bb815Sopenharmony_ci
52425bb815Sopenharmony_ci            set lastCheckedLineNumber $lineNumber
53425bb815Sopenharmony_ci        } elseif {$type == "space"} {
54425bb815Sopenharmony_ci        } elseif {$type != "eof"} {
55425bb815Sopenharmony_ci            if {$type == "rightbrace" && $indent > 0} {
56425bb815Sopenharmony_ci                incr indent -2
57425bb815Sopenharmony_ci            }
58425bb815Sopenharmony_ci
59425bb815Sopenharmony_ci            if {$is_in_pp_define == "no" && $is_in_comment == "no" && $parentheses_level == 0 &&
60425bb815Sopenharmony_ci                $is_in_template == "no"} {
61425bb815Sopenharmony_ci                set line [getLine $fileName $lineNumber]
62425bb815Sopenharmony_ci
63425bb815Sopenharmony_ci                if {$lineNumber != $lastCheckedLineNumber} {
64425bb815Sopenharmony_ci                    if {[regexp {^[[:blank:]]*} $line match]} {
65425bb815Sopenharmony_ci                        set real_indent [string length $match]
66425bb815Sopenharmony_ci                        if {$indent != $real_indent} {
67425bb815Sopenharmony_ci                            if {[regexp {^[[:blank:]]*(private:|public:|protected:)} $line]} {
68425bb815Sopenharmony_ci                                if {$indent != $real_indent + 2} {
69425bb815Sopenharmony_ci                                    set exp_indent [expr  {$indent - 2}]
70425bb815Sopenharmony_ci                                    report $fileName $lineNumber "Indentation: $real_indent -> $exp_indent. Line: '$line'"
71425bb815Sopenharmony_ci                                }
72425bb815Sopenharmony_ci                            } elseif {![regexp {^[[:alnum:]_]{1,}:$} $line] || $real_indent != 0} {
73425bb815Sopenharmony_ci                                report $fileName $lineNumber "Indentation: $real_indent -> $indent. Line: '$line'"
74425bb815Sopenharmony_ci                            }
75425bb815Sopenharmony_ci                        }
76425bb815Sopenharmony_ci                    }
77425bb815Sopenharmony_ci                }
78425bb815Sopenharmony_ci
79425bb815Sopenharmony_ci                if {$lineNumber == $lastCheckedLineNumber} {
80425bb815Sopenharmony_ci                    if {$type == "leftbrace"} {
81425bb815Sopenharmony_ci                        if {![regexp {^[[:blank:]]*\{[[:blank:]]*$} $line]
82425bb815Sopenharmony_ci                            && ![regexp {[^\{=]=[^\{=]\{.*\},?} $line]} {
83425bb815Sopenharmony_ci                            report $fileName $lineNumber "Left brace is not the only non-space character in the line: '$line'"
84425bb815Sopenharmony_ci                        }
85425bb815Sopenharmony_ci                    }
86425bb815Sopenharmony_ci                    if {$type == "rightbrace"} {
87425bb815Sopenharmony_ci                        if {![regexp {^.* = .*\{.*\}[,;]?$} $line]
88425bb815Sopenharmony_ci                            && ![regexp {[^\{=]=[^\{=]\{.*\}[,;]?} $line]} {
89425bb815Sopenharmony_ci                            report $fileName $lineNumber "Right brace is not first non-space character in the line: '$line'"
90425bb815Sopenharmony_ci                        }
91425bb815Sopenharmony_ci                    }
92425bb815Sopenharmony_ci                }
93425bb815Sopenharmony_ci                if {$type == "rightbrace"} {
94425bb815Sopenharmony_ci                    if {![regexp {^[[:blank:]]*\};?((( [a-z_\(][a-z0-9_\(\)]{0,}){1,})?;| /\*.*\*/| //.*)?$} $line]
95425bb815Sopenharmony_ci                        && ![regexp {[^\{=]=[^\{=]\{.*\}[,;]?} $line]} {
96425bb815Sopenharmony_ci                        report $fileName $lineNumber "Right brace is not the only non-space character in the line and \
97425bb815Sopenharmony_ci                            is not single right brace followed by \[a-z0-9_() \] string and single semicolon character: '$line'"
98425bb815Sopenharmony_ci                    }
99425bb815Sopenharmony_ci                }
100425bb815Sopenharmony_ci            }
101425bb815Sopenharmony_ci
102425bb815Sopenharmony_ci            if {$type == "leftbrace"} {
103425bb815Sopenharmony_ci                if {![regexp {^extern "C"} [getLine $fileName [expr {$lineNumber - 1}]]]} {
104425bb815Sopenharmony_ci                    incr indent 2
105425bb815Sopenharmony_ci                }
106425bb815Sopenharmony_ci            } elseif {$type == "leftparen"} {
107425bb815Sopenharmony_ci                incr parentheses_level 1
108425bb815Sopenharmony_ci            } elseif {$type == "rightparen"} {
109425bb815Sopenharmony_ci                incr parentheses_level -1
110425bb815Sopenharmony_ci            }
111425bb815Sopenharmony_ci
112425bb815Sopenharmony_ci            if {$is_in_template == "yes"} {
113425bb815Sopenharmony_ci                if {$type == "less"} {
114425bb815Sopenharmony_ci                    incr template_brackets_level
115425bb815Sopenharmony_ci                } elseif {$type == "greater"} {
116425bb815Sopenharmony_ci                    incr template_brackets_level -1
117425bb815Sopenharmony_ci                    if {$template_brackets_level == 0} {
118425bb815Sopenharmony_ci                        set is_in_template "no"
119425bb815Sopenharmony_ci                    }
120425bb815Sopenharmony_ci                }
121425bb815Sopenharmony_ci            }
122425bb815Sopenharmony_ci
123425bb815Sopenharmony_ci            set lastCheckedLineNumber $lineNumber
124425bb815Sopenharmony_ci        }
125425bb815Sopenharmony_ci    }
126425bb815Sopenharmony_ci}
127