Skip to content

Commit 99faa03

Browse files
committed
[NFC] Standardize dump() methods in frontend
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler. This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size. Despite the large number of files and lines affected, this change is NFC.
1 parent 312fefc commit 99faa03

File tree

101 files changed

+407
-344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+407
-344
lines changed

docs/DebuggingTheCompiler.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ debugging press <CTRL>-C on the LLDB prompt.
214214
Note that this only works in Xcode if the PATH variable in the scheme's
215215
environment setting contains the path to the dot tool.
216216

217+
swift/Basic/Debug.h includes macros to help contributors declare these methods
218+
with the proper attributes to ensure they'll be available in the debugger. In
219+
particular, if you see ``SWIFT_DEBUG_DUMP`` in a class declaration, that class
220+
has a ``dump()`` method you can call.
221+
217222
Debugging and Profiling on SIL level
218223
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219224

include/swift/AST/ASTNode.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SWIFT_AST_AST_NODE_H
1919

2020
#include "llvm/ADT/PointerUnion.h"
21+
#include "swift/Basic/Debug.h"
2122
#include "swift/AST/TypeAlignments.h"
2223

2324
namespace llvm {
@@ -62,9 +63,7 @@ namespace swift {
6263
FUNC(Decl)
6364
#undef FUNC
6465

65-
LLVM_ATTRIBUTE_DEPRECATED(
66-
void dump() const LLVM_ATTRIBUTE_USED,
67-
"only for use within the debugger");
66+
SWIFT_DEBUG_DUMP;
6867
void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const;
6968

7069
/// Whether the AST node is implicit.

include/swift/AST/ASTScope.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "swift/AST/NameLookup.h" // for DeclVisibilityKind
3333
#include "swift/AST/SimpleRequest.h"
3434
#include "swift/Basic/Compiler.h"
35+
#include "swift/Basic/Debug.h"
3536
#include "swift/Basic/LLVM.h"
3637
#include "swift/Basic/NullablePtr.h"
3738
#include "swift/Basic/SourceManager.h"
@@ -330,8 +331,7 @@ class ASTScopeImpl {
330331
virtual NullablePtr<const void> addressForPrinting() const;
331332

332333
public:
333-
LLVM_ATTRIBUTE_DEPRECATED(void dump() const LLVM_ATTRIBUTE_USED,
334-
"only for use within the debugger");
334+
SWIFT_DEBUG_DUMP;
335335

336336
void dumpOneScopeMapLocation(std::pair<unsigned, unsigned> lineColumn);
337337

include/swift/AST/AccessScope.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/AST/AttrKind.h"
1717
#include "swift/AST/DeclContext.h"
18+
#include "swift/Basic/Debug.h"
1819
#include "llvm/ADT/PointerIntPair.h"
1920

2021
namespace swift {
@@ -87,7 +88,7 @@ class AccessScope {
8788
return None;
8889
}
8990

90-
void dump() const;
91+
SWIFT_DEBUG_DUMP;
9192
};
9293

9394
} // end namespace swift

include/swift/AST/AnyFunctionRef.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_AST_ANY_FUNCTION_REF_H
1515

1616
#include "swift/Basic/Compiler.h"
17+
#include "swift/Basic/Debug.h"
1718
#include "swift/Basic/LLVM.h"
1819
#include "swift/AST/Decl.h"
1920
#include "swift/AST/Expr.h"
@@ -177,8 +178,7 @@ class AnyFunctionRef {
177178
#pragma warning(push)
178179
#pragma warning(disable: 4996)
179180
#endif
180-
LLVM_ATTRIBUTE_DEPRECATED(void dump() const LLVM_ATTRIBUTE_USED,
181-
"only for use within the debugger") {
181+
SWIFT_DEBUG_DUMP {
182182
if (auto afd = TheFunction.dyn_cast<AbstractFunctionDecl *>()) {
183183
return afd->dump();
184184
}

include/swift/AST/Attr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_ATTR_H
1818
#define SWIFT_ATTR_H
1919

20+
#include "swift/Basic/Debug.h"
2021
#include "swift/Basic/InlineBitfield.h"
2122
#include "swift/Basic/SourceLoc.h"
2223
#include "swift/Basic/UUID.h"
@@ -1583,7 +1584,7 @@ class DeclAttributes {
15831584
/// a declaration is deprecated on all deployment targets, or null otherwise.
15841585
const AvailableAttr *getDeprecated(const ASTContext &ctx) const;
15851586

1586-
void dump(const Decl *D = nullptr) const;
1587+
SWIFT_DEBUG_DUMPER(dump(const Decl *D = nullptr));
15871588
void print(ASTPrinter &Printer, const PrintOptions &Options,
15881589
const Decl *D = nullptr) const;
15891590
static void print(ASTPrinter &Printer, const PrintOptions &Options,

include/swift/AST/CaptureInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_AST_CAPTURE_INFO_H
1414
#define SWIFT_AST_CAPTURE_INFO_H
1515

16+
#include "swift/Basic/Debug.h"
1617
#include "swift/Basic/LLVM.h"
1718
#include "swift/Basic/OptionSet.h"
1819
#include "swift/Basic/SourceLoc.h"
@@ -219,7 +220,7 @@ class CaptureInfo {
219220
return StorageAndFlags.getPointer()->getOpaqueValue();
220221
}
221222

222-
void dump() const;
223+
SWIFT_DEBUG_DUMP;
223224
void print(raw_ostream &OS) const;
224225
};
225226

include/swift/AST/ConcreteDeclRef.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_AST_CONCRETEDECLREF_H
1818
#define SWIFT_AST_CONCRETEDECLREF_H
1919

20+
#include "swift/Basic/Debug.h"
2021
#include "swift/Basic/LLVM.h"
2122
#include "swift/AST/SubstitutionMap.h"
2223
#include "swift/AST/TypeAlignments.h"
@@ -81,8 +82,8 @@ class ConcreteDeclRef {
8182
}
8283

8384
/// Dump a debug representation of this reference.
84-
void dump(raw_ostream &os);
85-
void dump() LLVM_ATTRIBUTE_USED;
85+
void dump(raw_ostream &os) const;
86+
SWIFT_DEBUG_DUMP;
8687
};
8788

8889
} // end namespace swift

include/swift/AST/Decl.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "swift/AST/Witness.h"
3636
#include "swift/Basic/ArrayRefView.h"
3737
#include "swift/Basic/Compiler.h"
38+
#include "swift/Basic/Debug.h"
3839
#include "swift/Basic/InlineBitfield.h"
3940
#include "swift/Basic/NullablePtr.h"
4041
#include "swift/Basic/OptionalEnum.h"
@@ -776,12 +777,8 @@ class alignas(1 << DeclAlignInBits) Decl {
776777

777778
SourceLoc TrailingSemiLoc;
778779

779-
LLVM_ATTRIBUTE_DEPRECATED(
780-
void dump() const LLVM_ATTRIBUTE_USED,
781-
"only for use within the debugger");
782-
LLVM_ATTRIBUTE_DEPRECATED(
783-
void dump(const char *filename) const LLVM_ATTRIBUTE_USED,
784-
"only for use within the debugger");
780+
SWIFT_DEBUG_DUMP;
781+
SWIFT_DEBUG_DUMPER(dump(const char *filename));
785782
void dump(raw_ostream &OS, unsigned Indent = 0) const;
786783

787784
/// Pretty-print the given declaration.
@@ -1202,9 +1199,7 @@ class RequirementRepr {
12021199
return repr->SecondType.getTypeRepr();
12031200
}
12041201

1205-
LLVM_ATTRIBUTE_DEPRECATED(
1206-
void dump() const LLVM_ATTRIBUTE_USED,
1207-
"only for use within the debugger");
1202+
SWIFT_DEBUG_DUMP;
12081203
void print(raw_ostream &OS) const;
12091204
void print(ASTPrinter &Printer) const;
12101205
};
@@ -1374,7 +1369,7 @@ class GenericParamList final :
13741369
GenericParamList *clone(DeclContext *dc) const;
13751370

13761371
void print(raw_ostream &OS) const;
1377-
void dump();
1372+
SWIFT_DEBUG_DUMP;
13781373
};
13791374

13801375
/// A trailing where clause.
@@ -2714,7 +2709,7 @@ class ValueDecl : public Decl {
27142709
void dumpRef(raw_ostream &os) const;
27152710

27162711
/// Dump a reference to the given declaration.
2717-
void dumpRef() const;
2712+
SWIFT_DEBUG_DUMPER(dumpRef());
27182713

27192714
/// Returns true if the declaration is a static member of a type.
27202715
///

include/swift/AST/DeclContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/LookupKinds.h"
2424
#include "swift/AST/ResilienceExpansion.h"
2525
#include "swift/AST/TypeAlignments.h"
26+
#include "swift/Basic/Debug.h"
2627
#include "swift/Basic/LLVM.h"
2728
#include "swift/Basic/STLExtras.h"
2829
#include "swift/Basic/SourceLoc.h"
@@ -597,7 +598,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
597598
/// \returns true if traversal was aborted, false otherwise.
598599
bool walkContext(ASTWalker &Walker);
599600

600-
void dumpContext() const;
601+
SWIFT_DEBUG_DUMPER(dumpContext());
601602
unsigned printContext(llvm::raw_ostream &OS, unsigned indent = 0,
602603
bool onlyAPartialLine = false) const;
603604

0 commit comments

Comments
 (0)