Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support parsing substring with ' ' space #1648

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e85071f
add sub-string
elahehrashedi Nov 17, 2020
e0c2332
Merge branch 'develop' into ela/empty-define
elahehrashedi Nov 17, 2020
bc09fe5
add (token || '')
elahehrashedi Nov 17, 2020
3fbe125
Merge branch 'develop' into ela/empty-define
elahehrashedi Nov 17, 2020
c6cdcc3
Merge branch 'develop' into ela/empty-define
elahehrashedi Dec 14, 2020
b0996fe
check for \' comments
elahehrashedi Dec 14, 2020
4efe4f5
Merge branch 'develop' into ela/empty-define
elahehrashedi Jan 4, 2021
f742d91
Merge branch 'develop' into ela/empty-define
elahehrashedi Feb 3, 2021
4844340
add both " and ' to posix substring
elahehrashedi Feb 3, 2021
526355f
Merge branch 'develop' into ela/empty-define
elahehrashedi Feb 3, 2021
3d0aa55
keep the command as a string
elahehrashedi Feb 3, 2021
c769c6c
add quote to arguments with space
elahehrashedi Feb 3, 2021
576b8d7
look for ' ' inside a compile-command substring
elahehrashedi Feb 4, 2021
4ba00e3
this version works well
elahehrashedi Feb 8, 2021
25d6390
clean the code before adding test
elahehrashedi Feb 8, 2021
cabe77e
Clean the code before adding test
elahehrashedi Feb 8, 2021
2745f0f
add tests
elahehrashedi Feb 9, 2021
68aaba0
Merge branch 'develop' into ella/space-compdb
elahehrashedi Feb 9, 2021
76bd6a8
let to const
elahehrashedi Feb 9, 2021
e039488
Merge branch 'ella/space-compdb' of https://github.com/microsoft/vsco…
elahehrashedi Feb 9, 2021
e627cbc
remove a test to check for later
elahehrashedi Feb 9, 2021
f1463fa
remove a test to check in later
elahehrashedi Feb 9, 2021
610aac7
modify tests
elahehrashedi Feb 10, 2021
e0a99bb
move test folder
elahehrashedi Feb 10, 2021
84f9e02
remove comments
elahehrashedi Feb 10, 2021
126b14e
fix a test
elahehrashedi Feb 10, 2021
7fb7128
remove two quotes inside quotes in posix
elahehrashedi Feb 10, 2021
701dbf4
fix a test
elahehrashedi Feb 10, 2021
09a7568
Merge branch 'develop' into ella/space-compdb
elahehrashedi Feb 10, 2021
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
16 changes: 6 additions & 10 deletions src/compdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,21 @@ interface BaseCompileCommand {
output?: string;
}

