diff options
-rw-r--r-- | components/dom_struct/lib.rs | 1 | ||||
-rw-r--r-- | components/script/Cargo.toml | 3 | ||||
-rw-r--r-- | components/script_plugins/Cargo.toml | 1 | ||||
-rw-r--r-- | components/script_plugins/lib.rs | 7 | ||||
-rw-r--r-- | components/script_plugins/webidl_must_inherit.rs | 133 |
5 files changed, 144 insertions, 1 deletions
diff --git a/components/dom_struct/lib.rs b/components/dom_struct/lib.rs index 2fdac787706..59a3cf8072c 100644 --- a/components/dom_struct/lib.rs +++ b/components/dom_struct/lib.rs @@ -19,6 +19,7 @@ pub fn dom_struct(args: TokenStream, input: TokenStream) -> TokenStream { #[derive(DenyPublicFields, DomObject, JSTraceable, MallocSizeOf)] #[must_root] #[repr(C)] + #[webidl] }; // Work around https://github.com/rust-lang/rust/issues/46489 diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index 8076d77b7bb..c6dc04c6004 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -17,7 +17,8 @@ debugmozjs = ['js/debugmozjs'] profilemozjs = ['js/profilemozjs'] unstable = [] unrooted_must_root_lint = ["script_plugins/unrooted_must_root_lint"] -default = ["unrooted_must_root_lint"] +webidl_lint = ["script_plugins/webidl_lint"] +default = ["unrooted_must_root_lint", "webidl_lint"] webgl_backtrace = ["backtrace", "canvas_traits/webgl_backtrace"] js_backtrace = ["backtrace"] diff --git a/components/script_plugins/Cargo.toml b/components/script_plugins/Cargo.toml index c6bc3eb2250..235303d2983 100644 --- a/components/script_plugins/Cargo.toml +++ b/components/script_plugins/Cargo.toml @@ -11,3 +11,4 @@ plugin = true [features] unrooted_must_root_lint = [] +webidl_lint = [] diff --git a/components/script_plugins/lib.rs b/components/script_plugins/lib.rs index 36c81ce6239..eda133d4b7b 100644 --- a/components/script_plugins/lib.rs +++ b/components/script_plugins/lib.rs @@ -31,6 +31,9 @@ use syntax::feature_gate::AttributeType::Whitelisted; #[cfg(feature = "unrooted_must_root_lint")] mod unrooted_must_root; +#[cfg(feature = "webidl_lint")] +mod webidl_must_inherit; + /// Utilities for writing plugins #[cfg(feature = "unrooted_must_root_lint")] mod utils; @@ -40,7 +43,11 @@ pub fn plugin_registrar(reg: &mut Registry) { #[cfg(feature = "unrooted_must_root_lint")] reg.register_late_lint_pass(Box::new(unrooted_must_root::UnrootedPass::new())); + #[cfg(feature = "webidl_lint")] + reg.register_late_lint_pass(Box::new(webidl_must_inherit::WebIdlPass::new())); + reg.register_attribute("allow_unrooted_interior".to_string(), Whitelisted); reg.register_attribute("allow_unrooted_in_rc".to_string(), Whitelisted); reg.register_attribute("must_root".to_string(), Whitelisted); + reg.register_attribute("webidl".to_string(), Whitelisted); } diff --git a/components/script_plugins/webidl_must_inherit.rs b/components/script_plugins/webidl_must_inherit.rs new file mode 100644 index 00000000000..96ef9e75db2 --- /dev/null +++ b/components/script_plugins/webidl_must_inherit.rs @@ -0,0 +1,133 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use rustc::hir::{self, HirId}; +use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass}; +use rustc::ty; +use std::env; + +use std::io; +use std::path; +use syntax::ast; +declare_lint!( + WEBIDL_INHERIT_CORRECT, + Deny, + "Warn and report usage of incorrect webidl inheritance" +); + +pub struct WebIdlPass; + +impl WebIdlPass { + pub fn new() -> WebIdlPass { + WebIdlPass + } +} + +fn get_typ_name(typ: String) -> Option<String> { + if let Some(i) = typ.rfind(':') { + return Some(typ[i + 1..].to_string()); + } + None +} + +fn get_webidl_path(struct_name: &str) -> io::Result<path::PathBuf> { + let mut dir = env::current_dir()?; + dir.push("components/script/dom/webidls/"); + dir.push(format!("{}.webidl", struct_name)); + + return Ok(dir); +} + +/// Checks if a type is unrooted or contains any owned unrooted types +fn is_webidl_ty(cx: &LateContext, ty: &ty::TyS) -> bool { + let mut ret = false; + ty.maybe_walk(|t| { + match t.sty { + ty::Adt(did, _substs) => { + if cx.tcx.has_attr(did.did, "webidl") { + ret = true; + } + false + }, + ty::Ref(..) => false, // don't recurse down &ptrs + ty::RawPtr(..) => false, // don't recurse down *ptrs + ty::FnDef(..) | ty::FnPtr(_) => false, + _ => true, + } + }); + ret +} + +fn check_webidl(name: &str, parent_name: Option<String>) -> io::Result<()> { + let path = get_webidl_path(&name)?; + println!("struct_webidl_path: {:?}", &path); + + if let Some(parent) = parent_name { + let parent_path = get_webidl_path(&parent)?; + println!("parent_path: {:?}", &parent_path); + } + + Ok(()) +} + +impl LintPass for WebIdlPass { + fn name(&self) -> &'static str { + "ServoWebIDLPass" + } + + fn get_lints(&self) -> LintArray { + lint_array!(WEBIDL_INHERIT_CORRECT) + } +} + +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for WebIdlPass { + fn check_struct_def( + &mut self, + cx: &LateContext<'a, 'tcx>, + def: &'tcx hir::VariantData, + n: ast::Name, + _gen: &'tcx hir::Generics, + id: HirId, + ) { + let def_id = cx.tcx.hir().local_def_id_from_hir_id(id); + if !is_webidl_ty(cx, cx.tcx.type_of(def_id)) { + return; + } + + let item = match cx.tcx.hir().get_by_hir_id(id) { + hir::Node::Item(item) => item, + _ => cx + .tcx + .hir() + .expect_item_by_hir_id(cx.tcx.hir().get_parent_item(id)), + }; + + let struct_name = n.to_string(); + println!("struct_name: {:?}", struct_name); + + let mut parent_typ_name: Option<String> = None; + for ref field in def.fields() { + let def_id = cx.tcx.hir().local_def_id_from_hir_id(field.hir_id); + let ty = cx.tcx.type_of(def_id); + let typ = ty.to_string(); + if let Some(typ_name) = get_typ_name(ty.to_string()) { + parent_typ_name = Some(typ_name); + break; + } else { + cx.span_lint(WEBIDL_INHERIT_CORRECT, field.span, "Cannot get type name"); + } + + // Only first field is relevant. + break; + } + + // TODO Open and parse corresponding webidl file. + cx.span_lint(WEBIDL_INHERIT_CORRECT, item.ident.span, "WEBIDL present."); + + match check_webidl(&struct_name, parent_typ_name) { + Ok(()) => {}, + Err(e) => println!("ERRORRR: {:?}", e), + }; + } +} |