Skip to content

[5.3] Revise #file changes from SE-0274 #32811

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 10 commits into from
Jul 17, 2020
17 changes: 4 additions & 13 deletions include/swift/AST/DefaultArgumentKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ enum class DefaultArgumentKind : uint8_t {
/// The default argument is inherited from the corresponding argument of the
/// overridden declaration.
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.
Column,
/// The #function default argument, which is expanded at the call site.
Function,
/// The #dsohandle default argument, which is expanded at the call site.
DSOHandle,
/// The "nil" literal.
NilLiteral,
/// An empty array literal.
Expand All @@ -56,8 +44,11 @@ enum class DefaultArgumentKind : uint8_t {
EmptyDictionary,
/// A reference to the stored property. This is a special default argument
/// kind for the synthesized memberwise constructor to emit a call to the
// property's initializer.
/// property's initializer.
StoredProperty,
// Magic identifier literals expanded at the call site:
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) NAME,
#include "swift/AST/MagicIdentifierKinds.def"
};
enum { NumDefaultArgumentKindBits = 4 };

Expand Down
18 changes: 13 additions & 5 deletions include/swift/AST/DiagnosticsCommon.def
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,22 @@ ERROR(sdk_node_unrecognized_decl_kind,none,
ERROR(sdk_node_unrecognized_accessor_kind,none,
"unrecognized accessor kind '%0' in SDK node", (StringRef))

// Emitted from ModuleDecl::computeMagicFileStringMap()
WARNING(pound_source_location_creates_pound_file_conflicts,none,
"'#sourceLocation' directive produces '#file' string of '%0', which "
"conflicts with '#file' strings produced by other paths in the module",
(StringRef))
// Emitted from ModuleDecl::computeFileIDMap()
WARNING(source_location_creates_file_id_conflicts,none,
"'#sourceLocation' directive produces '#fileID' string of '%0', which "
"conflicts with '#fileID' strings produced by other paths in the "
"module", (StringRef))
NOTE(fixit_correct_source_location_file,none,
"change file in '#sourceLocation' to '%0'", (StringRef))

// Usually, but not always, emitted from the driver
ERROR(error_two_files_same_name,none,
"filename \"%0\" used twice: '%1' and '%2'",
(StringRef, StringRef, StringRef))
NOTE(note_explain_two_files_same_name,none,
"filenames are used to distinguish private declarations with the same "
"name", ())

//------------------------------------------------------------------------------
// MARK: Circular reference diagnostics
//------------------------------------------------------------------------------
Expand Down
7 changes: 0 additions & 7 deletions include/swift/AST/DiagnosticsDriver.def
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ WARNING(warn_arclite_not_found_when_link_objc_runtime,none,
"unable to find Objective-C runtime support library 'arclite'; "
"pass '-no-link-objc-runtime' to silence this warning", ())

ERROR(error_two_files_same_name,none,
"filename \"%0\" used twice: '%1' and '%2'",
(StringRef, StringRef, StringRef))
NOTE(note_explain_two_files_same_name,none,
"filenames are used to distinguish private declarations with the same "
"name", ())

WARNING(warn_cannot_stat_input,none,
"unable to determine when '%0' was last modified: %1",
(StringRef, StringRef))
Expand Down
27 changes: 9 additions & 18 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1074,17 +1074,14 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
class MagicIdentifierLiteralExpr : public LiteralExpr {
public:
enum Kind : unsigned {
File, FilePath, Line, Column, Function, DSOHandle
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) NAME,
#include "swift/AST/MagicIdentifierKinds.def"
};

static StringRef getKindString(MagicIdentifierLiteralExpr::Kind value) {
switch (value) {
case File: return "#file";
case FilePath: return "#filePath";
case Function: return "#function";
case Line: return "#line";
case Column: return "#column";
case DSOHandle: return "#dsohandle";
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) case NAME: return STRING;
#include "swift/AST/MagicIdentifierKinds.def"
}

llvm_unreachable("Unhandled MagicIdentifierLiteralExpr in getKindString.");
Expand All @@ -1107,21 +1104,15 @@ class MagicIdentifierLiteralExpr : public LiteralExpr {
return static_cast<Kind>(Bits.MagicIdentifierLiteralExpr.Kind);
}

bool isFile() const { return getKind() == File; }
bool isFunction() const { return getKind() == Function; }
bool isLine() const { return getKind() == Line; }
bool isColumn() const { return getKind() == Column; }

bool isString() const {
switch (getKind()) {
case File:
case FilePath:
case Function:
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case NAME: \
return true;
case Line:
case Column:
case DSOHandle:
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case NAME: \
return false;
#include "swift/AST/MagicIdentifierKinds.def"
}
llvm_unreachable("bad Kind");
}
Expand Down
116 changes: 116 additions & 0 deletions include/swift/AST/MagicIdentifierKinds.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//===--- MagicIdentifierKinds.def - Swift #ident metaprogramming -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines macros used for macro-metaprogramming with magic
// identifier literals.
//
//===----------------------------------------------------------------------===//

// Used for any magic identifier.
#ifndef MAGIC_IDENTIFIER
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
#endif

// Used for magic identifiers which produce string literals.
#ifndef MAGIC_STRING_IDENTIFIER
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
#endif

// Used for magic identifiers which produce integer literals.
#ifndef MAGIC_INT_IDENTIFIER
#define MAGIC_INT_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
#endif

// Used for magic identifiers which produce raw pointers.
#ifndef MAGIC_POINTER_IDENTIFIER
#define MAGIC_POINTER_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
#endif

// Used when a given token always maps to a particular magic identifier kind.
#ifndef MAGIC_IDENTIFIER_TOKEN
#define MAGIC_IDENTIFIER_TOKEN(NAME, TOKEN)
#endif

// Used when a given token always maps to a particular magic identifier kind,
// but that token is deprecated.
#ifndef MAGIC_IDENTIFIER_DEPRECATED_TOKEN
#define MAGIC_IDENTIFIER_DEPRECATED_TOKEN(NAME, TOKEN) MAGIC_IDENTIFIER_TOKEN(NAME, TOKEN)
#endif



//
// Magic string literals
//

/// The \c #fileID magic identifier literal.
MAGIC_STRING_IDENTIFIER(FileID, "#fileID", PoundFileIDExpr)
MAGIC_IDENTIFIER_TOKEN(FileID, pound_fileID)

/// The \c #file magic identifier literal, written in code where it is
/// a synonym for \c #fileID (i.e. "Swift 6 mode" code).
MAGIC_STRING_IDENTIFIER(FileIDSpelledAsFile, "#file", PoundFileExpr)
// tok::pound_file is shared with FilePathSpelledAsFile; please write custom
// code paths for it.

/// The \c #filePath magic identifier literal.
MAGIC_STRING_IDENTIFIER(FilePath, "#filePath", PoundFilePathExpr)
MAGIC_IDENTIFIER_TOKEN(FilePath, pound_filePath)

/// The \c #file magic identifier literal, written in code where it is
/// a synonym for \c #filePath (i.e. Swift 5 mode code).
MAGIC_STRING_IDENTIFIER(FilePathSpelledAsFile, "#file", PoundFileExpr)
// tok::pound_file is shared with FileIDSpelledAsFile; please write custom
// code paths for it.
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(FilePathSpelledAsFile, kw___FILE__)

/// The \c #function magic identifier literal.
MAGIC_STRING_IDENTIFIER(Function, "#function", PoundFunctionExpr)
MAGIC_IDENTIFIER_TOKEN(Function, pound_function)
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Function, kw___FUNCTION__)



