-
Notifications
You must be signed in to change notification settings - Fork 31
add whitelist feature to dpdusage #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hendriktews
wants to merge
1
commit into
rocq-community:master
Choose a base branch
from
hendriktews:whitelist
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,44 @@ module Node = Dpd_compute.Node | |
|
||
let version_option = ref false | ||
let print_path_option = ref true | ||
let strict_whitelist = ref false | ||
|
||
let threshold_option = ref 0 | ||
|
||
module Ident_set = Set.Make(String) | ||
|
||
let whitelisted = ref Ident_set.empty | ||
|
||
let add_whitelisted_item s = | ||
whitelisted := Ident_set.add s !whitelisted | ||
|
||
let remove_surrounding_white_space s = | ||
let i = ref 0 in | ||
let j = ref (String.length s - 1) in | ||
while !i <= !j && (s.[!i] = ' ' || s.[!i] = '\t') do | ||
incr i | ||
done; | ||
while !i <= !j && (s.[!j] = ' ' || s.[!j] = '\t') do | ||
decr j | ||
done; | ||
String.sub s !i (!j - !i + 1) | ||
|
||
let add_whitelist_file f = | ||
let ic = | ||
try open_in f | ||
with Sys_error m -> | ||
Dpd_compute.error "Whitelist file %s: %[email protected].@." f m; | ||
exit 1 | ||
in | ||
try while true do | ||
let line = input_line ic in | ||
let line = remove_surrounding_white_space line in | ||
if String.length line > 0 && line.[0] <> '#' | ||
then add_whitelisted_item line | ||
done | ||
with End_of_file -> | ||
close_in ic | ||
|
||
let spec_args = [ | ||
("-with-defs", Arg.Set Dpd_compute.with_defs, | ||
": show everything (default)"); | ||
|
@@ -32,6 +67,14 @@ let spec_args = [ | |
": print path (default)"); | ||
("-without-path", Arg.Clear print_path_option, | ||
": do not print path"); | ||
("-whitelist", Arg.String add_whitelisted_item, | ||
"path:ident : whitelist path:ident"); | ||
("-whitelist-file", Arg.String add_whitelist_file, | ||
"file : whitelist content of file"); | ||
("-strict-whitelist", Arg.Set strict_whitelist, | ||
": warn about whitelisted items above threshold"); | ||
("-liberal-whitelist", Arg.Clear strict_whitelist, | ||
": don't warn about whitelisted items (default)"); | ||
("-v", Arg.Set version_option, | ||
": print version and exit"); | ||
] | ||
|
@@ -40,23 +83,44 @@ let print_usage g t = | |
let print_node n = | ||
let d = (G.in_degree g n) in | ||
if d <= t then | ||
if !print_path_option then | ||
let prefix = match Node.get_attrib "path" n with | ||
let prefix = match Node.get_attrib "path" n with | ||
| None -> "" | ||
| Some d -> d^":" | ||
in Format.printf "%s%s\t(%d)\n" prefix (Node.name n) d | ||
| Some p -> p | ||
in | ||
let long_id = prefix ^ ":" ^ (Node.name n) in | ||
if Ident_set.mem long_id !whitelisted then | ||
begin | ||
if !strict_whitelist then | ||
whitelisted := Ident_set.remove long_id !whitelisted | ||
end | ||
else | ||
Format.printf "%s\t(%d)\n" (Node.name n) d | ||
if !print_path_option then | ||
Format.printf "%s:%s\t(%d)\n" prefix (Node.name n) d | ||
else | ||
Format.printf "%s\t(%d)\n" (Node.name n) d | ||
in | ||
G.iter_vertex print_node g | ||
|
||
let warn_on_unused_whitelite threshold = | ||
if not (Ident_set.is_empty !whitelisted) then begin | ||
Dpd_compute.warning | ||
"The following whitelisted items do not \ | ||
appear@ or are above threshold %d:@." | ||
threshold; | ||
Ident_set.iter | ||
(Format.printf "\t%s@.") | ||
!whitelisted | ||
end | ||
|
||
let do_file _ f = | ||
try | ||
Dpd_compute.feedback "read file %s@." f; | ||
let g = Dpd_lex.read f in | ||
let g = Dpd_compute.build_graph g in | ||
Dpd_compute.simplify_graph g; | ||
print_usage g !threshold_option | ||
print_usage g !threshold_option; | ||
if !strict_whitelist then | ||
warn_on_unused_whitelite !threshold_option | ||
with Dpd_compute.Error msg -> Dpd_compute.error "%s@." msg | ||
|
||
let main () = | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be better to use
String.trim
.