From be66c506c7fd6fdb7363f724075d02ca0d35713a Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Fri, 9 Aug 2024 13:39:24 +0800 Subject: [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. --- clang/lib/Sema/Sema.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'clang/lib/Sema/Sema.cpp') diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 19d8692..633b822 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. -- cgit v1.1