-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: cxx-qt-gen: add support for passing in CfgEvaluator
- Loading branch information
1 parent
f5afefe
commit 457c3eb
Showing
17 changed files
with
381 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// SPDX-FileCopyrightText: CXX Authors | ||
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com> | ||
// SPDX-FileContributor: David Tolnay <dtolnay@gmail.com> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use crate::syntax::cfg::{parse_attribute, CfgExpr}; | ||
use cxx_gen::{CfgEvaluator, CfgResult}; | ||
use quote::quote; | ||
use syn::{Attribute, Error, LitStr}; | ||
|
||
pub(crate) fn try_eval_attributes( | ||
cfg_evaluator: &dyn CfgEvaluator, | ||
attrs: &[Attribute], | ||
) -> Result<bool, Error> { | ||
// Build a single CfgExpr from the Attributes | ||
let cfg_expr = attrs | ||
.iter() | ||
.map(parse_attribute) | ||
.collect::<Result<Vec<CfgExpr>, Error>>()? | ||
.into_iter() | ||
.reduce(|mut acc, e| { | ||
acc.merge(e); | ||
acc | ||
}); | ||
|
||
// Evaluate the CfgExpr against the CfgEvaluator | ||
if let Some(cfg_expr) = cfg_expr { | ||
try_eval(cfg_evaluator, &cfg_expr).map_err(|errs| { | ||
errs.into_iter() | ||
.reduce(|mut acc, e| { | ||
acc.combine(e); | ||
acc | ||
}) | ||
.expect("There should be at least one error") | ||
}) | ||
} else { | ||
Ok(true) | ||
} | ||
} | ||
|
||
fn try_eval(cfg_evaluator: &dyn CfgEvaluator, expr: &CfgExpr) -> Result<bool, Vec<Error>> { | ||
match expr { | ||
CfgExpr::Unconditional => Ok(true), | ||
CfgExpr::Eq(ident, string) => { | ||
let key = ident.to_string(); | ||
let value = string.as_ref().map(LitStr::value); | ||
match cfg_evaluator.eval(&key, value.as_deref()) { | ||
CfgResult::True => Ok(true), | ||
CfgResult::False => Ok(false), | ||
CfgResult::Undetermined { msg } => { | ||
let span = quote!(#ident #string); | ||
Err(vec![Error::new_spanned(span, msg)]) | ||
} | ||
} | ||
} | ||
CfgExpr::All(list) => { | ||
let mut all_errors = Vec::new(); | ||
for subexpr in list { | ||
match try_eval(cfg_evaluator, subexpr) { | ||
Ok(true) => {} | ||
Ok(false) => return Ok(false), | ||
Err(errors) => all_errors.extend(errors), | ||
} | ||
} | ||
if all_errors.is_empty() { | ||
Ok(true) | ||
} else { | ||
Err(all_errors) | ||
} | ||
} | ||
CfgExpr::Any(list) => { | ||
let mut all_errors = Vec::new(); | ||
for subexpr in list { | ||
match try_eval(cfg_evaluator, subexpr) { | ||
Ok(true) => return Ok(true), | ||
Ok(false) => {} | ||
Err(errors) => all_errors.extend(errors), | ||
} | ||
} | ||
if all_errors.is_empty() { | ||
Ok(false) | ||
} else { | ||
Err(all_errors) | ||
} | ||
} | ||
CfgExpr::Not(subexpr) => match try_eval(cfg_evaluator, subexpr) { | ||
Ok(value) => Ok(!value), | ||
Err(errors) => Err(errors), | ||
}, | ||
} | ||
} | ||
This file contains 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 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 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 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
Oops, something went wrong.