Skip to content

Commit 2498203

Browse files
authored
Merge pull request #65990 from rintaro/5.9-macros-attributes-rdar107386648
[5.9][Macro] Attributes for freestanding macro expansion
2 parents 41364ab + c342213 commit 2498203

25 files changed

+549
-364
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,8 @@ ERROR(macro_expansion_expr_expected_macro_identifier,PointsToFirstBadToken,
20562056
"expected a macro identifier for a pound literal expression", ())
20572057
ERROR(macro_expansion_decl_expected_macro_identifier,PointsToFirstBadToken,
20582058
"expected a macro identifier for a pound literal declaration", ())
2059+
ERROR(extra_whitespace_macro_expansion_identifier,PointsToFirstBadToken,
2060+
"extraneous whitespace between '#' and macro name is not permitted", ())
20592061

20602062
ERROR(macro_role_attr_expected_kind,PointsToFirstBadToken,
20612063
"expected %select{a freestanding|an attached}0 macro role such as "

include/swift/Parse/Parser.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,15 @@ class Parser {
900900
// Decl Parsing
901901

902902
/// Returns true if parser is at the start of a Swift decl or decl-import.
903-
bool isStartOfSwiftDecl(bool allowPoundIfAttributes = true);
903+
bool isStartOfSwiftDecl(bool allowPoundIfAttributes = true,
904+
bool hadAttrsOrModifiers = false);
904905

905906
/// Returns true if the parser is at the start of a SIL decl.
906907
bool isStartOfSILDecl();
907908

909+
/// Returns true if the parser is at a freestanding macro expansion.
910+
bool isStartOfFreestandingMacroExpansion();
911+
908912
/// Parse the top-level Swift items into the provided vector.
909913
///
910914
/// Each item will be a declaration, statement, or expression.
@@ -1747,6 +1751,16 @@ class Parser {
17471751
DeclNameRef parseDeclNameRef(DeclNameLoc &loc, const Diagnostic &diag,
17481752
DeclNameOptions flags);
17491753

1754+
/// Parse macro expansion.
1755+
///
1756+
/// macro-expansion:
1757+
/// '#' identifier generic-arguments? expr-paren? trailing-closure?
1758+
ParserStatus parseFreestandingMacroExpansion(
1759+
SourceLoc &poundLoc, DeclNameLoc &macroNameLoc, DeclNameRef &macroNameRef,
1760+
SourceLoc &leftAngleLoc, SmallVectorImpl<TypeRepr *> &genericArgs,
1761+
SourceLoc &rightAngleLoc, ArgumentList *&argList, bool isExprBasic,
1762+
const Diagnostic &diag);
1763+
17501764
ParserResult<Expr> parseExprIdentifier();
17511765
Expr *parseExprEditorPlaceholder(Token PlaceholderTok,
17521766
Identifier PlaceholderId);

lib/AST/ASTWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
14821482

14831483
bool shouldSkip(Decl *D) {
14841484
if (!Walker.shouldWalkMacroArgumentsAndExpansion().second &&
1485-
D->isInMacroExpansionInContext())
1485+
D->isInMacroExpansionInContext() && !Walker.Parent.isNull())
14861486
return true;
14871487

14881488
if (auto *VD = dyn_cast<VarDecl>(D)) {

lib/AST/Attr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ OrigDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
856856
auto declLoc = decl->getStartLoc();
857857
auto *mod = decl->getModuleContext();
858858
auto *declFile = mod->getSourceFileContainingLocation(declLoc);
859-
auto *attrFile = mod->getSourceFileContainingLocation(Attr->AtLoc);
859+
auto *attrFile = mod->getSourceFileContainingLocation(Attr->getLocation());
860860
if (!declFile || !attrFile)
861861
return Attr;
862862

lib/AST/Decl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/Decl.h"
1818
#include "swift/AST/ASTContext.h"
1919
#include "swift/AST/ASTMangler.h"
20+
#include "swift/AST/ASTPrinter.h"
2021
#include "swift/AST/ASTWalker.h"
2122
#include "swift/AST/AccessRequests.h"
2223
#include "swift/AST/AccessScope.h"
@@ -10065,6 +10066,13 @@ void swift::simple_display(llvm::raw_ostream &out, const Decl *decl) {
1006510066
typeRepr->print(out);
1006610067
else
1006710068
ext->getSelfNominalTypeDecl()->dumpRef(out);
10069+
} else if (auto med = dyn_cast<MacroExpansionDecl>(decl)) {
10070+
out << '#' << med->getMacroName() << " in ";
10071+
printContext(out, med->getDeclContext());
10072+
if (med->getLoc().isValid()) {
10073+
out << '@';
10074+
med->getLoc().print(out, med->getASTContext().SourceMgr);
10075+
}
1006810076
} else {
1006910077
out << "(unknown decl)";
1007010078
}

lib/ASTGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ add_pure_swift_host_library(swiftASTGen STATIC
3232
SwiftSyntax::SwiftParserDiagnostics
3333
SwiftSyntax::SwiftSyntax
3434
SwiftSyntax::SwiftSyntaxMacros
35+
SwiftSyntax::SwiftSyntaxMacroExpansion
3536
swiftLLVMJSON
3637
)

lib/ASTGen/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ let package = Package(
3737
.product(name: "SwiftOperators", package: "swift-syntax"),
3838
.product(name: "SwiftParser", package: "swift-syntax"),
3939
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
40+
.product(name: "SwiftSyntaxMacroExpansion", package: "swift-syntax"),
4041
"swiftLLVMJSON"
4142
],
4243
path: "Sources/ASTGen",
43-
exclude: ["CMakeLists.txt"],
4444
swiftSettings: swiftSetttings
4545
),
4646
.target(

0 commit comments

Comments
 (0)