SonarDelphi icon indicating copy to clipboard operation
SonarDelphi copied to clipboard

Receiving plenty of errors while parsing code

Open hkoestin opened this issue 3 years ago • 7 comments

I am using SonarQube 9.1 with the latest plugin version 1.0.7

When running the Code Analysis from Azure DevOps, I receive lots of errors on the console (see below).

Is there a way to get around these issues?

Kind regards, Harald

Output (extract):

##[error]$SOME-PATH$\uFunctions.IPC.pas line 47:2 mismatched input 'TIPCAsyncDataServer' expecting IMPLEMENTATION
$SOME-PATH$\uFunctions.IPC.pas line 47:2 mismatched input 'TIPCAsyncDataServer' expecting IMPLEMENTATION
##[error]$SOME-PATH$\uFunctions.IPC.pas line 78:5 mismatched input ';' expecting DOT
$SOME-PATH$\uFunctions.IPC.pas line 78:5 mismatched input ';' expecting DOT
##[error]ERROR: Error while parsing $SOME-PATH$\uFunctions.IPC.pas

INFO: 20% done...
INFO: 30% done...
INFO: 40% done...
##[error]$SOME-PATH$\Web.Win.Sockets.pas line 857:0 no viable alternative at input 'begin'
$SOME-PATH$\Web.Win.Sockets.pas line 857:0 no viable alternative at input 'begin'
##[error]$SOME-PATH$\Web.Win.Sockets.pas line 858:2 no viable alternative at input 'Result'

##[error]ERROR: Error while parsing $SOME-PATH$\Web.Win.Sockets.pas
ERROR: Error while parsing $SOME-PATH$\Web.Win.Sockets.pas
INFO: 50% done...
INFO: 60% done...
##[error]$SOME-PATH$\uConnection.Provider.pas line 196:2 no viable alternative at input 'TConnectionProviderSingleton'
$SOME-PATH$\uConnection.Provider.pas line 196:2 no viable alternative at input 'TConnectionProviderSingleton'
##[error]ERROR: Error while parsing $SOME-PATH$\uConnection.Provider.pas
ERROR: Error while parsing $SOME-PATH$\uConnection.Provider.pas
INFO: 70% done...
INFO: 80% done...
INFO: 90% done...
INFO: Done
INFO: Processing metrics...
INFO: 0% done...
##[error]$SOME-PATH$/uMessagesConst.pas line 4:20 no viable alternative at character '\'

hkoestin avatar Oct 22 '21 07:10 hkoestin

Can you supply a sample file? Ideally a small one.

And does the unit contain conditional code like {$ifdef}? If so, do the parsing errors disappear once you remove the conditional code?

joachimmarder avatar Oct 22 '21 12:10 joachimmarder

Below you would find some code to reproduce the issue.

There are no conditionals in that file, however, there are "old-style" comments in the file.

{*******************************************************************************
* Class validates the XML against the XSD. You should ensure that CoInitialize
* is called in your app before you access the methods below.
* @path \trunc\00_CommonGlobal\02_Functions\uXMLValidator.pas
* @author XYZ
* @author Copyright © 2021 by ...
* @date 27/03/13 19:12:04
* @remarks
*******************************************************************************}
unit uXMLValidator;

interface

uses
  System.Classes;

type
  XMLValidator = class
  public
    class function IsValidXML(AXML: String; ASchemaDir: String): Boolean; overload; static;
    class function IsValidXML(AXMLFile: String): Boolean; overload; static;
    class function IsValidXML(AStream: TStream; ASchemaDir: String): Boolean; overload; static;
  end;

implementation

uses
  System.SysUtils, Xml.xmldom, Xml.Win.msxmldom,  uFunctions.Files;

function IsValidXMLInternal(AStream: TStream; AText: String; ASchemaDir: String): Boolean;
var
  doc: IDOMPersist;
  ParseOpt: IDOMParseOptions;
  OldDir: String;
