Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MATLAB code analyzer #3018

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 11 additions & 44 deletions .github/actions/setup-dependencies/action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
name: Setup Dependencies

inputs:
use_matlab:
description: "Indicates whether to install and configure MATLAB"
type: choice
required: true
default: "false"
options:
- true
- false
use_ccache:
description: "Indicates whether to install and configure ccache"
type: choice
Expand Down Expand Up @@ -100,50 +108,9 @@ runs:
#
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the MATLAB setup to a separate action to be able to use it from both CI and MATLAB Analyzer workflows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't both workflows use setup-dependencies?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was how setup-dependencies detect if MATLAB has to be installed:

 if: runner.os == 'Linux' && matrix.config == 'matlab'

There is no matrix config with the new job.

# MATLAB
#
- name: Setup MATLAB
id: setup-matlab
uses: matlab-actions/setup-matlab@v2
with:
release: "R2024a"
if: matrix.config == 'matlab'

- name: Set MATLAB_HOME and MATLAB_VERSION
run: |
# Needed for MATLAB R2024a
sudo apt install -y libgtk2.0-0
echo "MATLAB_VERSION=R2024a" >> $GITHUB_ENV
echo "MATLAB_HOME=${{ steps.setup-matlab.outputs.matlabroot }}" >> $GITHUB_ENV
shell: bash
if: runner.os == 'Linux' && matrix.config == 'matlab'

- name: Set MATLAB_HOME and MATLAB_VERSION
run: |
echo "MATLAB_VERSION=R2024a" >> $env:GITHUB_ENV
echo "MATLAB_HOME=${{ steps.setup-matlab.outputs.matlabroot }}" >> $env:GITHUB_ENV
shell: powershell
if: runner.os == 'Windows' && matrix.config == 'matlab'

# https://github.com/matlab-actions/run-command/issues/53
- name: Get run-matlab-command
run: |
wget -O /usr/local/bin/run-matlab-command https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v2/glnxa64/run-matlab-command
chmod +x /usr/local/bin/run-matlab-command
echo "MATLAB_COMMAND=/usr/local/bin/run-matlab-command" >> $GITHUB_ENV

# https://www.mathworks.com/matlabcentral/answers/1907290-how-to-manually-select-the-libstdc-library-to-use-to-resolve-a-version-glibcxx_-not-found
echo "LD_PRELOAD=/lib/x86_64-linux-gnu/libstdc++.so.6" >> $GITHUB_ENV

shell: bash
if: runner.os == 'Linux' && matrix.config == 'matlab'

# Windows is currently not working. We get an error: "'matlab' executable not found on the system path"
# However, MATLAB is installed and the path is (seemingly) set correctly
- name: Get run-matlab-command
run: |
Invoke-WebRequest https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v2/win64/run-matlab-command.exe -OutFile C:\Windows\System32\run-matlab-command.exe
echo "MATLAB_COMMAND=C:\Windows\System32\run-matlab-command.exe" >> $env:GITHUB_ENV
shell: powershell
if: runner.os == 'Windows' && matrix.config == 'matlab'
- name: Setup Dependencies
uses: ./.github/actions/setup-matlab
if: inputs.use_matlab == 'true'

#
# Cache
Expand Down
51 changes: 51 additions & 0 deletions .github/actions/setup-matlab/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Setup MATLAB
description: Setup MATLAB for use in GitHub Actions
runs:
using: "composite"
steps:
#
# MATLAB
#
- name: Setup MATLAB
id: setup-matlab
uses: matlab-actions/setup-matlab@v2
with:
release: "R2024a"

- name: Set MATLAB_HOME and MATLAB_VERSION
run: |
# Needed for MATLAB R2024a
sudo apt install -y libgtk2.0-0
echo "MATLAB_VERSION=R2024a" >> $GITHUB_ENV
echo "MATLAB_HOME=${{ steps.setup-matlab.outputs.matlabroot }}" >> $GITHUB_ENV
shell: bash
if: runner.os == 'Linux'

- name: Set MATLAB_HOME and MATLAB_VERSION
run: |
echo "MATLAB_VERSION=R2024a" >> $env:GITHUB_ENV
echo "MATLAB_HOME=${{ steps.setup-matlab.outputs.matlabroot }}" >> $env:GITHUB_ENV
shell: powershell
if: runner.os == 'Windows'

