Skip to content

Commit 25b8b6c

Browse files
committed
Add tests and fix SourcLocation for API note errors
1 parent 8f8317f commit 25b8b6c

File tree

9 files changed

+171
-1
lines changed

9 files changed

+171
-1
lines changed

clang/lib/Sema/SemaAPINotes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ static void applyBoundsSafety(Sema &S, ValueDecl *D,
451451
}
452452

453453
S.applyPtrCountedByEndedByAttr(D, *Info.getLevel(), Kind, ParsedExpr.get(),
454-
SourceLocation(), SourceRange(), AttrName);
454+
D->getLocation(), D->getSourceRange(),
455+
AttrName);
455456
}
456457
}
457458
/* TO_UPSTREAM(BoundsSafety) OFF */

clang/test/APINotes/Inputs/Headers/BoundsUnsafe.apinotes

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ Functions:
5757
Kind: counted_by
5858
Level: 0
5959
BoundedBy: 7
60+
- Name: asdf_counted_nullable
61+
Parameters:
62+
- Position: 1
63+
BoundsSafety:
64+
Kind: counted_by
65+
Level: 0
66+
BoundedBy: len
67+
- Name: asdf_counted_noescape
68+
Parameters:
69+
- Position: 0
70+
NoEscape: true
71+
BoundsSafety:
72+
Kind: counted_by
73+
Level: 0
74+
BoundedBy: len
6075
- Name: asdf_nterm
6176
Parameters:
6277
- Position: 0

clang/test/APINotes/Inputs/Headers/BoundsUnsafe.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ void asdf_ended(int * buf, int * end);
77
void asdf_sized_mul(int * buf, int size, int count);
88
void asdf_counted_out(int ** buf, int * len);
99
void asdf_counted_const(int * buf);
10+
void asdf_counted_nullable(int len, int * _Nullable buf);
11+
void asdf_counted_noescape(int * buf, int len);
1012

1113
void asdf_nterm(char * buf);

clang/test/APINotes/Inputs/Headers/BoundsUnsafeObjC.apinotes

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ Classes:
6767
Kind: counted_by
6868
Level: 0
6969
BoundedBy: 7
70+
- Selector: "asdf_counted_nullable:buf:"
71+
MethodKind: Instance
72+
Parameters:
73+
- Position: 1
74+
BoundsSafety:
75+
Kind: counted_by
76+
Level: 0
77+
BoundedBy: len
78+
- Selector: "asdf_counted_noescape:len:"
79+
MethodKind: Instance
80+
Parameters:
81+
- Position: 0
82+
NoEscape: true
83+
BoundsSafety:
84+
Kind: counted_by
85+
Level: 0
86+
BoundedBy: len
7087
- Selector: "asdf_nterm:"
7188
MethodKind: Instance
7289
Parameters:

clang/test/APINotes/Inputs/Headers/BoundsUnsafeObjC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- (void) asdf_sized_mul: (int *)buf size:(int)size count:(int)count;
99
- (void) asdf_counted_out: (int **)buf len:(int *)len;
1010
- (void) asdf_counted_const: (int *)buf;
11+
- (void) asdf_counted_nullable: (int)len buf:(int * _Nullable)buf;
12+
- (void) asdf_counted_noescape: (int *)buf len: (int)len;
1113

1214
- (void) asdf_nterm: (char *) buf;
1315
@end

clang/test/APINotes/Inputs/Headers/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ module BoundsUnsafe {
22
header "BoundsUnsafe.h"
33
}
44

