-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Implement SE-0075: CanImport #11613
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
Implement SE-0075: CanImport #11613
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ Optional<PlatformConditionKind> getPlatformConditionKind(StringRef Name) { | |
.Case("arch", PlatformConditionKind::Arch) | ||
.Case("_endian", PlatformConditionKind::Endianness) | ||
.Case("_runtime", PlatformConditionKind::Runtime) | ||
.Case("canImport", PlatformConditionKind::CanImport) | ||
.Default(None); | ||
} | ||
|
||
|
@@ -289,7 +290,7 @@ class ValidateIfConfigCondition : | |
return E; | ||
} | ||
|
||
// ( 'os' | 'arch' | '_endian' | '_runtime' ) '(' identifier ')'' | ||
// ( 'os' | 'arch' | '_endian' | '_runtime' | 'canImport') '(' identifier ')'' | ||
auto Kind = getPlatformConditionKind(*KindName); | ||
if (!Kind.hasValue()) { | ||
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_expression); | ||
|
@@ -331,6 +332,8 @@ class ValidateIfConfigCondition : | |
DiagName = "architecture"; break; | ||
case PlatformConditionKind::Endianness: | ||
DiagName = "endianness"; break; | ||
case PlatformConditionKind::CanImport: | ||
DiagName = "import conditional"; break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this testable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of now, no. There's no validation of the module name being performed, so there's no way this will get hit (see the FIXME). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, just checking. |
||
case PlatformConditionKind::Runtime: | ||
llvm_unreachable("handled above"); | ||
} | ||
|
@@ -450,6 +453,9 @@ class EvaluateIfConfigCondition : | |
Str, SourceLoc(), nullptr).getValue(); | ||
auto thisVersion = Ctx.LangOpts.EffectiveLanguageVersion; | ||
return thisVersion >= Val; | ||
} else if (KindName == "canImport") { | ||
auto Str = extractExprSource(Ctx.SourceMgr, Arg); | ||
return Ctx.canImportModule({ Ctx.getIdentifier(Str) , E->getLoc() }); | ||
} | ||
|
||
auto Val = getDeclRefStr(Arg); | ||
|
@@ -567,9 +573,10 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig( | |
Expr *Condition = nullptr; | ||
bool isActive = false; | ||
|
||
// Parse and evaluate the directive. | ||
// Parse the condition. Evaluate it to determine the active | ||
// clause unless we're doing a parse-only pass. | ||
if (isElse) { | ||
isActive = !foundActive; | ||
isActive = !foundActive && State->PerformConditionEvaluation; | ||
} else { | ||
llvm::SaveAndRestore<bool> S(InPoundIfEnvironment, true); | ||
ParserResult<Expr> Result = parseExprSequence(diag::expected_expr, | ||
|
@@ -582,8 +589,9 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig( | |
// Error in the condition; | ||
isActive = false; | ||
isVersionCondition = false; | ||
} else if (!foundActive) { | ||
// Evaluate the condition only if we haven't found any active one. | ||
} else if (!foundActive && State->PerformConditionEvaluation) { | ||
// Evaluate the condition only if we haven't found any active one and | ||
// we're not in parse-only mode. | ||
isActive = evaluateIfConfigCondition(Condition, Context); | ||
isVersionCondition = isVersionIfConfigCondition(Condition); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1366,7 +1366,7 @@ SourceFile *SwiftLangSupport::getSyntacticSourceFile( | |
Error = "Compiler invocation set up failed"; | ||
return nullptr; | ||
} | ||
ParseCI.performParseOnly(); | ||
ParseCI.performParseOnly(/*EvaluateConditionals*/true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @benlangmuir Is this correct? Should this be a parameter to this function? Its callers are the 'syntactic rename' and the 'find rename ranges' actions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like a fine stopgap to me. |
||
|
||
SourceFile *SF = nullptr; | ||
unsigned BufferID = ParseCI.getInputBufferIDs().back(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit unfortunate to lose the named constant here...
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, but I'm not really sure what it buys us outside of this one place.