From 2916206cab0cca4ae02471b1b77e70cb8abd9455 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 18 Dec 2020 14:28:18 +0000 Subject: [PATCH] Match more SPDX-License-Identifer lines --- Changelog.md | 3 +- libsolidity/parsing/Parser.cpp | 29 +++++++++++++++---- .../syntaxTests/license/license_double.sol | 2 +- .../syntaxTests/license/license_missing.sol | 1 + 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Changelog.md b/Changelog.md index 0cb887768..e3f48da71 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,7 @@ Language Features: Compiler Features: + * Parser: Extend parsing rule for ``SPDX-License-Identifier``. * SMTChecker: Function definitions can be annotated with the custom Natspec tag ``custom:smtchecker abstract-function-nondet`` to be abstracted by a nondeterministic value when called. * Standard JSON / combined JSON: New artifact "functionDebugData" that contains bytecode offsets of entry points of functions and potentially more information in the future. * Yul Optimizer: Evaluate ``keccak256(a, c)``, when the value at memory location ``a`` is known at compile time and ``c`` is a constant ``<= 32``. @@ -47,7 +48,6 @@ Compiler Features: * Yul EVM Code Transform: Stack Optimization: Reuse slots of unused function arguments and defer allocating stack slots for return variables until after expression statements and assignments that do not reference them. * Yul Optimizer: Added a new step FunctionSpecializer, that specializes a function with its literal arguments. - Bugfixes: * Antlr Grammar: Fix parsing of import paths involving properly distinguishing between empty and non-empty string literals in general. * AST Output: Fix ``kind`` field of ``ModifierInvocation`` for base constructor calls. @@ -100,7 +100,6 @@ Compiler Features: * Optimizer: Simple inlining when jumping to small blocks that jump again after a few side-effect free opcodes. * NatSpec: Allow and export all tags that start with ``@custom:``. - Bugfixes: * AST: Added ``referencedDeclaration`` for enum members. * Code Generator: Fix internal error when functions are passed as parameters of other callables, when the function types can be implicitly converted, but not identical. diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 560d374c9..e546b0d28 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -2052,7 +2052,10 @@ bool Parser::variableDeclarationStart() optional Parser::findLicenseString(std::vector> const& _nodes) { // We circumvent the scanner here, because it skips non-docstring comments. - static regex const licenseRegex("SPDX-License-Identifier:\\s*([a-zA-Z0-9 ()+.-]+)"); + // This matches the entire line starting with the SPDX-License-Identifier. + static regex const licenseLineRegex("SPDX-License-Identifier:\\s*([^\n])\\s*(\\*/)?"); + // This is the actual allowed format for the license tag. + static regex const licenseRegex("^([a-zA-Z0-9 ()+.-]*)$"); // Search inside all parts of the source not covered by parsed nodes. // This will leave e.g. "global comments". @@ -2070,12 +2073,26 @@ optional Parser::findLicenseString(std::vector> cons vector matches; for (auto const& [start, end]: sequencesToSearch) { - smatch match; - if (regex_search(start, end, match, licenseRegex)) + smatch lineMatch; + if (regex_search(start, end, lineMatch, licenseLineRegex)) { - string license{boost::trim_copy(string(match[1]))}; - if (!license.empty()) - matches.emplace_back(std::move(license)); + string license{boost::trim_copy(string(lineMatch[1]))}; + smatch licenseMatch; + if (regex_search(license, licenseMatch, licenseRegex)) + { + license = string(licenseMatch[1]); + if (!license.empty()) + matches.emplace_back(std::move(license)); + } + else + { + parserError( + 5406_error, + {-1, -1, m_scanner->charStream()}, + "SPDX license identifier is ill-formatted. " + "Please see https://spdx.org for more information." + ); + } } } diff --git a/test/libsolidity/syntaxTests/license/license_double.sol b/test/libsolidity/syntaxTests/license/license_double.sol index 0885ad150..9a652e1e2 100644 --- a/test/libsolidity/syntaxTests/license/license_double.sol +++ b/test/libsolidity/syntaxTests/license/license_double.sol @@ -2,4 +2,4 @@ contract C {} // SPDX-License-Identifier: MIT // ---- -// ParserError 3716: Multiple SPDX license identifiers found in source file. Use "AND" or "OR" to combine multiple licenses. Please see https://spdx.org for more information. +// Warning 1878: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: " to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information. diff --git a/test/libsolidity/syntaxTests/license/license_missing.sol b/test/libsolidity/syntaxTests/license/license_missing.sol index e919dc6e5..780c86b6d 100644 --- a/test/libsolidity/syntaxTests/license/license_missing.sol +++ b/test/libsolidity/syntaxTests/license/license_missing.sol @@ -1,3 +1,4 @@ // This test is actually useless, as the test suite adds the automatic preamble. contract C {} // ---- +// Warning 1878: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: " to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.