Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "swift/AST/AttrKind.h"
#include "swift/AST/ClangModuleLoader.h"
#include "clang/AST/Attr.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/Specifiers.h"
#include "llvm/Support/VirtualFileSystem.h"

Expand Down Expand Up @@ -213,7 +214,8 @@ class ClangImporter final : public ClangModuleLoader {
std::vector<std::string>
getClangDepScanningInvocationArguments(ASTContext &ctx);

static std::unique_ptr<clang::CompilerInvocation>
static std::pair<std::unique_ptr<clang::CompilerInvocation>,
std::unique_ptr<clang::DiagnosticOptions>>
createClangInvocation(ClangImporter *importer,
const ClangImporterOptions &importerOpts,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
Expand All @@ -223,8 +225,9 @@ class ClangImporter final : public ClangModuleLoader {
///
/// \return a pair of the Clang Driver and the diagnostic engine, which needs
/// to be alive during the use of the Driver.
static std::pair<clang::driver::Driver,
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>>
static std::tuple<clang::driver::Driver,
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>,
std::unique_ptr<clang::DiagnosticOptions>>
createClangDriver(
const LangOptions &LangOpts,
const ClangImporterOptions &ClangImporterOpts,
Expand Down
21 changes: 12 additions & 9 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,9 @@ std::optional<std::vector<std::string>> ClangImporter::getClangCC1Arguments(
return CI->getCC1CommandLine();
}

std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation(
std::pair<std::unique_ptr<clang::CompilerInvocation>,
std::unique_ptr<clang::DiagnosticOptions>>
ClangImporter::createClangInvocation(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary. clangDiags is a local, so diagOpts being local should be fine. The returned CI doesn't hold on to either of these.

ClangImporter *importer, const ClangImporterOptions &importerOpts,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
const std::vector<std::string> &CC1Args) {
Expand All @@ -1297,19 +1299,19 @@ std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation(
// option here is either generated by dependency scanner or just round tripped
// from `getClangCC1Arguments` so we don't expect it to fail. Use a simple
// printing diagnostics consumer for debugging any unexpected error.
clang::DiagnosticOptions diagOpts;
auto diagOpts = std::make_unique<clang::DiagnosticOptions>();
clang::DiagnosticsEngine clangDiags(
new clang::DiagnosticIDs(), diagOpts,
new clang::TextDiagnosticPrinter(llvm::errs(), diagOpts));
new clang::DiagnosticIDs(), *diagOpts,
new clang::TextDiagnosticPrinter(llvm::errs(), *diagOpts));

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

return CI;
return {std::move(CI), std::move(diagOpts)};
}

std::unique_ptr<ClangImporter> ClangImporter::create(
Expand Down Expand Up @@ -1357,8 +1359,9 @@ std::unique_ptr<ClangImporter> ClangImporter::create(
[] { llvm::errs() << "' '"; });
llvm::errs() << "'\n";
}
importer->Impl.Invocation = createClangInvocation(
importer.get(), importerOpts, VFS, importer->Impl.ClangArgs);
std::tie(importer->Impl.Invocation, importer->Impl.DiagnosticOptions) =
createClangInvocation(importer.get(), importerOpts, VFS,
importer->Impl.ClangArgs);
if (!importer->Impl.Invocation)
return nullptr;
}
Expand Down Expand Up @@ -1434,7 +1437,7 @@ std::unique_ptr<ClangImporter> ClangImporter::create(
ctx, instance.getVirtualFileSystemPtr(), true);
if (!swiftTargetClangArgs)
return nullptr;
auto swiftTargetClangInvocation = createClangInvocation(
auto [swiftTargetClangInvocation, clangDiagOpts] = createClangInvocation(
importer.get(), importerOpts, instance.getVirtualFileSystemPtr(),
*swiftTargetClangArgs);
if (!swiftTargetClangInvocation)
Expand Down
26 changes: 15 additions & 11 deletions lib/ClangImporter/ClangIncludePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,23 @@ parseClangDriverArgs(const clang::driver::Driver &clangDriver,
return clangDriver.getOpts().ParseArgs(args, unused1, unused2);
}

std::pair<clang::driver::Driver,
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>>
std::tuple<clang::driver::Driver,
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>,
std::unique_ptr<clang::DiagnosticOptions>>
ClangImporter::createClangDriver(
const LangOptions &LangOpts, const ClangImporterOptions &ClangImporterOpts,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs) {

auto diagVFS = vfs ? vfs : llvm::vfs::getRealFileSystem();

clang::DiagnosticOptions diagOpts;
auto diagOpts = std::make_unique<clang::DiagnosticOptions>();
auto *silentDiagConsumer = new clang::DiagnosticConsumer();
auto clangDiags = clang::CompilerInstance::createDiagnostics(
*diagVFS, diagOpts, silentDiagConsumer);
*diagVFS, *diagOpts, silentDiagConsumer);
clang::driver::Driver clangDriver(ClangImporterOpts.clangPath,
LangOpts.Target.str(), *clangDiags,
"clang LLVM compiler", vfs);
return {std::move(clangDriver), clangDiags};
return {std::move(clangDriver), clangDiags, std::move(diagOpts)};
}

/// Given a list of include paths and a list of file names, finds the first
Expand Down Expand Up @@ -208,8 +209,9 @@ getLibcFileMapping(const ASTContext &ctx, StringRef modulemapFileName,
const llvm::Triple &triple = ctx.LangOpts.Target;

// Extract the libc path from Clang driver.
auto [clangDriver, clangDiagEngine] = ClangImporter::createClangDriver(
ctx.LangOpts, ctx.ClangImporterOpts, vfs);
auto [clangDriver, clangDiagEngine, clangDiagOpts] =
ClangImporter::createClangDriver(ctx.LangOpts, ctx.ClangImporterOpts,
vfs);
auto clangDriverArgs = ClangImporter::createClangArgs(
ctx.ClangImporterOpts, ctx.SearchPathOpts, clangDriver);

Expand Down Expand Up @@ -287,8 +289,9 @@ static void getLibStdCxxFileMapping(
return;

// Extract the libstdc++ installation path from Clang driver.
auto [clangDriver, clangDiagEngine] = ClangImporter::createClangDriver(
ctx.LangOpts, ctx.ClangImporterOpts, vfs);
auto [clangDriver, clangDiagEngine, clangDiagOpts] =
ClangImporter::createClangDriver(ctx.LangOpts, ctx.ClangImporterOpts,
vfs);
auto clangDriverArgs = ClangImporter::createClangArgs(
ctx.ClangImporterOpts, ctx.SearchPathOpts, clangDriver);

Expand Down Expand Up @@ -482,8 +485,9 @@ void GetWindowsFileMappings(
if (!Triple.isWindowsMSVCEnvironment())
return;

auto [Driver, clangDiagEngine] = ClangImporter::createClangDriver(
Context.LangOpts, Context.ClangImporterOpts, driverVFS);
auto [Driver, clangDiagEngine, clangDiagOpts] =
ClangImporter::createClangDriver(Context.LangOpts,
Context.ClangImporterOpts, driverVFS);
const llvm::opt::InputArgList Args = ClangImporter::createClangArgs(
Context.ClangImporterOpts, Context.SearchPathOpts, Driver);
const clang::driver::ToolChain &ToolChain = Driver.getToolChain(Args, Triple);
Expand Down
3 changes: 3 additions & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
/// Clang arguments used to create the Clang invocation.
std::vector<std::string> ClangArgs;

/// Clang diagnostic options used to create the Clang invocation.
std::unique_ptr<clang::DiagnosticOptions> DiagnosticOptions;
Comment on lines +554 to +555
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Clang diagnostic options used to create the Clang invocation.
std::unique_ptr<clang::DiagnosticOptions> DiagnosticOptions;
/// Clang diagnostic options used to create the Clang driver.
std::unique_ptr<clang::DiagnosticOptions> DiagnosticOptions;

The invocation doesn't need external DiagnosticOptions.


/// Mapping from Clang swift_attr attribute text to the Swift source file(s)
/// that contain that attribute text.
///
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ void CompilerInvocation::computeCXXStdlibOptions() {
LangOpts.PlatformDefaultCXXStdlib = CXXStdlibKind::Msvcprt;
} else if (LangOpts.Target.isOSLinux() || LangOpts.Target.isOSDarwin() ||
LangOpts.Target.isOSFreeBSD()) {
auto [clangDriver, clangDiagEngine] =
auto [clangDriver, clangDiagEngine, clangDiagOpts] =
ClangImporter::createClangDriver(LangOpts, ClangImporterOpts);
auto clangDriverArgs = ClangImporter::createClangArgs(
ClangImporterOpts, SearchPathOpts, clangDriver);
Expand Down