Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

How do you run an analysis? #151

Open
typesanitizer opened this issue Sep 26, 2018 · 3 comments
Open

How do you run an analysis? #151

typesanitizer opened this issue Sep 26, 2018 · 3 comments

Comments

@typesanitizer
Copy link

typesanitizer commented Sep 26, 2018

Pasting my Reddit comment here:

let's say that given a crate's source code, I want to create a data structure containing type signatures of all functions with proper naming, trait/lifetime constraints, post proc macro-expansion. I do not care about codegen (so being able to avoid it would a big plus).

What would be the best way of doing so? (Best = if there is some change in the compiler/cargo, I get the change soon).

I got a suggestion that I should use this crate. Looking at the source code here/in rls, I can't really tell what is going on :(. For example, if I have

    let host = AnalysisHost::new(Target::Debug);
    let p = Path::new("/home/theindigamer/Code/rls-analysis");
    // Not entirely sure what the second path should be
    host.reload(p, Path::new("."));
    println!("Roots: {:?}", host.def_roots());

I'd naively expect that I'll get the ids of rls-analysis crate and its dependencies (transitively), and then I can call for_each_child_def to recursively traverse all the modules in a crate. Instead I get an empty vector, so it seems like nothing has been analyzed yet. Am I vaguely on the right track or way off the mark?

The other option I'm exploring is using syn to parse stuff (which is working ok) and implement the module traversal/Cargo.toml parsing myself.

I understand that writing a long explanation might be too much work and you may not have the time; in that case, a pointer to roughly the right place(s) in the code would still be appreciated.

@nrc
Copy link
Member

nrc commented Oct 7, 2018

The this works is that you need to provide save-analysis data to this crate. The easiest way to do that is to run a nightly compiler with -Zsave-analysis. This crate then needs pointing at the directory with the save-analysis data in it for all crates. As an example of doing all this, see running rustc and updating rls-analysis from date

@typesanitizer
Copy link
Author

typesanitizer commented Oct 13, 2018

I think you accidentally linked to the same thing twice. But yeah, I can see the save analysis data generated in JSON after running it! However, a couple of lines in the linked code don't make sense to me. With cargo 1.31.0-nightly (ad6e5c003 2018-09-28), I actually see the data being generated under target/debug/deps/save-analysis whereas these lines

https://github.com/nrc/cargo-src/blob/34b3b4cae63ed2de53672135cac8a4ffa5b26a71/src/build.rs#L19

https://github.com/nrc/cargo-src/blob/34b3b4cae63ed2de53672135cac8a4ffa5b26a71/src/build.rs#L71-L75

make me think it should be saved under target/rls/debug/deps/save-analysis. Anyways, if I ignore that for a moment, the path_prefix (1st) and base_dir (2nd) arguments weren't clear to me so I looked at this:

https://github.com/nrc/cargo-src/blob/3c1a3327a2a92a61275f711cfa9b27fd43712f31/src/file_controller/mod.rs#L187-L190

and it seems path_prefix should be "." and base_dir should be "/home/.../crate-dir" -- is that right? I've tried various different combinations (including appending `"/target") but none of them work.

(Not sure why the code snippets aren't being rendered 🙁 )

@nrc
Copy link
Member

nrc commented Oct 23, 2018

Sorry the second link should have been: https://github.com/nrc/cargo-src/blob/master/src/file_controller/mod.rs#L187-L192

You can generate the data wherever you like. The RLS creates a separate directory for building (target/rls) and so looks in there. That is how CargoAnalysisLoader is setup, but you can pass your own loader if you want to use a different directory

path_prefix and base_dir are somewhat confusing. base_dir is used for converting relative paths to absolute ones, I think it points either to the crate directory or the src directory. path_prefix should point to the crate directory

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants