Skip to content

Commit 5852661

Browse files
authored
Merge pull request #276 from jcreedcmu/jcreed/prep-jump-to-def
Add bqrs types and ability to run template queries.
2 parents a86adbd + 55d3db0 commit 5852661

File tree

4 files changed

+127
-9
lines changed

4 files changed

+127
-9
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
export const PAGE_SIZE = 1000;
3+
4+
export type ColumnKind = "f" | "i" | "s" | "b" | "d" | "e";
5+
6+
export interface Column {
7+
name?: string,
8+
kind: ColumnKind,
9+
}
10+
11+
12+
export interface ResultSetSchema {
13+
name: string,
14+
rows: number,
15+
columns: Column[],
16+
pagination?: PaginationInfo,
17+
}
18+
19+
export function getResultSetSchema(resultSetName: string, resultSets: BQRSInfo): ResultSetSchema | undefined {
20+
for (const schema of resultSets["result-sets"]) {
21+
if (schema.name === resultSetName) {
22+
return schema;
23+
}
24+
}
25+
return undefined;
26+
}
27+
export interface PaginationInfo {
28+
"step-size": number,
29+
offsets: number[],
30+
}
31+
32+
export interface BQRSInfo {
33+
"result-sets": ResultSetSchema[]
34+
}
35+
36+
export interface EntityValue {
37+
url?: UrlValue,
38+
label?: string
39+
}
40+
41+
export interface LineColumnLocation {
42+
uri: string
43+
startLine: number,
44+
startColumn: number,
45+
endLine: number,
46+
endColumn: number,
47+
charOffset: never
48+
charLength: never
49+
}
50+
51+
export interface OffsetLengthLocation {
52+
uri: string,
53+
startLine: never,
54+
startColumn: never,
55+
endLine: never,
56+
endColumn: never,
57+
charOffset: number,
58+
charLength: number,
59+
}
60+
61+
export interface WholeFileLocation {
62+
uri: string,
63+
startLine: never,
64+
startColumn: never,
65+
endLine: never,
66+
endColumn: never,
67+
charOffset: never,
68+
charLength: never,
69+
}
70+
71+
export type UrlValue = LineColumnLocation | OffsetLengthLocation | WholeFileLocation | string;
72+
73+
74+
export type ColumnValue = EntityValue | number | string | boolean;
75+
76+
export interface DecodedBqrsChunk {
77+
tuples: ColumnValue[][],
78+
next?: number
79+
}

extensions/ql-vscode/src/cli.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import * as child_process from 'child_process';
21
import * as cpp from 'child-process-promise';
2+
import * as child_process from 'child_process';
33
import * as fs from 'fs-extra';
44
import * as path from 'path';
55
import * as sarif from 'sarif';
6+
import { Readable } from 'stream';
7+
import { StringDecoder } from 'string_decoder';
68
import * as tk from 'tree-kill';
79
import * as util from 'util';
8-
import { SortDirection, QueryMetadata } from './interface-types';
9-
import { Logger, ProgressReporter } from './logging';
10-
import { Disposable, CancellationToken } from 'vscode';
10+
import { CancellationToken, Disposable } from 'vscode';
11+
import { BQRSInfo, DecodedBqrsChunk } from "./bqrs-cli-types";
1112
import { DistributionProvider } from './distribution';
1213
import { assertNever } from './helpers-pure';
13-
import { Readable } from 'stream';
14-
import { StringDecoder } from 'string_decoder';
14+
import { QueryMetadata, SortDirection } from './interface-types';
15+
import { Logger, ProgressReporter } from './logging';
1516

