Skip to content

Commit 6dc260a

Browse files
committed
Remove unnecessary clone of macro definitions
1 parent 73013e1 commit 6dc260a

File tree

2 files changed

+21
-42
lines changed

2 files changed

+21
-42
lines changed

src/ir/context.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::BindgenOptions;
2626
use crate::{Entry, HashMap, HashSet};
2727
use clang_sys;
2828
use proc_macro2::{Ident, Span};
29+
use rcc::InternedStr;
2930
use std::borrow::Cow;
3031
use std::cell::Cell;
3132
use std::collections::HashMap as StdHashMap;
@@ -350,8 +351,8 @@ pub struct BindgenContext {
350351
/// hard errors while parsing duplicated macros, as well to allow macro
351352
/// expression parsing.
352353
///
353-
/// This needs to be an std::HashMap because the cexpr API requires it.
354-
parsed_macros: StdHashMap<Vec<u8>, rcc::Literal>,
354+
/// This needs to be an std::HashMap because the rcc API requires it.
355+
parsed_macros: StdHashMap<rcc::InternedStr, rcc::Definition>,
355356

356357
/// The active replacements collected from replaces="xxx" annotations.
357358
replacements: HashMap<Vec<String>, ItemId>,
@@ -2033,25 +2034,25 @@ If you encounter an error missing from this list, please file an issue or a PR!"
20332034
}
20342035

