Skip to content

Commit 807aa26

Browse files
committed
[C++20] [Modules] Don't ignore -fmodule-file when we compile pcm files
Close #62843. Previously when we compile .pcm files into .o files, the `-fmodule-file=<module-name>=<module-path>` option is ignored. This is conflicted with our consensus in #62707.
1 parent 53064c5 commit 807aa26

File tree

11 files changed

+47
-19
lines changed

11 files changed

+47
-19
lines changed

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ class ASTUnit {
694694
const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad,
695695
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
696696
const FileSystemOptions &FileSystemOpts,
697+
std::shared_ptr<HeaderSearchOptions> HSOpts,
697698
bool UseDebugInfo = false, bool OnlyLocalDecls = false,
698699
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
699700
bool AllowASTWithCompilerErrors = false,

clang/lib/CrossTU/CrossTranslationUnit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ CrossTranslationUnitContext::ASTLoader::loadFromDump(StringRef ASTDumpPath) {
568568
return ASTUnit::LoadFromASTFile(
569569
std::string(ASTDumpPath.str()),
570570
CI.getPCHContainerOperations()->getRawReader(), ASTUnit::LoadEverything,
571-
Diags, CI.getFileSystemOpts());
571+
Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOptsPtr());
572572
}
573573

574574
/// Load the AST from a source-file, which is supposed to be located inside the

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3765,12 +3765,6 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
37653765
}
37663766

37673767
if (HaveModules) {
3768-
// -fprebuilt-module-path specifies where to load the prebuilt module files.
3769-
for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) {
3770-
CmdArgs.push_back(Args.MakeArgString(
3771-
std::string("-fprebuilt-module-path=") + A->getValue()));
3772-
A->claim();
3773-
}
37743768
if (Args.hasFlag(options::OPT_fprebuilt_implicit_modules,
37753769
options::OPT_fno_prebuilt_implicit_modules, false))
37763770
CmdArgs.push_back("-fprebuilt-implicit-modules");
@@ -3803,9 +3797,16 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
38033797
// names to precompiled module files (the module is loaded only if used).
38043798
// The -fmodule-file=<file> form can be used to unconditionally load
38053799
// precompiled module files (whether used or not).
3806-
if (HaveModules)
3800+
if (HaveModules || Input.getType() == clang::driver::types::TY_ModuleFile) {
38073801
Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file);
3808-
else
3802+
3803+
// -fprebuilt-module-path specifies where to load the prebuilt module files.
3804+
for (const Arg *A : Args.filtered(options::OPT_fprebuilt_module_path)) {
3805+
CmdArgs.push_back(Args.MakeArgString(
3806+
std::string("-fprebuilt-module-path=") + A->getValue()));
3807+
A->claim();
3808+
}
3809+
} else
38093810
Args.ClaimAllArgs(options::OPT_fmodule_file);
38103811

38113812
// When building modules and generating crashdumps, we need to dump a module

clang/lib/Frontend/ASTMerge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void ASTMergeAction::ExecuteAction() {
4848
/*ShouldOwnClient=*/true));
4949
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
5050
ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
51-
CI.getFileSystemOpts(), false);
51+
CI.getFileSystemOpts(), CI.getHeaderSearchOptsPtr(), false);
5252

5353
if (!Unit)
5454
continue;

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,8 @@ void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
785785
std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
786786
const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
787787
WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
788-
const FileSystemOptions &FileSystemOpts, bool UseDebugInfo,
788+
const FileSystemOptions &FileSystemOpts,
789+
std::shared_ptr<HeaderSearchOptions> HSOpts, bool UseDebugInfo,
789790
bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
790791
bool AllowASTWithCompilerErrors, bool UserFilesAreVolatile,
791792
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
@@ -810,7 +811,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
810811
AST->getFileManager(),
811812
UserFilesAreVolatile);
812813
AST->ModuleCache = new InMemoryModuleCache;
813-
AST->HSOpts = std::make_shared<HeaderSearchOptions>();
814+
AST->HSOpts = HSOpts ? HSOpts : std::make_shared<HeaderSearchOptions>();
814815
AST->HSOpts->ModuleFormat = std::string(PCHContainerRdr.getFormats().front());
815816
AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
816817
AST->getSourceManager(),

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
612612
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
613613
std::string(InputFile), CI.getPCHContainerReader(),
614614
ASTUnit::LoadPreprocessorOnly, ASTDiags, CI.getFileSystemOpts(),
615-
CI.getCodeGenOpts().DebugTypeExtRefs);
615+
/*HeaderSearchOptions=*/nullptr, CI.getCodeGenOpts().DebugTypeExtRefs);
616616
if (!AST)
617617
return false;
618618

