diff --git a/misc/codegen/lib/schema.py b/misc/codegen/lib/schema.py index 37f0f53c5f51..5506c2d3de02 100644 --- a/misc/codegen/lib/schema.py +++ b/misc/codegen/lib/schema.py @@ -118,7 +118,7 @@ def check_types(self, known: typing.Iterable[str]): @dataclass class Schema: classes: Dict[str, Class] = field(default_factory=dict) - includes: Set[str] = field(default_factory=set) + includes: List[str] = field(default_factory=list) null: Optional[str] = None @property diff --git a/misc/codegen/loaders/schemaloader.py b/misc/codegen/loaders/schemaloader.py index ab84c8171739..d595585b01f6 100644 --- a/misc/codegen/loaders/schemaloader.py +++ b/misc/codegen/loaders/schemaloader.py @@ -135,7 +135,7 @@ def load(m: types.ModuleType) -> schema.Schema: if hasattr(defs, name): continue if name == "__includes": - includes = set(data) + includes = data continue if name.startswith("__"): continue diff --git a/rust/extractor/src/generated/.generated.list b/rust/extractor/src/generated/.generated.list index e97b3eb02c8d..b404b2e7541c 100644 --- a/rust/extractor/src/generated/.generated.list +++ b/rust/extractor/src/generated/.generated.list @@ -1,2 +1,2 @@ mod.rs 7cdfedcd68cf8e41134daf810c1af78624082b0c3e8be6570339b1a69a5d457e 7cdfedcd68cf8e41134daf810c1af78624082b0c3e8be6570339b1a69a5d457e -top.rs 569909061b9a993481764765a014327d143939778f2dbc79836e7496cdb83e1f 569909061b9a993481764765a014327d143939778f2dbc79836e7496cdb83e1f +top.rs 7150acaeab0b57039ca9f2ed20311229aab5fd48b533f13410ecc34fd8e3bda0 7150acaeab0b57039ca9f2ed20311229aab5fd48b533f13410ecc34fd8e3bda0 diff --git a/rust/extractor/src/generated/top.rs b/rust/extractor/src/generated/top.rs index 48ca548386be..de6d2106c1d7 100644 --- a/rust/extractor/src/generated/top.rs +++ b/rust/extractor/src/generated/top.rs @@ -5,48 +5,9 @@ use crate::trap::{TrapId, TrapEntry}; use codeql_extractor::trap; -#[derive(Debug)] -pub struct DbFile { - pub id: TrapId, - pub name: String, -} - -impl TrapEntry for DbFile { - fn extract_id(&mut self) -> TrapId { - std::mem::replace(&mut self.id, TrapId::Star) - } - - fn emit(self, id: trap::Label, out: &mut trap::Writer) { - out.add_tuple("db_files", vec![trap::Arg::Label(id)]); - out.add_tuple("files", vec![trap::Arg::Label(id), self.name.into()]); - } -} - -#[derive(Debug)] -pub struct DbLocation { - pub id: TrapId, - pub file: trap::Label, - pub start_line: usize, - pub start_column: usize, - pub end_line: usize, - pub end_column: usize, -} - -impl TrapEntry for DbLocation { - fn extract_id(&mut self) -> TrapId { - std::mem::replace(&mut self.id, TrapId::Star) - } - - fn emit(self, id: trap::Label, out: &mut trap::Writer) { - out.add_tuple("db_locations", vec![trap::Arg::Label(id)]); - out.add_tuple("locations", vec![trap::Arg::Label(id), self.file.into(), self.start_line.into(), self.start_column.into(), self.end_line.into(), self.end_column.into()]); - } -} - #[derive(Debug)] pub struct Label { pub id: TrapId, - pub location: Option, pub name: String, } @@ -57,16 +18,12 @@ impl TrapEntry for Label { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("labels", vec![trap::Arg::Label(id), self.name.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct MatchArm { pub id: TrapId, - pub location: Option, pub pat: trap::Label, pub guard: Option, pub expr: trap::Label, @@ -79,9 +36,6 @@ impl TrapEntry for MatchArm { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("match_arms", vec![trap::Arg::Label(id), self.pat.into(), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.guard { out.add_tuple("match_arm_guards", vec![trap::Arg::Label(id), v.into()]); } @@ -91,7 +45,6 @@ impl TrapEntry for MatchArm { #[derive(Debug)] pub struct RecordFieldPat { pub id: TrapId, - pub location: Option, pub name: String, pub pat: trap::Label, } @@ -103,16 +56,12 @@ impl TrapEntry for RecordFieldPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("record_field_pats", vec![trap::Arg::Label(id), self.name.into(), self.pat.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct RecordLitField { pub id: TrapId, - pub location: Option, pub name: String, pub expr: trap::Label, } @@ -124,16 +73,12 @@ impl TrapEntry for RecordLitField { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("record_lit_fields", vec![trap::Arg::Label(id), self.name.into(), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct TypeRef { pub id: TrapId, - pub location: Option, } impl TrapEntry for TypeRef { @@ -143,16 +88,12 @@ impl TrapEntry for TypeRef { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("type_refs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct Unimplemented { pub id: TrapId, - pub location: Option, } impl TrapEntry for Unimplemented { @@ -162,16 +103,12 @@ impl TrapEntry for Unimplemented { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("unimplementeds", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct AwaitExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, } @@ -182,16 +119,12 @@ impl TrapEntry for AwaitExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("await_exprs", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct BecomeExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, } @@ -202,16 +135,12 @@ impl TrapEntry for BecomeExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("become_exprs", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct BinaryOpExpr { pub id: TrapId, - pub location: Option, pub lhs: trap::Label, pub rhs: trap::Label, pub op: Option, @@ -224,9 +153,6 @@ impl TrapEntry for BinaryOpExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("binary_op_exprs", vec![trap::Arg::Label(id), self.lhs.into(), self.rhs.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.op { out.add_tuple("binary_op_expr_ops", vec![trap::Arg::Label(id), v.into()]); } @@ -236,7 +162,6 @@ impl TrapEntry for BinaryOpExpr { #[derive(Debug)] pub struct BindPat { pub id: TrapId, - pub location: Option, pub binding_id: String, pub subpat: Option, } @@ -248,9 +173,6 @@ impl TrapEntry for BindPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("bind_pats", vec![trap::Arg::Label(id), self.binding_id.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.subpat { out.add_tuple("bind_pat_subpats", vec![trap::Arg::Label(id), v.into()]); } @@ -260,7 +182,6 @@ impl TrapEntry for BindPat { #[derive(Debug)] pub struct BoxExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, } @@ -271,16 +192,12 @@ impl TrapEntry for BoxExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("box_exprs", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct BoxPat { pub id: TrapId, - pub location: Option, pub inner: trap::Label, } @@ -291,16 +208,12 @@ impl TrapEntry for BoxPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("box_pats", vec![trap::Arg::Label(id), self.inner.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct BreakExpr { pub id: TrapId, - pub location: Option, pub expr: Option, pub label: Option, } @@ -312,9 +225,6 @@ impl TrapEntry for BreakExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("break_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.expr { out.add_tuple("break_expr_exprs", vec![trap::Arg::Label(id), v.into()]); } @@ -327,7 +237,6 @@ impl TrapEntry for BreakExpr { #[derive(Debug)] pub struct CallExpr { pub id: TrapId, - pub location: Option, pub callee: trap::Label, pub args: Vec, pub is_assignee_expr: bool, @@ -340,9 +249,6 @@ impl TrapEntry for CallExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("call_exprs", vec![trap::Arg::Label(id), self.callee.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.args.into_iter().enumerate() { out.add_tuple("call_expr_args", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -355,7 +261,6 @@ impl TrapEntry for CallExpr { #[derive(Debug)] pub struct CastExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, pub type_ref: trap::Label, } @@ -367,16 +272,12 @@ impl TrapEntry for CastExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("cast_exprs", vec![trap::Arg::Label(id), self.expr.into(), self.type_ref.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct ClosureExpr { pub id: TrapId, - pub location: Option, pub args: Vec, pub arg_types: Vec>, pub ret_type: Option, @@ -392,9 +293,6 @@ impl TrapEntry for ClosureExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("closure_exprs", vec![trap::Arg::Label(id), self.body.into(), self.closure_kind.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.args.into_iter().enumerate() { out.add_tuple("closure_expr_args", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -415,7 +313,6 @@ impl TrapEntry for ClosureExpr { #[derive(Debug)] pub struct ConstBlockPat { pub id: TrapId, - pub location: Option, pub expr: trap::Label, } @@ -426,16 +323,12 @@ impl TrapEntry for ConstBlockPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("const_block_pats", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct ConstExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, } @@ -446,16 +339,12 @@ impl TrapEntry for ConstExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("const_exprs", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct ContinueExpr { pub id: TrapId, - pub location: Option, pub label: Option, } @@ -466,9 +355,6 @@ impl TrapEntry for ContinueExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("continue_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.label { out.add_tuple("continue_expr_labels", vec![trap::Arg::Label(id), v.into()]); } @@ -478,7 +364,6 @@ impl TrapEntry for ContinueExpr { #[derive(Debug)] pub struct ExprStmt { pub id: TrapId, - pub location: Option, pub expr: trap::Label, pub has_semicolon: bool, } @@ -490,9 +375,6 @@ impl TrapEntry for ExprStmt { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("expr_stmts", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if self.has_semicolon { out.add_tuple("expr_stmt_has_semicolon", vec![trap::Arg::Label(id)]); } @@ -502,7 +384,6 @@ impl TrapEntry for ExprStmt { #[derive(Debug)] pub struct FieldExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, pub name: String, } @@ -514,16 +395,12 @@ impl TrapEntry for FieldExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("field_exprs", vec![trap::Arg::Label(id), self.expr.into(), self.name.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct Function { pub id: TrapId, - pub location: Option, pub name: String, pub body: trap::Label, } @@ -535,16 +412,12 @@ impl TrapEntry for Function { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("functions", vec![trap::Arg::Label(id), self.name.into(), self.body.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct IfExpr { pub id: TrapId, - pub location: Option, pub condition: trap::Label, pub then: trap::Label, pub else_: Option, @@ -557,9 +430,6 @@ impl TrapEntry for IfExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("if_exprs", vec![trap::Arg::Label(id), self.condition.into(), self.then.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.else_ { out.add_tuple("if_expr_elses", vec![trap::Arg::Label(id), v.into()]); } @@ -569,7 +439,6 @@ impl TrapEntry for IfExpr { #[derive(Debug)] pub struct IndexExpr { pub id: TrapId, - pub location: Option, pub base: trap::Label, pub index: trap::Label, pub is_assignee_expr: bool, @@ -582,9 +451,6 @@ impl TrapEntry for IndexExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("index_exprs", vec![trap::Arg::Label(id), self.base.into(), self.index.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if self.is_assignee_expr { out.add_tuple("index_expr_is_assignee_expr", vec![trap::Arg::Label(id)]); } @@ -594,7 +460,6 @@ impl TrapEntry for IndexExpr { #[derive(Debug)] pub struct InlineAsmExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, } @@ -605,16 +470,12 @@ impl TrapEntry for InlineAsmExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("inline_asm_exprs", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct ItemStmt { pub id: TrapId, - pub location: Option, } impl TrapEntry for ItemStmt { @@ -624,16 +485,12 @@ impl TrapEntry for ItemStmt { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("item_stmts", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct LetExpr { pub id: TrapId, - pub location: Option, pub pat: trap::Label, pub expr: trap::Label, } @@ -645,16 +502,12 @@ impl TrapEntry for LetExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("let_exprs", vec![trap::Arg::Label(id), self.pat.into(), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct LetStmt { pub id: TrapId, - pub location: Option, pub pat: trap::Label, pub type_ref: Option, pub initializer: Option, @@ -668,9 +521,6 @@ impl TrapEntry for LetStmt { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("let_stmts", vec![trap::Arg::Label(id), self.pat.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.type_ref { out.add_tuple("let_stmt_type_refs", vec![trap::Arg::Label(id), v.into()]); } @@ -686,7 +536,6 @@ impl TrapEntry for LetStmt { #[derive(Debug)] pub struct LitPat { pub id: TrapId, - pub location: Option, pub expr: trap::Label, } @@ -697,16 +546,12 @@ impl TrapEntry for LitPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("lit_pats", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct LiteralExpr { pub id: TrapId, - pub location: Option, } impl TrapEntry for LiteralExpr { @@ -716,16 +561,12 @@ impl TrapEntry for LiteralExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("literal_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct LoopExpr { pub id: TrapId, - pub location: Option, pub body: trap::Label, pub label: Option, } @@ -737,9 +578,6 @@ impl TrapEntry for LoopExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("loop_exprs", vec![trap::Arg::Label(id), self.body.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.label { out.add_tuple("loop_expr_labels", vec![trap::Arg::Label(id), v.into()]); } @@ -749,7 +587,6 @@ impl TrapEntry for LoopExpr { #[derive(Debug)] pub struct MatchExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, pub branches: Vec, } @@ -761,9 +598,6 @@ impl TrapEntry for MatchExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("match_exprs", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.branches.into_iter().enumerate() { out.add_tuple("match_expr_branches", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -773,7 +607,6 @@ impl TrapEntry for MatchExpr { #[derive(Debug)] pub struct MethodCallExpr { pub id: TrapId, - pub location: Option, pub receiver: trap::Label, pub method_name: String, pub args: Vec, @@ -787,9 +620,6 @@ impl TrapEntry for MethodCallExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("method_call_exprs", vec![trap::Arg::Label(id), self.receiver.into(), self.method_name.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.args.into_iter().enumerate() { out.add_tuple("method_call_expr_args", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -802,7 +632,6 @@ impl TrapEntry for MethodCallExpr { #[derive(Debug)] pub struct MissingExpr { pub id: TrapId, - pub location: Option, } impl TrapEntry for MissingExpr { @@ -812,16 +641,12 @@ impl TrapEntry for MissingExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("missing_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct MissingPat { pub id: TrapId, - pub location: Option, } impl TrapEntry for MissingPat { @@ -831,16 +656,12 @@ impl TrapEntry for MissingPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("missing_pats", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct Module { pub id: TrapId, - pub location: Option, pub declarations: Vec, } @@ -851,9 +672,6 @@ impl TrapEntry for Module { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("modules", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.declarations.into_iter().enumerate() { out.add_tuple("module_declarations", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -863,7 +681,6 @@ impl TrapEntry for Module { #[derive(Debug)] pub struct OffsetOfExpr { pub id: TrapId, - pub location: Option, pub container: trap::Label, pub fields: Vec, } @@ -875,9 +692,6 @@ impl TrapEntry for OffsetOfExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("offset_of_exprs", vec![trap::Arg::Label(id), self.container.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.fields.into_iter().enumerate() { out.add_tuple("offset_of_expr_fields", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -887,7 +701,6 @@ impl TrapEntry for OffsetOfExpr { #[derive(Debug)] pub struct OrPat { pub id: TrapId, - pub location: Option, pub args: Vec, } @@ -898,9 +711,6 @@ impl TrapEntry for OrPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("or_pats", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.args.into_iter().enumerate() { out.add_tuple("or_pat_args", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -910,7 +720,6 @@ impl TrapEntry for OrPat { #[derive(Debug)] pub struct PathExpr { pub id: TrapId, - pub location: Option, pub path: trap::Label, } @@ -921,16 +730,12 @@ impl TrapEntry for PathExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("path_exprs", vec![trap::Arg::Label(id), self.path.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct PathPat { pub id: TrapId, - pub location: Option, pub path: trap::Label, } @@ -941,16 +746,12 @@ impl TrapEntry for PathPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("path_pats", vec![trap::Arg::Label(id), self.path.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct RangeExpr { pub id: TrapId, - pub location: Option, pub lhs: Option, pub rhs: Option, pub is_inclusive: bool, @@ -963,9 +764,6 @@ impl TrapEntry for RangeExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("range_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.lhs { out.add_tuple("range_expr_lhs", vec![trap::Arg::Label(id), v.into()]); } @@ -981,7 +779,6 @@ impl TrapEntry for RangeExpr { #[derive(Debug)] pub struct RangePat { pub id: TrapId, - pub location: Option, pub start: Option, pub end: Option, } @@ -993,9 +790,6 @@ impl TrapEntry for RangePat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("range_pats", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.start { out.add_tuple("range_pat_starts", vec![trap::Arg::Label(id), v.into()]); } @@ -1008,7 +802,6 @@ impl TrapEntry for RangePat { #[derive(Debug)] pub struct RecordLitExpr { pub id: TrapId, - pub location: Option, pub path: Option, pub fields: Vec, pub spread: Option, @@ -1023,9 +816,6 @@ impl TrapEntry for RecordLitExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("record_lit_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.path { out.add_tuple("record_lit_expr_paths", vec![trap::Arg::Label(id), v.into()]); } @@ -1047,7 +837,6 @@ impl TrapEntry for RecordLitExpr { #[derive(Debug)] pub struct RecordPat { pub id: TrapId, - pub location: Option, pub path: Option, pub args: Vec, pub has_ellipsis: bool, @@ -1060,9 +849,6 @@ impl TrapEntry for RecordPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("record_pats", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.path { out.add_tuple("record_pat_paths", vec![trap::Arg::Label(id), v.into()]); } @@ -1078,7 +864,6 @@ impl TrapEntry for RecordPat { #[derive(Debug)] pub struct RefExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, pub is_raw: bool, pub is_mut: bool, @@ -1091,9 +876,6 @@ impl TrapEntry for RefExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("ref_exprs", vec![trap::Arg::Label(id), self.expr.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if self.is_raw { out.add_tuple("ref_expr_is_raw", vec![trap::Arg::Label(id)]); } @@ -1106,7 +888,6 @@ impl TrapEntry for RefExpr { #[derive(Debug)] pub struct RefPat { pub id: TrapId, - pub location: Option, pub pat: trap::Label, pub is_mut: bool, } @@ -1118,9 +899,6 @@ impl TrapEntry for RefPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("ref_pats", vec![trap::Arg::Label(id), self.pat.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if self.is_mut { out.add_tuple("ref_pat_is_mut", vec![trap::Arg::Label(id)]); } @@ -1130,7 +908,6 @@ impl TrapEntry for RefPat { #[derive(Debug)] pub struct ReturnExpr { pub id: TrapId, - pub location: Option, pub expr: Option, } @@ -1141,9 +918,6 @@ impl TrapEntry for ReturnExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("return_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.expr { out.add_tuple("return_expr_exprs", vec![trap::Arg::Label(id), v.into()]); } @@ -1153,7 +927,6 @@ impl TrapEntry for ReturnExpr { #[derive(Debug)] pub struct SlicePat { pub id: TrapId, - pub location: Option, pub prefix: Vec, pub slice: Option, pub suffix: Vec, @@ -1166,9 +939,6 @@ impl TrapEntry for SlicePat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("slice_pats", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.prefix.into_iter().enumerate() { out.add_tuple("slice_pat_prefixes", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -1184,7 +954,6 @@ impl TrapEntry for SlicePat { #[derive(Debug)] pub struct TupleExpr { pub id: TrapId, - pub location: Option, pub exprs: Vec, pub is_assignee_expr: bool, } @@ -1196,9 +965,6 @@ impl TrapEntry for TupleExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("tuple_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.exprs.into_iter().enumerate() { out.add_tuple("tuple_expr_exprs", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -1211,7 +977,6 @@ impl TrapEntry for TupleExpr { #[derive(Debug)] pub struct TuplePat { pub id: TrapId, - pub location: Option, pub args: Vec, pub ellipsis_index: Option, } @@ -1223,9 +988,6 @@ impl TrapEntry for TuplePat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("tuple_pats", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.args.into_iter().enumerate() { out.add_tuple("tuple_pat_args", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -1238,7 +1000,6 @@ impl TrapEntry for TuplePat { #[derive(Debug)] pub struct TupleStructPat { pub id: TrapId, - pub location: Option, pub path: Option, pub args: Vec, pub ellipsis_index: Option, @@ -1251,9 +1012,6 @@ impl TrapEntry for TupleStructPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("tuple_struct_pats", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.path { out.add_tuple("tuple_struct_pat_paths", vec![trap::Arg::Label(id), v.into()]); } @@ -1269,7 +1027,6 @@ impl TrapEntry for TupleStructPat { #[derive(Debug)] pub struct UnaryOpExpr { pub id: TrapId, - pub location: Option, pub expr: trap::Label, pub op: String, } @@ -1281,16 +1038,12 @@ impl TrapEntry for UnaryOpExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("unary_op_exprs", vec![trap::Arg::Label(id), self.expr.into(), self.op.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct UnderscoreExpr { pub id: TrapId, - pub location: Option, } impl TrapEntry for UnderscoreExpr { @@ -1300,16 +1053,12 @@ impl TrapEntry for UnderscoreExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("underscore_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct WildPat { pub id: TrapId, - pub location: Option, } impl TrapEntry for WildPat { @@ -1319,16 +1068,12 @@ impl TrapEntry for WildPat { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("wild_pats", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct YeetExpr { pub id: TrapId, - pub location: Option, pub expr: Option, } @@ -1339,9 +1084,6 @@ impl TrapEntry for YeetExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("yeet_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.expr { out.add_tuple("yeet_expr_exprs", vec![trap::Arg::Label(id), v.into()]); } @@ -1351,7 +1093,6 @@ impl TrapEntry for YeetExpr { #[derive(Debug)] pub struct YieldExpr { pub id: TrapId, - pub location: Option, pub expr: Option, } @@ -1362,9 +1103,6 @@ impl TrapEntry for YieldExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("yield_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } if let Some(v) = self.expr { out.add_tuple("yield_expr_exprs", vec![trap::Arg::Label(id), v.into()]); } @@ -1374,7 +1112,6 @@ impl TrapEntry for YieldExpr { #[derive(Debug)] pub struct AsyncBlockExpr { pub id: TrapId, - pub location: Option, pub statements: Vec, pub tail: Option, } @@ -1386,9 +1123,6 @@ impl TrapEntry for AsyncBlockExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("async_block_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.statements.into_iter().enumerate() { out.add_tuple("block_expr_base_statements", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -1401,7 +1135,6 @@ impl TrapEntry for AsyncBlockExpr { #[derive(Debug)] pub struct BlockExpr { pub id: TrapId, - pub location: Option, pub statements: Vec, pub tail: Option, pub label: Option, @@ -1414,9 +1147,6 @@ impl TrapEntry for BlockExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("block_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.statements.into_iter().enumerate() { out.add_tuple("block_expr_base_statements", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -1432,7 +1162,6 @@ impl TrapEntry for BlockExpr { #[derive(Debug)] pub struct ElementListExpr { pub id: TrapId, - pub location: Option, pub elements: Vec, pub is_assignee_expr: bool, } @@ -1444,9 +1173,6 @@ impl TrapEntry for ElementListExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("element_list_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.elements.into_iter().enumerate() { out.add_tuple("element_list_expr_elements", vec![trap::Arg::Label(id), i.into(), v.into()]); } @@ -1459,7 +1185,6 @@ impl TrapEntry for ElementListExpr { #[derive(Debug)] pub struct RepeatExpr { pub id: TrapId, - pub location: Option, pub initializer: trap::Label, pub repeat: trap::Label, } @@ -1471,16 +1196,12 @@ impl TrapEntry for RepeatExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("repeat_exprs", vec![trap::Arg::Label(id), self.initializer.into(), self.repeat.into()]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } } } #[derive(Debug)] pub struct UnsafeBlockExpr { pub id: TrapId, - pub location: Option, pub statements: Vec, pub tail: Option, } @@ -1492,9 +1213,6 @@ impl TrapEntry for UnsafeBlockExpr { fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("unsafe_block_exprs", vec![trap::Arg::Label(id)]); - if let Some(v) = self.location { - out.add_tuple("locatable_locations", vec![trap::Arg::Label(id), v.into()]); - } for (i, v) in self.statements.into_iter().enumerate() { out.add_tuple("block_expr_base_statements", vec![trap::Arg::Label(id), i.into(), v.into()]); } diff --git a/rust/extractor/src/translate.rs b/rust/extractor/src/translate.rs index 315cfc399109..b794a1dd5fbd 100644 --- a/rust/extractor/src/translate.rs +++ b/rust/extractor/src/translate.rs @@ -62,11 +62,7 @@ impl CrateTranslator<'_> { if !self.file_labels.contains_key(&canonical) { self.archiver.archive(&canonical); canonical = fs::canonicalize(&canonical).unwrap_or(canonical); - let name = canonical.to_string_lossy(); - let label = self.trap.emit(generated::DbFile { - id: trap_key!["file;", name.as_ref()], - name: String::from(name), - }); + let label = self.trap.emit_file(&canonical); let line_index = ::line_index(self.db, file_id); self.file_labels .insert(canonical.clone(), FileData { label, line_index }); @@ -77,56 +73,55 @@ impl CrateTranslator<'_> { fn emit_location_for_ast_ptr( &mut self, + label: trap::Label, source: ra_ap_hir::InFile>, - ) -> Option - where + ) where T: AstNode, { source .file_id .file_id() - .map(|f| (f.file_id(), source)) - .and_then(|(file_id, source)| self.emit_file(file_id).map(|data| (data, source))) - .map(|(data, source)| { + .map(|f| f.file_id()) + .and_then(|file_id| self.emit_file(file_id)) + .map(|data| { let range = source.value.text_range(); - self.emit_location_for_textrange(data, range) - }) + self.emit_location_for_textrange(label, data, range) + }); } fn emit_location_for_expr( &mut self, + label: trap::Label, expr: ra_ap_hir_def::hir::ExprId, source_map: &BodySourceMap, - ) -> Option { - let source = source_map.expr_syntax(expr).ok()?; - self.emit_location_for_ast_ptr(source) + ) { + if let Ok(source) = source_map.expr_syntax(expr) { + self.emit_location_for_ast_ptr(label, source); + } } fn emit_location_for_pat( &mut self, + label: trap::Label, pat_id: ra_ap_hir_def::hir::PatId, source_map: &BodySourceMap, - ) -> Option { - let source = source_map.pat_syntax(pat_id).ok()?; - self.emit_location_for_ast_ptr(source) + ) { + if let Ok(source) = source_map.pat_syntax(pat_id) { + self.emit_location_for_ast_ptr(label, source); + } } fn emit_literal_or_const_pat( &mut self, pat: &ra_ap_hir_def::hir::LiteralOrConst, - location: Option, body: &Body, source_map: &BodySourceMap, ) -> trap::Label { match pat { ra_ap_hir_def::hir::LiteralOrConst::Literal(_literal) => { - let expr = self.trap.emit(generated::LiteralExpr { - id: TrapId::Star, - location: None, - }); + let expr = self.trap.emit(generated::LiteralExpr { id: TrapId::Star }); self.trap.emit(generated::LitPat { id: TrapId::Star, - location, expr, }) } @@ -138,14 +133,17 @@ impl CrateTranslator<'_> { fn emit_location_for_label( &mut self, + label: trap::Label, label_id: ra_ap_hir_def::hir::LabelId, source_map: &BodySourceMap, - ) -> Option { + ) { // 'catch' a panic if the source map is incomplete let source = std::panic::catch_unwind(|| source_map.label_syntax(label_id)).ok(); - source.and_then(|source| self.emit_location_for_ast_ptr(source)) + if let Some(source) = source { + self.emit_location_for_ast_ptr(label, source) + } } - fn emit_location(&mut self, entity: T) -> Option + fn emit_location(&mut self, label: trap::Label, entity: T) where T::Ast: AstNode, { @@ -155,10 +153,15 @@ impl CrateTranslator<'_> { .and_then(|(file_id, source)| self.emit_file(file_id).map(|data| (data, source))) .map(|(data, source)| { let range = source.value.syntax().text_range(); - self.emit_location_for_textrange(data, range) - }) + self.emit_location_for_textrange(label, data, range); + }); } - fn emit_location_for_textrange(&mut self, data: FileData, range: TextRange) -> trap::Label { + fn emit_location_for_textrange( + &mut self, + label: trap::Label, + data: FileData, + range: TextRange, + ) { let start = data.line_index.line_col(range.start()); let end = data.line_index.line_col( range @@ -166,7 +169,7 @@ impl CrateTranslator<'_> { .checked_sub(TextSize::new(1)) .unwrap_or(range.end()), ); - self.trap.emit_location(data.label, start, end) + self.trap.emit_location(data.label, label, start, end) } fn emit_label( &mut self, @@ -174,23 +177,21 @@ impl CrateTranslator<'_> { body: &Body, source_map: &BodySourceMap, ) -> trap::Label { - let location: Option = self.emit_location_for_label(label_id, source_map); let label = &body.labels[label_id]; - self.trap.emit(generated::Label { + let ret = self.trap.emit(generated::Label { id: TrapId::Star, - location, name: label.name.as_str().into(), - }) + }); + self.emit_location_for_label(ret, label_id, source_map); + ret } - fn emit_unimplemented(&mut self, location: Option) -> trap::Label { - self.trap.emit(generated::Unimplemented { - id: TrapId::Star, - location, - }) + fn emit_unimplemented(&mut self) -> trap::Label { + self.trap + .emit(generated::Unimplemented { id: TrapId::Star }) } - fn emit_path(&mut self, _path: &Path, location: Option) -> trap::Label { - self.emit_unimplemented(location) + fn emit_path(&mut self, _path: &Path) -> trap::Label { + self.emit_unimplemented() } fn emit_record_field_pat( @@ -200,14 +201,14 @@ impl CrateTranslator<'_> { source_map: &BodySourceMap, ) -> trap::Label { let RecordFieldPat { name, pat } = field_pat; - let location = self.emit_location_for_pat(*pat, source_map); - let pat = self.emit_pat(*pat, body, source_map); - self.trap.emit(generated::RecordFieldPat { + let pat_label = self.emit_pat(*pat, body, source_map); + let ret = self.trap.emit(generated::RecordFieldPat { id: TrapId::Star, - location, name: name.as_str().into(), - pat, - }) + pat: pat_label, + }); + self.emit_location_for_pat(ret, *pat, source_map); + ret } fn emit_record_lit_field( @@ -217,27 +218,24 @@ impl CrateTranslator<'_> { source_map: &BodySourceMap, ) -> trap::Label { let RecordLitField { name, expr } = field_expr; - let location = self.emit_location_for_expr(*expr, source_map); - let expr = self.emit_expr(*expr, body, source_map); - self.trap.emit(generated::RecordLitField { + let expr_label = self.emit_expr(*expr, body, source_map); + let ret = self.trap.emit(generated::RecordLitField { id: TrapId::Star, - location, name: name.as_str().into(), - expr, - }) + expr: expr_label, + }); + self.emit_location_for_expr(ret, *expr, source_map); + ret } fn emit_pat(&mut self, pat_id: PatId, body: &Body, source_map: &BodySourceMap) -> trap::Label { - let location: Option = self.emit_location_for_pat(pat_id, source_map); let pat = &body.pats[pat_id]; - match pat { - ra_ap_hir_def::hir::Pat::Missing => self.trap.emit(generated::MissingPat { - id: TrapId::Star, - location, - }), - ra_ap_hir_def::hir::Pat::Wild => self.trap.emit(generated::WildPat { - id: TrapId::Star, - location, - }), + let ret = match pat { + ra_ap_hir_def::hir::Pat::Missing => { + self.trap.emit(generated::MissingPat { id: TrapId::Star }) + } + ra_ap_hir_def::hir::Pat::Wild => { + self.trap.emit(generated::WildPat { id: TrapId::Star }) + } ra_ap_hir_def::hir::Pat::Tuple { args, ellipsis } => { let args = args .into_iter() @@ -246,7 +244,6 @@ impl CrateTranslator<'_> { let ellipsis_index = ellipsis.and_then(|x| x.try_into().ok()); self.trap.emit(generated::TuplePat { id: TrapId::Star, - location, args, ellipsis_index, }) @@ -258,7 +255,6 @@ impl CrateTranslator<'_> { .collect(); self.trap.emit(generated::OrPat { id: TrapId::Star, - location, args, }) } @@ -267,14 +263,13 @@ impl CrateTranslator<'_> { args, ellipsis, } => { - let path = path.as_ref().map(|path| self.emit_path(path, location)); + let path = path.as_ref().map(|path| self.emit_path(path)); let args = args .into_iter() .map(|arg| self.emit_record_field_pat(arg, body, source_map)) .collect(); self.trap.emit(generated::RecordPat { id: TrapId::Star, - location, path, args, has_ellipsis: *ellipsis, @@ -283,13 +278,12 @@ impl CrateTranslator<'_> { ra_ap_hir_def::hir::Pat::Range { start, end } => { let start = start .as_ref() - .map(|x| self.emit_literal_or_const_pat(x, location, body, source_map)); + .map(|x| self.emit_literal_or_const_pat(x, body, source_map)); let end = end .as_ref() - .map(|x| self.emit_literal_or_const_pat(x, location, body, source_map)); + .map(|x| self.emit_literal_or_const_pat(x, body, source_map)); self.trap.emit(generated::RangePat { id: TrapId::Star, - location, start, end, }) @@ -310,17 +304,15 @@ impl CrateTranslator<'_> { .collect(); self.trap.emit(generated::SlicePat { id: TrapId::Star, - location, prefix, slice, suffix, }) } ra_ap_hir_def::hir::Pat::Path(path) => { - let path = self.emit_path(path, location); + let path = self.emit_path(path); self.trap.emit(generated::PathPat { id: TrapId::Star, - location, path, }) } @@ -328,7 +320,6 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(*expr, body, source_map); self.trap.emit(generated::LitPat { id: TrapId::Star, - location, expr, }) } @@ -336,7 +327,6 @@ impl CrateTranslator<'_> { let subpat = subpat.map(|pat| self.emit_pat(pat, body, source_map)); self.trap.emit(generated::BindPat { id: TrapId::Star, - location, subpat, binding_id: body.bindings[*id].name.as_str().into(), }) @@ -346,14 +336,13 @@ impl CrateTranslator<'_> { args, ellipsis, } => { - let path = path.as_ref().map(|path| self.emit_path(path, location)); + let path = path.as_ref().map(|path| self.emit_path(path)); let args = args .into_iter() .map(|arg| self.emit_pat(*arg, body, source_map)) .collect(); self.trap.emit(generated::TupleStructPat { id: TrapId::Star, - location, path, args, ellipsis_index: ellipsis.map(|x| x as usize), @@ -363,7 +352,6 @@ impl CrateTranslator<'_> { let pat = self.emit_pat(*pat, body, source_map); self.trap.emit(generated::RefPat { id: TrapId::Star, - location, pat, is_mut: mutability.is_mut(), }) @@ -372,7 +360,6 @@ impl CrateTranslator<'_> { let inner = self.emit_pat(*inner, body, source_map); self.trap.emit(generated::BoxPat { id: TrapId::Star, - location, inner, }) } @@ -380,14 +367,15 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(*expr, body, source_map); self.trap.emit(generated::ConstBlockPat { id: TrapId::Star, - location, expr, }) } - } + }; + self.emit_location_for_pat(ret, pat_id, source_map); + ret } fn emit_type_ref(&mut self, _type_ref: &TypeRef) -> trap::Label { - self.emit_unimplemented(None) + self.emit_unimplemented() } fn emit_match_arm( &mut self, @@ -395,17 +383,17 @@ impl CrateTranslator<'_> { body: &Body, source_map: &BodySourceMap, ) -> trap::Label { - let location: Option = self.emit_location_for_pat(arm.pat, source_map); let pat = self.emit_pat(arm.pat, body, source_map); let guard = arm.guard.map(|g| self.emit_expr(g, body, source_map)); let expr = self.emit_expr(arm.expr, body, source_map); - self.trap.emit(generated::MatchArm { + let ret = self.trap.emit(generated::MatchArm { id: TrapId::Star, - location, pat, guard, expr, - }) + }); + self.emit_location_for_pat(ret, arm.pat, source_map); + ret } fn emit_stmt( &mut self, @@ -420,9 +408,7 @@ impl CrateTranslator<'_> { initializer, else_branch, } => { - // TODO: find a way to get the location of the entire statement - let location = self.emit_location_for_pat(*pat, source_map); - let pat = self.emit_pat(*pat, body, source_map); + let pat_label = self.emit_pat(*pat, body, source_map); let type_ref = type_ref .as_ref() .map(|type_ref| self.emit_type_ref(type_ref)); @@ -430,29 +416,29 @@ impl CrateTranslator<'_> { initializer.map(|initializer| self.emit_expr(initializer, body, source_map)); let else_ = else_branch.map(|else_| self.emit_expr(else_, body, source_map)); - self.trap.emit(generated::LetStmt { + let ret = self.trap.emit(generated::LetStmt { id: TrapId::Star, - location, - pat, + pat: pat_label, type_ref, initializer, else_, - }) + }); + // TODO: find a way to get the location of the entire statement + self.emit_location_for_pat(ret, *pat, source_map); + ret } Statement::Expr { expr, has_semi } => { - let location = self.emit_location_for_expr(*expr, source_map); - let expr = self.emit_expr(*expr, body, source_map); - self.trap.emit(generated::ExprStmt { + let expr_label = self.emit_expr(*expr, body, source_map); + let ret = self.trap.emit(generated::ExprStmt { id: TrapId::Star, - location, - expr, + expr: expr_label, has_semicolon: *has_semi, - }) + }); + // TODO: find a way to get the location of the entire statement + self.emit_location_for_expr(ret, *expr, source_map); + ret } - Statement::Item => self.trap.emit(generated::ItemStmt { - id: TrapId::Star, - location: None, - }), + Statement::Item => self.trap.emit(generated::ItemStmt { id: TrapId::Star }), } } fn emit_expr( @@ -461,18 +447,15 @@ impl CrateTranslator<'_> { body: &Body, source_map: &BodySourceMap, ) -> trap::Label { - let location: Option = self.emit_location_for_expr(expr_id, source_map); let expr = &body[expr_id]; - match expr { - ra_ap_hir_def::hir::Expr::Missing => self.trap.emit(generated::MissingExpr { - id: TrapId::Star, - location, - }), + let ret = match expr { + ra_ap_hir_def::hir::Expr::Missing => { + self.trap.emit(generated::MissingExpr { id: TrapId::Star }) + } ra_ap_hir_def::hir::Expr::Path(path) => { - let path = self.emit_path(path, location); + let path = self.emit_path(path); self.trap.emit(generated::PathExpr { id: TrapId::Star, - location, path, }) } @@ -486,7 +469,6 @@ impl CrateTranslator<'_> { let else_ = else_branch.map(|x| self.emit_expr(x, body, source_map)); self.trap.emit(generated::IfExpr { id: TrapId::Star, - location, condition, then, else_, @@ -497,7 +479,6 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(*expr, body, source_map); self.trap.emit(generated::LetExpr { id: TrapId::Star, - location, pat, expr, }) @@ -516,7 +497,6 @@ impl CrateTranslator<'_> { let label = label.map(|l| self.emit_label(l, body, source_map)); self.trap.emit(generated::BlockExpr { id: TrapId::Star, - location, statements, tail, label, @@ -534,7 +514,6 @@ impl CrateTranslator<'_> { let tail = tail.map(|expr_id| self.emit_expr(expr_id, body, source_map)); self.trap.emit(generated::AsyncBlockExpr { id: TrapId::Star, - location, statements, tail, }) @@ -544,7 +523,6 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(expr_id, body, source_map); self.trap.emit(generated::ConstExpr { id: TrapId::Star, - location, expr, }) } @@ -560,7 +538,6 @@ impl CrateTranslator<'_> { let tail = tail.map(|expr_id| self.emit_expr(expr_id, body, source_map)); self.trap.emit(generated::UnsafeBlockExpr { id: TrapId::Star, - location, statements, tail, }) @@ -573,7 +550,6 @@ impl CrateTranslator<'_> { let label = label.map(|l| self.emit_label(l, body, source_map)); self.trap.emit(generated::LoopExpr { id: TrapId::Star, - location, body: loop_body, label, }) @@ -590,7 +566,6 @@ impl CrateTranslator<'_> { .collect(); self.trap.emit(generated::CallExpr { id: TrapId::Star, - location, callee, args, is_assignee_expr: *is_assignee_expr, @@ -607,12 +582,9 @@ impl CrateTranslator<'_> { .into_iter() .map(|e| self.emit_expr(*e, body, source_map)) .collect(); - let generic_args = generic_args - .as_ref() - .map(|_args| self.emit_unimplemented(None)); + let generic_args = generic_args.as_ref().map(|_args| self.emit_unimplemented()); self.trap.emit(generated::MethodCallExpr { id: TrapId::Star, - location, receiver, method_name: method_name.as_str().into(), args, @@ -628,7 +600,6 @@ impl CrateTranslator<'_> { self.trap.emit(generated::MatchExpr { id: TrapId::Star, - location, expr, branches, }) @@ -637,7 +608,6 @@ impl CrateTranslator<'_> { let label = label.map(|l| self.emit_label(l, body, source_map)); self.trap.emit(generated::ContinueExpr { id: TrapId::Star, - location, label, }) } @@ -646,7 +616,6 @@ impl CrateTranslator<'_> { let label = label.map(|l| self.emit_label(l, body, source_map)); self.trap.emit(generated::BreakExpr { id: TrapId::Star, - location, expr, label, }) @@ -655,7 +624,6 @@ impl CrateTranslator<'_> { let expr = expr.map(|e| self.emit_expr(e, body, source_map)); self.trap.emit(generated::ReturnExpr { id: TrapId::Star, - location, expr, }) } @@ -663,7 +631,6 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(*expr, body, source_map); self.trap.emit(generated::BecomeExpr { id: TrapId::Star, - location, expr, }) } @@ -671,7 +638,6 @@ impl CrateTranslator<'_> { let expr = expr.map(|e| self.emit_expr(e, body, source_map)); self.trap.emit(generated::YieldExpr { id: TrapId::Star, - location, expr, }) } @@ -679,7 +645,6 @@ impl CrateTranslator<'_> { let expr = expr.map(|e| self.emit_expr(e, body, source_map)); self.trap.emit(generated::YeetExpr { id: TrapId::Star, - location, expr, }) } @@ -690,7 +655,7 @@ impl CrateTranslator<'_> { ellipsis, is_assignee_expr, } => { - let path = path.as_ref().map(|path| self.emit_path(path, location)); + let path = path.as_ref().map(|path| self.emit_path(path)); let fields = fields .into_iter() .map(|field| self.emit_record_lit_field(field, body, source_map)) @@ -698,7 +663,6 @@ impl CrateTranslator<'_> { let spread = spread.map(|expr_id| self.emit_expr(expr_id, body, source_map)); self.trap.emit(generated::RecordLitExpr { id: TrapId::Star, - location, path, fields, spread, @@ -710,7 +674,6 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(*expr, body, source_map); self.trap.emit(generated::FieldExpr { id: TrapId::Star, - location, expr, name: name.as_str().into(), }) @@ -719,7 +682,6 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(*expr, body, source_map); self.trap.emit(generated::AwaitExpr { id: TrapId::Star, - location, expr, }) } @@ -728,7 +690,6 @@ impl CrateTranslator<'_> { let type_ref: trap::Label = self.emit_type_ref(type_ref.as_ref()); self.trap.emit(generated::CastExpr { id: TrapId::Star, - location, expr, type_ref, }) @@ -741,7 +702,6 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(*expr, body, source_map); self.trap.emit(generated::RefExpr { id: TrapId::Star, - location, expr, is_mut: mutability.is_mut(), is_raw: rawness.is_raw(), @@ -751,7 +711,6 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(*expr, body, source_map); self.trap.emit(generated::BoxExpr { id: TrapId::Star, - location, expr, }) } @@ -764,7 +723,6 @@ impl CrateTranslator<'_> { }; self.trap.emit(generated::UnaryOpExpr { id: TrapId::Star, - location, expr, op: op.into(), }) @@ -775,7 +733,6 @@ impl CrateTranslator<'_> { let op = op.map(|op| format!("{op}")); self.trap.emit(generated::BinaryOpExpr { id: TrapId::Star, - location, lhs, rhs, op, @@ -790,7 +747,6 @@ impl CrateTranslator<'_> { let rhs = rhs.map(|rhs| self.emit_expr(rhs, body, source_map)); self.trap.emit(generated::RangeExpr { id: TrapId::Star, - location, lhs, rhs, is_inclusive: *range_type == RangeOp::Inclusive, @@ -805,7 +761,6 @@ impl CrateTranslator<'_> { let index = self.emit_expr(*index, body, source_map); self.trap.emit(generated::IndexExpr { id: TrapId::Star, - location, base, index, is_assignee_expr: *is_assignee_expr, @@ -837,7 +792,6 @@ impl CrateTranslator<'_> { .collect(); self.trap.emit(generated::ClosureExpr { id: TrapId::Star, - location, args, arg_types, body: expr, @@ -856,7 +810,6 @@ impl CrateTranslator<'_> { .collect(); self.trap.emit(generated::TupleExpr { id: TrapId::Star, - location, exprs, is_assignee_expr: *is_assignee_expr, }) @@ -871,7 +824,6 @@ impl CrateTranslator<'_> { .collect(); self.trap.emit(generated::ElementListExpr { id: TrapId::Star, - location, elements, is_assignee_expr: *is_assignee_expr, }) @@ -885,25 +837,21 @@ impl CrateTranslator<'_> { self.trap.emit(generated::RepeatExpr { id: TrapId::Star, - location, initializer, repeat, }) } - ra_ap_hir_def::hir::Expr::Literal(_literal) => self.trap.emit(generated::LiteralExpr { - id: TrapId::Star, - location, - }), - ra_ap_hir_def::hir::Expr::Underscore => self.trap.emit(generated::UnderscoreExpr { - id: TrapId::Star, - location, - }), + ra_ap_hir_def::hir::Expr::Literal(_literal) => { + self.trap.emit(generated::LiteralExpr { id: TrapId::Star }) + } + ra_ap_hir_def::hir::Expr::Underscore => self + .trap + .emit(generated::UnderscoreExpr { id: TrapId::Star }), ra_ap_hir_def::hir::Expr::OffsetOf(offset) => { let container = self.emit_type_ref(&offset.container); let fields = offset.fields.iter().map(|x| x.as_str().into()).collect(); self.trap.emit(generated::OffsetOfExpr { id: TrapId::Star, - location, container, fields, }) @@ -912,11 +860,12 @@ impl CrateTranslator<'_> { let expr = self.emit_expr(asm.e, body, source_map); self.trap.emit(generated::InlineAsmExpr { id: TrapId::Star, - location, expr, }) } - } + }; + self.emit_location_for_expr(ret, expr_id, source_map); + ret } fn emit_definition( @@ -925,15 +874,12 @@ impl CrateTranslator<'_> { id: ModuleDef, labels: &mut Vec, ) { - match id { - ModuleDef::Module(_) => { - self.emit_unimplemented(None); - } + let _label = match id { + ModuleDef::Module(_) => self.emit_unimplemented(), ModuleDef::Function(function) => { let def: ra_ap_hir::DefWithBody = function.into(); let name = function.name(self.db); - let location = self.emit_location(function); let body = if self.extract_dependencies || self.krate.origin(self.db).is_local() { let (body, source_map) = self.db.body_with_source_map(def.into()); @@ -941,54 +887,62 @@ impl CrateTranslator<'_> { log::trace!("{}", &txt); self.emit_expr(body.body_expr, &body, &source_map) } else { - self.trap.emit(generated::MissingExpr { - id: TrapId::Star, - location: None, - }) + self.trap.emit(generated::MissingExpr { id: TrapId::Star }) }; - labels.push(self.trap.emit(generated::Function { + let label = self.trap.emit(generated::Function { id: trap_key![module_label, name.as_str()], - location, name: name.as_str().into(), body, - })) + }); + self.emit_location(label, function); + // TODO: move this below to add all to module children. For now this causes a + // DB inconsistency + labels.push(label); + label } ModuleDef::Adt(adt) => { - let location = self.emit_location(adt); - self.emit_unimplemented(location); + let label = self.emit_unimplemented(); + self.emit_location(label, adt); + label } ModuleDef::Variant(variant) => { - let location = self.emit_location(variant); - self.emit_unimplemented(location); + let label = self.emit_unimplemented(); + self.emit_location(label, variant); + label } ModuleDef::Const(const_) => { - let location = self.emit_location(const_); - self.emit_unimplemented(location); + let label = self.emit_unimplemented(); + self.emit_location(label, const_); + label } ModuleDef::Static(static_) => { - let location = self.emit_location(static_); - self.emit_unimplemented(location); + let label = self.emit_unimplemented(); + self.emit_location(label, static_); + label } ModuleDef::Trait(trait_) => { - let location = self.emit_location(trait_); - self.emit_unimplemented(location); + let label = self.emit_unimplemented(); + self.emit_location(label, trait_); + label } ModuleDef::TraitAlias(alias) => { - let location = self.emit_location(alias); - self.emit_unimplemented(location); + let label = self.emit_unimplemented(); + self.emit_location(label, alias); + label } ModuleDef::TypeAlias(type_alias) => { - let location = self.emit_location(type_alias); - self.emit_unimplemented(location); - } - ModuleDef::BuiltinType(_builtin_type) => { - self.emit_unimplemented(None); + let label = self.emit_unimplemented(); + self.emit_location(label, type_alias); + label } + ModuleDef::BuiltinType(_builtin_type) => self.emit_unimplemented(), ModuleDef::Macro(macro_) => { - let location = self.emit_location(macro_); - self.emit_unimplemented(location); + let label = self.emit_unimplemented(); + self.emit_location(label, macro_); + label } - } + }; + //TODO: labels.push(label) for all, not just functions } fn emit_module(&mut self, label: trap::Label, module: Module) { @@ -998,7 +952,6 @@ impl CrateTranslator<'_> { } self.trap.emit(generated::Module { id: label.into(), - location: None, declarations: children, }); } diff --git a/rust/extractor/src/trap.rs b/rust/extractor/src/trap.rs index af56166cd051..e96e5c5f49d6 100644 --- a/rust/extractor/src/trap.rs +++ b/rust/extractor/src/trap.rs @@ -1,7 +1,6 @@ use crate::config::Compression; -use crate::generated; use crate::{config, path}; -use codeql_extractor::trap; +use codeql_extractor::{extractor, trap}; use log::debug; use ra_ap_ide_db::line_index::LineCol; use std::ffi::OsString; @@ -82,29 +81,35 @@ impl TrapFile { pub fn emit_location( &mut self, file_label: trap::Label, + entity_label: trap::Label, start: LineCol, end: LineCol, - ) -> trap::Label { + ) { let start_line = 1 + start.line as usize; let start_column = 1 + start.col as usize; let end_line = 1 + end.line as usize; let end_column = 1 + end.col as usize; - let (ret, _) = self.writer.location_label(trap::Location { - file_label, - start_line, - start_column, - end_line, - end_column, - }); - self.emit(generated::DbLocation { - id: ret.into(), - file: file_label, - start_line, - start_column, - end_line, - end_column, - }); - ret + let location_label = extractor::location_label( + &mut self.writer, + trap::Location { + file_label, + start_line, + start_column, + end_line, + end_column, + }, + ); + self.writer.add_tuple( + "locatable_locations", + vec![ + trap::Arg::Label(entity_label), + trap::Arg::Label(location_label), + ], + ); + } + + pub fn emit_file(&mut self, absolute_path: &Path) -> trap::Label { + extractor::populate_file(&mut self.writer, absolute_path) } pub fn label(&mut self, id: TrapId) -> trap::Label { @@ -157,9 +162,11 @@ impl TrapFileProvider { ); debug!("creating trap file {}", path.display()); path = self.trap_dir.join(path); + let mut writer = trap::Writer::new(); + extractor::populate_empty_location(&mut writer); TrapFile { path, - writer: trap::Writer::new(), + writer, compression: self.compression, } } diff --git a/rust/prefix.dbscheme b/rust/prefix.dbscheme index b422464bb902..4810e11069d4 100644 --- a/rust/prefix.dbscheme +++ b/rust/prefix.dbscheme @@ -1,6 +1,5 @@ -/** - * The source location of the snapshot. - */ -sourceLocationPrefix( - string prefix: string ref +#keyset[id] +locatable_locations( + int id: @locatable ref, + int location: @location_default ref ); diff --git a/rust/ql/.generated.list b/rust/ql/.generated.list index 5c21f4e90510..cc88ed815049 100644 --- a/rust/ql/.generated.list +++ b/rust/ql/.generated.list @@ -31,10 +31,6 @@ lib/codeql/rust/elements/ConstExpr.qll df129a4a1f65c07076e15097571ffd12b91a63d3d lib/codeql/rust/elements/ConstExprConstructor.qll b4c96adc2878047c36d7ceaba2346ef66a2269b5260a56c3d7ff6e3a332bad75 ce15bbfd1448e47d3039e912363efa607cc2c29d44b8248ac91c307af7b57016 lib/codeql/rust/elements/ContinueExpr.qll 46635ab257d8cc8ab3ba4b6786df7a2ff4d2e6360578bebde8bec1d6ae90b8c7 a0c60fb018ced67e1d6eed1c493b2c6d729636f1325b556ed5bcbcff072cd8d7 lib/codeql/rust/elements/ContinueExprConstructor.qll adc5c5b4fda5dc5102cdace41c32a6c94fe07a2e2555ced6ee62a2d2551b90a2 9dc5045b0d91a3a28cc1c0d59c6fd40620257a6c18ea8480792183c4d802fd8a -lib/codeql/rust/elements/DbFile.qll 056d363e1ba5ec08cacb2e90b8a7a3b47f52ded5dc2289edd4e85921fc50f37e 18e6926f77265a3a6319ca2f3bf3d529d17d46cebdd2ef6753357fdc53c22c70 -lib/codeql/rust/elements/DbFileConstructor.qll ea93dc49b23b1c6d800ab9d0b9cacfa9dc661bfdb04b9e6efcad2bdb050fb0ab f7a891b1786604eee57a784733555b677e2580770d51d18073b59e7ca65df1d4 -lib/codeql/rust/elements/DbLocation.qll 1f694594e8e4ab65a8781cd443ad4f864447ca88e2cb65504aee5a779393c84d 003ec72275406eb8f5ddd6ccc2b258fb7c906d4bb2c0ef1ba235f291624321ca -lib/codeql/rust/elements/DbLocationConstructor.qll 8848abace985818a5d3a6eddfc4cb200795970146d282b037b4f22ae6230b894 44dba880e17bb1072fa12451ccaae4830fd04dcc61f7403d35510309fde6906e lib/codeql/rust/elements/Declaration.qll d4ec5c83728f1837243caf2f27d06fd05ecdd2ca440112accff99bfd37b45e5f c1cd9b297be8b69207e75d24b29949b9f71c78406ee0ffd38d0b0810288d6140 lib/codeql/rust/elements/ElementListExpr.qll ec535846c4f449a3e58bb3b8dc583960ef3b42a10836ad13a6c3091f625ab463 3797f92086ecab90406b7493953d78af27b0b5c68199e2f37abc15f3d1cf88ed lib/codeql/rust/elements/ElementListExprConstructor.qll 12b06597e0700cd0eac70e42cbdc1a2d410e0ffcd05c21a213812a488b5b236b 7adb2e442f1bc362c44824aaba0ab4a7fb4a4bc550a3c96f963dc03bed582d39 @@ -130,7 +126,7 @@ lib/codeql/rust/elements/YeetExpr.qll 04a1f4f7b2bb697e30ab284720ed30c0c8e1377cac lib/codeql/rust/elements/YeetExprConstructor.qll f1871c6e0c966c52806e370dbe2956585bbfa1dcf0bd7ebfdb2bd39b7cfd0d7b a2333e80a325a921a66c34151401da12c250951952ccb0c81e5102dc62299503 lib/codeql/rust/elements/YieldExpr.qll b0f238ba2e4b83b449b44224d766b6cf6b15523a413467f608f4a711d34edc01 355fcafe43915d69a07725ec3707e66abfefc6157bd7dc1c1fd846a965c6580d lib/codeql/rust/elements/YieldExprConstructor.qll ff46ba17cc24abfcb0e310c7458d0539b704e7a771ad00853bd3a1844e4e6f82 1c13662ef01c88f1cf057d099eb46524036133e51a0e36ddf947f727ac6046bb -lib/codeql/rust/elements.qll c0e6bff934b1925ec03e55afc2b5b9127f51dc4a613e047e687bc83cdab4f196 c0e6bff934b1925ec03e55afc2b5b9127f51dc4a613e047e687bc83cdab4f196 +lib/codeql/rust/elements.qll 54bf00efa3d1846ed2ec874cbdcf5bf5789bf5df240cb90ea6c68d4b469abdd2 54bf00efa3d1846ed2ec874cbdcf5bf5789bf5df240cb90ea6c68d4b469abdd2 lib/codeql/rust/generated/ArrayExpr.qll b9778720acf4080c065897ba1471be185c0c35f3ea01c15594c0a3ee4029b231 cbc8c9dbcb805063c4b893e21ed0bf00312490a0c92ee126d49dbfff6978fa99 lib/codeql/rust/generated/AstNode.qll 0598fac7859906f4103124270dfb2fbdb838387b1c45000bf50a4b62c797ec41 f47c84878c7c9676109e358073bddab92a1dbeb4d977d236ecc1eae44d81c894 lib/codeql/rust/generated/AsyncBlockExpr.qll 1e9d3bcd5d5703bf61748f2746d3f9a4e2a12080268b29a2685b762d13c30555 1e9d3bcd5d5703bf61748f2746d3f9a4e2a12080268b29a2685b762d13c30555 @@ -149,15 +145,12 @@ lib/codeql/rust/generated/ClosureExpr.qll f0c7ce7aecc9da9663cbda3e285be73b23464f lib/codeql/rust/generated/ConstBlockPat.qll d0818fe4cee066f1e6d3439c82122942ae62941e69da686b7d5c399e820c395c 2fae5a2f0457bb7106d52ac7457afb597d7ac9658b8dcff8e76294f5fe34019a lib/codeql/rust/generated/ConstExpr.qll dd851fb049429fe965569beb75957a6a596137b333a6cd7cd588d1c4d40412c4 824825bc46a393827d5df5ae30f622ef2a053ea1f5e338c3b957625a8542cfa5 lib/codeql/rust/generated/ContinueExpr.qll 436847767d2f68f95d011df0eb8a175924c029ac747daf620a203596479b20b3 e362f28dde0bf3e6ff3b234e81a44bc5026b4c9ed38b8b7872954cca9565eece -lib/codeql/rust/generated/DbFile.qll 4dbf1931124291e0d6a958ae882f8aeef006642f72adc7ff86cffd3a4e9a970a 4dbf1931124291e0d6a958ae882f8aeef006642f72adc7ff86cffd3a4e9a970a -lib/codeql/rust/generated/DbLocation.qll 735d9351b5eb46a3231b528600dddec3a4122c18c210d4d632a8d4ceaf7f02e9 735d9351b5eb46a3231b528600dddec3a4122c18c210d4d632a8d4ceaf7f02e9 lib/codeql/rust/generated/Declaration.qll bbf5ba3792797a829b0032c41fa99d22c26e4277d655099912cdbafb80f0c8cd c4666a71099b21ad5cd83ef6f991ba18f9bef03b3ffbcedfa10aec081d6501d5 lib/codeql/rust/generated/Element.qll 21567fa7348dccdf69dd34e73cb6de7cc9c7e0f3f7bb419a1abd787f7dc851a1 21567fa7348dccdf69dd34e73cb6de7cc9c7e0f3f7bb419a1abd787f7dc851a1 lib/codeql/rust/generated/ElementListExpr.qll 3f7e9c1f7249f7283406d2e59b00af750b6dea93b284d7f25af66fe4b3345fea d340242d6072e274fbafd6ff2a5455bf53b3b77ed91539e91563d67cf2ed48f0 lib/codeql/rust/generated/Expr.qll 32cdd43d17e8b2fb7c73ec723eba89704d1e853e29d304db5eea3979fcdd2e0b 0b8382b0659afa1bd1d13d0cd492d7fbdc0fd7a5162fa658ca2973bc15ee6534 lib/codeql/rust/generated/ExprStmt.qll f35543fe1481f768eb8abe3fd0d36e2dedf00f0d1adbc31562e6280ef291d0b6 0663097b4b539c5f35dab9b26ab55baee879c7ef543810581347a8951aee46d9 lib/codeql/rust/generated/FieldExpr.qll 25dd7f15ee3fe1b0de4371cab9df83d3713c1612501e5496c9a15df8d093a755 3c75b0136d1849f6de1bbd14bda4285c52d51c8a6427984c7e5302c05d706e99 -lib/codeql/rust/generated/File.qll 2eff5c882d044d2e360fe6382fb55e5a45f6ccb9df323cfa1fae41eae9d2a47f 87e7a906b3a1b4de056952e288ddd7f69fa7cb1bd9dc7dd3972868a9002ac1e4 lib/codeql/rust/generated/Function.qll 84a00eb88479efdfe70fe51c3b5cb27ae64a54b48dcca1f2e02f68691b7d907a cde5b965ab27e811f0d24eb4f12bca90c3e8aec3a4c1d9b8bd0023745dfab105 lib/codeql/rust/generated/IfExpr.qll 7d8e5bd93bb8eda6d5d6551431b0389a2ec5893bd8c13276205d6677856c8341 935bf1be8f790a52e71a6a28456f2f1a5c5cbe6e64bf20fa8602980560899835 lib/codeql/rust/generated/IndexExpr.qll d44004268aa2e7d79e29133eb593d748beef1d4612318ef469220b3c2252a057 86892c04c59936d33f0cfd5272a04a6ef726f477c2e8f4ef27dae7240af9c804 @@ -168,8 +161,7 @@ lib/codeql/rust/generated/LetExpr.qll 896efc732db1ddc8be7281408dfeaf6f04f29d25ee lib/codeql/rust/generated/LetStmt.qll 1f8fda11b71689fb1a1b9b25a2ce046c56f36f26cddb354805bd395a03e4db3d 80ad6ea6afb1891a02db434cfefdb95b0fb7d766af6246ff27129dc15cb48ace lib/codeql/rust/generated/LitPat.qll 92c3c0f32ab9d5b08e246231e5465fe18536dee99351f73e158048bb007baf8a 6736a7824e5bdb0cc16de1231bdb5169d2f48251d5917bf2c31a36422b0bf2fd lib/codeql/rust/generated/LiteralExpr.qll 9202e11f56a2c5e99fb98ed784c7ca043c1f5d80680b48ba4ea340dd689ebe55 9202e11f56a2c5e99fb98ed784c7ca043c1f5d80680b48ba4ea340dd689ebe55 -lib/codeql/rust/generated/Locatable.qll 9e9685bba50ad2220701f3465e63af9331f7f9dc548ad906ff954fc2ce0a4400 78c89b2cc78a357d682ab65310dd474603071f07c1eaaab07711714ce17549f2 -lib/codeql/rust/generated/Location.qll bce4c72988ec6fedd1439c60a37c45aa6147c962904709ef9f12206937174be4 d57000571771a2d997c50d9a43ef1c2f075960f847effa0e80ea91fd4c6b4d6c +lib/codeql/rust/generated/Locatable.qll 2d2b9a7cf26d6134b1e4a2047dfe99f09deb9231a05047d37b697c11ea91d0d2 dae7e0c66024d7f03a58d71ffef93cf23aeb629d7a21d36901c443fb29ff4d3d lib/codeql/rust/generated/LoopExpr.qll e9304e282a97984e147b92ec7061c98fde238a8691e945e4cb775ccf34c27b0c 65b859e44e83bddb710d4ce9e5ab1346b98eaaa46509671776b75c9e8f1c1667 lib/codeql/rust/generated/MatchArm.qll 5ad0dc254b6d58ccd856e4235b68ca0226a898c33f1f6b6fe7b48266a01ca627 f519af39f45e820eb3a70cefb0e4dfb541c3cf17952c00c6dd7e59f854a629bd lib/codeql/rust/generated/MatchExpr.qll f8b422699337324c91ec9c55024630efe114ca617f4f088a18d180df132d5754 13169bde872f533f55aa196d7150761857c593a4657ab34051346dde14e736c7 @@ -179,14 +171,14 @@ lib/codeql/rust/generated/MissingPat.qll 0d8034cee20bacf07ebb9337c797f53a25686a1 lib/codeql/rust/generated/Module.qll 2a931a4f2cdb2fee00ed83af045ea63d36b7dbd708e58c30445b5610feaae333 cd62add5c31a509f965aa294f44a1607ec7c62e3a9e3fe9ee063b3c814f4eb62 lib/codeql/rust/generated/OffsetOfExpr.qll 58dfd632efcb48de7fe6ffbcb2192fcf95bfabdb407a751133f63a0e32ae7489 21ebb1b3d66849fc21c04083cfa751eb56c55809cd52664020bd61ccfbe5ea68 lib/codeql/rust/generated/OrPat.qll f8fe5c7b83a08dabcc530484a696274930040ea13501ae20f1426faeec67bcf0 f3adb3148890531b698570a48740335983a5e81977ba4ac651778f940f184398 -lib/codeql/rust/generated/ParentChild.qll 956f55ac17d66926c59f76d391b75c30767d236b145d6ae402f4308fa22a7d01 dd3cabb5b4a9ba42be60345f445cde18b0d7be934aeb94d7312eeef122e148d2 +lib/codeql/rust/generated/ParentChild.qll e1658ad4b3406b882726ed623e3668b99d1353c92c22f2939e2b7fc2b4053711 739da3c782d40cbd83c2b1d752376a0363802b64b950bea61e79cfb63bdd4f6d lib/codeql/rust/generated/Pat.qll fe1c72856442dbab5655eff93f86c2cbce8d69d9fa1f99a0f9203061ea1112a4 d85d86e8b6c48df733589d186f610b1cd9086629180701e017774bddc62402c7 lib/codeql/rust/generated/PathExpr.qll 3664ed2ad08097e4446b0fdad148118c754f8203541ae588d967ba9d79b6bf21 0d987d2358fe9b42e57585e7ae906de80e9f4ccf7c20e1d9bb7624ffbad96941 lib/codeql/rust/generated/PathPat.qll acc4dda795bae97626a8d0041538f3ba1f0b591c743fed381cf198a8b04653cb c3deee1b3bb9bd37ae3ed4761d8ee2f498384fe5e1f5e31c0f9b99dfd864a0d5 lib/codeql/rust/generated/PureSynthConstructors.qll 5eb1fc4f6a04172c34ae31e4931e4bf1f8b72fbe414c5f644731a45372d13573 5eb1fc4f6a04172c34ae31e4931e4bf1f8b72fbe414c5f644731a45372d13573 lib/codeql/rust/generated/RangeExpr.qll f499d8c1f260d6262a55c1f3640aaee832ed8c9aac922cb2e05fefbca4509053 a01563640bc23fbce9d33da756bc209fd16155dc76a7fed4ba325979723f48a5 lib/codeql/rust/generated/RangePat.qll 6ec95f6cb9c4bd93b38990bb1e3b89b526624305ac6ee7b94e6fb0a2f3db28fc 0e193f3816a7587d5103dba421bc2bf22b869522353d4e3f43d49a792eac6cf4 -lib/codeql/rust/generated/Raw.qll 48d1e7d642bd2a7605dbafe3929c558560054a4e4e3e4b36925a8bfafb7536b9 3b881e64127e9b41fee7e091de725f5cd1cb1263e4a52c02adb1fb339fe36c3d +lib/codeql/rust/generated/Raw.qll fcc1c9f3b06f7c1ae5ad6f7e7ab508c5fbbe9f13a5888db7e1a1e878d53b0f7e 45b6ee33b3ebe83d7f297eb0a9700da42648dc6228caed1e6649e940ea304907 lib/codeql/rust/generated/RecordFieldPat.qll 26bed2285d849b9b7ac52d86131eacb40df912db350e423e81fb98c393c08a69 05ed735aecee90901a1bdfae05d9f85d7f6581616eca3a9262fdef6673222f9b lib/codeql/rust/generated/RecordLitExpr.qll 1d3264446ff57e8b169f1ad6da150b2d457d6b60eda0ff63e2353eb8ef9e9113 f523dd5ce7f4bac0aafab7b27c6cfe766c72a9ee0c92d7a263347e67edf096df lib/codeql/rust/generated/RecordLitField.qll bc704b56a986f3a399dc9c3dc2b61cca0d40cd389c694b9fe13374780835ffcc ab6b05a87f240a97cc2a8c15bb84a1338ad33ce367619125a8514e8815fd050e @@ -197,8 +189,8 @@ lib/codeql/rust/generated/RepeatExpr.qll 43aff00e966e4550179d756489e4cbc30618d66 lib/codeql/rust/generated/ReturnExpr.qll 6160f3a14ff1cbd6a297edae015769f90e8e31201639828d8a9d0d6e38c1ef99 b8c8a12f78281e558d230c6959236780758e9645fe22aca697b948535c20f9be lib/codeql/rust/generated/SlicePat.qll b4de6692eebf1205940e04da963adc20a07b15923c3c3a7a512a24e3bd8342c9 ee9b919983807f39d97cfe8ca66b76bdbfde76db02db5c93268ce22cbddf4213 lib/codeql/rust/generated/Stmt.qll 55688c8f42f6e7fd1b871e572d75fac60d0543e38c4be4638abbb00187651d3d f978006a8453137f989249e849a7c935a090da3a9b0116145da80068760e12fd -lib/codeql/rust/generated/Synth.qll 03ecd0d7e89aca555d2393bbea8de1cde0476e28fb9f198ed3355a74f1b5c1c8 11855cc446c2d8a77503795a7c395e86ff149ea10d773a6e50e54b08dd438642 -lib/codeql/rust/generated/SynthConstructors.qll 07106a119dcfc7a839454d1fa66c0e65d6ab17eeace40cd5bc857a65dc7c859b 07106a119dcfc7a839454d1fa66c0e65d6ab17eeace40cd5bc857a65dc7c859b +lib/codeql/rust/generated/Synth.qll 6d1c4648a7f705bf5d7e8f8081f835c03feb6eee99bbf5fcca825902cb0cac20 620582c3743f8e0172288660bf5b2f344d8696620675ad0a04df20da55ba9c45 +lib/codeql/rust/generated/SynthConstructors.qll 15f62ecc76505a326df6a6d61896892ecbacdd1edc8bb4fede39f1a84e36431e 15f62ecc76505a326df6a6d61896892ecbacdd1edc8bb4fede39f1a84e36431e lib/codeql/rust/generated/TupleExpr.qll 13e4dbc1afcabf388c793145cd399789f4014662f2eed1d49cbe96eeb8355413 bfa0708885c120bad24e29deb29641c69a5e5361654f3a144ead9543bfbd7197 lib/codeql/rust/generated/TuplePat.qll 23911b2ac7ee2279df8ef40a6e447437ef0ed62518504b17874a7652bf5e3f4b fc4f6f7ea40754290de194ac55939f08549bd637104baf8dc84ca3938bcbd1f1 lib/codeql/rust/generated/TupleStructPat.qll fff004cce780501eac94fe4b146619a84e02c85cae12ffeba5a4058e8c9738ea 738659f8208aa65d1d8cf601e0d92a90a890d6cbaec51cf04c81fc75a827e30b @@ -206,14 +198,11 @@ lib/codeql/rust/generated/TypeRef.qll 7cc468c2f473ee13c11a97c4360100376560e8fc42 lib/codeql/rust/generated/UnaryOpExpr.qll fd55d4bc9cd1a49d1f38f02fef16771f29bb5fb2512abd18341d56665859d18c f271ef5036410c018f48d6f15b17cb9beaf4111a42fc638ac4ed3db974a5f870 lib/codeql/rust/generated/UnderscoreExpr.qll cd7f615e41562b80d89e413c1c808048da7746fd445f0eb6ad8c5d9996b44d5d cd7f615e41562b80d89e413c1c808048da7746fd445f0eb6ad8c5d9996b44d5d lib/codeql/rust/generated/Unimplemented.qll 375b7935b7f4103728ece3042282ae82d19e471d7a9fa58c8cbdea31ea0cb113 375b7935b7f4103728ece3042282ae82d19e471d7a9fa58c8cbdea31ea0cb113 -lib/codeql/rust/generated/UnknownFile.qll ec9d1a3f15ecbf1743d4e39cb3b2f217aa9b54951c93302c2c4c238c3f0ce595 ec9d1a3f15ecbf1743d4e39cb3b2f217aa9b54951c93302c2c4c238c3f0ce595 -lib/codeql/rust/generated/UnknownLocation.qll a19e2838c52d702d268ae530f3dbd6fcd8bb28a237a52636a960f225454103cf a19e2838c52d702d268ae530f3dbd6fcd8bb28a237a52636a960f225454103cf lib/codeql/rust/generated/UnsafeBlockExpr.qll 8464597373ea46f6391394f02c4ceb93ffe8441b434e6e71907b0a3369f72d8e 8464597373ea46f6391394f02c4ceb93ffe8441b434e6e71907b0a3369f72d8e lib/codeql/rust/generated/WildPat.qll 8a2cede00ac2941cb94e294ffa81ada9ae6e61d8d8a720ce4f288740345802f8 8a2cede00ac2941cb94e294ffa81ada9ae6e61d8d8a720ce4f288740345802f8 lib/codeql/rust/generated/YeetExpr.qll 2b37cf55ec26958cf226885e99d81c8bbc6ece69fbe92d9fcc8884ee0bc4aad4 e371531507311ea8a9fbaac74442fe9994ae85f0acdb79cc870e5318af52aba9 lib/codeql/rust/generated/YieldExpr.qll 70ca98e14b24f12a3cbfe690417543fdce45b162f241834e2f7f58543aa11bda 40fe1281107317a7d80c7141229eed9c6904805dff615dfd0141ede2457e2c57 test/extractor-tests/generated/Expr/MISSING_SOURCE.txt cc7c395e7c651d62596826b1a0bedf10f35d01b8afeef47600b4ddaf804f406e cc7c395e7c651d62596826b1a0bedf10f35d01b8afeef47600b4ddaf804f406e -test/extractor-tests/generated/File/File.ql dec43be882fad904fab0c6447ca93633d801cb08ff8bec309befde7d2b9e5dda 74e1f1d698558c35fa03935cc34f4c8145d376b56d7657b18aeb338f5ca752cf test/extractor-tests/generated/Function/Function.ql c49434420dbb6fc3d9e6294161dcd3d3b306fae5ba5c85b610e534b8b15ef74c fe02208b673b74eebed92b5cbb3a8a06c31c0681eb28f3e596515663f14fa9e2 test/extractor-tests/generated/Module/MISSING_SOURCE.txt cc7c395e7c651d62596826b1a0bedf10f35d01b8afeef47600b4ddaf804f406e cc7c395e7c651d62596826b1a0bedf10f35d01b8afeef47600b4ddaf804f406e test/extractor-tests/generated/Pat/MISSING_SOURCE.txt cc7c395e7c651d62596826b1a0bedf10f35d01b8afeef47600b4ddaf804f406e cc7c395e7c651d62596826b1a0bedf10f35d01b8afeef47600b4ddaf804f406e diff --git a/rust/ql/.gitattributes b/rust/ql/.gitattributes index 048e925f2742..7cbeda2ad1b6 100644 --- a/rust/ql/.gitattributes +++ b/rust/ql/.gitattributes @@ -33,10 +33,6 @@ /lib/codeql/rust/elements/ConstExprConstructor.qll linguist-generated /lib/codeql/rust/elements/ContinueExpr.qll linguist-generated /lib/codeql/rust/elements/ContinueExprConstructor.qll linguist-generated -/lib/codeql/rust/elements/DbFile.qll linguist-generated -/lib/codeql/rust/elements/DbFileConstructor.qll linguist-generated -/lib/codeql/rust/elements/DbLocation.qll linguist-generated -/lib/codeql/rust/elements/DbLocationConstructor.qll linguist-generated /lib/codeql/rust/elements/Declaration.qll linguist-generated /lib/codeql/rust/elements/ElementListExpr.qll linguist-generated /lib/codeql/rust/elements/ElementListExprConstructor.qll linguist-generated @@ -151,15 +147,12 @@ /lib/codeql/rust/generated/ConstBlockPat.qll linguist-generated /lib/codeql/rust/generated/ConstExpr.qll linguist-generated /lib/codeql/rust/generated/ContinueExpr.qll linguist-generated -/lib/codeql/rust/generated/DbFile.qll linguist-generated -/lib/codeql/rust/generated/DbLocation.qll linguist-generated /lib/codeql/rust/generated/Declaration.qll linguist-generated /lib/codeql/rust/generated/Element.qll linguist-generated /lib/codeql/rust/generated/ElementListExpr.qll linguist-generated /lib/codeql/rust/generated/Expr.qll linguist-generated /lib/codeql/rust/generated/ExprStmt.qll linguist-generated /lib/codeql/rust/generated/FieldExpr.qll linguist-generated -/lib/codeql/rust/generated/File.qll linguist-generated /lib/codeql/rust/generated/Function.qll linguist-generated /lib/codeql/rust/generated/IfExpr.qll linguist-generated /lib/codeql/rust/generated/IndexExpr.qll linguist-generated @@ -171,7 +164,6 @@ /lib/codeql/rust/generated/LitPat.qll linguist-generated /lib/codeql/rust/generated/LiteralExpr.qll linguist-generated /lib/codeql/rust/generated/Locatable.qll linguist-generated -/lib/codeql/rust/generated/Location.qll linguist-generated /lib/codeql/rust/generated/LoopExpr.qll linguist-generated /lib/codeql/rust/generated/MatchArm.qll linguist-generated /lib/codeql/rust/generated/MatchExpr.qll linguist-generated @@ -208,14 +200,11 @@ /lib/codeql/rust/generated/UnaryOpExpr.qll linguist-generated /lib/codeql/rust/generated/UnderscoreExpr.qll linguist-generated /lib/codeql/rust/generated/Unimplemented.qll linguist-generated -/lib/codeql/rust/generated/UnknownFile.qll linguist-generated -/lib/codeql/rust/generated/UnknownLocation.qll linguist-generated /lib/codeql/rust/generated/UnsafeBlockExpr.qll linguist-generated /lib/codeql/rust/generated/WildPat.qll linguist-generated /lib/codeql/rust/generated/YeetExpr.qll linguist-generated /lib/codeql/rust/generated/YieldExpr.qll linguist-generated /test/extractor-tests/generated/Expr/MISSING_SOURCE.txt linguist-generated -/test/extractor-tests/generated/File/File.ql linguist-generated /test/extractor-tests/generated/Function/Function.ql linguist-generated /test/extractor-tests/generated/Module/MISSING_SOURCE.txt linguist-generated /test/extractor-tests/generated/Pat/MISSING_SOURCE.txt linguist-generated diff --git a/rust/ql/lib/codeql/IDEContextual.qll b/rust/ql/lib/codeql/IDEContextual.qll index cfb127a36e7b..538232fed947 100644 --- a/rust/ql/lib/codeql/IDEContextual.qll +++ b/rust/ql/lib/codeql/IDEContextual.qll @@ -2,7 +2,7 @@ * Provides shared predicates related to contextual queries in the code viewer. */ -private import codeql.rust.elements.File +import codeql.files.FileSystem private import codeql.util.FileSystem /** diff --git a/rust/ql/lib/codeql/Locations.qll b/rust/ql/lib/codeql/Locations.qll new file mode 100644 index 000000000000..148c6b01f2d9 --- /dev/null +++ b/rust/ql/lib/codeql/Locations.qll @@ -0,0 +1,68 @@ +/** Provides classes for working with locations. */ + +import files.FileSystem + +/** + * A location as given by a file, a start line, a start column, + * an end line, and an end column. + * + * For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). + */ +class Location extends @location_default { + /** Gets the file for this location. */ + File getFile() { locations_default(this, result, _, _, _, _) } + + /** Gets the 1-based line number (inclusive) where this location starts. */ + int getStartLine() { locations_default(this, _, result, _, _, _) } + + /** Gets the 1-based column number (inclusive) where this location starts. */ + int getStartColumn() { locations_default(this, _, _, result, _, _) } + + /** Gets the 1-based line number (inclusive) where this location ends. */ + int getEndLine() { locations_default(this, _, _, _, result, _) } + + /** Gets the 1-based column number (inclusive) where this location ends. */ + int getEndColumn() { locations_default(this, _, _, _, _, result) } + + /** Gets the number of lines covered by this location. */ + int getNumLines() { result = this.getEndLine() - this.getStartLine() + 1 } + + /** Gets a textual representation of this element. */ + bindingset[this] + pragma[inline_late] + string toString() { + exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | + this.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and + result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn + ) + } + + /** + * Holds if this element is at the specified location. + * The location spans column `startcolumn` of line `startline` to + * column `endcolumn` of line `endline` in file `filepath`. + * For more information, see + * [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). + */ + predicate hasLocationInfo( + string filepath, int startline, int startcolumn, int endline, int endcolumn + ) { + exists(File f | + locations_default(this, f, startline, startcolumn, endline, endcolumn) and + filepath = f.getAbsolutePath() + ) + } + + /** Holds if this location starts strictly before the specified location. */ + pragma[inline] + predicate strictlyBefore(Location other) { + this.getStartLine() < other.getStartLine() + or + this.getStartLine() = other.getStartLine() and this.getStartColumn() < other.getStartColumn() + } +} + +/** An entity representing an empty location. */ +class EmptyLocation extends Location { + EmptyLocation() { empty_location(this) } +} diff --git a/rust/ql/lib/codeql/files/FileSystem.qll b/rust/ql/lib/codeql/files/FileSystem.qll new file mode 100644 index 000000000000..528dde52fd91 --- /dev/null +++ b/rust/ql/lib/codeql/files/FileSystem.qll @@ -0,0 +1,36 @@ +/** Provides classes for working with files and folders. */ + +private import codeql.Locations +private import codeql.util.FileSystem + +private module Input implements InputSig { + abstract class ContainerBase extends @container { + abstract string getAbsolutePath(); + + ContainerBase getParentContainer() { containerparent(result, this) } + + string toString() { result = this.getAbsolutePath() } + } + + class FolderBase extends ContainerBase, @folder { + override string getAbsolutePath() { folders(this, result) } + } + + class FileBase extends ContainerBase, @file { + override string getAbsolutePath() { files(this, result) } + } + + predicate hasSourceLocationPrefix = sourceLocationPrefix/1; +} + +private module Impl = Make; + +class Container = Impl::Container; + +class Folder = Impl::Folder; + +/** A file. */ +class File extends Container, Impl::File { + /** Holds if this file was extracted from ordinary source code. */ + predicate fromSource() { any() } +} diff --git a/rust/ql/lib/codeql/rust/controlflow/internal/PrintCfg.ql b/rust/ql/lib/codeql/rust/controlflow/internal/PrintCfg.ql index d4db94ca22ae..e7b25ea8df8e 100644 --- a/rust/ql/lib/codeql/rust/controlflow/internal/PrintCfg.ql +++ b/rust/ql/lib/codeql/rust/controlflow/internal/PrintCfg.ql @@ -7,7 +7,7 @@ * @tags ide-contextual-queries/print-cfg */ -private import codeql.rust.elements.File +private import codeql.files.FileSystem private import codeql.rust.controlflow.internal.ControlFlowGraphImpl private import codeql.rust.controlflow.ControlFlowGraph diff --git a/rust/ql/lib/codeql/rust/elements.qll b/rust/ql/lib/codeql/rust/elements.qll index efb06ae9eea2..c470b41d84c7 100644 --- a/rust/ql/lib/codeql/rust/elements.qll +++ b/rust/ql/lib/codeql/rust/elements.qll @@ -21,15 +21,12 @@ import codeql.rust.elements.ClosureExpr import codeql.rust.elements.ConstBlockPat import codeql.rust.elements.ConstExpr import codeql.rust.elements.ContinueExpr -import codeql.rust.elements.DbFile -import codeql.rust.elements.DbLocation import codeql.rust.elements.Declaration import codeql.rust.elements.Element import codeql.rust.elements.ElementListExpr import codeql.rust.elements.Expr import codeql.rust.elements.ExprStmt import codeql.rust.elements.FieldExpr -import codeql.rust.elements.File import codeql.rust.elements.Function import codeql.rust.elements.IfExpr import codeql.rust.elements.IndexExpr @@ -41,7 +38,6 @@ import codeql.rust.elements.LetStmt import codeql.rust.elements.LitPat import codeql.rust.elements.LiteralExpr import codeql.rust.elements.Locatable -import codeql.rust.elements.Location import codeql.rust.elements.LoopExpr import codeql.rust.elements.MatchArm import codeql.rust.elements.MatchExpr @@ -73,8 +69,6 @@ import codeql.rust.elements.TypeRef import codeql.rust.elements.UnaryOpExpr import codeql.rust.elements.UnderscoreExpr import codeql.rust.elements.Unimplemented -import codeql.rust.elements.UnknownFile -import codeql.rust.elements.UnknownLocation import codeql.rust.elements.UnsafeBlockExpr import codeql.rust.elements.WildPat import codeql.rust.elements.YeetExpr diff --git a/rust/ql/lib/codeql/rust/elements/DbFile.qll b/rust/ql/lib/codeql/rust/elements/DbFile.qll deleted file mode 100644 index d1659d5206dd..000000000000 --- a/rust/ql/lib/codeql/rust/elements/DbFile.qll +++ /dev/null @@ -1,8 +0,0 @@ -// generated by codegen, remove this comment if you wish to edit this file -/** - * This module provides a hand-modifiable wrapper around the generated class `DbFile`. - */ - -private import codeql.rust.generated.DbFile - -class DbFile extends Generated::DbFile { } diff --git a/rust/ql/lib/codeql/rust/elements/DbFileConstructor.qll b/rust/ql/lib/codeql/rust/elements/DbFileConstructor.qll deleted file mode 100644 index 974a4f8ceefe..000000000000 --- a/rust/ql/lib/codeql/rust/elements/DbFileConstructor.qll +++ /dev/null @@ -1,14 +0,0 @@ -// generated by codegen, remove this comment if you wish to edit this file -/** - * This module defines the hook used internally to tweak the characteristic predicate of - * `DbFile` synthesized instances. - * INTERNAL: Do not use. - */ - -private import codeql.rust.generated.Raw - -/** - * The characteristic predicate of `DbFile` synthesized instances. - * INTERNAL: Do not use. - */ -predicate constructDbFile(Raw::DbFile id) { any() } diff --git a/rust/ql/lib/codeql/rust/elements/DbLocation.qll b/rust/ql/lib/codeql/rust/elements/DbLocation.qll deleted file mode 100644 index 727e4e8790f6..000000000000 --- a/rust/ql/lib/codeql/rust/elements/DbLocation.qll +++ /dev/null @@ -1,8 +0,0 @@ -// generated by codegen, remove this comment if you wish to edit this file -/** - * This module provides a hand-modifiable wrapper around the generated class `DbLocation`. - */ - -private import codeql.rust.generated.DbLocation - -class DbLocation extends Generated::DbLocation { } diff --git a/rust/ql/lib/codeql/rust/elements/DbLocationConstructor.qll b/rust/ql/lib/codeql/rust/elements/DbLocationConstructor.qll deleted file mode 100644 index af8518a06d21..000000000000 --- a/rust/ql/lib/codeql/rust/elements/DbLocationConstructor.qll +++ /dev/null @@ -1,14 +0,0 @@ -// generated by codegen, remove this comment if you wish to edit this file -/** - * This module defines the hook used internally to tweak the characteristic predicate of - * `DbLocation` synthesized instances. - * INTERNAL: Do not use. - */ - -private import codeql.rust.generated.Raw - -/** - * The characteristic predicate of `DbLocation` synthesized instances. - * INTERNAL: Do not use. - */ -predicate constructDbLocation(Raw::DbLocation id) { any() } diff --git a/rust/ql/lib/codeql/rust/elements/File.qll b/rust/ql/lib/codeql/rust/elements/File.qll deleted file mode 100644 index f9d2e62017fe..000000000000 --- a/rust/ql/lib/codeql/rust/elements/File.qll +++ /dev/null @@ -1,111 +0,0 @@ -/** - * This module provides a hand-modifiable wrapper around the generated class `File`. - */ - -private import codeql.rust.generated.File -private import codeql.rust.elements.Location -private import codeql.rust.elements.UnknownLocation - -class File extends Generated::File { - /** toString */ - override string toString() { result = this.getAbsolutePath() } - - /** Gets the absolute path of this file. */ - string getAbsolutePath() { result = this.getName() } - - /** Gets the full name of this file. */ - string getFullName() { result = this.getAbsolutePath() } - - /** Gets the URL of this file. */ - string getURL() { result = "file://" + this.getAbsolutePath() + ":0:0:0:0" } - - /** - * Holds if either, - * - `part` is the base name of this container and `i = 1`, or - * - `part` is the stem of this container and `i = 2`, or - * - `part` is the extension of this container and `i = 3`. - */ - cached - private predicate splitAbsolutePath(string part, int i) { - part = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(?:\\.([^.]*))?)", i) - } - - /** Gets the base name of this file. */ - string getBaseName() { this.splitAbsolutePath(result, 1) } - - /** - * Gets the extension of this container, that is, the suffix of its base name - * after the last dot character, if any. - * - * In particular, - * - * - if the name does not include a dot, there is no extension, so this - * predicate has no result; - * - if the name ends in a dot, the extension is the empty string; - * - if the name contains multiple dots, the extension follows the last dot. - * - * Here are some examples of absolute paths and the corresponding extensions - * (surrounded with quotes to avoid ambiguity): - * - * - * - * - * - * - * - * - *
Absolute pathExtension
"/tmp/tst.txt""txt"
"/tmp/.classpath""classpath"
"/bin/bash"not defined
"/tmp/tst2."""
"/tmp/x.tar.gz""gz"
- */ - string getExtension() { this.splitAbsolutePath(result, 3) } - - /** - * Gets the stem of this container, that is, the prefix of its base name up to - * (but not including) the last dot character if there is one, or the entire - * base name if there is not. - * - * Here are some examples of absolute paths and the corresponding stems - * (surrounded with quotes to avoid ambiguity): - * - * - * - * - * - * - * - * - *
Absolute pathStem
"/tmp/tst.txt""tst"
"/tmp/.classpath"""
"/bin/bash""bash"
"/tmp/tst2.""tst2"
"/tmp/x.tar.gz""x.tar"
- */ - string getStem() { this.splitAbsolutePath(result, 2) } - - /** - * Gets the number of lines containing code in this file. This value - * is approximate. - */ - int getNumberOfLinesOfCode() { - result = - count(int line | - exists(Location loc | - not loc instanceof UnknownLocation and loc.getFile() = this and loc.getStartLine() = line - ) - ) - } - - /** - * Gets the relative path of this file from the root folder of the - * analyzed source location. The relative path of the root folder itself - * would be the empty string. - * - * This has no result if the file is outside the source root, that is, - * if the root folder is not a reflexive, transitive parent of this file. - */ - string getRelativePath() { - exists(string absPath, string pref | - absPath = this.getAbsolutePath() and sourceLocationPrefix(pref) - | - absPath = pref and result = "" - or - absPath = pref.regexpReplaceAll("/$", "") + "/" + result and - not result.matches("/%") - ) - } -} diff --git a/rust/ql/lib/codeql/rust/elements/Locatable.qll b/rust/ql/lib/codeql/rust/elements/Locatable.qll index f6df70bb0899..ca209c1ee3e3 100644 --- a/rust/ql/lib/codeql/rust/elements/Locatable.qll +++ b/rust/ql/lib/codeql/rust/elements/Locatable.qll @@ -3,16 +3,23 @@ */ private import codeql.rust.generated.Locatable -private import codeql.rust.elements.File -private import codeql.rust.elements.UnknownLocation +import codeql.Locations +private import codeql.rust.generated.Synth +private import codeql.rust.generated.Raw class Locatable extends Generated::Locatable { + /** Gets the primary location of this element. */ pragma[nomagic] - override Location getLocation() { - result = Generated::Locatable.super.getLocation() - or - not exists(Generated::Locatable.super.getLocation()) and - result instanceof UnknownLocation + final Location getLocation() { + exists(Raw::Locatable raw | + raw = Synth::convertLocatableToRaw(this) and + ( + locatable_locations(raw, result) + or + not exists(Location loc | locatable_locations(raw, loc)) and + result instanceof EmptyLocation + ) + ) } /** diff --git a/rust/ql/lib/codeql/rust/elements/Location.qll b/rust/ql/lib/codeql/rust/elements/Location.qll deleted file mode 100644 index a433422337d2..000000000000 --- a/rust/ql/lib/codeql/rust/elements/Location.qll +++ /dev/null @@ -1,29 +0,0 @@ -/** - * This module provides a hand-modifiable wrapper around the generated class `Location`. - */ - -private import codeql.rust.generated.Location - -class Location extends Generated::Location { - /** - * Holds if this location is described by `path`, `startLine`, `startColumn`, `endLine` and `endColumn`. - */ - predicate hasLocationInfo(string path, int startLine, int startColumn, int endLine, int endColumn) { - path = this.getFile().getFullName() and - startLine = this.getStartLine() and - startColumn = this.getStartColumn() and - endLine = this.getEndLine() and - endColumn = this.getEndColumn() - } - - /** - * Gets a textual representation of this location. - */ - override string toString() { - exists(string filePath, int startLine, int startColumn, int endLine, int endColumn | - this.hasLocationInfo(filePath, startLine, startColumn, endLine, endColumn) - | - toUrl(filePath, startLine, startColumn, endLine, endColumn, result) - ) - } -} diff --git a/rust/ql/lib/codeql/rust/elements/UnknownFile.qll b/rust/ql/lib/codeql/rust/elements/UnknownFile.qll deleted file mode 100644 index 30c2ab8ba1c3..000000000000 --- a/rust/ql/lib/codeql/rust/elements/UnknownFile.qll +++ /dev/null @@ -1,9 +0,0 @@ -/** - * This module provides a hand-modifiable wrapper around the generated class `UnknownFile`. - */ - -private import codeql.rust.generated.UnknownFile - -class UnknownFile extends Generated::UnknownFile { - override string getName() { result = "" } -} diff --git a/rust/ql/lib/codeql/rust/elements/UnknownLocation.qll b/rust/ql/lib/codeql/rust/elements/UnknownLocation.qll deleted file mode 100644 index 0374aa833b86..000000000000 --- a/rust/ql/lib/codeql/rust/elements/UnknownLocation.qll +++ /dev/null @@ -1,21 +0,0 @@ -/** - * This module provides a hand-modifiable wrapper around the generated class `UnknownLocation`. - */ - -private import codeql.rust.generated.UnknownLocation -private import codeql.rust.elements.File -private import codeql.rust.elements.UnknownFile - -class UnknownLocation extends Generated::UnknownLocation { - override File getFile() { result instanceof UnknownFile } - - override int getStartLine() { result = 0 } - - override int getStartColumn() { result = 0 } - - override int getEndLine() { result = 0 } - - override int getEndColumn() { result = 0 } - - override string toString() { result = "UnknownLocation" } -} diff --git a/rust/ql/lib/codeql/rust/generated/DbFile.qll b/rust/ql/lib/codeql/rust/generated/DbFile.qll deleted file mode 100644 index 7128045152c2..000000000000 --- a/rust/ql/lib/codeql/rust/generated/DbFile.qll +++ /dev/null @@ -1,23 +0,0 @@ -// generated by codegen -/** - * This module provides the generated definition of `DbFile`. - * INTERNAL: Do not import directly. - */ - -private import codeql.rust.generated.Synth -private import codeql.rust.generated.Raw -import codeql.rust.elements.File - -/** - * INTERNAL: This module contains the fully generated definition of `DbFile` and should not - * be referenced directly. - */ -module Generated { - /** - * INTERNAL: Do not reference the `Generated::DbFile` class directly. - * Use the subclass `DbFile`, where the following predicates are available. - */ - class DbFile extends Synth::TDbFile, File { - override string getAPrimaryQlClass() { result = "DbFile" } - } -} diff --git a/rust/ql/lib/codeql/rust/generated/DbLocation.qll b/rust/ql/lib/codeql/rust/generated/DbLocation.qll deleted file mode 100644 index a0944fa4b6de..000000000000 --- a/rust/ql/lib/codeql/rust/generated/DbLocation.qll +++ /dev/null @@ -1,23 +0,0 @@ -// generated by codegen -/** - * This module provides the generated definition of `DbLocation`. - * INTERNAL: Do not import directly. - */ - -private import codeql.rust.generated.Synth -private import codeql.rust.generated.Raw -import codeql.rust.elements.Location - -/** - * INTERNAL: This module contains the fully generated definition of `DbLocation` and should not - * be referenced directly. - */ -module Generated { - /** - * INTERNAL: Do not reference the `Generated::DbLocation` class directly. - * Use the subclass `DbLocation`, where the following predicates are available. - */ - class DbLocation extends Synth::TDbLocation, Location { - override string getAPrimaryQlClass() { result = "DbLocation" } - } -} diff --git a/rust/ql/lib/codeql/rust/generated/File.qll b/rust/ql/lib/codeql/rust/generated/File.qll deleted file mode 100644 index c5ebe20092c9..000000000000 --- a/rust/ql/lib/codeql/rust/generated/File.qll +++ /dev/null @@ -1,26 +0,0 @@ -// generated by codegen -/** - * This module provides the generated definition of `File`. - * INTERNAL: Do not import directly. - */ - -private import codeql.rust.generated.Synth -private import codeql.rust.generated.Raw -import codeql.rust.elements.Element - -/** - * INTERNAL: This module contains the fully generated definition of `File` and should not - * be referenced directly. - */ -module Generated { - /** - * INTERNAL: Do not reference the `Generated::File` class directly. - * Use the subclass `File`, where the following predicates are available. - */ - class File extends Synth::TFile, Element { - /** - * Gets the name of this file. - */ - string getName() { result = Synth::convertFileToRaw(this).(Raw::File).getName() } - } -} diff --git a/rust/ql/lib/codeql/rust/generated/Locatable.qll b/rust/ql/lib/codeql/rust/generated/Locatable.qll index a0cdb9a007b5..680246a5feb5 100644 --- a/rust/ql/lib/codeql/rust/generated/Locatable.qll +++ b/rust/ql/lib/codeql/rust/generated/Locatable.qll @@ -7,7 +7,6 @@ private import codeql.rust.generated.Synth private import codeql.rust.generated.Raw import codeql.rust.elements.Element -import codeql.rust.elements.Location /** * INTERNAL: This module contains the fully generated definition of `Locatable` and should not @@ -18,20 +17,5 @@ module Generated { * INTERNAL: Do not reference the `Generated::Locatable` class directly. * Use the subclass `Locatable`, where the following predicates are available. */ - class Locatable extends Synth::TLocatable, Element { - /** - * Gets the location of this locatable, if it exists. - */ - Location getLocation() { - result = - Synth::convertLocationFromRaw(Synth::convertLocatableToRaw(this) - .(Raw::Locatable) - .getLocation()) - } - - /** - * Holds if `getLocation()` exists. - */ - final predicate hasLocation() { exists(this.getLocation()) } - } + class Locatable extends Synth::TLocatable, Element { } } diff --git a/rust/ql/lib/codeql/rust/generated/Location.qll b/rust/ql/lib/codeql/rust/generated/Location.qll deleted file mode 100644 index dac13d4a4188..000000000000 --- a/rust/ql/lib/codeql/rust/generated/Location.qll +++ /dev/null @@ -1,52 +0,0 @@ -// generated by codegen -/** - * This module provides the generated definition of `Location`. - * INTERNAL: Do not import directly. - */ - -private import codeql.rust.generated.Synth -private import codeql.rust.generated.Raw -import codeql.rust.elements.Element -import codeql.rust.elements.File - -/** - * INTERNAL: This module contains the fully generated definition of `Location` and should not - * be referenced directly. - */ -module Generated { - /** - * INTERNAL: Do not reference the `Generated::Location` class directly. - * Use the subclass `Location`, where the following predicates are available. - */ - class Location extends Synth::TLocation, Element { - /** - * Gets the file of this location. - */ - File getFile() { - result = - Synth::convertFileFromRaw(Synth::convertLocationToRaw(this).(Raw::Location).getFile()) - } - - /** - * Gets the start line of this location. - */ - int getStartLine() { result = Synth::convertLocationToRaw(this).(Raw::Location).getStartLine() } - - /** - * Gets the start column of this location. - */ - int getStartColumn() { - result = Synth::convertLocationToRaw(this).(Raw::Location).getStartColumn() - } - - /** - * Gets the end line of this location. - */ - int getEndLine() { result = Synth::convertLocationToRaw(this).(Raw::Location).getEndLine() } - - /** - * Gets the end column of this location. - */ - int getEndColumn() { result = Synth::convertLocationToRaw(this).(Raw::Location).getEndColumn() } - } -} diff --git a/rust/ql/lib/codeql/rust/generated/ParentChild.qll b/rust/ql/lib/codeql/rust/generated/ParentChild.qll index a383e174b769..73b1c76f46fb 100644 --- a/rust/ql/lib/codeql/rust/generated/ParentChild.qll +++ b/rust/ql/lib/codeql/rust/generated/ParentChild.qll @@ -10,19 +10,6 @@ private module Impl { none() } - private Element getImmediateChildOfFile(File e, int index, string partialPredicateCall) { - exists(int b, int bElement, int n | - b = 0 and - bElement = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfElement(e, i, _)) | i) and - n = bElement and - ( - none() - or - result = getImmediateChildOfElement(e, index - b, partialPredicateCall) - ) - ) - } - private Element getImmediateChildOfLocatable(Locatable e, int index, string partialPredicateCall) { exists(int b, int bElement, int n | b = 0 and @@ -36,19 +23,6 @@ private module Impl { ) } - private Element getImmediateChildOfLocation(Location e, int index, string partialPredicateCall) { - exists(int b, int bElement, int n | - b = 0 and - bElement = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfElement(e, i, _)) | i) and - n = bElement and - ( - none() - or - result = getImmediateChildOfElement(e, index - b, partialPredicateCall) - ) - ) - } - private Element getImmediateChildOfAstNode(AstNode e, int index, string partialPredicateCall) { exists(int b, int bLocatable, int n | b = 0 and @@ -62,62 +36,6 @@ private module Impl { ) } - private Element getImmediateChildOfDbFile(DbFile e, int index, string partialPredicateCall) { - exists(int b, int bFile, int n | - b = 0 and - bFile = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfFile(e, i, _)) | i) and - n = bFile and - ( - none() - or - result = getImmediateChildOfFile(e, index - b, partialPredicateCall) - ) - ) - } - - private Element getImmediateChildOfDbLocation(DbLocation e, int index, string partialPredicateCall) { - exists(int b, int bLocation, int n | - b = 0 and - bLocation = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfLocation(e, i, _)) | i) and - n = bLocation and - ( - none() - or - result = getImmediateChildOfLocation(e, index - b, partialPredicateCall) - ) - ) - } - - private Element getImmediateChildOfUnknownFile( - UnknownFile e, int index, string partialPredicateCall - ) { - exists(int b, int bFile, int n | - b = 0 and - bFile = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfFile(e, i, _)) | i) and - n = bFile and - ( - none() - or - result = getImmediateChildOfFile(e, index - b, partialPredicateCall) - ) - ) - } - - private Element getImmediateChildOfUnknownLocation( - UnknownLocation e, int index, string partialPredicateCall - ) { - exists(int b, int bLocation, int n | - b = 0 and - bLocation = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfLocation(e, i, _)) | i) and - n = bLocation and - ( - none() - or - result = getImmediateChildOfLocation(e, index - b, partialPredicateCall) - ) - ) - } - private Element getImmediateChildOfDeclaration( Declaration e, int index, string partialPredicateCall ) { @@ -1301,14 +1219,6 @@ private module Impl { // * none() simplifies generation, as we can append `or ...` without a special case for the first item none() or - result = getImmediateChildOfDbFile(e, index, partialAccessor) - or - result = getImmediateChildOfDbLocation(e, index, partialAccessor) - or - result = getImmediateChildOfUnknownFile(e, index, partialAccessor) - or - result = getImmediateChildOfUnknownLocation(e, index, partialAccessor) - or result = getImmediateChildOfLabel(e, index, partialAccessor) or result = getImmediateChildOfMatchArm(e, index, partialAccessor) diff --git a/rust/ql/lib/codeql/rust/generated/Raw.qll b/rust/ql/lib/codeql/rust/generated/Raw.qll index 28531dfe4a0b..df45ca063aec 100644 --- a/rust/ql/lib/codeql/rust/generated/Raw.qll +++ b/rust/ql/lib/codeql/rust/generated/Raw.qll @@ -13,72 +13,13 @@ module Raw { /** * INTERNAL: Do not use. */ - class File extends @file, Element { - /** - * Gets the name of this file. - */ - string getName() { files(this, result) } - } - - /** - * INTERNAL: Do not use. - */ - class Locatable extends @locatable, Element { - /** - * Gets the location of this locatable, if it exists. - */ - Location getLocation() { locatable_locations(this, result) } - } - - /** - * INTERNAL: Do not use. - */ - class Location extends @location, Element { - /** - * Gets the file of this location. - */ - File getFile() { locations(this, result, _, _, _, _) } - - /** - * Gets the start line of this location. - */ - int getStartLine() { locations(this, _, result, _, _, _) } - - /** - * Gets the start column of this location. - */ - int getStartColumn() { locations(this, _, _, result, _, _) } - - /** - * Gets the end line of this location. - */ - int getEndLine() { locations(this, _, _, _, result, _) } - - /** - * Gets the end column of this location. - */ - int getEndColumn() { locations(this, _, _, _, _, result) } - } + class Locatable extends @locatable, Element { } /** * INTERNAL: Do not use. */ class AstNode extends @ast_node, Locatable { } - /** - * INTERNAL: Do not use. - */ - class DbFile extends @db_file, File { - override string toString() { result = "DbFile" } - } - - /** - * INTERNAL: Do not use. - */ - class DbLocation extends @db_location, Location { - override string toString() { result = "DbLocation" } - } - /** * INTERNAL: Do not use. */ diff --git a/rust/ql/lib/codeql/rust/generated/Synth.qll b/rust/ql/lib/codeql/rust/generated/Synth.qll index 24cd549448af..fa4b015ef44f 100644 --- a/rust/ql/lib/codeql/rust/generated/Synth.qll +++ b/rust/ql/lib/codeql/rust/generated/Synth.qll @@ -75,14 +75,6 @@ module Synth { * INTERNAL: Do not use. */ TContinueExpr(Raw::ContinueExpr id) { constructContinueExpr(id) } or - /** - * INTERNAL: Do not use. - */ - TDbFile(Raw::DbFile id) { constructDbFile(id) } or - /** - * INTERNAL: Do not use. - */ - TDbLocation(Raw::DbLocation id) { constructDbLocation(id) } or /** * INTERNAL: Do not use. */ @@ -251,14 +243,6 @@ module Synth { * INTERNAL: Do not use. */ TUnimplemented(Raw::Unimplemented id) { constructUnimplemented(id) } or - /** - * INTERNAL: Do not use. - */ - TUnknownFile() or - /** - * INTERNAL: Do not use. - */ - TUnknownLocation() or /** * INTERNAL: Do not use. */ @@ -309,21 +293,11 @@ module Synth { TRangeExpr or TRecordLitExpr or TRefExpr or TReturnExpr or TTupleExpr or TUnaryOpExpr or TUnderscoreExpr or TYeetExpr or TYieldExpr; - /** - * INTERNAL: Do not use. - */ - class TFile = TDbFile or TUnknownFile; - /** * INTERNAL: Do not use. */ class TLocatable = TAstNode; - /** - * INTERNAL: Do not use. - */ - class TLocation = TDbLocation or TUnknownLocation; - /** * INTERNAL: Do not use. */ @@ -441,20 +415,6 @@ module Synth { cached TContinueExpr convertContinueExprFromRaw(Raw::Element e) { result = TContinueExpr(e) } - /** - * INTERNAL: Do not use. - * Converts a raw element to a synthesized `TDbFile`, if possible. - */ - cached - TDbFile convertDbFileFromRaw(Raw::Element e) { result = TDbFile(e) } - - /** - * INTERNAL: Do not use. - * Converts a raw element to a synthesized `TDbLocation`, if possible. - */ - cached - TDbLocation convertDbLocationFromRaw(Raw::Element e) { result = TDbLocation(e) } - /** * INTERNAL: Do not use. * Converts a raw element to a synthesized `TElementListExpr`, if possible. @@ -749,20 +709,6 @@ module Synth { cached TUnimplemented convertUnimplementedFromRaw(Raw::Element e) { result = TUnimplemented(e) } - /** - * INTERNAL: Do not use. - * Converts a raw element to a synthesized `TUnknownFile`, if possible. - */ - cached - TUnknownFile convertUnknownFileFromRaw(Raw::Element e) { none() } - - /** - * INTERNAL: Do not use. - * Converts a raw element to a synthesized `TUnknownLocation`, if possible. - */ - cached - TUnknownLocation convertUnknownLocationFromRaw(Raw::Element e) { none() } - /** * INTERNAL: Do not use. * Converts a raw element to a synthesized `TUnsafeBlockExpr`, if possible. @@ -858,13 +804,7 @@ module Synth { * Converts a raw DB element to a synthesized `TElement`, if possible. */ cached - TElement convertElementFromRaw(Raw::Element e) { - result = convertFileFromRaw(e) - or - result = convertLocatableFromRaw(e) - or - result = convertLocationFromRaw(e) - } + TElement convertElementFromRaw(Raw::Element e) { result = convertLocatableFromRaw(e) } /** * INTERNAL: Do not use. @@ -939,17 +879,6 @@ module Synth { result = convertYieldExprFromRaw(e) } - /** - * INTERNAL: Do not use. - * Converts a raw DB element to a synthesized `TFile`, if possible. - */ - cached - TFile convertFileFromRaw(Raw::Element e) { - result = convertDbFileFromRaw(e) - or - result = convertUnknownFileFromRaw(e) - } - /** * INTERNAL: Do not use. * Converts a raw DB element to a synthesized `TLocatable`, if possible. @@ -957,17 +886,6 @@ module Synth { cached TLocatable convertLocatableFromRaw(Raw::Element e) { result = convertAstNodeFromRaw(e) } - /** - * INTERNAL: Do not use. - * Converts a raw DB element to a synthesized `TLocation`, if possible. - */ - cached - TLocation convertLocationFromRaw(Raw::Element e) { - result = convertDbLocationFromRaw(e) - or - result = convertUnknownLocationFromRaw(e) - } - /** * INTERNAL: Do not use. * Converts a raw DB element to a synthesized `TPat`, if possible. @@ -1121,20 +1039,6 @@ module Synth { cached Raw::Element convertContinueExprToRaw(TContinueExpr e) { e = TContinueExpr(result) } - /** - * INTERNAL: Do not use. - * Converts a synthesized `TDbFile` to a raw DB element, if possible. - */ - cached - Raw::Element convertDbFileToRaw(TDbFile e) { e = TDbFile(result) } - - /** - * INTERNAL: Do not use. - * Converts a synthesized `TDbLocation` to a raw DB element, if possible. - */ - cached - Raw::Element convertDbLocationToRaw(TDbLocation e) { e = TDbLocation(result) } - /** * INTERNAL: Do not use. * Converts a synthesized `TElementListExpr` to a raw DB element, if possible. @@ -1429,20 +1333,6 @@ module Synth { cached Raw::Element convertUnimplementedToRaw(TUnimplemented e) { e = TUnimplemented(result) } - /** - * INTERNAL: Do not use. - * Converts a synthesized `TUnknownFile` to a raw DB element, if possible. - */ - cached - Raw::Element convertUnknownFileToRaw(TUnknownFile e) { none() } - - /** - * INTERNAL: Do not use. - * Converts a synthesized `TUnknownLocation` to a raw DB element, if possible. - */ - cached - Raw::Element convertUnknownLocationToRaw(TUnknownLocation e) { none() } - /** * INTERNAL: Do not use. * Converts a synthesized `TUnsafeBlockExpr` to a raw DB element, if possible. @@ -1538,13 +1428,7 @@ module Synth { * Converts a synthesized `TElement` to a raw DB element, if possible. */ cached - Raw::Element convertElementToRaw(TElement e) { - result = convertFileToRaw(e) - or - result = convertLocatableToRaw(e) - or - result = convertLocationToRaw(e) - } + Raw::Element convertElementToRaw(TElement e) { result = convertLocatableToRaw(e) } /** * INTERNAL: Do not use. @@ -1619,17 +1503,6 @@ module Synth { result = convertYieldExprToRaw(e) } - /** - * INTERNAL: Do not use. - * Converts a synthesized `TFile` to a raw DB element, if possible. - */ - cached - Raw::Element convertFileToRaw(TFile e) { - result = convertDbFileToRaw(e) - or - result = convertUnknownFileToRaw(e) - } - /** * INTERNAL: Do not use. * Converts a synthesized `TLocatable` to a raw DB element, if possible. @@ -1637,17 +1510,6 @@ module Synth { cached Raw::Element convertLocatableToRaw(TLocatable e) { result = convertAstNodeToRaw(e) } - /** - * INTERNAL: Do not use. - * Converts a synthesized `TLocation` to a raw DB element, if possible. - */ - cached - Raw::Element convertLocationToRaw(TLocation e) { - result = convertDbLocationToRaw(e) - or - result = convertUnknownLocationToRaw(e) - } - /** * INTERNAL: Do not use. * Converts a synthesized `TPat` to a raw DB element, if possible. diff --git a/rust/ql/lib/codeql/rust/generated/SynthConstructors.qll b/rust/ql/lib/codeql/rust/generated/SynthConstructors.qll index c4281ed696ee..e5a7b47c5765 100644 --- a/rust/ql/lib/codeql/rust/generated/SynthConstructors.qll +++ b/rust/ql/lib/codeql/rust/generated/SynthConstructors.qll @@ -18,8 +18,6 @@ import codeql.rust.elements.ClosureExprConstructor import codeql.rust.elements.ConstBlockPatConstructor import codeql.rust.elements.ConstExprConstructor import codeql.rust.elements.ContinueExprConstructor -import codeql.rust.elements.DbFileConstructor -import codeql.rust.elements.DbLocationConstructor import codeql.rust.elements.ElementListExprConstructor import codeql.rust.elements.ExprStmtConstructor import codeql.rust.elements.FieldExprConstructor diff --git a/rust/ql/lib/codeql/rust/generated/UnknownFile.qll b/rust/ql/lib/codeql/rust/generated/UnknownFile.qll deleted file mode 100644 index 4b5ce7cc81e4..000000000000 --- a/rust/ql/lib/codeql/rust/generated/UnknownFile.qll +++ /dev/null @@ -1,23 +0,0 @@ -// generated by codegen -/** - * This module provides the generated definition of `UnknownFile`. - * INTERNAL: Do not import directly. - */ - -private import codeql.rust.generated.Synth -private import codeql.rust.generated.Raw -import codeql.rust.elements.File - -/** - * INTERNAL: This module contains the fully generated definition of `UnknownFile` and should not - * be referenced directly. - */ -module Generated { - /** - * INTERNAL: Do not reference the `Generated::UnknownFile` class directly. - * Use the subclass `UnknownFile`, where the following predicates are available. - */ - class UnknownFile extends Synth::TUnknownFile, File { - override string getAPrimaryQlClass() { result = "UnknownFile" } - } -} diff --git a/rust/ql/lib/codeql/rust/generated/UnknownLocation.qll b/rust/ql/lib/codeql/rust/generated/UnknownLocation.qll deleted file mode 100644 index 5adaae87e7fc..000000000000 --- a/rust/ql/lib/codeql/rust/generated/UnknownLocation.qll +++ /dev/null @@ -1,23 +0,0 @@ -// generated by codegen -/** - * This module provides the generated definition of `UnknownLocation`. - * INTERNAL: Do not import directly. - */ - -private import codeql.rust.generated.Synth -private import codeql.rust.generated.Raw -import codeql.rust.elements.Location - -/** - * INTERNAL: This module contains the fully generated definition of `UnknownLocation` and should not - * be referenced directly. - */ -module Generated { - /** - * INTERNAL: Do not reference the `Generated::UnknownLocation` class directly. - * Use the subclass `UnknownLocation`, where the following predicates are available. - */ - class UnknownLocation extends Synth::TUnknownLocation, Location { - override string getAPrimaryQlClass() { result = "UnknownLocation" } - } -} diff --git a/rust/ql/lib/codeql/rust/printast/PrintAst.qll b/rust/ql/lib/codeql/rust/printast/PrintAst.qll index af16368f3c0c..b485efe8022b 100644 --- a/rust/ql/lib/codeql/rust/printast/PrintAst.qll +++ b/rust/ql/lib/codeql/rust/printast/PrintAst.qll @@ -12,7 +12,7 @@ private int getOrder(PrintAstNode node) { | n order by - loc.getFile().getName(), loc.getStartLine(), loc.getStartColumn(), loc.getEndLine(), + loc.getFile().getAbsolutePath(), loc.getStartLine(), loc.getStartColumn(), loc.getEndLine(), loc.getEndColumn() ) } diff --git a/rust/ql/lib/rust.dbscheme b/rust/ql/lib/rust.dbscheme index c38f0f61ea0f..2aaadbba9909 100644 --- a/rust/ql/lib/rust.dbscheme +++ b/rust/ql/lib/rust.dbscheme @@ -1,55 +1,131 @@ // generated by codegen -// from prefix.dbscheme +// from ../shared/tree-sitter-extractor/src/generator/prefix.dbscheme +/*- Files and folders -*/ + /** - * The source location of the snapshot. + * The location of an element. + * The location spans column `startcolumn` of line `startline` to + * column `endcolumn` of line `endline` in file `file`. + * For more information, see + * [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). */ -sourceLocationPrefix( - string prefix: string ref +locations_default( + unique int id: @location_default, + int file: @file ref, + int beginLine: int ref, + int beginColumn: int ref, + int endLine: int ref, + int endColumn: int ref ); +files( + unique int id: @file, + string name: string ref +); -// from schema.py +folders( + unique int id: @folder, + string name: string ref +); -@element = - @file -| @locatable -| @location -; +@container = @file | @folder -@file = - @db_file -; +containerparent( + int parent: @container ref, + unique int child: @container ref +); -#keyset[id] -files( - int id: @file ref, - string name: string ref +/*- Empty location -*/ + +empty_location( + int location: @location_default ref ); -@locatable = - @ast_node +/*- Source location prefix -*/ + +/** + * The source location of the snapshot. + */ +sourceLocationPrefix(string prefix : string ref); + +/*- Diagnostic messages -*/ + +diagnostics( + unique int id: @diagnostic, + int severity: int ref, + string error_tag: string ref, + string error_message: string ref, + string full_error_message: string ref, + int location: @location_default ref +); + +/*- Diagnostic messages: severity -*/ + +case @diagnostic.severity of + 10 = @diagnostic_debug +| 20 = @diagnostic_info +| 30 = @diagnostic_warning +| 40 = @diagnostic_error +; + +/*- YAML -*/ + +#keyset[parent, idx] +yaml (unique int id: @yaml_node, + int kind: int ref, + int parent: @yaml_node_parent ref, + int idx: int ref, + string tag: string ref, + string tostring: string ref); + +case @yaml_node.kind of + 0 = @yaml_scalar_node +| 1 = @yaml_mapping_node +| 2 = @yaml_sequence_node +| 3 = @yaml_alias_node ; +@yaml_collection_node = @yaml_mapping_node | @yaml_sequence_node; + +@yaml_node_parent = @yaml_collection_node | @file; + +yaml_anchors (unique int node: @yaml_node ref, + string anchor: string ref); + +yaml_aliases (unique int alias: @yaml_alias_node ref, + string target: string ref); + +yaml_scalars (unique int scalar: @yaml_scalar_node ref, + int style: int ref, + string value: string ref); + +yaml_errors (unique int id: @yaml_error, + string message: string ref); + +yaml_locations(unique int locatable: @yaml_locatable ref, + int location: @location_default ref); + +@yaml_locatable = @yaml_node | @yaml_error; + + +// from prefix.dbscheme #keyset[id] locatable_locations( int id: @locatable ref, - int location: @location ref + int location: @location_default ref ); -@location = - @db_location + +// from schema.py + +@element = + @locatable ; -#keyset[id] -locations( - int id: @location ref, - int file: @file ref, - int start_line: int ref, - int start_column: int ref, - int end_line: int ref, - int end_column: int ref -); +@locatable = + @ast_node +; @ast_node = @declaration @@ -64,14 +140,6 @@ locations( | @unimplemented ; -db_files( - unique int id: @db_file -); - -db_locations( - unique int id: @db_location -); - @declaration = @function | @module diff --git a/rust/ql/lib/rust.qll b/rust/ql/lib/rust.qll index cedced159c33..bdb9f3df374c 100644 --- a/rust/ql/lib/rust.qll +++ b/rust/ql/lib/rust.qll @@ -1,3 +1,5 @@ /** Top-level import for the Rust language pack */ import codeql.rust.elements +import codeql.Locations +import codeql.files.FileSystem diff --git a/rust/ql/test/TestUtils.qll b/rust/ql/test/TestUtils.qll index 317b71f2bdfc..8cee9fa6a53e 100644 --- a/rust/ql/test/TestUtils.qll +++ b/rust/ql/test/TestUtils.qll @@ -1,13 +1,10 @@ -private import codeql.rust.elements +private import rust cached predicate toBeTested(Element e) { - exists(File f | - f.getName().matches("%rust/ql/test%") and - ( - e = f - or - e.(Locatable).getLocation().getFile() = f - ) - ) + not e instanceof Locatable or + fileIsInTest(e.(Locatable).getFile()) } + +cached +predicate fileIsInTest(File f) { f.getAbsolutePath().matches("%rust/ql/test%") } diff --git a/rust/ql/test/extractor-tests/File/EmptyLocation.expected b/rust/ql/test/extractor-tests/File/EmptyLocation.expected new file mode 100644 index 000000000000..e5f9d898f89d --- /dev/null +++ b/rust/ql/test/extractor-tests/File/EmptyLocation.expected @@ -0,0 +1 @@ +| file://:0:0:0:0 | @0:0:0:0 | file://:0:0:0:0 | | diff --git a/rust/ql/test/extractor-tests/File/EmptyLocation.ql b/rust/ql/test/extractor-tests/File/EmptyLocation.ql new file mode 100644 index 000000000000..76ce9ba523dd --- /dev/null +++ b/rust/ql/test/extractor-tests/File/EmptyLocation.ql @@ -0,0 +1,4 @@ +import rust + +from EmptyLocation loc +select loc, loc.getFile() diff --git a/rust/ql/test/extractor-tests/File/File.expected b/rust/ql/test/extractor-tests/File/File.expected new file mode 100644 index 000000000000..924ed370b35b --- /dev/null +++ b/rust/ql/test/extractor-tests/File/File.expected @@ -0,0 +1,3 @@ +| a_file.rs:0:0:0:0 | a_file.rs | +| another_file.rs:0:0:0:0 | another_file.rs | +| lib.rs:0:0:0:0 | lib.rs | diff --git a/rust/ql/test/extractor-tests/File/File.ql b/rust/ql/test/extractor-tests/File/File.ql new file mode 100644 index 000000000000..c562e2587fe6 --- /dev/null +++ b/rust/ql/test/extractor-tests/File/File.ql @@ -0,0 +1,6 @@ +import rust +import TestUtils + +from File f +where fileIsInTest(f) +select f diff --git a/rust/ql/test/extractor-tests/File/a_file.rs b/rust/ql/test/extractor-tests/File/a_file.rs new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/rust/ql/test/extractor-tests/File/another_file.rs b/rust/ql/test/extractor-tests/File/another_file.rs new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/rust/ql/test/extractor-tests/generated/File/File.ql b/rust/ql/test/extractor-tests/generated/File/File.ql deleted file mode 100644 index 20550a0d1b51..000000000000 --- a/rust/ql/test/extractor-tests/generated/File/File.ql +++ /dev/null @@ -1,10 +0,0 @@ -// generated by codegen -import codeql.rust.elements -import TestUtils - -from File x, string getName -where - toBeTested(x) and - not x.isUnknown() and - getName = x.getName() -select x, x.getPrimaryQlClasses(), "getName:", getName diff --git a/rust/ql/test/extractor-tests/generated/File/test.rs b/rust/ql/test/extractor-tests/generated/File/test.rs deleted file mode 100644 index f328e4d9d04c..000000000000 --- a/rust/ql/test/extractor-tests/generated/File/test.rs +++ /dev/null @@ -1 +0,0 @@ -fn main() {} diff --git a/rust/schema.py b/rust/schema.py index 1a5a19b2e0b8..d9ea1ca19cb3 100644 --- a/rust/schema.py +++ b/rust/schema.py @@ -11,6 +11,7 @@ from misc.codegen.lib.schemadefs import * +include("../shared/tree-sitter-extractor/src/generator/prefix.dbscheme") include("prefix.dbscheme") @@ -19,43 +20,9 @@ class Element: pass -@qltest.collapse_hierarchy -class File(Element): - name: string - - -@qltest.skip -@qltest.collapse_hierarchy -class Location(Element): - file: File - start_line: int - start_column: int - end_line: int - end_column: int - - -class DbFile(File): - pass - - -class DbLocation(Location): - pass - - -@synth.on_arguments() -class UnknownFile(File): - pass - - -@synth.on_arguments() -class UnknownLocation(Location): - pass - - @qltest.skip class Locatable(Element): - location: optional[Location] - + pass @qltest.skip class AstNode(Locatable): diff --git a/shared/tree-sitter-extractor/BUILD.bazel b/shared/tree-sitter-extractor/BUILD.bazel index dc9001a32d25..a107a8a7a061 100644 --- a/shared/tree-sitter-extractor/BUILD.bazel +++ b/shared/tree-sitter-extractor/BUILD.bazel @@ -14,3 +14,8 @@ rust_library( ], deps = all_crate_deps(), ) + +filegroup( + name = "dbscheme-prefix", + srcs = ["src/generator/prefix.dbscheme"], +) diff --git a/shared/tree-sitter-extractor/src/extractor/mod.rs b/shared/tree-sitter-extractor/src/extractor/mod.rs index 4b0d0643d9da..00f940ff89cf 100644 --- a/shared/tree-sitter-extractor/src/extractor/mod.rs +++ b/shared/tree-sitter-extractor/src/extractor/mod.rs @@ -154,7 +154,7 @@ fn global_location(writer: &mut trap::Writer, location: trap::Location) -> trap: /** Get the label for the given location, creating it as a fresh ID if we haven't seen the location * yet for this file. */ -fn location_label(writer: &mut trap::Writer, location: trap::Location) -> trap::Label { +pub fn location_label(writer: &mut trap::Writer, location: trap::Location) -> trap::Label { let (loc_label, fresh) = writer.location_label(location); if fresh { writer.add_tuple(