Skip to content

Commit 4c01988

Browse files
authored
Add Slice to Csharp --enable-analysis (#4617)
1 parent b132f44 commit 4c01988

File tree

10 files changed

+57
-25
lines changed

10 files changed

+57
-25
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ jobs:
5858
build_flags: "OPTIMIZE=no"
5959
test_flags: "--swift-config=debug --csharp-config=Debug"
6060
python_tests: true
61+
enable_csharp_analysis: true
6162
- os: ubuntu-24.04
6263
config: "debug"
6364
build_flags: "OPTIMIZE=no"
6465
test_flags: "--csharp-config=Debug"
6566
python_tests: true
67+
enable_csharp_analysis: true
6668

6769
# TODO - figure out how to properly install debug Python
6870
- os: windows-2022
@@ -72,6 +74,7 @@ jobs:
7274
test_flags: "--platform=x64 --config=Debug"
7375
msbuild_project: "msbuild/ice.proj"
7476
python_tests: true
77+
enable_csharp_analysis: true
7578

7679
# iOS
7780
- os: macos-26
@@ -171,6 +174,12 @@ jobs:
171174
shell: powershell
172175
if: runner.os == 'Windows'
173176

177+
- name: Enable C# Analysis
178+
if: matrix.enable_csharp_analysis == true
179+
run: |
180+
echo "EnableAnalysis=true" >> $env:GITHUB_ENV
181+
shell: pwsh
182+
174183
- name: Build ${{ matrix.config }} on ${{ matrix.os }}
175184
uses: ./.github/actions/build
176185
timeout-minutes: 90

cpp/src/slice2cs/CsUtil.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,8 @@ Slice::Csharp::writeOptionalMarshalUnmarshalCode(
813813
}
814814
else
815815
{
816-
out << nl << stream << ".writeSize(" << param << ".Count * "
817-
<< (keyType->minWireSize() + valueType->minWireSize()) << " + (" << param << ".Count > 254 ? 5 : 1));";
816+
out << nl << stream << ".writeSize((" << param << ".Count * "
817+
<< (keyType->minWireSize() + valueType->minWireSize()) << ") + (" << param << ".Count > 254 ? 5 : 1));";
818818
}
819819
writeMarshalUnmarshalCode(out, type, scope, param, marshal, customStream);
820820
if (keyType->isVariableLength() || valueType->isVariableLength())
@@ -1611,7 +1611,7 @@ Slice::Csharp::writeOptionalSequenceMarshalUnmarshalCode(
16111611
}
16121612
else if (st->minWireSize() > 1)
16131613
{
1614-
out << nl << stream << ".writeSize(" << length << " * " << st->minWireSize() << " + (" << length
1614+
out << nl << stream << ".writeSize((" << length << " * " << st->minWireSize() << ") + (" << length
16151615
<< " > 254 ? 5 : 1));";
16161616
}
16171617
writeSequenceMarshalUnmarshalCode(out, seq, scope, param, marshal, true, stream);

cpp/src/slice2cs/Gen.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,9 @@ Slice::CsVisitor::modulePrefixEnd(const ModulePtr& p)
758758
}
759759
}
760760

