]> git.lizzy.rs Git - rust.git/commitdiff
repair macro docs
authorJohn Clements <clements@racket-lang.org>
Thu, 17 Jul 2014 16:45:31 +0000 (09:45 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 21 Jul 2014 16:54:07 +0000 (09:54 -0700)
In f1ad425199b0d89dab275a8c8f6f29a73d316f70, I changed the handling
of macros, to prevent macro invocations from occurring in fully expanded
source. Instead, I added a side table. It contained only the
spans of the macros, because this was the only information required
in order to make macro export work.

However, librustdoc was also affected by this change, since it
extracts macro information in a similar way. As a result of the earlier
change, exported macros were no longer documented.

In order to repair this, I've adjusted the side table to contain whole
items, rather than just the spans.

src/librustc/metadata/encoder.rs
src/librustdoc/visit_ast.rs
src/libsyntax/ast.rs
src/libsyntax/ext/base.rs
src/libsyntax/ext/expand.rs
src/libsyntax/fold.rs

index 439455ff3d15ce3291c07acd9955b1fd852266bd..7997af1ee5e11980e971a569c5b4548d8db70260 100644 (file)
@@ -1618,8 +1618,8 @@ fn encode_macro_defs(ecx: &EncodeContext,
                      krate: &Crate,
                      ebml_w: &mut Encoder) {
     ebml_w.start_tag(tag_exported_macros);
-    for span in krate.exported_macros.iter() {
-        encode_macro_def(ecx, ebml_w, span);
+    for item in krate.exported_macros.iter() {
+        encode_macro_def(ecx, ebml_w, &item.span);
     }
     ebml_w.end_tag();
 }
index c8f9ed64a77f8f7bed411a1dba8d5ae7bcf49a06..594a235339669694357c7693e11a061dfa1cf6c7 100644 (file)
 use core;
 use doctree::*;
 
+// looks to me like the first two of these are actually
+// output parameters, maybe only mutated once; perhaps
+// better simply to have the visit method return a tuple
+// containing them?
+
+// also, is there some reason that this doesn't use the 'visit'
+// framework from syntax?
+
 pub struct RustdocVisitor<'a> {
     pub module: Module,
     pub attrs: Vec<ast::Attribute>,
@@ -64,6 +72,9 @@ pub fn visit(&mut self, krate: &ast::Crate) {
                                               ast::CRATE_NODE_ID,
                                               &krate.module,
                                               None);
+        // attach the crate's exported macros to the top-level module:
+        self.module.macros = krate.exported_macros.iter()
+            .map(|it| self.visit_macro(&**it)).collect();
         self.module.is_crate = true;
     }
 
@@ -323,15 +334,20 @@ pub fn visit_item(&mut self, item: &ast::Item, om: &mut Module) {
             ast::ItemForeignMod(ref fm) => {
                 om.foreigns.push(fm.clone());
             }
-            ast::ItemMac(ref _m) => {
-                om.macros.push(Macro {
-                    id: item.id,
-                    attrs: item.attrs.iter().map(|x| *x).collect(),
-                    name: item.ident,
-                    where: item.span,
-                    stab: self.stability(item.id),
-                })
+            ast::ItemMac(_) => {
+                fail!("rustdoc: macros should be gone, after expansion");
             }
         }
     }
+
+    // convert each exported_macro into a doc item
+    fn visit_macro(&self, item: &ast::Item) -> Macro {
+        Macro {
+            id: item.id,
+            attrs: item.attrs.iter().map(|x| *x).collect(),
+            name: item.ident,
+            where: item.span,
+            stab: self.stability(item.id),
+        }
+    }
 }
index 7ad9a18a15e1b3b27b62a68458138e922ad5f708..614bbd1c3ed00f4e5592891798e2ee22aca65d68 100644 (file)
@@ -256,7 +256,7 @@ pub struct Crate {
     pub attrs: Vec<Attribute>,
     pub config: CrateConfig,
     pub span: Span,
-    pub exported_macros: Vec<Span>
+    pub exported_macros: Vec<Gc<Item>>
 }
 
 pub type MetaItem = Spanned<MetaItem_>;
index 49bd3697884c72750ff2455ca8cffbc91ea1a4ed..5341f0c2d61b26ba44c5a12fcb0350db637eed09 100644 (file)
@@ -104,9 +104,9 @@ fn expand(&self,
 /// just into the compiler's internal macro table, for `make_def`).
 pub trait MacResult {
     /// Define a new macro.
-    // this should go away; the idea that a macro might expand into
-    // either a macro definition or an expression, depending on what
-    // the context wants, is kind of silly.
+    // this particular flavor should go away; the idea that a macro might
+    // expand into either a macro definition or an expression, depending
+    // on what the context wants, is kind of silly.
     fn make_def(&self) -> Option<MacroDef> {
         None
     }
@@ -431,7 +431,7 @@ pub struct ExtCtxt<'a> {
 
     pub mod_path: Vec<ast::Ident> ,
     pub trace_mac: bool,
-    pub exported_macros: Vec<codemap::Span>
+    pub exported_macros: Vec<Gc<ast::Item>>
 }
 
 impl<'a> ExtCtxt<'a> {
@@ -562,9 +562,6 @@ pub fn ident_of(&self, st: &str) -> ast::Ident {
     pub fn name_of(&self, st: &str) -> ast::Name {
         token::intern(st)
     }
-    pub fn push_exported_macro(&mut self, span: codemap::Span) {
-        self.exported_macros.push(span);
-    }
 }
 
 /// Extract a string literal from the macro expanded version of `expr`,
index fdb698441fc0c0d48358a92837124755662725c0..c10f3ce07749b49399d42924096023a49e844f27 100644 (file)
@@ -536,7 +536,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander)
             // create issue to recommend refactoring here?
             fld.extsbox.insert(intern(name.as_slice()), ext);
             if attr::contains_name(it.attrs.as_slice(), "macro_export") {
-                fld.cx.push_exported_macro(it.span);
+                fld.cx.exported_macros.push(it);
             }
             SmallVector::zero()
         }
@@ -1039,7 +1039,7 @@ pub struct ExportedMacros {
 pub fn expand_crate(parse_sess: &parse::ParseSess,
                     cfg: ExpansionConfig,
                     // these are the macros being imported to this crate:
-                    macros: Vec<ExportedMacros>,
+                    imported_macros: Vec<ExportedMacros>,
                     user_exts: Vec<NamedSyntaxExtension>,
                     c: Crate) -> Crate {
     let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg);
@@ -1048,7 +1048,7 @@ pub fn expand_crate(parse_sess: &parse::ParseSess,
         cx: &mut cx,
     };
 
-    for ExportedMacros { crate_name, macros } in macros.move_iter() {
+    for ExportedMacros { crate_name, macros } in imported_macros.move_iter() {
         let name = format!("<{} macros>", token::get_ident(crate_name))
             .into_string();
 
index e31ec0486538444a9fbb02b4503f0a49fedaa69d..271eee7d08a039db2a196b26630cef891698bb28 100644 (file)
@@ -752,7 +752,7 @@ pub fn noop_fold_crate<T: Folder>(c: Crate, folder: &mut T) -> Crate {
         attrs: c.attrs.iter().map(|x| folder.fold_attribute(*x)).collect(),
         config: c.config.iter().map(|x| fold_meta_item_(*x, folder)).collect(),
         span: folder.new_span(c.span),
-        exported_macros: c.exported_macros.iter().map(|sp| folder.new_span(*sp)).collect(),
+        exported_macros: c.exported_macros
     }
 }