Skip to content

Commit a5a0a11

Browse files
committed
Unified: Add macro call expressions to the AST
1 parent 29754b0 commit a5a0a11

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎unified/extractor/ast_types.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ supertypes:
1313
- binary_expr
1414
- unary_expr
1515
- call_expr
16+
- macro_call_expr
1617
- member_access_expr
1718
- super_expr
1819
- function_expr
@@ -171,6 +172,13 @@ named:
171172
callee: expr
172173
argument*: argument
173174

175+
# A macro call expression, such as `#expect(x == y)` in Swift or
176+
# `assert!(x == y)` in Rust.
177+
macro_call_expr:
178+
macro_name_node: identifier
179+
type_argument*: expr
180+
argument*: argument
181+
174182
argument:
175183
modifier*: modifier
176184
name_node?: identifier

‎unified/ql/lib/codeql/unified/internal/Ast.qll‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,34 @@ module Unified {
956956
}
957957
}
958958

959+
/** A class representing `macro_call_expr` nodes. */
960+
class MacroCallExpr extends @unified_macro_call_expr, F::Expr {
961+
/** Gets the name of the primary QL class for this element. */
962+
final override string getAPrimaryQlClass() { result = "MacroCallExpr" }
963+
964+
/** Gets the node corresponding to the field `argument`. */
965+
final F::Argument getArgument(int i) { unified_macro_call_expr_argument(this, i, result) }
966+
967+
/** Gets the node corresponding to the field `argument`. */
968+
final F::Argument getAnArgument() { result = this.getArgument(_) }
969+
970+
/** Gets the node corresponding to the field `macro_name_node`. */
971+
final F::Identifier getMacroNameNode() { unified_macro_call_expr_def(this, result) }
972+
973+
/** Gets the node corresponding to the field `type_argument`. */
974+
final F::Expr getTypeArgument(int i) { unified_macro_call_expr_type_argument(this, i, result) }
975+
976+
/** Gets the node corresponding to the field `type_argument`. */
977+
final F::Expr getATypeArgument() { result = this.getTypeArgument(_) }
978+
979+
/** Gets a field or child node of this node. */
980+
final override F::AstNode getAFieldOrChild() {
981+
unified_macro_call_expr_argument(this, _, result) or
982+
unified_macro_call_expr_def(this, result) or
983+
unified_macro_call_expr_type_argument(this, _, result)
984+
}
985+
}
986+
959987
/** A class representing `map_literal` nodes. */
960988
class MapLiteral extends @unified_map_literal, F::Expr {
961989
/** Gets the name of the primary QL class for this element. */
@@ -1717,6 +1745,12 @@ module Unified {
17171745
or
17181746
result = node.(LabeledStmt).getStmt() and i = -1 and name = "getStmt"
17191747
or
1748+
result = node.(MacroCallExpr).getArgument(i) and name = "getArgument"
1749+
or
1750+
result = node.(MacroCallExpr).getMacroNameNode() and i = -1 and name = "getMacroNameNode"
1751+
or
1752+
result = node.(MacroCallExpr).getTypeArgument(i) and name = "getTypeArgument"
1753+
or
17201754
result = node.(MapLiteral).getElement(i) and name = "getElement"
17211755
or
17221756
result = node.(MemberAccessExpr).getBase() and i = -1 and name = "getBase"
@@ -1938,6 +1972,8 @@ module UnifiedFinal {
19381972

19391973
final class LabeledStmt = F::LabeledStmt;
19401974

1975+
final class MacroCallExpr = F::MacroCallExpr;
1976+
19411977
final class MapLiteral = F::MapLiteral;
19421978

19431979
final class Member = F::Member;

‎unified/ql/lib/unified.dbscheme‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ unified_equality_type_constraint_def(
440440
int right: @unified_expr ref
441441
);
442442

443-
@unified_expr = @unified_array_literal | @unified_binary_expr | @unified_block | @unified_break_expr | @unified_bulk_importing_pattern | @unified_call_expr | @unified_conditional_pattern | @unified_continue_expr | @unified_expr_pattern | @unified_function_expr | @unified_generic_type_expr | @unified_if_expr | @unified_key_value_pair | @unified_map_literal | @unified_member_access_expr | @unified_named_pattern | @unified_or_pattern | @unified_pattern_guard_expr | @unified_return_expr | @unified_string_interpolation_expr | @unified_switch_expr | @unified_throw_expr | @unified_token_boolean_literal | @unified_token_builtin_expr | @unified_token_empty_expr | @unified_token_float_literal | @unified_token_identifier | @unified_token_ignore_pattern | @unified_token_inferred_type_expr | @unified_token_int_literal | @unified_token_regex_literal | @unified_token_string_literal | @unified_token_super_expr | @unified_token_unhandled_node | @unified_token_unsupported_node | @unified_try_expr | @unified_tuple_expr | @unified_type_cast_expr | @unified_type_test_expr | @unified_unary_expr | @unified_unresolved_operator_sequence
443+
@unified_expr = @unified_array_literal | @unified_binary_expr | @unified_block | @unified_break_expr | @unified_bulk_importing_pattern | @unified_call_expr | @unified_conditional_pattern | @unified_continue_expr | @unified_expr_pattern | @unified_function_expr | @unified_generic_type_expr | @unified_if_expr | @unified_key_value_pair | @unified_macro_call_expr | @unified_map_literal | @unified_member_access_expr | @unified_named_pattern | @unified_or_pattern | @unified_pattern_guard_expr | @unified_return_expr | @unified_string_interpolation_expr | @unified_switch_expr | @unified_throw_expr | @unified_token_boolean_literal | @unified_token_builtin_expr | @unified_token_empty_expr | @unified_token_float_literal | @unified_token_identifier | @unified_token_ignore_pattern | @unified_token_inferred_type_expr | @unified_token_int_literal | @unified_token_regex_literal | @unified_token_string_literal | @unified_token_super_expr | @unified_token_unhandled_node | @unified_token_unsupported_node | @unified_try_expr | @unified_tuple_expr | @unified_type_cast_expr | @unified_type_test_expr | @unified_unary_expr | @unified_unresolved_operator_sequence
444444

445445
@unified_expr_or_operator = @unified_expr | @unified_token_infix_operator
446446

@@ -631,6 +631,25 @@ unified_labeled_stmt_def(
631631
int stmt: @unified_stmt ref
632632
);
633633

634+
#keyset[unified_macro_call_expr, index]
635+
unified_macro_call_expr_argument(
636+
int unified_macro_call_expr: @unified_macro_call_expr ref,
637+
int index: int ref,
638+
unique int argument: @unified_argument ref
639+
);
640+
641+
#keyset[unified_macro_call_expr, index]
642+
unified_macro_call_expr_type_argument(
643+
int unified_macro_call_expr: @unified_macro_call_expr ref,
644+
int index: int ref,
645+
unique int type_argument: @unified_expr ref
646+
);
647+
648+
unified_macro_call_expr_def(
649+
unique int id: @unified_macro_call_expr,
650+
int macro_name_node: @unified_token_identifier ref
651+
);
652+
634653
#keyset[unified_map_literal, index]
635654
unified_map_literal_element(
636655
int unified_map_literal: @unified_map_literal ref,
@@ -1006,7 +1025,7 @@ unified_trivia_tokeninfo(
10061025
string value: string ref
10071026
);
10081027

1009-
@unified_ast_node = @unified_accessor_declaration | @unified_argument | @unified_array_literal | @unified_associated_type_declaration | @unified_base_type | @unified_binary_expr | @unified_block | @unified_bound_type_constraint | @unified_break_expr | @unified_bulk_importing_pattern | @unified_call_expr | @unified_catch_clause | @unified_class_like_declaration | @unified_conditional_pattern | @unified_constructor_declaration | @unified_continue_expr | @unified_defer_stmt | @unified_destructor_declaration | @unified_do_while_stmt | @unified_equality_type_constraint | @unified_expr_pattern | @unified_for_each_stmt | @unified_function_declaration | @unified_function_expr | @unified_generic_type_expr | @unified_guard_if_stmt | @unified_if_expr | @unified_import_declaration | @unified_initializer_declaration | @unified_key_value_pair | @unified_labeled_stmt | @unified_map_literal | @unified_member_access_expr | @unified_named_pattern | @unified_operator_syntax_declaration | @unified_or_pattern | @unified_parameter | @unified_pattern_guard_expr | @unified_return_expr | @unified_string_interpolation_expr | @unified_switch_case | @unified_switch_expr | @unified_throw_expr | @unified_token | @unified_top_level | @unified_trivia_token | @unified_try_expr | @unified_tuple_expr | @unified_type_alias_declaration | @unified_type_cast_expr | @unified_type_parameter | @unified_type_test_expr | @unified_unary_expr | @unified_unresolved_operator_sequence | @unified_variable_declaration | @unified_while_stmt
1028+
@unified_ast_node = @unified_accessor_declaration | @unified_argument | @unified_array_literal | @unified_associated_type_declaration | @unified_base_type | @unified_binary_expr | @unified_block | @unified_bound_type_constraint | @unified_break_expr | @unified_bulk_importing_pattern | @unified_call_expr | @unified_catch_clause | @unified_class_like_declaration | @unified_conditional_pattern | @unified_constructor_declaration | @unified_continue_expr | @unified_defer_stmt | @unified_destructor_declaration | @unified_do_while_stmt | @unified_equality_type_constraint | @unified_expr_pattern | @unified_for_each_stmt | @unified_function_declaration | @unified_function_expr | @unified_generic_type_expr | @unified_guard_if_stmt | @unified_if_expr | @unified_import_declaration | @unified_initializer_declaration | @unified_key_value_pair | @unified_labeled_stmt | @unified_macro_call_expr | @unified_map_literal | @unified_member_access_expr | @unified_named_pattern | @unified_operator_syntax_declaration | @unified_or_pattern | @unified_parameter | @unified_pattern_guard_expr | @unified_return_expr | @unified_string_interpolation_expr | @unified_switch_case | @unified_switch_expr | @unified_throw_expr | @unified_token | @unified_top_level | @unified_trivia_token | @unified_try_expr | @unified_tuple_expr | @unified_type_alias_declaration | @unified_type_cast_expr | @unified_type_parameter | @unified_type_test_expr | @unified_unary_expr | @unified_unresolved_operator_sequence | @unified_variable_declaration | @unified_while_stmt
10101029

10111030
unified_ast_node_location(
10121031
unique int node: @unified_ast_node ref,

0 commit comments

Comments
 (0)