761-
Slice::Gen::Gen(const string& base, const vector<string>& includePaths, const string& dir) : _includePaths(includePaths)
761+
Slice::Gen::Gen(const string& base, const vector<string>& includePaths, const string& dir, bool enableAnalysis)
762+
: _includePaths(includePaths),
763+
_enableAnalysis(enableAnalysis)
762764
{
763765
string fileBase = base;
764766
string::size_type pos = base.find_last_of("/\\");
@@ -781,7 +783,11 @@ Slice::Gen::Gen(const string& base, const vector<string>& includePaths, const st
781783
}
782784
FileTracker::instance()->addFile(file);
783785
printHeader();
784-
printGeneratedHeader(_out, fileBase + ".ice");
786+
787+
if (!_enableAnalysis)
788+
{
789+
printGeneratedHeader(_out, fileBase + ".ice");
790+
}
785791

786792
_out << sp;
787793
_out << nl << "#nullable enable";
@@ -790,20 +796,21 @@ Slice::Gen::Gen(const string& base, const vector<string>& includePaths, const st
790796

791797
_out << sp;
792798

793-
/*
794-
// Disable some warnings when auto-generated is removed from the header. See printGeneratedHeader above.
795-
_out << nl << "#pragma warning disable SA1403 // File may only contain a single namespace";
796-
_out << nl << "#pragma warning disable SA1611 // The documentation for parameter x is missing";
799+
if (_enableAnalysis)
800+
{
801+
// Disable some warnings when auto-generated is removed from the header. See printGeneratedHeader above.
802+
_out << nl << "#pragma warning disable SA1403 // File may only contain a single namespace";
803+
_out << nl << "#pragma warning disable SA1611 // The documentation for parameter x is missing";
797804

798-
_out << nl << "#pragma warning disable CA1041 // Provide a message for the ObsoleteAttribute that marks ...";
799-
_out << nl << "#pragma warning disable CA1068 // Cancellation token as last parameter";
800-
_out << nl << "#pragma warning disable CA1725 // Change parameter name istr_ to istr in order to match ...";
805+
_out << nl << "#pragma warning disable CA1041 // Provide a message for the ObsoleteAttribute that marks ...";
806+
_out << nl << "#pragma warning disable CA1068 // Cancellation token as last parameter";
807+
_out << nl << "#pragma warning disable CA1725 // Change parameter name istr_ to istr in order to match ...";
801808

802-
// Missing doc - only necessary for the tests.
803-
_out << nl << "#pragma warning disable SA1602";
804-
_out << nl << "#pragma warning disable SA1604";
805-
_out << nl << "#pragma warning disable SA1605";
806-
*/
809+
// Missing doc - only necessary for the tests.
810+
_out << nl << "#pragma warning disable SA1602";
811+
_out << nl << "#pragma warning disable SA1604";
812+
_out << nl << "#pragma warning disable SA1605";
813+
}
807814

808815
_out << nl << "#pragma warning disable CS1591 // Missing XML Comment";
809816
_out << nl << "#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment";
@@ -1112,7 +1119,7 @@ Slice::Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p)
11121119
_out << nl << "public static " << name << " read(Ice.InputStream istr)";
11131120
_out << sb;
11141121
_out << nl << "int sz = istr.readSize();";
1115-
_out << nl << name << " r = new " << name << "();";
1122+
_out << nl << "var r = new " << name << "();";
11161123
_out << nl << "for (int i = 0; i < sz; ++i)";
11171124
_out << sb;
11181125
_out << nl << keyS << " k;";
@@ -1581,7 +1588,7 @@ Slice::Gen::TypesVisitor::visitDataMember(const DataMemberPtr& p)
15811588
BuiltinPtr builtin = dynamic_pointer_cast<Builtin>(p->type());
15821589

