]> git.lizzy.rs Git - rust.git/commitdiff
Switch logic to Span instead of HashMap
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Tue, 17 Jan 2017 22:54:51 +0000 (23:54 +0100)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Fri, 3 Feb 2017 10:08:20 +0000 (11:08 +0100)
src/librustdoc/clean/mod.rs
src/librustdoc/html/markdown.rs
src/librustdoc/markdown.rs
src/librustdoc/test.rs
src/libsyntax/attr.rs
src/libsyntax/print/pprust.rs
src/libsyntax/std_inject.rs
src/libsyntax/test.rs

index cdb24a56367fc39749f20d213cf0faadd62ebbe4..fe2edcad25c20d84bdea3f08805e483231f8f4e5 100644 (file)
@@ -521,17 +521,22 @@ fn has_word(self, word: &str) -> bool {
 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug, Default)]
 pub struct Attributes {
     pub doc_strings: Vec<String>,
-    pub other_attrs: Vec<ast::Attribute>
+    pub other_attrs: Vec<ast::Attribute>,
+    pub span: Option<syntax_pos::Span>,
 }
 
 impl Attributes {
     pub fn from_ast(attrs: &[ast::Attribute]) -> Attributes {
         let mut doc_strings = vec![];
+        let mut sp = None;
         let other_attrs = attrs.iter().filter_map(|attr| {
             attr.with_desugared_doc(|attr| {
                 if let Some(value) = attr.value_str() {
                     if attr.check_name("doc") {
                         doc_strings.push(value.to_string());
+                        if sp.is_none() {
+                            sp = Some(attr.span);
+                        }
                         return None;
                     }
                 }
@@ -541,7 +546,8 @@ pub fn from_ast(attrs: &[ast::Attribute]) -> Attributes {
         }).collect();
         Attributes {
             doc_strings: doc_strings,
-            other_attrs: other_attrs
+            other_attrs: other_attrs,
+            span: sp,
         }
     }
 
index c35342e25b523d715734370b005cb648a5a0b6c4..e8ff8930bdd7d32eb16557c3bbfce9822eccca34 100644 (file)
@@ -429,12 +429,12 @@ fn dont_escape(c: u8) -> bool {
     }
 }
 
-pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
+pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, start_line: usize) {
     extern fn block(_ob: *mut hoedown_buffer,
                     text: *const hoedown_buffer,
                     lang: *const hoedown_buffer,
                     data: *const hoedown_renderer_data,
-                    _: libc::size_t) {
+                    line: libc::size_t) {
         unsafe {
             if text.is_null() { return }
             let block_info = if lang.is_null() {
@@ -453,11 +453,12 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
                 stripped_filtered_line(l).unwrap_or(l)
             });
             let text = lines.collect::<Vec<&str>>().join("\n");
+            let line = tests.get_line() + line;
             tests.add_test(text.to_owned(),
                            block_info.should_panic, block_info.no_run,
                            block_info.ignore, block_info.test_harness,
                            block_info.compile_fail, block_info.error_codes,
-                           block_info.original);
+                           line);
         }
     }
 
@@ -478,6 +479,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
         }
     }
 
+    tests.set_line(start_line);
     unsafe {
         let ob = hoedown_buffer_new(DEF_OUNIT);
         let renderer = hoedown_html_renderer_new(0, 0);
index 50f748deb0b9e840a4d7d407bd6773e9fa1d9081..49497957be980734fe03a7e29713a4022b4dd319 100644 (file)
@@ -154,8 +154,9 @@ pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
     let mut opts = TestOptions::default();
     opts.no_crate_inject = true;
     let mut collector = Collector::new(input.to_string(), cfgs, libs, externs,
-                                       true, opts, maybe_sysroot, &input_str, "input".to_string());
-    find_testable_code(&input_str, &mut collector);
+                                       true, opts, maybe_sysroot, "input".to_string(),
+                                       None);
+    find_testable_code(&input_str, &mut collector, 0);
     test_args.insert(0, "rustdoctest".to_string());
     testing::test_main(&test_args, collector.tests);
     0
index bad2986ab628b09b06cae03e306dbeb5c421e767..d5451d6a6c37d4e4a4877324da3c871aa93ba1e2 100644 (file)
@@ -8,10 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use std::collections::HashMap;
 use std::env;
 use std::ffi::OsString;
-use std::fs::File;
 use std::io::prelude::*;
 use std::io;
 use std::path::PathBuf;
@@ -39,6 +37,7 @@
 use syntax::ast;
 use syntax::codemap::CodeMap;
 use syntax::feature_gate::UnstableFeatures;
+use syntax_pos::{BytePos, DUMMY_SP, Pos};
 use errors;
 use errors::emitter::ColorConfig;
 
