Skip to content

Commit ea848ae

Browse files
committed
Rename C++ macro 'defer' -> 'SWIFT_DEFER'
In C++ we can't have nice things. The macro name 'defer' collided with use of 'defer' in the Tokens.def file and we were already doing horrible workarounds in a couple of places to allow them to be included into the same file. So use a less awesome but more robust name (thanks to Joe for suggesting SWIFT_DEFER). Incidentally, sort a bunch of #inlcudes.
1 parent c30286a commit ea848ae

13 files changed

+55
-56
lines changed

include/swift/Basic/Defer.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// This file defines a 'defer' macro for performing a cleanup on any exit out
14-
// of a scope.
13+
// This file defines a 'SWIFT_DEFER' macro for performing a cleanup on any exit
14+
// out of a scope.
1515
//
1616
//===----------------------------------------------------------------------===//
1717

@@ -45,17 +45,15 @@ namespace swift {
4545
#define DEFER_CONCAT_IMPL(x, y) x##y
4646
#define DEFER_MACRO_CONCAT(x, y) DEFER_CONCAT_IMPL(x, y)
4747

48-
#define defer_impl \
49-
auto DEFER_MACRO_CONCAT(defer_func, __COUNTER__) = \
50-
::swift::detail::DeferTask() + [&]()
51-
5248
/// This macro is used to register a function / lambda to be run on exit from a
5349
/// scope. Its typical use looks like:
5450
///
55-
/// defer {
51+
/// SWIFT_DEFER {
5652
/// stuff
5753
/// };
5854
///
59-
#define defer defer_impl
55+
#define SWIFT_DEFER \
56+
auto DEFER_MACRO_CONCAT(defer_func, __COUNTER__) = \
57+
::swift::detail::DeferTask() + [&]()
6058

6159
#endif // SWIFT_BASIC_DEFER_H

lib/AST/ASTPrinter.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#include "swift/AST/ArchetypeBuilder.h"
18-
#include "swift/AST/ASTContext.h"
1917
#include "swift/AST/ASTPrinter.h"
18+
#include "swift/AST/ASTContext.h"
2019
#include "swift/AST/ASTVisitor.h"
20+
#include "swift/AST/ArchetypeBuilder.h"
2121
#include "swift/AST/Attr.h"
2222
#include "swift/AST/Decl.h"
2323
#include "swift/AST/Expr.h"
@@ -29,13 +29,13 @@
2929
#include "swift/AST/TypeVisitor.h"
3030
#include "swift/AST/TypeWalker.h"
3131
#include "swift/AST/Types.h"
32+
#include "swift/Basic/Defer.h"
3233
#include "swift/Basic/Fallthrough.h"
3334
#include "swift/Basic/PrimitiveParsing.h"
3435
#include "swift/Basic/STLExtras.h"
3536
#include "swift/Basic/StringExtras.h"
36-
#include "swift/Parse/Lexer.h"
37-
#include "swift/Basic/Defer.h" // Must come after include of Tokens.def.
3837
#include "swift/Config.h"
38+
#include "swift/Parse/Lexer.h"
3939
#include "swift/Sema/IDETypeChecking.h"
4040
#include "swift/Strings.h"
4141
#include "clang/AST/ASTContext.h"
@@ -44,8 +44,8 @@
4444
#include "clang/Basic/Module.h"
4545
#include "llvm/ADT/StringSwitch.h"
4646
#include "llvm/Support/ConvertUTF.h"
47-
#include "llvm/Support/raw_ostream.h"
4847
#include "llvm/Support/SaveAndRestore.h"
48+
#include "llvm/Support/raw_ostream.h"
4949
#include <algorithm>
5050
#include <queue>
5151

@@ -975,9 +975,6 @@ void ASTPrinter::printName(Identifier Name, PrintNameContext Context) {
975975
printNamePost(Context);
976976
}
977977

978-
// FIXME: Restore defer after Tokens.def.
979-
#define defer defer_impl
980-
981978
void StreamPrinter::printText(StringRef Text) {
982979
OS << Text;
983980
}
@@ -1510,7 +1507,7 @@ void PrintAST::printWhereClause(ArrayRef<RequirementRepr> requirements) {
15101507
}
15111508

15121509
Printer.callPrintStructurePre(PrintStructureKind::GenericRequirement);
1513-
defer {
1510+
SWIFT_DEFER {
15141511
Printer.printStructurePost(PrintStructureKind::GenericRequirement);
15151512
};
15161513

@@ -2508,7 +2505,7 @@ void PrintAST::visitParamDecl(ParamDecl *decl) {
25082505
void PrintAST::printOneParameter(const ParamDecl *param, bool Curried,
25092506
bool ArgNameIsAPIByDefault) {
25102507
Printer.callPrintStructurePre(PrintStructureKind::FunctionParameter, param);
2511-
defer {
2508+
SWIFT_DEFER {
25122509
Printer.printStructurePost(PrintStructureKind::FunctionParameter, param);
25132510
};
25142511

@@ -3398,7 +3395,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
33983395

33993396
void visit(Type T) {
34003397
Printer.printTypePre(TypeLoc::withoutLoc(T));
3401-
defer { Printer.printTypePost(TypeLoc::withoutLoc(T)); };
3398+
SWIFT_DEFER { Printer.printTypePost(TypeLoc::withoutLoc(T)); };
34023399

34033400
// If we have an alternate name for this type, use it.
34043401
if (Options.AlternativeTypeNames) {
@@ -3505,7 +3502,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
35053502

35063503
void visitTupleType(TupleType *T) {
35073504
Printer.callPrintStructurePre(PrintStructureKind::TupleType);
3508-
defer { Printer.printStructurePost(PrintStructureKind::TupleType); };
3505+
SWIFT_DEFER { Printer.printStructurePost(PrintStructureKind::TupleType); };
35093506

35103507
Printer << "(";
35113508

@@ -3517,7 +3514,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
35173514
Type EltType = TD.getType();
35183515

35193516
Printer.callPrintStructurePre(PrintStructureKind::TupleElement);
3520-
defer { Printer.printStructurePost(PrintStructureKind::TupleElement); };
3517+
SWIFT_DEFER {
3518+
Printer.printStructurePost(PrintStructureKind::TupleElement);
3519+
};
35213520

35223521
if (TD.hasName()) {
35233522
Printer.printName(TD.getName(), PrintNameContext::TupleElement);
@@ -3730,7 +3729,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37303729

37313730
void visitFunctionType(FunctionType *T) {
37323731
Printer.callPrintStructurePre(PrintStructureKind::FunctionType);
3733-
defer { Printer.printStructurePost(PrintStructureKind::FunctionType); };
3732+
SWIFT_DEFER {
3733+
Printer.printStructurePost(PrintStructureKind::FunctionType);
3734+
};
37343735

37353736
printFunctionExtInfo(T->getExtInfo());
37363737
printWithParensIfNotSimple(T->getInput());
@@ -3747,7 +3748,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37473748

37483749
void visitPolymorphicFunctionType(PolymorphicFunctionType *T) {
37493750
Printer.callPrintStructurePre(PrintStructureKind::FunctionType);
3750-
defer { Printer.printStructurePost(PrintStructureKind::FunctionType); };
3751+
SWIFT_DEFER {
3752+
Printer.printStructurePost(PrintStructureKind::FunctionType);
3753+
};
37513754

37523755
printFunctionExtInfo(T->getExtInfo());
37533756
printGenericParams(&T->getGenericParams());
@@ -3901,7 +3904,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
39013904

39023905
void visitGenericFunctionType(GenericFunctionType *T) {
39033906
Printer.callPrintStructurePre(PrintStructureKind::FunctionType);
3904-
defer { Printer.printStructurePost(PrintStructureKind::FunctionType); };
3907+
SWIFT_DEFER {
3908+
Printer.printStructurePost(PrintStructureKind::FunctionType);
3909+
};
39053910

39063911
printFunctionExtInfo(T->getExtInfo());
39073912
printGenericSignature(T->getGenericParams(), T->getRequirements());

lib/AST/Attr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options)
342342
}
343343

344344
Printer.callPrintStructurePre(PrintStructureKind::BuiltinAttribute);
345-
defer { Printer.printStructurePost(PrintStructureKind::BuiltinAttribute); };
345+
SWIFT_DEFER {
346+
Printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
347+
};
346348

347349
switch (getKind()) {
348350
case DAK_Semantics:

lib/AST/TypeRepr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void TypeRepr::print(ASTPrinter &Printer, const PrintOptions &Opts) const {
9898
// The type part of a NamedTypeRepr will get the callback.
9999
if (!isa<NamedTypeRepr>(this))
100100
Printer.printTypePre(TypeLoc(const_cast<TypeRepr *>(this)));
101-
defer {
101+
SWIFT_DEFER {
102102
if (!isa<NamedTypeRepr>(this))
103103
Printer.printTypePost(TypeLoc(const_cast<TypeRepr *>(this)));
104104
};
@@ -397,7 +397,7 @@ TupleTypeRepr *TupleTypeRepr::create(ASTContext &C,
397397
void TupleTypeRepr::printImpl(ASTPrinter &Printer,
398398
const PrintOptions &Opts) const {
399399
Printer.callPrintStructurePre(PrintStructureKind::TupleType);
400-
defer { Printer.printStructurePost(PrintStructureKind::TupleType); };
400+
SWIFT_DEFER { Printer.printStructurePost(PrintStructureKind::TupleType); };
401401

402402
Printer << "(";
403403

lib/IDE/CodeCompletion.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,38 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/IDE/CodeCompletion.h"
14-
#include "swift/IDE/CodeCompletionCache.h"
15-
#include "swift/IDE/Utils.h"
1614
#include "CodeCompletionResultBuilder.h"
1715
#include "swift/AST/ASTPrinter.h"
1816
#include "swift/AST/ASTWalker.h"
1917
#include "swift/AST/Comment.h"
2018
#include "swift/AST/LazyResolver.h"
2119
#include "swift/AST/NameLookup.h"
2220
#include "swift/AST/USRGeneration.h"
21+
#include "swift/Basic/Defer.h"
2322
#include "swift/Basic/Fallthrough.h"
2423
#include "swift/Basic/LLVM.h"
2524
#include "swift/ClangImporter/ClangImporter.h"
2625
#include "swift/ClangImporter/ClangModule.h"
26+
#include "swift/IDE/CodeCompletionCache.h"
27+
#include "swift/IDE/Utils.h"
2728
#include "swift/Parse/CodeCompletionCallbacks.h"
2829
#include "swift/Sema/IDETypeChecking.h"
29-
#include "swift/Basic/Defer.h" // must be included after Tokens.def.
3030
#include "swift/Subsystems.h"
31-
#include "llvm/ADT/SmallSet.h"
32-
#include "llvm/ADT/SmallString.h"
33-
#include "llvm/ADT/StringRef.h"
34-
#include "llvm/Support/raw_ostream.h"
35-
#include "llvm/Support/SaveAndRestore.h"
3631
#include "clang/AST/ASTContext.h"
3732
#include "clang/AST/Attr.h"
3833
#include "clang/AST/Comment.h"
3934
#include "clang/AST/CommentVisitor.h"
4035
#include "clang/AST/Decl.h"
4136
#include "clang/Basic/Module.h"
4237
#include "clang/Index/USRGeneration.h"
38+
#include "llvm/ADT/SmallSet.h"
39+
#include "llvm/ADT/SmallString.h"
40+
#include "llvm/ADT/StringRef.h"
41+
#include "llvm/Support/SaveAndRestore.h"
42+
#include "llvm/Support/raw_ostream.h"
4343
#include <algorithm>
4444
#include <string>
4545

46-
#undef defer // for Tokens.def; use defer_impl instead.
47-
4846
using namespace swift;
4947
using namespace ide;
5048

@@ -3259,7 +3257,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
32593257
CodeCompletionExpr CCE((SourceRange()));
32603258
sequence.back() = &CCE;
32613259

3262-
defer_impl {
3260+
SWIFT_DEFER {
32633261
// Reset sequence.
32643262
SE->setElement(SE->getNumElements() - 1, nullptr);
32653263
SE->setElement(SE->getNumElements() - 2, nullptr);

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4073,7 +4073,7 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
40734073

40744074
// No matter what error path we take, make sure the
40754075
// PatternBindingDecl/TopLevel code block are added.
4076-
defer {
4076+
SWIFT_DEFER {
40774077
// If we didn't parse any patterns, don't create the pattern binding decl.
40784078
if (PBDEntries.empty())
40794079
return;

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ void swift::maybeAddAccessorsToVariable(VarDecl *var, TypeChecker &TC) {
17021702
assert(!var->getBehavior()->Conformance.hasValue());
17031703

17041704
// The property should be considered computed by the time we're through.
1705-
defer {
1705+
SWIFT_DEFER {
17061706
assert(!var->hasStorage() && "behavior var was not made computed");
17071707
};
17081708

lib/Sema/IterativeTypeChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void IterativeTypeChecker::satisfy(TypeCheckRequest request) {
8484

8585
// Add this request to the stack of active requests.
8686
ActiveRequests.push_back(request);
87-
defer { ActiveRequests.pop_back(); };
87+
SWIFT_DEFER { ActiveRequests.pop_back(); };
8888

8989
while (true) {
9090
// Process this requirement, enumerating dependencies if anything else needs

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ bool swift::diagnoseArgumentLabelError(TypeChecker &TC, const Expr *expr,
849849
llvm::SmallString<16> str;
850850
// If the diagnostic is local, flush it before returning.
851851
// This makes sure it's emitted before 'str' is destroyed.
852-
defer { diagOpt.reset(); };
852+
SWIFT_DEFER { diagOpt.reset(); };
853853

854854
if (newNames[0].empty()) {
855855
// This is probably a conversion from a value of labeled tuple type to

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class FindCapturedVars : public ASTWalker {
270270
}
271271
// Recursively check the transitive captures.
272272
capturePath.push_back(func);
273-
defer { capturePath.pop_back(); };
273+
SWIFT_DEFER { capturePath.pop_back(); };
274274
for (auto capture : func->getCaptureInfo().getCaptures())
275275
if (!validateForwardCapture(capture.getDecl()))
276276
return false;

0 commit comments

Comments
 (0)