1617
/**
1718
* The version of the SARIF format that we are using.
@@ -461,6 +462,7 @@ export class CodeQLCliServer implements Disposable {
461462
* Gets the RAM setting for the query server.
462463
* @param queryMemoryMb The maximum amount of RAM to use, in MB.
463464
* Leave `undefined` for CodeQL to choose a limit based on the available system memory.
465+
* @param progressReporter The progress reporter to send progress information to.
464466
* @returns String arguments that can be passed to the CodeQL query server,
465467
* indicating how to split the given RAM limit between heap and off-heap memory.
466468
*/
@@ -471,6 +473,38 @@ export class CodeQLCliServer implements Disposable {
471473
}
472474
return await this.runJsonCodeQlCliCommand<string[]>(['resolve', 'ram'], args, "Resolving RAM settings", progressReporter);
473475
}
476+
/**
477+
* Gets the headers (and optionally pagination info) of a bqrs.
478+
* @param bqrsPath The path to the bqrs.
479+
* @param pageSize The page size to precompute offsets into the binary file for.
480+
*/
481+
async bqrsInfo(bqrsPath: string, pageSize?: number): Promise<BQRSInfo> {
482+
const subcommandArgs = (
483+
pageSize ? ["--paginate-rows", pageSize.toString()] : []
484+
).concat(
485+
bqrsPath
486+
);
487+
return await this.runJsonCodeQlCliCommand<BQRSInfo>(['bqrs', 'info'], subcommandArgs, "Reading bqrs header");
488+
}
489+
490+
/**
491+
* Gets the results from a bqrs.
492+
* @param bqrsPath The path to the bqrs.
493+
* @param resultSet The result set to get.
494+
* @param pageSize How many results to get.
495+
* @param offset The 0-based index of the first result to get.
496+
*/
497+
async bqrsDecode(bqrsPath: string, resultSet: string, pageSize?: number, offset?: number): Promise<DecodedBqrsChunk> {
498+
const subcommandArgs = [
499+
"--entities=url,string",
500+
"--result-set", resultSet,
501+
].concat(
502+
pageSize ? ["--rows", pageSize.toString()] : []
503+
).concat(
504+
offset ? ["--start-at", offset.toString()] : []
505+
).concat([bqrsPath]);
506+
return await this.runJsonCodeQlCliCommand<DecodedBqrsChunk>(['bqrs', 'decode'], subcommandArgs, "Reading bqrs data");
507+
}
474508

475509

476510
async interpretBqrs(metadata: { kind: string, id: string }, resultsPath: string, interpretedResultsPath: string, sourceInfo?: SourceInfo): Promise<sarif.Log> {

extensions/ql-vscode/src/messages.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ export interface EvaluateQueriesParams {
635635
useSequenceHint: boolean;
636636
}
637637

638+
export type TemplateDefinitions = { [key: string]: TemplateSource; }
639+
638640
/**
639641
* A single query that should be run
640642
*/
@@ -658,7 +660,7 @@ export interface QueryToRun {
658660
/**
659661
* Values to set for each template
660662
*/
661-
templateValues?: { [key: string]: TemplateSource; };
663+
templateValues?: TemplateDefinitions;
662664
/**
663665
* Whether templates without values in the templateValues
664666
* map should be set to the empty set or give an error.

extensions/ql-vscode/src/run-queries.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class QueryInfo {
5656
public readonly queryDbscheme: string, // the dbscheme file the query expects, based on library path resolution
5757
public readonly quickEvalPosition?: messages.Position,
5858
public readonly metadata?: QueryMetadata,
59+
public readonly templates?: messages.TemplateDefinitions,
5960
) {
6061
this.queryID = QueryInfo.nextQueryId++;
6162
this.compiledQueryPath = path.join(tmpDir.name, `compiledQuery${this.queryID}.qlo`);
@@ -80,6 +81,7 @@ export class QueryInfo {
8081
resultsPath: this.resultsPaths.resultsPath,
8182
qlo: vscode.Uri.file(this.compiledQueryPath).toString(),
8283
allowUnknownTemplates: true,
84+
templateValues: this.templates,
8385
id: callbackId,
8486
timeoutSecs: qs.config.timeoutSecs,
8587
}
@@ -362,7 +364,8 @@ export async function compileAndRunQueryAgainstDatabase(
362364
qs: qsClient.QueryServerClient,
363365
db: DatabaseItem,
364366
quickEval: boolean,
365-
selectedQueryUri: vscode.Uri | undefined
367+
selectedQueryUri: vscode.Uri | undefined,
368+
templates?: messages.TemplateDefinitions,
366369
): Promise<QueryWithResults> {
367370

368371
if (!db.contents || !db.contents.dbSchemeUri) {
@@ -415,7 +418,7 @@ export async function compileAndRunQueryAgainstDatabase(
415418
logger.log(`Couldn't resolve metadata for ${qlProgram.queryPath}: ${e}`);
416419
}
417420

418-
const query = new QueryInfo(qlProgram, db, packConfig.dbscheme, quickEvalPosition, metadata);
421+
const query = new QueryInfo(qlProgram, db, packConfig.dbscheme, quickEvalPosition, metadata, templates);
419422
await checkDbschemeCompatibility(cliServer, qs, query);
420423

421424
let errors;

0 commit comments

Comments
 (0)