@@ -81,7 +80,7 @@ pub fn run(input: &str,
     let _ignore = dep_graph.in_ignore();
     let cstore = Rc::new(CStore::new(&dep_graph));
     let mut sess = session::build_session_(
-        sessopts, &dep_graph, Some(input_path.clone()), handler, codemap, cstore.clone(),
+        sessopts, &dep_graph, Some(input_path.clone()), handler, codemap.clone(), cstore.clone(),
     );
     rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
     sess.parse_sess.config =
@@ -99,14 +98,6 @@ pub fn run(input: &str,
     });
     let opts = scrape_test_config(hir_forest.krate());
     let filename = input_path.to_str().unwrap_or("").to_owned();
-    let mut f = match File::open(input_path) {
-        Ok(f) => f,
-        _ => return 1,
-    };
-    let mut file_content = String::new();
-    if let Err(_) = f.read_to_string(&mut file_content) {
-        return 1;
-    }
     let mut collector = Collector::new(crate_name,
                                        cfgs,
                                        libs,
@@ -114,8 +105,8 @@ pub fn run(input: &str,
                                        false,
                                        opts,
                                        maybe_sysroot,
-                                       &file_content,
-                                       filename);
+                                       filename,
+                                       Some(codemap));
 
     {
         let dep_graph = DepGraph::new(false);
@@ -399,27 +390,15 @@ pub struct Collector {
     cratename: String,
     opts: TestOptions,
     maybe_sysroot: Option<PathBuf>,
-    code_blocks: HashMap<String, Vec<u32>>,
     filename: String,
+    start_line: usize,
+    codemap: Option<Rc<CodeMap>>,
 }
 
 impl Collector {
     pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
                use_headers: bool, opts: TestOptions, maybe_sysroot: Option<PathBuf>,
-               file_content: &str, filename: String) -> Collector {
-        let mut line_number = 1;
-        let mut block_lines = HashMap::new();
-        for (pos, block) in file_content.split("```").enumerate() {
-            if (pos & 1) != 0 {
-                let key = format!("{}", block.replace("/// ", "").replace("//!", ""));
-                if !block_lines.contains_key(&key) {
-                    block_lines.insert(key.clone(), Vec::new());
-                }
-                block_lines.get_mut(&key).unwrap().push(line_number);
-            }
-            line_number += block.lines().count() as u32 - 1;
-        }
-
+               filename: String, codemap: Option<Rc<CodeMap>>) -> Collector {
         Collector {
             tests: Vec::new(),
             names: Vec::new(),
@@ -432,30 +411,17 @@ pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: Ext
             cratename: cratename,
             opts: opts,
             maybe_sysroot: maybe_sysroot,
-            code_blocks: block_lines,
             filename: filename,
+            start_line: 0,
+            codemap: codemap,
         }
     }
 
-    fn get_line_from_key(&mut self, key: &String) -> u32 {
-        let (line, need_removal) = if let Some(l) = self.code_blocks.get_mut(key) {
-            let need_removal = l.len() > 1;
-            (l.pop().unwrap_or(1), need_removal)
-        } else {
-            return 1;
-        };
-        if need_removal {
-            self.code_blocks.remove(key);
-        }
-        line
-    }
-
     pub fn add_test(&mut self, test: String,
                     should_panic: bool, no_run: bool, should_ignore: bool,
                     as_test_harness: bool, compile_fail: bool, error_codes: Vec<String>,
-                    original: String) {
-        let line_number = self.get_line_from_key(&format!("{}\n{}\n", original, test));
-        let name = format!("{} - line {}", self.filename, line_number);
+                    line: usize) {
+        let name = format!("{} - line {}", self.filename, line);
         self.cnt += 1;
         let cfgs = self.cfgs.clone();
         let libs = self.libs.clone();
@@ -499,6 +465,18 @@ pub fn add_test(&mut self, test: String,
         });
     }
 
