From b63f5d7c00d6a39dcf0459f9704d6c4d784f5e20 Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Fri, 15 Sep 2017 18:10:43 -0700 Subject: [PATCH] [infer][clang] translate the annotations on fields Summary: Translate the annotations on C++ fields. Reviewed By: sblackshear Differential Revision: D5847193 fbshipit-source-id: 591600f --- infer/src/clang/CType_decl.ml | 3 +- .../codetoanalyze/cpp/nullable/example.cpp | 34 +++++++++++++++++-- .../codetoanalyze/cpp/nullable/issues.exp | 9 +++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/infer/src/clang/CType_decl.ml b/infer/src/clang/CType_decl.ml index e1a90947a..715063239 100644 --- a/infer/src/clang/CType_decl.ml +++ b/infer/src/clang/CType_decl.ml @@ -122,8 +122,7 @@ let rec get_struct_fields tenv decl = | FieldDecl (_, {ni_name}, qt, _) -> let id = CGeneral_utils.mk_class_field_name class_tname ni_name in let typ = qual_type_to_sil_type tenv qt in - let annotation_items = [] in - (* For the moment we don't use them*) + let annotation_items = CAst_utils.sil_annot_of_type qt in [(id, typ, annotation_items)] | _ -> [] diff --git a/infer/tests/codetoanalyze/cpp/nullable/example.cpp b/infer/tests/codetoanalyze/cpp/nullable/example.cpp index 602819782..6f1cd3125 100644 --- a/infer/tests/codetoanalyze/cpp/nullable/example.cpp +++ b/infer/tests/codetoanalyze/cpp/nullable/example.cpp @@ -8,8 +8,9 @@ */ class T { private: - int* _Nullable nullable_field; int* unnanotated_field; + int* _Nullable nullable_field; + int* _Nonnull nonnull_field; public: void assign_nullable_field_to_null_okay() { nullable_field = nullptr; } @@ -17,6 +18,9 @@ class T { public: void assign_unnanotated_field_to_null_bad() { unnanotated_field = nullptr; } + public: + void assign_nonnull_field_to_null_bad() { nonnull_field = nullptr; } + public: void test_nullable_field_for_null_okay() { if (nullable_field == nullptr) { @@ -30,5 +34,31 @@ class T { } public: - void FN_dereference_nullable_field_bad() { *nullable_field = 42; } + void test_nonnull_field_for_null_bad() { + if (nonnull_field == nullptr) { + } + } + + public: + void dereference_unnanotated_field_okay() { *unnanotated_field = 42; } + + public: + void dereference_nonnull_field_okay() { *nonnull_field = 42; } + + public: + void dereference_nullable_field_bad() { *nullable_field = 42; } + + public: + void dereference_unnanotated_field_after_test_for_null_bad() { + if (unnanotated_field == nullptr) { + *unnanotated_field = 42; + } + } + + public: + void FP_dereference_nonnull_field_after_test_for_null_okay() { + if (nonnull_field == nullptr) { + *nonnull_field = 42; + } + } }; diff --git a/infer/tests/codetoanalyze/cpp/nullable/issues.exp b/infer/tests/codetoanalyze/cpp/nullable/issues.exp index 37bedb616..9e56ea0ef 100644 --- a/infer/tests/codetoanalyze/cpp/nullable/issues.exp +++ b/infer/tests/codetoanalyze/cpp/nullable/issues.exp @@ -1,4 +1,9 @@ -codetoanalyze/cpp/nullable/example.cpp, T_assign_nullable_field_to_null_okay, 0, FIELD_SHOULD_BE_NULLABLE, [Field nullable_field is assigned null here] +codetoanalyze/cpp/nullable/example.cpp, T_FP_dereference_nonnull_field_after_test_for_null_okay, 1, FIELD_SHOULD_BE_NULLABLE, [Field nonnull_field is compared to null here] +codetoanalyze/cpp/nullable/example.cpp, T_FP_dereference_nonnull_field_after_test_for_null_okay, 2, NULL_DEREFERENCE, [start of procedure FP_dereference_nonnull_field_after_test_for_null_okay,Condition is true] +codetoanalyze/cpp/nullable/example.cpp, T_assign_nonnull_field_to_null_bad, 0, FIELD_SHOULD_BE_NULLABLE, [Field nonnull_field is assigned null here] codetoanalyze/cpp/nullable/example.cpp, T_assign_unnanotated_field_to_null_bad, 0, FIELD_SHOULD_BE_NULLABLE, [Field unnanotated_field is assigned null here] -codetoanalyze/cpp/nullable/example.cpp, T_test_nullable_field_for_null_okay, 1, FIELD_SHOULD_BE_NULLABLE, [Field nullable_field is compared to null here] +codetoanalyze/cpp/nullable/example.cpp, T_dereference_nullable_field_bad, 0, NULL_DEREFERENCE, [start of procedure dereference_nullable_field_bad] +codetoanalyze/cpp/nullable/example.cpp, T_dereference_unnanotated_field_after_test_for_null_bad, 1, FIELD_SHOULD_BE_NULLABLE, [Field unnanotated_field is compared to null here] +codetoanalyze/cpp/nullable/example.cpp, T_dereference_unnanotated_field_after_test_for_null_bad, 2, NULL_DEREFERENCE, [start of procedure dereference_unnanotated_field_after_test_for_null_bad,Condition is true] +codetoanalyze/cpp/nullable/example.cpp, T_test_nonnull_field_for_null_bad, 1, FIELD_SHOULD_BE_NULLABLE, [Field nonnull_field is compared to null here] codetoanalyze/cpp/nullable/example.cpp, T_test_unnanotated_field_for_null_bad, 1, FIELD_SHOULD_BE_NULLABLE, [Field unnanotated_field is compared to null here]