15831590
// Don't explicitly initialize to default value.
1584-
if (!builtin || builtin->kind() == Builtin::KindString || defaultValue != "0")
1591+
if (!builtin || builtin->kind() == Builtin::KindString || (defaultValue != "0" && defaultValue != "false"))
15851592
{
15861593
_out << " = ";
15871594
writeConstantValue(p->type(), p->defaultValueType(), defaultValue);

cpp/src/slice2cs/Gen.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace Slice
7575
class Gen final
7676
{
7777
public:
78-
Gen(const std::string&, const std::vector<std::string>&, const std::string&);
78+
Gen(const std::string&, const std::vector<std::string>&, const std::string&, bool);
7979
Gen(const Gen&) = delete;
8080
~Gen();
8181

@@ -84,6 +84,7 @@ namespace Slice
8484
private:
8585
IceInternal::Output _out;
8686
std::vector<std::string> _includePaths;
87+
bool _enableAnalysis;
8788

8889
void printHeader();
8990

cpp/src/slice2cs/Main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ usage(const string& n)
105105
"--depend Generate Makefile dependencies.\n"
106106
"--depend-xml Generate dependencies in XML format.\n"
107107
"--depend-file FILE Write dependencies to FILE instead of standard output.\n"
108+
"--enable-analysis Enable static analysis for generated code by not including\n"
109+
" <auto-generated> tags which suppresses analyzer warnings.\n"
108110
"--validate Validate command line options.\n";
109111
}
110112

@@ -122,6 +124,7 @@ compile(const vector<string>& argv)
122124
opts.addOpt("", "depend");
123125
opts.addOpt("", "depend-xml");
124126
opts.addOpt("", "depend-file", IceInternal::Options::NeedArg, "");
127+
opts.addOpt("", "enable-analysis");
125128
opts.addOpt("d", "debug");
126129

127130
bool validate = find(argv.begin(), argv.end(), "--validate") != argv.end();
@@ -180,6 +183,8 @@ compile(const vector<string>& argv)
180183

181184
string dependFile = opts.optArg("depend-file");
182185

186+
bool enableAnalysis = opts.isSet("enable-analysis");
187+
183188
bool debug = opts.isSet("debug");
184189

185190
if (sliceFiles.empty())
@@ -251,7 +256,7 @@ compile(const vector<string>& argv)
251256
CsharpDocCommentFormatter formatter;
252257
parseAllDocComments(unit, formatter);
253258

254-
Gen gen(preprocessor->getBaseName(), includePaths, output);
259+
Gen gen(preprocessor->getBaseName(), includePaths, output, enableAnalysis);
255260
gen.generate(unit);
256261

257262
status |= unit->getStatus();

csharp/src/Directory.Build.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@
1717
<GlobalAnalyzerConfigFiles Include="$(MSBuildThisFileDirectory)../msbuild/CodeAnalysis.Base.globalconfig" />
1818
<GlobalAnalyzerConfigFiles Include="$(MSBuildThisFileDirectory)../msbuild/CodeAnalysis.Src.globalconfig" />
1919
</ItemGroup>
20+
<ItemDefinitionGroup>
21+
<SliceCompile Condition="'$(EnableAnalysis)' == 'true'">
22+
<AdditionalOptions>--enable-analysis</AdditionalOptions>
23+
</SliceCompile>
24+
</ItemDefinitionGroup>
2025
</Project>

csharp/test/Directory.Build.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>
2020
</ItemGroup>
21+
<ItemDefinitionGroup>
22+
<SliceCompile Condition="'$(EnableAnalysis)' == 'true'">
23+
<AdditionalOptions>--enable-analysis</AdditionalOptions>
24+
</SliceCompile>
25+
</ItemDefinitionGroup>
2126
</Project>

csharp/test/Ice/ami/AllTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public bool SentSynchronously
5555
}
5656
}
5757

58-
public void Report(bool sentSynchronously)
58+
public void Report(bool value)
5959
{
60-
SentSynchronously = sentSynchronously;
60+
SentSynchronously = value;
6161
Sent = true;
6262
}
6363

csharp/test/Ice/executor/AllTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Progress()
1414

1515
public bool getResult() => _sentSynchronously;
1616

17-
public void Report(bool sentSynchronously) => _sentSynchronously = sentSynchronously;
17+
public void Report(bool value) => _sentSynchronously = value;
1818

1919
private bool _sentSynchronously;
2020
}

slice/IceGrid/Descriptor.ice

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module IceGrid
9595
/// The descriptors of well-known objects.
9696
ObjectDescriptorSeq objects;
9797

98-
/// The descriptors of allocatable objects
98+
/// The descriptors of allocatable objects.
9999
ObjectDescriptorSeq allocatables;
100100
}
101101

0 commit comments

Comments
 (0)