@@ -680,6 +680,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
680680
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
681681
std::string(InputFile), CI.getPCHContainerReader(),
682682
ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(),
683+
CI.getHeaderSearchOptsPtr(),
683684
CI.getCodeGenOpts().DebugTypeExtRefs);
684685

685686
if (!AST)

clang/test/Modules/no-implicit-std-cxx-module.cppm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
// RUN: -Wno-read-modules-implicitly -DNO_DIAG
1111
// RUN: %clang_cc1 -std=c++20 %t/user.cpp -fmodule-file=a=%t/a.pcm -fmodule-file=b=%t/b.pcm \
1212
// RUN: -DNO_DIAG -verify -fsyntax-only
13+
//
14+
// RUN: %clang_cc1 -std=c++20 %t/a.pcm -S -emit-llvm -o - 2>&1 | FileCheck %t/a.cppm
15+
// RUN: %clang_cc1 -std=c++20 %t/a.pcm -fmodule-file=b=%t/b.pcm -S -emit-llvm -o - 2>&1 \
16+
// RUN: | FileCheck %t/a.cppm -check-prefix=CHECK-CORRECT
17+
//
18+
// RUN: mkdir -p %t/tmp
19+
// RUN: mv %t/b.pcm %t/tmp/b.pcm
20+
// RUN: not %clang_cc1 -std=c++20 %t/a.pcm -S -emit-llvm -o - 2>&1 \
21+
// RUN: | FileCheck %t/a.cppm -check-prefix=CHECK-ERROR
22+
// RUN: %clang_cc1 -std=c++20 %t/a.pcm -S -emit-llvm -o - 2>&1 -fmodule-file=b=%t/tmp/b.pcm \
23+
// RUN: | FileCheck %t/a.cppm -check-prefix=CHECK-CORRECT
1324

1425
//--- b.cppm
1526
export module b;
@@ -24,6 +35,14 @@ export int a() {
2435
return b() + 43;
2536
}
2637

38+
// CHECK: it is deprecated to read module 'b' implcitly;
39+
40+
// CHECK-CORRECT-NOT: warning
41+
// CHECK-CORRECT-NOT: error
42+
43+
// CHECK-ERROR: error: module file{{.*}}not found: module file not found
44+
45+
2746
//--- user.cpp
2847
#ifdef NO_DIAG
2948
// expected-no-diagnostics

clang/tools/c-index-test/core_main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,13 @@ static bool printSourceSymbolsFromModule(StringRef modulePath,
270270
return true;
271271
}
272272

273+
auto HSOpts = std::make_shared<HeaderSearchOptions>();
274+
273275
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
274276
CompilerInstance::createDiagnostics(new DiagnosticOptions());
275277
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
276278
std::string(modulePath), *pchRdr, ASTUnit::LoadASTOnly, Diags,
277-
FileSystemOpts, /*UseDebugInfo=*/false,
279+
FileSystemOpts, HSOpts, /*UseDebugInfo=*/false,
278280
/*OnlyLocalDecls=*/true, CaptureDiagsKind::None,
279281
/*AllowASTWithCompilerErrors=*/true,
280282
/*UserFilesAreVolatile=*/false);

clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ static bool HandleAST(StringRef AstPath) {
153153

154154
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
155155
AstPath.str(), CI->getPCHContainerOperations()->getRawReader(),
156-
ASTUnit::LoadASTOnly, DiagEngine, CI->getFileSystemOpts());
156+
ASTUnit::LoadASTOnly, DiagEngine, CI->getFileSystemOpts(),
157+
CI->getHeaderSearchOptsPtr());
157158

158159
if (!Unit)
159160
return false;

clang/tools/libclang/CIndex.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3796,14 +3796,15 @@ enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
37963796

37973797
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
37983798
FileSystemOptions FileSystemOpts;
3799+
auto HSOpts = std::make_shared<HeaderSearchOptions>();
37993800

38003801
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
38013802
CompilerInstance::createDiagnostics(new DiagnosticOptions());
38023803
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
38033804
ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(),
3804-
ASTUnit::LoadEverything, Diags, FileSystemOpts, /*UseDebugInfo=*/false,
3805-
CXXIdx->getOnlyLocalDecls(), CaptureDiagsKind::All,
3806-
/*AllowASTWithCompilerErrors=*/true,
3805+
ASTUnit::LoadEverything, Diags, FileSystemOpts, HSOpts,
3806+
/*UseDebugInfo=*/false, CXXIdx->getOnlyLocalDecls(),
3807+
CaptureDiagsKind::All, /*AllowASTWithCompilerErrors=*/true,
38073808
/*UserFilesAreVolatile=*/true);
38083809
*out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU));
38093810
return *out_TU ? CXError_Success : CXError_Failure;

0 commit comments

Comments
 (0)