//
// Magic integer literals
//

/// The \c #line magic identifier literal.
MAGIC_INT_IDENTIFIER(Line, "#line", PoundLineExpr)
MAGIC_IDENTIFIER_TOKEN(Line, pound_line)
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Line, kw___LINE__)

/// The \c #column magic identifier literal.
MAGIC_INT_IDENTIFIER(Column, "#column", PoundColumnExpr)
MAGIC_IDENTIFIER_TOKEN(Column, pound_column)
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Column, kw___COLUMN__)



//
// Magic raw pointer literals
//

/// The \c #dsohandle magic identifier literal.
MAGIC_POINTER_IDENTIFIER(DSOHandle, "#dsohandle", PoundDsohandleExpr)
MAGIC_IDENTIFIER_TOKEN(DSOHandle, pound_dsohandle)
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(DSOHandle, kw___DSO_HANDLE__)




#undef MAGIC_IDENTIFIER
#undef MAGIC_STRING_IDENTIFIER
#undef MAGIC_INT_IDENTIFIER
#undef MAGIC_POINTER_IDENTIFIER
#undef MAGIC_IDENTIFIER_TOKEN
#undef MAGIC_IDENTIFIER_DEPRECATED_TOKEN
15 changes: 6 additions & 9 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,23 +294,20 @@ class ModuleDecl : public DeclContext, public TypeDecl {
void addFile(FileUnit &newFile);
void removeFile(FileUnit &existingFile);

/// Creates a map from \c #filePath strings to corresponding \c #file
/// Creates a map from \c #filePath strings to corresponding \c #fileID
/// strings, diagnosing any conflicts.
///
/// A given \c #filePath string always maps to exactly one \c #file string,
/// A given \c #filePath string always maps to exactly one \c #fileID string,
/// but it is possible for \c #sourceLocation directives to introduce
/// duplicates in the opposite direction. If there are such conflicts, this
/// method will diagnose the conflict and choose a "winner" among the paths
/// in a reproducible way. The \c bool paired with the \c #file string is
/// in a reproducible way. The \c bool paired with the \c #fileID string is
/// \c true for paths which did not have a conflict or won a conflict, and
/// \c false for paths which lost a conflict. Thus, if you want to generate a
/// reverse mapping, you should drop or special-case the \c #file strings that
/// are paired with \c false.
///
/// Note that this returns an empty StringMap if concise \c #file strings are
/// disabled. Users should fall back to using the file path in this case.
/// reverse mapping, you should drop or special-case the \c #fileID strings
/// that are paired with \c false.
llvm::StringMap<std::pair<std::string, /*isWinner=*/bool>>
computeMagicFileStringMap(bool shouldDiagnose) const;
computeFileIDMap(bool shouldDiagnose) const;

/// Add a file declaring a cross-import overlay.
void addCrossImportOverlayFile(StringRef file);
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ def enable_experimental_additive_arithmetic_derivation :

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

// Diagnostic control options
Expand Down
9 changes: 3 additions & 6 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,10 @@ getForeignErrorConventionKindString(ForeignErrorConvention::Kind value) {
static StringRef getDefaultArgumentKindString(DefaultArgumentKind value) {
switch (value) {
case DefaultArgumentKind::None: return "none";
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";
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case DefaultArgumentKind::NAME: return STRING;
#include "swift/AST/MagicIdentifierKinds.def"
case DefaultArgumentKind::Inherited: return "inherited";
case DefaultArgumentKind::Line: return "#line";
case DefaultArgumentKind::NilLiteral: return "nil";
case DefaultArgumentKind::EmptyArray: return "[]";
case DefaultArgumentKind::EmptyDictionary: return "[:]";
Expand Down
9 changes: 3 additions & 6 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2739,12 +2739,9 @@ void PrintAST::printOneParameter(const ParamDecl *param,
Printer << " = ";

switch (param->getDefaultArgumentKind()) {
case DefaultArgumentKind::File:
case DefaultArgumentKind::Line:
case DefaultArgumentKind::Column:
case DefaultArgumentKind::Function:
case DefaultArgumentKind::DSOHandle:
case DefaultArgumentKind::NilLiteral:
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case DefaultArgumentKind::NAME:
#include "swift/AST/MagicIdentifierKinds.def"
Printer.printKeyword(defaultArgStr, Options);
break;
default:
Expand Down
27 changes: 9 additions & 18 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6321,12 +6321,9 @@ bool ParamDecl::hasDefaultExpr() const {
case DefaultArgumentKind::StoredProperty:
return false;
case DefaultArgumentKind::Normal:
case DefaultArgumentKind::File:
case DefaultArgumentKind::FilePath:
case DefaultArgumentKind::Line:
case DefaultArgumentKind::Column:
case DefaultArgumentKind::Function:
case DefaultArgumentKind::DSOHandle:
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case DefaultArgumentKind::NAME:
#include "swift/AST/MagicIdentifierKinds.def"
case DefaultArgumentKind::NilLiteral:
case DefaultArgumentKind::EmptyArray:
case DefaultArgumentKind::EmptyDictionary:
Expand All @@ -6344,12 +6341,9 @@ bool ParamDecl::hasCallerSideDefaultExpr() const {
case DefaultArgumentKind::StoredProperty:
case DefaultArgumentKind::Normal:
return false;
case DefaultArgumentKind::File:
case DefaultArgumentKind::FilePath:
case DefaultArgumentKind::Line:
case DefaultArgumentKind::Column:
case DefaultArgumentKind::Function:
case DefaultArgumentKind::DSOHandle:
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case DefaultArgumentKind::NAME:
#include "swift/AST/MagicIdentifierKinds.def"
case DefaultArgumentKind::NilLiteral:
case DefaultArgumentKind::EmptyArray:
case DefaultArgumentKind::EmptyDictionary:
Expand Down Expand Up @@ -6585,12 +6579,9 @@ ParamDecl::getDefaultValueStringRepresentation(
scratch);
}
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";
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case DefaultArgumentKind::NAME: return STRING;
#include "swift/AST/MagicIdentifierKinds.def"
case DefaultArgumentKind::NilLiteral: return "nil";
case DefaultArgumentKind::EmptyArray: return "[]";
case DefaultArgumentKind::EmptyDictionary: return "[:]";
Expand Down
Loading