Skip to content

Commit 2fdb3d6

Browse files
authored
Merge branch 'main' into closure-expr-as-cfg-callable
2 parents ef224b9 + b0c66dd commit 2fdb3d6

File tree

8 files changed

+162
-36
lines changed

8 files changed

+162
-36
lines changed

ql/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ql/extractor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2018"
1010
flate2 = "1.0"
1111
node-types = { path = "../node-types" }
1212
tree-sitter = "0.19"
13-
tree-sitter-ql = { git = "https://github.com/tausbn/tree-sitter-ql.git", rev = "725395405e65814f10095a451404b0ced5dc6289" }
13+
tree-sitter-ql = { git = "https://github.com/tausbn/tree-sitter-ql.git", rev = "99f3bc30fa772b07ad19a23799fe7433dc15f765" }
1414
clap = "2.33"
1515
tracing = "0.1"
1616
tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }

ql/generator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ clap = "2.33"
1111
node-types = { path = "../node-types" }
1212
tracing = "0.1"
1313
tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }
14-
tree-sitter-ql = { git = "https://github.com/tausbn/tree-sitter-ql.git", rev = "725395405e65814f10095a451404b0ced5dc6289" }
14+
tree-sitter-ql = { git = "https://github.com/tausbn/tree-sitter-ql.git", rev = "99f3bc30fa772b07ad19a23799fe7433dc15f765" }

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ class TypeExpr extends TType, AstNode {
673673
* Gets the module of the type, if it exists.
674674
* E.g. `DataFlow` in `DataFlow::Node`.
675675
*/
676-
ModuleExpr getModule() { toQL(result) = type.getChild() }
676+
ModuleExpr getModule() { toQL(result) = type.getQualifier() }
677677

678678
/**
679679
* Gets the type that this type reference refers to.

ql/ql/src/codeql_ql/ast/internal/TreeSitter.qll

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ module QL {
137137
/** Gets the node corresponding to the field `name`. */
138138
final LiteralId getName() { ql_arityless_predicate_expr_def(this, result) }
139139

140-
/** Gets the child of this node. */
141-
final ModuleExpr getChild() { ql_arityless_predicate_expr_child(this, result) }
140+
/** Gets the node corresponding to the field `qualifier`. */
141+
final ModuleExpr getQualifier() { ql_arityless_predicate_expr_qualifier(this, result) }
142142

143143
/** Gets a field or child node of this node. */
144144
final override AstNode getAFieldOrChild() {
145145
ql_arityless_predicate_expr_def(this, result) or
146-
ql_arityless_predicate_expr_child(this, result)
146+
ql_arityless_predicate_expr_qualifier(this, result)
147147
}
148148
}
149149

@@ -944,15 +944,24 @@ module QL {
944944
/** Gets the name of the primary QL class for this element. */
945945
final override string getAPrimaryQlClass() { result = "Module" }
946946

947+
/** Gets the node corresponding to the field `implements`. */
948+
final SignatureExpr getImplements(int i) { ql_module_implements(this, i, result) }
949+
947950
/** Gets the node corresponding to the field `name`. */
948951
final ModuleName getName() { ql_module_def(this, result) }
949952

953+
/** Gets the node corresponding to the field `parameter`. */
954+
final ModuleParam getParameter(int i) { ql_module_parameter(this, i, result) }
955+
950956
/** Gets the `i`th child of this node. */
951957
final AstNode getChild(int i) { ql_module_child(this, i, result) }
952958

953959
/** Gets a field or child node of this node. */
954960
final override AstNode getAFieldOrChild() {
955-
ql_module_def(this, result) or ql_module_child(this, _, result)
961+
ql_module_implements(this, _, result) or
962+
ql_module_def(this, result) or
963+
ql_module_parameter(this, _, result) or
964+
ql_module_child(this, _, result)
956965
}
957966
}
958967

@@ -985,6 +994,18 @@ module QL {
985994
}
986995
}
987996

