31
31
#include " swift/Basic/Fallthrough.h"
32
32
#include " swift/Basic/PrimitiveParsing.h"
33
33
#include " swift/Basic/STLExtras.h"
34
+ #include " swift/Parse/Lexer.h"
34
35
#include " swift/Config.h"
35
36
#include " swift/Sema/CodeCompletionTypeChecking.h"
36
37
#include " swift/Strings.h"
@@ -292,6 +293,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
292
293
Printer.printDeclNameEndLoc (decl);
293
294
}
294
295
296
+ void printSourceRange (CharSourceRange Range, ASTContext &Ctx) {
297
+ Printer << Ctx.SourceMgr .extractText (Range);
298
+ }
299
+
295
300
void printClangDocumentationComment (const clang::Decl *D) {
296
301
const auto &ClangContext = D->getASTContext ();
297
302
const clang::RawComment *RC = ClangContext.getRawCommentForAnyRedecl (D);
@@ -1411,13 +1416,20 @@ void PrintAST::visitEnumDecl(EnumDecl *decl) {
1411
1416
printDocumentationComment (decl);
1412
1417
printAttributes (decl);
1413
1418
printAccessibility (decl);
1414
- if (!Options.SkipIntroducerKeywords )
1415
- Printer << " enum " ;
1416
- recordDeclLoc (decl,
1417
- [&]{
1418
- printNominalDeclName (decl);
1419
- });
1420
- printInherited (decl);
1419
+
1420
+ if (Options.PrintOriginalSourceText && decl->getStartLoc ().isValid ()) {
1421
+ ASTContext &Ctx = decl->getASTContext ();
1422
+ printSourceRange (CharSourceRange (Ctx.SourceMgr , decl->getStartLoc (),
1423
+ decl->getBraces ().Start .getAdvancedLoc (-1 )), Ctx);
1424
+ } else {
1425
+ if (!Options.SkipIntroducerKeywords )
1426
+ Printer << " enum " ;
1427
+ recordDeclLoc (decl,
1428
+ [&]{
1429
+ printNominalDeclName (decl);
1430
+ });
1431
+ printInherited (decl);
1432
+ }
1421
1433
if (Options.TypeDefinitions ) {
1422
1434
printMembersOfDecl (decl);
1423
1435
}
@@ -1427,13 +1439,20 @@ void PrintAST::visitStructDecl(StructDecl *decl) {
1427
1439
printDocumentationComment (decl);
1428
1440
printAttributes (decl);
1429
1441
printAccessibility (decl);
1430
- if (!Options.SkipIntroducerKeywords )
1431
- Printer << " struct " ;
1432
- recordDeclLoc (decl,
1433
- [&]{
1434
- printNominalDeclName (decl);
1435
- });
1436
- printInherited (decl);
1442
+
1443
+ if (Options.PrintOriginalSourceText && decl->getStartLoc ().isValid ()) {
1444
+ ASTContext &Ctx = decl->getASTContext ();
1445
+ printSourceRange (CharSourceRange (Ctx.SourceMgr , decl->getStartLoc (),
1446
+ decl->getBraces ().Start .getAdvancedLoc (-1 )), Ctx);
1447
+ } else {
1448
+ if (!Options.SkipIntroducerKeywords )
1449
+ Printer << " struct " ;
1450
+ recordDeclLoc (decl,
1451
+ [&]{
1452
+ printNominalDeclName (decl);
1453
+ });
1454
+ printInherited (decl);
1455
+ }
1437
1456
if (Options.TypeDefinitions ) {
1438
1457
printMembersOfDecl (decl);
1439
1458
}
@@ -1443,14 +1462,21 @@ void PrintAST::visitClassDecl(ClassDecl *decl) {
1443
1462
printDocumentationComment (decl);
1444
1463
printAttributes (decl);
1445
1464
printAccessibility (decl);
1446
- if (!Options.SkipIntroducerKeywords )
1447
- Printer << " class " ;
1448
- recordDeclLoc (decl,
1449
- [&]{
1450
- printNominalDeclName (decl);
1451
- });
1452
1465
1453
- printInherited (decl);
1466
+ if (Options.PrintOriginalSourceText && decl->getStartLoc ().isValid ()) {
1467
+ ASTContext &Ctx = decl->getASTContext ();
1468
+ printSourceRange (CharSourceRange (Ctx.SourceMgr , decl->getStartLoc (),
1469
+ decl->getBraces ().Start .getAdvancedLoc (-1 )), Ctx);
1470
+ } else {
1471
+ if (!Options.SkipIntroducerKeywords )
1472
+ Printer << " class " ;
1473
+ recordDeclLoc (decl,
1474
+ [&]{
1475
+ printNominalDeclName (decl);
1476
+ });
1477
+
1478
+ printInherited (decl);
1479
+ }
1454
1480
1455
1481
if (Options.TypeDefinitions ) {
1456
1482
printMembersOfDecl (decl);
@@ -1461,30 +1487,37 @@ void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
1461
1487
printDocumentationComment (decl);
1462
1488
printAttributes (decl);
1463
1489
printAccessibility (decl);
1464
- if (!Options.SkipIntroducerKeywords )
1465
- Printer << " protocol " ;
1466
- recordDeclLoc (decl,
1467
- [&]{
1468
- printNominalDeclName (decl);
1469
- });
1470
1490
1471
- // Figure out whether we need an explicit 'class' in the inheritance.
1472
- bool explicitClass = false ;
1473
- if (decl->requiresClass () && !decl->isObjC ()) {
1474
- bool inheritsRequiresClass = false ;
1475
- for (auto proto : decl->getLocalProtocols (
1476
- ConformanceLookupKind::OnlyExplicit)) {
1477
- if (proto->requiresClass ()) {
1478
- inheritsRequiresClass = true ;
1479
- break ;
1491
+ if (Options.PrintOriginalSourceText && decl->getStartLoc ().isValid ()) {
1492
+ ASTContext &Ctx = decl->getASTContext ();
1493
+ printSourceRange (CharSourceRange (Ctx.SourceMgr , decl->getStartLoc (),
1494
+ decl->getBraces ().Start .getAdvancedLoc (-1 )), Ctx);
1495
+ } else {
1496
+ if (!Options.SkipIntroducerKeywords )
1497
+ Printer << " protocol " ;
1498
+ recordDeclLoc (decl,
1499
+ [&]{
1500
+ printNominalDeclName (decl);
1501
+ });
1502
+
1503
+ // Figure out whether we need an explicit 'class' in the inheritance.
1504
+ bool explicitClass = false ;
1505
+ if (decl->requiresClass () && !decl->isObjC ()) {
1506
+ bool inheritsRequiresClass = false ;
1507
+ for (auto proto : decl->getLocalProtocols (
1508
+ ConformanceLookupKind::OnlyExplicit)) {
1509
+ if (proto->requiresClass ()) {
1510
+ inheritsRequiresClass = true ;
1511
+ break ;
1512
+ }
1480
1513
}
1514
+
1515
+ if (!inheritsRequiresClass)
1516
+ explicitClass = true ;
1481
1517
}
1482
1518
1483
- if (!inheritsRequiresClass)
1484
- explicitClass = true ;
1519
+ printInherited (decl, explicitClass);
1485
1520
}
1486
-
1487
- printInherited (decl, explicitClass);
1488
1521
if (Options.TypeDefinitions ) {
1489
1522
printMembersOfDecl (decl);
1490
1523
}
@@ -1791,36 +1824,52 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
1791
1824
printDocumentationComment (decl);
1792
1825
printAttributes (decl);
1793
1826
printAccessibility (decl);
1794
- if (!Options.SkipIntroducerKeywords ) {
1795
- if (decl->isStatic () && !decl->isOperator ())
1796
- printStaticKeyword (decl->getCorrectStaticSpelling ());
1797
- if (decl->isMutating () && !decl->getAttrs ().hasAttribute <MutatingAttr>())
1798
- Printer << " mutating " ;
1799
- Printer << " func " ;
1800
- }
1801
- recordDeclLoc (decl,
1802
- [&]{
1803
- if (!decl->hasName ())
1804
- Printer << " <anonymous>" ;
1805
- else
1806
- Printer.printName (decl->getName ());
1807
- if (decl->isGeneric ()) {
1808
- printGenericParams (decl->getGenericParams ());
1809
- }
1810
1827
1811
- printFunctionParameters (decl);
1812
- });
1828
+ if (Options.PrintOriginalSourceText && decl->getStartLoc ().isValid ()) {
1829
+ ASTContext &Ctx = decl->getASTContext ();
1830
+ SourceLoc StartLoc = decl->getStartLoc ();
1831
+ SourceLoc EndLoc;
1832
+ if (!decl->getBodyResultTypeLoc ().isNull ()) {
1833
+ EndLoc = decl->getBodyResultTypeLoc ().getSourceRange ().End ;
1834
+ } else {
1835
+ EndLoc = decl->getSignatureSourceRange ().End ;
1836
+ }
1837
+ CharSourceRange Range =
1838
+ Lexer::getCharSourceRangeFromSourceRange (Ctx.SourceMgr ,
1839
+ SourceRange (StartLoc, EndLoc));
1840
+ printSourceRange (Range, Ctx);
1841
+ } else {
1842
+ if (!Options.SkipIntroducerKeywords ) {
1843
+ if (decl->isStatic () && !decl->isOperator ())
1844
+ printStaticKeyword (decl->getCorrectStaticSpelling ());
1845
+ if (decl->isMutating () && !decl->getAttrs ().hasAttribute <MutatingAttr>())
1846
+ Printer << " mutating " ;
1847
+ Printer << " func " ;
1848
+ }
1849
+ recordDeclLoc (decl,
1850
+ [&]{
1851
+ if (!decl->hasName ())
1852
+ Printer << " <anonymous>" ;
1853
+ else
1854
+ Printer.printName (decl->getName ());
1855
+ if (decl->isGeneric ()) {
1856
+ printGenericParams (decl->getGenericParams ());
1857
+ }
1813
1858
1814
- auto &Context = decl->getASTContext ();
1815
- Type ResultTy = decl->getResultType ();
1816
- if (ResultTy && !ResultTy->isEqual (TupleType::getEmpty (Context))) {
1817
- Printer << " -> " ;
1818
- if (Options.pTransformer ) {
1819
- ResultTy = Options.pTransformer ->transformByName (ResultTy);
1820
- PrintOptions FreshOptions;
1821
- ResultTy->print (Printer, FreshOptions);
1822
- } else
1823
- ResultTy->print (Printer, Options);
1859
+ printFunctionParameters (decl);
1860
+ });
1861
+
1862
+ auto &Context = decl->getASTContext ();
1863
+ Type ResultTy = decl->getResultType ();
1864
+ if (ResultTy && !ResultTy->isEqual (TupleType::getEmpty (Context))) {
1865
+ Printer << " -> " ;
1866
+ if (Options.pTransformer ) {
1867
+ ResultTy = Options.pTransformer ->transformByName (ResultTy);
1868
+ PrintOptions FreshOptions;
1869
+ ResultTy->print (Printer, FreshOptions);
1870
+ } else
1871
+ ResultTy->print (Printer, Options);
1872
+ }
1824
1873
}
1825
1874
1826
1875
if (!Options.FunctionDefinitions || !decl->getBody ()) {
0 commit comments