Skip to content

Commit

Permalink
Use cstr macro for ffi literal strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
upsuper committed Feb 14, 2018
1 parent 9f3ce51 commit 92feec1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 44 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ports/geckolib/Cargo.toml
Expand Up @@ -16,6 +16,7 @@ gecko_debug = ["style/gecko_debug"]
[dependencies]
atomic_refcell = "0.1"
cssparser = "0.23.0"
cstr = "0.1.2"
env_logger = {version = "0.4", default-features = false} # disable `regex` to reduce code size
libc = "0.2"
log = {version = "0.3.5", features = ["release_max_level_info"]}
Expand Down
89 changes: 45 additions & 44 deletions ports/geckolib/error_reporter.rs
Expand Up @@ -9,6 +9,7 @@
use cssparser::{CowRcStr, serialize_identifier, ToCss};
use cssparser::{SourceLocation, ParseError, ParseErrorKind, Token, BasicParseErrorKind};
use selectors::parser::SelectorParseErrorKind;
use std::ffi::CStr;
use std::ptr;
use style::error_reporting::{ParseErrorReporter, ContextualParseError};
use style::gecko_bindings::bindings::{Gecko_CreateCSSErrorReporter, Gecko_DestroyCSSErrorReporter};
Expand Down Expand Up @@ -76,7 +77,7 @@ enum Action {
trait ErrorHelpers<'a> {
fn error_data(self) -> (CowRcStr<'a>, ErrorKind<'a>);
fn error_params(self) -> ErrorParams<'a>;
fn to_gecko_message(&self) -> (Option<&'static [u8]>, &'static [u8], Action);
fn to_gecko_message(&self) -> (Option<&'static CStr>, &'static CStr, Action);
}

fn extract_error_param<'a>(err: ErrorKind<'a>) -> Option<ErrorString<'a>> {
Expand Down Expand Up @@ -226,48 +227,48 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
})
}

