GeorgeGribkov | eeb6bc5 | 2020-05-19 10:48:29 +0300 | [diff] [blame] | 1 | // Copyright (c) 2020, Compiler Explorer Authors |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer in the |
| 11 | // documentation and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 23 | // POSSIBILITY OF SUCH DAMAGE. |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 24 | |
Austin Morton | 044dcfb | 2020-09-26 16:59:26 -0400 | [diff] [blame] | 25 | import path from 'path'; |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 26 | |
Austin Morton | 044dcfb | 2020-09-26 16:59:26 -0400 | [diff] [blame] | 27 | import fs from 'fs-extra'; |
| 28 | |
| 29 | import * as exec from '../exec'; |
| 30 | import { logger } from '../logger'; |
| 31 | import * as utils from '../utils'; |
| 32 | |
| 33 | import { BaseTool } from './base-tool'; |
| 34 | |
| 35 | export class PvsStudioTool extends BaseTool { |
Matt Godbolt | aceaae2 | 2021-04-03 18:09:58 -0500 | [diff] [blame] | 36 | static get key() { |
| 37 | return 'pvs-studio-tool'; |
| 38 | } |
Austin Morton | bac07fe | 2020-09-25 11:21:30 -0400 | [diff] [blame] | 39 | |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 40 | constructor(toolInfo, env) { |
| 41 | super(toolInfo, env); |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 42 | this.plogConverterPath = this.env.compilerProps('plogConverter', '/usr/bin/plog-converter'); |
Rubén Rincón Blanco | 4e683c5 | 2021-02-08 07:15:13 +0100 | [diff] [blame] | 43 | |
| 44 | this.addOptionsToToolArgs = false; |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | async runTool(compilationInfo, inputFilepath, args) { |
Matt Godbolt | 29125df | 2020-06-03 07:14:54 -0500 | [diff] [blame] | 48 | if (compilationInfo.code !== 0) { |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 49 | return this.createErrorResponse('Unable to start analysis due to compilation error.'); |
GeorgeGribkov | d6fd4e7 | 2020-06-03 11:52:47 +0300 | [diff] [blame] | 50 | } |
| 51 | |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 52 | const sourceDir = path.dirname(inputFilepath); |
| 53 | |
| 54 | // Collecting the flags of compilation |
Partouf | 56e64c1 | 2021-03-04 22:08:42 +0100 | [diff] [blame] | 55 | let compileFlags = utils.splitArguments(compilationInfo.compiler.options); |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 56 | |
| 57 | const includeflags = super.getIncludeArguments(compilationInfo.libraries, compilationInfo.compiler); |
| 58 | compileFlags = compileFlags.concat(includeflags); |
| 59 | |
Patrick Quist | 697c8e9 | 2020-09-25 02:09:47 +0200 | [diff] [blame] | 60 | const libOptions = super.getLibraryOptions(compilationInfo.libraries, compilationInfo.compiler); |
| 61 | compileFlags = compileFlags.concat(libOptions); |
| 62 | |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 63 | const manualCompileFlags = compilationInfo.options.filter(option => (option !== inputFilepath)); |
| 64 | compileFlags = compileFlags.concat(manualCompileFlags); |
Partouf | 3a458c1 | 2020-05-22 14:14:49 +0200 | [diff] [blame] | 65 | |
GeorgeGribkov | cf9494d | 2020-06-04 16:16:04 +0300 | [diff] [blame] | 66 | compileFlags = compileFlags.filter(function (flag) { |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 67 | return flag !== ''; |
GeorgeGribkov | cf9494d | 2020-06-04 16:16:04 +0300 | [diff] [blame] | 68 | }); |
| 69 | |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 70 | // Deal with args |
| 71 | args = []; |
| 72 | |
| 73 | // Necessary arguments |
Matt Godbolt | 5c5bc4d | 2021-05-27 23:25:37 -0500 | [diff] [blame] | 74 | args.push('analyze', '--source-file', inputFilepath); |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 75 | const outputFilePath = path.join(sourceDir, 'pvs-studio-log.log'); |
Matt Godbolt | 5c5bc4d | 2021-05-27 23:25:37 -0500 | [diff] [blame] | 76 | args.push( |
| 77 | '--output-file', outputFilePath, |
| 78 | // Exclude directories from analysis |
| 79 | '-e', '/opt', |
| 80 | '-e', '/usr', |
| 81 | '--pvs-studio-path', path.dirname(this.tool.exe) + '/pvs-studio', |
| 82 | // TODO: expand this to switch() for all supported compilers: |
| 83 | // visualcpp, clang, gcc, bcc, bcc_clang64, iar, keil5, keil5_gnu |
| 84 | '--preprocessor', |
| 85 | ); |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 86 | if (compilationInfo.compiler.group.includes('clang')) |
| 87 | args.push('clang'); |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 88 | else |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 89 | args.push('gcc'); |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 90 | |
GeorgeGribkov | 91972db | 2020-05-29 12:45:41 +0300 | [diff] [blame] | 91 | // Now let's push the path to the compiler |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 92 | if (this.tool.compilerLanguage === 'c') { |
| 93 | args.push('--cc'); |
Matt Godbolt | 29125df | 2020-06-03 07:14:54 -0500 | [diff] [blame] | 94 | } else { |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 95 | args.push('--cxx'); |
GeorgeGribkov | 91972db | 2020-05-29 12:45:41 +0300 | [diff] [blame] | 96 | } |
Matt Godbolt | 5c5bc4d | 2021-05-27 23:25:37 -0500 | [diff] [blame] | 97 | args.push(compilationInfo.compiler.exe, '--cl-params'); |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 98 | args = args.concat(compileFlags); |
Partouf | 3a458c1 | 2020-05-22 14:14:49 +0200 | [diff] [blame] | 99 | |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 100 | // If you are to modify this code, |
| 101 | // don't forget that super.runTool() does args.push(inputFilepath) inside. |
Partouf | b2d1027 | 2020-05-28 12:18:25 +0200 | [diff] [blame] | 102 | const result = await super.runTool(compilationInfo, inputFilepath, args); |
Matt Godbolt | 142c824 | 2020-05-21 22:55:24 -0500 | [diff] [blame] | 103 | if (result.code !== 0) { |
| 104 | return result; |
| 105 | } |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 106 | |
| 107 | // Convert log to readable format |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 108 | const plogConverterOutputFilePath = path.join(sourceDir, 'pvs-studio-log.err'); |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 109 | |
Matt Godbolt | 142c824 | 2020-05-21 22:55:24 -0500 | [diff] [blame] | 110 | const plogConverterArgs = []; |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 111 | plogConverterArgs.push('-t', 'errorfile', '-a', 'FAIL:1,2,3;GA:1,2,3', '-o', |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 112 | plogConverterOutputFilePath, |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 113 | 'pvs-studio-log.log'); |
Partouf | 3a458c1 | 2020-05-22 14:14:49 +0200 | [diff] [blame] | 114 | |
| 115 | const plogExecOptions = this.getDefaultExecOptions(); |
Partouf | b2d1027 | 2020-05-28 12:18:25 +0200 | [diff] [blame] | 116 | plogExecOptions.customCwd = sourceDir; |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 117 | |
Matt Godbolt | 142c824 | 2020-05-21 22:55:24 -0500 | [diff] [blame] | 118 | const plogConverterResult = await exec.execute( |
Partouf | 3a458c1 | 2020-05-22 14:14:49 +0200 | [diff] [blame] | 119 | this.plogConverterPath, plogConverterArgs, plogExecOptions); |
Matt Godbolt | 142c824 | 2020-05-21 22:55:24 -0500 | [diff] [blame] | 120 | if (plogConverterResult.code !== 0) { |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 121 | logger.warn('plog-converter failed', plogConverterResult); |
Matt Godbolt | 142c824 | 2020-05-21 22:55:24 -0500 | [diff] [blame] | 122 | return this.convertResult(plogConverterResult, inputFilepath); |
| 123 | } |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 124 | let plogConverterOutput = (await fs.readFile(plogConverterOutputFilePath, 'utf8')).toString(); |
| 125 | |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 126 | // Sometimes if a code fragment can't be analyzed, PVS-Studio makes a warning for a preprocessed file |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 127 | // (*.PVS-Studio.i) |
| 128 | // This name can't be parsed by utils.parseOutput |
| 129 | // so let's just replace it with a source file name. |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 130 | plogConverterOutput = plogConverterOutput.replace(sourceDir + '/example.PVS-Studio.i', inputFilepath); |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 131 | |
| 132 | result.stdout = utils.parseOutput(plogConverterOutput, plogConverterResult.filenameTransform(inputFilepath)); |
| 133 | |
| 134 | // Now let's trim the documentation link |
Matt Godbolt | d9ebcd1 | 2020-05-21 22:26:07 -0500 | [diff] [blame] | 135 | if (result.stdout.length > 0) { |
Maxim Stefanov | c909aee | 2021-06-22 04:48:54 +0300 | [diff] [blame] | 136 | const idx = result.stdout[0].text.indexOf('The documentation for all analyzer warnings is available here:'); |
Maxim Stefanov | 3fe2a21 | 2021-06-25 16:38:14 +0300 | [diff] [blame] | 137 | result.stdout[0].text = result.stdout[0].text.substring(idx).concat('\n\n'); |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 138 | } |
| 139 | |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 140 | // The error output is unnecessary for the user |
Partouf | 101d6f1 | 2020-05-26 01:02:08 +0200 | [diff] [blame] | 141 | // If you need to debug PVS-Studio-tool, you can comment out this line |
| 142 | result.stderr = []; |
| 143 | |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 144 | return result; |
| 145 | } |
Partouf | 0438c8f | 2020-05-26 03:17:55 +0200 | [diff] [blame] | 146 | |
| 147 | getDefaultExecOptions() { |
| 148 | const execOptions = super.getDefaultExecOptions(); |
Matt Godbolt | aceaae2 | 2021-04-03 18:09:58 -0500 | [diff] [blame] | 149 | execOptions.env = { |
| 150 | ...execOptions.env, |
| 151 | PATH: process.env.PATH + ':/opt/compiler-explorer/pvs-studio-latest/bin', |
| 152 | }; |
Partouf | 0438c8f | 2020-05-26 03:17:55 +0200 | [diff] [blame] | 153 | |
| 154 | return execOptions; |
| 155 | } |
GeorgeGribkov | ae3f22c | 2020-05-18 16:57:22 +0300 | [diff] [blame] | 156 | } |