Skip to content

Commit 2c08331

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-rebranch
2 parents 56f6343 + eddb736 commit 2c08331

File tree

1 file changed

+41
-86
lines changed

1 file changed

+41
-86
lines changed

lib/Serialization/SerializeDoc.cpp

Lines changed: 41 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@ using swift::version::Version;
3535
using llvm::BCBlockRAII;
3636

3737
using FileNameToGroupNameMap = llvm::StringMap<std::string>;
38-
using pFileNameToGroupNameMap = std::unique_ptr<FileNameToGroupNameMap>;
3938

4039
namespace {
4140
class YamlGroupInputParser {
4241
ASTContext &Ctx;
4342
StringRef RecordPath;
4443
static constexpr const char * const Separator = "/";
4544

46-
// FIXME: This isn't thread-safe.
47-
static llvm::StringMap<pFileNameToGroupNameMap> AllMaps;
48-
4945
bool parseRoot(FileNameToGroupNameMap &Map, llvm::yaml::Node *Root,
5046
StringRef ParentName) {
5147
auto *MapNode = dyn_cast<llvm::yaml::MappingNode>(Root);
@@ -87,28 +83,23 @@ class YamlGroupInputParser {
8783
return false;
8884
}
8985

90-
public:
91-
YamlGroupInputParser(ASTContext &Ctx, StringRef RecordPath):
92-
Ctx(Ctx), RecordPath(RecordPath) {}
93-
94-
FileNameToGroupNameMap* getParsedMap() {
95-
return AllMaps[RecordPath].get();
96-
}
97-
98-
bool diagnoseGroupInfoFile(bool FileMissing = false) {
86+
FileNameToGroupNameMap diagnoseGroupInfoFile(bool FileMissing = false) {
9987
Ctx.Diags.diagnose(SourceLoc(),
10088
FileMissing ? diag::cannot_find_group_info_file:
10189
diag::cannot_parse_group_info_file, RecordPath);
102-
return true;
90+
return {};
10391
}
10492

105-
// Parse the Yaml file that contains the group information.
106-
// True on failure; false on success.
107-
bool parse() {
108-
// If we have already parsed this group info file, return false;
109-
auto FindMap = AllMaps.find(RecordPath);
110-
if (FindMap != AllMaps.end())
111-
return false;
93+
public:
94+
YamlGroupInputParser(ASTContext &Ctx, StringRef RecordPath):
95+
Ctx(Ctx), RecordPath(RecordPath) {}
96+
97+
/// Parse the Yaml file that contains the group information.
98+
///
99+
/// If the record path is empty, returns an empty map.
100+
FileNameToGroupNameMap parse() {
101+
if (RecordPath.empty())
102+
return {};
112103

113104
auto Buffer = llvm::MemoryBuffer::getFile(RecordPath);
114105
if (!Buffer) {
@@ -133,97 +124,61 @@ class YamlGroupInputParser {
133124
if (!Map) {
134125
return diagnoseGroupInfoFile();
135126
}
136-
pFileNameToGroupNameMap pMap(new FileNameToGroupNameMap());
137-
std::string Empty;
138-
if (parseRoot(*pMap, Root, Empty))
127+
FileNameToGroupNameMap Result;
128+
if (parseRoot(Result, Root, ""))
139129
return diagnoseGroupInfoFile();
140130

141-
// Save the parsed map to the owner.
142-
AllMaps[RecordPath] = std::move(pMap);
143-
return false;
131+
// Return the parsed map.
132+
return Result;
144133
}
145134
};
146135

147-
llvm::StringMap<pFileNameToGroupNameMap> YamlGroupInputParser::AllMaps;
148-
149136
class DeclGroupNameContext {
150-
struct GroupNameCollector {
151-
static const StringLiteral NullGroupName;
152-
const bool Enable;
153-
GroupNameCollector(bool Enable) : Enable(Enable) {}
154-
virtual ~GroupNameCollector() = default;
155-
virtual StringRef getGroupNameInternal(const Decl *VD) = 0;
156-
StringRef getGroupName(const Decl *VD) {
157-
return Enable ? getGroupNameInternal(VD) : StringRef(NullGroupName);
158-
};
159-
};
160-
161-
class GroupNameCollectorFromJson : public GroupNameCollector {
162-
StringRef RecordPath;
163-
FileNameToGroupNameMap* pMap = nullptr;
164-
ASTContext &Ctx;
165-
166-
public:
167-
GroupNameCollectorFromJson(StringRef RecordPath, ASTContext &Ctx) :
168-
GroupNameCollector(!RecordPath.empty()), RecordPath(RecordPath),
169-
Ctx(Ctx) {}
170-
StringRef getGroupNameInternal(const Decl *VD) override {
171-
// We need the file path, so there has to be a location.
172-
if (VD->getLoc().isInvalid())
173-
return NullGroupName;
174-
auto PathOp = VD->getDeclContext()->getParentSourceFile()->getBufferID();
175-
if (!PathOp.hasValue())
176-
return NullGroupName;
177-
StringRef FullPath =
178-
Ctx.SourceMgr.getIdentifierForBuffer(PathOp.getValue());
179-
if (!pMap) {
180-
YamlGroupInputParser Parser(Ctx, RecordPath);
181-
if (!Parser.parse()) {
182-
183-
// Get the file-name to group map if parsing correctly.
184-
pMap = Parser.getParsedMap();
185-
}
186-
}
187-
if (!pMap)
188-
return NullGroupName;
189-
StringRef FileName = llvm::sys::path::filename(FullPath);
190-
auto Found = pMap->find(FileName);
191-
if (Found == pMap->end()) {
192-
Ctx.Diags.diagnose(SourceLoc(), diag::error_no_group_info, FileName);
193-
return NullGroupName;
194-
}
195-
return Found->second;
196-
}
197-
};
198-
137+
ASTContext &Ctx;
138+
FileNameToGroupNameMap FileToGroupMap;
199139
llvm::MapVector<StringRef, unsigned> Map;
200140
std::vector<StringRef> ViewBuffer;
201-
std::unique_ptr<GroupNameCollector> pNameCollector;
202141

203142
public:
204143
DeclGroupNameContext(StringRef RecordPath, ASTContext &Ctx) :
205-
pNameCollector(new GroupNameCollectorFromJson(RecordPath, Ctx)) {}
144+
Ctx(Ctx), FileToGroupMap(YamlGroupInputParser(Ctx, RecordPath).parse()) {}
145+
206146
uint32_t getGroupSequence(const Decl *VD) {
207-
return Map.insert(std::make_pair(pNameCollector->getGroupName(VD),
208-
Map.size())).first->second;
147+
if (FileToGroupMap.empty())
148+
return 0;
149+
150+
// We need the file path, so there has to be a location.
151+
if (VD->getLoc().isInvalid())
152+
return 0;
153+
StringRef FullPath =
154+
VD->getDeclContext()->getParentSourceFile()->getFilename();
155+
if (FullPath.empty())
156+
return 0;
157+
StringRef FileName = llvm::sys::path::filename(FullPath);
158+
auto Found = FileToGroupMap.find(FileName);
159+
if (Found == FileToGroupMap.end()) {
160+
Ctx.Diags.diagnose(SourceLoc(), diag::error_no_group_info, FileName);
161+
return 0;
162+
}
163+
164+
StringRef GroupName = Found->second;
165+
return Map.insert(std::make_pair(GroupName, Map.size()+1)).first->second;
209166
}
210167

211168
ArrayRef<StringRef> getOrderedGroupNames() {
212169
ViewBuffer.clear();
170+
ViewBuffer.push_back(""); // 0 is always outside of any group.
213171
for (auto It = Map.begin(); It != Map.end(); ++ It) {
214172
ViewBuffer.push_back(It->first);
215173
}
216174
return llvm::makeArrayRef(ViewBuffer);
217175
}
218176

219177
bool isEnable() {
220-
return pNameCollector->Enable;
178+
return !FileToGroupMap.empty();
221179
}
222180
};
223181

224-
const StringLiteral
225-
DeclGroupNameContext::GroupNameCollector::NullGroupName = "";
226-
227182
struct DeclCommentTableData {
228183
StringRef Brief;
229184
RawComment Raw;

0 commit comments

Comments
 (0)