Skip to content

Commit

Permalink
add constexpr implicit const diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisenwave committed Aug 31, 2024
1 parent bf1abc2 commit d0285de
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ <h2>C Declarations &amp; printf/scanf &#10132; Prose</h2>
<span>Function with empty parameter list takes unspecified number of parameters</span>
</div>
<div id="d-constexpr-implicit-const" class="d-info" hidden>
<span>Constexpr adds an implicit const in this case</span>
<span>constexpr adds an implicit const in this case</span>
</div>

<div id="d-format-syntax-error" class="d-error" hidden>
Expand Down
5 changes: 2 additions & 3 deletions js/prose-decl.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export class Explainer {
}
}

// TODO: add regression testing
if (histo.has('constexpr')
&& !Explainer.isExplicitlyConst(declarator, histo.has('const'))) {
this.showDiagnostic('constexpr-implicit-const');
Expand Down Expand Up @@ -223,8 +222,8 @@ export class Explainer {
/**
* Returns copy with all consecutive trailing elements that satisfy
* a given predicate removed.
* @param {Array} arr
* @param {any} predicate
* @param {Array} arr the array
* @param {any} predicate the predicate
* @returns {Array}
*/
static withoutTrailing(arr, predicate) {
Expand Down
55 changes: 53 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,69 @@ describe('Examples', function () {

code = 'signed bool';
describe(code, function () {
const codeCopy = code;
it('throws because "signed" and "bool" conflict', function () {
assert.throws(() => codeToProse(code),
assert.throws(() => codeToProse(codeCopy),
{message: 'Conflicting specifiers signed and bool'});
});
});

code = 'constexpr const int *x';
describe(code, function () {
const {paragraphs, _} = codeToProse(code);
const {paragraphs} = codeToProse(code);
it('has correct prose', function () {
const expected = ['Declare x as constexpr pointer to const int'];
assert.deepEqual(paragraphs, expected);
});
});

code = 'constexpr int x';
describe(code, function () {
const {paragraphs, diagnostics} = codeToProse(code);
it('has correct prose', function () {
const expected = ['Declare x as constexpr int'];
assert.deepEqual(paragraphs, expected);
});
it('has constexpr-implicit-const diagnostic', function() {
assert.deepEqual(diagnostics, ['constexpr-implicit-const']);
});
});

code = 'constexpr const int x';
describe(code, function () {
const {paragraphs, diagnostics} = codeToProse(code);
it('has correct prose', function () {
const expected = ['Declare x as constexpr const int'];
assert.deepEqual(paragraphs, expected);
});
it('has no diagnostics', function() {
assert.equal(diagnostics.length, 0);
});
});

code = 'constexpr const int * const x';
describe(code, function () {
const {paragraphs, diagnostics} = codeToProse(code);
it('has correct prose', function () {
const expected = ['Declare x as constexpr const ' +
'pointer to const int'];
assert.deepEqual(paragraphs, expected);
});
it('has no diagnostics', function() {
assert.equal(diagnostics.length, 0);
});
});

code = 'constexpr const int * x[][]';
describe(code, function () {
const {paragraphs, diagnostics} = codeToProse(code);
it('has correct prose', function () {
const expected = ['Declare x as constexpr array of arrays ' +
'of pointers to const int'];
assert.deepEqual(paragraphs, expected);
});
it('has constexpr-implicit-const diagnostic', function() {
assert.deepEqual(diagnostics, ['constexpr-implicit-const']);
});
});
});

0 comments on commit d0285de

Please sign in to comment.