@@ -68,16 +68,16 @@ llvm::ErrorOr<swiftscan_string_ref_t> getTargetInfo(ArrayRef<const char *> Comma
6868  return  c_string_utils::create_clone (ResultStr.c_str ());
6969}
7070
71- void  DependencyScannerDiagnosticCollectingConsumer ::handleDiagnostic
71+ void  DependencyScanDiagnosticCollector ::handleDiagnostic
7272                      const  DiagnosticInfo &Info) {
7373  addDiagnostic (SM, Info);
7474  for  (auto  ChildInfo : Info.ChildDiagnosticInfo ) {
7575    addDiagnostic (SM, *ChildInfo);
7676  }
7777}
7878
79- void  DependencyScannerDiagnosticCollectingConsumer ::addDiagnosticSourceManager &SM,  const  DiagnosticInfo &Info) { 
80-   llvm::sys::SmartScopedLock< true >  Lock (ScanningDiagnosticConsumerStateLock); 
79+ void  DependencyScanDiagnosticCollector ::addDiagnostic
80+     SourceManager &SM,  const  DiagnosticInfo &Info) { 
8181  //  Determine what kind of diagnostic we're emitting.
8282  llvm::SourceMgr::DiagKind SMKind;
8383  switch  (Info.Kind ) {
@@ -115,6 +115,12 @@ void DependencyScannerDiagnosticCollectingConsumer::addDiagnostic(SourceManager
115115  Diagnostics.push_back (ScannerDiagnosticInfo{Msg.getMessage ().str (), SMKind});
116116}
117117
118+ void  LockingDependencyScanDiagnosticCollector::addDiagnostic (
119+     SourceManager &SM, const  DiagnosticInfo &Info) {
120+   llvm::sys::SmartScopedLock<true > Lock (ScanningDiagnosticConsumerStateLock);
121+   DependencyScanDiagnosticCollector::addDiagnostic (SM, Info);
122+ }
123+ 
118124DependencyScanningTool::DependencyScanningTool ()
119125    : ScanningService(std::make_unique<SwiftDependencyScanningService>()),
120126      VersionedPCMInstanceCacheCache (
@@ -126,18 +132,20 @@ DependencyScanningTool::getDependencies(
126132    ArrayRef<const  char  *> Command,
127133    const  llvm::StringSet<> &PlaceholderModules) {
128134  //  The primary instance used to scan the query Swift source-code
129-   auto  InstanceOrErr  = initScannerForAction (Command);
130-   if  (std::error_code EC = InstanceOrErr .getError ())
135+   auto  QueryContextOrErr  = initScannerForAction (Command);
136+   if  (std::error_code EC = QueryContextOrErr .getError ())
131137    return  EC;
132-   auto  Instance  = std::move (*InstanceOrErr );
138+   auto  QueryContext  = std::move (*QueryContextOrErr );
133139
134140  //  Local scan cache instance, wrapping the shared global cache.
135141  ModuleDependenciesCache cache (
136-       *ScanningService, Instance ->getMainModule ()->getNameStr ().str (),
137-       Instance ->getInvocation ().getFrontendOptions ().ExplicitModulesOutputPath ,
138-       Instance ->getInvocation ().getModuleScanningHash ());
142+       *ScanningService, QueryContext. ScanInstance ->getMainModule ()->getNameStr ().str (),
143+       QueryContext. ScanInstance ->getInvocation ().getFrontendOptions ().ExplicitModulesOutputPath ,
144+       QueryContext. ScanInstance ->getInvocation ().getModuleScanningHash ());
139145  //  Execute the scanning action, retrieving the in-memory result
140-   auto  DependenciesOrErr = performModuleScan (*Instance.get (), cache);
146+   auto  DependenciesOrErr = performModuleScan (*QueryContext.ScanInstance .get (), 
147+                                              QueryContext.ScanDiagnostics .get (),
148+                                              cache);
141149  if  (DependenciesOrErr.getError ())
142150    return  std::make_error_code (std::errc::not_supported);
143151  auto  Dependencies = std::move (*DependenciesOrErr);
@@ -148,17 +156,19 @@ DependencyScanningTool::getDependencies(
148156llvm::ErrorOr<swiftscan_import_set_t >
149157DependencyScanningTool::getImports (ArrayRef<const  char  *> Command) {
150158  //  The primary instance used to scan the query Swift source-code
151-   auto  InstanceOrErr  = initScannerForAction (Command);
152-   if  (std::error_code EC = InstanceOrErr .getError ())
159+   auto  QueryContextOrErr  = initScannerForAction (Command);
160+   if  (std::error_code EC = QueryContextOrErr .getError ())
153161    return  EC;
154-   auto  Instance  = std::move (*InstanceOrErr );
162+   auto  QueryContext  = std::move (*QueryContextOrErr );
155163
156164  //  Local scan cache instance, wrapping the shared global cache.
157165  ModuleDependenciesCache cache (
158-       *ScanningService, Instance->getMainModule ()->getNameStr ().str (),
159-       Instance->getInvocation ().getFrontendOptions ().ExplicitModulesOutputPath ,
160-       Instance->getInvocation ().getModuleScanningHash ());
161-   auto  DependenciesOrErr = performModulePrescan (*Instance.get (), cache);
166+       *ScanningService, QueryContext.ScanInstance ->getMainModule ()->getNameStr ().str (),
167+       QueryContext.ScanInstance ->getInvocation ().getFrontendOptions ().ExplicitModulesOutputPath ,
168+       QueryContext.ScanInstance ->getInvocation ().getModuleScanningHash ());
169+   auto  DependenciesOrErr = performModulePrescan (*QueryContext.ScanInstance .get (), 
170+                                                 QueryContext.ScanDiagnostics .get (),
171+                                                 cache);
162172  if  (DependenciesOrErr.getError ())
163173    return  std::make_error_code (std::errc::not_supported);
164174  auto  Dependencies = std::move (*DependenciesOrErr);
@@ -172,19 +182,20 @@ DependencyScanningTool::getDependencies(
172182    const  std::vector<BatchScanInput> &BatchInput,
173183    const  llvm::StringSet<> &PlaceholderModules) {
174184  //  The primary instance used to scan Swift modules
175-   auto  InstanceOrErr  = initScannerForAction (Command);
176-   if  (std::error_code EC = InstanceOrErr .getError ())
185+   auto  QueryContextOrErr  = initScannerForAction (Command);
186+   if  (std::error_code EC = QueryContextOrErr .getError ())
177187    return  std::vector<llvm::ErrorOr<swiftscan_dependency_graph_t >>(
178188        BatchInput.size (), std::make_error_code (std::errc::invalid_argument));
179-   auto  Instance  = std::move (*InstanceOrErr );
189+   auto  QueryContext  = std::move (*QueryContextOrErr );
180190
181191  //  Local scan cache instance, wrapping the shared global cache.
182192  ModuleDependenciesCache cache (
183-       *ScanningService, Instance ->getMainModule ()->getNameStr ().str (),
184-       Instance ->getInvocation ().getFrontendOptions ().ExplicitModulesOutputPath ,
185-       Instance ->getInvocation ().getModuleScanningHash ());
193+       *ScanningService, QueryContext. ScanInstance ->getMainModule ()->getNameStr ().str (),
194+       QueryContext. ScanInstance ->getInvocation ().getFrontendOptions ().ExplicitModulesOutputPath ,
195+       QueryContext. ScanInstance ->getInvocation ().getModuleScanningHash ());
186196  auto  BatchScanResults = performBatchModuleScan (
187-       *Instance.get (), cache, VersionedPCMInstanceCacheCache.get (),
197+       *QueryContext.ScanInstance .get (), QueryContext.ScanDiagnostics .get (),
198+       cache, VersionedPCMInstanceCacheCache.get (),
188199      Saver, BatchInput);
189200
190201  return  BatchScanResults;
@@ -221,7 +232,7 @@ void DependencyScanningTool::resetCache() {
221232}
222233
223234std::vector<
224-     DependencyScannerDiagnosticCollectingConsumer ::ScannerDiagnosticInfo>
235+     DependencyScanDiagnosticCollector ::ScannerDiagnosticInfo>
225236DependencyScanningTool::getDiagnostics () {
226237  llvm::sys::SmartScopedLock<true > Lock (DependencyScanningToolStateLock);
227238  return  CDC.Diagnostics ;
@@ -232,25 +243,27 @@ void DependencyScanningTool::resetDiagnostics() {
232243  CDC.reset ();
233244}
234245
235- llvm::ErrorOr<std::unique_ptr<CompilerInstance> >
246+ llvm::ErrorOr<ScanQueryInstance >
236247DependencyScanningTool::initScannerForAction (
237248    ArrayRef<const  char  *> Command) {
238249  //  The remainder of this method operates on shared state in the
239250  //  scanning service and global LLVM state with:
240251  //  llvm::cl::ResetAllOptionOccurrences
241252  llvm::sys::SmartScopedLock<true > Lock (DependencyScanningToolStateLock);
242-   auto  instanceOrErr = initCompilerInstanceForScan (Command);
243-   if  (instanceOrErr.getError ())
244-     return  instanceOrErr;
245-   return  instanceOrErr;
253+   return  initCompilerInstanceForScan (Command);
246254}
247255
248- llvm::ErrorOr<std::unique_ptr<CompilerInstance> >
256+ llvm::ErrorOr<ScanQueryInstance >
249257DependencyScanningTool::initCompilerInstanceForScan (
250258    ArrayRef<const  char  *> CommandArgs) {
251259  //  State unique to an individual scan
252260  auto  Instance = std::make_unique<CompilerInstance>();
261+   auto  ScanDiagnosticConsumer = std::make_unique<DependencyScanDiagnosticCollector>();
262+ 
263+   //  FIXME: The shared CDC must be deprecated once all clients have switched
264+   //  to using per-scan diagnostic output embedded in the `swiftscan_dependency_graph_s`
253265  Instance->addDiagnosticConsumer (&CDC);
266+   Instance->addDiagnosticConsumer (ScanDiagnosticConsumer.get ());
254267
255268  //  Basic error checking on the arguments
256269  if  (CommandArgs.empty ()) {
@@ -300,7 +313,8 @@ DependencyScanningTool::initCompilerInstanceForScan(
300313
301314  (void )Instance->getMainModule ();
302315
303-   return  Instance;
316+   return  ScanQueryInstance{std::move (Instance), 
317+                            std::move (ScanDiagnosticConsumer)};
304318}
305319
306320} //  namespace dependencies
0 commit comments