Skip to content

Commit

Permalink
[annot] Add option to skip annotations from superclasses/interfaces
Browse files Browse the repository at this point in the history
Summary: Previously, if a class inherited from a base class or extended an interface, all of their annotations applied to all of the methods of the class. With this option, only "local" annotations are applied. That is, a function `foo` is only considered to be annotated by the interface or base class annotation, if `foo` is present in the base class / interface.

Reviewed By: ngorogiannis

Differential Revision: D56885801

fbshipit-source-id: 339858fa288018cbdd62a950057556e9784c6925
  • Loading branch information
hajduakos authored and facebook-github-bot committed May 3, 2024
1 parent 12fcbe1 commit 570f6d6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
9 changes: 5 additions & 4 deletions infer/man/man1/infer-analyze.txt
Expand Up @@ -634,10 +634,11 @@ ERLANG OPTIONS
ones). (Conversely: --erlang-reliability)

JAVA OPTIONS
--no-annotation-reachability-apply-class-annotations
Deactivates: Applies annotations of a class/interface to all its
methods (Conversely:
--annotation-reachability-apply-class-annotations)
--no-annotation-reachability-apply-superclass-annotations
Deactivates: Applies annotations from superclasses and interfaces
also on methods that are not overridden from the superclass or
interface. (Conversely:
--annotation-reachability-apply-superclass-annotations)

--annotation-reachability-custom-models json
Specify a map from annotations to lists of regexps to treat
Expand Down
9 changes: 5 additions & 4 deletions infer/man/man1/infer-full.txt
Expand Up @@ -70,10 +70,11 @@ OPTIONS
--no-annotation-reachability)
See also infer-analyze(1).

--no-annotation-reachability-apply-class-annotations
Deactivates: Applies annotations of a class/interface to all its
methods (Conversely:
--annotation-reachability-apply-class-annotations)
--no-annotation-reachability-apply-superclass-annotations
Deactivates: Applies annotations from superclasses and interfaces
also on methods that are not overridden from the superclass or
interface. (Conversely:
--annotation-reachability-apply-superclass-annotations)
See also infer-analyze(1).

--annotation-reachability-custom-models json
Expand Down
9 changes: 5 additions & 4 deletions infer/man/man1/infer.txt
Expand Up @@ -70,10 +70,11 @@ OPTIONS
--no-annotation-reachability)
See also infer-analyze(1).

--no-annotation-reachability-apply-class-annotations
Deactivates: Applies annotations of a class/interface to all its
methods (Conversely:
--annotation-reachability-apply-class-annotations)
--no-annotation-reachability-apply-superclass-annotations
Deactivates: Applies annotations from superclasses and interfaces
also on methods that are not overridden from the superclass or
interface. (Conversely:
--annotation-reachability-apply-superclass-annotations)
See also infer-analyze(1).

--annotation-reachability-custom-models json
Expand Down
18 changes: 14 additions & 4 deletions infer/src/base/Config.ml
Expand Up @@ -603,12 +603,22 @@ and analysis_schedule_file =
^ ResultsDirEntryName.get_path ~results_dir:"infer-out" AnalysisDependencyGraph )


and annotation_reachability_apply_class_annotations =
CLOpt.mk_bool ~long:"annotation-reachability-apply-class-annotations"
and _annotation_reachability_apply_class_annotations =
CLOpt.mk_bool ~long:""
~deprecated:["-annotation-reachability-apply-class-annotations"]
~deprecated_no:["-no-annotation-reachability-apply-class-annotations"]
~in_help:InferCommand.[(Analyze, manual_java)]
"Applies annotations of a class/interface to all its methods" ~default:true


and annotation_reachability_apply_superclass_annotations =
CLOpt.mk_bool ~long:"annotation-reachability-apply-superclass-annotations"
~in_help:InferCommand.[(Analyze, manual_java)]
"Applies annotations from superclasses and interfaces also on methods that are not overridden \
from the superclass or interface."
~default:true


and annotation_reachability_custom_models =
CLOpt.mk_json ~long:"annotation-reachability-custom-models"
~in_help:InferCommand.[(Analyze, manual_java)]
Expand Down Expand Up @@ -3826,8 +3836,8 @@ and abstract_pulse_models_for_erlang = !abstract_pulse_models_for_erlang

and analysis_schedule_file = !analysis_schedule_file

and annotation_reachability_apply_class_annotations =
!annotation_reachability_apply_class_annotations
and annotation_reachability_apply_superclass_annotations =
!annotation_reachability_apply_superclass_annotations


and annotation_reachability_custom_models = !annotation_reachability_custom_models
Expand Down
2 changes: 1 addition & 1 deletion infer/src/base/Config.mli
Expand Up @@ -130,7 +130,7 @@ val abstract_pulse_models_for_erlang : bool

val analysis_schedule_file : string option

val annotation_reachability_apply_class_annotations : bool
val annotation_reachability_apply_superclass_annotations : bool

val annotation_reachability_custom_models : Yojson.Safe.t

Expand Down
11 changes: 8 additions & 3 deletions infer/src/checkers/annotationReachability.ml
Expand Up @@ -62,9 +62,14 @@ let parse_custom_models () =


let check_attributes check tenv pname =
Config.annotation_reachability_apply_class_annotations
&& PatternMatch.Java.check_class_attributes check tenv pname
|| Annotations.pname_has_return_annot pname check
let proc_has_attribute = Annotations.pname_has_return_annot pname check in
let class_has_attribute =
( if Config.annotation_reachability_apply_superclass_annotations then
PatternMatch.Java.check_class_attributes
else PatternMatch.Java.check_current_class_attributes )
check tenv pname
in
class_has_attribute || proc_has_attribute


let method_overrides is_annotated tenv pname =
Expand Down
2 changes: 1 addition & 1 deletion infer/tests/codetoanalyze/java/annotreach/.inferconfig
Expand Up @@ -44,5 +44,5 @@
"codetoanalyze\\.java\\.checkers\\.CustomAnnotations\\.sanitizerDefinedInConfig"
]
},
"annotation-reachability-apply-class-annotations": true
"annotation-reachability-apply-superclass-annotations": true
}

0 comments on commit 570f6d6

Please sign in to comment.