Skip to content

Fix analysis errors around type of types #363

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

Merged
merged 1 commit into from
Apr 17, 2025
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **API:** `EnumeratorOccurrence::getMoveNext` method.
- **API:** `EnumeratorOccurrence::getCurrent` method.
- **API:** `ForInStatementNode.getEnumeratorOccurrence` method.
- **API:** `TypeOfTypeNode::getTypeReferenceNode` method.

### Changed

Expand All @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- False positives on enums that are never referenced by name (but have used values) in `UnusedType`.
- Name resolution failures in legacy initialization sections referencing the implementation section.
- Incorrect file position calculation for multiline string tokens.
- Analysis errors around `type of` type declarations.

## [1.15.0] - 2025-04-03

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ simpleProcedureType : procedureTypeHeading -> ^(TkProcedureType<Procedu
procedureTypeHeading : FUNCTION<ProcedureTypeHeadingNodeImpl>^ routineParameters? routineReturnType? ((';')? interfaceDirective)*
| PROCEDURE<ProcedureTypeHeadingNodeImpl>^ routineParameters? ((';')? interfaceDirective)*
;
typeOfType : TYPE<TypeOfTypeNodeImpl>^ OF typeDecl
typeOfType : TYPE<TypeOfTypeNodeImpl>^ OF typeReference
;
strongAliasType : TYPE<StrongAliasTypeNodeImpl>^ typeReferenceOrStringOrFile codePageExpression?
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import au.com.integradev.delphi.antlr.ast.visitors.DelphiParserVisitor;
import javax.annotation.Nonnull;
import org.antlr.runtime.Token;
import org.sonar.plugins.communitydelphi.api.ast.TypeNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeOfTypeNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeReferenceNode;
import org.sonar.plugins.communitydelphi.api.type.Type;

public final class TypeOfTypeNodeImpl extends TypeNodeImpl implements TypeOfTypeNode {
Expand All @@ -35,9 +36,19 @@ public <T> T accept(DelphiParserVisitor<T> visitor, T data) {
return visitor.visit(this, data);
}

@Override
public TypeReferenceNode getTypeReferenceNode() {
return (TypeReferenceNode) getChild(1);
}

@Override
@Nonnull
protected Type createType() {
return ((TypeNode) getChild(0)).getType();
String image = null;
if (parent instanceof TypeDeclarationNode) {
image = ((TypeDeclarationNode) parent).fullyQualifiedName();
}
Type type = getTypeReferenceNode().getType();
return getTypeFactory().classOf(image, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
*/
package org.sonar.plugins.communitydelphi.api.ast;

public interface TypeOfTypeNode extends TypeNode {}
public interface TypeOfTypeNode extends TypeNode {
TypeReferenceNode getTypeReferenceNode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ void testClassReferenceConstructorTypeResolution() {
verifyUsages(8, 16, reference(22, 11));
}

@Test
void testClassReferenceTypeOf() {
execute("classReferences/TypeOf.pas");
verifyUsages(10, 10, reference(17, 2));
}

@Test
void testSimpleForwardDeclarations() {
execute("forwardDeclarations/Simple.pas");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
unit TypeOf;

interface

type
TIntegerType = type of Integer;

implementation

procedure Foo(T: TIntegerType);
begin
// do nothing
end;

procedure Test;
begin
Foo(Integer);
end;

end.
Loading