Author: Chuanqi Xu Date: 2024-08-09T13:39:24+08:00 New Revision: be66c506c7fd6fdb7363f724075d02ca0d35713a
URL: https://github.com/llvm/llvm-project/commit/be66c506c7fd6fdb7363f724075d02ca0d35713a DIFF: https://github.com/llvm/llvm-project/commit/be66c506c7fd6fdb7363f724075d02ca0d35713a.diff LOG: [C++20] [Modules] Emit Errors when compiling a non-module source as module (#102565) Close https://github.com/llvm/llvm-project/issues/101398 The root cause of the issue is that I removed the codes before and failed to recognize it in time and this was not found for a long time due to it only crashes with invalid codes. Added: clang/test/Modules/pr101398.cppm Modified: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/Sema.cpp clang/test/CXX/module/dcl.dcl/dcl.module/dcl.module.interface/p1.cppm Removed: ################################################################################ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 5cdf36660b2a66..554dbaff2ce0d8 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11693,6 +11693,8 @@ def err_module_not_defined : Error< def err_module_redeclaration : Error< "translation unit contains multiple module declarations">; def note_prev_module_declaration : Note<"previous module declaration is here">; +def err_module_declaration_missing : Error< + "missing 'export module' declaration in module interface unit">; def err_module_declaration_missing_after_global_module_introducer : Error< "missing 'module' declaration at end of global module fragment " "introduced here">; diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 19d8692ee64849..633b8220ffbf11 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -1272,6 +1272,18 @@ void Sema::ActOnEndOfTranslationUnit() { Module::ExplicitGlobalModuleFragment) { Diag(ModuleScopes.back().BeginLoc, diag::err_module_declaration_missing_after_global_module_introducer); + } else if (getLangOpts().getCompilingModule() == + LangOptions::CMK_ModuleInterface && + // We can't use ModuleScopes here since ModuleScopes is always + // empty if we're compiling the BMI. + !getASTContext().getCurrentNamedModule()) { + // If we are building a module interface unit, we should have seen the + // module declaration. + // + // FIXME: Make a better guess as to where to put the module declaration. + Diag(getSourceManager().getLocForStartOfFile( + getSourceManager().getMainFileID()), + diag::err_module_declaration_missing); } // Now we can decide whether the modules we're building need an initializer. diff --git a/clang/test/CXX/module/dcl.dcl/dcl.module/dcl.module.interface/p1.cppm b/clang/test/CXX/module/dcl.dcl/dcl.module/dcl.module.interface/p1.cppm index 1a01ffac0154ae..84ef85126c369a 100644 --- a/clang/test/CXX/module/dcl.dcl/dcl.module/dcl.module.interface/p1.cppm +++ b/clang/test/CXX/module/dcl.dcl/dcl.module/dcl.module.interface/p1.cppm @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -std=c++20 %s -verify -o /dev/null +// RUN: %clang_cc1 -std=c++20 %s -verify -emit-module-interface -o /dev/null // RUN: %clang_cc1 -std=c++20 %s -DINTERFACE -verify -emit-module-interface -o %t // RUN: %clang_cc1 -std=c++20 %s -DIMPLEMENTATION -verify -fmodule-file=A=%t -o /dev/null // @@ -15,6 +15,8 @@ module A; // #module-decl // expected-error@-2 {{missing 'export' specifier in module declaration while building module interface}} #define INTERFACE #endif +#else // Not in a module +// expected-error@* {{missing 'export module' declaration in module interface unit}} #endif #ifndef INTERFACE diff --git a/clang/test/Modules/pr101398.cppm b/clang/test/Modules/pr101398.cppm new file mode 100644 index 00000000000000..843d0ce84fdce3 --- /dev/null +++ b/clang/test/Modules/pr101398.cppm @@ -0,0 +1,5 @@ +// RUN: mkdir -p %t +// RUN: %clang -std=c++20 -xc++-module %s -Xclang -verify --precompile -o %t/tmp.pcm +// not modules + +// expected-error@* {{missing 'export module' declaration in module interface unit}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits