• 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
17# Indentation
18
19foreach fileName [getSourceFileNames] {
20    set indent 0
21    set lastCheckedLineNumber -1
22    set is_in_comment "no"
23    set is_in_pp_define "no"
24    set is_in_class "no"
25    set is_in_template "no"
26    set parentheses_level 0
27    set template_brackets_level 0
28
29    foreach token [getTokens $fileName 1 0 -1 -1 {}] {
30        set type [lindex $token 3]
31        set lineNumber [lindex $token 1]
32
33        if {$is_in_comment == "yes"} {
34            set is_in_comment "no"
35        }
36
37        if {$type == "newline"} {
38            set is_in_pp_define "no"
39        } elseif {$type == "class"} {
40            set is_in_class "yes"
41        } elseif {$type == "template"} {
42            set is_in_template "yes"
43        } elseif {$is_in_class == "yes" && $type == "semicolon" && $indent == 0} {
44            set is_in_class "no"
45        } elseif {$type == "ccomment"} {
46            set is_in_comment "yes"
47        } elseif {[string first "pp_" $type] == 0} {
48            if {$type == "pp_define"} {
49                set is_in_pp_define "yes"
50            }
51
52            set lastCheckedLineNumber $lineNumber
53        } elseif {$type == "space"} {
54        } elseif {$type != "eof"} {
55            if {$type == "rightbrace" && $indent > 0} {
56                incr indent -2
57            }
58
59            if {$is_in_pp_define == "no" && $is_in_comment == "no" && $parentheses_level == 0 &&
60                $is_in_template == "no"} {
61                set line [getLine $fileName $lineNumber]
62
63                if {$lineNumber != $lastCheckedLineNumber} {
64                    if {[regexp {^[[:blank:]]*} $line match]} {
65                        set real_indent [string length $match]
66                        if {$indent != $real_indent} {
67                            if {[regexp {^[[:blank:]]*(private:|public:|protected:)} $line]} {
68                                if {$indent != $real_indent + 2} {
69                                    set exp_indent [expr  {$indent - 2}]
70                                    report $fileName $lineNumber "Indentation: $real_indent -> $exp_indent. Line: '$line'"
71                                }
72                            } elseif {![regexp {^[[:alnum:]_]{1,}:$} $line] || $real_indent != 0} {
73                                report $fileName $lineNumber "Indentation: $real_indent -> $indent. Line: '$line'"
74                            }
75                        }
76                    }
77                }
78
79                if {$lineNumber == $lastCheckedLineNumber} {
80                    if {$type == "leftbrace"} {
81                        if {![regexp {^[[:blank:]]*\{[[:blank:]]*$} $line]
82                            && ![regexp {[^\{=]=[^\{=]\{.*\},?} $line]} {
83                            report $fileName $lineNumber "Left brace is not the only non-space character in the line: '$line'"
84                        }
85                    }
86                    if {$type == "rightbrace"} {
87                        if {![regexp {^.* = .*\{.*\}[,;]?$} $line]
88                            && ![regexp {[^\{=]=[^\{=]\{.*\}[,;]?} $line]} {
89                            report $fileName $lineNumber "Right brace is not first non-space character in the line: '$line'"
90                        }
91                    }
92                }
93                if {$type == "rightbrace"} {
94                    if {![regexp {^[[:blank:]]*\};?((( [a-z_\(][a-z0-9_\(\)]{0,}){1,})?;| /\*.*\*/| //.*)?$} $line]
95                        && ![regexp {[^\{=]=[^\{=]\{.*\}[,;]?} $line]} {
96                        report $fileName $lineNumber "Right brace is not the only non-space character in the line and \
97                            is not single right brace followed by \[a-z0-9_() \] string and single semicolon character: '$line'"
98                    }
99                }
100            }
101
102            if {$type == "leftbrace"} {
103                if {![regexp {^extern "C"} [getLine $fileName [expr {$lineNumber - 1}]]]} {
104                    incr indent 2
105                }
106            } elseif {$type == "leftparen"} {
107                incr parentheses_level 1
108            } elseif {$type == "rightparen"} {
109                incr parentheses_level -1
110            }
111
112            if {$is_in_template == "yes"} {
113                if {$type == "less"} {
114                    incr template_brackets_level
115                } elseif {$type == "greater"} {
116                    incr template_brackets_level -1
117                    if {$template_brackets_level == 0} {
118                        set is_in_template "no"
119                    }
120                }
121            }
122
123            set lastCheckedLineNumber $lineNumber
124        }
125    }
126}
127