Skip to content
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

Shorten #file and add #filePath (behind an experimental flag) #25656

Merged
merged 6 commits into from
Dec 7, 2019
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
2 changes: 2 additions & 0 deletions include/swift/AST/DefaultArgumentKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ enum class DefaultArgumentKind : uint8_t {
Inherited,
/// The #file default argument, which is expanded at the call site.
File,
/// The #filePath default argument, which is expanded at the call site.
FilePath,
/// The #line default argument, which is expanded at the call site.
Line,
/// The #column default argument, which is expanded at the call site.
Expand Down
3 changes: 2 additions & 1 deletion include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
class MagicIdentifierLiteralExpr : public LiteralExpr {
public:
enum Kind : unsigned {
File, Line, Column, Function, DSOHandle
File, FilePath, Line, Column, Function, DSOHandle
};
private:
SourceLoc Loc;
Expand All @@ -1067,6 +1067,7 @@ class MagicIdentifierLiteralExpr : public LiteralExpr {
bool isString() const {
switch (getKind()) {
case File:
case FilePath:
case Function:
return true;
case Line:
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ namespace swift {
/// when using RequireExplicitAvailability.
std::string RequireExplicitAvailabilityTarget;

/// If false, '#file' evaluates to the full path rather than a
/// human-readable string.
bool EnableConcisePoundFile = false;

///
/// Support for alternate usage modes
///
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ def enable_experimental_differentiable_programming : Flag<["-"], "enable-experim
Flags<[FrontendOption]>,
HelpText<"Enable experimental differentiable programming features">;

def enable_experimental_concise_pound_file : Flag<["-"],
"enable-experimental-concise-pound-file">,
Flags<[FrontendOption]>,
HelpText<"Enable experimental concise '#file' identifier and '#filePath' alternative">;

// Diagnostic control options
def suppress_warnings : Flag<["-"], "suppress-warnings">,
Flags<[FrontendOption]>,
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ static StringRef getDefaultArgumentKindString(DefaultArgumentKind value) {
case DefaultArgumentKind::Column: return "#column";
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
case DefaultArgumentKind::File: return "#file";
case DefaultArgumentKind::FilePath: return "#filePath";
case DefaultArgumentKind::Function: return "#function";
case DefaultArgumentKind::Inherited: return "inherited";
case DefaultArgumentKind::Line: return "#line";
Expand All @@ -316,6 +317,7 @@ static StringRef
getMagicIdentifierLiteralExprKindString(MagicIdentifierLiteralExpr::Kind value) {
switch (value) {
case MagicIdentifierLiteralExpr::File: return "#file";
case MagicIdentifierLiteralExpr::FilePath: return "#filePath";
case MagicIdentifierLiteralExpr::Function: return "#function";
case MagicIdentifierLiteralExpr::Line: return "#line";
case MagicIdentifierLiteralExpr::Column: return "#column";
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6083,6 +6083,7 @@ bool ParamDecl::hasDefaultExpr() const {
return false;
case DefaultArgumentKind::Normal:
case DefaultArgumentKind::File:
case DefaultArgumentKind::FilePath:
case DefaultArgumentKind::Line:
case DefaultArgumentKind::Column:
case DefaultArgumentKind::Function:
Expand All @@ -6105,6 +6106,7 @@ bool ParamDecl::hasCallerSideDefaultExpr() const {
case DefaultArgumentKind::Normal:
return false;
case DefaultArgumentKind::File:
case DefaultArgumentKind::FilePath:
case DefaultArgumentKind::Line:
case DefaultArgumentKind::Column:
case DefaultArgumentKind::Function:
Expand Down Expand Up @@ -6414,6 +6416,7 @@ ParamDecl::getDefaultValueStringRepresentation(
}
case DefaultArgumentKind::Inherited: return "super";
case DefaultArgumentKind::File: return "#file";
case DefaultArgumentKind::FilePath: return "#filePath";
case DefaultArgumentKind::Line: return "#line";
case DefaultArgumentKind::Column: return "#column";
case DefaultArgumentKind::Function: return "#function";
Expand Down
2 changes: 2 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
inputArgs.AddLastArg(arguments, options::OPT_enable_astscope_lookup);
inputArgs.AddLastArg(arguments, options::OPT_disable_astscope_lookup);
inputArgs.AddLastArg(arguments, options::OPT_disable_parser_lookup);
inputArgs.AddLastArg(arguments,
options::OPT_enable_experimental_concise_pound_file);

// Pass on any build config options
inputArgs.AddAllArgs(arguments, options::OPT_D);
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.OptimizationRemarkMissedPattern =
generateOptimizationRemarkRegex(Diags, Args, A);

Opts.EnableConcisePoundFile =
Args.hasArg(OPT_enable_experimental_concise_pound_file);

llvm::Triple Target = Opts.Target;
StringRef TargetArg;
if (const Arg *A = Args.getLastArg(OPT_target)) {
Expand Down
5 changes: 5 additions & 0 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
return !includeDefaultArgs;

case DefaultArgumentKind::File:
case DefaultArgumentKind::FilePath:
case DefaultArgumentKind::Line:
case DefaultArgumentKind::Column:
case DefaultArgumentKind::Function:
Expand Down Expand Up @@ -3653,6 +3654,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
CodeCompletionLiteralKind::StringLiteral, "String");
addFromProto("#file", CodeCompletionKeywordKind::pound_file,
CodeCompletionLiteralKind::StringLiteral, "String");
if (Ctx.LangOpts.EnableConcisePoundFile) {
addFromProto("#filePath", CodeCompletionKeywordKind::pound_file,
CodeCompletionLiteralKind::StringLiteral, "String");
}
addFromProto("#line", CodeCompletionKeywordKind::pound_line,
CodeCompletionLiteralKind::IntegerLiteral, "Int");
addFromProto("#column", CodeCompletionKeywordKind::pound_column,
Expand Down
12 changes: 12 additions & 0 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,8 @@ getMagicIdentifierLiteralKind(tok Kind) {
case tok::kw___FILE__:
case tok::pound_file:
return MagicIdentifierLiteralExpr::Kind::File;
case tok::pound_filePath:
return MagicIdentifierLiteralExpr::Kind::FilePath;
case tok::kw___FUNCTION__:
case tok::pound_function:
return MagicIdentifierLiteralExpr::Kind::Function;
Expand Down Expand Up @@ -1446,6 +1448,15 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
.fixItReplace(Tok.getLoc(), replacement);
LLVM_FALLTHROUGH;
}

case tok::pound_filePath:
// Check twice because of fallthrough--this is ugly but temporary.
if (Tok.is(tok::pound_filePath) && !Context.LangOpts.EnableConcisePoundFile)
diagnose(Tok.getLoc(), diag::unknown_pound_expr, "filePath");
// Continue since we actually do know how to handle it. This avoids extra
// diagnostics.
LLVM_FALLTHROUGH;

case tok::pound_column:
case tok::pound_file:
case tok::pound_function:
Expand All @@ -1455,6 +1466,7 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
switch (Tok.getKind()) {
case tok::pound_column: SKind = SyntaxKind::PoundColumnExpr; break;
case tok::pound_file: SKind = SyntaxKind::PoundFileExpr; break;
case tok::pound_filePath: SKind = SyntaxKind::PoundFilePathExpr; break;
case tok::pound_function: SKind = SyntaxKind::PoundFunctionExpr; break;
// FIXME: #line was renamed to #sourceLocation
case tok::pound_line: SKind = SyntaxKind::PoundLineExpr; break;
Expand Down
2 changes: 2 additions & 0 deletions lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ static DefaultArgumentKind getDefaultArgKind(Expr *init) {
return DefaultArgumentKind::Column;
case MagicIdentifierLiteralExpr::File:
return DefaultArgumentKind::File;
case MagicIdentifierLiteralExpr::FilePath:
return DefaultArgumentKind::FilePath;
case MagicIdentifierLiteralExpr::Line:
return DefaultArgumentKind::Line;
case MagicIdentifierLiteralExpr::Function:
Expand Down
1 change: 1 addition & 0 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant,
case DefaultArgumentKind::Inherited:
case DefaultArgumentKind::Column:
case DefaultArgumentKind::File:
case DefaultArgumentKind::FilePath:
case DefaultArgumentKind::Line:
case DefaultArgumentKind::Function:
case DefaultArgumentKind::DSOHandle:
Expand Down
36 changes: 33 additions & 3 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4876,6 +4876,27 @@ getMagicFunctionString(SILGenFunction &SGF) {
return SGF.MagicFunctionString;
}

static StringRef
getMagicFilePathString(SILGenFunction &SGF, SourceLoc loc) {
if (!loc.isValid())
return "";

return SGF.getASTContext().SourceMgr.getDisplayNameForLoc(loc);
}

static std::string
getConciseMagicFileString(SILGenFunction &SGF, SourceLoc loc) {
if (!loc.isValid())
return "";

auto path = getMagicFilePathString(SGF, loc);
auto value = llvm::sys::path::filename(path).str();
value += " (";
value += SGF.getModule().getSwiftModule()->getNameStr();
value += ")";
return value;
}

/// Emit an application of the given allocating initializer.
RValue SILGenFunction::emitApplyAllocatingInitializer(SILLocation loc,
ConcreteDeclRef init,
Expand Down Expand Up @@ -5131,9 +5152,18 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
auto magicLiteral = cast<MagicIdentifierLiteralExpr>(literal);
switch (magicLiteral->getKind()) {
case MagicIdentifierLiteralExpr::File: {
std::string value;
if (loc.isValid())
value = ctx.SourceMgr.getDisplayNameForLoc(loc);
std::string value = getASTContext().LangOpts.EnableConcisePoundFile
? getConciseMagicFileString(*this, loc)
: getMagicFilePathString(*this, loc).str();
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,
magicLiteral->getStringEncoding());
builtinInit = magicLiteral->getBuiltinInitializer();
init = magicLiteral->getInitializer();
break;
}

case MagicIdentifierLiteralExpr::FilePath: {
StringRef value = getMagicFilePathString(*this, loc);
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,
magicLiteral->getStringEncoding());
builtinInit = magicLiteral->getBuiltinInitializer();
Expand Down
1 change: 1 addition & 0 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3681,6 +3681,7 @@ RValue RValueEmitter::
visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
switch (E->getKind()) {
case MagicIdentifierLiteralExpr::File:
case MagicIdentifierLiteralExpr::FilePath:
case MagicIdentifierLiteralExpr::Function:
case MagicIdentifierLiteralExpr::Line:
case MagicIdentifierLiteralExpr::Column:
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,7 @@ namespace {
Expr *visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *expr) {
switch (expr->getKind()) {
case MagicIdentifierLiteralExpr::File:
case MagicIdentifierLiteralExpr::FilePath:
case MagicIdentifierLiteralExpr::Function:
return handleStringLiteralExpr(expr);

Expand Down
1 change: 1 addition & 0 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,7 @@ namespace {
switch (expr->getKind()) {
case MagicIdentifierLiteralExpr::Column:
case MagicIdentifierLiteralExpr::File:
case MagicIdentifierLiteralExpr::FilePath:
case MagicIdentifierLiteralExpr::Function:
case MagicIdentifierLiteralExpr::Line:
return visitLiteralExpr(expr);
Expand Down
5 changes: 5 additions & 0 deletions lib/Sema/TypeCheckExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,11 @@ static Expr *synthesizeCallerSideDefault(const ParamDecl *param,
MagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr::File, loc,
/*implicit=*/true);

case DefaultArgumentKind::FilePath:
return new (ctx)
MagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr::FilePath, loc,
/*implicit=*/true);

case DefaultArgumentKind::Line:
return new (ctx)
MagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr::Line, loc,
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,7 @@ static void diagnoseIgnoredLiteral(ASTContext &Ctx, LiteralExpr *LE) {
case ExprKind::MagicIdentifierLiteral:
switch (cast<MagicIdentifierLiteralExpr>(LE)->getKind()) {
case MagicIdentifierLiteralExpr::Kind::File: return "#file";
case MagicIdentifierLiteralExpr::Kind::FilePath: return "#filePath";
case MagicIdentifierLiteralExpr::Kind::Line: return "#line";
case MagicIdentifierLiteralExpr::Kind::Column: return "#column";
case MagicIdentifierLiteralExpr::Kind::Function: return "#function";
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ ProtocolDecl *TypeChecker::getLiteralProtocol(ASTContext &Context, Expr *expr) {
if (auto E = dyn_cast<MagicIdentifierLiteralExpr>(expr)) {
switch (E->getKind()) {
case MagicIdentifierLiteralExpr::File:
case MagicIdentifierLiteralExpr::FilePath:
case MagicIdentifierLiteralExpr::Function:
return TypeChecker::getProtocol(
Context, expr->getLoc(),
Expand Down
2 changes: 2 additions & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ getActualDefaultArgKind(uint8_t raw) {
return swift::DefaultArgumentKind::Column;
case serialization::DefaultArgumentKind::File:
return swift::DefaultArgumentKind::File;
case serialization::DefaultArgumentKind::FilePath:
return swift::DefaultArgumentKind::FilePath;
case serialization::DefaultArgumentKind::Line:
return swift::DefaultArgumentKind::Line;
case serialization::DefaultArgumentKind::Function:
Expand Down
3 changes: 2 additions & 1 deletion lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 526; // @_dynamicReplacement adjustments
const uint16_t SWIFTMODULE_VERSION_MINOR = 527; // #filePath

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down Expand Up @@ -437,6 +437,7 @@ enum class DefaultArgumentKind : uint8_t {
None = 0,
Normal,
File,
FilePath,
Line,
Column,
Function,
Expand Down
1 change: 1 addition & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ static uint8_t getRawStableDefaultArgumentKind(swift::DefaultArgumentKind kind)
CASE(Inherited)
CASE(Column)
CASE(File)
CASE(FilePath)
CASE(Line)
CASE(Function)
CASE(DSOHandle)
Expand Down
19 changes: 19 additions & 0 deletions test/SILGen/magic_identifier_file.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-emit-silgen -module-name Foo %s | %FileCheck --check-prefixes=BOTH,ABSOLUTE %s
// RUN: %target-swift-emit-silgen -enable-experimental-concise-pound-file -DNEEDS_CONCISE -module-name Foo %s | %FileCheck --check-prefixes=BOTH,CONCISE %s

// FIXME: Once this feature becomes non-experimental, we should update existing
// tests and delete this file.

func directUse() {
// BOTH-LABEL: sil {{.*}} @$s3Foo9directUseyyF
print(#file)
// ABSOLUTE: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_file.swift"
// CONCISE: string_literal utf8 "magic_identifier_file.swift (Foo)"
}

func indirectUse() {
// BOTH-LABEL: sil {{.*}} @$s3Foo11indirectUseyyF
fatalError()
// ABSOLUTE: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_file.swift"
// CONCISE: string_literal utf8 "magic_identifier_file.swift (Foo)"
}
26 changes: 26 additions & 0 deletions test/SILGen/magic_identifier_filepath.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Check that we generate the right code with the flag.
// RUN: %target-swift-emit-silgen -enable-experimental-concise-pound-file -module-name Foo %s | %FileCheck %s

// Check that we give errors for use of #filePath if concise #file isn't enabled.
// FIXME: Drop if we stop rejecting this.
// RUN: %target-typecheck-verify-swift -module-name Foo %s

// FIXME: Once this feature becomes non-experimental, we should duplicate
// existing #file tests and delete this file.

func directUse() {
print(#filePath) // expected-error {{use of unknown directive '#filePath'}}

// CHECK-LABEL: sil {{.*}} @$s3Foo9directUseyyF
// CHECK: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_filepath.swift"
}

func indirectUse() {
functionWithFilePathDefaultArgument()

// CHECK-LABEL: sil {{.*}} @$s3Foo11indirectUseyyF
// CHECK: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_filepath.swift"
}

func functionWithFilePathDefaultArgument(file: String = #filePath) {}
// expected-error@-1 {{use of unknown directive '#filePath'}}
6 changes: 6 additions & 0 deletions utils/gyb_syntax_support/ExprNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
Child('PoundFile', kind='PoundFileToken'),
]),

# A #filePath expression.
Node('PoundFilePathExpr', kind='Expr',
children=[
Child('PoundFilePath', kind='PoundFilePathToken'),
]),

# A #function expression.
Node('PoundFunctionExpr', kind='Expr',
children=[
Expand Down
1 change: 1 addition & 0 deletions utils/gyb_syntax_support/NodeSerializationCodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
'DifferentiationParam': 237,
'DifferentiableAttributeFuncSpecifier': 238,
'FunctionDeclName': 239,
'PoundFilePathExpr': 240,
}


Expand Down
2 changes: 2 additions & 0 deletions utils/gyb_syntax_support/Token.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ def macro_name(self):
serialization_code=73),
PoundKeyword('PoundFile', 'file', text='#file',
serialization_code=68),
PoundKeyword('PoundFilePath', 'filePath', text='#filePath',
serialization_code=121),
PoundKeyword('PoundColumn', 'column', text='#column',
serialization_code=70),
PoundKeyword('PoundFunction', 'function', text='#function',
Expand Down