Skip to content

Commit 6531b2e

Browse files
author
Aaron Eline
authored
Merge pull request #357 from correctcomputation/cr
CheckedRegions understand return types, fixes #263
2 parents 531e239 + 8448c26 commit 6531b2e

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

clang/lib/3C/CheckedRegions.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
using namespace llvm;
2727
using namespace clang;
2828

29+
30+
// Check if the compound statement is a function body
31+
// Used in both visitors so abstracted to a function
32+
bool isTopLevel(ASTContext *Context, CompoundStmt *S) {
33+
const auto &Parents = Context->getParents(*S);
34+
if (Parents.empty()) {
35+
return false;
36+
}
37+
// Ensure that our parent is a functiondecl
38+
return Parents[0].get<FunctionDecl>() != nullptr;
39+
}
40+
2941
// CheckedRegionAdder
3042

3143
bool CheckedRegionAdder::VisitCompoundStmt(CompoundStmt *S) {
@@ -92,12 +104,10 @@ CheckedRegionAdder::findParentCompound(const ast_type_traits::DynTypedNode &N,
92104
return *Min;
93105
}
94106

107+
108+
95109
bool CheckedRegionAdder::isFunctionBody(CompoundStmt *S) {
96-
const auto &Parents = Context->getParents(*S);
97-
if (Parents.empty()) {
98-
return false;
99-
}
100-
return Parents[0].get<FunctionDecl>();
110+
return isTopLevel(Context, S);
101111
}
102112

103113
bool CheckedRegionAdder::isParentChecked(
@@ -154,13 +164,26 @@ bool CheckedRegionFinder::VisitDoStmt(DoStmt *S) {
154164

155165
bool CheckedRegionFinder::VisitCompoundStmt(CompoundStmt *S) {
156166
// Visit all subblocks, find all unchecked types
157-
bool Localwild = 0;
167+
bool Localwild = false;
158168
for (const auto &SubStmt : S->children()) {
159169
CheckedRegionFinder Sub(Context, Writer, Info, Seen, Map, EmitWarnings);
160170
Sub.TraverseStmt(SubStmt);
161171
Localwild |= Sub.Wild;
162172
}
163173

174+
// If we are a function def, need to check return type
175+
if (isTopLevel(Context, S)) {
176+
const auto &Parents = Context->getParents(*S);
177+
assert(!Parents.empty());
178+
FunctionDecl* Parent = const_cast<FunctionDecl*>(Parents[0].get<FunctionDecl>());
179+
assert(Parent != nullptr);
180+
auto retType = Parent->getReturnType().getTypePtr();
181+
if (retType->isPointerType()) {
182+
CVarOption CV = Info.getVariable(Parent, Context);
183+
Localwild |= isWild(CV) || containsUncheckedPtr(Parent->getReturnType());
184+
}
185+
}
186+
164187
markChecked(S, Localwild);
165188

166189
Wild = false;

clang/test/3C/checkedregions.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,10 @@ void baz(void) {
8080
bad();
8181
}
8282
}
83+
84+
int* g() {
85+
//CHECK: int* g(void) {
86+
return 1;
87+
}
88+
89+

clang/test/3C/fn_sets.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ void foo1(int *z) {
5151
/* Testing Something with a larger set of functions */
5252

5353
int *a() {
54-
//CHECK: int *a(void) _Checked {
54+
//CHECK: int *a(void) {
5555
return 0;
5656
}
5757
int *b() {
58-
//CHECK: int *b(void) _Checked {
58+
//CHECK: int *b(void) {
5959
return 0;
6060
}
6161
int *c() {
62-
//CHECK: int *c(void) _Checked {
62+
//CHECK: int *c(void) {
6363
return 0;
6464
}
6565
int *d() {
66-
//CHECK: int *d(void) _Checked {
66+
//CHECK: int *d(void) {
6767
return 0;
6868
}
6969
int *e() {
@@ -72,7 +72,7 @@ int *e() {
7272
//CHECK: return (int*) 1;
7373
}
7474
int *i() {
75-
//CHECK: _Ptr<int> i(void) _Checked {
75+
//CHECK: _Ptr<int> i(void) _Checked {
7676
return 0;
7777
}
7878

0 commit comments

Comments
 (0)