All files / src/linters clang.ts

90.47% Statements 57/63
53.57% Branches 15/28
85.71% Functions 6/7
90.47% Lines 57/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208        7x 7x 7x   7x 7x   7x 8x 8x     8x 8x   8x 8x 8x                 3x 3x     3x               3x     3x 3x   3x   3x   3x   3x         3x         3x         3x         3x         3x           3x                                                 3x     3x     3x 3x   3x       20x     20x         20x 20x   1x       19x   19x 3x                     16x 15x                   1x                           15x 15x       3x   3x       3x       3x       3x 3x   3x 3x 3x     3x      
// Copyright (c) 2017-2022 The VSCode C/C++ Flylint Authors
//
// SPDX-License-Identifier: MIT
 
import * as path from 'path';
import { ClangSeverityMaps, Settings, VS_DiagnosticSeverity } from '../settings';
import { Linter, Lint } from './linter';
import { InternalDiagnostic } from '../server';
import { path as sysPath } from '../utils';
import { DiagnosticSeverity } from 'vscode-languageserver/node';
 
export class Clang extends Linter {
    private actualFileName: string = '';
    private tmpFileName: string = '';
 
    constructor(settings: Settings, workspaceRoot: string) {
        super('Clang', settings, workspaceRoot, false);
        this.cascadeCommonSettings('clang');
 
        this.executable = settings.clang.executable;
        this.configFile = settings.clang.configFile;
        this.active = this.enabled = settings.clang.enable;
    }
 
    /* istanbul ignore next */
    public lintOn(): Lint[] {
        return [Lint.ON_SAVE, Lint.ON_TYPE, Lint.ON_BUILD];
    }
 
    protected buildCommandLine(fileName: string, tmpFileName: string): string[] {
        let includePathParams = this.getIncludePathParams();
        let languageParam = this.getLanguageParam();
        let iquoteParams: string[];
 
        Iif (this.settings.run === 'onType') {
            iquoteParams = this.expandedArgsFor(
                '-iquote',
                false,
                [path.dirname(fileName)].concat(this.includePaths),
                null
            );
        } else {
            iquoteParams = [];
        }
 
        let pedanticParams = this.getPedanticParams();
        let msExtensions = this.settings.clang.msExtensions ?
            ['-fms-extensions'] : [];
        let noExceptions = this.settings.clang.noExceptions ?
            ['-fno-exceptions'] : [];
        let noRtti = this.settings.clang.noRtti ?
            ['-fno-rtti'] : [];
        let blocks = this.settings.clang.blocks ?
            ['-fblocks'] : [];
        let includeArgParams = this.expandedArgsFor(
            '-include',
            false,
            this.settings.clang.includes,
            null);
        let warningsParams = this.expandedArgsFor(
            '-W',
            true,
            this.settings.clang.warnings,
            null);
        let standardParams = this.expandedArgsFor(
            '--std=',
            true,
            this.standard,
            ['c11', 'c++11']);
        let standardLibParams = this.expandedArgsFor(
            '--stdlib=',
            true,
            this.settings.clang.standardLibs,
            null);
        let defineParams = this.expandedArgsFor(
            '-D',
            true,
            this.defines,
            null);
        let undefineParams = this.expandedArgsFor(
            '-U',
            true,
            this.undefines,
            null);
 
        let args = [
            this.executable,
            '-fsyntax-only',
            '-fno-color-diagnostics',
            '-fno-caret-diagnostics',
            '-fno-diagnostics-show-option',
            '-fdiagnostics-show-category=name',
            '-ferror-limit=200'
        ]
            .concat(iquoteParams)
            .concat(standardParams)
            .concat(pedanticParams)
            .concat(standardLibParams)
            .concat(msExtensions)
            .concat(noExceptions)
            .concat(noRtti)
            .concat(blocks)
            .concat(includeArgParams)
            .concat(warningsParams)
            .concat(defineParams)
            .concat(undefineParams)
            .concat(includePathParams)
            .concat(languageParam)
            .concat((this.settings.clang.extraArgs || []).map(a => this.expandVariables(a).result || a));
 
        Iif (this.settings.run === 'onType') {
            args.push(sysPath(tmpFileName));
        } else {
            args.push(sysPath(fileName));
        }
 
        this.actualFileName = fileName;
        this.tmpFileName = tmpFileName;
 
        return args;
    }
 
    protected parseLine(line: string): InternalDiagnostic | null {
        let regex = /^(.+?):([0-9]+):([0-9]+):\s(fatal|error|warning|note)(?: error)?:\s(.*)$/;
        let regexArray: RegExpExecArray | null;
 
        Iif (line === '') {
            // skip this line
            return null;
        }
 
        let excludeRegex = /^(WX.*|_WX.*|__WX.*|Q_.*|warning: .* incompatible with .*|warning: .* input unused|warning: include location .* is unsafe for cross-compilation.*)$/;
        if (excludeRegex.exec(line) !== null) {
            // skip this line
            return null;
        }
 
        let inFileArray: RegExpExecArray | null;
        let inFileRegex = /^In file included from (.+?):([0-9]+):$/;
 
        if ((inFileArray = inFileRegex.exec(line)) !== null) {
            return {
                fileName: (inFileArray[1] === this.tmpFileName ? this.actualFileName : inFileArray[1]),
                line: parseInt(inFileArray[2]) - 1,
                column: 0,
                severity: DiagnosticSeverity.Warning,
                code: 0,
                message: 'Issues in file included from here',
                source: this.name
            };
        }
 
        if ((regexArray = regex.exec(line)) !== null) {
            return {
                fileName: (regexArray[1] === this.tmpFileName ? this.actualFileName : regexArray[1]),
                line: parseInt(regexArray[2]) - 1,
                column: parseInt(regexArray[3]) - 1,
                severity: this.getSeverityCode(regexArray[4]),
                code: 0,
                message: regexArray[5],
                source: this.name,
            };
        } else {
            return {
                parseError: 'Line could not be parsed: ' + line,
                fileName: '',
                line: 0,
                column: 0,
                severity: DiagnosticSeverity.Error,
                code: 0,
                message: '',
                source: this.name
            };
        }
    }
 
    private getSeverityCode(severity: string): DiagnosticSeverity {
        let output = this.settings.clang.severityLevels[severity as keyof ClangSeverityMaps];
        return VS_DiagnosticSeverity.from(output);
    }
 
    private getPedanticParams(): string[] {
        let params: string[] = [];
 
        Iif (this.settings.clang.pedantic) {
            params.push(`-pedantic`);
        }
 
        Iif (this.settings.clang.pedanticErrors) {
            params.push(`-pedantic-errors`);
        }
 
        return params;
    }
 
    private getLanguageParam(): string[] {
        let language = this.language;
        let params: string[] = [];
 
        if (this.isValidLanguage(language)) {
            params.push(`-x`);
            params.push(`${language}`);
        }
 
        return params;
    }
}