|
3 | 3 | use std::fs::File;
|
4 | 4 | use std::io::Write;
|
5 | 5 |
|
| 6 | +use pcs::utils::PlaceRepacker; |
6 | 7 | use std::cell::RefCell;
|
7 | 8 | use tracing::{debug, info, trace, warn};
|
8 | 9 | use tracing_subscriber;
|
@@ -65,15 +66,22 @@ fn should_check_body(body: &BodyWithBorrowckFacts<'_>) -> bool {
|
65 | 66 | // true
|
66 | 67 | }
|
67 | 68 |
|
68 |
| -fn run_pcs_on_all_fns<'tcx>(tcx: TyCtxt<'tcx>) { |
| 69 | +fn env_feature_enabled(feature: &str) -> bool { |
| 70 | + ["true", "1"].contains(&std::env::var(feature).unwrap_or_default().as_str()) |
| 71 | +} |
| 72 | + |
| 73 | +fn run_pcg_on_all_fns<'tcx>(tcx: TyCtxt<'tcx>) { |
69 | 74 | let mut item_names = vec![];
|
70 | 75 |
|
71 |
| - let vis_dir = if std::env::var("PCS_VISUALIZATION").unwrap_or_default() == "true" { |
| 76 | + let vis_dir = if env_feature_enabled("PCG_VISUALIZATION") { |
72 | 77 | Some("visualization/data")
|
73 | 78 | } else {
|
74 | 79 | None
|
75 | 80 | };
|
76 | 81 |
|
| 82 | + let emit_pcg_annotations = env_feature_enabled("PCG_EMIT_ANNOTATIONS"); |
| 83 | + let check_pcg_annotations = env_feature_enabled("PCG_CHECK_ANNOTATIONS"); |
| 84 | + |
77 | 85 | if let Some(path) = &vis_dir {
|
78 | 86 | if std::path::Path::new(path).exists() {
|
79 | 87 | std::fs::remove_dir_all(path)
|
@@ -109,30 +117,39 @@ fn run_pcs_on_all_fns<'tcx>(tcx: TyCtxt<'tcx>) {
|
109 | 117 | tcx,
|
110 | 118 | vis_dir.map(|dir| format!("{}/{}", dir, item_name)),
|
111 | 119 | );
|
112 |
| - if let Ok(source) = tcx.sess.source_map().span_to_snippet(body.body.span) { |
113 |
| - let expected_annotations = source |
114 |
| - .lines() |
115 |
| - .flat_map(|l| l.split("// PCG: ").nth(1)) |
116 |
| - .map(|l| l.trim()) |
117 |
| - .collect::<Vec<_>>(); |
118 |
| - if !expected_annotations.is_empty() { |
119 |
| - eprintln!("Expected annotations: {:?}", expected_annotations); |
120 |
| - let mut debug_lines = Vec::new(); |
121 |
| - for (idx, _) in body.body.basic_blocks.iter_enumerated() { |
122 |
| - let block = output.get_all_for_bb(idx); |
123 |
| - debug_lines.extend(block.debug_lines()); |
| 120 | + if emit_pcg_annotations || check_pcg_annotations { |
| 121 | + let mut debug_lines = Vec::new(); |
| 122 | + for (idx, _) in body.body.basic_blocks.iter_enumerated() { |
| 123 | + let block = output.get_all_for_bb(idx); |
| 124 | + debug_lines |
| 125 | + .extend(block.debug_lines(PlaceRepacker::new(&body.body, tcx))); |
| 126 | + } |
| 127 | + if emit_pcg_annotations { |
| 128 | + for line in debug_lines.iter() { |
| 129 | + eprintln!("// PCG: {}", line); |
124 | 130 | }
|
125 |
| - let missing_annotations = expected_annotations |
126 |
| - .iter() |
127 |
| - .filter(|a| !debug_lines.contains(&a.to_string())) |
128 |
| - .collect::<Vec<_>>(); |
129 |
| - if !missing_annotations.is_empty() { |
130 |
| - panic!("Missing annotations: {:?}", missing_annotations); |
| 131 | + } |
| 132 | + if check_pcg_annotations { |
| 133 | + if let Ok(source) = |
| 134 | + tcx.sess.source_map().span_to_snippet(body.body.span) |
| 135 | + { |
| 136 | + let expected_annotations = source |
| 137 | + .lines() |
| 138 | + .flat_map(|l| l.split("// PCG: ").nth(1)) |
| 139 | + .map(|l| l.trim()) |
| 140 | + .collect::<Vec<_>>(); |
| 141 | + if !expected_annotations.is_empty() { |
| 142 | + let missing_annotations = expected_annotations |
| 143 | + .iter() |
| 144 | + .filter(|a| !debug_lines.contains(&a.to_string())) |
| 145 | + .collect::<Vec<_>>(); |
| 146 | + if !missing_annotations.is_empty() { |
| 147 | + panic!("Missing annotations: {:?}", missing_annotations); |
| 148 | + } |
| 149 | + } |
| 150 | + } else { |
| 151 | + tracing::warn!("No source for function: {}", item_name); |
131 | 152 | }
|
132 |
| - // eprintln!("Debug lines:"); |
133 |
| - // for line in debug_lines { |
134 |
| - // eprintln!("{}", line); |
135 |
| - // } |
136 | 153 | }
|
137 | 154 | }
|
138 | 155 | }
|
@@ -175,7 +192,7 @@ impl driver::Callbacks for PcsCallbacks {
|
175 | 192 | _compiler: &Compiler,
|
176 | 193 | queries: &'tcx Queries<'tcx>,
|
177 | 194 | ) -> Compilation {
|
178 |
| - queries.global_ctxt().unwrap().enter(run_pcs_on_all_fns); |
| 195 | + queries.global_ctxt().unwrap().enter(run_pcg_on_all_fns); |
179 | 196 | if std::env::var("CARGO").is_ok() {
|
180 | 197 | Compilation::Continue
|
181 | 198 | } else {
|
|
0 commit comments