Skip to content

Commit 21ccb27

Browse files
authored
[ClangImporter] Undo changes around createClangInvocation (followup to #85445) (#85457)
Feedback in #85445 after it merged pointed out that the changes around `createClangInvocation` are not necessary because `CompilerInvocation` do not hold a reference to `clang::DiagnosticOptions`, only the `clang::driver::Driver` does. These changes undo the modifications done there and return the code to the previous state (but keeps the changes around `createClangDriver` which was causing the use-after-free).
1 parent 80e6cbf commit 21ccb27

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ class ClangImporter final : public ClangModuleLoader {
216216
std::vector<std::string>
217217
getClangDepScanningInvocationArguments(ASTContext &ctx);
218218

219-
static std::pair<std::unique_ptr<clang::CompilerInvocation>,
220-
std::unique_ptr<clang::DiagnosticOptions>>
219+
static std::unique_ptr<clang::CompilerInvocation>
221220
createClangInvocation(ClangImporter *importer,
222221
const ClangImporterOptions &importerOpts,
223222
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,

lib/ClangImporter/ClangImporter.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,7 @@ std::optional<std::vector<std::string>> ClangImporter::getClangCC1Arguments(
12841284
return CI->getCC1CommandLine();
12851285
}
12861286

1287-
std::pair<std::unique_ptr<clang::CompilerInvocation>,
1288-
std::unique_ptr<clang::DiagnosticOptions>>
1289-
ClangImporter::createClangInvocation(
1287+
std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation(
12901288
ClangImporter *importer, const ClangImporterOptions &importerOpts,
12911289
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
12921290
const std::vector<std::string> &CC1Args) {
@@ -1300,19 +1298,19 @@ ClangImporter::createClangInvocation(
13001298
// option here is either generated by dependency scanner or just round tripped
13011299
// from `getClangCC1Arguments` so we don't expect it to fail. Use a simple
13021300
// printing diagnostics consumer for debugging any unexpected error.
1303-
auto diagOpts = std::make_unique<clang::DiagnosticOptions>();
1301+
clang::DiagnosticOptions diagOpts;
13041302
clang::DiagnosticsEngine clangDiags(
1305-
new clang::DiagnosticIDs(), *diagOpts,
1306-
new clang::TextDiagnosticPrinter(llvm::errs(), *diagOpts));
1303+
new clang::DiagnosticIDs(), diagOpts,
1304+
new clang::TextDiagnosticPrinter(llvm::errs(), diagOpts));
13071305

13081306
// Finally, use the CC1 command-line and the diagnostic engine
13091307
// to instantiate our Invocation.
13101308
auto CI = std::make_unique<clang::CompilerInvocation>();
13111309
if (!clang::CompilerInvocation::CreateFromArgs(
13121310
*CI, invocationArgs, clangDiags, importerOpts.clangPath.c_str()))
1313-
return {nullptr, nullptr};
1311+
return nullptr;
13141312

1315-
return {std::move(CI), std::move(diagOpts)};
1313+
return CI;
13161314
}
13171315

13181316
std::unique_ptr<ClangImporter> ClangImporter::create(
@@ -1360,9 +1358,8 @@ std::unique_ptr<ClangImporter> ClangImporter::create(
13601358
[] { llvm::errs() << "' '"; });
13611359
llvm::errs() << "'\n";
13621360
}
1363-
std::tie(importer->Impl.Invocation, importer->Impl.DiagnosticOptions) =
1364-
createClangInvocation(importer.get(), importerOpts, VFS,
1365-
importer->Impl.ClangArgs);
1361+
importer->Impl.Invocation = createClangInvocation(
1362+
importer.get(), importerOpts, VFS, importer->Impl.ClangArgs);
13661363
if (!importer->Impl.Invocation)
13671364
return nullptr;
13681365
}
@@ -1438,7 +1435,7 @@ std::unique_ptr<ClangImporter> ClangImporter::create(
14381435
ctx, instance.getVirtualFileSystemPtr(), true);
14391436
if (!swiftTargetClangArgs)
14401437
return nullptr;
1441-
auto [swiftTargetClangInvocation, clangDiagOpts] = createClangInvocation(
1438+
auto swiftTargetClangInvocation = createClangInvocation(
14421439
importer.get(), importerOpts, instance.getVirtualFileSystemPtr(),
14431440
*swiftTargetClangArgs);
14441441
if (!swiftTargetClangInvocation)

lib/ClangImporter/ImporterImpl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
551551
/// Clang arguments used to create the Clang invocation.
552552
std::vector<std::string> ClangArgs;
553553

554-
/// Clang diagnostic options used to create the Clang invocation.
555-
std::unique_ptr<clang::DiagnosticOptions> DiagnosticOptions;
556-
557554
/// Mapping from Clang swift_attr attribute text to the Swift source file(s)
558555
/// that contain that attribute text.
559556
///

0 commit comments

Comments
 (0)