Skip to content

Commit 51b6167

Browse files
committed
Adding initializer to checked pointers
1 parent b2d8f0d commit 51b6167

14 files changed

+42
-26
lines changed

tools/checked-c-convert/RewriteUtils.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <algorithm>
1313
#include <map>
1414
#include <sstream>
15-
15+
#include "clang/AST/Type.h"
1616
#include "RewriteUtils.h"
1717
#include "MappingVisitor.h"
1818
#include "Utils.h"
@@ -168,6 +168,11 @@ void rewrite( VarDecl *VD,
168168
SourceLocation eqLoc = VD->getInitializerStartLoc();
169169
TR.setEnd(eqLoc);
170170
sRewrite = sRewrite + " = ";
171+
} else {
172+
// DAMN, there is no initializer, lets add it.
173+
if(isPointerType(VD)) {
174+
sRewrite = sRewrite + " = NULL";
175+
}
171176
}
172177

173178
// Is it a variable type? This is the easy case, we can re-write it
@@ -242,6 +247,10 @@ void rewrite( VarDecl *VD,
242247
if (Expr *E = VDL->getInit()) {
243248
newMLDecl << " = ";
244249
E->printPretty(newMLDecl, nullptr, A.getPrintingPolicy());
250+
} else {
251+
if(isPointerType(VDL)) {
252+
newMLDecl << " = NULL";
253+
}
245254
}
246255
newMLDecl << ";\n";
247256
}