+    pub fn get_line(&self) -> usize {
+        if let Some(ref codemap) = self.codemap{
+            codemap.lookup_char_pos(BytePos(self.start_line as u32)).line - 1
+        } else {
+            self.start_line
+        }
+    }
+
+    pub fn set_line(&mut self, start_line: usize) {
+        self.start_line = start_line;
+    }
+
     pub fn register_header(&mut self, name: &str, level: u32) {
         if self.use_headers && level == 1 {
             // we use these headings as test names, so it's good if
@@ -539,7 +517,8 @@ fn visit_testable<F: FnOnce(&mut Self)>(&mut self,
         attrs.unindent_doc_comments();
         if let Some(doc) = attrs.doc_value() {
             self.collector.cnt = 0;
-            markdown::find_testable_code(doc, self.collector);
+            markdown::find_testable_code(doc, self.collector,
+                                         attrs.span.unwrap_or(DUMMY_SP).lo.to_usize());
         }
 
         nested(self);
index 099ca8f02d2b20cc63c307d04f859338fa5fd032..455a6a0fb32e21ae736fd81a50579dabdb09f976 100644 (file)
@@ -280,9 +280,9 @@ pub fn with_desugared_doc<T, F>(&self, f: F) -> T where
                 Symbol::intern("doc"),
                 Symbol::intern(&strip_doc_comment_decoration(&comment.as_str())));
             if self.style == ast::AttrStyle::Outer {
-                f(&mk_attr_outer(self.id, meta))
+                f(&mk_attr_outer(self.span, self.id, meta))
             } else {
-                f(&mk_attr_inner(self.id, meta))
+                f(&mk_attr_inner(self.span, self.id, meta))
             }
         } else {
             f(self)
@@ -339,8 +339,8 @@ pub fn mk_attr_id() -> AttrId {
 }
 
 /// Returns an inner attribute with the given value.
-pub fn mk_attr_inner(id: AttrId, item: MetaItem) -> Attribute {
-    mk_spanned_attr_inner(DUMMY_SP, id, item)
+pub fn mk_attr_inner(span: Span, id: AttrId, item: MetaItem) -> Attribute {
+    mk_spanned_attr_inner(span, id, item)
 }
 
 /// Returns an innter attribute with the given value and span.
@@ -356,8 +356,8 @@ pub fn mk_spanned_attr_inner(sp: Span, id: AttrId, item: MetaItem) -> Attribute
 
 
 /// Returns an outer attribute with the given value.
-pub fn mk_attr_outer(id: AttrId, item: MetaItem) -> Attribute {
-    mk_spanned_attr_outer(DUMMY_SP, id, item)
+pub fn mk_attr_outer(span: Span, id: AttrId, item: MetaItem) -> Attribute {
+    mk_spanned_attr_outer(span, id, item)
 }
 
 /// Returns an outer attribute with the given value and span.
index e75671d27aa248eaed57ef87d1a51c0b9d90bba5..9d7f57b2af934b42be9ef7d0dbd0a20a1bc21efd 100644 (file)
@@ -27,6 +27,7 @@
 use ptr::P;
 use std_inject;
 use symbol::{Symbol, keywords};
+use syntax_pos::DUMMY_SP;
 use tokenstream::{self, TokenTree};
 
 use rustc_i128::i128;
@@ -118,12 +119,12 @@ pub fn print_crate<'a>(cm: &'a CodeMap,
         // #![feature(prelude_import)]
         let prelude_import_meta = attr::mk_list_word_item(Symbol::intern("prelude_import"));
         let list = attr::mk_list_item(Symbol::intern("feature"), vec![prelude_import_meta]);
-        let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(), list);
+        let fake_attr = attr::mk_attr_inner(DUMMY_SP, attr::mk_attr_id(), list);
         s.print_attribute(&fake_attr)?;
 
         // #![no_std]
         let no_std_meta = attr::mk_word_item(Symbol::intern("no_std"));
-        let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(), no_std_meta);
+        let fake_attr = attr::mk_attr_inner(DUMMY_SP, attr::mk_attr_id(), no_std_meta);
         s.print_attribute(&fake_attr)?;
     }
 
index 68d807b24a788547bc8516c68dbffd496e4ce353..4a2dfaf61247cb1648e5ea6ce0a3ffb049556421 100644 (file)
@@ -56,7 +56,8 @@ pub fn maybe_inject_crates_ref(sess: &ParseSess,
     let crate_name = Symbol::intern(&alt_std_name.unwrap_or(name.to_string()));
 
     krate.module.items.insert(0, P(ast::Item {
-        attrs: vec![attr::mk_attr_outer(attr::mk_attr_id(),
+        attrs: vec![attr::mk_attr_outer(DUMMY_SP,
+                                        attr::mk_attr_id(),
                                         attr::mk_word_item(Symbol::intern("macro_use")))],
         vis: ast::Visibility::Inherited,
         node: ast::ItemKind::ExternCrate(Some(crate_name)),
index 74ec33fdd2a85eeea381bbdcd11f842e364e91fc..dd2756cd2b22c95aa42b715c59a095488ab4bf41 100644 (file)
@@ -195,7 +195,8 @@ fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
                     let dead_code_str = Symbol::intern("dead_code");
                     let word_vec = vec![attr::mk_list_word_item(dead_code_str)];
                     let allow_dead_code_item = attr::mk_list_item(allow_str, word_vec);
-                    let allow_dead_code = attr::mk_attr_outer(attr::mk_attr_id(),
+                    let allow_dead_code = attr::mk_attr_outer(DUMMY_SP,
+                                                              attr::mk_attr_id(),
                                                               allow_dead_code_item);
 
                     ast::Item {