Skip to content

Commit 5c2c9aa

Browse files
committed
optimize static check
1 parent 5c0e055 commit 5c2c9aa

4 files changed

Lines changed: 171 additions & 34 deletions

File tree

src/EIDEProject.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
ConfigMap, FileGroup,
4646
ProjectConfiguration, ProjectConfigData, WorkspaceConfiguration,
4747
CreateOptions,
48-
ProjectConfigEvent, ProjectFileGroup, FileItem, EIDE_CONF_VERSION, ProjectTargetInfo, VirtualFolder, VirtualFile, CppConfigItem, ProjectBaseApi, ProjectType
48+
ProjectConfigEvent, ProjectFileGroup, FileItem, EIDE_CONF_VERSION, ProjectTargetInfo, VirtualFolder, VirtualFile, CppConfigItem, ProjectBaseApi, ProjectType, BuilderConfigData
4949
} from './EIDETypeDefine';
5050
import { ToolchainName, IToolchian, ToolchainManager } from './ToolchainManager';
5151
import { GlobalEvent } from './GlobalEvents';
@@ -57,7 +57,7 @@ import { WebPanelManager } from './WebPanelManager';
5757
import { DependenceManager } from './DependenceManager';
5858
import * as platform from './Platform';
5959
import { IDebugConfigGenerator } from './DebugConfigGenerator';
60-
import { md5, copyObject, compareVersion } from './utility';
60+
import { md5, copyObject, compareVersion, isGccFamilyToolchain } from './utility';
6161
import { ResInstaller } from './ResInstaller';
6262
import {
6363
view_str$prompt$not_found_compiler, view_str$operation$name_can_not_be_blank,
@@ -3568,6 +3568,18 @@ class EIDEProject extends AbstractProject {
35683568
}
35693569
}
35703570

