[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.
This commit is contained in:
Harlan
2018-10-02 19:48:40 -07:00
committed by GitHub
parent 13b884ba6c
commit 8061358211
2 changed files with 41 additions and 4 deletions

View File

@@ -14,6 +14,7 @@
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ASTNode.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/Parse/Lexer.h"
#include "llvm/ADT/SmallVector.h"
@@ -92,9 +93,15 @@ struct ExtractInactiveRanges : public ASTWalker {
return false;
}
// Ignore range from beginning of '#if' to the beginning of the elements
// of this clause.
addRange(start, Lexer::getLocForEndOfLine(sourceMgr, clause->Loc));
// Ignore range from beginning of '#if', '#elseif', or '#else' to the
// beginning of the elements of this clause.
auto elementsBegin = clause->Loc;
// If there's a condition (e.g. this isn't a '#else' block), then ignore
// everything up to the end of the condition.
if (auto cond = clause->Cond) {
elementsBegin = cond->getEndLoc();
}
addRange(start, Lexer::getLocForEndOfLine(sourceMgr, elementsBegin));
// Ignore range from effective end of the elements of this clause to the
// end of the '#endif'

View File

@@ -69,6 +69,37 @@ public func hasClosureDefaultArgWithComplexPoundIf(_ x: () -> Void = {
}) {
}
// CHECK: func hasClosureDefaultArgWithMultilinePoundIfCondition(_ x: () -> Void = {
// CHECK-NOT: #if (
// CHECK-NOT: !false && true
// CHECK-NOT: )
// CHECK: print("should appear")
// CHECK-NOT: #endif
// CHECK-NOT: #if (
// CHECK-NOT: !true
// CHECK-NOT: )
// CHECK-NOT: print("should not appear")
// CHECK-NOT: #else
// CHECK: print("also should appear")
// CHECK-NOT: #endif
// CHECK-NEXT: })
public func hasClosureDefaultArgWithMultilinePoundIfCondition(_ x: () -> Void = {
#if (
!false && true
)
print("should appear")
#endif
#if (
!true
)
print("should not appear")
#else
print("also should appear")
#endif
}) {
}
// CHECK: func hasClosureDefaultArgWithSinglePoundIf(_ x: () -> Void = {
// CHECK-NOT: #if true
// CHECK: print("true")
@@ -85,7 +116,6 @@ public func hasClosureDefaultArgWithSinglePoundIf(_ x: () -> Void = {
}) {
}
// CHECK: func hasSimpleDefaultArgs(_ x: Int = 0, b: Int = 1)
public func hasSimpleDefaultArgs(_ x: Int = 0, b: Int = 1) {
}