Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASTDumper] Dump DeclContext #79311

Merged
merged 1 commit into from
Feb 13, 2025
Merged

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Feb 12, 2025

  • Include DeclContext of the node where possible
  • Add 'default-with-decl-contexts' dump style that dumps the dect context hierarchy in addition to the AST
  • Support -dump-parse with -dump-ast-format json

For example, for:

var value = 1 {
    didSet {}
}
class C {
    var foo = 42, bar = 86
    @Custom(arg: 1)
    func method(x: Int) {}
}

-dump-parse -dump-ast-format default-dc emits:

0x156943bd0 Module name=test
  0x156946a08 FileUnit file="test.swift"
    0x1569470b8 TopLevelCodeDecl line=1
    0x156947208 AbstractFunctionDecl name=_ : (no type set)
    0x156947510 ClassDecl name=C
      0x1569476b0 Initializer PatternBinding 0x0x156947858 #0
      0x1569477d8 Initializer PatternBinding 0x0x156947858 #1
      0x15694ffe0 AbstractFunctionDecl name=method(x:) : (no type set)
      0x156947940 Initializer CustomAttribute
(source_file "test.swift"
  (top_level_code_decl decl_context=0x156946a08 range=[test.swift:1:1 - line:3:1]
    (brace_stmt implicit range=[test.swift:1:1 - line:3:1]
      (pattern_binding_decl decl_context=0x1569470b8 range=[test.swift:1:1 - line:3:1]
        (pattern_entry
          (pattern_named "value")
          (original_init=integer_literal_expr type="<null>" value="1" builtin_initializer="**NULL**" initializer="**NULL**")
          (processed_init=integer_literal_expr type="<null>" value="1" builtin_initializer="**NULL**" initializer="**NULL**")))))
  (var_decl decl_context=0x156946a08 range=[test.swift:1:5 - line:1:5] "value"
    (accessor_decl decl_context=0x156946a08 range=[test.swift:2:5 - line:2:13] <anonymous @ 0x156947210> didSet for="value" thrown_type="<null>"
      (parameter_list range=[test.swift:2:5 - line:2:5]
        (parameter "oldValue" decl_context=0x156947208))
      (brace_stmt range=[test.swift:2:12 - line:2:13])))
  (class_decl decl_context=0x156946a08 range=[test.swift:4:1 - line:8:1] "C"
    (pattern_binding_decl decl_context=0x156947510 range=[test.swift:5:5 - line:5:25]
      (pattern_entry init_context=0x1569476b0
        (pattern_named "foo")
        (original_init=integer_literal_expr type="<null>" value="42" builtin_initializer="**NULL**" initializer="**NULL**")
        (processed_init=integer_literal_expr type="<null>" value="42" builtin_initializer="**NULL**" initializer="**NULL**"))
      (pattern_entry init_context=0x1569477d8
        (pattern_named "bar")
        (original_init=integer_literal_expr type="<null>" value="86" builtin_initializer="**NULL**" initializer="**NULL**")
        (processed_init=integer_literal_expr type="<null>" value="86" builtin_initializer="**NULL**" initializer="**NULL**")))
    (var_decl decl_context=0x156947510 range=[test.swift:5:9 - line:5:9] "foo")
    (var_decl decl_context=0x156947510 range=[test.swift:5:19 - line:5:19] "bar")
    (func_decl decl_context=0x156947510 range=[test.swift:7:5 - line:7:26] "method(x:)"
      (custom_attr range=[test.swift:6:5 - line:6:19] init_context=0x156947940
        (type_unqualified_ident id="Custom" unbound)
        (argument_list labels="arg:"
          (argument label="arg"
            (integer_literal_expr type="<null>" value="1" builtin_initializer="**NULL**" initializer="**NULL**"))))
      (parameter "self" decl_context=0x15694ffe0) thrown_type="<null>"
      (parameter_list range=[test.swift:7:16 - line:7:23]
        (parameter "x" decl_context=0x15694ffe0 apiName="x"))
      (brace_stmt range=[test.swift:7:25 - line:7:26]))))

You can verify the each AST node references the correct DeclContext.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a utility to use -dump-ast results in the test suite. This gives stable "address" representation across multiple test runs.
Currently not used, but I will use this in near future.

@rintaro
Copy link
Member Author

rintaro commented Feb 12, 2025

@swift-ci Please smoke test

* Include `DeclContext` of the node where possible
* Add 'default-with-decl-contexts' dump style that dumps the dect context
  hierarchy in addition to the AST
* Support `-dump-parse` with `-dump-ast-format json`
@rintaro
Copy link
Member Author

rintaro commented Feb 12, 2025

@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented Feb 12, 2025

@swift-ci Please smoke test

@rintaro rintaro enabled auto-merge February 13, 2025 00:25
@rintaro
Copy link
Member Author

rintaro commented Feb 13, 2025

@swift-ci Please smoke test Linux

@rintaro
Copy link
Member Author

rintaro commented Feb 13, 2025

@swift-ci Please smoke test Windows

@rintaro rintaro merged commit 153421f into swiftlang:main Feb 13, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants