Skip to content

Commit

Permalink
Merge pull request #4698 from openium:feature/swift-5.9-support
Browse files Browse the repository at this point in the history
[swift] Add macro expansion support for swift 5.9 #4698
  • Loading branch information
adangel committed Dec 11, 2023
2 parents abed5b0 + 84c2eb7 commit cc8455e
Show file tree
Hide file tree
Showing 16 changed files with 966 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/pages/pmd/languages/swift.md
Expand Up @@ -10,3 +10,5 @@ summary: "Swift-specific features and guidance"
> powerful for experts. It is fast, modern, safe, and a joy to write.
{% include language_info.html name='Swift' id='swift' implementation='swift::lang.swift.SwiftLanguageModule' supports_pmd=true supports_cpd=true since='5.3.7' %}

The grammar of the languages is documented in [The Swift Language Reference](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/aboutthelanguagereference/).
7 changes: 7 additions & 0 deletions docs/pages/release_notes.md
Expand Up @@ -73,6 +73,10 @@ As PMD 7 revamped the Java module, if you have custom rules, you need to migrate
See the use case [I'm using custom rules]({{ baseurl }}pmd_userdocs_migrating_to_pmd7.html#im-using-custom-rules)
in the Migration Guide.

##### Swift Support

* limited support for Swift 5.9 (Macro Expansions)

#### Rule Changes

**New Rules**
Expand Down Expand Up @@ -142,6 +146,7 @@ The following previously deprecated classes have been removed:
* [#4640](https://github.com/pmd/pmd/pull/4640): \[cli] Launch script fails if run via "bash pmd" - [Shai Bennathan](https://github.com/shai-bennathan) (@shai-bennathan)
* [#4673](https://github.com/pmd/pmd/pull/4673): \[javascript] CPD: Added support for decorator notation - [Wener](https://github.com/wener-tiobe) (@wener-tiobe)
* [#4677](https://github.com/pmd/pmd/pull/4677): \[apex] Add new rule: OperationWithHighCostInLoop - [Thomas Prouvot](https://github.com/tprouvot) (@tprouvot)
* [#4698](https://github.com/pmd/pmd/pull/4698): \[swift] Add macro expansion support for swift 5.9 - [Richard B.](https://github.com/kenji21) (@kenji21)
* [#4706](https://github.com/pmd/pmd/pull/4706): \[java] DetachedTestCase should not report on abstract methods - [Debamoy Datta](https://github.com/Debamoy) (@Debamoy)
* [#4719](https://github.com/pmd/pmd/pull/4719): \[java] UnnecessaryCaseChange: example doc toUpperCase() should compare to a capitalized string - [ciufudean](https://github.com/ciufudean) (@ciufudean)
* [#4738](https://github.com/pmd/pmd/pull/4738): \[doc] Added reference to the PMD extension for bld - [Erik C. Thauvin](https://github.com/ethauvin) (@ethauvin)
Expand Down Expand Up @@ -219,6 +224,7 @@ For more information on the languages, see the [Detailed Release Notes for PMD 7
#### New: Swift support

* use PMD to analyze Swift code with PMD rules
* limited support for Swift 5.9 (Macro Expansions)
* initially 4 built-in rules

Contributors: [Lucas Soncini](https://github.com/lsoncini) (@lsoncini),
Expand Down Expand Up @@ -811,6 +817,7 @@ Language specific fixes:
* [#4665](https://github.com/pmd/pmd/pull/4665): \[java] Doc: Fix references AutoClosable -> AutoCloseable - [Andrey Bozhko](https://github.com/AndreyBozhko) (@AndreyBozhko)
* [#4673](https://github.com/pmd/pmd/pull/4673): \[javascript] CPD: Added support for decorator notation - [Wener](https://github.com/wener-tiobe) (@wener-tiobe)
* [#4677](https://github.com/pmd/pmd/pull/4677): \[apex] Add new rule: OperationWithHighCostInLoop - [Thomas Prouvot](https://github.com/tprouvot) (@tprouvot)
* [#4698](https://github.com/pmd/pmd/pull/4698): \[swift] Add macro expansion support for swift 5.9 - [Richard B.](https://github.com/kenji21) (@kenji21)
* [#4706](https://github.com/pmd/pmd/pull/4706): \[java] DetachedTestCase should not report on abstract methods - [Debamoy Datta](https://github.com/Debamoy) (@Debamoy)
* [#4719](https://github.com/pmd/pmd/pull/4719): \[java] UnnecessaryCaseChange: example doc toUpperCase() should compare to a capitalized string - [ciufudean](https://github.com/ciufudean) (@ciufudean)
* [#4738](https://github.com/pmd/pmd/pull/4738): \[doc] Added reference to the PMD extension for bld - [Erik C. Thauvin](https://github.com/ethauvin) (@ethauvin)
Expand Down
8 changes: 6 additions & 2 deletions docs/pages/release_notes_pmd7.md
Expand Up @@ -154,8 +154,12 @@ See [the example report](report-examples/cpdhtml-v2.html).

### New: Swift support

Given the full Antlr support, PMD now fully supports Swift. We are pleased to announce we are shipping a number of
rules starting with PMD 7.
Given the full Antlr support, PMD now fully supports Swift for creating rules. Previously only CPD was supported.

Note: There is only limited support for newer Swift language features in the parser, e.g. Swift 5.9 (Macro Expansions)
are supported, but other features are not.

We are pleased to announce we are shipping a number of rules starting with PMD 7.

* {% rule "swift/errorprone/ForceCast" %} (`swift-errorprone`) flags all force casts, making sure you are
defensively considering all types. Having the application crash shouldn't be an option.
Expand Down
Expand Up @@ -122,6 +122,7 @@ public Vocabulary getVocabulary() {

case "@": return "at-symbol";
case "$": return "dollar";
case "#": return "hash";

case "\\": return "backslash";
case "/": return "slash";
Expand Down
Expand Up @@ -60,7 +60,7 @@ class BinaryDistributionIT extends AbstractBinaryDistributionTest {
"scala-2.10", "scala-2.11", "scala-2.12", "scala-2.13",
"swift-4.2", "swift-5.0", "swift-5.1", "swift-5.2",
"swift-5.3", "swift-5.4", "swift-5.5", "swift-5.6",
"swift-5.7", "vf-52", "vf-53", "vf-54", "vf-55", "vf-56",
"swift-5.7", "swift-5.8", "swift-5.9", "vf-52", "vf-53", "vf-54", "vf-55", "vf-56",
"vf-57", "vf-58", "vf-59",
"vm-2.0", "vm-2.1", "vm-2.2", "vm-2.3", "wsdl-1.1",
"wsdl-2.0", "xml-1.0", "xml-1.1", "xsl-1.0", "xsl-2.0",
Expand Down
Expand Up @@ -596,6 +596,7 @@ primaryExpression
| implicitMemberExpression
// | implicit_member_expression disallow as ambig with explicit member expr in postfix_expression
| wildcardExpression
| macroExpansionExpression
| selectorExpression
| keyPathExpression
;
Expand Down Expand Up @@ -685,6 +686,11 @@ tupleElement: (identifier ':')? expression ;

wildcardExpression : '_' ;

// GRAMMAR OF A MACRO EXPANSION EXPRESSION
// https://docs.swift.org/swift-book/documentation/the-swift-programming-language/expressions#Macro-Expansion-Expression
// https://github.com/apple/swift-evolution/blob/main/proposals/0382-expression-macros.md#macro-expansion
macroExpansionExpression: '#' identifier genericArgumentClause? functionCallArgumentClause? ;

// GRAMMAR OF A SELECTOR EXPRESSION

selectorExpression
Expand Down Expand Up @@ -831,6 +837,7 @@ sType
| sType '?' // optional-type
| sType '!' // implicitly-unwrapped-optional-type
| protocolCompositionType
| 'some' sType // opaque-type
| sType '.' 'Type' | sType '.' 'Protocol' // metatype
| 'Any' | 'Self'
;
Expand Down Expand Up @@ -963,7 +970,8 @@ contextSensitiveKeyword :
'lazy' | 'left' | 'mutating' | 'none' | 'nonmutating' | 'optional' | 'operator' | 'override' | 'postfix' | 'precedence' |
'prefix' | 'Protocol' | 'required' | 'right' | 'set' | 'Type' | 'unowned' | 'weak' | 'willSet' |
'iOS' | 'iOSApplicationExtension' | 'OSX' | 'OSXApplicationExtension­' | 'watchOS' | 'x86_64' |
'arm' | 'arm64' | 'i386' | 'os' | 'arch' | 'safe' | 'tvOS' | 'file' | 'line' | 'default' | 'Self' | 'var'
'arm' | 'arm64' | 'i386' | 'os' | 'arch' | 'safe' | 'tvOS' | 'file' | 'line' | 'default' | 'Self' | 'var' |
'some'
;

grammarString:
Expand Down
Expand Up @@ -30,7 +30,9 @@ public SwiftLanguageModule() {
.addVersion("5.4")
.addVersion("5.5")
.addVersion("5.6")
.addDefaultVersion("5.7"),
.addVersion("5.7")
.addVersion("5.8")
.addDefaultVersion("5.9"),
new SwiftHandler());
}

Expand Down
Expand Up @@ -4,9 +4,14 @@

package net.sourceforge.pmd.lang.swift.ast;

import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseParser;
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwTopLevel;
Expand All @@ -15,10 +20,21 @@
* Adapter for the SwiftParser.
*/
public final class PmdSwiftParser extends AntlrBaseParser<SwiftNode, SwTopLevel> {
private static final Logger LOGGER = LoggerFactory.getLogger(PmdSwiftParser.class);

@Override
protected SwTopLevel parse(final Lexer lexer, ParserTask task) {
SwiftParser parser = new SwiftParser(new CommonTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
LOGGER.warn("Syntax error at {}:{}:{}: {}", task.getFileId().getOriginalPath(),
line, charPositionInLine, msg);
// TODO: eventually we should throw a parse exception
// throw new ParseException(msg).withLocation(FileLocation.caret(task.getFileId(), line, charPositionInLine));
}
});
return parser.topLevel().makeAstInfo(task);
}

Expand Down
Expand Up @@ -18,4 +18,13 @@ void testBtree() {
doTest("BTree");
}

@Test
void swift59() {
doTest("Swift5.9");
}

@Test
void macroExpansions() {
doTest("MacroExpansionExpressions");
}
}
Expand Up @@ -50,6 +50,11 @@ void testSwift56() {
doTest("Swift5.6");
}

@Test
void testSwift59() {
doTest("Swift5.9");
}

@Test
void testStackoverflowOnLongLiteral() {
doTest("Issue628");
Expand Down
@@ -0,0 +1,11 @@
// Samples from https://github.com/apple/swift-evolution/blob/main/proposals/0382-expression-macros.md#macro-expansion

// macro expansion expression with one argument
let _: (Int, String) = #addBlocker(x + y * z)

// macro expansion expressions within macro arguments
let _: (Int, String) = #addBlocker(#stringify(1 + 2))

// default built-in macros
let _: String = #fileID
let _: Int = #line

0 comments on commit cc8455e

Please sign in to comment.