[clang] 96122b5 - [C++20] [Modules] Introduce ForceCheckCXX20ModulesInputFiles options for
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 8 01:53:30 PDT 2023
Author: Chuanqi Xu
Date: 2023-09-08T16:53:12+08:00
New Revision: 96122b5b717c2b1a291d7298ac50b1daf02bd97c
URL: https://github.com/llvm/llvm-project/commit/96122b5b717c2b1a291d7298ac50b1daf02bd97c
DIFF: https://github.com/llvm/llvm-project/commit/96122b5b717c2b1a291d7298ac50b1daf02bd97c.diff
LOG: [C++20] [Modules] Introduce ForceCheckCXX20ModulesInputFiles options for
C++20 modules
Previously, we banned the check for input files from C++20 modules since
we thought the BMI from C++20 modules should be a standalone artifact.
However, during the recent experiment with clangd for modules, I find
it is necessary to tell whether or not a BMI is out-of-date by checking the
input files especially for language servers.
So this patch brings a header search option
ForceCheckCXX20ModulesInputFiles to allow the tools (concretly, clangd)
to check the input files from BMI.
Added:
clang/test/Modules/cxx20-force-check-input.cppm
Modified:
clang/include/clang/Driver/Options.td
clang/include/clang/Lex/HeaderSearchOptions.h
clang/lib/Frontend/ASTUnit.cpp
clang/lib/Serialization/ASTReader.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index b29e2e53f938544..4bc292c1fa1620c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2906,6 +2906,11 @@ def fvalidate_ast_input_files_content:
" Files with mismatching mtime's are considered valid"
" if both contents is identical">,
MarshallingInfoFlag<HeaderSearchOpts<"ValidateASTInputFilesContent">>;
+def fforce_check_cxx20_modules_input_files:
+ Flag <["-"], "fforce-check-cxx20-modules-input-files">,
+ Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
+ HelpText<"Check the input source files from C++20 modules explicitly">,
+ MarshallingInfoFlag<HeaderSearchOpts<"ForceCheckCXX20ModulesInputFiles">>;
def fmodules_validate_input_files_content:
Flag <["-"], "fmodules-validate-input-files-content">,
Group<f_Group>, Flags<[NoXarchOption]>,
diff --git a/clang/include/clang/Lex/HeaderSearchOptions.h b/clang/include/clang/Lex/HeaderSearchOptions.h
index 6436a9b3bde20a2..126659f3ac00223 100644
--- a/clang/include/clang/Lex/HeaderSearchOptions.h
+++ b/clang/include/clang/Lex/HeaderSearchOptions.h
@@ -211,6 +211,9 @@ class HeaderSearchOptions {
// validate consistency.
unsigned ValidateASTInputFilesContent : 1;
+ // Whether the input files from C++20 Modules should be checked.
+ unsigned ForceCheckCXX20ModulesInputFiles : 1;
+
/// Whether the module includes debug information (-gmodules).
unsigned UseDebugInfo : 1;
@@ -233,7 +236,8 @@ class HeaderSearchOptions {
UseStandardCXXIncludes(true), UseLibcxx(false), Verbose(false),
ModulesValidateOncePerBuildSession(false),
ModulesValidateSystemHeaders(false),
- ValidateASTInputFilesContent(false), UseDebugInfo(false),
+ ValidateASTInputFilesContent(false),
+ ForceCheckCXX20ModulesInputFiles(false), UseDebugInfo(false),
ModulesValidateDiagnosticOptions(true), ModulesHashContent(false),
ModulesStrictContextHash(false) {}
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index a4753f525d9de2d..167db521a890cd9 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -550,12 +550,16 @@ class ASTInfoCollector : public ASTReaderListener {
bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
StringRef SpecificModuleCachePath,
bool Complain) override {
- // Preserve previously set header search paths.
+ // llvm::SaveAndRestore doesn't support bit field.
+ auto ForceCheckCXX20ModulesInputFiles =
+ this->HSOpts.ForceCheckCXX20ModulesInputFiles;
llvm::SaveAndRestore X(this->HSOpts.UserEntries);
llvm::SaveAndRestore Y(this->HSOpts.SystemHeaderPrefixes);
llvm::SaveAndRestore Z(this->HSOpts.VFSOverlayFiles);
this->HSOpts = HSOpts;
+ this->HSOpts.ForceCheckCXX20ModulesInputFiles =
+ ForceCheckCXX20ModulesInputFiles;
return false;
}
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 8ece979f2408e4d..576da09095002b0 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -2428,6 +2428,16 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
// For standard C++ modules, we don't need to check the inputs.
bool SkipChecks = F.StandardCXXModule;
+ const HeaderSearchOptions &HSOpts =
+ PP.getHeaderSearchInfo().getHeaderSearchOpts();
+
+ // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
+ // modules.
+ if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
+ SkipChecks = false;
+ Overridden = false;
+ }
+
OptionalFileEntryRefDegradesToFileEntryPtr File = OptionalFileEntryRef(
expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)));
diff --git a/clang/test/Modules/cxx20-force-check-input.cppm b/clang/test/Modules/cxx20-force-check-input.cppm
new file mode 100644
index 000000000000000..4611f8f7cafa17e
--- /dev/null
+++ b/clang/test/Modules/cxx20-force-check-input.cppm
@@ -0,0 +1,33 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \
+// RUN: %t/a.cppm -emit-module-interface -o %t/a.pcm
+//
+// RUN: echo "inline int bar = 46;" >> %t/foo.h
+// RUNX: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \
+// RUNX: %t/use.cpp -fmodule-file=a=%t/a.pcm -verify -fsyntax-only
+// RUNX: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \
+// RUNX: %t/a.pcm -emit-llvm -o - | FileCheck %t/a.ll
+//
+// RUN: echo "export int var = 43;" >> %t/a.cppm
+// RUNX: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \
+// RUNX: %t/use.cpp -fmodule-file=a=%t/a.pcm -verify -fsyntax-only
+// RUNX: %clang_cc1 -std=c++20 -triple %itanium_abi_triple \
+// RUNX: %t/a.pcm -emit-llvm -o - | FileCheck %t/a.ll
+//
+// RUN: not %clang_cc1 -std=c++20 -triple %itanium_abi_triple \
+// RUN: -fforce-check-cxx20-modules-input-files %t/a.pcm \
+// RUN: -emit-llvm -o - 2>&1 | FileCheck %t/a.cppm -check-prefix=CHECK-FAILURE
+
+//--- foo.h
+inline int foo = 43;
+
+//--- a.cppm
+// expected-no-diagnostics
+module;
+#include "foo.h"
+export module a;
+export using ::foo;
+
+// CHECK-FAILURE: fatal error:{{.*}}a.cppm' has been modified since the AST file {{.*}}was built
More information about the cfe-commits
mailing list