From 5b388f86aa9ce65778677ae56587867d6786355e Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Sat, 6 May 2023 11:10:43 +0800 Subject: [PATCH] [C++20] [Modules] Don't check input files for C++20 Modules Close https://github.com/llvm/llvm-project/issues/62269 Currently, the compiler will emit errors when we compile C++20 modules if the referenced files changed or got removed. This is because we reuse the existing logic from Clang implicit modules. It is helpful for clang implicit modules since it is implicit and we want to be sure things don't go wrong. But it is not necessary for C++20 modules. The C++20 modules is explicit and it is build systems' responsibility to maintain the dependencies. So the check in the compiler side may be an overkill. --- .../include/clang/Serialization/ModuleFile.h | 3 + clang/lib/Serialization/ASTReader.cpp | 15 +++-- clang/lib/Serialization/ASTWriter.cpp | 20 ++++--- clang/test/Modules/cxx20-no-check-input.cppm | 56 +++++++++++++++++++ 4 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 clang/test/Modules/cxx20-no-check-input.cppm diff --git a/clang/include/clang/Serialization/ModuleFile.h b/clang/include/clang/Serialization/ModuleFile.h index 871fdd0a4838..45fb75df8a43 100644 --- a/clang/include/clang/Serialization/ModuleFile.h +++ b/clang/include/clang/Serialization/ModuleFile.h @@ -164,6 +164,9 @@ public: /// Whether this precompiled header is a relocatable PCH file. bool RelocatablePCH = false; + /// Whether this mdoule file is a standard c++ module. + bool StandardCXXModule = false; + /// Whether timestamps are included in this module file. bool HasTimestamps = false; diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 13fec49e841c..9c31f9db1467 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -2389,12 +2389,15 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { StringRef Filename = FI.Filename; uint64_t StoredContentHash = FI.ContentHash; + // For standard C++ modules, we don't need to check the inputs. + bool SkipChecks = F.StandardCXXModule; + OptionalFileEntryRefDegradesToFileEntryPtr File = OptionalFileEntryRef( expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false))); // For an overridden file, create a virtual file with the stored // size/timestamp. - if ((Overridden || Transient) && !File) + if ((Overridden || Transient || SkipChecks) && !File) File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); if (!File) { @@ -2417,7 +2420,7 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { // PCH. SourceManager &SM = getSourceManager(); // FIXME: Reject if the overrides are different. - if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { + if ((!Overridden && !Transient) && !SkipChecks && SM.isFileOverridden(File)) { if (Complain) Error(diag::err_fe_pch_file_overridden, Filename); @@ -2476,7 +2479,7 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { }; bool IsOutOfDate = false; - auto FileChange = HasInputFileChanged(); + auto FileChange = SkipChecks ? Change{Change::None} : HasInputFileChanged(); // For an overridden file, there is nothing to validate. if (!Overridden && FileChange.Kind != Change::None) { if (Complain && !Diags.isDiagnosticInFlight()) { @@ -2821,7 +2824,7 @@ ASTReader::ReadControlBlock(ModuleFile &F, return VersionMismatch; } - bool hasErrors = Record[6]; + bool hasErrors = Record[7]; if (hasErrors && !DisableValidation) { // If requested by the caller and the module hasn't already been read // or compiled, mark modules on error as out-of-date. @@ -2845,7 +2848,9 @@ ASTReader::ReadControlBlock(ModuleFile &F, if (F.RelocatablePCH) F.BaseDirectory = isysroot.empty() ? "/" : isysroot; - F.HasTimestamps = Record[5]; + F.StandardCXXModule = Record[5]; + + F.HasTimestamps = Record[6]; const std::string &CurBranch = getClangFullRepositoryVersion(); StringRef ASTBranch = Blob; diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 94c851d2dbf1..e124cb4c5f81 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -1243,6 +1243,8 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context, MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj. MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min. MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable + // Standard C++ module + MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag @@ -1250,15 +1252,15 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context, assert((!WritingModule || isysroot.empty()) && "writing module as a relocatable PCH?"); { - RecordData::value_type Record[] = { - METADATA, - VERSION_MAJOR, - VERSION_MINOR, - CLANG_VERSION_MAJOR, - CLANG_VERSION_MINOR, - !isysroot.empty(), - IncludeTimestamps, - ASTHasCompilerErrors}; + RecordData::value_type Record[] = {METADATA, + VERSION_MAJOR, + VERSION_MINOR, + CLANG_VERSION_MAJOR, + CLANG_VERSION_MINOR, + !isysroot.empty(), + isWritingStdCXXNamedModules(), + IncludeTimestamps, + ASTHasCompilerErrors}; Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record, getClangFullRepositoryVersion()); } diff --git a/clang/test/Modules/cxx20-no-check-input.cppm b/clang/test/Modules/cxx20-no-check-input.cppm new file mode 100644 index 000000000000..f4b42b444f2e --- /dev/null +++ b/clang/test/Modules/cxx20-no-check-input.cppm @@ -0,0 +1,56 @@ +// RUN: rm -rf %t +// RUN: split-file %s %t +// RUN: cd %t +// +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/a.cppm -emit-module-interface -o %t/a.pcm +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/use.cpp -fmodule-file=a=%t/a.pcm -verify -fsyntax-only +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/a.pcm -emit-llvm -o - | FileCheck %t/a.ll +// +// RUN: echo "//Update" >> %t/foo.h +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/use.cpp -fmodule-file=a=%t/a.pcm -verify -fsyntax-only +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/a.pcm -emit-llvm -o - | FileCheck %t/a.ll +// +// RUN: echo "//Update" >> %t/a.cppm +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/use.cpp -fmodule-file=a=%t/a.pcm -verify -fsyntax-only +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/a.pcm -emit-llvm -o - | FileCheck %t/a.ll +// +// RUN: rm -f %t/foo.h +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/use.cpp -fmodule-file=a=%t/a.pcm -verify -fsyntax-only +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/a.pcm -emit-llvm -o - | FileCheck %t/a.ll +// +// RUN: rm -f %t/a.cppm +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/use.cpp -fmodule-file=a=%t/a.pcm -verify -fsyntax-only +// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \ +// RUN: %t/a.pcm -emit-llvm -o - | FileCheck %t/a.ll + +//--- foo.h +inline int foo = 43; + +//--- a.cppm +// expected-no-diagnostics +module; +#include "foo.h" +export module a; +export using ::foo; + +//--- a.ll +// check the LLVM IR are generated succesfully. +// CHECK: define{{.*}}@_ZGIW1a + +//--- use.cpp +// expected-no-diagnostics +import a; +int use() { + return foo; +} +