Skip to content

Commit

Permalink
Merge pull request #1334 from ahayzen-kdab/cxx-gen-allow-for-cfg-eval
Browse files Browse the repository at this point in the history
gen: allow for cfg_evaluator to be set in cxx_gen
  • Loading branch information
dtolnay committed Apr 8, 2024
2 parents 8f390ea + a430b55 commit 38b5842
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gen/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod syntax;

pub use crate::error::Error;
pub use crate::gen::include::{Include, HEADER};
pub use crate::gen::{GeneratedCode, Opt};
pub use crate::gen::{CfgEvaluator, CfgResult, GeneratedCode, Opt};
pub use crate::syntax::IncludeKind;
use proc_macro2::TokenStream;

Expand Down
18 changes: 14 additions & 4 deletions gen/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,32 @@ pub struct Opt {
/// Rust code from one shared object or executable depends on these C++
/// functions in another.
pub cxx_impl_annotations: Option<String>,
/// Optional [`CfgEvaluator`] for handling cfg attributes
pub cfg_evaluator: Box<dyn CfgEvaluator>,

pub(super) gen_header: bool,
pub(super) gen_implementation: bool,
pub(super) allow_dot_includes: bool,
pub(super) cfg_evaluator: Box<dyn CfgEvaluator>,
pub(super) doxygen: bool,
}

pub(super) trait CfgEvaluator {
/// An evaluator which parses cfg attributes
pub trait CfgEvaluator {
/// For a given cfg name and value return a [`CfgResult`] indicating if it's enabled
fn eval(&self, name: &str, value: Option<&str>) -> CfgResult;
}

pub(super) enum CfgResult {
/// Results of a [`CfgEvaluator`]
pub enum CfgResult {
/// cfg option is enabled
True,
/// cfg option is disabled
False,
Undetermined { msg: String },
/// cfg option is not enabled or disabled
Undetermined {
/// Custom message explaining why the cfg option is undetermined
msg: String,
},
}

/// Results of code generation.
Expand Down

0 comments on commit 38b5842

Please sign in to comment.