Skip to content

Commit 45b12f3

Browse files
authored
Merge pull request #52 from srcML/develop
Add guarding to stack accesses
2 parents 31e703a + b806e20 commit 45b12f3

24 files changed

Lines changed: 899 additions & 225 deletions

nameCollector.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
/**
1111
*
1212
* Takes srcML input (with --position) of a source code file(s)
13-
* Gets a vector<identifier> which contains name, category, and position
14-
* of all the user defined identifiers in the file
15-
*
1613
* Works for srcML single unit or archives.
1714
*
18-
*
1915
* Need to have installed: libxml2 and srcml
2016
* Need to build and install srcSAX:
2117
* In srcSAX folder:
22-
* cmake CMakeLists.txt
18+
* cmake CMakeLists.txt -B build
19+
* cd build
2320
* make
2421
* sudo make install
2522
*
@@ -50,8 +47,13 @@ int main(int argc, char * argv[]) {
5047
bool noHeader = false; //Do not print header in csv
5148
bool appendOutput = false;
5249

53-
CLI::App app{"nameCollector: Output all user defined identifier names in a srcML file (i.e., one or more source code files). "};
50+
std::string description = "nameCollector";
51+
description = description + "\n Produces a list of all user defined identifier names in a source code file(s). Works for C, C++, C#, Java, Python. ";
52+
description = description + "\n Input: a srcML file/archive - which is one or more source code files. srcML needs --position option.";
53+
description = description + "\n Output: For each name gives type, syntactic category, and location in file. CSV or text description output.";
54+
description = description + "\n Typical USAGE: ./nameCollector -i foo.cpp.xml -o foo.cpp.csv --csv \n";
5455

56+
CLI::App app{description};
5557
app.add_option("-i, --input", inputFile, "Name of srcML file of source code with --position option");
5658
app.add_option("-o, --output", outputFile, "Name of output file");
5759
app.add_option("-f, --format", outputFormat, "The output format (text by default): csv, text"); //To support other output options

nameCollectorHandler.hpp

Lines changed: 56 additions & 43 deletions
Large diffs are not rendered by default.

testsuite/CMakeLists.txt

100644100755
File mode changed.

testsuite/cli.sh

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,191 @@ else
503503
echo "Test set_pos passed"
504504
fi
505505

506+
507+
# Testing the CSV header and position attribute features
508+
#####################################################################
509+
cat <<EOF > archive_position.xml
510+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
511+
<unit xmlns="http://www.srcML.org/srcML/src" xmlns:pos="http://www.srcML.org/srcML/position" revision="1.0.0" pos:tabs="8">
512+
513+
<unit revision="1.0.0" language="C++" filename="a.cpp" pos:tabs="8" hash="7caa72dfd567a04e9e64b0502814bde478a317b5"><decl_stmt pos:start="1:1" pos:end="1:6"><decl pos:start="1:1" pos:end="1:5"><type pos:start="1:1" pos:end="1:3"><name pos:start="1:1" pos:end="1:3">int</name></type> <name pos:start="1:5" pos:end="1:5">x</name></decl>;</decl_stmt>
514+
</unit>
515+
516+
</unit>
517+
EOF
518+
519+
output=$(cat archive_position.xml | ../bin/nameCollector --csv 2>&1 )
520+
expected=$(cat <<EOF
521+
Name,Type,Category,File,Position,Language,Stereotype
522+
x,int,global,a.cpp,1:5,C++,
523+
EOF
524+
)
525+
if [[ "$output" != "$expected" ]]; then
526+
echo "Test archive_position passed failed!"
527+
echo "Expected: '$expected'"
528+
echo "Got: '$output'"
529+
exit 1
530+
else
531+
echo "Test archive_position passed"
532+
fi
533+
534+
#####################################################################
535+
cat <<EOF > archive_no_position.xml
536+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
537+
<unit xmlns="http://www.srcML.org/srcML/src" revision="1.0.0">
538+
539+
<unit revision="1.0.0" language="C++" filename="a.cpp" hash="7caa72dfd567a04e9e64b0502814bde478a317b5"><decl_stmt><decl><type><name>int</name></type> <name>x</name></decl>;</decl_stmt>
540+
</unit>
541+
542+
</unit>
543+
EOF
544+
545+
output=$(cat archive_no_position.xml | ../bin/nameCollector --csv 2>&1 )
546+
expected=$(cat <<EOF
547+
WARNING: srcml --position NOT used to generate input file.
548+
Name,Type,Category,File,Position,Language,Stereotype
549+
x,int,global,a.cpp,,C++,
550+
EOF
551+
)
552+
if [[ "$output" != "$expected" ]]; then
553+
echo "Test archive_no_position passed failed!"
554+
echo "Expected: '$expected'"
555+
echo "Got: '$output'"
556+
exit 1
557+
else
558+
echo "Test archive_no_position passed"
559+
fi
560+
561+
#####################################################################
562+
cat <<EOF > single_unit_position.xml
563+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
564+
<unit xmlns="http://www.srcML.org/srcML/src" xmlns:pos="http://www.srcML.org/srcML/position" revision="1.0.0" language="C++" filename="a.cpp" pos:tabs="8"><decl_stmt pos:start="1:1" pos:end="1:6"><decl pos:start="1:1" pos:end="1:5"><type pos:start="1:1" pos:end="1:3"><name pos:start="1:1" pos:end="1:3">int</name></type> <name pos:start="1:5" pos:end="1:5">x</name></decl>;</decl_stmt>
565+
</unit>
566+
EOF
567+
568+
output=$(cat single_unit_position.xml | ../bin/nameCollector --csv 2>&1 )
569+
expected=$(cat <<EOF
570+
Name,Type,Category,File,Position,Language,Stereotype
571+
x,int,global,a.cpp,1:5,C++,
572+
EOF
573+
)
574+
if [[ "$output" != "$expected" ]]; then
575+
echo "Test single_unit_position passed failed!"
576+
echo "Expected: '$expected'"
577+
echo "Got: '$output'"
578+
exit 1
579+
else
580+
echo "Test single_unit_position passed"
581+
fi
582+
583+
#####################################################################
584+
cat <<EOF > single_unit_no_position.xml
585+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
586+
<unit xmlns="http://www.srcML.org/srcML/src" revision="1.0.0" language="C++" filename="a.cpp"><decl_stmt><decl><type><name>int</name></type> <name>x</name></decl>;</decl_stmt>
587+
</unit>
588+
EOF
589+
590+
output=$(cat single_unit_no_position.xml | ../bin/nameCollector --csv 2>&1 )
591+
expected=$(cat <<EOF
592+
WARNING: srcml --position NOT used to generate input file.
593+
Name,Type,Category,File,Position,Language,Stereotype
594+
x,int,global,a.cpp,,C++,
595+
EOF
596+
)
597+
if [[ "$output" != "$expected" ]]; then
598+
echo "Test single_unit_no_position passed failed!"
599+
echo "Expected: '$expected'"
600+
echo "Got: '$output'"
601+
exit 1
602+
else
603+
echo "Test single_unit_no_position passed"
604+
fi
605+
606+
#####################################################################
607+
cat <<EOF > double_file_position.xml
608+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
609+
<unit xmlns="http://www.srcML.org/srcML/src" xmlns:pos="http://www.srcML.org/srcML/position" revision="1.0.0" pos:tabs="8">
610+
611+
<unit revision="1.0.0" language="C++" filename="a.cpp" pos:tabs="8" hash="7caa72dfd567a04e9e64b0502814bde478a317b5"><decl_stmt pos:start="1:1" pos:end="1:6"><decl pos:start="1:1" pos:end="1:5"><type pos:start="1:1" pos:end="1:3"><name pos:start="1:1" pos:end="1:3">int</name></type> <name pos:start="1:5" pos:end="1:5">x</name></decl>;</decl_stmt>
612+
</unit>
613+
614+
<unit revision="1.0.0" language="C++" filename="b.cpp" pos:tabs="8" hash="4d9f53177cc4ca85d01a8b93ffb6d9b4f9effe7f"><decl_stmt pos:start="1:1" pos:end="1:6"><decl pos:start="1:1" pos:end="1:5"><type pos:start="1:1" pos:end="1:3"><name pos:start="1:1" pos:end="1:3">int</name></type> <name pos:start="1:5" pos:end="1:5">y</name></decl>;</decl_stmt>
615+
</unit>
616+
617+
</unit>
618+
EOF
619+
620+
output=$(cat double_file_position.xml | ../bin/nameCollector --csv 2>&1 )
621+
expected=$(cat <<EOF
622+
Name,Type,Category,File,Position,Language,Stereotype
623+
x,int,global,a.cpp,1:5,C++,
624+
y,int,global,b.cpp,1:5,C++,
625+
EOF
626+
)
627+
if [[ "$output" != "$expected" ]]; then
628+
echo "Test double_file_position passed failed!"
629+
echo "Expected: '$expected'"
630+
echo "Got: '$output'"
631+
exit 1
632+
else
633+
echo "Test double_file_position passed"
634+
fi
635+
636+
#####################################################################
637+
cat <<EOF > double_file_no_position.xml
638+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
639+
<unit xmlns="http://www.srcML.org/srcML/src" revision="1.0.0">
640+
641+
<unit revision="1.0.0" language="C++" filename="a.cpp" hash="7caa72dfd567a04e9e64b0502814bde478a317b5"><decl_stmt><decl><type><name>int</name></type> <name>x</name></decl>;</decl_stmt>
642+
</unit>
643+
644+
<unit revision="1.0.0" language="C++" filename="b.cpp" hash="4d9f53177cc4ca85d01a8b93ffb6d9b4f9effe7f"><decl_stmt><decl><type><name>int</name></type> <name>y</name></decl>;</decl_stmt>
645+
</unit>
646+
647+
</unit>
648+
EOF
649+
650+
output=$(cat double_file_no_position.xml | ../bin/nameCollector --csv 2>&1 )
651+
expected=$(cat <<EOF
652+
WARNING: srcml --position NOT used to generate input file.
653+
Name,Type,Category,File,Position,Language,Stereotype
654+
x,int,global,a.cpp,,C++,
655+
y,int,global,b.cpp,,C++,
656+
EOF
657+
)
658+
if [[ "$output" != "$expected" ]]; then
659+
echo "Test double_file_no_position passed failed!"
660+
echo "Expected: '$expected'"
661+
echo "Got: '$output'"
662+
exit 1
663+
else
664+
echo "Test double_file_no_position passed"
665+
fi
666+
667+
#####################################################################
668+
cat <<EOF > archive_no_header.xml
669+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
670+
<unit xmlns="http://www.srcML.org/srcML/src" xmlns:pos="http://www.srcML.org/srcML/position" revision="1.0.0" pos:tabs="8">
671+
672+
<unit revision="1.0.0" language="C++" filename="a.cpp" pos:tabs="8" hash="7caa72dfd567a04e9e64b0502814bde478a317b5"><decl_stmt pos:start="1:1" pos:end="1:6"><decl pos:start="1:1" pos:end="1:5"><type pos:start="1:1" pos:end="1:3"><name pos:start="1:1" pos:end="1:3">int</name></type> <name pos:start="1:5" pos:end="1:5">x</name></decl>;</decl_stmt>
673+
</unit>
674+
675+
</unit>
676+
EOF
677+
678+
output=$(cat archive_no_header.xml | ../bin/nameCollector --csv -n 2>&1 )
679+
expected=$(cat <<EOF
680+
x,int,global,a.cpp,1:5,C++,
681+
EOF
682+
)
683+
if [[ "$output" != "$expected" ]]; then
684+
echo "Test archive_no_header passed failed!"
685+
echo "Expected: '$expected'"
686+
echo "Got: '$output'"
687+
exit 1
688+
else
689+
echo "Test archive_no_header passed"
690+
fi
691+
692+
506693
exit 0

testsuite/test_c_function.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ expected_functions=(
8585
for function in "${expected_functions[@]}"; do
8686
if ! echo "$output" | grep -Fq "$function"; then
8787
echo "Test test_c_function failed!"
88-
echo "Expected field: '$function' not found"
88+
echo "Expected function: '$function' not found"
8989
echo "Got:"
9090
echo "$output"
9191
exit 1
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
3+
cat <<EOF > test_c_function_parameter.c
4+
#include <stdio.h>
5+
// passing function pointers
6+
void function_with_function_parameter ( int (*f)(int, int) ) {
7+
int result;
8+
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
9+
result = (*f)(ctr, (ctr+1));
10+
printf("result: %d\n", result);
11+
}
12+
}
13+
14+
int add(int x, int y){
15+
return x+y;
16+
}
17+
18+
void pass_function_with_multiple_parameters(void (*fxnPointer)(float, int, double)) {};
19+
void function_with_multiple_parameters(int (*fxnPointer2)(int, char), char, float) {};
20+
void function_with_multiple_function_parameters(int (*fxnPointerA)(int), char (*fxnPointerB)(char), void (*fxnPointerC)(float)) {};
21+
int main(){
22+
function_with_function_parameter(add);
23+
return 0;
24+
}
25+
26+
EOF
27+
28+
input=$(srcml test_c_function_parameter.c --position)
29+
output=$(echo "$input" | ./nameCollector )
30+
expected="function_with_function_parameter is a void function in C file: test_c_function_parameter.c:3:6
31+
f is a function-parameter in C file: test_c_function_parameter.c:3:47
32+
result is a int local in C file: test_c_function_parameter.c:4:9
33+
ctr is a int local in C file: test_c_function_parameter.c:5:15
34+
add is a int function in C file: test_c_function_parameter.c:11:5
35+
x is a int parameter in C file: test_c_function_parameter.c:11:13
36+
y is a int parameter in C file: test_c_function_parameter.c:11:20
37+
pass_function_with_multiple_parameters is a void function in C file: test_c_function_parameter.c:15:6
38+
fxnPointer is a function-parameter in C file: test_c_function_parameter.c:15:52
39+
function_with_multiple_parameters is a void function in C file: test_c_function_parameter.c:16:6
40+
fxnPointer2 is a function-parameter in C file: test_c_function_parameter.c:16:46
41+
function_with_multiple_function_parameters is a void function in C file: test_c_function_parameter.c:17:6
42+
fxnPointerA is a function-parameter in C file: test_c_function_parameter.c:17:55
43+
fxnPointerB is a function-parameter in C file: test_c_function_parameter.c:17:81
44+
fxnPointerC is a function-parameter in C file: test_c_function_parameter.c:17:108
45+
main is a int function in C file: test_c_function_parameter.c:18:5"
46+
47+
48+
expected_function_parameters=(
49+
"f is a function-parameter in C file: test_c_function_parameter.c:3:47"
50+
"fxnPointer is a function-parameter in C file: test_c_function_parameter.c:15:52"
51+
"fxnPointer2 is a function-parameter in C file: test_c_function_parameter.c:16:46"
52+
"fxnPointerA is a function-parameter in C file: test_c_function_parameter.c:17:55"
53+
"fxnPointerB is a function-parameter in C file: test_c_function_parameter.c:17:81"
54+
"fxnPointerC is a function-parameter in C file: test_c_function_parameter.c:17:108"
55+
)
56+
57+
for function_parameter in "${expected_function_parameters[@]}"; do
58+
if ! echo "$output" | grep -Fq "$function_parameter"; then
59+
echo "Test test_c_function_parameter failed!"
60+
echo "Expected function_parameter: '$function_parameter' not found"
61+
echo "Got:"
62+
echo "$output"
63+
exit 1
64+
fi
65+
done
66+
echo "Test test_c_function_parameter passed!"
67+
68+
if [[ "$output" != "$expected" ]]; then
69+
echo "Test test_c_function_parameter output did not match expected!"
70+
echo "Expected: '$expected'"
71+
echo "Got: '$output'"
72+
exit 1
73+
fi
74+
# Repeat tests
75+
76+
exit 0

testsuite/test_c_label.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
cat <<EOF > test_c_label.c
4+
#include <stdio.h>
5+
6+
int main(){
7+
int x=5;
8+
if(x == 6){
9+
goto conditionA;
10+
} else if(x > 6){
11+
goto conditonB;
12+
} else{
13+
goto conditionC;
14+
}
15+
16+
conditionA:
17+
printf("conditonA");
18+
goto end;
19+
conditonB:
20+
printf("conditonB");
21+
goto end;
22+
conditionC:
23+
printf("conditonC");
24+
goto end;
25+
end:
26+
printf("\n");
27+
return 0;
28+
}
29+
EOF
30+
31+
input=$(srcml test_c_label.c --position)
32+
output=$(echo "$input" | ./nameCollector )
33+
expected="main is a int function in C file: test_c_label.c:3:5
34+
x is a int local in C file: test_c_label.c:4:9
35+
conditionA is a label in C file: test_c_label.c:13:5
36+
conditonB is a label in C file: test_c_label.c:16:5
37+
conditionC is a label in C file: test_c_label.c:19:5
38+
end is a label in C file: test_c_label.c:22:5"
39+
40+
expected_label=(
41+
"conditionA is a label in C file: test_c_label.c:13:5"
42+
"conditonB is a label in C file: test_c_label.c:16:5"
43+
"conditionC is a label in C file: test_c_label.c:19:5"
44+
"end is a label in C file: test_c_label.c:22:5"
45+
)
46+
47+
for label in "${expected_label[@]}"; do
48+
if ! echo "$output" | grep -Fq "$label"; then
49+
echo "Test test_c_label failed!"
50+
echo "Expected label: '$label' not found"
51+
echo "Got:"
52+
echo "$output"
53+
exit 1
54+
fi
55+
done
56+
echo "Test test_c_label passed!"
57+
58+
if [[ "$output" != "$expected" ]]; then
59+
echo "Test test_c_label output did not match expected!"
60+
echo "Expected: '$expected'"
61+
echo "Got: '$output'"
62+
exit 1
63+
fi
64+
# Repeat tests
65+
66+
exit 0

0 commit comments

Comments
 (0)