# https://github.com/matlab-actions/run-command/issues/53
- name: Get run-matlab-command
run: |
wget -O /usr/local/bin/run-matlab-command https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v2/glnxa64/run-matlab-command
chmod +x /usr/local/bin/run-matlab-command
echo "MATLAB_COMMAND=/usr/local/bin/run-matlab-command" >> $GITHUB_ENV

# https://www.mathworks.com/matlabcentral/answers/1907290-how-to-manually-select-the-libstdc-library-to-use-to-resolve-a-version-glibcxx_-not-found
echo "LD_PRELOAD=/lib/x86_64-linux-gnu/libstdc++.so.6" >> $GITHUB_ENV

shell: bash
if: runner.os == 'Linux'

# Windows is currently not working. We get an error: "'matlab' executable not found on the system path"
# However, MATLAB is installed and the path is (seemingly) set correctly
- name: Get run-matlab-command
run: |
Invoke-WebRequest https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v2/win64/run-matlab-command.exe -OutFile C:\Windows\System32\run-matlab-command.exe
echo "MATLAB_COMMAND=C:\Windows\System32\run-matlab-command.exe" >> $env:GITHUB_ENV
shell: powershell
if: runner.os == 'Windows' && matrix.config == 'matlab'
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ jobs:

- name: Setup Dependencies
uses: ./.github/actions/setup-dependencies
with:
use_matlab: ${{ matrix.config == 'matlab' }}

- name: Build ${{ matrix.config }} on ${{ matrix.os }}
uses: ./.github/actions/build
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/matlab_analyzer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Matlab Analyzer

on:
workflow_dispatch:
push:
branches: ["main"]
pull_request:
# The branches below must be a subset of the branches above
branches: ["main"]

jobs:
matlab-analyzer:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Dependencies
uses: ./.github/actions/setup-dependencies
with:
use_matlab: true

- name: Build MATLAB on Ubuntu
uses: ./.github/actions/build
timeout-minutes: 90
with:
working_directory: "matlab"
build_cpp_and_python: true

- name: MATLAB Analyzer
run: |
$MATLAB_COMMAND code_analyzer
working-directory: ./matlab/config
shell: bash

- name: Upload Analyzer Report
uses: actions/upload-artifact@v4
with:
name: matlab-analyzer-report
path: ./matlab/config/result.json
if-no-files-found: ignore
if: always()
4 changes: 2 additions & 2 deletions cpp/src/slice2matlab/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
out.inc();
writeBaseClassArrayParams(out, allMembers, false);
out.dec();
out << nl << "end;";
out << nl << "end";

out << nl << self << " = " << self << "@" << getAbsolute(base) << "(v{:});";

Expand Down Expand Up @@ -1817,7 +1817,7 @@ CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
out << nl << self << "." << q->fixedName << " = " << q->fixedName << ';';
}
out.dec();
out << nl << "end;";
out << nl << "end";
}

out.dec();
Expand Down
1 change: 1 addition & 0 deletions matlab/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ lib/x86_64-linux-gnu/
toolbox/build
toolbox/toolbox.prj
toolbox/info.xml
config/result.json
14 changes: 14 additions & 0 deletions matlab/config/analyzer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"baseConfiguration" : "factory",
"checks":
{
"PROPLC":
{
"enabled" : false
},
"PROP":
{
"enabled" : false
}
}
}
10 changes: 10 additions & 0 deletions matlab/config/code_analyzer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function code_analyzer()
result = codeIssues(["../lib/+Ice", "../lib/+IceInternal", "../lib/generated"], CodeAnalyzerConfiguration="analyzer.json");
export(result, "result.json", FileFormat="json");
disp(result);
if height(result.Issues) > 0
exit(1);
else
exit(0);
end
end
2 changes: 1 addition & 1 deletion matlab/lib/+Ice/InputStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,11 @@ function skipSlice(obj)
end
end
function r = readOptional(obj, tag, fmt)
import IceInternal.Protocol;
%assert(isobject(obj.encapsStack));
if obj.encoding_1_0
r = false; % Optional members aren't supported with the 1.0 encoding.
elseif isobject(obj.encapsStackDecoder)
import IceInternal.Protocol;
current = obj.encapsStackDecoder.current;
if ~isobject(current) || bitand(current.sliceFlags, Protocol.FLAG_HAS_OPTIONAL_MEMBERS)
r = readOptionalImpl(tag, fmt, obj.encapsStack.endPos);
Expand Down
14 changes: 7 additions & 7 deletions matlab/lib/+Ice/ObjectPrx.m
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ function iceEndWriteParams(~, os)
size = os.buf.size;
end