3571+
private _getCompilerIntrDefsForCpptools<T extends BuilderConfigData>(
3572+
toolchain: IToolchian, builderCfg: T, builderOpts: ICompileOptions): string[] {
3573+
3574+
if (['AC5', 'AC6'].includes(toolchain.name) || isGccFamilyToolchain(toolchain.name)) {
3575+
// we have provide a xxx-intr.h for cpptools,
3576+
// so return empty list.
3577+
return [];
3578+
} else {
3579+
return toolchain.getInternalDefines(builderCfg, builderOpts);
3580+
}
3581+
}
3582+
35713583
private doUpdateCpptoolsConfig() {
35723584

35733585
const builderOpts = this.getBuilderOptions();
@@ -3577,7 +3589,7 @@ class EIDEProject extends AbstractProject {
35773589
// get project includes and defines
35783590
const depMerge = prjConfig.GetAllMergeDep();
35793591
const defMacros: string[] = ['__VSCODE_CPPTOOL']; // it's for internal force include header
3580-
const intrDefs = toolchain.getInternalDefines(<any>prjConfig.config.compileConfig, builderOpts);
3592+
const intrDefs = this._getCompilerIntrDefsForCpptools(toolchain, <any>prjConfig.config.compileConfig, builderOpts);
35813593
const defLi = defMacros.concat(depMerge.defineList, intrDefs);
35823594
depMerge.incList = depMerge.incList.concat(this.getSourceIncludeList()).map(p => this.ToAbsolutePath(p));
35833595

src/EIDEProjectExplorer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5635,20 +5635,25 @@ export class ProjectExplorer implements CustomConfigurationProvider {
56355635
cppcheck_plat = 'avr8';
56365636
}
56375637

5638+
const sourceList = getSourceList(prj);
56385639
cppcheckConf = cppcheckConf
56395640
.replace('${cppcheck_build_folder}', File.normalize(prj.getOutputRoot()))
56405641
.replace('${platform}', cppcheck_plat)
56415642
.replace('${lib_list}', cfgList.map((str) => `<library>${escapeXml(str)}</library>`).join(os.EOL + '\t\t'))
56425643
.replace('${include_list}', includeList.map((str) => `<dir name="${escapeXml(str)}/"/>`).join(os.EOL + '\t\t'))
56435644
.replace('${macro_list}', fixedDefList.map((str) => `<define name="${escapeXml(str)}"/>`).join(os.EOL + '\t\t'))
5644-
.replace('${source_list}', getSourceList(prj).map((str) => `<dir name="${escapeXml(str)}"/>`).join(os.EOL + '\t\t'));
5645+
.replace('${source_list}', sourceList.map((str) => `<dir name="${escapeXml(str)}"/>`).join(os.EOL + '\t\t'));
56455646

56465647
confFile.Write(cppcheckConf);
56475648

56485649
/* make command */
56495650

5651+
let max_cpus = os.cpus().length;
5652+
if (max_cpus > sourceList.length * 2) max_cpus = sourceList.length;
5653+
if (max_cpus < 4) max_cpus = 4;
5654+
if (max_cpus > 12) max_cpus = 12;
56505655
cmds.push(
5651-
'-j', '4',
5656+
'-j', max_cpus.toString(),
56525657
`--error-exitcode=0`,
56535658
`--report-progress`,
56545659
`--enable=warning`,

src/ToolchainManager.ts

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import * as fs from 'fs';
3737
import * as events from 'events';
3838
import * as NodePath from 'path';
3939
import * as os from 'os';
40-
import { ICompileOptions, ArmBaseBuilderConfigData } from "./EIDEProjectModules";
40+
import { ICompileOptions, ArmBaseBuilderConfigData, ArmBaseCompileData } from "./EIDEProjectModules";
4141
import * as utility from "./utility";
4242

4343
export type ToolchainName =
@@ -101,7 +101,7 @@ export interface IToolchian {
101101
getGccFamilyCompilerPathForCpptools(): string | undefined;
102102

103103
/**
104-
* get compiler internal defines (for cpptools)
104+
* get compiler internal defines (for static check)
105105
*/
106106
getInternalDefines<T extends BuilderConfigData>(builderCfg: T, builderOpts: ICompileOptions): string[];
107107

@@ -442,27 +442,6 @@ export class ToolchainManager {
442442
}
443443
}
444444

445-
class MacroHandler {
446-
447-
private regMatchers = {
448-
'normal_macro': /^#define (\w+) (.*)$/,
449-
'func_macro': /^#define (\w+\([^\)]*\)) (.*)$/
450-
};
451-
452-
toExpression(macro: string): string | undefined {
453-
454-
let mList = this.regMatchers['normal_macro'].exec(macro);
455-
if (mList && mList.length > 2) {
456-
return `${mList[1]}=${mList[2]}`;
457-
}
458-
459-
mList = this.regMatchers['func_macro'].exec(macro);
460-
if (mList && mList.length > 2) {
461-
return `${mList[1]}=`;
462-
}
463-
}
464-
}
465-
466445
//=======================================================
467446

468447
class KeilC51 implements IToolchian {
@@ -1499,7 +1478,7 @@ class AC5 implements IToolchian {
14991478
if (cpuMap[cpuKey]) {
15001479

15011480
const result: string[] = [];
1502-
const macroParser = new MacroHandler();
1481+
const macroParser = new utility.CppMacroDefinesConv();
15031482
const armccDir = File.fromArray([this.getToolchainDir().path, 'bin']).path;
15041483
const cmdList = [`--cpu ${cpuMap[cpuKey]}`, '--apcs=interwork'];
15051484

@@ -1973,7 +1952,57 @@ class GCC implements IToolchian {
19731952
}
19741953

19751954
getInternalDefines<T extends BuilderConfigData>(builderCfg: T, builderOpts: ICompileOptions): string[] {
1976-
return [];
1955+
1956+
return [
1957+
'__GNUC__=10',
1958+
'__GNUC_MINOR__=2',
1959+
'__GNUC_PATCHLEVEL__=1',
1960+
'__GNUC_STDC_INLINE__=1',
1961+
'__thumb__=1',
1962+
'__thumb2__=1'
1963+
];
1964+
1965+
// const cfg: ArmBaseBuilderConfigData = <any>builderCfg;
1966+
1967+
// const cpu = cfg.cpuType.toLowerCase()
1968+
// .replace('cortex-m0+', 'cortex-m0plus');
1969+
// const fpu = cfg.floatingPointHardware.toLowerCase()
1970+
// .replace('single', 'sp').replace('double', 'dp');
1971+
1972+
// const compilerArgs: string[] = ['-mthumb'];
1973+
1974+
// // setup cpu
1975+
// compilerArgs.push(`-mcpu=${cpu}`);
1976+
1977+
// // setup fpu
1978+
// switch (fpu) {
1979+
// case 'sp':
1980+
// compilerArgs.push(`-mfpu=fpv5-sp-d16`);
1981+
// break;
1982+
// case 'dp':
1983+
// compilerArgs.push(`-mfpu=fpv5-d16`);
1984+
// break;
1985+
// default:
1986+
// break;
1987+
// }
1988+
// if (['sp', 'dp'].includes(fpu)) {
1989+
// if (typeof builderOpts.global['$float-abi-type'] == 'string') {
1990+
// const abiType = builderOpts.global['$float-abi-type'];
1991+
// compilerArgs.push(`-mfloat-abi=${abiType}`);
1992+
// }
1993+
// }
1994+
1995+
// const r = utility.getGccInternalDefines(
1996+
// this.getToolchainDir().path + '/bin', this.getToolPrefix(), compilerArgs);
1997+
1998+
// if (!r)
1999+
// return [];
2000+
2001+
// const result: string[] = r
2002+
// .filter(d => d.type != 'func')
2003+
// .map(d => `${d.name}=${(d.value == undefined || d.value == '') ? '1' : d.value}`);
2004+
2005+
// return result;
19772006
}
19782007

19792008
getCustomDefines(): string[] | undefined {
@@ -2496,7 +2525,12 @@ class MTI_GCC implements IToolchian {
24962525
}
24972526

24982527
getInternalDefines<T extends BuilderConfigData>(builderCfg: T, builderOpts: ICompileOptions): string[] {
2499-
return [];
2528+
return [
2529+
'__GNUC__=10',
2530+
'__GNUC_MINOR__=2',
2531+
'__GNUC_PATCHLEVEL__=1',
2532+
'__GNUC_STDC_INLINE__=1',
2533+
];
25002534
}
25012535

25022536
getCustomDefines(): string[] | undefined {
@@ -2758,7 +2792,12 @@ class RISCV_GCC implements IToolchian {
27582792
}
27592793

27602794
getInternalDefines<T extends BuilderConfigData>(builderCfg: T, builderOpts: ICompileOptions): string[] {
2761-
return [];
2795+
return [
2796+
'__GNUC__=10',
2797+
'__GNUC_MINOR__=2',
2798+
'__GNUC_PATCHLEVEL__=1',
2799+
'__GNUC_STDC_INLINE__=1',
2800+
];
27622801
}
27632802

27642803
getCustomDefines(): string[] | undefined {
@@ -2970,7 +3009,12 @@ class AnyGcc implements IToolchian {
29703009
}
29713010

29723011
getInternalDefines<T extends BuilderConfigData>(builderCfg: T, builderOpts: ICompileOptions): string[] {
2973-
return [];
3012+
return [
3013+
'__GNUC__=10',
3014+
'__GNUC_MINOR__=2',
3015+
'__GNUC_PATCHLEVEL__=1',
3016+
'__GNUC_STDC_INLINE__=1',
3017+
];
29743018
}
29753019

29763020
getCustomDefines(): string[] | undefined {

src/utility.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,82 @@ import { isArray } from 'util';
4242
import { ExeCmd } from '../lib/node-utility/Executable';
4343
import { GlobalEvent } from './GlobalEvents';
4444
import { SettingManager } from './SettingManager';
45+
import { ToolchainName } from './ToolchainManager';
46+
47+
export interface CppMacroDefine {
48+
type: 'var' | 'func';
49+
name: string;
50+
value: string;
51+
};
52+
53+
export class CppMacroDefinesConv {
54+
55+
private regMatchers = {
56+
'var' : /^#define (\w+) (.*)$/,
57+
'func': /^#define (\w+\([^\)]*\)) (.*)$/
58+
};
59+
60+
toExpression(line: string): string | undefined {
61+
62+
let mList = this.regMatchers['var'].exec(line);
63+
if (mList && mList.length > 2) {
64+
return `${mList[1]}=${mList[2]}`;
65+
}
66+
67+
mList = this.regMatchers['func'].exec(line);
68+
if (mList && mList.length > 2) {
69+
return `${mList[1]}=`;
70+
}
71+
}
72+
73+
parse(line: string): CppMacroDefine | undefined {
74+
75+
let mList = this.regMatchers['var'].exec(line);
76+
if (mList && mList.length > 2) {
77+
return {
78+
type: 'var',
79+
name: mList[1],
80+
value: mList[2],
81+
};
82+
}
83+
84+
mList = this.regMatchers['func'].exec(line);
85+
if (mList && mList.length > 2) {
86+
return {
87+
type: 'func',
88+
name: mList[1],
89+
value: mList[2],
90+
};
91+
}
92+
}
93+
}
94+
95+
export function getGccInternalDefines(gcc_dir: string, gcc_prefix: string, cmds: string[] | undefined): CppMacroDefine[] | undefined {
96+
try {
97+
const gccName = gcc_prefix + 'gcc';
98+
const cmdArgs = (cmds || []).concat(['-E', '-dM', '-', `<${platform.osGetNullDev()}`]);
99+
const cmdLine = `${gccName} ` + cmdArgs.join(' ');
100+
const outputs = child_process.execSync(cmdLine, { cwd: gcc_dir }).toString().split(/\r\n|\n/);
101+
const results: CppMacroDefine[] = [];
102+
const mHandler = new CppMacroDefinesConv();
103+
104+
outputs.filter((line) => { return line.trim() !== ''; })
105+
.forEach((line) => {
106+
const value = mHandler.parse(line);
107+
if (value) {
108+
results.push(value);
109+
}
110+
});
111+
112+
return results;
113+
} catch (error) {
114+
GlobalEvent.emit('globalLog', ExceptionToMessage(error, 'Warning'));
115+
}
116+
}
117+
118+
export function isGccFamilyToolchain(name: ToolchainName): boolean {
119+
return name.includes('GCC');
120+
}
45121

46122
export function getGccSystemSearchList(gccPath: string): string[] | undefined {
47123
try {
@@ -58,7 +134,7 @@ export function getGccSystemSearchList(gccPath: string): string[] | undefined {
58134
return f.path;
59135
});
60136
} catch (error) {
61-
// do nothing
137+
GlobalEvent.emit('globalLog', ExceptionToMessage(error, 'Warning'));
62138
}
63139
}
64140

0 commit comments

Comments
 (0)