Skip to content

Commit

Permalink
[infer] Revise empty method name for cxx constructor
Browse files Browse the repository at this point in the history
Summary: For some constructors of non-class objects in C++, the clang frontend gives empty method name, e.g. struct, lambda, and union.  For better readability, this diff replaces them to a constant non-empty name `__cxx_constructor`.

Reviewed By: ezgicicek

Differential Revision: D32558068

fbshipit-source-id: 6cc606346f
  • Loading branch information
skcho authored and facebook-github-bot committed Nov 29, 2021
1 parent 03d3e0e commit d8c79eb
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 160 deletions.
20 changes: 16 additions & 4 deletions infer/src/clang/CType_decl.ml
Expand Up @@ -673,18 +673,30 @@ and procname_from_decl ?tenv ?block_return_type ?outer_proc meth_decl =
| None ->
[]
in
let mk_cpp_method decl_info name_info fdi mdi =
let mangled = get_mangled_method_name fdi mdi in
let method_name = CAst_utils.get_unqualified_name name_info in
let class_typename = get_class_typename ?tenv decl_info in
mk_cpp_method ?tenv class_typename method_name ~meth_decl mangled parameters
in
match meth_decl with
| FunctionDecl (decl_info, name_info, _, fdi) ->
let name = CAst_utils.get_qualified_name name_info in
mk_c_function ?tenv name (Some (decl_info, fdi)) parameters
| CXXConstructorDecl (decl_info, {ni_name= ""; ni_qual_name= "" :: qual_names}, _, fdi, mdi) ->
(* For some constructors of non-class objects in C++, the clang frontend gives empty method
name, e.g. struct, lambda, and union. For better readability, we replace them to a
constant non-empty name. *)
let name_info =
{ ni_name= CFrontend_config.cxx_constructor
; ni_qual_name= CFrontend_config.cxx_constructor :: qual_names }
in
mk_cpp_method decl_info name_info fdi mdi
| CXXMethodDecl (decl_info, name_info, _, fdi, mdi)
| CXXConstructorDecl (decl_info, name_info, _, fdi, mdi)
| CXXConversionDecl (decl_info, name_info, _, fdi, mdi)
| CXXDestructorDecl (decl_info, name_info, _, fdi, mdi) ->
let mangled = get_mangled_method_name fdi mdi in
let method_name = CAst_utils.get_unqualified_name name_info in
let class_typename = get_class_typename ?tenv decl_info in
mk_cpp_method ?tenv class_typename method_name ~meth_decl mangled parameters
mk_cpp_method decl_info name_info fdi mdi
| ObjCMethodDecl (decl_info, name_info, mdi) ->
objc_method_procname ?tenv decl_info name_info.Clang_ast_t.ni_name mdi parameters
| BlockDecl _ ->
Expand Down
2 changes: 2 additions & 0 deletions infer/src/clang/cFrontend_config.ml
Expand Up @@ -62,6 +62,8 @@ let clang_bin xx =

let class_method = "class"

let cxx_constructor = "__cxx_constructor"

let fbAssertWithSignalAndLogFunctionHelper = "FBAssertWithSignalAndLogFunctionHelper"

let google_LogMessageFatal = "google::LogMessageFatal_LogMessageFatal"
Expand Down
2 changes: 2 additions & 0 deletions infer/src/clang/cFrontend_config.mli
Expand Up @@ -58,6 +58,8 @@ val clang_bin : string -> string

val class_method : string

val cxx_constructor : string

val fbAssertWithSignalAndLogFunctionHelper : string