fn to_gecko_message(&self) -> (Option<&'static [u8]>, &'static [u8], Action) {
let (msg, action): (&[u8], Action) = match *self {
fn to_gecko_message(&self) -> (Option<&'static CStr>, &'static CStr, Action) {
let (msg, action): (&CStr, Action) = match *self {
ContextualParseError::UnsupportedPropertyDeclaration(
_, ParseError { kind: ParseErrorKind::Basic(BasicParseErrorKind::UnexpectedToken(_)), .. }
) |
ContextualParseError::UnsupportedPropertyDeclaration(
_, ParseError { kind: ParseErrorKind::Basic(BasicParseErrorKind::AtRuleInvalid(_)), .. }
) => {
(b"PEParseDeclarationDeclExpected\0", Action::Skip)
(cstr!("PEParseDeclarationDeclExpected"), Action::Skip)
}
ContextualParseError::UnsupportedPropertyDeclaration(
_, ParseError { kind: ParseErrorKind::Custom(ref err), .. }
) => {
match *err {
StyleParseErrorKind::InvalidColor(_, _) => {
return (Some(b"PEColorNotColor\0"),
b"PEValueParsingError\0", Action::Drop)
return (Some(cstr!("PEColorNotColor")),
cstr!("PEValueParsingError"), Action::Drop)
}
StyleParseErrorKind::InvalidFilter(_, _) => {
return (Some(b"PEExpectedNoneOrURLOrFilterFunction\0"),
b"PEValueParsingError\0", Action::Drop)
return (Some(cstr!("PEExpectedNoneOrURLOrFilterFunction")),
cstr!("PEValueParsingError"), Action::Drop)
}
StyleParseErrorKind::OtherInvalidValue(_) => {
(b"PEValueParsingError\0", Action::Drop)
(cstr!("PEValueParsingError"), Action::Drop)
}
_ => (b"PEUnknownProperty\0", Action::Drop)
_ => (cstr!("PEUnknownProperty"), Action::Drop)
}
}
ContextualParseError::UnsupportedPropertyDeclaration(..) =>
(b"PEUnknownProperty\0", Action::Drop),
(cstr!("PEUnknownProperty"), Action::Drop),
ContextualParseError::UnsupportedFontFaceDescriptor(..) =>
(b"PEUnknownFontDesc\0", Action::Skip),
(cstr!("PEUnknownFontDesc"), Action::Skip),
ContextualParseError::InvalidKeyframeRule(..) =>
(b"PEKeyframeBadName\0", Action::Nothing),
(cstr!("PEKeyframeBadName"), Action::Nothing),
ContextualParseError::UnsupportedKeyframePropertyDeclaration(..) =>
(b"PEBadSelectorKeyframeRuleIgnored\0", Action::Nothing),
(cstr!("PEBadSelectorKeyframeRuleIgnored"), Action::Nothing),
ContextualParseError::InvalidRule(
_, ParseError { kind: ParseErrorKind::Custom(
StyleParseErrorKind::UnexpectedTokenWithinNamespace(_)
), .. }
) => {
(b"PEAtNSUnexpected\0", Action::Nothing)
(cstr!("PEAtNSUnexpected"), Action::Nothing)
}
ContextualParseError::InvalidRule(
_, ParseError { kind: ParseErrorKind::Basic(BasicParseErrorKind::AtRuleInvalid(_)), .. }
Expand All @@ -277,84 +278,84 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
StyleParseErrorKind::UnsupportedAtRule(_)
), .. }
) => {
(b"PEUnknownAtRule\0", Action::Nothing)
(cstr!("PEUnknownAtRule"), Action::Nothing)
}
ContextualParseError::InvalidRule(_, ref err) => {
let prefix = match err.kind {
ParseErrorKind::Custom(StyleParseErrorKind::SelectorError(ref err)) => match *err {
SelectorParseErrorKind::UnexpectedTokenInAttributeSelector(_) => {
Some(&b"PEAttSelUnexpected\0"[..])
Some(cstr!("PEAttSelUnexpected"))
}
SelectorParseErrorKind::ExpectedBarInAttr(_) => {
Some(&b"PEAttSelNoBar\0"[..])
Some(cstr!("PEAttSelNoBar"))
}
SelectorParseErrorKind::BadValueInAttr(_) => {
Some(&b"PEAttSelBadValue\0"[..])
Some(cstr!("PEAttSelBadValue"))
}
SelectorParseErrorKind::NoQualifiedNameInAttributeSelector(_) => {
Some(&b"PEAttributeNameOrNamespaceExpected\0"[..])
Some(cstr!("PEAttributeNameOrNamespaceExpected"))
}
SelectorParseErrorKind::InvalidQualNameInAttr(_) => {
Some(&b"PEAttributeNameExpected\0"[..])
Some(cstr!("PEAttributeNameExpected"))
}
SelectorParseErrorKind::ExplicitNamespaceUnexpectedToken(_) => {
Some(&b"PETypeSelNotType\0"[..])
Some(cstr!("PETypeSelNotType"))
}
SelectorParseErrorKind::ExpectedNamespace(_) => {
Some(&b"PEUnknownNamespacePrefix\0"[..])
Some(cstr!("PEUnknownNamespacePrefix"))
}
SelectorParseErrorKind::EmptySelector => {
Some(&b"PESelectorGroupNoSelector\0"[..])
Some(cstr!("PESelectorGroupNoSelector"))
}
SelectorParseErrorKind::DanglingCombinator => {
Some(&b"PESelectorGroupExtraCombinator\0"[..])
Some(cstr!("PESelectorGroupExtraCombinator"))
}
SelectorParseErrorKind::UnsupportedPseudoClassOrElement(_) => {
Some(&b"PEPseudoSelUnknown\0"[..])
Some(cstr!("PEPseudoSelUnknown"))
}
SelectorParseErrorKind::PseudoElementExpectedColon(_) => {
Some(&b"PEPseudoSelEndOrUserActionPC\0"[..])
Some(cstr!("PEPseudoSelEndOrUserActionPC"))
}
SelectorParseErrorKind::NoIdentForPseudo(_) => {
Some(&b"PEPseudoClassArgNotIdent\0"[..])
Some(cstr!("PEPseudoClassArgNotIdent"))
}
SelectorParseErrorKind::PseudoElementExpectedIdent(_) => {
Some(&b"PEPseudoSelBadName\0"[..])
Some(cstr!("PEPseudoSelBadName"))
}
SelectorParseErrorKind::ClassNeedsIdent(_) => {
Some(&b"PEClassSelNotIdent\0"[..])
Some(cstr!("PEClassSelNotIdent"))
}
SelectorParseErrorKind::EmptyNegation => {
Some(&b"PENegationBadArg\0"[..])
Some(cstr!("PENegationBadArg"))
}
_ => None,
},
_ => None,
};
return (prefix, b"PEBadSelectorRSIgnored\0", Action::Nothing);
return (prefix, cstr!("PEBadSelectorRSIgnored"), Action::Nothing);
}
ContextualParseError::InvalidMediaRule(_, ref err) => {
let err: &[u8] = match err.kind {
let err: &CStr = match err.kind {
ParseErrorKind::Custom(StyleParseErrorKind::ExpectedIdentifier(..)) => {
b"PEGatherMediaNotIdent\0"
cstr!("PEGatherMediaNotIdent")
},
ParseErrorKind::Custom(StyleParseErrorKind::MediaQueryExpectedFeatureName(..)) => {
b"PEMQExpectedFeatureName\0"
cstr!("PEMQExpectedFeatureName")
},
ParseErrorKind::Custom(StyleParseErrorKind::MediaQueryExpectedFeatureValue) => {
b"PEMQExpectedFeatureValue\0"
cstr!("PEMQExpectedFeatureValue")
},
ParseErrorKind::Custom(StyleParseErrorKind::RangedExpressionWithNoValue) => {
b"PEMQNoMinMaxWithoutValue\0"
cstr!("PEMQNoMinMaxWithoutValue")
},
_ => {
b"PEDeclDropped\0"
cstr!("PEDeclDropped")
},
};
(err, Action::Nothing)
}
ContextualParseError::UnsupportedRule(..) =>
(b"PEDeclDropped\0", Action::Nothing),
(cstr!("PEDeclDropped"), Action::Nothing),
ContextualParseError::UnsupportedViewportDescriptorDeclaration(..) |
ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(..) |
ContextualParseError::InvalidCounterStyleWithoutSymbols(..) |
Expand All @@ -364,21 +365,21 @@ impl<'a> ErrorHelpers<'a> for ContextualParseError<'a> {
ContextualParseError::InvalidCounterStyleExtendsWithAdditiveSymbols |
ContextualParseError::UnsupportedFontFeatureValuesDescriptor(..) |
ContextualParseError::InvalidFontFeatureValuesRule(..) =>
(b"PEUnknownAtRule\0", Action::Skip),
(cstr!("PEUnknownAtRule"), Action::Skip),
ContextualParseError::UnsupportedValue(_, ParseError { ref kind, .. }) => {
match *kind {
ParseErrorKind::Custom(
StyleParseErrorKind::ValueError(
ValueParseErrorKind::InvalidColor(..)
)
) => (b"PEColorNotColor\0", Action::Nothing),
) => (cstr!("PEColorNotColor"), Action::Nothing),
_ => {
// Not the best error message, since we weren't parsing
// a declaration, just a value. But we don't produce
// UnsupportedValue errors other than InvalidColors
// currently.
debug_assert!(false, "should use a more specific error message");
(b"PEDeclDropped\0", Action::Nothing)
(cstr!("PEDeclDropped"), Action::Nothing)
}
}
}
Expand All @@ -392,8 +393,8 @@ impl ErrorReporter {
let (pre, name, action) = error.to_gecko_message();
let suffix = match action {
Action::Nothing => ptr::null(),
Action::Skip => b"PEDeclSkipped\0".as_ptr(),
Action::Drop => b"PEDeclDropped\0".as_ptr(),
Action::Skip => cstr!("PEDeclSkipped").as_ptr(),
Action::Drop => cstr!("PEDeclDropped").as_ptr(),
};
let params = error.error_params();
let param = params.main_param;
Expand Down
1 change: 1 addition & 0 deletions ports/geckolib/lib.rs
Expand Up @@ -4,6 +4,7 @@


extern crate cssparser;
#[macro_use] extern crate cstr;
extern crate env_logger;
extern crate libc;
#[macro_use] extern crate log;
Expand Down
1 change: 1 addition & 0 deletions ports/geckolib/tests/Cargo.toml
Expand Up @@ -14,6 +14,7 @@ doctest = false
[dependencies]
atomic_refcell = "0.1"
cssparser = "0.23.0"
cstr = "0.1.2"
env_logger = "0.4"
euclid = "0.16"
geckoservo = {path = "../../../ports/geckolib"}
Expand Down
1 change: 1 addition & 0 deletions ports/geckolib/tests/lib.rs
Expand Up @@ -13,6 +13,7 @@

extern crate atomic_refcell;
extern crate cssparser;
#[macro_use] extern crate cstr;
extern crate env_logger;
extern crate geckoservo;
#[macro_use] extern crate log;
Expand Down

0 comments on commit 92feec1

Please sign in to comment.