Skip to content

Commit 8061358

Browse files
author
Harlan
authored
[InlinableText] Handle multiline #if conditions (#19675)
Previously, a #if of the form: ```swift #if ( false ) print("x") #endif ``` Would be emitted after #if-stripping as ```swift false ) print("x") ``` Because the old logic assumed conditions will always appear on one line. Instead, for active clauses that have conditions, skip to the next line after the end of the condition instead.
1 parent 13b884b commit 8061358

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lib/AST/InlinableText.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "swift/AST/ASTWalker.h"
1515
#include "swift/AST/ASTNode.h"
1616
#include "swift/AST/Decl.h"
17+
#include "swift/AST/Expr.h"
1718
#include "swift/Parse/Lexer.h"
1819

1920
#include "llvm/ADT/SmallVector.h"
@@ -92,9 +93,15 @@ struct ExtractInactiveRanges : public ASTWalker {
9293
return false;
9394
}
9495

95-
// Ignore range from beginning of '#if' to the beginning of the elements
96-
// of this clause.
97-
addRange(start, Lexer::getLocForEndOfLine(sourceMgr, clause->Loc));
96+
// Ignore range from beginning of '#if', '#elseif', or '#else' to the
97+
// beginning of the elements of this clause.
98+
auto elementsBegin = clause->Loc;
99+
// If there's a condition (e.g. this isn't a '#else' block), then ignore
100+
// everything up to the end of the condition.
101+
if (auto cond = clause->Cond) {
102+
elementsBegin = cond->getEndLoc();
103+
}
104+
addRange(start, Lexer::getLocForEndOfLine(sourceMgr, elementsBegin));
98105

99106
// Ignore range from effective end of the elements of this clause to the
100107
// end of the '#endif'

test/ModuleInterface/if-configs.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,37 @@ public func hasClosureDefaultArgWithComplexPoundIf(_ x: () -> Void = {
6969
}) {
7070
}
7171

72+
// CHECK: func hasClosureDefaultArgWithMultilinePoundIfCondition(_ x: () -> Void = {
73+
// CHECK-NOT: #if (
74+
// CHECK-NOT: !false && true
75+
// CHECK-NOT: )
76+
// CHECK: print("should appear")
77+
// CHECK-NOT: #endif
78+
// CHECK-NOT: #if (
79+
// CHECK-NOT: !true
80+
// CHECK-NOT: )
81+
// CHECK-NOT: print("should not appear")
82+
// CHECK-NOT: #else
83+
// CHECK: print("also should appear")
84+
// CHECK-NOT: #endif
85+
// CHECK-NEXT: })
86+
public func hasClosureDefaultArgWithMultilinePoundIfCondition(_ x: () -> Void = {
87+
#if (
88+
!false && true
89+
)
90+
print("should appear")
91+
#endif
92+
93+
#if (
94+
!true
95+
)
96+
print("should not appear")
97+
#else
98+
print("also should appear")
99+
#endif
100+
}) {
101+
}
102+
72103
// CHECK: func hasClosureDefaultArgWithSinglePoundIf(_ x: () -> Void = {
73104
// CHECK-NOT: #if true
74105
// CHECK: print("true")
@@ -85,7 +116,6 @@ public func hasClosureDefaultArgWithSinglePoundIf(_ x: () -> Void = {
85116
}) {
86117
}
87118

88-
89119
// CHECK: func hasSimpleDefaultArgs(_ x: Int = 0, b: Int = 1)
90120
public func hasSimpleDefaultArgs(_ x: Int = 0, b: Int = 1) {
91121
}

0 commit comments

Comments
 (0)