Skip to content

Commit c54c9c7

Browse files
committed
[Gardening] Extract basic source info structs from RawComment.h
1 parent eae6c00 commit c54c9c7

28 files changed

+197
-127
lines changed

include/swift/AST/FileUnit.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#define SWIFT_AST_FILEUNIT_H
1515

1616
#include "swift/AST/Module.h"
17+
#include "swift/AST/RawComment.h"
18+
#include "swift/Basic/BasicSourceInfo.h"
1719

1820
namespace swift {
1921
static inline unsigned alignOfFileUnit();

include/swift/AST/Module.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#include "swift/AST/Identifier.h"
2424
#include "swift/AST/Import.h"
2525
#include "swift/AST/LookupKinds.h"
26-
#include "swift/AST/RawComment.h"
2726
#include "swift/AST/Type.h"
27+
#include "swift/Basic/BasicSourceInfo.h"
2828
#include "swift/Basic/Compiler.h"
2929
#include "swift/Basic/OptionSet.h"
3030
#include "swift/Basic/STLExtras.h"

include/swift/AST/NameLookup.h

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/Basic/Debug.h"
2626
#include "swift/Basic/NullablePtr.h"
2727
#include "swift/Basic/SourceLoc.h"
28+
#include "swift/Basic/SourceManager.h"
2829

2930
namespace swift {
3031
class ASTContext;

include/swift/AST/RawComment.h

+3-75
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
#ifndef SWIFT_AST_RAW_COMMENT_H
1414
#define SWIFT_AST_RAW_COMMENT_H
1515

16-
#include "swift/Basic/Fingerprint.h"
17-
#include "swift/Basic/LLVM.h"
1816
#include "swift/Basic/SourceLoc.h"
19-
#include "swift/Basic/SourceManager.h"
17+
#include "llvm/ADT/ArrayRef.h"
18+
#include "llvm/ADT/StringRef.h"
2019

2120
namespace swift {
2221

2322
class SourceFile;
23+
class SourceManager;
2424

2525
struct SingleRawComment {
2626
enum class CommentKind {
@@ -82,78 +82,6 @@ struct CommentInfo {
8282
uint32_t SourceOrder;
8383
};
8484

85-
struct LineColumn {
86-
uint32_t Line = 0;
87-
uint32_t Column = 0;
88-
bool isValid() const { return Line && Column; }
89-
};
90-
91-
struct BasicDeclLocs {
92-
StringRef SourceFilePath;
93-
SmallVector<std::pair<LineColumn, uint32_t>, 4> DocRanges;
94-
LineColumn Loc;
95-
LineColumn StartLoc;
96-
LineColumn EndLoc;
97-
};
98-
99-
class BasicSourceFileInfo {
100-
/// If this is non-null, fields other than 'FilePath' hasn't been populated.
101-
/// The 'getInt()' part indicates this instance is constructed with a
102-
/// SourceFile.
103-
llvm::PointerIntPair<const SourceFile *, 1, bool> SFAndIsFromSF;
104-
105-
StringRef FilePath;
106-
Fingerprint InterfaceHashIncludingTypeMembers = Fingerprint::ZERO();
107-
/// Does *not* include the type-body hashes of the top level types.
108-
/// Just the `SourceFile` hashes.
109-
/// Used for incremental imports.
110-
Fingerprint InterfaceHashExcludingTypeMembers = Fingerprint::ZERO();
111-
llvm::sys::TimePoint<> LastModified = {};
112-
uint64_t FileSize = 0;
113-
114-
// Populate the from 'SF' member if exist. 'SF' will be cleared.
115-
void populateWithSourceFileIfNeeded();
116-
117-
public:
118-
BasicSourceFileInfo(StringRef FilePath,
119-
Fingerprint InterfaceHashIncludingTypeMembers,
120-
Fingerprint InterfaceHashExcludingTypeMembers,
121-
llvm::sys::TimePoint<> LastModified, uint64_t FileSize)
122-
: FilePath(FilePath),
123-
InterfaceHashIncludingTypeMembers(InterfaceHashIncludingTypeMembers),
124-
InterfaceHashExcludingTypeMembers(InterfaceHashExcludingTypeMembers),
125-
LastModified(LastModified), FileSize(FileSize) {}
126-
127-
/// Construct with a `SourceFile`. `getInterfaceHashIncludingTypeMembers()`,
128-
/// `getInterfaceHashExcludingTypeMembers()`, `getLastModified()` and `getFileSize()` are laizily
129-
/// populated when accessed.
130-
BasicSourceFileInfo(const SourceFile *SF);
131-
132-
bool isFromSourceFile() const;
133-
134-
StringRef getFilePath() const { return FilePath; }
135-
136-
Fingerprint getInterfaceHashIncludingTypeMembers() const {
137-
const_cast<BasicSourceFileInfo *>(this)->populateWithSourceFileIfNeeded();
138-
return InterfaceHashIncludingTypeMembers;
139-
}
140-
141-
Fingerprint getInterfaceHashExcludingTypeMembers() const {
142-
const_cast<BasicSourceFileInfo *>(this)->populateWithSourceFileIfNeeded();
143-
return InterfaceHashExcludingTypeMembers;
144-
}
145-
146-
llvm::sys::TimePoint<> getLastModified() const {
147-
const_cast<BasicSourceFileInfo *>(this)->populateWithSourceFileIfNeeded();
148-
return LastModified;
149-
}
150-
151-
uint64_t getFileSize() const {
152-
const_cast<BasicSourceFileInfo *>(this)->populateWithSourceFileIfNeeded();
153-
return FileSize;
154-
}
155-
};
156-
15785
} // namespace swift
15886

15987
#endif // LLVM_SWIFT_AST_RAW_COMMENT_H

include/swift/Basic/BasicSourceInfo.h

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//===--- BasicSourceInfo.h - Simple source information ----------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_BASIC_BASIC_SOURCE_INFO_H
14+
#define SWIFT_BASIC_BASIC_SOURCE_INFO_H
15+
16+
#include "swift/Basic/Fingerprint.h"
17+
#include "swift/Basic/LLVM.h"
18+
#include "llvm/ADT/PointerIntPair.h"
19+
#include "llvm/Support/Chrono.h"
20+
21+
namespace swift {
22+
23+
class SourceFile;
24+
25+
struct SourcePosition {
26+
uint32_t Line = 0;
27+
uint32_t Column = 0;
28+
bool isValid() const { return Line && Column; }
29+
};
30+
31+
struct BasicDeclLocs {
32+
StringRef SourceFilePath;
33+
SmallVector<std::pair<SourcePosition, uint32_t>, 4> DocRanges;
34+
SourcePosition Loc;
35+
SourcePosition StartLoc;
36+
SourcePosition EndLoc;
37+
};
38+
39+
class BasicSourceFileInfo {
40+
/// If this is non-null, fields other than 'FilePath' hasn't been populated.
41+
/// The 'getInt()' part indicates this instance is constructed with a
42+
/// SourceFile.
43+
llvm::PointerIntPair<const SourceFile *, 1, bool> SFAndIsFromSF;
44+
45+
StringRef FilePath;
46+
Fingerprint InterfaceHashIncludingTypeMembers = Fingerprint::ZERO();
47+
/// Does *not* include the type-body hashes of the top level types.
48+
/// Just the `SourceFile` hashes.
49+
/// Used for incremental imports.
50+
Fingerprint InterfaceHashExcludingTypeMembers = Fingerprint::ZERO();
51+
llvm::sys::TimePoint<> LastModified = {};
52+
uint64_t FileSize = 0;
53+
54+
// Populate the from 'SF' member if exist. 'SF' will be cleared.
55+
void populateWithSourceFileIfNeeded();
56+
57+
public:
58+
BasicSourceFileInfo(StringRef FilePath,
59+
Fingerprint InterfaceHashIncludingTypeMembers,
60+
Fingerprint InterfaceHashExcludingTypeMembers,
61+
llvm::sys::TimePoint<> LastModified, uint64_t FileSize)
62+
: FilePath(FilePath),
63+
InterfaceHashIncludingTypeMembers(InterfaceHashIncludingTypeMembers),
64+
InterfaceHashExcludingTypeMembers(InterfaceHashExcludingTypeMembers),
65+
LastModified(LastModified), FileSize(FileSize) {}
66+
67+
/// Construct with a `SourceFile`. `getInterfaceHashIncludingTypeMembers()`,
68+
/// `getInterfaceHashExcludingTypeMembers()`, `getLastModified()` and
69+
/// `getFileSize()` are laizily populated when accessed.
70+
BasicSourceFileInfo(const SourceFile *SF);
71+
72+
bool isFromSourceFile() const;
73+
74+
StringRef getFilePath() const { return FilePath; }
75+
76+
Fingerprint getInterfaceHashIncludingTypeMembers() const {
77+
const_cast<BasicSourceFileInfo *>(this)->populateWithSourceFileIfNeeded();
78+
return InterfaceHashIncludingTypeMembers;
79+
}
80+
81+
Fingerprint getInterfaceHashExcludingTypeMembers() const {
82+
const_cast<BasicSourceFileInfo *>(this)->populateWithSourceFileIfNeeded();
83+
return InterfaceHashExcludingTypeMembers;
84+
}
85+
86+
llvm::sys::TimePoint<> getLastModified() const {
87+
const_cast<BasicSourceFileInfo *>(this)->populateWithSourceFileIfNeeded();
88+
return LastModified;
89+
}
90+
91+
uint64_t getFileSize() const {
92+
const_cast<BasicSourceFileInfo *>(this)->populateWithSourceFileIfNeeded();
93+
return FileSize;
94+
}
95+
};
96+
97+
} // namespace swift
98+
99+
#endif // SWIFT_BASIC_BASIC_SOURCE_INFO_H
100+

include/swift/IDE/Utils.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/IDE/SourceEntityWalker.h"
2323
#include "swift/Parse/Token.h"
2424
#include "llvm/ADT/StringRef.h"
25+
#include "llvm/Support/VirtualFileSystem.h"
2526
#include <memory>
2627
#include <string>
2728
#include <functional>

include/swift/SIL/SILRemarkStreamer.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef SWIFT_SIL_SILREMARKSTREAMER_H
2020
#define SWIFT_SIL_SILREMARKSTREAMER_H
2121

22+
#include "swift/Basic/SourceManager.h"
2223
#include "swift/SIL/OptimizationRemark.h"
2324
#include "llvm/Remarks/RemarkStreamer.h"
2425

lib/AST/GenericSignatureBuilder.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "swift/AST/TypeWalker.h"
3737
#include "swift/Basic/Debug.h"
3838
#include "swift/Basic/Defer.h"
39+
#include "swift/Basic/SourceManager.h"
3940
#include "swift/Basic/Statistic.h"
4041
#include "llvm/ADT/GraphTraits.h"
4142
#include "llvm/ADT/DenseMap.h"

lib/AST/Module.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -880,12 +880,12 @@ SourceFile::getBasicLocsForDecl(const Decl *D) const {
880880

881881
for (const auto &SRC : D->getRawComment(/*SerializedOK*/false).Comments) {
882882
Result.DocRanges.push_back(std::make_pair(
883-
LineColumn { SRC.StartLine, SRC.StartColumn },
883+
SourcePosition { SRC.StartLine, SRC.StartColumn },
884884
SRC.Range.getByteLength())
885885
);
886886
}
887887

888-
auto setLineColumn = [&SM](LineColumn &Home, SourceLoc Loc) {
888+
auto setLineColumn = [&SM](SourcePosition &Home, SourceLoc Loc) {
889889
if (Loc.isValid()) {
890890
std::tie(Home.Line, Home.Column) = SM.getPresumedLineAndColumnForLoc(Loc);
891891
}

lib/AST/ModuleLoader.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/ModuleLoader.h"
2121
#include "swift/Basic/FileTypes.h"
2222
#include "swift/Basic/Platform.h"
23+
#include "swift/Basic/SourceManager.h"
2324
#include "clang/Frontend/Utils.h"
2425
#include "swift/ClangImporter/ClangImporter.h"
2526

lib/AST/RawComment.cpp

-40
Original file line numberDiff line numberDiff line change
@@ -262,43 +262,3 @@ CharSourceRange RawComment::getCharSourceRange() {
262262
static_cast<const char *>(Start.getOpaquePointerValue());
263263
return CharSourceRange(Start, Length);
264264
}
265-
266-
BasicSourceFileInfo::BasicSourceFileInfo(const SourceFile *SF)
267-
: SFAndIsFromSF(SF, true) {
268-
FilePath = SF->getFilename();
269-
}
270-
271-
bool BasicSourceFileInfo::isFromSourceFile() const {
272-
return SFAndIsFromSF.getInt();
273-
}
274-
275-
void BasicSourceFileInfo::populateWithSourceFileIfNeeded() {
276-
const auto *SF = SFAndIsFromSF.getPointer();
277-
if (!SF)
278-
return;
279-
SWIFT_DEFER {
280-
SFAndIsFromSF.setPointer(nullptr);
281-
};
282-
283-
SourceManager &SM = SF->getASTContext().SourceMgr;
284-
285-
if (FilePath.empty())
286-
return;
287-
auto stat = SM.getFileSystem()->status(FilePath);
288-
if (!stat)
289-
return;
290-
291-
LastModified = stat->getLastModificationTime();
292-
FileSize = stat->getSize();
293-
294-
if (SF->hasInterfaceHash()) {
295-
InterfaceHashIncludingTypeMembers = SF->getInterfaceHashIncludingTypeMembers();
296-
InterfaceHashExcludingTypeMembers = SF->getInterfaceHash();
297-
} else {
298-
// FIXME: Parse the file with EnableInterfaceHash option.
299-
InterfaceHashIncludingTypeMembers = Fingerprint::ZERO();
300-
InterfaceHashExcludingTypeMembers = Fingerprint::ZERO();
301-
}
302-
303-
return;
304-
}

lib/Basic/BasicSourceInfo.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===--- BasicSourceInfo.cpp - Simple source information ------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/ASTContext.h"
14+
#include "swift/AST/SourceFile.h"
15+
#include "swift/Basic/BasicSourceInfo.h"
16+
#include "swift/Basic/Defer.h"
17+
#include "swift/Basic/SourceManager.h"
18+
19+
using namespace swift;
20+
21+
BasicSourceFileInfo::BasicSourceFileInfo(const SourceFile *SF)
22+
: SFAndIsFromSF(SF, true) {
23+
FilePath = SF->getFilename();
24+
}
25+
26+
bool BasicSourceFileInfo::isFromSourceFile() const {
27+
return SFAndIsFromSF.getInt();
28+
}
29+
30+
void BasicSourceFileInfo::populateWithSourceFileIfNeeded() {
31+
const auto *SF = SFAndIsFromSF.getPointer();
32+
if (!SF)
33+
return;
34+
SWIFT_DEFER {
35+
SFAndIsFromSF.setPointer(nullptr);
36+
};
37+
38+
SourceManager &SM = SF->getASTContext().SourceMgr;
39+
40+
if (FilePath.empty())
41+
return;
42+
auto stat = SM.getFileSystem()->status(FilePath);
43+
if (!stat)
44+
return;
45+
46+
LastModified = stat->getLastModificationTime();
47+
FileSize = stat->getSize();
48+
49+
if (SF->hasInterfaceHash()) {
50+
InterfaceHashIncludingTypeMembers = SF->getInterfaceHashIncludingTypeMembers();
51+
InterfaceHashExcludingTypeMembers = SF->getInterfaceHash();
52+
} else {
53+
// FIXME: Parse the file with EnableInterfaceHash option.
54+
InterfaceHashIncludingTypeMembers = Fingerprint::ZERO();
55+
InterfaceHashExcludingTypeMembers = Fingerprint::ZERO();
56+
}
57+
58+
return;
59+
}

lib/Basic/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ generate_revision_inc(llvm_revision_inc LLVM "${LLVM_MAIN_SRC_DIR}")
4242
generate_revision_inc(swift_revision_inc Swift "${SWIFT_SOURCE_DIR}")
4343

4444
add_swift_host_library(swiftBasic STATIC
45+
BasicSourceInfo.cpp
4546
Cache.cpp
4647
ClusteredBitVector.cpp
4748
DiverseStack.cpp

lib/ClangImporter/ClangModuleDependencyScanner.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616
#include "ImporterImpl.h"
1717
#include "swift/AST/ModuleDependencies.h"
18+
#include "swift/Basic/SourceManager.h"
1819
#include "swift/ClangImporter/ClangImporter.h"
1920
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
2021
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"

lib/IRGen/GenType.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/PrettyStackTrace.h"
2424
#include "swift/AST/Types.h"
2525
#include "swift/Basic/Platform.h"
26+
#include "swift/Basic/SourceManager.h"
2627
#include "swift/IRGen/Linking.h"
2728
#include "swift/SIL/SILModule.h"
2829
#include "llvm/IR/DerivedTypes.h"

0 commit comments

Comments
 (0)