|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#[macro_use] |
| 12 | +extern crate rustc; |
| 13 | +extern crate syntax_pos; |
| 14 | + |
| 15 | +use rustc::session::Session; |
| 16 | +use syntax_pos::Span; |
| 17 | + |
| 18 | +pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) { |
| 19 | + let mut err_count = 0; |
| 20 | + { |
| 21 | + let mut say = |s: &str| { |
| 22 | + match (sp, sess) { |
| 23 | + (_, None) => bug!("{}", s), |
| 24 | + (Some(sp), Some(sess)) => sess.span_err(sp, s), |
| 25 | + (None, Some(sess)) => sess.err(s), |
| 26 | + } |
| 27 | + err_count += 1; |
| 28 | + }; |
| 29 | + if s.is_empty() { |
| 30 | + say("crate name must not be empty"); |
| 31 | + } |
| 32 | + for c in s.chars() { |
| 33 | + if c.is_alphanumeric() { continue } |
| 34 | + if c == '_' { continue } |
| 35 | + say(&format!("invalid character `{}` in crate name: `{}`", c, s)); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if err_count > 0 { |
| 40 | + sess.unwrap().abort_if_errors(); |
| 41 | + } |
| 42 | +} |
0 commit comments