@@ -35,17 +35,13 @@ using swift::version::Version;
35
35
using llvm::BCBlockRAII;
36
36
37
37
using FileNameToGroupNameMap = llvm::StringMap<std::string>;
38
- using pFileNameToGroupNameMap = std::unique_ptr<FileNameToGroupNameMap>;
39
38
40
39
namespace {
41
40
class YamlGroupInputParser {
42
41
ASTContext &Ctx;
43
42
StringRef RecordPath;
44
43
static constexpr const char * const Separator = " /" ;
45
44
46
- // FIXME: This isn't thread-safe.
47
- static llvm::StringMap<pFileNameToGroupNameMap> AllMaps;
48
-
49
45
bool parseRoot (FileNameToGroupNameMap &Map, llvm::yaml::Node *Root,
50
46
StringRef ParentName) {
51
47
auto *MapNode = dyn_cast<llvm::yaml::MappingNode>(Root);
@@ -87,28 +83,23 @@ class YamlGroupInputParser {
87
83
return false ;
88
84
}
89
85
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 ) {
99
87
Ctx.Diags .diagnose (SourceLoc (),
100
88
FileMissing ? diag::cannot_find_group_info_file:
101
89
diag::cannot_parse_group_info_file, RecordPath);
102
- return true ;
90
+ return {} ;
103
91
}
104
92
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 {};
112
103
113
104
auto Buffer = llvm::MemoryBuffer::getFile (RecordPath);
114
105
if (!Buffer) {
@@ -133,97 +124,61 @@ class YamlGroupInputParser {
133
124
if (!Map) {
134
125
return diagnoseGroupInfoFile ();
135
126
}
136
- pFileNameToGroupNameMap pMap (new FileNameToGroupNameMap ());
137
- std::string Empty;
138
- if (parseRoot (*pMap, Root, Empty))
127
+ FileNameToGroupNameMap Result;
128
+ if (parseRoot (Result, Root, " " ))
139
129
return diagnoseGroupInfoFile ();
140
130
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;
144
133
}
145
134
};
146
135
147
- llvm::StringMap<pFileNameToGroupNameMap> YamlGroupInputParser::AllMaps;
148
-
149
136
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;
199
139
llvm::MapVector<StringRef, unsigned > Map;
200
140
std::vector<StringRef> ViewBuffer;
201
- std::unique_ptr<GroupNameCollector> pNameCollector;
202
141
203
142
public:
204
143
DeclGroupNameContext (StringRef RecordPath, ASTContext &Ctx) :
205
- pNameCollector (new GroupNameCollectorFromJson(RecordPath, Ctx)) {}
144
+ Ctx (Ctx), FileToGroupMap(YamlGroupInputParser(Ctx, RecordPath).parse()) {}
145
+
206
146
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 ;
209
166
}
210
167
211
168
ArrayRef<StringRef> getOrderedGroupNames () {
212
169
ViewBuffer.clear ();
170
+ ViewBuffer.push_back (" " ); // 0 is always outside of any group.
213
171
for (auto It = Map.begin (); It != Map.end (); ++ It) {
214
172
ViewBuffer.push_back (It->first );
215
173
}
216
174
return llvm::makeArrayRef (ViewBuffer);
217
175
}
218
176
219
177
bool isEnable () {
220
- return pNameCollector-> Enable ;
178
+ return !FileToGroupMap. empty () ;
221
179
}
222
180
};
223
181
224
- const StringLiteral
225
- DeclGroupNameContext::GroupNameCollector::NullGroupName = " " ;
226
-
227
182
struct DeclCommentTableData {
228
183
StringRef Brief;
229
184
RawComment Raw;
0 commit comments