From 0eccdb25dc9f3109bece49d1bf49f44543aad9c3 Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Thu, 26 Sep 2019 02:52:28 -0700 Subject: [PATCH] [clang] Do not try to store block declarations as methods of a C++ struct. Summary: This was causing a crash, because when trying to create a procname from a block at that point we don't have the block return type, which is needed for the name. I don't understand why BlockDecl doesn't contain the type, but I looked again and it doesn't (also in clang). So in general we need to pass it from the context, but that's not possible in this case. Also, one could argue that such a block is not a method from the struct, since it's just a block that is assigned to a field as initialization. Reviewed By: skcho Differential Revision: D17575197 fbshipit-source-id: 3974ead3f --- infer/src/clang/CType_decl.ml | 3 +-- .../codetoanalyze/objcpp/frontend/Makefile | 2 +- .../objcpp/frontend/block_in_struct.mm | 18 ++++++++++++++++++ .../objcpp/frontend/block_in_struct.mm.dot | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 infer/tests/codetoanalyze/objcpp/frontend/block_in_struct.mm create mode 100644 infer/tests/codetoanalyze/objcpp/frontend/block_in_struct.mm.dot diff --git a/infer/src/clang/CType_decl.ml b/infer/src/clang/CType_decl.ml index 8904f6b99..2a61eab1d 100644 --- a/infer/src/clang/CType_decl.ml +++ b/infer/src/clang/CType_decl.ml @@ -682,8 +682,7 @@ and get_struct_methods struct_decl tenv = | CXXConstructorDecl _ | CXXConversionDecl _ | CXXDestructorDecl _ - | ObjCMethodDecl _ - | BlockDecl _ -> + | ObjCMethodDecl _ -> Some (procname_from_decl ~tenv decl) | _ -> None ) diff --git a/infer/tests/codetoanalyze/objcpp/frontend/Makefile b/infer/tests/codetoanalyze/objcpp/frontend/Makefile index 4682ca735..181e0146b 100644 --- a/infer/tests/codetoanalyze/objcpp/frontend/Makefile +++ b/infer/tests/codetoanalyze/objcpp/frontend/Makefile @@ -10,7 +10,7 @@ CLANG_OPTIONS = -c $(OBJCPP_CLANG_OPTIONS) INFER_OPTIONS = SOURCES = \ - $(wildcard */*.mm) \ + $(wildcard *.mm) $(wildcard */*.mm) \ include $(TESTS_DIR)/clang-frontend.make include $(TESTS_DIR)/objc.make diff --git a/infer/tests/codetoanalyze/objcpp/frontend/block_in_struct.mm b/infer/tests/codetoanalyze/objcpp/frontend/block_in_struct.mm new file mode 100644 index 000000000..2f080c814 --- /dev/null +++ b/infer/tests/codetoanalyze/objcpp/frontend/block_in_struct.mm @@ -0,0 +1,18 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#import + +typedef BOOL (^ExperimentBlock)(void); + +struct Configuration { + + ExperimentBlock block = ^{ + return NO; + }; +}; + +int main() { return 0; } diff --git a/infer/tests/codetoanalyze/objcpp/frontend/block_in_struct.mm.dot b/infer/tests/codetoanalyze/objcpp/frontend/block_in_struct.mm.dot new file mode 100644 index 000000000..c0d47ee4e --- /dev/null +++ b/infer/tests/codetoanalyze/objcpp/frontend/block_in_struct.mm.dot @@ -0,0 +1,14 @@ +/* @generated */ +digraph cfg { +"main.fad58de7366495db4650cfefac2fcd61_1" [label="1: Start main\nFormals: \nLocals: \n " color=yellow style=filled] + + + "main.fad58de7366495db4650cfefac2fcd61_1" -> "main.fad58de7366495db4650cfefac2fcd61_3" ; +"main.fad58de7366495db4650cfefac2fcd61_2" [label="2: Exit main \n " color=yellow style=filled] + + +"main.fad58de7366495db4650cfefac2fcd61_3" [label="3: Return Stmt \n *&return:int=0 [line 18, column 14]\n APPLY_ABSTRACTION; [line 18, column 14]\n " shape="box"] + + + "main.fad58de7366495db4650cfefac2fcd61_3" -> "main.fad58de7366495db4650cfefac2fcd61_2" ; +}