997+
/** A class representing `moduleInstantiation` nodes. */
998+
class ModuleInstantiation extends @ql_module_instantiation, AstNode {
999+
/** Gets the name of the primary QL class for this element. */
1000+
final override string getAPrimaryQlClass() { result = "ModuleInstantiation" }
1001+
1002+
/** Gets the `i`th child of this node. */
1003+
final SignatureExpr getChild(int i) { ql_module_instantiation_child(this, i, result) }
1004+
1005+
/** Gets a field or child node of this node. */
1006+
final override AstNode getAFieldOrChild() { ql_module_instantiation_child(this, _, result) }
1007+
}
1008+
9881009
/** A class representing `moduleMember` nodes. */
9891010
class ModuleMember extends @ql_module_member, AstNode {
9901011
/** Gets the name of the primary QL class for this element. */
@@ -1009,6 +1030,23 @@ module QL {
10091030
final override AstNode getAFieldOrChild() { ql_module_name_def(this, result) }
10101031
}
10111032

1033+
/** A class representing `moduleParam` nodes. */
1034+
class ModuleParam extends @ql_module_param, AstNode {
1035+
/** Gets the name of the primary QL class for this element. */
1036+
final override string getAPrimaryQlClass() { result = "ModuleParam" }
1037+
1038+
/** Gets the node corresponding to the field `parameter`. */
1039+
final SimpleId getParameter() { ql_module_param_def(this, result, _) }
1040+
1041+
/** Gets the node corresponding to the field `signature`. */
1042+
final SignatureExpr getSignature() { ql_module_param_def(this, _, result) }
1043+
1044+
/** Gets a field or child node of this node. */
1045+
final override AstNode getAFieldOrChild() {
1046+
ql_module_param_def(this, result, _) or ql_module_param_def(this, _, result)
1047+
}
1048+
}
1049+
10121050
/** A class representing `mul_expr` nodes. */
10131051
class MulExpr extends @ql_mul_expr, AstNode {
10141052
/** Gets the name of the primary QL class for this element. */
@@ -1277,6 +1315,23 @@ module QL {
12771315
final override AstNode getAFieldOrChild() { ql_set_literal_child(this, _, result) }
12781316
}
12791317

1318+
/** A class representing `signatureExpr` nodes. */
1319+
class SignatureExpr extends @ql_signature_expr, AstNode {
1320+
/** Gets the name of the primary QL class for this element. */
1321+
final override string getAPrimaryQlClass() { result = "SignatureExpr" }
1322+
1323+
/** Gets the node corresponding to the field `predicate`. */
1324+
final PredicateExpr getPredicate() { ql_signature_expr_predicate(this, result) }
1325+
1326+
/** Gets the node corresponding to the field `type_expr`. */
1327+
final TypeExpr getTypeExpr() { ql_signature_expr_type_expr(this, result) }
1328+
1329+
/** Gets a field or child node of this node. */
1330+
final override AstNode getAFieldOrChild() {
1331+
ql_signature_expr_predicate(this, result) or ql_signature_expr_type_expr(this, result)
1332+
}
1333+
}
1334+
12801335
/** A class representing `simpleId` tokens. */
12811336
class SimpleId extends @ql_token_simple_id, Token {
12821337
/** Gets the name of the primary QL class for this element. */
@@ -1357,12 +1412,17 @@ module QL {
13571412
/** Gets the node corresponding to the field `name`. */
13581413
final ClassName getName() { ql_type_expr_name(this, result) }
13591414

1415+
/** Gets the node corresponding to the field `qualifier`. */
1416+
final ModuleExpr getQualifier() { ql_type_expr_qualifier(this, result) }
1417+
13601418
/** Gets the child of this node. */
13611419
final AstNode getChild() { ql_type_expr_child(this, result) }
13621420

13631421
/** Gets a field or child node of this node. */
13641422
final override AstNode getAFieldOrChild() {
1365-
ql_type_expr_name(this, result) or ql_type_expr_child(this, result)
1423+
ql_type_expr_name(this, result) or
1424+
ql_type_expr_qualifier(this, result) or
1425+
ql_type_expr_child(this, result)
13661426
}
13671427
}
13681428

ql/ql/src/ql.dbscheme

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ ql_annotation_def(
9595
int name: @ql_token_annot_name ref
9696
);
9797

98-
ql_arityless_predicate_expr_child(
98+
ql_arityless_predicate_expr_qualifier(
9999
unique int ql_arityless_predicate_expr: @ql_arityless_predicate_expr ref,
100-
unique int child: @ql_module_expr ref
100+
unique int qualifier: @ql_module_expr ref
101101
);
102102

103103
ql_arityless_predicate_expr_def(
@@ -601,6 +601,20 @@ ql_member_predicate_def(
601601
int return_type: @ql_memberPredicate_returnType_type ref
602602
);
603603

604+
#keyset[ql_module, index]
605+
ql_module_implements(
606+
int ql_module: @ql_module ref,
607+
int index: int ref,
608+
unique int implements: @ql_signature_expr ref
609+
);
610+
611+
#keyset[ql_module, index]
612+
ql_module_parameter(
613+
int ql_module: @ql_module ref,
614+
int index: int ref,
615+
unique int parameter: @ql_module_param ref
616+
);
617+
604618
@ql_module_child_type = @ql_module_alias_body | @ql_module_member
605619

606620
#keyset[ql_module, index]
@@ -625,13 +639,24 @@ ql_module_expr_name(
625639
unique int name: @ql_token_simple_id ref
626640
);
627641

628-
@ql_moduleExpr_child_type = @ql_module_expr | @ql_token_simple_id
642+
@ql_moduleExpr_child_type = @ql_module_expr | @ql_module_instantiation | @ql_token_simple_id
629643

630644
ql_module_expr_def(
631645
unique int id: @ql_module_expr,
632646
int child: @ql_moduleExpr_child_type ref
633647
);
634648

649+
#keyset[ql_module_instantiation, index]
650+
ql_module_instantiation_child(
651+
int ql_module_instantiation: @ql_module_instantiation ref,
652+
int index: int ref,
653+
unique int child: @ql_signature_expr ref
654+
);
655+
656+
ql_module_instantiation_def(
657+
unique int id: @ql_module_instantiation
658+
);
659+
635660
@ql_moduleMember_child_type = @ql_annotation | @ql_classless_predicate | @ql_dataclass | @ql_datatype | @ql_import_directive | @ql_module | @ql_select | @ql_token_qldoc
636661

637662
#keyset[ql_module_member, index]
@@ -650,6 +675,12 @@ ql_module_name_def(
650675
int child: @ql_token_simple_id ref
651676
);
652677

678+
ql_module_param_def(
679+
unique int id: @ql_module_param,
680+
int parameter: @ql_token_simple_id ref,
681+
int signature: @ql_signature_expr ref
682+
);
683+
653684
@ql_mul_expr_left_type = @ql_add_expr | @ql_aggregate | @ql_call_or_unqual_agg_expr | @ql_comp_term | @ql_conjunction | @ql_disjunction | @ql_expr_annotation | @ql_if_term | @ql_implication | @ql_in_expr | @ql_instance_of | @ql_literal | @ql_mul_expr | @ql_negation | @ql_par_expr | @ql_prefix_cast | @ql_qualified_expr | @ql_quantified | @ql_range | @ql_set_literal | @ql_special_call | @ql_super_ref | @ql_unary_expr | @ql_variable
654685

655686
@ql_mul_expr_right_type = @ql_add_expr | @ql_aggregate | @ql_call_or_unqual_agg_expr | @ql_comp_term | @ql_conjunction | @ql_disjunction | @ql_expr_annotation | @ql_if_term | @ql_implication | @ql_in_expr | @ql_instance_of | @ql_literal | @ql_mul_expr | @ql_negation | @ql_par_expr | @ql_prefix_cast | @ql_qualified_expr | @ql_quantified | @ql_range | @ql_set_literal | @ql_special_call | @ql_super_ref | @ql_unary_expr | @ql_variable
@@ -855,6 +886,20 @@ ql_set_literal_def(
855886
unique int id: @ql_set_literal
856887
);
857888

889+
ql_signature_expr_predicate(
890+
unique int ql_signature_expr: @ql_signature_expr ref,
891+
unique int predicate: @ql_predicate_expr ref
892+
);
893+
894+
ql_signature_expr_type_expr(
895+
unique int ql_signature_expr: @ql_signature_expr ref,
896+
unique int type_expr: @ql_type_expr ref
897+
);
898+
899+
ql_signature_expr_def(
900+
unique int id: @ql_signature_expr
901+
);
902+
858903
ql_special_call_def(
859904
unique int id: @ql_special_call,
860905
int child: @ql_token_special_id ref
@@ -883,7 +928,12 @@ ql_type_expr_name(
883928
unique int name: @ql_token_class_name ref
884929
);
885930

886-
@ql_typeExpr_child_type = @ql_module_expr | @ql_token_dbtype | @ql_token_primitive_type
931+
ql_type_expr_qualifier(
932+
unique int ql_type_expr: @ql_type_expr ref,
933+
unique int qualifier: @ql_module_expr ref
934+
);
935+
936+
@ql_typeExpr_child_type = @ql_token_dbtype | @ql_token_primitive_type
887937

888938
ql_type_expr_child(
889939
unique int ql_type_expr: @ql_type_expr ref,
@@ -1057,7 +1107,7 @@ case @ql_token.kind of
10571107
;
10581108

10591109

1060-
@ql_ast_node = @ql_add_expr | @ql_aggregate | @ql_annot_arg | @ql_annotation | @ql_arityless_predicate_expr | @ql_as_expr | @ql_as_exprs | @ql_body | @ql_bool | @ql_call_body | @ql_call_or_unqual_agg_expr | @ql_charpred | @ql_class_member | @ql_classless_predicate | @ql_comp_term | @ql_conjunction | @ql_dataclass | @ql_datatype | @ql_datatype_branch | @ql_datatype_branches | @ql_db_annotation | @ql_db_args_annotation | @ql_db_branch | @ql_db_case_decl | @ql_db_col_type | @ql_db_column | @ql_db_entry | @ql_db_repr_type | @ql_db_table | @ql_db_table_name | @ql_db_union_decl | @ql_disjunction | @ql_expr_aggregate_body | @ql_expr_annotation | @ql_field | @ql_full_aggregate_body | @ql_higher_order_term | @ql_if_term | @ql_implication | @ql_import_directive | @ql_import_module_expr | @ql_in_expr | @ql_instance_of | @ql_literal | @ql_member_predicate | @ql_module | @ql_module_alias_body | @ql_module_expr | @ql_module_member | @ql_module_name | @ql_mul_expr | @ql_negation | @ql_order_by | @ql_order_bys | @ql_par_expr | @ql_predicate_alias_body | @ql_predicate_expr | @ql_prefix_cast | @ql_ql | @ql_qual_module_expr | @ql_qualified_expr | @ql_qualified_rhs | @ql_quantified | @ql_range | @ql_select | @ql_set_literal | @ql_special_call | @ql_super_ref | @ql_token | @ql_type_alias_body | @ql_type_expr | @ql_type_union_body | @ql_unary_expr | @ql_unqual_agg_body | @ql_var_decl | @ql_var_name | @ql_variable | @ql_yaml_comment | @ql_yaml_entry | @ql_yaml_key | @ql_yaml_keyvaluepair | @ql_yaml_listitem
1110+
@ql_ast_node = @ql_add_expr | @ql_aggregate | @ql_annot_arg | @ql_annotation | @ql_arityless_predicate_expr | @ql_as_expr | @ql_as_exprs | @ql_body | @ql_bool | @ql_call_body | @ql_call_or_unqual_agg_expr | @ql_charpred | @ql_class_member | @ql_classless_predicate | @ql_comp_term | @ql_conjunction | @ql_dataclass | @ql_datatype | @ql_datatype_branch | @ql_datatype_branches | @ql_db_annotation | @ql_db_args_annotation | @ql_db_branch | @ql_db_case_decl | @ql_db_col_type | @ql_db_column | @ql_db_entry | @ql_db_repr_type | @ql_db_table | @ql_db_table_name | @ql_db_union_decl | @ql_disjunction | @ql_expr_aggregate_body | @ql_expr_annotation | @ql_field | @ql_full_aggregate_body | @ql_higher_order_term | @ql_if_term | @ql_implication | @ql_import_directive | @ql_import_module_expr | @ql_in_expr | @ql_instance_of | @ql_literal | @ql_member_predicate | @ql_module | @ql_module_alias_body | @ql_module_expr | @ql_module_instantiation | @ql_module_member | @ql_module_name | @ql_module_param | @ql_mul_expr | @ql_negation | @ql_order_by | @ql_order_bys | @ql_par_expr | @ql_predicate_alias_body | @ql_predicate_expr | @ql_prefix_cast | @ql_ql | @ql_qual_module_expr | @ql_qualified_expr | @ql_qualified_rhs | @ql_quantified | @ql_range | @ql_select | @ql_set_literal | @ql_signature_expr | @ql_special_call | @ql_super_ref | @ql_token | @ql_type_alias_body | @ql_type_expr | @ql_type_union_body | @ql_unary_expr | @ql_unqual_agg_body | @ql_var_decl | @ql_var_name | @ql_variable | @ql_yaml_comment | @ql_yaml_entry | @ql_yaml_key | @ql_yaml_keyvaluepair | @ql_yaml_listitem
10611111

10621112
@ql_ast_node_parent = @file | @ql_ast_node
10631113

swift/extractor/SwiftExtractor.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ static void archiveFile(const SwiftExtractorConfiguration& config, swift::Source
4949
}
5050
}
5151

52-
static void extractFile(const SwiftExtractorConfiguration& config,
53-
swift::CompilerInstance& compiler,
54-
swift::SourceFile& file) {
52+
static void extractDeclarations(const SwiftExtractorConfiguration& config,
53+
swift::CompilerInstance& compiler,
54+
llvm::StringRef filename,
55+
llvm::ArrayRef<swift::Decl*> topLevelDecls) {
5556
// The extractor can be called several times from different processes with
5657
// the same input file(s)
5758
// We are using PID to avoid concurrent access
5859
// TODO: find a more robust approach to avoid collisions?
59-
std::string tempTrapName = file.getFilename().str() + '.' + std::to_string(getpid()) + ".trap";
60+
std::string tempTrapName = filename.str() + '.' + std::to_string(getpid()) + ".trap";
6061
llvm::SmallString<PATH_MAX> tempTrapPath(config.trapDir);
6162
llvm::sys::path::append(tempTrapPath, tempTrapName);
6263

@@ -84,7 +85,7 @@ static void extractFile(const SwiftExtractorConfiguration& config,
8485
TrapOutput trap{trapStream};
8586
TrapArena arena{};
8687

87-
// TODO move default location emission elsewhere, possibly in a separate global trap file
88+
// TODO: move default location emission elsewhere, possibly in a separate global trap file
8889
auto unknownFileLabel = arena.allocateLabel<FileTag>();
8990
// the following cannot conflict with actual files as those have an absolute path starting with /
9091
trap.assignKey(unknownFileLabel, "unknown");
@@ -93,22 +94,23 @@ static void extractFile(const SwiftExtractorConfiguration& config,
9394
trap.assignKey(unknownLocationLabel, "unknown");
9495
trap.emit(LocationsTrap{unknownLocationLabel, unknownFileLabel});
9596

96-
// In the case of emtpy files, the dispatcher is not called, but we still want to 'record' the
97-
// fact that the file was extracted
98-
// TODO: to be moved elsewhere
99-
llvm::SmallString<PATH_MAX> srcFilePath(file.getFilename());
100-
llvm::sys::fs::make_absolute(srcFilePath);
101-
auto fileLabel = arena.allocateLabel<FileTag>();
102-
trap.assignKey(fileLabel, srcFilePath.str().str());
103-
trap.emit(FilesTrap{fileLabel, srcFilePath.str().str()});
104-
10597
SwiftVisitor visitor(compiler.getSourceMgr(), arena, trap);
106-
for (swift::Decl* decl : file.getTopLevelDecls()) {
98+
for (swift::Decl* decl : topLevelDecls) {
10799
visitor.extract(decl);
108100
}
101+
if (topLevelDecls.empty()) {
102+
// In the case of emtpy files, the dispatcher is not called, but we still want to 'record' the
103+
// fact that the file was extracted
104+
// TODO: to be moved elsewhere
105+
llvm::SmallString<PATH_MAX> srcFilePath(filename);
106+
llvm::sys::fs::make_absolute(srcFilePath);
107+
auto fileLabel = arena.allocateLabel<FileTag>();
108+
trap.assignKey(fileLabel, srcFilePath.str().str());
109+
trap.emit(FilesTrap{fileLabel, srcFilePath.str().str()});
110+
}
109111

110112
// TODO: Pick a better name to avoid collisions
111-
std::string trapName = file.getFilename().str() + ".trap";
113+
std::string trapName = filename.str() + ".trap";
112114
llvm::SmallString<PATH_MAX> trapPath(config.trapDir);
113115
llvm::sys::path::append(trapPath, trapName);
114116

@@ -121,10 +123,24 @@ static void extractFile(const SwiftExtractorConfiguration& config,
121123

122124
void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
123125
swift::CompilerInstance& compiler) {
124-
// The extraction will only work if one (or more) `-primary-file` CLI option is provided, which
125-
// is what always happen in case of `swift build` and `xcodebuild`
126-
for (auto s : compiler.getPrimarySourceFiles()) {
127-
archiveFile(config, *s);
128-
extractFile(config, compiler, *s);
126+
for (auto& [_, module] : compiler.getASTContext().getLoadedModules()) {
127+
// We only extract system and builtin modules here as the other "user" modules can be built
128+
// during the build process and then re-used at a later stage. In this case, we extract the
129+
// user code twice: once during the module build in a form of a source file, and then as
130+
// a pre-built module during building of the dependent source files.
131+
if (module->isSystemModule() || module->isBuiltinModule()) {
132+
llvm::SmallVector<swift::Decl*> decls;
133+
module->getTopLevelDecls(decls);
134+
// TODO: pass ModuleDecl directly when we have module extraction in place?
135+
extractDeclarations(config, compiler, module->getModuleFilename(), decls);
136+
} else {
137+
// The extraction will only work if one (or more) `-primary-file` CLI option is provided,
138+
// which is what always happens in case of `swift build` and `xcodebuild`
139+
for (auto primaryFile : module->getPrimarySourceFiles()) {
140+
archiveFile(config, *primaryFile);
141+
extractDeclarations(config, compiler, primaryFile->getFilename(),
142+
primaryFile->getTopLevelDecls());
143+
}
144+
}
129145
}
130146
}

swift/ql/test/library-tests/controlflow/graph/Cfg.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import codeql.swift.controlflow.ControlFlowGraph
66
import codeql.swift.controlflow.internal.ControlFlowGraphImpl::TestOutput
77

88
class MyRelevantNode extends RelevantNode {
9-
MyRelevantNode() { exists(this) }
9+
MyRelevantNode() { getScope().getLocation().getFile().getName().matches("%swift/ql/test%") }
1010
}

0 commit comments

Comments
 (0)