Skip to content

Commit 5a29371

Browse files
committed
[AST] Never call 'getSerializedLocs()' on non-serialized file unit
For example 'SourceFile' can handle all operations inside 'getSerializedLocs()', but it unnecessarily loads source content in a new buffer, and resolve 'SourceLoc' from the line and the column. And the result is completely useless. rdar://problem/75010520
1 parent b25fd90 commit 5a29371

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/AST/Decl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,9 @@ const Decl::CachedExternalSourceLocs *Decl::getSerializedLocs() const {
600600
return CachedSerializedLocs;
601601
}
602602
auto *File = cast<FileUnit>(getDeclContext()->getModuleScopeContext());
603+
assert(File->getKind() == FileUnitKind::SerializedAST &&
604+
"getSerializedLocs() should only be called on decls in "
605+
"a 'SerializedASTFile'");
603606
auto Locs = File->getBasicLocsForDecl(this);
604607
if (!Locs.hasValue()) {
605608
static const Decl::CachedExternalSourceLocs NullLocs{};

lib/AST/RawComment.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,14 @@ RawComment Decl::getRawComment(bool SerializedOK) const {
147147
return Result;
148148
}
149149

150-
// Ask the parent module.
151-
if (auto *Unit =
152-
dyn_cast<FileUnit>(this->getDeclContext()->getModuleScopeContext())) {
150+
if (!getDeclContext())
151+
return RawComment();
152+
auto *Unit = dyn_cast<FileUnit>(getDeclContext()->getModuleScopeContext());
153+
if (!Unit)
154+
return RawComment();
155+
156+
switch (Unit->getKind()) {
157+
case FileUnitKind::SerializedAST: {
153158
if (SerializedOK) {
154159
if (const auto *CachedLocs = getSerializedLocs()) {
155160
if (!CachedLocs->DocRanges.empty()) {
@@ -158,8 +163,8 @@ RawComment Decl::getRawComment(bool SerializedOK) const {
158163
if (Range.isValid()) {
159164
SRCs.push_back({ Range, Context.SourceMgr });
160165
} else {
161-
// if we've run into an invalid range, don't bother trying to load any of
162-
// the other comments
166+
// if we've run into an invalid range, don't bother trying to load
167+
// any of the other comments
163168
SRCs.clear();
164169
break;
165170
}
@@ -178,10 +183,17 @@ RawComment Decl::getRawComment(bool SerializedOK) const {
178183
Context.setRawComment(this, C->Raw);
179184
return C->Raw;
180185
}
181-
}
182186

183-
// Give up.
184-
return RawComment();
187+
return RawComment();
188+
}
189+
case FileUnitKind::Source:
190+
case FileUnitKind::Builtin:
191+
case FileUnitKind::Synthesized:
192+
case FileUnitKind::ClangModule:
193+
case FileUnitKind::DWARFModule:
194+
return RawComment();
195+
}
196+
llvm_unreachable("invalid file kind");
185197
}
186198

187199
static const Decl* getGroupDecl(const Decl *D) {

0 commit comments

Comments
 (0)