interface StringCompileCommand extends BaseCompileCommand {
export interface ArgsCompileCommand extends BaseCompileCommand {
command: string;
arguments?: string[];
}

interface ArgsCompileCommand extends BaseCompileCommand {
arguments: string[];
}

export type CompileCommand = StringCompileCommand|ArgsCompileCommand;

export class CompilationDatabase {
private readonly _infoByFilePath: Map<string, ArgsCompileCommand>;
constructor(infos: CompileCommand[]) {
constructor(infos: ArgsCompileCommand[]) {
this._infoByFilePath = infos.reduce(
(acc, cur) => acc.set(util.platformNormalizePath(cur.file), {
directory: cur.directory,
file: cur.file,
output: cur.output,
arguments: 'arguments' in cur ? cur.arguments : [...shlex.split(cur.command)],
command: cur.command,
arguments: cur.arguments ? cur.arguments : [...shlex.split(cur.command)],
}),
new Map<string, ArgsCompileCommand>(),
);
Expand All @@ -48,7 +44,7 @@ export class CompilationDatabase {
}
const data = await fs.readFile(dbpath);
try {
const content = JSON.parse(data.toString()) as CompileCommand[];
const content = JSON.parse(data.toString()) as ArgsCompileCommand[];
return new CompilationDatabase(content);
} catch (e) {
log.warning(localize('error.parsing.compilation.database', 'Error parsing compilation database "{0}": {1}', dbpath, util.errorToString(e)));
Expand Down
52 changes: 23 additions & 29 deletions src/drivers/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as api from '@cmt/api';
import {CMakeExecutable} from '@cmt/cmake/cmake-executable';
import * as codepages from '@cmt/code-pages';
import {ConfigureTrigger} from "@cmt/cmake-tools";
import {CompileCommand} from '@cmt/compdb';
import {ArgsCompileCommand} from '@cmt/compdb';
import {ConfigurationReader} from '@cmt/config';
import {CMakeBuildConsumer, CompileOutputConsumer} from '@cmt/diagnostics/build';
import {CMakeOutputConsumer} from '@cmt/diagnostics/cmake';
Expand All @@ -22,7 +22,6 @@ import paths from '@cmt/paths';
import {fs} from '@cmt/pr';
import * as proc from '@cmt/proc';
import rollbar from '@cmt/rollbar';
import * as shlex from '@cmt/shlex';
import * as telemetry from '@cmt/telemetry';
import * as util from '@cmt/util';
import {ConfigureArguments, VariantOption} from '@cmt/variant';
Expand Down Expand Up @@ -267,34 +266,29 @@ export abstract class CMakeDriver implements vscode.Disposable {
* Launch the given compilation command in an embedded terminal.
* @param cmd The compilation command from a compilation database to run
*/
runCompileCommand(cmd: CompileCommand): vscode.Terminal {
if ('command' in cmd) {
const args = [...shlex.split(cmd.command)];
return this.runCompileCommand({directory: cmd.directory, file: cmd.file, arguments: args});
} else {
const env = this.getEffectiveSubprocessEnvironment();
const key = `${cmd.directory}${JSON.stringify(env)}`;
let existing = this._compileTerms.get(key);
if (existing && this.config.clearOutputBeforeBuild) {
this._compileTerms.delete(key);
existing.dispose();
existing = undefined;
}
if (!existing) {
const shellPath = process.platform === 'win32' ? 'cmd.exe' : undefined;
const term = vscode.window.createTerminal({
name: localize('file.compilation', 'File Compilation'),
cwd: cmd.directory,
env,
shellPath,
});
this._compileTerms.set(key, term);
existing = term;
}
existing.show();
existing.sendText(cmd.arguments.map(s => shlex.quote(s)).join(' ') + '\r\n');
return existing;
runCompileCommand(cmd: ArgsCompileCommand): vscode.Terminal {
const env = this.getEffectiveSubprocessEnvironment();
const key = `${cmd.directory}${JSON.stringify(env)}`;
let existing = this._compileTerms.get(key);
if (existing && this.config.clearOutputBeforeBuild) {
this._compileTerms.delete(key);
existing.dispose();
existing = undefined;
}
if (!existing) {
const shellPath = process.platform === 'win32' ? 'cmd.exe' : undefined;
const term = vscode.window.createTerminal({
name: localize('file.compilation', 'File Compilation'),
cwd: cmd.directory,
env,
shellPath,
});
this._compileTerms.set(key, term);
existing = term;
}
existing.show();
existing.sendText(cmd.command + '\r\n');
return existing;
}

/**
Expand Down
29 changes: 21 additions & 8 deletions src/shlex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface ShlexOptions {
mode: 'windows'|'posix';
mode: 'windows' | 'posix';
}

export function* split(str: string, opt?: ShlexOptions): Iterable<string> {
Expand All @@ -11,9 +11,10 @@ export function* split(str: string, opt?: ShlexOptions): Iterable<string> {
if (opt.mode == 'posix') {
escapeChars += '\\';
}
let quoteChar: string|undefined;
let escapeChar: string|undefined;
let token: string|undefined;
let quoteChar: string | undefined;
let escapeChar: string | undefined;
let token: string | undefined;
let subquote: boolean = false;
for (let i = 0; i < str.length; ++i) {
const char = str.charAt(i);
if (escapeChar) {
Expand Down Expand Up @@ -41,26 +42,38 @@ export function* split(str: string, opt?: ShlexOptions): Iterable<string> {

if (quoteChar) {
if (char === quoteChar) {
// Reached the end of a sub-quoted token.
if (subquote) {
subquote = false;
token = (token || '') + char;
}
// Reached the end of a quoted token.
quoteChar = undefined;
continue;
}

// Another quoted char
token = (token || '') + char;
continue;
}

if (quoteChars.includes(char)) {
// Beginning of a sub-quoted token
if (i > 0 && (opt.mode === 'posix' ? (str.substring(i - 1, i + 1) === "\\\'" || str.substring(i - 1, i + 1) === "\\\"") : str.substring(i - 1, i + 1) === "\\\"")) {
elahehrashedi marked this conversation as resolved.
Show resolved Hide resolved
subquote = true;
quoteChar = char;
// Accumulate
token = (token || '') + char;
continue;
}
// Beginning a new quoted token
quoteChar = char;
token = '';
continue;
}

if (/[\t \n\r\f]/.test(char)) {
if (!quoteChar && /[\t \n\r\f]/.test(char)) {
if (token !== undefined) {
yield token;
yield token.includes(' ') ? `"${token}"` : token;
}
token = undefined;
continue;
Expand All @@ -71,7 +84,7 @@ export function* split(str: string, opt?: ShlexOptions): Iterable<string> {
}

if (token !== undefined) {
yield token;
yield token.includes(' ') ? `"${token}"` : token;
}
}

Expand Down