5+
module BoundsUnsafeErrors {
6+
header "BoundsUnsafeErrors.h"
7+
}
8+
59
module BoundsUnsafeObjC {
610
header "BoundsUnsafeObjC.h"
711
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// RUN: rm -rf %t && mkdir -p %t
2+
// RUN: split-file %s %t/Headers
3+
// RUN: not %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fsyntax-only -fapinotes-modules -I %t/Headers -fexperimental-bounds-safety-attributes %t/Headers/SemaErrors.c 2>&1 | FileCheck %s
4+
// RUN: not %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fsyntax-only -fapinotes-modules -I %t/Headers -fexperimental-bounds-safety-attributes %t/Headers/NegLevel.c 2>&1 | FileCheck %s --check-prefix NEGLEVEL
5+
// RUN: not %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fsyntax-only -fapinotes-modules -I %t/Headers -fexperimental-bounds-safety-attributes %t/Headers/InvalidKind.c 2>&1 | FileCheck %s --check-prefix INVALIDKIND
6+
7+
//--- module.modulemap
8+
module SemaErrors {
9+
header "SemaErrors.h"
10+
}
11+
module NegLevel {
12+
header "NegLevel.h"
13+
}
14+
module InvalidKind {
15+
header "InvalidKind.h"
16+
}
17+
18+
//--- SemaErrors.c
19+
#include "SemaErrors.h"
20+
//--- SemaErrors.apinotes
21+
Name: OOBLevel
22+
Functions:
23+
- Name: oob_level
24+
Parameters:
25+
- Position: 0
26+
BoundsSafety:
27+
Kind: counted_by
28+
Level: 42
29+
BoundedBy: len
30+
- Name: off_by_1_level
31+
Parameters:
32+
- Position: 0
33+
BoundsSafety:
34+
Kind: counted_by
35+
Level: 1
36+
BoundedBy: len
37+
- Name: nonpointer_param
38+
Parameters:
39+
- Position: 0
40+
BoundsSafety:
41+
Kind: counted_by
42+
Level: 0
43+
BoundedBy: len
44+
- Name: wrong_name
45+
Parameters:
46+
- Position: 0
47+
BoundsSafety:
48+
Kind: counted_by
49+
Level: 0
50+
BoundedBy: len
51+
- Name: wrong_scope
52+
Parameters:
53+
- Position: 0
54+
BoundsSafety:
55+
Kind: counted_by
56+
Level: 0
57+
BoundedBy: static_len
58+
//--- SemaErrors.h
59+
// CHECK: SemaErrors.h:{{.*}}:{{.*}}: error: __counted_by attribute only applies to pointer arguments
60+
// CHECK-NEXT: oob_level
61+
void oob_level(int * buf, int len);
62+
// CHECK: SemaErrors.h:{{.*}}:{{.*}}: error: __counted_by attribute only applies to pointer arguments
63+
// CHECK-NEXT: off_by_1_level
64+
void off_by_1_level(int * buf, int len);
65+
// CHECK: SemaErrors.h:{{.*}}:{{.*}}: error: __counted_by attribute only applies to pointer arguments
66+
// CHECK-NEXT: nonpointer_param
67+
void nonpointer_param(int buf, int len);
68+
// CHECK: <API Notes>:1:1: error: use of undeclared identifier 'len'; did you mean 'len2'?
69+
// CHECK: SemaErrors.h:{{.*}}:{{.*}}: note: 'len2' declared here
70+
// CHECK-NEXT: wrong_name
71+
void wrong_name(int * buf, int len2);
72+
// CHECK: SemaErrors.h:{{.*}}:{{.*}}: error: count expression in function declaration may only reference parameters of that function
73+
// CHECK-NEXT: wrong_scope
74+
int static_len = 5;
75+
void wrong_scope(int * buf);
76+
// CHECK: SemaErrors.c:{{.*}}:{{.*}}: fatal error: could not build module 'SemaErrors'
77+
78+
79+
//--- NegLevel.apinotes
80+
Name: NegLevel
81+
Functions:
82+
- Name: neg_level
83+
Parameters:
84+
- Position: 0
85+
BoundsSafety:
86+
Kind: counted_by
87+
Level: -1
88+
BoundedBy: len
89+
//--- NegLevel.h
90+
void neg_level(int * buf, int len);
91+
//--- NegLevel.c
92+
#include "NegLevel.h"
93+
// NEGLEVEL: NegLevel.apinotes:{{.*}}:{{.*}}: error: invalid number
94+
// NEGLEVEL-NEXT: Level: -1
95+
// NEGLEVEL: NegLevel.c:{{.*}}:{{.*}}: fatal error: could not build module 'NegLevel'
96+
97+
98+
//--- InvalidKind.apinotes
99+
Name: InvalidKind
100+
Functions:
101+
- Name: invalid_kind
102+
Parameters:
103+
- Position: 0
104+
BoundsSafety:
105+
Kind: __counted_by
106+
Level: 0
107+
BoundedBy: len
108+
//--- InvalidKind.h
109+
void invalid_kind(int * buf, int len);
110+
//--- InvalidKind.c
111+
#include "InvalidKind.h"
112+
// INVALIDKIND: InvalidKind.apinotes:{{.*}}:{{.*}}: error: unknown enumerated scalar
113+
// INVALIDKIND-NEXT: Kind: __counted_by
114+
// INVALIDKIND: InvalidKind.c:{{.*}}:{{.*}}: fatal error: could not build module 'InvalidKind'
115+

clang/test/APINotes/boundssafety.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
// CHECK: asdf_counted_const 'void (int * __counted_by(7))'
2929
// CHECK: buf 'int * __counted_by(7)':'int *'
3030

31+
// CHECK: asdf_counted_nullable 'void (int, int * __counted_by(len) _Nullable)'
32+
// CHECK: buf 'int * __counted_by(len) _Nullable':'int *'
33+
34+
// CHECK: asdf_counted_noescape 'void (int * __counted_by(len), int)'
35+
// CHECK: buf 'int * __counted_by(len)':'int *'
36+
// CHECK-NEXT: NoEscapeAttr
37+
3138
// CHECK: asdf_nterm 'void (int * __terminated_by(0))'
3239
// CHECK: buf 'int * __terminated_by(0)':'int *'
3340

clang/test/APINotes/boundssafety.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@
4343
// CHECK-LABEL: asdf_counted_const
4444
// CHECK: buf 'int * __counted_by(7)':'int *'
4545

46+
// CHECK-LABEL: asdf_counted_nullable
47+
// CHECK: buf 'int * __counted_by(len) _Nullable':'int *'
48+
49+
// CHECK-LABEL: asdf_counted_noescape
50+
// CHECK: buf 'int * __counted_by(len)':'int *'
51+
// CHECK-NEXT: NoEscapeAttr
52+
4653
// CHECK-LABEL: asdf_nterm
4754
// CHECK: buf 'int * __terminated_by(0)':'int *'
4855

0 commit comments

Comments
 (0)