Skip to content

Commit e1e7a3f

Browse files
authored
Merge pull request #25656 from brentdax/file-name-basis
Shorten #file and add #filePath (behind an experimental flag)
2 parents c93d509 + 63ec1cf commit e1e7a3f

27 files changed

+145
-5
lines changed

include/swift/AST/DefaultArgumentKind.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ enum class DefaultArgumentKind : uint8_t {
3838
Inherited,
3939
/// The #file default argument, which is expanded at the call site.
4040
File,
41+
/// The #filePath default argument, which is expanded at the call site.
42+
FilePath,
4143
/// The #line default argument, which is expanded at the call site.
4244
Line,
4345
/// The #column default argument, which is expanded at the call site.

include/swift/AST/Expr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
10401040
class MagicIdentifierLiteralExpr : public LiteralExpr {
10411041
public:
10421042
enum Kind : unsigned {
1043-
File, Line, Column, Function, DSOHandle
1043+
File, FilePath, Line, Column, Function, DSOHandle
10441044
};
10451045
private:
10461046
SourceLoc Loc;
@@ -1067,6 +1067,7 @@ class MagicIdentifierLiteralExpr : public LiteralExpr {
10671067
bool isString() const {
10681068
switch (getKind()) {
10691069
case File:
1070+
case FilePath:
10701071
case Function:
10711072
return true;
10721073
case Line:

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ namespace swift {
9494
/// when using RequireExplicitAvailability.
9595
std::string RequireExplicitAvailabilityTarget;
9696

97+
/// If false, '#file' evaluates to the full path rather than a
98+
/// human-readable string.
99+
bool EnableConcisePoundFile = false;
100+
97101
///
98102
/// Support for alternate usage modes
99103
///

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ def enable_experimental_differentiable_programming : Flag<["-"], "enable-experim
454454
Flags<[FrontendOption]>,
455455
HelpText<"Enable experimental differentiable programming features">;
456456

457+
def enable_experimental_concise_pound_file : Flag<["-"],
458+
"enable-experimental-concise-pound-file">,
459+
Flags<[FrontendOption]>,
460+
HelpText<"Enable experimental concise '#file' identifier and '#filePath' alternative">;
461+
457462
// Diagnostic control options
458463
def suppress_warnings : Flag<["-"], "suppress-warnings">,
459464
Flags<[FrontendOption]>,

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ static StringRef getDefaultArgumentKindString(DefaultArgumentKind value) {
300300
case DefaultArgumentKind::Column: return "#column";
301301
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
302302
case DefaultArgumentKind::File: return "#file";
303+
case DefaultArgumentKind::FilePath: return "#filePath";
303304
case DefaultArgumentKind::Function: return "#function";
304305
case DefaultArgumentKind::Inherited: return "inherited";
305306
case DefaultArgumentKind::Line: return "#line";
@@ -316,6 +317,7 @@ static StringRef
316317
getMagicIdentifierLiteralExprKindString(MagicIdentifierLiteralExpr::Kind value) {
317318
switch (value) {
318319
case MagicIdentifierLiteralExpr::File: return "#file";
320+
case MagicIdentifierLiteralExpr::FilePath: return "#filePath";
319321
case MagicIdentifierLiteralExpr::Function: return "#function";
320322
case MagicIdentifierLiteralExpr::Line: return "#line";
321323
case MagicIdentifierLiteralExpr::Column: return "#column";

lib/AST/Decl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6071,6 +6071,7 @@ bool ParamDecl::hasDefaultExpr() const {
60716071
return false;
60726072
case DefaultArgumentKind::Normal:
60736073
case DefaultArgumentKind::File:
6074+
case DefaultArgumentKind::FilePath:
60746075
case DefaultArgumentKind::Line:
60756076
case DefaultArgumentKind::Column:
60766077
case DefaultArgumentKind::Function:
@@ -6093,6 +6094,7 @@ bool ParamDecl::hasCallerSideDefaultExpr() const {
60936094
case DefaultArgumentKind::Normal:
60946095
return false;
60956096
case DefaultArgumentKind::File:
6097+
case DefaultArgumentKind::FilePath:
60966098
case DefaultArgumentKind::Line:
60976099
case DefaultArgumentKind::Column:
60986100
case DefaultArgumentKind::Function:
@@ -6402,6 +6404,7 @@ ParamDecl::getDefaultValueStringRepresentation(
64026404
}
64036405
case DefaultArgumentKind::Inherited: return "super";
64046406
case DefaultArgumentKind::File: return "#file";
6407+
case DefaultArgumentKind::FilePath: return "#filePath";
64056408
case DefaultArgumentKind::Line: return "#line";
64066409
case DefaultArgumentKind::Column: return "#column";
64076410
case DefaultArgumentKind::Function: return "#function";

lib/Driver/ToolChains.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
238238
inputArgs.AddLastArg(arguments, options::OPT_enable_astscope_lookup);
239239
inputArgs.AddLastArg(arguments, options::OPT_disable_astscope_lookup);
240240
inputArgs.AddLastArg(arguments, options::OPT_disable_parser_lookup);
241+
inputArgs.AddLastArg(arguments,
242+
options::OPT_enable_experimental_concise_pound_file);
241243

242244
// Pass on any build config options
243245
inputArgs.AddAllArgs(arguments, options::OPT_D);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
453453
Opts.OptimizationRemarkMissedPattern =
454454
generateOptimizationRemarkRegex(Diags, Args, A);
455455

456+
Opts.EnableConcisePoundFile =
457+
Args.hasArg(OPT_enable_experimental_concise_pound_file);
458+
456459
llvm::Triple Target = Opts.Target;
457460
StringRef TargetArg;
458461
if (const Arg *A = Args.getLastArg(OPT_target)) {

lib/IDE/CodeCompletion.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
22442244
return !includeDefaultArgs;
22452245

22462246
case DefaultArgumentKind::File:
2247+
case DefaultArgumentKind::FilePath:
22472248
case DefaultArgumentKind::Line:
22482249
case DefaultArgumentKind::Column:
22492250
case DefaultArgumentKind::Function:
@@ -3653,6 +3654,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
36533654
CodeCompletionLiteralKind::StringLiteral, "String");
36543655
addFromProto("#file", CodeCompletionKeywordKind::pound_file,
36553656
CodeCompletionLiteralKind::StringLiteral, "String");
3657+
if (Ctx.LangOpts.EnableConcisePoundFile) {
3658+
addFromProto("#filePath", CodeCompletionKeywordKind::pound_file,
3659+
CodeCompletionLiteralKind::StringLiteral, "String");
3660+
}
36563661
addFromProto("#line", CodeCompletionKeywordKind::pound_line,
36573662
CodeCompletionLiteralKind::IntegerLiteral, "Int");
36583663
addFromProto("#column", CodeCompletionKeywordKind::pound_column,

lib/Parse/ParseExpr.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,8 @@ getMagicIdentifierLiteralKind(tok Kind) {
10141014
case tok::kw___FILE__:
10151015
case tok::pound_file:
10161016
return MagicIdentifierLiteralExpr::Kind::File;
1017+
case tok::pound_filePath:
1018+
return MagicIdentifierLiteralExpr::Kind::FilePath;
10171019
case tok::kw___FUNCTION__:
10181020
case tok::pound_function:
10191021
return MagicIdentifierLiteralExpr::Kind::Function;
@@ -1446,6 +1448,15 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
14461448
.fixItReplace(Tok.getLoc(), replacement);
14471449
LLVM_FALLTHROUGH;
14481450
}
1451+
1452+
case tok::pound_filePath:
1453+
// Check twice because of fallthrough--this is ugly but temporary.
1454+
if (Tok.is(tok::pound_filePath) && !Context.LangOpts.EnableConcisePoundFile)
1455+
diagnose(Tok.getLoc(), diag::unknown_pound_expr, "filePath");
1456+
// Continue since we actually do know how to handle it. This avoids extra
1457+
// diagnostics.
1458+
LLVM_FALLTHROUGH;
1459+
14491460
case tok::pound_column:
14501461
case tok::pound_file:
14511462
case tok::pound_function:
@@ -1455,6 +1466,7 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
14551466
switch (Tok.getKind()) {
14561467
case tok::pound_column: SKind = SyntaxKind::PoundColumnExpr; break;
14571468
case tok::pound_file: SKind = SyntaxKind::PoundFileExpr; break;
1469+
case tok::pound_filePath: SKind = SyntaxKind::PoundFilePathExpr; break;
14581470
case tok::pound_function: SKind = SyntaxKind::PoundFunctionExpr; break;
14591471
// FIXME: #line was renamed to #sourceLocation
14601472
case tok::pound_line: SKind = SyntaxKind::PoundLineExpr; break;

0 commit comments

Comments
 (0)