13
13
#include " DocFormat.h"
14
14
#include " Serialization.h"
15
15
#include " SourceInfoFormat.h"
16
- #include " swift/Basic/Defer.h"
17
16
#include " swift/AST/ASTContext.h"
18
17
#include " swift/AST/ASTWalker.h"
19
18
#include " swift/AST/DiagnosticsCommon.h"
20
19
#include " swift/AST/Module.h"
21
20
#include " swift/AST/ParameterList.h"
22
21
#include " swift/AST/SourceFile.h"
23
22
#include " swift/AST/USRGeneration.h"
23
+ #include " swift/Basic/Defer.h"
24
24
#include " swift/Basic/SourceManager.h"
25
+ #include " llvm/ADT/StringSet.h"
25
26
#include " llvm/Support/DJB.h"
26
27
#include " llvm/Support/EndianStream.h"
27
28
#include " llvm/Support/OnDiskHashTable.h"
@@ -546,25 +547,29 @@ class USRTableInfo {
546
547
out << key;
547
548
}
548
549
549
- void EmitData (raw_ostream &out, key_type_ref key, data_type_ref data, unsigned len) {
550
+ void EmitData (raw_ostream &out, key_type_ref key, data_type_ref data,
551
+ unsigned len) {
550
552
endian::Writer writer (out, little);
551
553
writer.write <uint32_t >(data);
552
554
}
553
555
};
554
556
555
557
class DeclUSRsTableWriter {
556
- llvm::StringMap< uint32_t > USRMap ;
558
+ llvm::StringSet<> USRs ;
557
559
llvm::OnDiskChainedHashTableGenerator<USRTableInfo> generator;
558
- uint32_t CurId = 0 ;
559
560
public:
560
- uint32_t peekNextId () const { return CurId; }
561
- Optional<uint32_t > getNewUSRID (StringRef USR) {
562
- if (USRMap.find (USR) == USRMap.end ()) {
563
- generator.insert (USRMap.insert (std::make_pair (USR, CurId)).first ->getKey (), CurId);
564
- ++CurId;
565
- return USRMap.find (USR)->second ;
566
- }
567
- return None;
561
+ uint32_t peekNextId () const { return USRs.size (); }
562
+ Optional<uint32_t > getNewUSRId (StringRef USR) {
563
+ // Attempt to insert the USR into the StringSet.
564
+ auto It = USRs.insert (USR);
565
+ // If the USR exists in the StringSet, return None.
566
+ if (!It.second )
567
+ return None;
568
+ auto Id = USRs.size () - 1 ;
569
+ // We have to insert the USR from the StringSet because it's where the
570
+ // memory is owned.
571
+ generator.insert (It.first ->getKey (), Id);
572
+ return Id;
568
573
}
569
574
void emitUSRsRecord (llvm::BitstreamWriter &out) {
570
575
decl_locs_block::DeclUSRSLayout USRsList (out);
@@ -606,10 +611,11 @@ struct BasicDeclLocsTableWriter : public ASTWalker {
606
611
DeclUSRsTableWriter &USRWriter;
607
612
StringWriter &FWriter;
608
613
BasicDeclLocsTableWriter (DeclUSRsTableWriter &USRWriter,
609
- StringWriter &FWriter): USRWriter(USRWriter), FWriter(FWriter) {}
614
+ StringWriter &FWriter): USRWriter(USRWriter),
615
+ FWriter (FWriter) {}
610
616
611
- std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override { return { false , S }; }
612
- std::pair<bool , Expr *> walkToExprPre (Expr *E) override { return { false , E }; }
617
+ std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override { return { false , S };}
618
+ std::pair<bool , Expr *> walkToExprPre (Expr *E) override { return { false , E };}
613
619
bool walkToTypeLocPre (TypeLoc &TL) override { return false ; }
614
620
bool walkToTypeReprPre (TypeRepr *T) override { return false ; }
615
621
bool walkToParameterListPre (ParameterList *PL) override { return false ; }
@@ -618,8 +624,8 @@ struct BasicDeclLocsTableWriter : public ASTWalker {
618
624
llvm::raw_svector_ostream out (Buffer);
619
625
endian::Writer writer (out, little);
620
626
writer.write <uint32_t >(data.SourceFileOffset );
621
- #define WRITE_LINE_COLUMN (X ) \
622
- writer.write <uint32_t >(data.X .Line ); \
627
+ #define WRITE_LINE_COLUMN (X ) \
628
+ writer.write <uint32_t >(data.X .Line ); \
623
629
writer.write <uint32_t >(data.X .Column );
624
630
WRITE_LINE_COLUMN (Loc)
625
631
WRITE_LINE_COLUMN (StartLoc);
@@ -632,7 +638,7 @@ writer.write<uint32_t>(data.X.Column);
632
638
llvm::raw_svector_ostream OS (Buffer);
633
639
if (ide::printDeclUSR (D, OS))
634
640
return None;
635
- return USRWriter.getNewUSRID (OS.str ());
641
+ return USRWriter.getNewUSRId (OS.str ());
636
642
}
637
643
638
644
LineColumn getLineColumn (SourceManager &SM, SourceLoc Loc) {
@@ -674,8 +680,8 @@ writer.write<uint32_t>(data.X.Column);
674
680
return None;
675
681
DeclLocationsTableData Result;
676
682
Result.SourceFileOffset = FWriter.getTextOffset (Locs->SourceFilePath );
677
- #define COPY_LINE_COLUMN (X ) \
678
- Result.X .Line = Locs->X .Line ; \
683
+ #define COPY_LINE_COLUMN (X ) \
684
+ Result.X .Line = Locs->X .Line ; \
679
685
Result.X .Column = Locs->X .Column ;
680
686
COPY_LINE_COLUMN (Loc)
681
687
COPY_LINE_COLUMN (StartLoc)
@@ -693,13 +699,13 @@ Result.X.Column = Locs->X.Column;
693
699
694
700
bool walkToDeclPre (Decl *D) override {
695
701
SWIFT_DEFER {
696
- assert (USRWriter.peekNextId () * sizeof (DeclLocationsTableData) == Buffer.size () &&
697
- " USR Id has a one-to-one mapping with DeclLocationsTableData" );
702
+ assert (USRWriter.peekNextId () * sizeof (DeclLocationsTableData)
703
+ == Buffer.size () &&
704
+ " USR Id has a one-to-one mapping with DeclLocationsTableData" );
698
705
};
699
- // We shouldn't expose any Decls that .swiftdoc file isn't willing to expose.
700
- // .swiftdoc doesn't include comments for double underscored symbols, but for .swiftsourceinfo,
701
- // having the source location for these symbols isn't a concern becuase these
702
- // symbols are in .swiftinterface anyway.
706
+ // .swiftdoc doesn't include comments for double underscored symbols, but
707
+ // for .swiftsourceinfo, having the source location for these symbols isn't
708
+ // a concern becuase these symbols are in .swiftinterface anyway.
703
709
if (!shouldIncludeDecl (D, /* ExcludeDoubleUnderscore*/ false ))
704
710
return false ;
705
711
if (!shouldSerializeSourceLoc (D))
@@ -718,7 +724,8 @@ Result.X.Column = Locs->X.Column;
718
724
};
719
725
720
726
static void emitBasicLocsRecord (llvm::BitstreamWriter &Out,
721
- ModuleOrSourceFile MSF, DeclUSRsTableWriter &USRWriter,
727
+ ModuleOrSourceFile MSF,
728
+ DeclUSRsTableWriter &USRWriter,
722
729
StringWriter &FWriter) {
723
730
assert (MSF);
724
731
const decl_locs_block::BasicDeclLocsLayout DeclLocsList (Out);
@@ -773,17 +780,19 @@ class SourceInfoSerializer : public SerializerBase {
773
780
control_block::TargetLayout Target (Out);
774
781
775
782
auto & LangOpts = M->getASTContext ().LangOpts ;
776
- Metadata.emit (ScratchRecord, SWIFTSOURCEINFO_VERSION_MAJOR, SWIFTSOURCEINFO_VERSION_MINOR,
783
+ Metadata.emit (ScratchRecord, SWIFTSOURCEINFO_VERSION_MAJOR,
784
+ SWIFTSOURCEINFO_VERSION_MINOR,
777
785
/* short version string length*/ 0 , /* compatibility length*/ 0 ,
778
- version::getSwiftFullVersion (LangOpts.EffectiveLanguageVersion ));
786
+ version::getSwiftFullVersion (LangOpts.EffectiveLanguageVersion ));
779
787
780
788
ModuleName.emit (ScratchRecord, M->getName ().str ());
781
789
Target.emit (ScratchRecord, LangOpts.Target .str ());
782
790
}
783
791
}
784
792
};
785
793
}
786
- void serialization::writeSourceInfoToStream (raw_ostream &os, ModuleOrSourceFile DC) {
794
+ void serialization::writeSourceInfoToStream (raw_ostream &os,
795
+ ModuleOrSourceFile DC) {
787
796
assert (DC);
788
797
SourceInfoSerializer S{SWIFTSOURCEINFO_SIGNATURE, DC};
789
798
// FIXME: This is only really needed for debugging. We don't actually use it.
@@ -797,10 +806,11 @@ void serialization::writeSourceInfoToStream(raw_ostream &os, ModuleOrSourceFile
797
806
StringWriter FPWriter;
798
807
emitBasicLocsRecord (S.Out , DC, USRWriter, FPWriter);
799
808
// Emit USR table mapping from a USR to USR Id.
800
- // The basic locs record uses USR Id instead of actual USR, so that we don't need to repeat
801
- // USR texts for newly added records.
809
+ // The basic locs record uses USR Id instead of actual USR, so that we
810
+ // don't need to repeat USR texts for newly added records.
802
811
USRWriter.emitUSRsRecord (S.Out );
803
- // A blob of 0 terminated strings referenced by the location records, e.g. file paths.
812
+ // A blob of 0 terminated strings referenced by the location records,
813
+ // e.g. file paths.
804
814
FPWriter.emitSourceFilesRecord (S.Out );
805
815
}
806
816
}
0 commit comments