20352036
/// Have we parsed the macro named `macro_name` already?
2036-
pub fn parsed_macro(&self, macro_name: &[u8]) -> bool {
2037-
self.parsed_macros.contains_key(macro_name)
2037+
pub fn parsed_macro(&self, macro_name: InternedStr) -> bool {
2038+
self.parsed_macros.contains_key(&macro_name)
20382039
}
20392040

20402041
/// Get the currently parsed macros.
20412042
pub fn parsed_macros(
20422043
&self,
2043-
) -> &StdHashMap<Vec<u8>, rcc::Literal> {
2044+
) -> &StdHashMap<InternedStr, rcc::Definition> {
20442045
debug_assert!(!self.in_codegen_phase());
20452046
&self.parsed_macros
20462047
}
20472048

20482049
/// Mark the macro named `macro_name` as parsed.
20492050
pub fn note_parsed_macro(
20502051
&mut self,
2051-
id: Vec<u8>,
2052+
id: InternedStr,
20522053
value: rcc::Literal,
20532054
) {
2054-
self.parsed_macros.insert(id, value);
2055+
self.parsed_macros.insert(id, rcc::Definition::Object(vec![rcc::Token::Literal(value)]));
20552056
}
20562057

20572058
/// Are we in the codegen phase?

src/ir/var.rs

+13-35
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::parse::{
1313
};
1414
use std::io;
1515
use std::collections::HashMap;
16+
use rcc::{InternedStr, Literal};
1617

1718
/// The type for a constant variable.
1819
#[derive(Debug)]
@@ -134,7 +135,6 @@ impl ClangSubItemParser for Var {
134135
cursor: clang::Cursor,
135136
ctx: &mut BindgenContext,
136137
) -> Result<ParseResult<Self>, ParseError> {
137-
use rcc::Literal;
138138
use clang_sys::*;
139139
match cursor.kind() {
140140
CXCursor_MacroDefinition => {
@@ -156,29 +156,22 @@ impl ClangSubItemParser for Var {
156156

157157
assert!(!id.is_empty(), "Empty macro name?");
158158

159-
let previously_defined = ctx.parsed_macro(&id);
159+
let previously_defined = ctx.parsed_macro(id);
160160

161161
// NB: It's important to "note" the macro even if the result is
162162
// not an integer, otherwise we might lose other kind of
163163
// derived macros.
164-
ctx.note_parsed_macro(id.clone(), value.clone());
164+
ctx.note_parsed_macro(id, value.clone());
165165

166166
if previously_defined {
167-
let name = String::from_utf8(id).unwrap();
168-
warn!("Duplicated macro definition: {}", name);
167+
warn!("Duplicated macro definition: {}", id);
169168
return Err(ParseError::Continue);
170169
}
171170

172-
// NOTE: Unwrapping, here and above, is safe, because the
173-
// identifier of a token comes straight from clang, and we
174-
// enforce utf8 there, so we should have already panicked at
175-
// this point.
176-
let name = String::from_utf8(id).unwrap();
177-
178171
let parse_int = |value| {
179172
let kind = ctx
180173
.parse_callbacks()
181-
.and_then(|c| c.int_macro(&name, value))
174+
.and_then(|c| c.int_macro(rcc::get_str!(id), value))
182175
.unwrap_or_else(|| {
183176
default_macro_constant_type(value)
184177
});
@@ -200,7 +193,7 @@ impl ClangSubItemParser for Var {
200193
ctx,
201194
);
202195
if let Some(callbacks) = ctx.parse_callbacks() {
203-
callbacks.str_macro(&name, &val);
196+
callbacks.str_macro(rcc::get_str!(id), &val);
204197
}
205198
(TypeKind::Pointer(char_ty), VarType::String(val))
206199
}
@@ -211,7 +204,7 @@ impl ClangSubItemParser for Var {
211204
let ty = Item::builtin_type(type_kind, true, ctx);
212205

213206
Ok(ParseResult::New(
214-
Var::new(name, None, ty, Some(val), true),
207+
Var::new(id.resolve_and_clone(), None, ty, Some(val), true),
215208
Some(cursor),
216209
))
217210
}
@@ -309,7 +302,7 @@ impl ClangSubItemParser for Var {
309302
fn parse_macro(
310303
ctx: &BindgenContext,
311304
cursor: &clang::Cursor,
312-
) -> Option<(Vec<u8>, rcc::Literal)> {
305+
) -> Option<(InternedStr, Literal)> {
313306
use rcc::Token;
314307

315308
// TODO: pass this in to rcc somehow
@@ -318,15 +311,15 @@ fn parse_macro(
318311
// TODO: remove(0) is expensive
319312
let ident = rcc_tokens.remove(0);
320313
let ident_str = match ident.data {
321-
Token::Id(id) => id.resolve_and_clone().into_bytes(),
314+
Token::Id(id) => id,
322315
_ => return None,
323316
};
324317
if ident_str.is_empty() {
325318
return None;
326319
}
327320

328321
// TODO: remove this clone (will need changes in rcc)
329-
if let Some(literal) = rcc_expr(rcc_tokens.clone().into_iter(), &parsed_macros) {
322+
if let Some(literal) = rcc_expr(rcc_tokens.clone().into_iter(), parsed_macros) {
330323
return Some((ident_str, literal));
331324
}
332325

@@ -337,37 +330,22 @@ fn parse_macro(
337330
// https://bugs.llvm.org//show_bug.cgi?id=9069
338331
// https://reviews.llvm.org/D26446
339332
rcc_tokens.pop()?;
340-
if let Some(literal) = rcc_expr(rcc_tokens.into_iter(), &parsed_macros) {
333+
if let Some(literal) = rcc_expr(rcc_tokens.into_iter(), parsed_macros) {
341334
Some((ident_str, literal))
342335
} else {
343336
None
344337
}
345338
}
346339

347-
fn rcc_expr(rcc_tokens: impl Iterator<Item = rcc::Locatable<rcc::Token>>, definitions: &HashMap<Vec<u8>, rcc::Literal>) -> Option<rcc::Literal> {
340+
fn rcc_expr(rcc_tokens: impl Iterator<Item = rcc::Locatable<rcc::Token>>, definitions: &HashMap<InternedStr, rcc::Definition>) -> Option<Literal> {
348341
use rcc::PreProcessor;
349342

350-
//let mut replacer = MacroReplacer::with_definitions(std::iter::empty().peekable(), &definitions);
351343
let mut rcc_tokens = rcc_tokens.peekable();
352344
let location = rcc_tokens.peek()?.location;
353-
let map = definitions.iter().map(|(k, v)| {
354-
(String::from_utf8_lossy(k).to_string().into(), rcc::Definition::Object(vec![rcc::Token::Literal(v.clone())]))
355-
}).collect();
356-
PreProcessor::cpp_expr(&map, rcc_tokens, location).ok()?.const_fold().ok()?.into_literal().ok()
357-
/*
358-
let first = rcc_tokens.next()?;
359-
360-
let mut parser = Parser::new(first, rcc_tokens.map(Result::<_, CompileError>::Ok), false);
361-
let expr = parser.expr().ok()?;
362-
let mut analyzer = PureAnalyzer::new();
363-
364-
analyzer.expr(expr).const_fold().ok()?.into_literal().ok()
365-
*/
345+
PreProcessor::cpp_expr(definitions, rcc_tokens, location).ok()?.const_fold().ok()?.into_literal().ok()
366346
}
367347

368348
fn parse_int_literal_tokens(cursor: &clang::Cursor) -> Option<i64> {
369-
use rcc::Literal;
370-
371349
let rcc_tokens = cursor.rcc_tokens().into_iter();
372350

373351
// TODO(emilio): We can try to parse other kinds of literals.

0 commit comments

Comments
 (0)