Skip to content

Commit 86d5732

Browse files
dulmarodfacebook-github-bot
authored andcommitted
[objc] Add empty annotations that correspond to the captured variables added as formals of blocks
Summary: We add captured as formals in ObjC blocks, but we were missing adding annotations (at least empty ones) for those new formals. This means that the annotations that we do have for the formals of blocks were not treated correctly, because the list of formals and annotations was mismatched. This diff fixes it. It also removes those dummy annotations for the captured in the Parameter Not Null Checked checker. Reviewed By: skcho Differential Revision: D32724769 fbshipit-source-id: f7a6ddab10
1 parent 1353959 commit 86d5732

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

infer/src/checkers/ParameterNotNullChecked.ml

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,37 @@ end
191191

192192
module Analyzer = AbstractInterpreter.MakeWTO (TransferFunctions)
193193

194-
let formals_not_captured attributes =
195-
let is_not_captured (formal, _) =
196-
not
197-
(List.exists
198-
~f:(fun ({name} : CapturedVar.t) -> Mangled.equal formal name)
199-
attributes.ProcAttributes.captured )
194+
let formals_annotations_not_captured attributes =
195+
let formals = attributes.ProcAttributes.formals in
196+
let param_annotations = attributes.ProcAttributes.method_annotation.params in
197+
let formals_annotations_not_captured not_captured (formal, typ) annotation =
198+
if
199+
not
200+
(List.exists
201+
~f:(fun ({name} : CapturedVar.t) -> Mangled.equal formal name)
202+
attributes.ProcAttributes.captured )
203+
then ((formal, typ), annotation) :: not_captured
204+
else not_captured
200205
in
201-
List.filter ~f:is_not_captured attributes.ProcAttributes.formals
206+
let annotations_not_captured_opt =
207+
List.fold2 ~f:formals_annotations_not_captured ~init:[] formals param_annotations
208+
in
209+
match annotations_not_captured_opt with
210+
| List.Or_unequal_lengths.Ok not_captured ->
211+
not_captured |> List.rev |> List.unzip
212+
| List.Or_unequal_lengths.Unequal_lengths ->
213+
(formals, param_annotations)
202214

203215

204-
let init_block_params attributes formals_not_captured initBlockParams =
205-
let add_non_nullable_block blockParams annotation (formal, _) =
216+
let init_block_params formals_not_captured annotations_not_captured initBlockParams =
217+
let add_non_nullable_block blockParams (formal, _) annotation =
206218
if is_block_param formals_not_captured formal && Annotations.ia_is_nonnull annotation then
207219
BlockParams.add formal blockParams
208220
else blockParams
209221
in
210-
let annotations = attributes.ProcAttributes.method_annotation.params in
211222
match
212-
List.fold2 annotations formals_not_captured ~init:initBlockParams ~f:add_non_nullable_block
223+
List.fold2 formals_not_captured annotations_not_captured ~init:initBlockParams
224+
~f:add_non_nullable_block
213225
with
214226
| List.Or_unequal_lengths.Ok blockParams ->
215227
blockParams
@@ -229,8 +241,12 @@ let init_block_param_trace_info attributes formals_not_captured traceInfo =
229241

230242
let checker ({IntraproceduralAnalysis.proc_desc} as analysis_data') =
231243
let attributes = Procdesc.get_attributes proc_desc in
232-
let formals_not_captured = formals_not_captured attributes in
233-
let initial_blockParams = init_block_params attributes formals_not_captured BlockParams.empty in
244+
let formals_not_captured, annotations_not_captured =
245+
formals_annotations_not_captured attributes
246+
in
247+
let initial_blockParams =
248+
init_block_params formals_not_captured annotations_not_captured BlockParams.empty
249+
in
234250
let initTraceInfo = init_block_param_trace_info attributes formals_not_captured [] in
235251
let initial =
236252
Domain.singleton

infer/src/clang/cMethod_trans.ml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,6 @@ let create_local_procdesc ?(set_objc_accessor_attr = false) ?(record_lambda_capt
222222
let create_new_procdesc () =
223223
let all_params = Option.to_list ms.CMethodSignature.class_param @ ms.CMethodSignature.params in
224224
let has_added_return_param = ms.CMethodSignature.has_added_return_param in
225-
let method_annotation =
226-
let return = snd ms.CMethodSignature.ret_type in
227-
let params = List.map ~f:(fun ({annot} : CMethodSignature.param_type) -> annot) all_params in
228-
Annot.Method.{return; params}
229-
in
230225
let formals =
231226
List.map ~f:(fun ({name; typ} : CMethodSignature.param_type) -> (name, typ)) all_params
232227
in
@@ -235,7 +230,16 @@ let create_local_procdesc ?(set_objc_accessor_attr = false) ?(record_lambda_capt
235230
if is_cpp_lambda_call_operator then []
236231
else List.map ~f:(fun {CapturedVar.name; typ} -> (name, typ)) captured_mangled
237232
in
233+
let captured_annotations =
234+
List.init (List.length captured_as_formals) ~f:(fun _ -> Annot.Item.empty)
235+
in
238236
let formals = captured_as_formals @ formals in
237+
let method_annotation =
238+
let return = snd ms.CMethodSignature.ret_type in
239+
let params = List.map ~f:(fun ({annot} : CMethodSignature.param_type) -> annot) all_params in
240+
let all_params = captured_annotations @ params in
241+
Annot.Method.{return; params= all_params}
242+
in
239243
let const_formals =
240244
get_const_params_indices ~shift:(List.length captured_as_formals) all_params
241245
in

infer/tests/codetoanalyze/objc/parameter-not-null-checked/Blocks_as_parameters.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,21 @@ - (void)uploadTaskWithRequestOk:(NSURLRequest*)urlRequest
9494
}];
9595
}
9696

97+
typedef void (^AnnotateBlock)(const char* key, id value);
98+
typedef void (^AnnotateSyncBlock)(AnnotateBlock annotate);
99+
100+
void AnnotateSync(id flowId, NS_NOESCAPE AnnotateSyncBlock block) {}
101+
102+
+ (void)startWithSessionOk:(id)session video:(id)video {
103+
NSString* const videoID = @"";
104+
NSString* const flowId = @"";
105+
106+
NSString* const entryPointType = @"";
107+
108+
AnnotateSync(flowId, ^(AnnotateBlock _Nonnull annotate) {
109+
annotate("entry_point_type", entryPointType);
110+
annotate("media_id", videoID);
111+
});
112+
}
113+
97114
@end

0 commit comments

Comments
 (0)