traverse-graph 0.1.4

Call graph analysis and visualization for Solidity smart contracts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
    This module focuses on extracting comments from Solidity source code files
    and associating them with the relevant code items (like contracts, functions,
    state variables, etc.). It utilizes `tree-sitter` to parse the Solidity
    Abstract Syntax Tree (AST).

    Key functionalities include:
    - Defining `SourceItemKind` to categorize Solidity code elements (e.g.,
      Contract, Function, StateVariable).
    - Defining `SourceComment` to store the extracted comment text, its source
      span, the kind and name of the associated code item, the item's span,
      and a flag indicating if it's a NatSpec comment.
    - Using a `tree-sitter` query to identify comments (both block and line
      comments) that are positioned immediately before or adjacent to
      recognizable Solidity constructs.
    - Extracting details for each matched comment and its corresponding code
      item, including the item's kind and name. For certain items like state
      variables, the name is derived by inspecting the item node's children,
      as it might not be directly captured by a simple query name field.
    - The main function `extract_source_comments` takes Solidity source code
      as input and returns a vector of `SourceComment` structs.

    This module acts as a bridge between raw Solidity code and structured
    comment information, which can then be further processed, for instance,
    by parsing the `text` field of `SourceComment` using the `natspec/mod.rs`
    parsers if `is_natspec` is true.
*/
use crate::parser::get_node_text;
use anyhow::{Context, Result};
use crate::parser::get_solidity_language;
use streaming_iterator::StreamingIterator;
use tree_sitter::{Node, Parser, Query, QueryCursor};
use serde::{Serialize, Deserialize};

