Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions extensions/ql-vscode/src/ast-cfg-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,18 @@ import {
TemplatePrintAstProvider,
TemplatePrintCfgProvider,
} from "./contextual/templateProvider";
import { compileAndRunQuery } from "./local-queries";
import { QueryRunner } from "./queryRunner";
import { QueryHistoryManager } from "./query-history/query-history-manager";
import { DatabaseUI } from "./local-databases-ui";
import { ResultsView } from "./interface";
import { AstCfgCommands } from "./common/commands";
import { LocalQueries } from "./local-queries";

type AstCfgOptions = {
queryRunner: QueryRunner;
queryHistoryManager: QueryHistoryManager;
databaseUI: DatabaseUI;
localQueryResultsView: ResultsView;
queryStorageDir: string;

localQueries: LocalQueries;
astViewer: AstViewer;
astTemplateProvider: TemplatePrintAstProvider;
cfgTemplateProvider: TemplatePrintCfgProvider;
};

export function getAstCfgCommands({
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
localQueries,
astViewer,
astTemplateProvider,
cfgTemplateProvider,
Expand Down Expand Up @@ -59,12 +46,7 @@ export function getAstCfgCommands({
window.activeTextEditor?.document,
);
if (res) {
await compileAndRunQuery(
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
await localQueries.compileAndRunQuery(
false,
res[0],
progress,
Expand Down
6 changes: 5 additions & 1 deletion extensions/ql-vscode/src/common/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export interface LogOptions {
trailingNewline?: boolean;
}

export interface Logger {
/** Minimal logger interface. */
export interface BaseLogger {
/**
* Writes the given log message, optionally followed by a newline.
* This function is asynchronous and will only resolve once the message is written
Expand All @@ -15,7 +16,10 @@ export interface Logger {
* @param options Optional settings.
*/
log(message: string, options?: LogOptions): Promise<void>;
}

/** Full logger interface, including a function to show the log in the UI. */
export interface Logger extends BaseLogger {
/**
* Reveal the logger channel in the UI.
*
Expand Down
6 changes: 3 additions & 3 deletions extensions/ql-vscode/src/contextual/astBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DatabaseItem } from "../local-databases";
import { ChildAstItem, AstItem } from "../astViewer";
import fileRangeFromURI from "./fileRangeFromURI";
import { Uri } from "vscode";
import { QueryWithResults } from "../run-queries-shared";
import { QueryOutputDir } from "../run-queries-shared";

/**
* A class that wraps a tree of QL results from a query that
Expand All @@ -14,12 +14,12 @@ export default class AstBuilder {
private roots: AstItem[] | undefined;
private bqrsPath: string;
constructor(
queryResults: QueryWithResults,
outputDir: QueryOutputDir,
private cli: CodeQLCliServer,
public db: DatabaseItem,
public fileName: Uri,
) {
this.bqrsPath = queryResults.query.resultsPaths.resultsPath;
this.bqrsPath = outputDir.bqrsPath;
}

async getRoots(): Promise<AstItem[]> {
Expand Down
13 changes: 8 additions & 5 deletions extensions/ql-vscode/src/contextual/locationFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {
runContextualQuery,
} from "./queryResolver";
import { CancellationToken, LocationLink, Uri } from "vscode";
import { QueryWithResults } from "../run-queries-shared";
import { QueryOutputDir } from "../run-queries-shared";
import { QueryRunner } from "../queryRunner";
import { QueryResultType } from "../pure/new-messages";

export const SELECT_QUERY_NAME = "#select";
export const TEMPLATE_NAME = "selectedSourceFile";
Expand Down Expand Up @@ -78,21 +79,23 @@ export async function getLocationsForUriString(
token,
templates,
);
if (results.successful) {
links.push(...(await getLinksFromResults(results, cli, db, filter)));
if (results.resultType === QueryResultType.SUCCESS) {
links.push(
...(await getLinksFromResults(results.outputDir, cli, db, filter)),
);
}
}
return links;
}

async function getLinksFromResults(
results: QueryWithResults,
outputDir: QueryOutputDir,
cli: CodeQLCliServer,
db: DatabaseItem,
filter: (srcFile: string, destFile: string) => boolean,
): Promise<FullLocationLink[]> {
const localLinks: FullLocationLink[] = [];
const bqrsPath = results.query.resultsPaths.resultsPath;
const bqrsPath = outputDir.bqrsPath;
const info = await cli.bqrsInfo(bqrsPath);
const selectInfo = getResultSetSchema(SELECT_QUERY_NAME, info);
if (isValidSelect(selectInfo)) {
Expand Down
33 changes: 15 additions & 18 deletions extensions/ql-vscode/src/contextual/queryResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import {
import { KeyType, kindOfKeyType, nameOfKeyType, tagOfKeyType } from "./keyType";
import { CodeQLCliServer } from "../cli";
import { DatabaseItem } from "../local-databases";
import { extLogger } from "../common";
import { createInitialQueryInfo } from "../run-queries-shared";
import { CancellationToken, Uri } from "vscode";
import { extLogger, TeeLogger } from "../common";
import { CancellationToken } from "vscode";
import { ProgressCallback } from "../progress";
import { QueryRunner } from "../queryRunner";
import { CoreCompletedQuery, QueryRunner } from "../queryRunner";
import { redactableError } from "../pure/errors";
import { QLPACK_FILENAMES } from "../pure/ql";

Expand Down Expand Up @@ -169,32 +168,30 @@ export async function runContextualQuery(
progress: ProgressCallback,
token: CancellationToken,
templates: Record<string, string>,
) {
): Promise<CoreCompletedQuery> {
const { packPath, createdTempLockFile } = await resolveContextualQuery(
cli,
query,
);
const initialInfo = await createInitialQueryInfo(
Uri.file(query),
{
name: db.name,
databaseUri: db.databaseUri.toString(),
},
const queryRun = qs.createQueryRun(
db.databaseUri.fsPath,
{ queryPath: query, quickEvalPosition: undefined },
false,
getOnDiskWorkspaceFolders(),
queryStorageDir,
undefined,
templates,
);
void extLogger.log(
`Running contextual query ${query}; results will be stored in ${queryStorageDir}`,
`Running contextual query ${query}; results will be stored in ${queryRun.outputDir.querySaveDir}`,
);
const queryResult = await qs.compileAndRunQueryAgainstDatabase(
db,
initialInfo,
queryStorageDir,
const results = await queryRun.evaluate(
progress,
token,
templates,
new TeeLogger(qs.logger, queryRun.outputDir.logPath),
);
if (createdTempLockFile) {
await removeTemporaryLockFile(packPath);
}
return queryResult;
return results;
}
29 changes: 11 additions & 18 deletions extensions/ql-vscode/src/contextual/templateProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import {
runContextualQuery,
} from "./queryResolver";
import { isCanary, NO_CACHE_AST_VIEWER } from "../config";
import { QueryWithResults } from "../run-queries-shared";
import { QueryRunner } from "../queryRunner";
import { CoreCompletedQuery, QueryRunner } from "../queryRunner";

/**
* Runs templated CodeQL queries to find definitions in
Expand Down Expand Up @@ -155,25 +154,22 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
}
}

type QueryWithDb = {
query: QueryWithResults;
dbUri: Uri;
};

/**
* Run templated CodeQL queries to produce AST information for
* source-language files.
*/
export class TemplatePrintAstProvider {
private cache: CachedOperation<QueryWithDb>;
private cache: CachedOperation<CoreCompletedQuery>;

constructor(
private cli: CodeQLCliServer,
private qs: QueryRunner,
private dbm: DatabaseManager,
private queryStorageDir: string,
) {
this.cache = new CachedOperation<QueryWithDb>(this.getAst.bind(this));
this.cache = new CachedOperation<CoreCompletedQuery>(
this.getAst.bind(this),
);
}

async provideAst(
Expand All @@ -186,14 +182,14 @@ export class TemplatePrintAstProvider {
"Cannot view the AST. Please select a valid source file inside a CodeQL database.",
);
}
const { query, dbUri } = this.shouldCache()
const completedQuery = this.shouldCache()
? await this.cache.get(fileUri.toString(), progress, token)
: await this.getAst(fileUri.toString(), progress, token);

return new AstBuilder(
query,
completedQuery.outputDir,
this.cli,
this.dbm.findDatabaseItem(dbUri)!,
this.dbm.findDatabaseItem(Uri.file(completedQuery.dbPath))!,
fileUri,
);
}
Expand All @@ -206,7 +202,7 @@ export class TemplatePrintAstProvider {
uriString: string,
progress: ProgressCallback,
token: CancellationToken,
): Promise<QueryWithDb> {
): Promise<CoreCompletedQuery> {
const uri = Uri.parse(uriString, true);
if (uri.scheme !== zipArchiveScheme) {
throw new Error(
Expand Down Expand Up @@ -242,7 +238,7 @@ export class TemplatePrintAstProvider {
[TEMPLATE_NAME]: zippedArchive.pathWithinSourceArchive,
};

const queryResult = await runContextualQuery(
const results = await runContextualQuery(
query,
db,
this.queryStorageDir,
Expand All @@ -252,10 +248,7 @@ export class TemplatePrintAstProvider {
token,
templates,
);
return {
query: queryResult,
dbUri: db.databaseUri,
};
return results;
}
}

Expand Down
51 changes: 23 additions & 28 deletions extensions/ql-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ import {
QueryServerCommands,
TestUICommands,
} from "./common/commands";
import {
getLocalQueryCommands,
showResultsForCompletedQuery,
} from "./local-queries";
import { LocalQueries } from "./local-queries";
import { getAstCfgCommands } from "./ast-cfg-commands";
import { getQueryEditorCommands } from "./query-editor";
import { App } from "./common/app";
Expand Down Expand Up @@ -260,6 +257,7 @@ export interface CodeQLExtensionInterface {
readonly distributionManager: DistributionManager;
readonly databaseManager: DatabaseManager;
readonly databaseUI: DatabaseUI;
readonly localQueries: LocalQueries;
readonly variantAnalysisManager: VariantAnalysisManager;
readonly dispose: () => void;
}
Expand Down Expand Up @@ -716,12 +714,6 @@ async function activateWithInstalledDistribution(
void extLogger.log("Initializing query history manager.");
const queryHistoryConfigurationListener = new QueryHistoryConfigListener();
ctx.subscriptions.push(queryHistoryConfigurationListener);
const showResults = async (item: CompletedLocalQueryInfo) =>
showResultsForCompletedQuery(
localQueryResultsView,
item,
WebviewReveal.Forced,
);
const queryStorageDir = join(ctx.globalStorageUri.fsPath, "queries");
await ensureDir(queryStorageDir);

Expand Down Expand Up @@ -795,8 +787,10 @@ async function activateWithInstalledDistribution(
ctx,
queryHistoryConfigurationListener,
labelProvider,
async (from: CompletedLocalQueryInfo, to: CompletedLocalQueryInfo) =>
showResultsForComparison(compareView, from, to),
async (
from: CompletedLocalQueryInfo,
to: CompletedLocalQueryInfo,
): Promise<void> => showResultsForComparison(compareView, from, to),
);

ctx.subscriptions.push(qhm);
Expand All @@ -817,7 +811,8 @@ async function activateWithInstalledDistribution(
cliServer,
queryServerLogger,
labelProvider,
showResults,
async (item: CompletedLocalQueryInfo) =>
localQueries.showResultsForCompletedQuery(item, WebviewReveal.Forced),
);
ctx.subscriptions.push(compareView);

Expand Down Expand Up @@ -853,6 +848,18 @@ async function activateWithInstalledDistribution(
true,
);

const localQueries = new LocalQueries(
app,
qs,
qhm,
dbm,
cliServer,
databaseUI,
localQueryResultsView,
queryStorageDir,
);
ctx.subscriptions.push(localQueries);

void extLogger.log("Initializing QLTest interface.");
const testExplorerExtension = extensions.getExtension<TestHub>(
testExplorerExtensionId,
Expand Down Expand Up @@ -906,11 +913,7 @@ async function activateWithInstalledDistribution(
...databaseUI.getCommands(),
...dbModule.getCommands(),
...getAstCfgCommands({
queryRunner: qs,
queryHistoryManager: qhm,
databaseUI,
localQueryResultsView,
queryStorageDir,
localQueries,
astViewer,
astTemplateProvider,
cfgTemplateProvider,
Expand All @@ -930,16 +933,7 @@ async function activateWithInstalledDistribution(
}

const queryServerCommands: QueryServerCommands = {
...getLocalQueryCommands({
app,
queryRunner: qs,
queryHistoryManager: qhm,
databaseManager: dbm,
cliServer,
databaseUI,
localQueryResultsView,
queryStorageDir,
}),
...localQueries.getCommands(),
};

for (const [commandName, command] of Object.entries(queryServerCommands)) {
Expand Down Expand Up @@ -989,6 +983,7 @@ async function activateWithInstalledDistribution(
return {
ctx,
cliServer,
localQueries,
qs,
distributionManager,
databaseManager: dbm,
Expand Down
Loading