Skip to content

Commit 2489514

Browse files
authored
Merge pull request #342 from peterschrammel/cpplint-fix
Cpplint fixes, regression tests for cpplint, and a pre-commit hook.
2 parents d90a14a + d590db4 commit 2489514

File tree

56 files changed

+1330
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1330
-95
lines changed

.githooks/pre-commit

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
# Runs scripts/cpplint.py on the modified files
3+
# Based on https://github.com/s0enke/git-hooks/
4+
#
5+
# @author Peter Schrammel <[email protected]>
6+
7+
gitRoot="$(dirname $0)/../.."
8+
9+
# this is the magic:
10+
# retrieve all files in staging area that are added, modified or renamed
11+
# but no deletions etc
12+
files=$(git diff-index --name-only --cached --diff-filter=ACMR HEAD -- )
13+
14+
if [ "$files" == "" ]; then
15+
exit 0
16+
fi
17+
18+
# create temporary copy of staging area and delete upon exit
19+
cleanup()
20+
{
21+
rm -rf $tmpStaging
22+
}
23+
24+
trap cleanup EXIT
25+
26+
tmpStaging=$(mktemp -d)
27+
28+
# Copy contents of staged version of files to temporary staging area
29+
# because we only want the staged version that will be commited and not
30+
# the version in the working directory
31+
stagedFiles=""
32+
for file in $files
33+
do
34+
id=$(git diff-index --cached HEAD $file | cut -d " " -f4)
35+
36+
# create staged version of file in temporary staging area with the same
37+
# path as the original file
38+
mkdir -p "$tmpStaging/$(dirname $file)"
39+
git cat-file blob $id > "$tmpStaging/$file"
40+
stagedFiles="$stagedFiles $tmpStaging/$file"
41+
done
42+
43+
output=$(cd $gitRoot; python scripts/cpplint.py $stagedFiles 2>&1)
44+
retval=$?
45+
46+
if [ $retval -ne 0 ]
47+
then
48+
echo "$output"
49+
exit 1
50+
fi

CODING_STANDARD

+44-7
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ Here a few minimalistic coding rules for the CPROVER source tree.
33
Whitespaces:
44
- Use 2 spaces indent, no tabs.
55
- No lines wider than 80 chars.
6+
- When line is wider, do the following:
7+
- Subsequent lines should be indented two more than the initial line
8+
- Break after = if it is part of an assignment
9+
- For chained calls, prefer immediately before .
10+
- For other operators (e.g. &&, +) prefer immediately after the operator
11+
- For brackets, break after the bracket
12+
- In the case of function calls, put each argument on a separate line if
13+
they do not fit after one line break
614
- If a method is bigger than 50 lines, break it into parts.
715
- Put matching { } into the same column.
8-
- No spaces around operators, except &&, ||, and <<
9-
- Spaces after , and ; inside 'for'
16+
- No spaces around operators (=, +, ==, ...)
17+
Exceptions: Spaces around &&, || and <<
18+
- Space after comma (parameter lists, argument lists, ...)
19+
- Space after colon inside 'for'
20+
- No whitespaces at end of line
21+
- No whitespaces in blank lines
1022
- Put argument lists on next line (and ident 2 spaces) if too long
1123
- Put parameters on separate lines (and ident 2 spaces) if too long
1224
- No whitespaces around colon for inheritance,
@@ -16,8 +28,6 @@ Whitespaces:
1628
- if(...), else, for(...), do, and while(...) are always in a separate line
1729
- Break expressions in if, for, while if necessary and align them
1830
with the first character following the opening parenthesis
19-
- No whitespaces at end of line
20-
- Avoid whitespaces in blank lines
2131
- Use {} instead of ; for the empty statement
2232
- Single line blocks without { } are allowed,
2333
but put braces around multi-line blocks
@@ -29,9 +39,31 @@ Comments:
2939
- Do not use /* */ except for file and function comment blocks
3040
- Each source and header file must start with a comment block
3141
stating the Module name and Author [will be changed when we roll out doxygen]
32-
- Each function in the source file (not the header) is preceded
33-
by a comment block stating Name, Inputs, Outputs and Purpose [will be changed
34-
when we roll out doxygen]
42+
- Each function in the source file (not the header) is preceded
43+
by a function comment header consisting of a comment block stating
44+
Name, Inputs, Outputs and Purpose [will be changed when we roll
45+
out doxygen]
46+
- It should look like this:
47+
```
48+
/*******************************************************************\
49+
50+
Function: class_namet::function_name
51+
52+
Inputs:
53+
arg_name - Description of its purpose
54+
long_arg_name - Descriptions should be indented
55+
to match the first line of the
56+
description
57+
58+
Outputs: A description of what the function returns
59+
60+
Purpose: A description of what the function does.
61+
Again, indentation with the line above
62+
63+
\*******************************************************************/
64+
```
65+
- An empty line should appear between the bottom of the function comment header
66+
and the function.
3567
- Put comments on a separate line
3668
- Use comments to explain the non-obvious
3769
- Use #if 0 for commenting out source code
@@ -51,6 +83,8 @@ Naming:
5183

5284
Header files:
5385
- Avoid unnecessary #includes, especially in header files
86+
- Prefer forward declaration to includes, but forward declare at the top
87+
of the header file rather than in line
5488
- Guard headers with #ifndef CPROVER_DIRECTORIES_FILE_H, etc
5589

