From 697778cc3bbd4c32ae32df9272d027cf03de010d Mon Sep 17 00:00:00 2001 From: jrm Date: Thu, 25 Feb 2016 22:44:17 -0800 Subject: [PATCH] Add the annotation @IgnoreAllocations to stop tracking allocations whenever useless Summary:public Add support for stopping allocation tracking on methods annotated with IgnoreAllocations Reviewed By: cristianoc Differential Revision: D2979563 fb-gh-sync-id: 2699375 shipit-source-id: 2699375 --- .../infer/annotation/IgnoreAllocations.java | 19 +++++++++++++++++++ infer/src/checkers/annotations.ml | 4 ++++ infer/src/checkers/annotations.mli | 1 + infer/src/checkers/performanceCritical.ml | 5 +++-- .../java/checkers/NoAllocationExample.java | 11 +++++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 infer/annotations/com/facebook/infer/annotation/IgnoreAllocations.java diff --git a/infer/annotations/com/facebook/infer/annotation/IgnoreAllocations.java b/infer/annotations/com/facebook/infer/annotation/IgnoreAllocations.java new file mode 100644 index 000000000..efc80170a --- /dev/null +++ b/infer/annotations/com/facebook/infer/annotation/IgnoreAllocations.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2015 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.infer.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.CLASS) +@Target(ElementType.METHOD) +public @interface IgnoreAllocations {} diff --git a/infer/src/checkers/annotations.ml b/infer/src/checkers/annotations.ml index 283c9f9dc..b60891d86 100644 --- a/infer/src/checkers/annotations.ml +++ b/infer/src/checkers/annotations.ml @@ -111,6 +111,7 @@ let verify_annotation = "com.facebook.infer.annotation.Verify" let expensive = "Expensive" let performance_critical = "PerformanceCritical" let no_allocation = "NoAllocation" +let ignore_allocations = "IgnoreAllocations" let ia_is_nullable ia = ia_ends_with ia nullable @@ -158,6 +159,9 @@ let ia_is_performance_critical ia = let ia_is_no_allocation ia = ia_ends_with ia no_allocation +let ia_is_ignore_allocations ia = + ia_ends_with ia ignore_allocations + type annotation = | Nullable | Present diff --git a/infer/src/checkers/annotations.mli b/infer/src/checkers/annotations.mli index 538135d58..e2a697fb3 100644 --- a/infer/src/checkers/annotations.mli +++ b/infer/src/checkers/annotations.mli @@ -74,6 +74,7 @@ val ia_is_verify : Sil.item_annotation -> bool val ia_is_expensive : Sil.item_annotation -> bool val ia_is_performance_critical : Sil.item_annotation -> bool val ia_is_no_allocation : Sil.item_annotation -> bool +val ia_is_ignore_allocations : Sil.item_annotation -> bool val ia_iter : (Sil.annotation -> unit) -> Sil.item_annotation -> unit diff --git a/infer/src/checkers/performanceCritical.ml b/infer/src/checkers/performanceCritical.ml index beb78a226..1556aee1f 100644 --- a/infer/src/checkers/performanceCritical.ml +++ b/infer/src/checkers/performanceCritical.ml @@ -141,8 +141,9 @@ let method_allocates tenv pname = | Some { Specs.allocations } -> allocations <> [] | None -> false in - is_allocator tenv pname - || allocates () + not (check_method Annotations.ia_is_ignore_allocations pname) + && (is_allocator tenv pname + || allocates ()) let lookup_location pname = diff --git a/infer/tests/codetoanalyze/java/checkers/NoAllocationExample.java b/infer/tests/codetoanalyze/java/checkers/NoAllocationExample.java index 1d31ca1ac..967b132bf 100644 --- a/infer/tests/codetoanalyze/java/checkers/NoAllocationExample.java +++ b/infer/tests/codetoanalyze/java/checkers/NoAllocationExample.java @@ -10,6 +10,7 @@ package codetoanalyze.java.checkers; import com.facebook.infer.annotation.NoAllocation; +import com.facebook.infer.annotation.IgnoreAllocations; public class NoAllocationExample { @@ -50,4 +51,14 @@ public class NoAllocationExample { throwsException(); } + @IgnoreAllocations + void acceptableAllocation() { + new Object(); + } + + @NoAllocation + void onlyAllocatesInAcceptableWay() { + acceptableAllocation(); + } + }