26
26
#include " swift/AST/ConcreteDeclRef.h"
27
27
#include " swift/AST/DiagnosticEngine.h"
28
28
#include " swift/AST/DiagnosticsClangImporter.h"
29
+ #include " swift/AST/DiagnosticsFrontend.h"
29
30
#include " swift/AST/DiagnosticsSema.h"
30
31
#include " swift/AST/Evaluator.h"
31
32
#include " swift/AST/IRGenOptions.h"
@@ -328,7 +329,7 @@ class BridgingPPTracker : public clang::PPCallbacks {
328
329
return ;
329
330
SmallVector<clang::SourceLocation, 4 > IdLocs;
330
331
for (auto &P : Path)
331
- IdLocs.push_back (P.second );
332
+ IdLocs.push_back (P.getLoc () );
332
333
handleImport (ImportLoc, IdLocs, Imported);
333
334
}
334
335
@@ -982,10 +983,8 @@ bool ClangImporter::canReadPCH(StringRef PCHFilename) {
982
983
// FIXME: The following attempts to do an initial ReadAST invocation to verify
983
984
// the PCH, without causing trouble for the existing CompilerInstance.
984
985
// Look into combining creating the ASTReader along with verification + update
985
- // if necessary, so that we can create and use one ASTReader in the common case
986
- // when there is no need for update.
987
- clang::CompilerInstance CI (Impl.Instance ->getPCHContainerOperations (),
988
- &Impl.Instance ->getModuleCache ());
986
+ // if necessary, so that we can create and use one ASTReader in the common
987
+ // case when there is no need for update.
989
988
auto invocation =
990
989
std::make_shared<clang::CompilerInvocation>(*Impl.Invocation );
991
990
invocation->getPreprocessorOpts ().DisablePCHOrModuleValidation =
@@ -1000,10 +999,13 @@ bool ClangImporter::canReadPCH(StringRef PCHFilename) {
1000
999
// will try to free it.
1001
1000
invocation->getPreprocessorOpts ().RemappedFileBuffers .clear ();
1002
1001
1003
- CI.setInvocation (std::move (invocation));
1002
+ clang::DiagnosticOptions diagOpts;
1003
+ clang::CompilerInstance CI (std::move (invocation),
1004
+ Impl.Instance ->getPCHContainerOperations (),
1005
+ &Impl.Instance ->getModuleCache ());
1004
1006
CI.setTarget (&Impl.Instance ->getTarget ());
1005
1007
CI.setDiagnostics (&*clang::CompilerInstance::createDiagnostics (
1006
- Impl.Instance ->getVirtualFileSystem (), new clang::DiagnosticOptions () ));
1008
+ Impl.Instance ->getVirtualFileSystem (), diagOpts ));
1007
1009
1008
1010
// Note: Reusing the file manager is safe; this is a component that's already
1009
1011
// reused when building PCM files for the module cache.
@@ -1155,13 +1157,11 @@ std::optional<std::vector<std::string>> ClangImporter::getClangCC1Arguments(
1155
1157
//
1156
1158
// The long-term client for Clang diagnostics is set up afterwards, after the
1157
1159
// clang::CompilerInstance is created.
1158
- llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> tempDiagOpts{
1159
- new clang::DiagnosticOptions};
1160
- auto *tempDiagClient =
1161
- new ClangDiagnosticConsumer (Impl, *tempDiagOpts,
1162
- ctx.ClangImporterOpts .DumpClangDiagnostics );
1160
+ clang::DiagnosticOptions tempDiagOpts;
1161
+ auto *tempDiagClient = new ClangDiagnosticConsumer (
1162
+ Impl, tempDiagOpts, ctx.ClangImporterOpts .DumpClangDiagnostics );
1163
1163
auto clangDiags = clang::CompilerInstance::createDiagnostics (
1164
- *VFS, tempDiagOpts. get () , tempDiagClient,
1164
+ *VFS, tempDiagOpts, tempDiagClient,
1165
1165
/* owned*/ true );
1166
1166
1167
1167
// If using direct cc1 module build, use extra args to setup ClangImporter.
@@ -1281,10 +1281,10 @@ std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation(
1281
1281
// option here is either generated by dependency scanner or just round tripped
1282
1282
// from `getClangCC1Arguments` so we don't expect it to fail. Use a simple
1283
1283
// printing diagnostics consumer for debugging any unexpected error.
1284
- auto diagOpts = llvm::makeIntrusiveRefCnt< clang::DiagnosticOptions>() ;
1284
+ clang::DiagnosticOptions diagOpts ;
1285
1285
clang::DiagnosticsEngine clangDiags (
1286
1286
new clang::DiagnosticIDs (), diagOpts,
1287
- new clang::TextDiagnosticPrinter (llvm::errs (), diagOpts. get () ));
1287
+ new clang::TextDiagnosticPrinter (llvm::errs (), diagOpts));
1288
1288
1289
1289
// Finally, use the CC1 command-line and the diagnostic engine
1290
1290
// to instantiate our Invocation.
@@ -1380,12 +1380,10 @@ ClangImporter::create(ASTContext &ctx,
1380
1380
std::make_unique<clang::ObjectFilePCHContainerWriter>());
1381
1381
PCHContainerOperations->registerReader (
1382
1382
std::make_unique<clang::ObjectFilePCHContainerReader>());
1383
- importer->Impl .Instance .reset (
1384
- new clang::CompilerInstance ( std::move (PCHContainerOperations)));
1383
+ importer->Impl .Instance .reset (new clang::CompilerInstance (
1384
+ importer-> Impl . Invocation , std::move (PCHContainerOperations)));
1385
1385
}
1386
1386
auto &instance = *importer->Impl .Instance ;
1387
- instance.setInvocation (importer->Impl .Invocation );
1388
-
1389
1387
if (tracker)
1390
1388
instance.addDependencyCollector (tracker->getClangCollector ());
1391
1389
@@ -1430,7 +1428,7 @@ ClangImporter::create(ASTContext &ctx,
1430
1428
if (!swiftTargetClangInvocation)
1431
1429
return nullptr ;
1432
1430
auto targetInfo = clang::TargetInfo::CreateTargetInfo (
1433
- clangDiags, swiftTargetClangInvocation->TargetOpts );
1431
+ clangDiags, swiftTargetClangInvocation->getTargetOpts () );
1434
1432
// Ensure the target info has configured target-specific defines
1435
1433
std::string defineBuffer;
1436
1434
llvm::raw_string_ostream predefines (defineBuffer);
@@ -1442,7 +1440,7 @@ ClangImporter::create(ASTContext &ctx,
1442
1440
} else {
1443
1441
// Just use the existing Invocation's directly
1444
1442
importer->Impl .setSwiftTargetInfo (clang::TargetInfo::CreateTargetInfo (
1445
- clangDiags, importer->Impl .Invocation ->TargetOpts ));
1443
+ clangDiags, importer->Impl .Invocation ->getTargetOpts () ));
1446
1444
importer->Impl .setSwiftCodeGenOptions (
1447
1445
new clang::CodeGenOptions (importer->Impl .Invocation ->getCodeGenOpts ()));
1448
1446
}
@@ -1461,9 +1459,8 @@ ClangImporter::create(ASTContext &ctx,
1461
1459
// things here.
1462
1460
1463
1461
// Create the target instance.
1464
- instance.setTarget (
1465
- clang::TargetInfo::CreateTargetInfo (clangDiags,
1466
- instance.getInvocation ().TargetOpts ));
1462
+ instance.setTarget (clang::TargetInfo::CreateTargetInfo (
1463
+ clangDiags, instance.getInvocation ().getTargetOpts ()));
1467
1464
if (!instance.hasTarget ())
1468
1465
return nullptr ;
1469
1466
@@ -1916,9 +1913,8 @@ std::string ClangImporter::getBridgingHeaderContents(
1916
1913
clang::FileManager &fileManager = Impl.Instance ->getFileManager ();
1917
1914
1918
1915
clang::CompilerInstance rewriteInstance (
1919
- Impl.Instance ->getPCHContainerOperations (),
1920
- &Impl.Instance ->getModuleCache ());
1921
- rewriteInstance.setInvocation (invocation);
1916
+ std::move (invocation), Impl.Instance ->getPCHContainerOperations (),
1917
+ &Impl.Instance ->getModuleCache ());
1922
1918
rewriteInstance.createDiagnostics (fileManager.getVirtualFileSystem (),
1923
1919
new clang::IgnoringDiagConsumer);
1924
1920
rewriteInstance.setFileManager (&fileManager);
@@ -2022,9 +2018,8 @@ ClangImporter::cloneCompilerInstanceForPrecompiling() {
2022
2018
clang::FileManager &fileManager = Impl.Instance ->getFileManager ();
2023
2019
2024
2020
auto clonedInstance = std::make_unique<clang::CompilerInstance>(
2025
- Impl.Instance ->getPCHContainerOperations (),
2026
- &Impl.Instance ->getModuleCache ());
2027
- clonedInstance->setInvocation (std::move (invocation));
2021
+ std::move (invocation), Impl.Instance ->getPCHContainerOperations (),
2022
+ &Impl.Instance ->getModuleCache ());
2028
2023
clonedInstance->createDiagnostics (fileManager.getVirtualFileSystem (),
2029
2024
&Impl.Instance ->getDiagnosticClient (),
2030
2025
/* ShouldOwnClient=*/ false );
@@ -2313,17 +2308,15 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
2313
2308
auto realModuleName = SwiftContext.getRealModuleName (path.front ().Item ).str ();
2314
2309
2315
2310
// Convert the Swift import path over to a Clang import path.
2316
- SmallVector<std::pair<clang::IdentifierInfo *, clang::SourceLocation>, 4 >
2317
- clangPath;
2311
+ SmallVector<clang::IdentifierLoc, 4 > clangPath;
2318
2312
bool isTopModuleComponent = true ;
2319
2313
for (auto component : path) {
2320
2314
StringRef item = isTopModuleComponent? realModuleName:
2321
2315
component.Item .str ();
2322
2316
isTopModuleComponent = false ;
2323
2317
2324
- clangPath.emplace_back (
2325
- getClangPreprocessor ().getIdentifierInfo (item),
2326
- exportSourceLoc (component.Loc ));
2318
+ clangPath.emplace_back (exportSourceLoc (component.Loc ),
2319
+ getClangPreprocessor ().getIdentifierInfo (item));
2327
2320
}
2328
2321
2329
2322
auto &diagEngine = Instance->getDiagnostics ();
@@ -2333,14 +2326,13 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
2333
2326
auto loadModule = [&](clang::ModuleIdPath path,
2334
2327
clang::Module::NameVisibilityKind visibility)
2335
2328
-> clang::ModuleLoadResult {
2336
- auto importRAII =
2337
- diagClient.handleImport (clangPath.front ().first , diagEngine,
2338
- importLoc);
2329
+ auto importRAII = diagClient.handleImport (
2330
+ clangPath.front ().getIdentifierInfo (), diagEngine, importLoc);
2339
2331
2340
2332
std::string preservedIndexStorePathOption;
2341
2333
auto &clangFEOpts = Instance->getFrontendOpts ();
2342
2334
if (!clangFEOpts.IndexStorePath .empty ()) {
2343
- StringRef moduleName = path[0 ].first ->getName ();
2335
+ StringRef moduleName = path[0 ].getIdentifierInfo () ->getName ();
2344
2336
// Ignore the SwiftShims module for the index data.
2345
2337
if (moduleName == SwiftContext.SwiftShimsModuleName .str ()) {
2346
2338
preservedIndexStorePathOption = clangFEOpts.IndexStorePath ;
@@ -4178,8 +4170,8 @@ ClangImporter::getSwiftExplicitModuleDirectCC1Args() const {
4178
4170
});
4179
4171
4180
4172
clang::CompilerInvocation instance;
4181
- clang::DiagnosticsEngine clangDiags ( new clang::DiagnosticIDs (),
4182
- new clang::DiagnosticOptions () ,
4173
+ clang::DiagnosticOptions diagOpts;
4174
+ clang::DiagnosticsEngine clangDiags ( new clang::DiagnosticIDs (), diagOpts ,
4183
4175
new clang::IgnoringDiagConsumer ());
4184
4176
bool success = clang::CompilerInvocation::CreateFromArgs (instance, clangArgs,
4185
4177
clangDiags);
@@ -4232,8 +4224,19 @@ ClangImporter::getSwiftExplicitModuleDirectCC1Args() const {
4232
4224
if (!Impl.SwiftContext .SearchPathOpts .ScannerPrefixMapper .empty ()) {
4233
4225
// Remap all the paths if requested.
4234
4226
llvm::PrefixMapper Mapper;
4235
- clang::tooling::dependencies::DepscanPrefixMapping::configurePrefixMapper (
4236
- Impl.SwiftContext .SearchPathOpts .ScannerPrefixMapper , Mapper);
4227
+ SmallVector<llvm::MappedPrefix> Prefixes;
4228
+ if (auto E = llvm::MappedPrefix::transformJoined (
4229
+ Impl.SwiftContext .SearchPathOpts .ScannerPrefixMapper , Prefixes)) {
4230
+ // Take permanent ownership of this string. In general the diagnostic
4231
+ // might outlive this function.
4232
+ auto errorMessage =
4233
+ Impl.SwiftContext .AllocateCopy (llvm::toString (std::move (E)));
4234
+ Impl.SwiftContext .Diags .diagnose (SourceLoc (), diag::error_prefix_mapping,
4235
+ errorMessage);
4236
+ }
4237
+ Mapper.addRange (Prefixes);
4238
+ Mapper.sort ();
4239
+
4237
4240
clang::tooling::dependencies::DepscanPrefixMapping::remapInvocationPaths (
4238
4241
instance, Mapper);
4239
4242
instance.getFrontendOpts ().PathPrefixMappings .clear ();
0 commit comments