val google_LogMessageFatal : string
Expand Down
4 changes: 2 additions & 2 deletions infer/tests/codetoanalyze/cpp/biabduction/issues.exp
Expand Up @@ -199,8 +199,8 @@ codetoanalyze/cpp/shared/constructors/temp_object.cpp, temp_object::temp_field2_
codetoanalyze/cpp/shared/constructors/temp_object.cpp, temp_object::temp_field_div0, 0, DIVIDE_BY_ZERO, no_bucket, ERROR, [start of procedure temp_object::temp_field_div0(),start of procedure X,return from a call to temp_object::X::X,start of procedure temp_object::div()]
codetoanalyze/cpp/shared/constructors/temp_object.cpp, temp_object::temp_method_div0, 0, DIVIDE_BY_ZERO, no_bucket, ERROR, [start of procedure temp_object::temp_method_div0(),start of procedure X,return from a call to temp_object::X::X,start of procedure div]
codetoanalyze/cpp/shared/exceptions/Exceptions.cpp, call_deref_with_null, 0, NULL_DEREFERENCE, B1, ERROR, [start of procedure call_deref_with_null(),start of procedure deref_null()]
codetoanalyze/cpp/shared/lambda/lambda1.cpp, bar, 5, DIVIDE_BY_ZERO, no_bucket, ERROR, [start of procedure bar(),start of procedure ,return from a call to bar::lambda_shared_lambda_lambda1.cpp:9:15::,start of procedure operator(),return from a call to bar::lambda_shared_lambda_lambda1.cpp:9:15::operator()]
codetoanalyze/cpp/shared/lambda/lambda1.cpp, foo, 3, DIVIDE_BY_ZERO, no_bucket, ERROR, [start of procedure foo(),start of procedure ,return from a call to foo::lambda_shared_lambda_lambda1.cpp:17:17::,start of procedure ,return from a call to foo::lambda_shared_lambda_lambda1.cpp:18:12::,start of procedure operator(),return from a call to foo::lambda_shared_lambda_lambda1.cpp:18:12::operator()]
codetoanalyze/cpp/shared/lambda/lambda1.cpp, bar, 5, DIVIDE_BY_ZERO, no_bucket, ERROR, [start of procedure bar(),start of procedure __cxx_constructor,return from a call to bar::lambda_shared_lambda_lambda1.cpp:9:15::__cxx_constructor,start of procedure operator(),return from a call to bar::lambda_shared_lambda_lambda1.cpp:9:15::operator()]
codetoanalyze/cpp/shared/lambda/lambda1.cpp, foo, 3, DIVIDE_BY_ZERO, no_bucket, ERROR, [start of procedure foo(),start of procedure __cxx_constructor,return from a call to foo::lambda_shared_lambda_lambda1.cpp:17:17::__cxx_constructor,start of procedure __cxx_constructor,return from a call to foo::lambda_shared_lambda_lambda1.cpp:18:12::__cxx_constructor,start of procedure operator(),return from a call to foo::lambda_shared_lambda_lambda1.cpp:18:12::operator()]
codetoanalyze/cpp/shared/lambda/lambda1.cpp, foo::lambda_shared_lambda_lambda1.cpp:17:17::operator(), 0, DIVIDE_BY_ZERO, no_bucket, ERROR, [start of procedure operator()]
codetoanalyze/cpp/shared/methods/conversion_operator.cpp, conversion_operator::branch_div0, 4, DIVIDE_BY_ZERO, no_bucket, ERROR, [start of procedure conversion_operator::branch_div0(),start of procedure X,return from a call to conversion_operator::X::X,start of procedure operator_bool,return from a call to conversion_operator::X::operator_bool,Taking true branch,start of procedure operator_int,return from a call to conversion_operator::X::operator_int]
codetoanalyze/cpp/shared/methods/conversion_operator.cpp, conversion_operator::y_branch_div0, 6, DIVIDE_BY_ZERO, no_bucket, ERROR, [start of procedure conversion_operator::y_branch_div0(),start of procedure Y,return from a call to conversion_operator::Y::Y,start of procedure operator_X,start of procedure X,return from a call to conversion_operator::X::X,start of procedure X,return from a call to conversion_operator::X::X,return from a call to conversion_operator::Y::operator_X,start of procedure X,return from a call to conversion_operator::X::X,start of procedure operator_bool,return from a call to conversion_operator::X::operator_bool,Taking true branch,start of procedure operator_X,start of procedure X,return from a call to conversion_operator::X::X,start of procedure X,return from a call to conversion_operator::X::X,return from a call to conversion_operator::Y::operator_X,start of procedure X,return from a call to conversion_operator::X::X,start of procedure operator_int,return from a call to conversion_operator::X::operator_int]
Expand Down
Expand Up @@ -7,7 +7,7 @@ digraph cfg {
"__infer_globals_initializer_y.0ea250be2dd991733c9131c53abc3c54_2" [label="2: Exit __infer_globals_initializer_y \n " color=yellow style=filled]


"__infer_globals_initializer_y.0ea250be2dd991733c9131c53abc3c54_3" [label="3: DeclStmt \n VARIABLE_DECLARED(#GB<codetoanalyze/cpp/frontend/nestedoperators/union.cpp>$y:anonymous_union_nestedoperators_union.cpp:13:1); [line 13, column 1]\n n$0=_fun_anonymous_union_nestedoperators_union.cpp:13:1::(&#GB<codetoanalyze/cpp/frontend/nestedoperators/union.cpp>$y:anonymous_union_nestedoperators_union.cpp:13:1*) [line 23, column 3]\n " shape="box"]
"__infer_globals_initializer_y.0ea250be2dd991733c9131c53abc3c54_3" [label="3: DeclStmt \n VARIABLE_DECLARED(#GB<codetoanalyze/cpp/frontend/nestedoperators/union.cpp>$y:anonymous_union_nestedoperators_union.cpp:13:1); [line 13, column 1]\n n$0=_fun_anonymous_union_nestedoperators_union.cpp:13:1::__cxx_constructor(&#GB<codetoanalyze/cpp/frontend/nestedoperators/union.cpp>$y:anonymous_union_nestedoperators_union.cpp:13:1*) [line 23, column 3]\n " shape="box"]


"__infer_globals_initializer_y.0ea250be2dd991733c9131c53abc3c54_3" -> "__infer_globals_initializer_y.0ea250be2dd991733c9131c53abc3c54_2" ;
Expand Down Expand Up @@ -38,11 +38,11 @@ digraph cfg {


"main.fad58de7366495db4650cfefac2fcd61_7" -> "main.fad58de7366495db4650cfefac2fcd61_6" ;
"#anonymous_union_nestedoperators_union.cpp:13:1#{}12006367870009433944.b2a6fc5036a060aa9fbc38e79d264a62_1" [label="1: Start anonymous_union_nestedoperators_union.cpp:13:1::\nFormals: this:anonymous_union_nestedoperators_union.cpp:13:1*\nLocals: \n " color=yellow style=filled]
"__cxx_constructor#anonymous_union_nestedoperators_union.cpp:13:1#{}12006367870009433944.dc90875270147ed05fe2b6c5c9de5f66_1" [label="1: Start anonymous_union_nestedoperators_union.cpp:13:1::__cxx_constructor\nFormals: this:anonymous_union_nestedoperators_union.cpp:13:1*\nLocals: \n " color=yellow style=filled]


"#anonymous_union_nestedoperators_union.cpp:13:1#{}12006367870009433944.b2a6fc5036a060aa9fbc38e79d264a62_1" -> "#anonymous_union_nestedoperators_union.cpp:13:1#{}12006367870009433944.b2a6fc5036a060aa9fbc38e79d264a62_2" ;
"#anonymous_union_nestedoperators_union.cpp:13:1#{}12006367870009433944.b2a6fc5036a060aa9fbc38e79d264a62_2" [label="2: Exit anonymous_union_nestedoperators_union.cpp:13:1:: \n " color=yellow style=filled]
"__cxx_constructor#anonymous_union_nestedoperators_union.cpp:13:1#{}12006367870009433944.dc90875270147ed05fe2b6c5c9de5f66_1" -> "__cxx_constructor#anonymous_union_nestedoperators_union.cpp:13:1#{}12006367870009433944.dc90875270147ed05fe2b6c5c9de5f66_2" ;
"__cxx_constructor#anonymous_union_nestedoperators_union.cpp:13:1#{}12006367870009433944.dc90875270147ed05fe2b6c5c9de5f66_2" [label="2: Exit anonymous_union_nestedoperators_union.cpp:13:1::__cxx_constructor \n " color=yellow style=filled]


}
6 changes: 3 additions & 3 deletions infer/tests/codetoanalyze/cpp/performance-11/cost-issues.exp
Expand Up @@ -21,17 +21,17 @@ codetoanalyze/cpp/performance/deque.cpp, iteratec_over_deque_linear, 4 + 2 ⋅ d
codetoanalyze/cpp/performance/deque.cpp, range_based_loop_over_deque_linear, 9 + 5 ⋅ d->elements.length.ub + 14 ⋅ (d->elements.length.ub + 1), OnUIThread:false, [{d->elements.length.ub + 1},Loop,{d->elements.length.ub},Loop]
codetoanalyze/cpp/performance/foreachtest.cpp, add, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN, 21, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_.._performance_foreachtest.cpp:39:42::, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_.._performance_foreachtest.cpp:39:42::__cxx_constructor, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_.._performance_foreachtest.cpp:39:42::__infer_inner_destructor_~, 2, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_.._performance_foreachtest.cpp:39:42::operator(), 28, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_.._performance_foreachtest.cpp:39:42::~, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, loop_linear_list, 12 + 8 ⋅ list->elements.length.ub + 14 ⋅ (list->elements.length.ub + 1), OnUIThread:false, [{list->elements.length.ub + 1},Loop,{list->elements.length.ub},Loop]
codetoanalyze/cpp/performance/foreachtest.cpp, loop_linear_vec, 18 + 14 ⋅ vec->cpp.vector_elem.length.ub + 18 ⋅ (vec->cpp.vector_elem.length.ub + 1), OnUIThread:false, [{vec->cpp.vector_elem.length.ub + 1},Loop,{vec->cpp.vector_elem.length.ub},Loop]
codetoanalyze/cpp/performance/foreachtest.cpp, map_linear_FN, 9, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, map_linear_FN::lambda_.._performance_foreachtest.cpp:31:36::, 0, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, map_linear_FN::lambda_.._performance_foreachtest.cpp:31:36::__cxx_constructor, 0, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, map_linear_FN::lambda_.._performance_foreachtest.cpp:31:36::operator(), 7, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN, 27, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_.._performance_foreachtest.cpp:45:36::, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_.._performance_foreachtest.cpp:45:36::__cxx_constructor, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_.._performance_foreachtest.cpp:45:36::__infer_inner_destructor_~, 2, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_.._performance_foreachtest.cpp:45:36::operator(), 38, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_.._performance_foreachtest.cpp:45:36::~, 3, OnUIThread:false, []
Expand Down
6 changes: 3 additions & 3 deletions infer/tests/codetoanalyze/cpp/performance/cost-issues.exp
Expand Up @@ -21,17 +21,17 @@ codetoanalyze/cpp/performance/deque.cpp, iteratec_over_deque_linear, 3 + 2 ⋅ d
codetoanalyze/cpp/performance/deque.cpp, range_based_loop_over_deque_linear, 7 + 5 ⋅ d->elements.length.ub + 14 ⋅ (d->elements.length.ub + 1), OnUIThread:false, [{d->elements.length.ub + 1},Loop,{d->elements.length.ub},Loop]
codetoanalyze/cpp/performance/foreachtest.cpp, add, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN, 13, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_foreachtest.cpp:39:42::, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_foreachtest.cpp:39:42::__cxx_constructor, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_foreachtest.cpp:39:42::__infer_inner_destructor_~, 2, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_foreachtest.cpp:39:42::operator(), 26, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, list_quadratic_FN::lambda_foreachtest.cpp:39:42::~, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, loop_linear_list, 10 + 8 ⋅ list->elements.length.ub + 14 ⋅ (list->elements.length.ub + 1), OnUIThread:false, [{list->elements.length.ub + 1},Loop,{list->elements.length.ub},Loop]
codetoanalyze/cpp/performance/foreachtest.cpp, loop_linear_vec, 10 + 14 ⋅ vec->cpp.vector_elem.length.ub + 18 ⋅ (vec->cpp.vector_elem.length.ub + 1), OnUIThread:false, [{vec->cpp.vector_elem.length.ub + 1},Loop,{vec->cpp.vector_elem.length.ub},Loop]
codetoanalyze/cpp/performance/foreachtest.cpp, map_linear_FN, 5, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, map_linear_FN::lambda_foreachtest.cpp:31:36::, 0, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, map_linear_FN::lambda_foreachtest.cpp:31:36::__cxx_constructor, 0, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, map_linear_FN::lambda_foreachtest.cpp:31:36::operator(), 7, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN, 13, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_foreachtest.cpp:45:36::, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_foreachtest.cpp:45:36::__cxx_constructor, 3, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_foreachtest.cpp:45:36::__infer_inner_destructor_~, 2, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_foreachtest.cpp:45:36::operator(), 30, OnUIThread:false, []
codetoanalyze/cpp/performance/foreachtest.cpp, vector_quadratic_FN::lambda_foreachtest.cpp:45:36::~, 3, OnUIThread:false, []
Expand Down

0 comments on commit d8c79eb

Please sign in to comment.