tools/checked-c-convert/Utils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,8 @@ bool isFunctionAllocator(std::string funcName) {
189189

190190
float getTimeSpentInSeconds(clock_t startTime) {
191191
return float(clock() - startTime)/CLOCKS_PER_SEC;
192+
}
193+
194+
bool isPointerType(clang::VarDecl *VD) {
195+
return VD->getType().getTypePtr()->isPointerType();
192196
}

tools/checked-c-convert/Utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@ bool functionHasVarArgs(clang::FunctionDecl *FD);
5858
// check if the function is a allocator.
5959
bool isFunctionAllocator(std::string funcName);
6060

61+
// Is the given variable built in type?
62+
bool isPointerType(clang::VarDecl *VD);
63+
6164
clang::SourceLocation getFunctionDeclarationEnd(clang::FunctionDecl *FD, clang::SourceManager &S);
6265
#endif

tools/checked-c-convert/functests/arr/basic_inter.expected.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ int func(int *ptr, int *iptr : itype(_Ptr<int> ) , int *wild) {/*ARR:ptr*/
2323
int main() {
2424
int a, b, c;
2525
// this will be Ptr
26-
_Ptr<int> ap;
26+
_Ptr<int> ap = NULL;
2727
// this will be WILD
2828
int *bp;
2929
// this will be _Ptr
30-
_Ptr<int> cp;
30+
_Ptr<int> cp = NULL;
3131
// this will be _Ptr
32-
_Ptr<int> ap1;
32+
_Ptr<int> ap1 = NULL;
3333
// this will be WILD
3434
int *bp1;
3535
// this will be _Ptr
36-
_Ptr<int> cp1;
36+
_Ptr<int> cp1 = NULL;
3737

3838

3939
ap1 = &a;

tools/checked-c-convert/functests/arr/basic_inter_field.expected.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ _Ptr<int> func(_Ptr<int> ptr, char *arrptr) {/*ARR:arrptr*/
1818

1919
int main() {
2020
int a;
21-
_Ptr<int> b;
21+
_Ptr<int> b = NULL;
2222
char *wil;
2323
wil = 0xdeadbeef;
2424
b = func(&a, wil);

tools/checked-c-convert/functests/arr/basic_local.expected.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ int main() {
77
int *a;/*ARR:a*/
88
// c also should be identified as
99
// _Ptr as we do not use it.
10-
_Ptr<int> c;
10+
_Ptr<int> c = NULL;
1111
// we will make this wild.
1212
int *d;
1313
int b;

tools/checked-c-convert/functests/ntarr/basic_field_local.expected.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef struct {
2121

2222
int main() {
2323
foo obj;
24-
_Nt_array_ptr<char> bp;
24+
_Nt_array_ptr<char> bp = NULL;
2525
float b;
2626
foo2 obj2;
2727
int hel;

tools/checked-c-convert/functests/ntarr/basic_inter.expected.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ int func(int *ptr, int *iptr : itype(_Ptr<int> ) , int *wild) {/*ARR:ptr*/
2525
int main() {
2626
int a, b, c;
2727
// this will be ARR
28-
_Ptr<int> ap;
28+
_Ptr<int> ap = NULL;
2929
// this will be WILD
3030
int *bp;
3131
// this will be _Ptr
32-
_Ptr<int> cp;
32+
_Ptr<int> cp = NULL;
3333
// this will be wild
3434
char *ap1;
3535
// this will be WILD
3636
int *bp1;
3737
// this will be _Ptr
38-
_Ptr<int> cp1;
38+
_Ptr<int> cp1 = NULL;
3939

4040

4141
//ap1 = &a;

tools/checked-c-convert/functests/ntarr/basic_inter_field.expected.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ _Ptr<int> func(_Ptr<int> ptr, _Nt_array_ptr<char> ntptr) {
1616

1717
int main() {
1818
int a;
19-
_Ptr<int> b;
20-
_Nt_array_ptr<char> wil;
19+
_Ptr<int> b = NULL;
20+
_Nt_array_ptr<char> wil = NULL;
2121
a = strlen(wil);
2222
b = func(&a, wil);
2323
}

tools/checked-c-convert/functests/ntarr/basic_local.expected.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ int main() {
66
// as NTArr
77
// we use this as an argument
88
// to string function.
9-
_Nt_array_ptr<char> a;
9+
_Nt_array_ptr<char> a = NULL;
1010
// c should be identified as
1111
// ARR as we assign it the return value of
1212
// string function use it.
13-
_Nt_array_ptr<char> c;
13+
_Nt_array_ptr<char> c = NULL;
1414
// we will make this wild.
1515
int *d;
1616
int b;

tools/checked-c-convert/functests/ptr/basic_inter.expected.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ int func(_Ptr<int> ptr, int *iptr : itype(_Ptr<int> ) , int *wild) {
2323
int main() {
2424
int a, b, c;
2525
// this will be _Ptr
26-
_Ptr<int> ap;
26+
_Ptr<int> ap = NULL;
2727
// this will be WILD
2828
int *bp;
2929
// this will be _Ptr
30-
_Ptr<int> cp;
30+
_Ptr<int> cp = NULL;
3131
// this will be _Ptr
32-
_Ptr<int> ap1;
32+
_Ptr<int> ap1 = NULL;
3333
// this will be WILD
3434
int *bp1;
3535
// this will be _Ptr
36-
_Ptr<int> cp1;
36+
_Ptr<int> cp1 = NULL;
3737

3838

3939
ap1 = ap = &a;

tools/checked-c-convert/functests/ptr/basic_inter_field.expected.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ _Ptr<int> func(_Ptr<int> ptr, char *iwild : itype(_Ptr<char> ) ) {
1717

1818
int main() {
1919
int a;
20-
_Ptr<int> b;
20+
_Ptr<int> b = NULL;
2121
char *wil;
2222
wil = 0xdeadbeef;
2323
b = func(&a, wil);

tools/checked-c-convert/functests/ptr/basic_local.expected.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
int main() {
55
// a has to identified
66
// as _Ptr
7-
_Ptr<int> a;
7+
_Ptr<int> a = NULL;
88
// c also
99
// should be identified as
1010
// _Ptr
11-
_Ptr<int> c;
11+
_Ptr<int> c = NULL;
1212
int *d;
1313
int b;
1414
a = &b;

tools/checked-c-convert/functests/ptr/basic_return_itype.expected.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ static int *func(_Ptr<int> ptr, int *iptr : itype(_Ptr<int> ) , int *wild) : ity
2626
int main() {
2727
int a, b, c;
2828
// this will be _Ptr
29-
_Ptr<int> ap;
29+
_Ptr<int> ap = NULL;
3030
// this will be WILD
3131
int *bp;
3232
// this will be _Ptr
33-
_Ptr<int> cp;
33+
_Ptr<int> cp = NULL;
3434
// this will be _Ptr
35-
_Ptr<int> ap1;
35+
_Ptr<int> ap1 = NULL;
3636
// this will be WILD
3737
int *bp1;
3838
// this will be _Ptr
39-
_Ptr<int> cp1;
39+
_Ptr<int> cp1 = NULL;
4040

4141

4242
ap1 = ap = &a;

0 commit comments

Comments
 (0)