use super::{TextIndex, TextRange};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SourceItemKind {
    Contract,
    Interface,
    Library,
    Struct,
    Enum,
    Function,
    Modifier,
    Event,
    Error,
    StateVariable,
    UsingDirective,
    Unknown,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SourceComment {
    pub text: String,
    pub raw_comment_span: TextRange,
    pub item_kind: SourceItemKind,
    pub item_name: Option<String>,
    pub item_span: TextRange,
    pub is_natspec: bool,
}

fn node_to_text_range(node: &tree_sitter::Node) -> TextRange {
    TextRange {
        start: TextIndex {
            utf8: node.start_byte(),
            line: node.start_position().row,
            column: node.start_position().column,
        },
        end: TextIndex {
            utf8: node.end_byte(),
            line: node.end_position().row,
            column: node.end_position().column,
        },
    }
}

const SOURCE_ITEM_COMMENT_QUERY: &str = r#"
(
  (comment) @comment
  .
  [
    (contract_declaration name: (identifier) @item_name)
    (interface_declaration name: (identifier) @item_name)
    (library_declaration name: (identifier) @item_name)
    (struct_declaration name: (identifier) @item_name)
    (enum_declaration name: (identifier) @item_name)
    (function_definition name: (identifier) @item_name)
    (modifier_definition name: (identifier) @item_name)
    (event_definition name: (identifier) @item_name)
    (error_declaration name: (identifier) @item_name)
    (state_variable_declaration name: (identifier) @item_name)
    (using_directive)
  ] @item
)
"#;

pub fn extract_source_comments(source: &str) -> Result<Vec<SourceComment>> {
    let solidity_lang = get_solidity_language();
    let mut parser = Parser::new();
    parser
        .set_language(&solidity_lang)
        .context("Failed to set language for Solidity parser")?;

    let tree = parser
        .parse(source, None)
        .context("Failed to parse Solidity source")?;

    let query = Query::new(&solidity_lang, SOURCE_ITEM_COMMENT_QUERY)
        .context("Failed to create source item comment query")?;

    let mut query_cursor = QueryCursor::new();
    let mut matches = query_cursor.matches(&query, tree.root_node(), source.as_bytes());

    let mut source_comments = Vec::new();

    matches.advance();
    while let Some(mat) = matches.get() {
        let mut comment_node: Option<Node> = None;
        let mut item_node: Option<Node> = None;
        let mut item_name_node: Option<Node> = None;

        for capture in mat.captures {
            let capture_name = &query.capture_names()[capture.index as usize];
            match *capture_name {
                "comment" => comment_node = Some(capture.node),
                "item" => item_node = Some(capture.node),
                "item_name" => item_name_node = Some(capture.node),
                _ => {}
            }
        }

        if let (Some(comment_n), Some(item_n)) = (comment_node, item_node) {
            let comment_text_str = get_node_text(&comment_n, source);
            let is_natspec =
                comment_text_str.starts_with("///") || comment_text_str.starts_with("/**");

            let item_kind_str = item_n.kind();
            let (item_kind, extracted_name) = match item_kind_str {
                "contract_declaration" => (
                    SourceItemKind::Contract,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "interface_declaration" => (
                    SourceItemKind::Interface,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "library_declaration" => (
                    SourceItemKind::Library,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "struct_declaration" => (
                    SourceItemKind::Struct,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "enum_declaration" => (
                    SourceItemKind::Enum,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "function_definition" => (
                    SourceItemKind::Function,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "modifier_definition" => (
                    SourceItemKind::Modifier,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "event_definition" => (
                    SourceItemKind::Event,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "error_declaration" => (
                    SourceItemKind::Error,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "state_variable_declaration" => (
                    SourceItemKind::StateVariable,
                    item_name_node.map(|n| get_node_text(&n, source).to_string()),
                ),
                "using_directive" => (
                    SourceItemKind::UsingDirective,
                    Some(get_node_text(&item_n, source).to_string()), // Name for using_directive is the full text
                ),
                _ => (SourceItemKind::Unknown, None),
            };

            source_comments.push(SourceComment {
                text: comment_text_str.to_string(),
                raw_comment_span: node_to_text_range(&comment_n),
                item_kind,
                item_name: extracted_name,
                item_span: node_to_text_range(&item_n),
                is_natspec,
            });
        }
        matches.advance();
    }

    Ok(source_comments)
}

#[cfg(test)]
mod source_comment_extraction_tests {
    use super::*;
    

    #[test]
    fn test_extract_simple_contract_comment() {
        let source = r#"
        /// This is a contract
        contract MyContract {}
        "#;
        let comments = extract_source_comments(source).unwrap();
        assert_eq!(comments.len(), 1);
        let comment = &comments[0];
        assert_eq!(comment.text, "/// This is a contract");
        assert!(comment.is_natspec);
        assert_eq!(comment.item_kind, SourceItemKind::Contract);
        assert_eq!(comment.item_name, Some("MyContract".to_string()));
    }

    #[test]
    fn test_extract_function_comment() {
        let source = r#"
        /**
         * This is a function.
         * @param x an integer
         */
        function myFunction(uint x) public {}
        "#;
        let comments = extract_source_comments(source).unwrap();
        assert_eq!(comments.len(), 1);
        let comment = &comments[0];
        assert_eq!(
            comment.text,
            "/**\n         * This is a function.\n         * @param x an integer\n         */"
        );
        assert!(comment.is_natspec);
        assert_eq!(comment.item_kind, SourceItemKind::Function);
        assert_eq!(comment.item_name, Some("myFunction".to_string()));
    }

    #[test]
    fn test_extract_state_variable_comment() {
        let source = r#"
        contract TestContract {
            /// The counter value
            uint256 public count;
        }
        "#;
        let comments = extract_source_comments(source).unwrap();
        assert_eq!(comments.len(), 1);
        let comment = &comments[0];
        assert_eq!(comment.text, "/// The counter value");
        assert!(comment.is_natspec);
        assert_eq!(comment.item_kind, SourceItemKind::StateVariable);
        assert_eq!(comment.item_name, Some("count".to_string()));
    }

    #[test]
    fn test_extract_multiple_comments() {
        let source = r#"
        /// Contract C
        contract C {
            /// Var V
            uint public v;

            /** Func F */
            function f() public {}
        }
        "#;
        let comments = extract_source_comments(source).unwrap();
        assert_eq!(comments.len(), 3);

        let contract_comment = comments
            .iter()
            .find(|c| c.item_name == Some("C".to_string()))
            .unwrap();
        assert_eq!(contract_comment.text, "/// Contract C");
        assert_eq!(contract_comment.item_kind, SourceItemKind::Contract);

        let var_comment = comments
            .iter()
            .find(|c| c.item_name == Some("v".to_string()))
            .unwrap();
        assert_eq!(var_comment.text, "/// Var V");
        assert_eq!(var_comment.item_kind, SourceItemKind::StateVariable);

        let func_comment = comments
            .iter()
            .find(|c| c.item_name == Some("f".to_string()))
            .unwrap();
        assert_eq!(func_comment.text, "/** Func F */");
        assert_eq!(func_comment.item_kind, SourceItemKind::Function);
    }

    #[test]
    fn test_no_comment() {
        let source = "contract NoComment {}";
        let comments = extract_source_comments(source).unwrap();
        assert!(comments.is_empty());
    }

    #[test]
    fn test_regular_comment_not_natspec() {
        let source = r#"
        // A regular comment
        function test() public {}
        "#;
        let comments = extract_source_comments(source).unwrap();
        assert_eq!(comments.len(), 1);
        assert_eq!(comments[0].text, "// A regular comment");
        assert!(!comments[0].is_natspec);
        assert_eq!(comments[0].item_kind, SourceItemKind::Function);
        assert_eq!(comments[0].item_name, Some("test".to_string()));
    }

    #[test]
    fn test_using_directive_comment() {
        let source = r#"
        contract TestContract {
            /// @title Using SafeMath for uint256
            using SafeMath for uint256;
        }
        "#;
        let comments = extract_source_comments(source).unwrap();
        assert_eq!(comments.len(), 1);
        let comment = &comments[0];
        assert_eq!(comment.text, "/// @title Using SafeMath for uint256");
        assert!(comment.is_natspec);
        assert_eq!(comment.item_kind, SourceItemKind::UsingDirective);
        assert_eq!(
            comment.item_name,
            Some("using SafeMath for uint256;".to_string())
        );
    }

    #[test]
    fn test_state_variable_complex_declaration() {
        let source = r#"
        contract TestContract {
            /// Stores the owner of the contract
            address payable public owner;
        }
        "#;
        let comments = extract_source_comments(source).unwrap();
        assert_eq!(comments.len(), 1);
        let comment = &comments[0];
        assert_eq!(comment.text, "/// Stores the owner of the contract");
        assert_eq!(comment.item_kind, SourceItemKind::StateVariable);
        assert_eq!(comment.item_name, Some("owner".to_string()));
    }

    #[test]
    fn test_state_variable_no_name_found() {
        let source = r#"
        contract Test {
            /// This is a mapping
            mapping(address => uint) public balances;
        }
        "#;
        let comments = extract_source_comments(source).unwrap();
        let mapping_comment = comments
            .iter()
            .find(|c| c.text == "/// This is a mapping")
            .unwrap();
        assert_eq!(mapping_comment.item_kind, SourceItemKind::StateVariable);
        assert_eq!(mapping_comment.item_name, Some("balances".to_string()));
    }

    #[test]
    fn test_extract_struct_and_event_comments() {
        let source = r#"
        /// Defines a new proposal.
        struct Proposal {
            address proposer;
            string description;
            uint voteCount;
        }

        /** @dev Emitted when a new proposal is created.
          * @param proposalId The ID of the new proposal.
          */
        event ProposalCreated(uint proposalId);
        "#;
        let comments = extract_source_comments(source).unwrap();
        assert_eq!(comments.len(), 2);

        let struct_comment = comments
            .iter()
            .find(|c| c.item_name == Some("Proposal".to_string()))
            .unwrap();
        assert_eq!(struct_comment.text, "/// Defines a new proposal.");
        assert_eq!(struct_comment.item_kind, SourceItemKind::Struct);

        let event_comment = comments
            .iter()
            .find(|c| c.item_name == Some("ProposalCreated".to_string()))
            .unwrap();
        assert_eq!(event_comment.text, "/** @dev Emitted when a new proposal is created.\n          * @param proposalId The ID of the new proposal.\n          */");
        assert_eq!(event_comment.item_kind, SourceItemKind::Event);
    }
}