diff --git a/Package.swift b/Package.swift index 6f4aad3f299..249f9d645af 100644 --- a/Package.swift +++ b/Package.swift @@ -297,16 +297,6 @@ let package = Package( dependencies: ["_SwiftSyntaxTestSupport", "SwiftRefactor"] ), - // MARK: - Executable targets - - .executableTarget( - name: "swift-parser-cli", - dependencies: [ - "_InstructionCounter", "SwiftBasicFormat", "SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftParserDiagnostics", "SwiftSyntax", - .product(name: "ArgumentParser", package: "swift-argument-parser"), - ] - ), - // MARK: - Deprecated targets // MARK: PerformanceTest @@ -335,14 +325,3 @@ package.targets.append( } ) ) - -if useLocalDependencies { - package.dependencies += [ - .package(path: "../swift-argument-parser") - ] -} else { - // Building standalone. - package.dependencies += [ - .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2") - ] -} diff --git a/Sources/SwiftBasicFormat/SwiftBasicFormat.docc/FilingBugReports.md b/Sources/SwiftBasicFormat/SwiftBasicFormat.docc/FilingBugReports.md index e28c69a6d3a..3c6179ab246 100644 --- a/Sources/SwiftBasicFormat/SwiftBasicFormat.docc/FilingBugReports.md +++ b/Sources/SwiftBasicFormat/SwiftBasicFormat.docc/FilingBugReports.md @@ -6,9 +6,9 @@ Guide to provide steps for filing actionable bug reports for `BasicFormat` failu Reducing a failure requires the `swift-parser-cli` utility that you can build by checking out `swift-syntax` and running ``` -swift build --product swift-parser-cli +swift build --package-path SwiftParserCLI ``` -or building the `swift-parser-cli` target in Xcode. +or openning `SwiftParserCLI` package and building the `swift-parser-cli` target in Xcode. 1. After you have built `swift-parse-cli`, you can format a single source file using BasicFormat by running the following command. If you are only experiencing the issue while formatting a single node, e.g. while creating an `AccessorDeclSyntax` inside a macro, you can additionally pass the type of the node as `--node-type AccessorDeclSyntax` ``` diff --git a/Sources/SwiftParser/SwiftParser.docc/FilingBugReports.md b/Sources/SwiftParser/SwiftParser.docc/FilingBugReports.md index 794e966c103..915afc1ab19 100644 --- a/Sources/SwiftParser/SwiftParser.docc/FilingBugReports.md +++ b/Sources/SwiftParser/SwiftParser.docc/FilingBugReports.md @@ -2,7 +2,7 @@ Guide to provide steps for filing actionable bug reports for parser failures. -Reducing a test case requires the `swift-parser-cli` utility that you can build by checking out `swift-syntax` and running `swift build --product swift-parser-cli` or building the `swift-parser-cli` target in Xcode. +Reducing a test case requires the `swift-parser-cli` utility that you can build by checking out `swift-syntax` and running `swift build --package-path SwiftParserCLI` or openning the `SwiftParserCLI` package and building the `swift-parser-cli` target in Xcode. ## Round-Trip Failure or Parser Crash diff --git a/SwiftParserCLI/Package.swift b/SwiftParserCLI/Package.swift new file mode 100644 index 00000000000..c59ec321d32 --- /dev/null +++ b/SwiftParserCLI/Package.swift @@ -0,0 +1,47 @@ +// swift-tools-version:5.7 + +import Foundation +import PackageDescription + +let package = Package( + name: "SwiftParserCLI", + platforms: [ + .macOS(.v10_15) + ], + products: [ + .executable(name: "swift-parser-cli", targets: ["swift-parser-cli"]) + ], + dependencies: [ + .package(path: "..") + ], + targets: [ + .target( + name: "InstructionCounter" + ), + + .executableTarget( + name: "swift-parser-cli", + dependencies: [ + "InstructionCounter", + .product(name: "SwiftBasicFormat", package: "swift-syntax"), + .product(name: "SwiftDiagnostics", package: "swift-syntax"), + .product(name: "SwiftOperators", package: "swift-syntax"), + .product(name: "SwiftParser", package: "swift-syntax"), + .product(name: "SwiftParserDiagnostics", package: "swift-syntax"), + .product(name: "SwiftSyntax", package: "swift-syntax"), + .product(name: "ArgumentParser", package: "swift-argument-parser"), + ] + ), + ] +) + +if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil { + // Building standalone. + package.dependencies += [ + .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2") + ] +} else { + package.dependencies += [ + .package(path: "../../swift-argument-parser") + ] +} diff --git a/SwiftParserCLI/README.md b/SwiftParserCLI/README.md new file mode 100644 index 00000000000..f42dbdd7a12 --- /dev/null +++ b/SwiftParserCLI/README.md @@ -0,0 +1,14 @@ +# SwiftParserCLI + +This directory contains the source files of the `swift-parser-cli` program. + +You can build `swift-parser-cli` by checking out `swift-syntax` and running +``` +swift build --package-path SwiftParserCLI +``` +or entering this directory and running +``` +swift build +``` + +You can also open the `SwiftParserCLI` package and building the `swift-parser-cli` target in Xcode. diff --git a/SwiftParserCLI/Sources/InstructionCounter/include/InstructionsExecuted.h b/SwiftParserCLI/Sources/InstructionCounter/include/InstructionsExecuted.h new file mode 100644 index 00000000000..af9d9a4157b --- /dev/null +++ b/SwiftParserCLI/Sources/InstructionCounter/include/InstructionsExecuted.h @@ -0,0 +1,17 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#include + +/// On macOS returns the number of instructions the process has executed since +/// it was launched, on all other platforms returns 0. +uint64_t getInstructionsExecuted(); diff --git a/SwiftParserCLI/Sources/InstructionCounter/src/InstructionsExecuted.c b/SwiftParserCLI/Sources/InstructionCounter/src/InstructionsExecuted.c new file mode 100644 index 00000000000..4df6fa632f2 --- /dev/null +++ b/SwiftParserCLI/Sources/InstructionCounter/src/InstructionsExecuted.c @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#if __APPLE__ +#include +#if TARGET_OS_MAC && !TARGET_OS_IPHONE +#define TARGET_IS_MACOS 1 +#endif +#endif + +#include "InstructionsExecuted.h" + +#ifdef TARGET_IS_MACOS +#include +#include +#include + +uint64_t getInstructionsExecuted() { + struct rusage_info_v4 ru; + if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) { + return ru.ri_instructions; + } + return 0; +} +#else +uint64_t getInstructionsExecuted() { + return 0; +} +#endif diff --git a/Sources/swift-parser-cli/BasicFormat.swift b/SwiftParserCLI/Sources/swift-parser-cli/BasicFormat.swift similarity index 100% rename from Sources/swift-parser-cli/BasicFormat.swift rename to SwiftParserCLI/Sources/swift-parser-cli/BasicFormat.swift diff --git a/Sources/swift-parser-cli/Commands/PerformanceTest.swift b/SwiftParserCLI/Sources/swift-parser-cli/Commands/PerformanceTest.swift similarity index 99% rename from Sources/swift-parser-cli/Commands/PerformanceTest.swift rename to SwiftParserCLI/Sources/swift-parser-cli/Commands/PerformanceTest.swift index 300b175cbd0..4b8d545ee28 100644 --- a/Sources/swift-parser-cli/Commands/PerformanceTest.swift +++ b/SwiftParserCLI/Sources/swift-parser-cli/Commands/PerformanceTest.swift @@ -12,9 +12,9 @@ import ArgumentParser import Foundation +import InstructionCounter import SwiftParser import SwiftSyntax -import _InstructionCounter struct PerformanceTest: ParsableCommand { static var configuration = CommandConfiguration( diff --git a/Sources/swift-parser-cli/Commands/PrintDiags.swift b/SwiftParserCLI/Sources/swift-parser-cli/Commands/PrintDiags.swift similarity index 100% rename from Sources/swift-parser-cli/Commands/PrintDiags.swift rename to SwiftParserCLI/Sources/swift-parser-cli/Commands/PrintDiags.swift diff --git a/Sources/swift-parser-cli/Commands/PrintTree.swift b/SwiftParserCLI/Sources/swift-parser-cli/Commands/PrintTree.swift similarity index 100% rename from Sources/swift-parser-cli/Commands/PrintTree.swift rename to SwiftParserCLI/Sources/swift-parser-cli/Commands/PrintTree.swift diff --git a/Sources/swift-parser-cli/Commands/Reduce.swift b/SwiftParserCLI/Sources/swift-parser-cli/Commands/Reduce.swift similarity index 100% rename from Sources/swift-parser-cli/Commands/Reduce.swift rename to SwiftParserCLI/Sources/swift-parser-cli/Commands/Reduce.swift diff --git a/Sources/swift-parser-cli/Commands/VerifyRoundTrip.swift b/SwiftParserCLI/Sources/swift-parser-cli/Commands/VerifyRoundTrip.swift similarity index 100% rename from Sources/swift-parser-cli/Commands/VerifyRoundTrip.swift rename to SwiftParserCLI/Sources/swift-parser-cli/Commands/VerifyRoundTrip.swift diff --git a/Sources/swift-parser-cli/ParseCommand.swift b/SwiftParserCLI/Sources/swift-parser-cli/ParseCommand.swift similarity index 100% rename from Sources/swift-parser-cli/ParseCommand.swift rename to SwiftParserCLI/Sources/swift-parser-cli/ParseCommand.swift diff --git a/Sources/swift-parser-cli/SyntaxUtils.swift b/SwiftParserCLI/Sources/swift-parser-cli/SyntaxUtils.swift similarity index 100% rename from Sources/swift-parser-cli/SyntaxUtils.swift rename to SwiftParserCLI/Sources/swift-parser-cli/SyntaxUtils.swift diff --git a/Sources/swift-parser-cli/TerminalUtils.swift b/SwiftParserCLI/Sources/swift-parser-cli/TerminalUtils.swift similarity index 100% rename from Sources/swift-parser-cli/TerminalUtils.swift rename to SwiftParserCLI/Sources/swift-parser-cli/TerminalUtils.swift diff --git a/Sources/swift-parser-cli/Utils.swift b/SwiftParserCLI/Sources/swift-parser-cli/Utils.swift similarity index 100% rename from Sources/swift-parser-cli/Utils.swift rename to SwiftParserCLI/Sources/swift-parser-cli/Utils.swift diff --git a/Sources/swift-parser-cli/swift-parser-cli.swift b/SwiftParserCLI/Sources/swift-parser-cli/swift-parser-cli.swift similarity index 97% rename from Sources/swift-parser-cli/swift-parser-cli.swift rename to SwiftParserCLI/Sources/swift-parser-cli/swift-parser-cli.swift index d2b51ead174..21f02d924c0 100644 --- a/Sources/swift-parser-cli/swift-parser-cli.swift +++ b/SwiftParserCLI/Sources/swift-parser-cli/swift-parser-cli.swift @@ -12,12 +12,12 @@ import ArgumentParser import Foundation +import InstructionCounter import SwiftDiagnostics import SwiftOperators import SwiftParser import SwiftParserDiagnostics import SwiftSyntax -import _InstructionCounter #if os(Windows) import WinSDK diff --git a/SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/Build.swift b/SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/Build.swift index 171a6274dc9..ae884a7c02d 100644 --- a/SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/Build.swift +++ b/SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/Build.swift @@ -26,6 +26,7 @@ struct Build: ParsableCommand, BuildCommand { func run() throws { try buildTarget(packageDir: Paths.packageDir, targetName: "SwiftSyntax-all") try buildTarget(packageDir: Paths.examplesDir, targetName: "Examples-all") + try buildTarget(packageDir: Paths.swiftParserCliDir, targetName: "swift-parser-cli") try buildEditorExtension() } diff --git a/SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/Paths.swift b/SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/Paths.swift index 48a42e5e42f..693314275d4 100644 --- a/SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/Paths.swift +++ b/SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/Paths.swift @@ -32,6 +32,11 @@ enum Paths { .appendingPathComponent("Examples") } + static var swiftParserCliDir: URL { + packageDir + .appendingPathComponent("SwiftParserCLI") + } + static var codeGenerationDir: URL { packageDir .appendingPathComponent("CodeGeneration")