integrated-application-development / sonar-delphi

Delphi language plugin for SonarQube
GNU Lesser General Public License v3.0
104 stars 16 forks source link

Expression lexer failing to compile file in recursive directives #260

Closed EduardoVaz06 closed 4 months ago

EduardoVaz06 commented 4 months ago

Prerequisites

SonarDelphi version

1.6.0

SonarQube version

No response

Issue description

An exception is raised in some delphi files while analyzing it, au.integradev.delphi.preprocessor.directive.CompilerDirectiveParserImpl$CompilerDirectiveParserError: UNexpected character: '{'

(CompilerDirectiveParserImpl.java) EXPRESSION_LEXER seems to not expect another directive and fails to compile the file.

image image

Full exception on file: exception-log.txt

Steps to reproduce

Minimal Delphi code exhibiting the issue

{$IF LicenseVersion <> {$I __version.txt}}
  {$Message Fatal 'Version different than the expected'}
{$ENDIF}
Cirras commented 4 months ago

Oh wow, I've literally never seen this kind of pattern before. Good fun, we'll need to make some changes to the compiler directive parser to support this kind of nesting.

denieu commented 4 months ago

Explaining the use case a little better.

In the organization where I work we often use Git submodules as dependencies of our Delphi projects.

To add an extra layer of security, we add a version.txt file in the submodule storing the dependency version (git tag name). In software that uses this submodule, we maintain a constant with the expected version and at compile time we check if the file version.txt and the constant have the same value, if the value is different the software will not compile, forcing the developer to check why this happened.

Developers often updated the version of submodules by mistake, this strategy prevents this from being done unintentionally.

The strategy I mentioned is done through the code example brought by @EduardoVaz06.