begin
  if ASchemaDir <> '' then
    begin
      OldDir := GetCurrentDir;
      ChDir(ASchemaDir);
    end
  else
    OldDir := '';

  Result := False;
  try
    doc := MSXML_DOM.DOMImplementation.createDocument('', '', nil) as IDOMPersist;
    if doc.QueryInterface(IDOMParseOptions, ParseOpt) = 0 then
      begin
        ParseOpt.async := False;
        ParseOpt.resolveExternals := True;
        ParseOpt.validate := True;
        if AStream = nil then
          Result := doc.loadxml(AText)
        else
          Result := doc.loadFromStream(AStream);
      end
  finally
    if ASchemaDir <> '' then
      ChDir(OldDir);
  end;
end;

class function XMLValidator.IsValidXML(AXML: String; ASchemaDir: String): Boolean;
begin
  Result := IsValidXMLInternal(nil, AXML, ASchemaDir)
end;

class function XMLValidator.IsValidXML(AStream: TStream; ASchemaDir: String): Boolean;
begin
  Result := IsValidXMLInternal(AStream, '', ASchemaDir)
end;

class function XMLValidator.IsValidXML(AXMLFile: String): Boolean;
var
  s: String;
begin
  FileUtils.FileToString(s, AXMLFile);
  Result := IsValidXMLInternal(nil, s, ExtractFilePath(AXMLFile));
end;

end.

hkoestin avatar Oct 25 '21 09:10 hkoestin

Test.zip I loaded the file in AntlWorks and it was parsed successfully.

There are no conditionals in that file, however, there are "old-style" comments in the file.

I don't think they are a problem, but does the problem disappear if you remove those comments?

joachimmarder avatar Oct 26 '21 07:10 joachimmarder

Thank you for your answer.

Then maybe it is about the integration of the plugin with Azure DevOps and the SonarScanner used to perform the analysis.

Which part is actually generating the error-messages on the DevOps console log?

hkoestin avatar Oct 26 '21 13:10 hkoestin

Which part is actually generating the error-messages on the DevOps console log?

I'm not sure, I forked the project to fix some issues in the Antlr grammar definiton of Obejct Pascal. And I don't use Azure DevOps.

What is the exact error message that is generated for this sample file on your system?

joachimmarder avatar Oct 27 '21 19:10 joachimmarder

I did now update our Azure DevOps Server to 2020.1 and also updated SonarQube to 9.1 with a new SonarScanner version for DevOps - now the task "Run Code Analysis" is in version 5.0.0.

Analysis goes through smooth, however still showing the same issues in the output.

10:28:46.540 DEBUG: >> PROCESSING IF.Functions/uXMLValidator.pas
10:28:46.556 DEBUG: 'IF.Functions/uXMLValidator.pas' generated metadata with charset 'UTF-8'
##[error]$SOME_PATH$/IF.Functions/uXMLValidator.pas line 4:8 no viable alternative at character '\'
$SOME_PATH$/IF.Functions/uXMLValidator.pas line 4:8 no viable alternative at character '\'
##[error]$SOME_PATH$/IF.Functions/uXMLValidator.pas line 4:14 no viable alternative at character '\'
$SOME_PATH$/IF.Functions/uXMLValidator.pas line 4:30 no viable alternative at character '\'
$SOME_PATH$/IF.Functions/uXMLValidator.pas line 4:43 no viable alternative at character '\'
$SOME_PATH$/IF.Functions/uXMLValidator.pas line 4:14 no viable alternative at character '\'
$SOME_PATH$/IF.Functions/uXMLValidator.pas line 4:30 no viable alternative at character '\'
$SOME_PATH$/IF.Functions/uXMLValidator.pas line 4:43 no viable alternative at character '\'

Not sure if it is about the grammar or some other issues are triggering these messages. It basically still is fine, we just get a lot of false-positives on the analysis, which kinda sucks.

hkoestin avatar Oct 29 '21 10:10 hkoestin

$SOME_PATH$/IF.Functions/uXMLValidator.pas line 4:8 no viable alternative at character '\'

Well, it seems that some part of SonarQube has issues with the backslashes in the comments on top of the file. Just to be sure: Do the errors disappear if you remove the line with backslashes?

we just get a lot of false-positives on the analysis

Do you mean, SonarDelphi rules apply when they shouldn't? Do you have an example?

joachimmarder avatar Oct 30 '21 20:10 joachimmarder