if length(varargin) == 1
if isscalar(varargin)
%
% Avoid the string concatenation
%
Expand Down Expand Up @@ -1056,7 +1056,7 @@ function iceEndWriteParams(~, os)
size = os.buf.size;
end
futPtr = libpointer('voidPtr'); % Output param
if length(varargin) == 1
if isscalar(varargin)
%
% Avoid the string concatenation
%
Expand Down Expand Up @@ -1116,7 +1116,7 @@ function iceThrowUserException(~, is, varargin) % Varargs are user exception typ
function r = uncheckedCast(p, varargin)
if isempty(varargin)
r = p;
elseif length(varargin) == 1
elseif isscalar(varargin)
if ~isempty(p)
r = p.ice_facet(varargin{1});
else
Expand All @@ -1133,9 +1133,9 @@ function iceThrowUserException(~, is, varargin) % Varargs are user exception typ
hasFacet = false;
facet = [];
context = {};
if length(varargin) == 1
if isscalar(varargin)
if isa(varargin{1}, 'containers.Map')
context = { varargin{1} };
context = varargin(1);
elseif isempty(varargin{1}) || isa(varargin{1}, 'char')
hasFacet = true;
facet = varargin{1};
Expand All @@ -1145,7 +1145,7 @@ function iceThrowUserException(~, is, varargin) % Varargs are user exception typ
elseif length(varargin) == 2
hasFacet = true;
facet = varargin{1};
context = { varargin{2} };
context = varargin(2);
elseif length(varargin) > 2
throw(LocalException('Ice:ArgumentException', 'too many arguments to checkedCast'));
end
Expand All @@ -1167,7 +1167,7 @@ function iceThrowUserException(~, is, varargin) % Varargs are user exception typ
function r = iceUncheckedCast(p, cls, varargin)
hasFacet = false;
facet = [];
if length(varargin) == 1
if isscalar(varargin)
hasFacet = true;
facet = varargin{1};
elseif length(varargin) > 1
Expand Down
2 changes: 1 addition & 1 deletion matlab/lib/+Ice/identityToString.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

% Copyright (c) ZeroC, Inc. All rights reserved.

if length(varargin) == 1
if isscalar(varargin)
mode = varargin{1};
elseif isempty(varargin)
mode = Ice.ToStringMode.Unicode;
Expand Down
3 changes: 1 addition & 2 deletions matlab/lib/+IceInternal/EncapsDecoder10.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function throwException(obj)
end
end

function startInstance(obj, sliceType)
function startInstance(obj, ~)
%assert(obj.sliceType == sliceType);
obj.skipFirstSlice = true;
end
Expand Down Expand Up @@ -203,7 +203,6 @@ function readInstance(obj)
%
obj.startSlice();
mostDerivedId = obj.typeId;
v = [];
while true
%
% For the 1.0 encoding, the type ID for the base Object class
Expand Down
2 changes: 1 addition & 1 deletion matlab/lib/+IceInternal/EncapsDecoder11.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function throwException(obj)
end
end

function startInstance(obj, sliceType)
function startInstance(obj, ~)
%assert(obj.current.sliceType == sliceType);
obj.current.skipFirstSlice = true;
end
Expand Down
2 changes: 1 addition & 1 deletion matlab/lib/+IceInternal/EncapsEncoder10.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function endSlice(obj)
end

function writePendingValues(obj)
while length(obj.toBeMarshaledMap) > 0
while ~isempty(obj.toBeMarshaledMap)
%
% Consider the to be marshaled instances as marshaled now,
% this is necessary to avoid adding again the "to be
Expand Down