Skip to content

Commit 70407a4

Browse files
authored
Misc/changes fixes (#1926)
* Misc changes/fixes * Add color to console output * Code cleanup * Fix C++20 compile error
1 parent 902aeff commit 70407a4

File tree

6 files changed

+101
-99
lines changed

6 files changed

+101
-99
lines changed

include/CppSharp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
// Licensed under the MIT license
88
// ------------------------------------------------------------------------------------------- //
9+
// clang-format off
910
#pragma once
1011

1112
#if defined(__cplusplus_cli)
@@ -293,7 +294,7 @@ namespace clix {
293294
/// <param name="string">String to be marshalled to the other side</param>
294295
/// <returns>The marshaled representation of the string</returns>
295296
template<Encoding encoding, typename SourceType, class ResultType =
296-
typename detail::IfManaged<SourceType>::Select::Either<
297+
typename detail::IfManaged<SourceType>::Select::template Either<
297298
typename detail::StringTypeSelector<encoding>::Type,
298299
System::String^>::Type
299300
>

src/Core/Diagnostics.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,25 @@ public ConsoleDiagnostics()
112112
Level = DiagnosticKind.Message;
113113
}
114114

115+
private string GetColorForDiagKind(DiagnosticKind kind) =>
116+
kind switch
117+
{
118+
DiagnosticKind.Debug => null,
119+
DiagnosticKind.Message => null,
120+
DiagnosticKind.Warning => "\u001b[33m", // yellow
121+
DiagnosticKind.Error => "\u001b[91m", // red
122+
_ => null
123+
};
124+
115125
public void Emit(DiagnosticInfo info)
116126
{
117127
if (info.Kind < Level)
118128
return;
119129

120130
var currentIndentation = Indents.Sum();
121-
var message = new string(' ', currentIndentation) + info.Message;
131+
var colorString = GetColorForDiagKind(info.Kind);
132+
var colorReset = colorString == null ? null : "\u001b[0m";
133+
var message = $"{new string(' ', currentIndentation)}{colorString}{info.Message}{colorReset}";
122134

123135
if (info.Kind == DiagnosticKind.Error)
124136
{
@@ -128,7 +140,10 @@ public void Emit(DiagnosticInfo info)
128140
{
129141
Console.WriteLine(message);
130142
}
131-
Debug.WriteLine(message);
143+
144+
// Don't output debug messages to VS output window. This is extremely slow.
145+
if (info.Kind > DiagnosticKind.Debug)
146+
Debug.WriteLine(message);
132147
}
133148

134149
public void PushIndent(int level)

src/CppParser/Bootstrap/StmtCodeGenerators.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public override bool VisitProperty(Property property)
344344
{
345345
var expr = $"_S->{fieldName} = static_cast<AST::{typeName}>(WalkExpression(S->{methodName}()));";
346346

347-
if (fieldName == "base" && typeName is "CXXDependentScopeMemberExpr")
347+
if (fieldName == "base" && @class.Name == "CXXDependentScopeMemberExpr")
348348
{
349349
// Clang asserts that 'getBase()' is not called when 'isImplicitAccess()' returns true
350350
WriteLine("if (!S->isImplicitAccess())");

src/Generator/Passes/RenamePass.cs

Lines changed: 76 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,33 @@ protected RenamePass(RenameTargets targets)
4343

4444
public virtual bool Rename(Declaration decl, out string newName)
4545
{
46-
if (decl is Method method && !method.IsStatic)
46+
switch (decl)
4747
{
48-
Method rootBaseMethod;
49-
if (method.OriginalNamespace is Class @class && @class.IsInterface)
50-
rootBaseMethod = (Method)method.OriginalFunction;
51-
else
52-
rootBaseMethod = method.GetRootBaseMethod();
53-
if (rootBaseMethod != null && rootBaseMethod != method)
48+
case Method { IsStatic: false } method:
5449
{
55-
newName = rootBaseMethod.Name;
56-
return true;
57-
}
58-
}
50+
Method rootBaseMethod;
51+
if (method.OriginalNamespace is Class { IsInterface: true })
52+
rootBaseMethod = (Method)method.OriginalFunction;
53+
else
54+
rootBaseMethod = method.GetRootBaseMethod();
55+
if (rootBaseMethod != null && rootBaseMethod != method)
56+
{
57+
newName = rootBaseMethod.Name;
58+
return true;
59+
}
5960

60-
if (decl is Property property && !property.IsStatic)
61-
{
62-
var rootBaseProperty = ((Class)property.Namespace).GetBasePropertyByName(property);
63-
if (rootBaseProperty != null && rootBaseProperty != property)
61+
break;
62+
}
63+
case Property { IsStatic: false } property:
6464
{
65-
newName = rootBaseProperty.Name;
66-
return true;
65+
var rootBaseProperty = ((Class)property.Namespace).GetBasePropertyByName(property);
66+
if (rootBaseProperty != null && rootBaseProperty != property)
67+
{
68+
newName = rootBaseProperty.Name;
69+
return true;
70+
}
71+
72+
break;
6773
}
6874
}
6975

@@ -73,62 +79,46 @@ public virtual bool Rename(Declaration decl, out string newName)
7379

7480
public bool IsRenameableDecl(Declaration decl)
7581
{
76-
if (decl is Class)
77-
return Targets.HasFlag(RenameTargets.Class);
78-
79-
var method = decl as Method;
80-
if (method != null)
82+
switch (decl)
8183
{
82-
return Targets.HasFlag(RenameTargets.Method) &&
83-
method.Kind == CXXMethodKind.Normal &&
84-
method.Name != "dispose";
85-
}
86-
87-
var function = decl as Function;
88-
if (function != null)
89-
{
90-
// Special case the IDisposable.Dispose method.
91-
return Targets.HasFlag(RenameTargets.Function) &&
92-
(!function.IsOperator && function.Name != "dispose");
93-
}
94-
95-
if (decl is Parameter)
96-
return Targets.HasFlag(RenameTargets.Parameter);
97-
98-
if (decl is Enumeration.Item)
99-
return Targets.HasFlag(RenameTargets.EnumItem);
100-
101-
if (decl is Enumeration)
102-
return Targets.HasFlag(RenameTargets.Enum);
103-
104-
var property = decl as Property;
105-
if (property != null)
106-
return Targets.HasFlag(RenameTargets.Property) && !property.IsIndexer;
107-
108-
if (decl is Event)
109-
return Targets.HasFlag(RenameTargets.Event);
110-
111-
if (decl is TypedefDecl)
112-
return Targets.HasFlag(RenameTargets.Delegate);
113-
114-
if (decl is Namespace && !(decl is TranslationUnit))
115-
return Targets.HasFlag(RenameTargets.Namespace);
116-
117-
if (decl is Variable)
118-
return Targets.HasFlag(RenameTargets.Variable);
119-
120-
var field = decl as Field;
121-
if (field != null)
122-
{
123-
if (!Targets.HasFlag(RenameTargets.Field))
84+
case Class:
85+
return Targets.HasFlag(RenameTargets.Class);
86+
case Method method:
87+
return Targets.HasFlag(RenameTargets.Method) &&
88+
method.Kind == CXXMethodKind.Normal &&
89+
method.Name != "dispose";
90+
case Function function:
91+
// Special case the IDisposable.Dispose method.
92+
return Targets.HasFlag(RenameTargets.Function) &&
93+
(!function.IsOperator && function.Name != "dispose");
94+
case Parameter:
95+
return Targets.HasFlag(RenameTargets.Parameter);
96+
case Enumeration.Item:
97+
return Targets.HasFlag(RenameTargets.EnumItem);
98+
case Enumeration:
99+
return Targets.HasFlag(RenameTargets.Enum);
100+
case Property property:
101+
return Targets.HasFlag(RenameTargets.Property) && !property.IsIndexer;
102+
case Event:
103+
return Targets.HasFlag(RenameTargets.Event);
104+
case TypedefDecl:
105+
return Targets.HasFlag(RenameTargets.Delegate);
106+
case Namespace when !(decl is TranslationUnit):
107+
return Targets.HasFlag(RenameTargets.Namespace);
108+
case Variable:
109+
return Targets.HasFlag(RenameTargets.Variable);
110+
case Field when !Targets.HasFlag(RenameTargets.Field):
111+
return false;
112+
case Field field:
113+
{
114+
var fieldProperty = ((Class)field.Namespace).Properties.FirstOrDefault(
115+
p => p.Field == field);
116+
return (fieldProperty != null &&
117+
fieldProperty.IsInRefTypeAndBackedByValueClassField());
118+
}
119+
default:
124120
return false;
125-
var fieldProperty = ((Class)field.Namespace).Properties.FirstOrDefault(
126-
p => p.Field == field);
127-
return (fieldProperty != null &&
128-
fieldProperty.IsInRefTypeAndBackedByValueClassField());
129121
}
130-
131-
return false;
132122
}
133123

134124
public override bool VisitDeclaration(Declaration decl)
@@ -148,8 +138,7 @@ public override bool VisitDeclaration(Declaration decl)
148138

149139
private bool Rename(Declaration decl)
150140
{
151-
string newName;
152-
if (!Rename(decl, out newName) || AreThereConflicts(decl, newName))
141+
if (!Rename(decl, out var newName) || AreThereConflicts(decl, newName))
153142
return false;
154143

155144
decl.Name = newName;
@@ -167,8 +156,7 @@ private static bool AreThereConflicts(Declaration decl, string newName)
167156
declarations.AddRange(decl.Namespace.Events);
168157
declarations.Add(decl.Namespace);
169158

170-
var function = decl as Function;
171-
if (function != null)
159+
if (decl is Function function)
172160
// account for overloads
173161
declarations.AddRange(GetFunctionsWithTheSameParams(function));
174162
else
@@ -177,11 +165,10 @@ private static bool AreThereConflicts(Declaration decl, string newName)
177165
declarations.AddRange(decl.Namespace.Variables);
178166
declarations.AddRange(from typedefDecl in decl.Namespace.Typedefs
179167
let pointerType = typedefDecl.Type.Desugar() as PointerType
180-
where pointerType != null && pointerType.GetFinalPointee() is FunctionType
168+
where pointerType?.GetFinalPointee() is FunctionType
181169
select typedefDecl);
182170

183-
var specialization = decl as ClassTemplateSpecialization;
184-
if (specialization != null)
171+
if (decl is ClassTemplateSpecialization specialization)
185172
declarations.RemoveAll(d => specialization.TemplatedDecl.TemplatedDecl == d);
186173

187174
var @class = decl.Namespace as Class;
@@ -201,8 +188,7 @@ where typedefDecl.Type.Desugar() is FunctionType
201188
if (decl is Method && decl.IsGenerated)
202189
return @class.GetPropertyByName(newName) != null;
203190

204-
var property = decl as Property;
205-
if (property != null)
191+
if (decl is Property property)
206192
{
207193
Property existingProperty = @class.Properties.Find(
208194
p => p != decl && p.Name == newName);
@@ -216,8 +202,7 @@ where typedefDecl.Type.Desugar() is FunctionType
216202
}
217203
}
218204

219-
var enumItem = decl as Enumeration.Item;
220-
if (enumItem != null)
205+
if (decl is Enumeration.Item enumItem)
221206
return ((Enumeration)enumItem.Namespace).Items.Any(
222207
i => i != decl && i.Name == newName);
223208

@@ -226,8 +211,7 @@ where typedefDecl.Type.Desugar() is FunctionType
226211

227212
private static IEnumerable<Function> GetFunctionsWithTheSameParams(Function function)
228213
{
229-
var method = function as Method;
230-
if (method != null)
214+
if (function is Method method)
231215
{
232216
return ((Class)method.Namespace).Methods.Where(
233217
m => !m.Ignore && m.Parameters.SequenceEqual(function.Parameters, new ParameterComparer()));
@@ -391,14 +375,12 @@ public static string ConvertCaseString(Declaration decl, RenameCasePattern patte
391375
if (decl.Name.All(c => !char.IsLetter(c)))
392376
return decl.Name;
393377

394-
var typedef = decl as TypedefDecl;
395-
if (typedef != null && typedef.IsSynthetized)
396-
return decl.Name;
397-
398-
var property = decl as Property;
399-
if (property != null && property.GetMethod != null &&
400-
property.GetMethod.SynthKind == FunctionSynthKind.InterfaceInstance)
401-
return decl.Name;
378+
switch (decl)
379+
{
380+
case TypedefDecl { IsSynthetized: true }:
381+
case Property { GetMethod.SynthKind: FunctionSynthKind.InterfaceInstance }:
382+
return decl.Name;
383+
}
402384

403385
var sb = new StringBuilder(decl.Name);
404386
// check if it's been renamed to avoid a keyword
@@ -412,14 +394,14 @@ public static string ConvertCaseString(Declaration decl, RenameCasePattern patte
412394
{
413395
case RenameCasePattern.UpperCamelCase:
414396
// ensure separation in enum items by not ending up with more capitals in a row than before
415-
if (sb.Length == 1 || !char.IsUpper(sb[1]) || !(decl is Enumeration.Item))
397+
if (sb.Length == 1 || !char.IsUpper(sb[1]) || decl is not Enumeration.Item)
416398
sb[0] = char.ToUpperInvariant(sb[0]);
417-
if (@class != null && @class.Type == ClassType.Interface)
399+
if (@class is { Type: ClassType.Interface })
418400
sb[1] = char.ToUpperInvariant(sb[1]);
419401
break;
420402
case RenameCasePattern.LowerCamelCase:
421403
sb[0] = char.ToLowerInvariant(sb[0]);
422-
if (@class != null && @class.Type == ClassType.Interface)
404+
if (@class is { Type: ClassType.Interface })
423405
sb[1] = char.ToLowerInvariant(sb[1]);
424406
break;
425407
}

src/Generator/Types/Std/Stdlib.CSharp.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,13 @@ public override Type SignatureType(TypePrinterContext ctx)
307307
{
308308
if (ctx.Kind == TypePrinterContextKind.Managed)
309309
return new CILType(typeof(string));
310+
310311
var typePrinter = new CSharpTypePrinter(null);
311312
typePrinter.PushContext(TypePrinterContextKind.Native);
313+
312314
if (ctx.Type.Desugar().IsAddress())
313315
return new CustomType(typePrinter.IntPtrType);
316+
314317
ClassTemplateSpecialization basicString = GetBasicString(ctx.Type);
315318
return new CustomType(basicString.Visit(typePrinter).Type);
316319
}

tests/dotnet/Common/Common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "../Tests.h"
1+
#pragma once
2+
#include "../Tests.h"
23
#include "AnotherUnit.h"
34

45
#ifdef _WIN32

0 commit comments

Comments
 (0)