Skip to content

[InlinableText] Handle multiline #if conditions #19675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/AST/InlinableText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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'
Expand Down
32 changes: 31 additions & 1 deletion test/ModuleInterface/if-configs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
}