Skip to content

Commit 90f922d

Browse files
authored
default to c++14 on mac for build and run active file (#11300)
1 parent 4080514 commit 90f922d

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

Extension/src/LanguageServer/cppBuildTaskProvider.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export class CppBuildTask extends Task {
3131
isDefault?: boolean;
3232
}
3333

34+
interface BuildOptions {
35+
taskUsesActiveFile: boolean;
36+
insertStd?: boolean;
37+
}
38+
3439
export class CppBuildTaskProvider implements TaskProvider {
3540
static CppBuildScriptType: string = 'cppbuild';
3641

@@ -166,6 +171,7 @@ export class CppBuildTaskProvider implements TaskProvider {
166171
private getTask: (compilerPath: string, appendSourceToName: boolean, compilerArgs?: string[], definition?: CppBuildTaskDefinition, detail?: string) => Task = (compilerPath: string, appendSourceToName: boolean, compilerArgs?: string[], definition?: CppBuildTaskDefinition, detail?: string) => {
167172
const compilerPathBase: string = path.basename(compilerPath);
168173
const isCl: boolean = compilerPathBase.toLowerCase() === "cl.exe";
174+
const isClang: boolean = !isCl && compilerPathBase.toLowerCase().includes("clang");
169175
// Double-quote the command if it is not already double-quoted.
170176
let resolvedcompilerPath: string = isCl ? compilerPathBase : compilerPath;
171177
if (resolvedcompilerPath && !resolvedcompilerPath.startsWith("\"") && resolvedcompilerPath.includes(" ")) {
@@ -177,12 +183,12 @@ export class CppBuildTaskProvider implements TaskProvider {
177183
const taskLabel: string = ((appendSourceToName && !compilerPathBase.startsWith(ext.configPrefix)) ?
178184
ext.configPrefix : "") + compilerPathBase + " " + localize("build_active_file", "build active file");
179185
const programName: string = util.defaultExePath();
180-
const isClang: boolean = !isCl && compilerPathBase.toLowerCase().includes("clang");
181186
let args: string[] = isCl ?
182187
['/Zi', '/EHsc', '/nologo', `/Fe${programName}`, '${file}'] :
183188
isClang ?
184189
['-fcolor-diagnostics', '-fansi-escape-codes', '-g', '${file}', '-o', programName] :
185190
['-fdiagnostics-color=always', '-g', '${file}', '-o', programName];
191+
186192
if (compilerArgs && compilerArgs.length > 0) {
187193
args = args.concat(compilerArgs);
188194
}
@@ -205,7 +211,7 @@ export class CppBuildTaskProvider implements TaskProvider {
205211
const task: CppBuildTask = new Task(definition, scope, definition.label, ext.CppSourceStr,
206212
new CustomExecution(async (resolvedDefinition: TaskDefinition): Promise<Pseudoterminal> =>
207213
// When the task is executed, this callback will run. Here, we setup for running the task.
208-
new CustomBuildTaskTerminal(resolvedcompilerPath, resolvedDefinition.args, resolvedDefinition.options, taskUsesActiveFile)
214+
new CustomBuildTaskTerminal(resolvedcompilerPath, resolvedDefinition.args, resolvedDefinition.options, {taskUsesActiveFile, insertStd: isClang && os.platform() === 'darwin'})
209215
), isCl ? '$msCompile' : '$gcc');
210216

211217
task.group = TaskGroup.Build;
@@ -352,15 +358,20 @@ class CustomBuildTaskTerminal implements Pseudoterminal {
352358
public get onDidClose(): Event<number> { return this.closeEmitter.event; }
353359
private endOfLine: string = "\r\n";
354360

355-
constructor(private command: string, private args: string[], private options: cp.ExecOptions | cp.SpawnOptions | undefined, private taskUsesActiveFile: boolean) {
361+
constructor(private command: string, private args: string[], private options: cp.ExecOptions | cp.SpawnOptions | undefined, private buildOptions: BuildOptions) {
356362
}
357363

358364
async open(_initialDimensions: TerminalDimensions | undefined): Promise<void> {
359-
if (this.taskUsesActiveFile && !util.isCppOrCFile(window.activeTextEditor?.document.uri)) {
365+
if (this.buildOptions.taskUsesActiveFile && !util.isCppOrCFile(window.activeTextEditor?.document.uri)) {
360366
this.writeEmitter.fire(localize("cannot.build.non.cpp", 'Cannot build and debug because the active file is not a C or C++ source file.') + this.endOfLine);
361367
this.closeEmitter.fire(-1);
362368
return;
363369
}
370+
371+
// TODO: Remove when compiler query work goes in and we can determine the standard version from TypeScript
372+
if (this.buildOptions.taskUsesActiveFile && window.activeTextEditor?.document.languageId === 'cpp' && this.buildOptions.insertStd) {
373+
this.args.unshift('-std=gnu++14');
374+
}
364375
telemetry.logLanguageServerEvent("cppBuildTaskStarted");
365376
// At this point we can start using the terminal.
366377
this.writeEmitter.fire(localize("starting_build", "Starting build...") + this.endOfLine);

0 commit comments

Comments
 (0)