5690
C++ features:
@@ -85,6 +119,9 @@ C++ features:
85119
- We allow to use 3rd-party libraries directly.
86120
No wrapper matching the coding rules is required.
87121
Allowed libraries are: STL.
122+
- Use the auto keyword if and only if one of the following
123+
- The type is explictly repeated on the RHS (e.g. a constructor call)
124+
- Adding the type will increase confusion (e.g. iterators, function pointers)
88125

89126
Architecture-specific code:
90127
- Avoid if possible.

regression/cpp-linter/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
default: tests.log
2+
3+
test:
4+
@if ! ../test.pl -c python ../../../scripts/cpplint.py ; then \
5+
../failed-tests-printer.pl ; \
6+
exit 1 ; \
7+
fi
8+
9+
tests.log: ../test.pl
10+
@if ! ../test.pl -c "python ../../../scripts/cpplint.py" ; then \
11+
../failed-tests-printer.pl ; \
12+
exit 1 ; \
13+
fi
14+
15+
show:
16+
@for dir in *; do \
17+
if [ -d "$$dir" ]; then \
18+
vim -o "$$dir/*.c" "$$dir/*.out"; \
19+
fi; \
20+
done;
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*******************************************************************\
2+
3+
Module: Lint Examples
4+
5+
Author: Thomas Kiley, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/*******************************************************************\
10+
11+
Function: fun
12+
13+
Inputs:
14+
15+
Outputs:
16+
17+
Purpose:
18+
19+
\*******************************************************************/
20+
21+
static void fun()
22+
{
23+
catch(int)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CORE
2+
main.cpp
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*******************************************************************\
2+
3+
Module: Lint Examples
4+
5+
Author: Thomas Kiley, [email protected]
6+
7+
\*******************************************************************/
8+
9+
class temp_classt : public base_classt
10+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
main.cpp
3+
4+
^main\.cpp:9: There shouldn.t be a space between class identifier and : \[readability/identifiers\] \[4\]$
5+
^Total errors found: 1$
6+
^SIGNAL=0$
7+
--
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*******************************************************************\
2+
3+
Module: Lint Examples
4+
5+
Author: Thomas Kiley, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/*******************************************************************\
10+
11+
Function: fun
12+
13+
Inputs:
14+
15+
Outputs:
16+
17+
Purpose:
18+
19+
\*******************************************************************/
20+
21+
static void fun()
22+
{
23+
// This should not produce a warning
24+
do
25+
{
26+
int x=0;
27+
}
28+
while(a);
29+
30+
// This should
31+
while(b);
32+
33+
// As should this
34+
if(true)
35+
{
36+
do_something();
37+
}
38+
while(c);
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.cpp
3+
4+
^main\.cpp:31: Empty loop bodies should use {} or continue \[whitespace/empty_loop_body\] \[5\]$
5+
^main\.cpp:38: Empty loop bodies should use {} or continue \[whitespace/empty_loop_body\] \[5\]$
6+
^Total errors found: 2$
7+
^SIGNAL=0$
8+
--
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*******************************************************************\
2+
3+
Module: Lint Examples
4+
5+
Author: Thomas Kiley, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/*******************************************************************\
10+
11+
Function: fun
12+
13+
Inputs:
14+
15+
Outputs:
16+
17+
Purpose:
18+
19+
\*******************************************************************/
20+
21+
static void fun()
22+
{
23+
do
24+
{
25+
int x=0;
26+
} while(a);
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
main.cpp
3+
4+
^main\.cpp:26: while statement of do...while loop should be on a separate line to the closing brace \[readability/braces\] \[4\]
5+
^Total errors found: 1$
6+
^SIGNAL=0$
7+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************\
2+
3+
Module: Lint Examples
4+
5+
Author: Thomas Kiley, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/*******************************************************************\
10+
11+
Function: fun
12+
13+
Inputs:
14+
15+
Outputs:
16+
17+
Purpose:
18+
19+
\*******************************************************************/
20+
21+
static void fun()
22+
{
23+
do_something();
24+
}
25+
26+
static void foo()
27+
{
28+
// No function header
29+
do_something();
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.cpp
3+
4+
^main\.cpp:26: Could not find function header comment for foo \[readability/function_comment\] \[4\]
5+
^Total errors found: 1$
6+
7+
^SIGNAL=0$
8+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*******************************************************************\
2+
3+
Module: Lint Examples
4+
5+
Author: Thomas Kiley, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/*******************************************************************\
10+
11+
Function: fun
12+
13+
\*******************************************************************/
14+
15+
static void fun()
16+
{
17+
do_something();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.cpp
3+
4+
main\.cpp:15: Function header for fun missing Inputs: \[readability/function_comment\] \[4\]
5+
main\.cpp:15: Function header for fun missing Outputs: \[readability/function_comment\] \[4\]
6+
main\.cpp:15: Function header for fun missing Purpose: \[readability/function_comment\] \[4\]
7+
^Total errors found: 3$
8+
^SIGNAL=0$
9+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*******************************************************************\
2+
3+
Module: Lint Examples
4+
5+
Author: Thomas Kiley, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/*******************************************************************\
10+
11+
Function: fun
12+
13+
Inputs:
14+
15+
Outputs:
16+
17+
Purpose:
18+
19+
\*******************************************************************/
20+
static void fun()
21+
{
22+
do_something();
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
main.cpp
3+
4+
^main\.cpp:20: Insert an empty line between function header comment and the function fun \[readability/function_comment\] \[4\]$
5+
^Total errors found: 1$
6+
^SIGNAL=0$
7+
--

0 commit comments

Comments
 (0)