Add new coding style rules and fix appeared issues.

JerryScript-DCO-1.0-Signed-off-by: Andrey Shitov a.shitov@samsung.com
This commit is contained in:
Andrey Shitov
2015-05-12 19:17:07 +03:00
parent ca12c16607
commit 9763a93df3
89 changed files with 1152 additions and 626 deletions
+10
View File
@@ -1,4 +1,14 @@
set rules {
jerry_always_curly
jerry_braces_on_separate_line
jerry_braces_same_line_or_column
jerry_funcname_space_parentheses
jerry_identifier_no_space_bracket
jerry_indentation
jerry_max_line_length
jerry_no_space_after_opening_parentheses
jerry_no_space_before_closing_parentheses
jerry_no_tabs
jerry_no_trailing_spaces
jerry_switch_case
}
@@ -0,0 +1,87 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
foreach file_name [getSourceFileNames] {
set state "control"
set do_marks {}
set prev_tok_type ""
set prev_ctrl ""
set expect_while false
set expect_open_brace false
set paren_count 0
foreach token [getTokens $file_name 1 0 -1 -1 {if do while else for leftparen rightparen semicolon leftbrace rightbrace}] {
set tok_val [lindex $token 0]
set line_num [lindex $token 1]
set col_num [lindex $token 2]
set tok_type [lindex $token 3]
if {$state == "expression"} {
# puts "expression $paren_count $tok_type ($line_num , $col_num)"
if {$tok_type == "leftparen"} {
incr paren_count
} elseif {$tok_type == "rightparen"} {
incr paren_count -1
if {$paren_count == 0} {
set state "control"
set expect_open_brace true
} elseif {$paren_count < 0 } {
report $file_name $line_num "unexpected right parentheses"
}
} elseif {$tok_type != "semicolon"} {
report $file_name $line_num "unexpected token: $tok_type"
}
} else {
if {$expect_open_brace == true} {
if {$tok_type == "if" && $prev_tok_type == "else"} {
# empty
} elseif {$tok_type != "leftbrace"} {
report $file_name [lindex $prev_ctrl 1] "brace after \'[lindex $prev_ctrl 3]\' required"
}
set expect_open_brace false
}
if {$tok_type == "while" && ($expect_while == true || [lindex $prev_ctrl 3] == "do")} {
set expect_while false
set prev_ctrl ""
} elseif {$tok_type in {if for while}} {
set state "expression"
set prev_ctrl $token
} elseif {$tok_type in {do else}} {
set expect_open_brace true
set prev_ctrl $token
} elseif {$tok_type == "leftbrace"} {
if {[lindex $prev_ctrl 3] == "do"} {
lappend do_marks 1
} else {
lappend do_marks 0
}
set prev_ctrl ""
} elseif {$tok_type == "rightbrace"} {
if {[llength $do_marks] > 0} {
if {[lindex $do_marks end] == 1} {
set expect_while true
}
set do_marks [lreplace $do_marks end end]
} else {
report $file_name $line_num "unmatched brace"
}
}
}
set prev_tok_type $tok_type
}
}
@@ -0,0 +1,115 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
foreach file_name [getSourceFileNames] {
set state "normal"
set lines {}
set cols {}
set struct_marks {}
set expect_struct_name false
set prev_tok ""
set def_start false
set expect_newline false
set check_newline true
foreach token [getTokens $file_name 1 0 -1 -1 {}] {
set tok_val [lindex $token 0]
set line_num [lindex $token 1]
set col_num [lindex $token 2]
set tok_type [lindex $token 3]
if {$state == "macro"} {
if {$col_num == 0} {
set state "normal"
} else {
continue
}
}
if {$tok_type in {space ccomment cppcomment newline}} {
continue
}
if {$tok_type == "pp_define"} {
set state "macro"
set prev_tok ""
set def_start false
continue
}
if {$expect_struct_name == true} {
if {$tok_type == "identifier" && $line_num != [lindex $prev_tok 1]} {
report $file_name $line_num "type name should be on the same line with the rightbrace"
}
set expect_struct_name false
}
# check that rightbrace and typename (in struct, union and enum definitons) are on the same line
if {$tok_type in {struct enum union}} {
set def_start true
} elseif {$tok_type == "semicolon"} {
set def_start false
} elseif {$tok_type == "leftbrace"} {
lappend cols $col_num
lappend lines $line_num
if {$def_start == true} {
lappend struct_marks 1
set def_start false
} elseif {[lindex $prev_tok 3] == "assign"} {
lappend struct_marks 2
set check_newline false
} else {
lappend struct_marks 0
}
} elseif {$tok_type == "rightbrace"} {
if {[llength $lines] > 0} {
if {[lindex $struct_marks end] == 1} {
set expect_struct_name true
set check_newline false
} elseif {[lindex $struct_marks end] == 2} {
set check_newline false
}
set lines [lreplace $lines end end]
set cols [lreplace $cols end end]
set struct_marks [lreplace $struct_marks end end]
} else {
report $file_name $line_num "unmatched brace"
}
}
# check that braces are on separate lines
if {$check_newline == true} {
if {$expect_newline == true} {
if {$tok_type == "semicolon"} {
# empty
} elseif {[lindex $prev_tok 1] == $line_num} {
report $file_name $line_num "brace should be placed on a separate line"
} else {
set expect_newline false
}
} elseif {$tok_type in {leftbrace rightbrace}} {
if {[lindex $prev_tok 1] == $line_num} {
report $file_name $line_num "brace should be placed on a separate line"
}
set expect_newline true
}
} else {
set check_newline true
}
set prev_tok $token
}
}
@@ -0,0 +1,61 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
foreach file_name [getSourceFileNames] {
set state "normal"
set lines {}
set cols {}
foreach token [getTokens $file_name 1 0 -1 -1 {}] {
set tok_val [lindex $token 0]
set line_num [lindex $token 1]
set col_num [lindex $token 2]
set tok_type [lindex $token 3]
if {$state == "macro"} {
if {$col_num == 0} {
set state "normal"
} else {
set prev_tok_line $line_num
continue
}
}
if {$tok_type in {space ccomment cppcomment newline}} {
continue
}
if {$tok_type == "pp_define"} {
set state "macro"
continue
}
if {$tok_type == "leftbrace"} {
lappend cols $col_num
lappend lines $line_num
} elseif {$tok_type == "rightbrace"} {
if {[llength $lines] > 0} {
if {[lindex $lines end] != $line_num && [lindex $cols end] != $col_num} {
report $file_name $line_num "matching braces should be on the same line or column"
}
set lines [lreplace $lines end end]
set cols [lreplace $cols end end]
} else {
report $file_name $line_num "unmatched brace"
}
}
}
}
@@ -0,0 +1,60 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
proc check_part_of_the_file {file line_num col_start col_end} {
if {$col_start == $col_end} {
return
}
set line [getLine $file $line_num]
if {[regexp {^\s*#[ ]*define} $line]} {
return
}
set line [string range $line $col_start $col_end]
if {[regexp {([[:alnum:]][\s]{2,}\()|([[:alnum:]]\()} $line]} {
report $file $line_num "there should be exactly one space before left parentheses"
}
}
foreach fileName [getSourceFileNames] {
set checkLine 1
set checkColStart 0
set seenOmitToken false
foreach token [getTokens $fileName 1 0 -1 -1 {}] {
set lineNumber [lindex $token 1]
set colNumber [lindex $token 2]
set tokenType [lindex $token 3]
if {$checkLine != $lineNumber} {
if {!$seenOmitToken} {
check_part_of_the_file $fileName $checkLine $checkColStart end
}
set checkColStart $colNumber
set checkLine $lineNumber
} elseif {$seenOmitToken} {
set checkColStart $colNumber
}
if {$tokenType in {ccomment cppcomment stringlit}} {
check_part_of_the_file $fileName $checkLine $checkColStart $colNumber
set seenOmitToken true
} else {
set seenOmitToken false
}
}
}
@@ -0,0 +1,56 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
proc check_part_of_the_file {file line_num col_start col_end} {
if {$col_start == $col_end} {
return
}
set line [getLine $file $line_num]
set line [string range $line $col_start $col_end]
if {[regexp {[[:alnum:]_][\s]+\[} $line]} {
report $file $line_num "there should be no spaces between identifier and left bracket"
}
}
foreach fileName [getSourceFileNames] {
set checkLine 1
set checkColStart 0
set seenOmitToken false
foreach token [getTokens $fileName 1 0 -1 -1 {}] {
set lineNumber [lindex $token 1]
set colNumber [lindex $token 2]
set tokenType [lindex $token 3]
if {$checkLine != $lineNumber} {
if {!$seenOmitToken} {
check_part_of_the_file $fileName $checkLine $checkColStart end
}
set checkColStart $colNumber
set checkLine $lineNumber
} elseif {$seenOmitToken} {
set checkColStart $colNumber
}
if {$tokenType in {ccomment cppcomment stringlit}} {
check_part_of_the_file $fileName $checkLine $checkColStart $colNumber
set seenOmitToken true
} else {
set seenOmitToken false
}
}
}
@@ -0,0 +1,27 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set maxLen 120
foreach f [getSourceFileNames] {
set lineNumber 1
foreach line [getAllLines $f] {
if {[string length $line] > $maxLen} {
report $f $lineNumber "line is longer than ${maxLen} characters"
}
incr lineNumber
}
}
@@ -0,0 +1,56 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
proc check_part_of_the_file {file line_num col_start col_end} {
if {$col_start == $col_end} {
return
}
set line [getLine $file $line_num]
set line [string range $line $col_start $col_end]
if {[regexp {\(+[[:blank:]]} $line]} {
report $file $line_num "there should be no blank characters after opening parentheses"
}
}
foreach fileName [getSourceFileNames] {
set checkLine 1
set checkColStart 0
set seenOmitToken false
foreach token [getTokens $fileName 1 0 -1 -1 {}] {
set lineNumber [lindex $token 1]
set colNumber [lindex $token 2]
set tokenType [lindex $token 3]
if {$checkLine != $lineNumber} {
if {!$seenOmitToken} {
check_part_of_the_file $fileName $checkLine $checkColStart end
}
set checkColStart $colNumber
set checkLine $lineNumber
} elseif {$seenOmitToken} {
set checkColStart $colNumber
}
if {$tokenType in {ccomment cppcomment stringlit}} {
check_part_of_the_file $fileName $checkLine $checkColStart $colNumber
set seenOmitToken true
} else {
set seenOmitToken false
}
}
}
@@ -0,0 +1,25 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
foreach f [getSourceFileNames] {
set lineNumber 1
foreach line [getAllLines $f] {
if {[regexp {[[:graph:]][[:blank:]]+\)} $line]} {
report $f $lineNumber "there should be no blank characters before closing parentheses"
}
incr lineNumber
}
}
@@ -0,0 +1,25 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
foreach f [getSourceFileNames] {
set lineNumber 1
foreach line [getAllLines $f] {
if {[regexp {\t} $line]} {
report $f $lineNumber "tabs are not allowed"
}
incr lineNumber
}
}
@@ -0,0 +1,25 @@
#!/usr/bin/tclsh
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
foreach f [getSourceFileNames] {
set lineNumber 1
foreach line [getAllLines $f] {
if {[regexp {[[:blank:]]$} $line]} {
report $f $lineNumber "trailing space is not allowed"
}
incr lineNumber
}
}
@@ -27,7 +27,7 @@ foreach fileName [getSourceFileNames] {
if {$is_in_comment == "yes"} {
set is_in_comment "no"
}
if {$type == "newline"} {
set is_in_pp_define "no"
} elseif {$type == "ccomment"} {
@@ -140,7 +140,7 @@ foreach fileName [getSourceFileNames] {
if {$next_token_type == "semicolon"} {
set state "break"
}
continue
continue
} elseif {$state == "wait-for-break"} {
if {$next_token_type == "case" || $next_token_type == "default"} {
report $fileName [lindex $next_token 1] "Missing break or FALLTHRU comment before case (state $state)"