blob: 4c541d57b0c7a01952260507a8680a92ea3bafbd [file] [log] [blame] [raw]
Matt Godboltf68198a2020-09-26 17:50:40 -05001// Copyright (c) 2017, Compiler Explorer Authors
Matt Godbolt6e7e30b2017-12-19 10:15:17 -06002// 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.
24
Austin Morton044dcfb2020-09-26 16:59:26 -040025import { CompilerArguments } from '../../lib/compiler-arguments';
26import { BaseParser, ClangParser, GCCParser, PascalParser } from '../../lib/compilers/argument-parsers';
27import { FakeCompiler } from '../../lib/compilers/fake-for-test';
28import { makeCompilationEnvironment, should } from '../utils';
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060029
Rubén1a339022018-06-19 10:09:44 +020030const languages = {
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020031 'c++': {id: 'c++'},
Rubén1a339022018-06-19 10:09:44 +020032};
33
Austin Morton1b7fe242020-02-18 00:03:15 -050034let env;
Rubén1a339022018-06-19 10:09:44 +020035
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060036function makeCompiler(stdout, stderr, code) {
Austin Morton1b7fe242020-02-18 00:03:15 -050037 if (env === undefined) {
Austin Mortoncadbdf52020-03-07 01:50:49 -050038 env = makeCompilationEnvironment({languages});
Austin Morton1b7fe242020-02-18 00:03:15 -050039 }
40
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060041 if (code === undefined) code = 0;
Rubén1a339022018-06-19 10:09:44 +020042 const compiler = new FakeCompiler({lang: languages['c++'].id, remote: true}, env);
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020043 compiler.exec = () => Promise.resolve({code: code, stdout: stdout || '', stderr: stderr || ''});
Austin Mortonbf235502019-05-29 04:13:44 -040044 compiler.execCompilerCached = compiler.exec;
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020045 compiler.possibleArguments = new CompilerArguments('g82');
partoufe1202902018-01-03 23:19:25 +010046 return compiler;
partoufae1780f2018-01-03 22:47:45 +010047}
48
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060049describe('option parser', () => {
RabsRincon968ce502018-04-07 17:51:51 +020050 it('should do nothing for the base parser', () => {
51 const compiler = makeCompiler();
Austin Morton044dcfb2020-09-26 16:59:26 -040052 return BaseParser.parse(compiler).should.deep.equals(compiler);
RabsRincon968ce502018-04-07 17:51:51 +020053 });
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060054 it('should handle empty options', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -040055 return BaseParser.getOptions(makeCompiler()).should.eventually.deep.equals({});
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060056 });
57 it('should parse single-dash options', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -040058 return BaseParser.getOptions(makeCompiler('-foo\n')).should.eventually.deep.equals({
Matt Godbolt03a3fb32020-01-14 22:37:58 -060059 '-foo': {
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020060 description: '',
61 timesused: 0,
62 },
Matt Godbolt03a3fb32020-01-14 22:37:58 -060063 });
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060064 });
65 it('should parse double-dash options', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -040066 return BaseParser.getOptions(makeCompiler('--foo\n')).should.eventually.deep.equals({
Matt Godbolt03a3fb32020-01-14 22:37:58 -060067 '--foo': {
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020068 description: '',
69 timesused: 0,
70 },
Matt Godbolt03a3fb32020-01-14 22:37:58 -060071 });
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060072 });
73 it('should parse stderr options', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -040074 return BaseParser.getOptions(makeCompiler('', '--bar=monkey\n')).should.eventually.deep.equals({
Matt Godbolt03a3fb32020-01-14 22:37:58 -060075 '--bar=monkey': {
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020076 description: '',
77 timesused: 0,
78 },
Matt Godbolt03a3fb32020-01-14 22:37:58 -060079 });
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060080 });
81 it('handles non-option text', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -040082 return BaseParser.getOptions(makeCompiler('-foo=123\nthis is a fish\n-badger=123')).should.eventually.deep.equals(
Matt Godbolt03a3fb32020-01-14 22:37:58 -060083 {
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020084 '-foo=123': {description: 'this is a fish', timesused: 0},
85 '-badger=123': {description: '', timesused: 0},
Matt Godbolt03a3fb32020-01-14 22:37:58 -060086 });
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060087 });
88 it('should ignore if errors occur', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -040089 return BaseParser.getOptions(makeCompiler('--foo\n', '--bar\n', 1)).should.eventually.deep.equals({});
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060090 });
91});
92
93describe('gcc parser', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -040094 it('should handle empty options', async () => {
95 const result = await GCCParser.parse(makeCompiler());
96 should.not.exist(result.compiler.supportsGccDump);
97 result.compiler.options.should.equals('');
Matt Godbolt6e7e30b2017-12-19 10:15:17 -060098 });
99 it('should handle options', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -0400100 return GCCParser.parse(makeCompiler('-masm=intel\n-fdiagnostics-color=[blah]\n-fdump-tree-all'))
Matt Godbolt6e7e30b2017-12-19 10:15:17 -0600101 .should.eventually.satisfy(result => {
partoufe1202902018-01-03 23:19:25 +0100102 return Promise.all([
103 result.compiler.supportsGccDump.should.equals(true),
104 result.compiler.supportsIntel.should.equals(true),
105 result.compiler.intelAsm.should.equals('-masm=intel'),
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200106 result.compiler.options.should.equals('-fdiagnostics-color=always'),
partoufe1202902018-01-03 23:19:25 +0100107 ]);
Matt Godbolt6e7e30b2017-12-19 10:15:17 -0600108 });
109 });
partoufae1780f2018-01-03 22:47:45 +0100110 it('should handle undefined options', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -0400111 return GCCParser.parse(makeCompiler('-fdiagnostics-color=[blah]')).should.eventually.satisfy(result => {
partoufe1202902018-01-03 23:19:25 +0100112 return Promise.all([
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200113 result.compiler.options.should.equals('-fdiagnostics-color=always'),
partoufe1202902018-01-03 23:19:25 +0100114 ]);
partoufae1780f2018-01-03 22:47:45 +0100115 });
116 });
Matt Godbolt6e7e30b2017-12-19 10:15:17 -0600117});
118
119describe('clang parser', () => {
120 it('should handle empty options', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -0400121 return ClangParser.parse(makeCompiler()).should.eventually.satisfy(result => {
partoufe1202902018-01-03 23:19:25 +0100122 return Promise.all([
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200123 result.compiler.options.should.equals(''),
partoufe1202902018-01-03 23:19:25 +0100124 ]);
Matt Godbolt6e7e30b2017-12-19 10:15:17 -0600125 });
126 });
127 it('should handle options', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -0400128 return ClangParser.parse(makeCompiler('-fno-crash-diagnostics\n-fsave-optimization-record\n-fcolor-diagnostics'))
Matt Godbolt6e7e30b2017-12-19 10:15:17 -0600129 .should.eventually.satisfy(result => {
partoufe1202902018-01-03 23:19:25 +0100130 return Promise.all([
131 result.compiler.supportsOptOutput.should.equals(true),
132 result.compiler.optArg.should.equals('-fsave-optimization-record'),
asyntsec211702019-02-19 21:28:33 +0100133
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200134 result.compiler.options.should.include('-fcolor-diagnostics'),
135 result.compiler.options.should.include('-fno-crash-diagnostics'),
136 result.compiler.options.should.not.include('-fsave-optimization-record'),
asyntsec211702019-02-19 21:28:33 +0100137 ]);
138 });
139 });
Matt Godbolt6e7e30b2017-12-19 10:15:17 -0600140});
Partouf064ded52019-01-12 21:42:00 +0100141
Matt Godbolt03a3fb32020-01-14 22:37:58 -0600142describe('pascal parser', () => {
143 it('should handle empty options', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -0400144 return PascalParser.parse(makeCompiler()).should.eventually.satisfy(result => {
Matt Godbolt03a3fb32020-01-14 22:37:58 -0600145 return Promise.all([
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200146 result.compiler.options.should.equals(''),
Matt Godbolt03a3fb32020-01-14 22:37:58 -0600147 ]);
148 });
149 });
150});
151
Partouf064ded52019-01-12 21:42:00 +0100152describe('popular compiler arguments', () => {
Austin Morton1b7fe242020-02-18 00:03:15 -0500153 let compiler;
154
155 before(() => {
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200156 compiler = makeCompiler('-fsave-optimization-record\n-x\n-g\n-fcolor-diagnostics\n-O<number> optimization level\n-std=<c++11,c++14,c++17z>');
Austin Morton1b7fe242020-02-18 00:03:15 -0500157 });
Partouf064ded52019-01-12 21:42:00 +0100158
159 it('should return 5 arguments', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -0400160 return ClangParser.parse(compiler).then(compiler => {
Partouf064ded52019-01-12 21:42:00 +0100161 return compiler.should.satisfy(compiler => {
162 return Promise.all([
163 compiler.possibleArguments.getPopularArguments().should.deep.equal({
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200164 '-O<number>': {description: 'optimization level', timesused: 0},
165 '-fcolor-diagnostics': {description: '', timesused: 0},
166 '-fsave-optimization-record': {description: '', timesused: 0},
167 '-g': {description: '', timesused: 0},
168 '-x': {description: '', timesused: 0},
169 }),
Partouf064ded52019-01-12 21:42:00 +0100170 ]);
171 });
Matt Godbolt03a3fb32020-01-14 22:37:58 -0600172 });
Partouf064ded52019-01-12 21:42:00 +0100173 });
174
175 it('should return arguments except the ones excluded', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -0400176 return ClangParser.parse(compiler).then(compiler => {
Partouf064ded52019-01-12 21:42:00 +0100177 return compiler.should.satisfy(compiler => {
178 return Promise.all([
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200179 compiler.possibleArguments.getPopularArguments(['-O3', '--hello']).should.deep.equal({
180 '-fcolor-diagnostics': {description: '', timesused: 0},
181 '-fsave-optimization-record': {description: '', timesused: 0},
182 '-g': {description: '', timesused: 0},
183 '-x': {description: '', timesused: 0},
184 '-std=<c++11,c++14,c++17z>': {description: '', timesused: 0},
185 }),
Partouf064ded52019-01-12 21:42:00 +0100186 ]);
187 });
Matt Godbolt03a3fb32020-01-14 22:37:58 -0600188 });
Partouf064ded52019-01-12 21:42:00 +0100189 });
Partouf16bae632019-01-12 22:10:00 +0100190
191 it('should be able to exclude special params with assignments', () => {
Austin Morton044dcfb2020-09-26 16:59:26 -0400192 return ClangParser.parse(compiler).then(compiler => {
Partouf16bae632019-01-12 22:10:00 +0100193 return compiler.should.satisfy(compiler => {
194 return Promise.all([
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +0200195 compiler.possibleArguments.getPopularArguments(['-std=c++14', '-g', '--hello']).should.deep.equal({
196 '-O<number>': {description: 'optimization level', timesused: 0},
197 '-fcolor-diagnostics': {description: '', timesused: 0},
198 '-fsave-optimization-record': {description: '', timesused: 0},
199 '-x': {description: '', timesused: 0},
200 }),
Partouf16bae632019-01-12 22:10:00 +0100201 ]);
202 });
Matt Godbolt03a3fb32020-01-14 22:37:58 -0600203 });
Partouf16bae632019-01-12 22:10:00 +0100204 });
Partouf064ded52019-01-12 21:42:00 +0100205});