File: macro_default_argument_source_location.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (136 lines) | stat: -rw-r--r-- 4,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// REQUIRES: swift_swift_parser
// REQUIRES: executable_test

// RUN: %empty-directory(%t)

// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath

// RUN: %target-build-swift-dylib(%t/%target-library-name(WithMacroDefaultArg)) %S/Inputs/with_macro_default_arg_module.swift -module-name WithMacroDefaultArg -emit-module -emit-module-path %t/WithMacroDefaultArg.swiftmodule -load-plugin-library %t/%target-library-name(MacroDefinition) -I %t -swift-version 5

// RUN: %target-build-swift-dylib(%t/%target-library-name(WithMacroDefaultArgInterface)) %S/Inputs/with_macro_default_arg_interface.swift -module-name WithMacroDefaultArgInterface -enable-library-evolution -emit-module-interface-path %t/WithMacroDefaultArgInterface.swiftinterface -load-plugin-library %t/%target-library-name(MacroDefinition) -I %t -swift-version 5

// RUN: %target-build-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %S/Inputs/with_macro_default_arg_same_module.swift %s -o %t/main -module-name MacroUser -emit-tbd -emit-tbd-path %t/MacroUser.tbd -I %t -L %t %target-rpath(%t) -lWithMacroDefaultArg -lWithMacroDefaultArgInterface
// RUN: %target-codesign %t/main
// RUN: %target-codesign %t/%target-library-name(WithMacroDefaultArg)
// RUN: %target-codesign %t/%target-library-name(WithMacroDefaultArgInterface)
// RUN: %target-run %t/main %t/%target-library-name(WithMacroDefaultArg) %t/%target-library-name(WithMacroDefaultArgInterface) | %FileCheck %s

import WithMacroDefaultArg
import WithMacroDefaultArgInterface

struct SourceLocation: CustomStringConvertible {
    let line: Int
    let column: Int

    var description: String {
        "\(line):\(column)"
    }
}

func partOfDefaultArgumentOkay(
    // CHECK: [[# @LINE + 1]]:59
    location: SourceLocation = .init(line: #Line, column: #Column)
) {
    print(location)
}

// CHECK: [[# @LINE + 1]]
func parenthesizedExpansionAtDeclOkay(line: Int = (#Line)) {
    print(line)
}

func asDefaultArgument(line: Int = #Line) {
    print(line)
}

func asDefaultArgumentExpandingToBuiltInLine(line: Int = #MagicLine) {
    print(line)
}

@resultBuilder
enum SourceLocationBuilder {
    static func buildExpression<T>(
        _ expression: T, line: Int = #Line, column: Int = #Column
    ) -> SourceLocation {
        SourceLocation(line: line, column: column)
    }

    static func buildBlock(_ location: SourceLocation) -> SourceLocation {
        location
    }
}

func build(@SourceLocationBuilder body: () -> SourceLocation) -> SourceLocation {
    body()
}

let result = build {
    // CHECK: [[# @LINE + 1]]:5
    0
}

func sameFileID(builtIn: String = #fileID, expressionMacro: String = #FileID) {
    print(builtIn == expressionMacro)
}

func sameFilePath(builtIn: String = #filePath, expressionMacro: String = #FilePath) {
    print(builtIn == expressionMacro)
}

func sameLine(builtIn: Int = #line, expressionMacro: Int = #Line) {
    print(builtIn == expressionMacro)
}

func sameColumn(builtIn: Int = #column, expressionMacro: Int = #Column) {
    print(builtIn == expressionMacro)
}

func buildPrinter(
    @ClosureCallerBuilder makeCaller: () -> ClosureCaller
) -> ClosureCaller {
    makeCaller()
}

// CHECK: macro@MacroUser/macro_default_argument_source_location.swift#[[# @LINE + 1]]
let printWithFileLine = buildPrinter { }

@main struct Main {
    static func main() {
        partOfDefaultArgumentOkay()
        parenthesizedExpansionAtDeclOkay()
        print(result)
        printWithFileLine(context: "macro")
      
        // CHECK: hello world
        preferVariableFromLocalScope()
      
        do {
            let shadowed = 42
            // CHECK: hello 42
            preferVariableFromLocalScope()
        }
        
        // CHECK: [[# @LINE + 1]]
        asDefaultArgument()
        // CHECK: [[# @LINE + 1]]
        asDefaultArgumentExpandingToBuiltInLine()
        // CHECK: [[# @LINE + 1]]
        printCurrentLineDefinedAtAnotherFile()
        // CHECK: [[# @LINE + 1]]
        printCurrentLineDefinedAtAnotherModule()
        // CHECK: MacroUser/with_macro_default_arg_same_module.swift
        printAnotherFileName()
        // CHECK: MacroUser/macro_default_argument_source_location.swift
        printCurrentFileDefinedAtAnotherFile()
        // CHECK: MacroUser/macro_default_argument_source_location.swift
        printCurrentFileDefinedInAnotherModuleInterface()
        // CHECK: true
        sameFileID()
        // CHECK: true
        sameFilePath()
        // CHECK: true
        sameLine()
        // CHECK: true
        sameColumn()
    }
}