Skip to content

Commit d68f26c

Browse files
mvanbem-googcopybara-github
authored andcommitted
Record clang USR strings in C++ item reports
We can use this to unambiguously refer to C++ declarations across translation units. PiperOrigin-RevId: 873058970
1 parent 6abdec2 commit d68f26c

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

cc_bindings_from_rs/generate_bindings/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ fn item_name_for_error_report(db: &BindingsGenerator<'_>, def_id: DefId) -> erro
13231323
)
13241324
.into();
13251325
let id = ((def_id.index.as_u32() as u64) << 32) | def_id.krate.as_u32() as u64;
1326-
error_report::ItemName { name, id }
1326+
error_report::ItemName { name, id, unique_name: None }
13271327
}
13281328

13291329
/// Implementation of `BindingsGenerator::generate_item`.

common/error_report.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ pub struct ItemName {
228228
pub name: Rc<str>,
229229
/// Unique ID per target. (E.g. the address of the AST node.)
230230
pub id: u64,
231+
// A unique name for log aggregation purposes. For C++ items, this is a clang Unified Symbol
232+
// Resolution (USR) string.
233+
pub unique_name: Option<Rc<str>>,
231234
}
232235

233236
pub struct ItemScope<'a> {
@@ -328,7 +331,7 @@ impl ErrorReport {
328331
}
329332

330333
thread_local! {
331-
static DEFAULT_ITEM: ItemName = ItemName {name: Rc::from(""), id: 0};
334+
static DEFAULT_ITEM: ItemName = ItemName {name: Rc::from(""), id: 0, unique_name: None };
332335
}
333336

334337
impl ErrorReporting for ErrorReport {
@@ -367,7 +370,11 @@ impl ErrorReporting for ErrorReport {
367370
let mut map = self.map.borrow_mut();
368371
match map.entry(item.id) {
369372
Entry::Vacant(e) => {
370-
e.insert(ErrorReportEntry { name: item.name.clone(), ..Default::default() });
373+
e.insert(ErrorReportEntry {
374+
name: item.name.clone(),
375+
unique_name: item.unique_name.clone(),
376+
..Default::default()
377+
});
371378
}
372379
Entry::Occupied(e) => {
373380
assert_eq!(
@@ -420,6 +427,11 @@ pub struct ErrorReportEntry {
420427
/// (Note: can't use flagset, because it fails in recent rustc.)
421428
#[serde(default, skip_serializing_if = "is_default")]
422429
pub category: u32,
430+
431+
// A unique name for log aggregation purposes. For C++ items, this is a clang Unified Symbol
432+
// Resolution (USR) string.
433+
#[serde(default, skip_serializing_if = "Option::is_none")]
434+
pub unique_name: Option<Rc<str>>,
423435
}
424436

425437
fn is_default<T: Default + PartialEq>(value: &T) -> bool {
@@ -711,11 +723,15 @@ mod tests {
711723
fn error_report_item_name() {
712724
let report = ErrorReport::new();
713725
{
714-
let _scope = ItemScope::new(&report, ItemName { name: "foo".into(), id: 1 });
726+
let _scope =
727+
ItemScope::new(&report, ItemName { name: "foo".into(), id: 1, unique_name: None });
715728
report.report(&anyhow!("error in {}", "item 1"));
716729
}
717730
{
718-
let _scope = ItemScope::new(&report, ItemName { name: "bar".into(), id: 2 });
731+
let _scope = ItemScope::new(
732+
&report,
733+
ItemName { name: "bar".into(), id: 2, unique_name: Some("abc123".into()) },
734+
);
719735
report.report(&anyhow!("error in {}", "item 2"));
720736
}
721737

@@ -739,6 +755,7 @@ mod tests {
739755
"full_error": "error in item 2",
740756
},
741757
],
758+
"unique_name": "abc123",
742759
},
743760
]),
744761
);

rs_bindings_from_cc/generate_bindings/generate_comment_test.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ fn test_generate_unsupported_item_with_environment_production() -> Result<()> {
154154
let db = factory.make_db(Environment::Production);
155155
let _scope = error_report::ItemScope::new(
156156
db.errors(),
157-
error_report::ItemName { name: "test_item".into(), id: TEST_ITEM_ID.as_u64() },
157+
error_report::ItemName {
158+
name: "test_item".into(),
159+
id: TEST_ITEM_ID.as_u64(),
160+
unique_name: None,
161+
},
158162
);
159163
let actual = generate_unsupported(
160164
&db,
@@ -182,7 +186,11 @@ fn test_generate_unsupported_item_with_missing_source_loc() -> Result<()> {
182186
let db = factory.make_db(Environment::Production);
183187
let _scope = error_report::ItemScope::new(
184188
db.errors(),
185-
error_report::ItemName { name: "test_item".into(), id: TEST_ITEM_ID.as_u64() },
189+
error_report::ItemName {
190+
name: "test_item".into(),
191+
id: TEST_ITEM_ID.as_u64(),
192+
unique_name: None,
193+
},
186194
);
187195
let actual = generate_unsupported(
188196
&db,
@@ -207,7 +215,11 @@ fn test_generate_unsupported_item_with_environment_golden_test() -> Result<()> {
207215
let db = factory.make_db(Environment::GoldenTest);
208216
let _scope = error_report::ItemScope::new(
209217
db.errors(),
210-
error_report::ItemName { name: "test_item".into(), id: TEST_ITEM_ID.as_u64() },
218+
error_report::ItemName {
219+
name: "test_item".into(),
220+
id: TEST_ITEM_ID.as_u64(),
221+
unique_name: None,
222+
},
211223
);
212224
let actual = generate_unsupported(
213225
&db,

rs_bindings_from_cc/ir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,7 @@ impl Item {
20712071

20722072
fn error_item_name(&self, ir: &IR) -> error_report::ItemName {
20732073
let name = self.debug_name(ir);
2074-
error_report::ItemName { name, id: self.id().as_u64() }
2074+
error_report::ItemName { name, id: self.id().as_u64(), unique_name: self.unique_name() }
20752075
}
20762076

20772077
pub fn error_scope<'a>(

0 commit comments

Comments
 (0)