Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,9 @@ def err_import_in_wrong_fragment : Error<
"module%select{| partition}0 imports cannot be in the %select{global|private}1 module fragment">;

def err_export_empty : Error<"export declaration cannot be empty">;
def err_import_decl_not_in_module_fragment : Error<
"module import declaration can only appears in "
"global module fragment, module interface or module implementation">;
}

let CategoryName = "Generics Issue" in {
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,13 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
SeenError = false;
break;
case Sema::ModuleImportState::FirstDecl:
if (getLangOpts().CPlusPlusModules) {
Diag(ImportLoc, diag::err_import_decl_not_in_module_fragment);
Diag(ImportLoc, diag::note_global_module_introducer_missing)
<< FixItHint::CreateInsertion(PP.getMainFileFirstPPTokenLoc(),
"module;");
}

// If we found an import decl as the first declaration, we must be not in
// a C++20 module unit or we are in an invalid state.
ImportState = Sema::ModuleImportState::NotACXX20Module;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
Diag(ModuleLoc, diag::err_module_decl_not_at_start);
SourceLocation BeginLoc = PP.getMainFileFirstPPTokenLoc();
Diag(BeginLoc, diag::note_global_module_introducer_missing)
<< FixItHint::CreateInsertion(BeginLoc, "module;\n");
<< FixItHint::CreateInsertion(BeginLoc, "module;");
}

// C++23 [module.unit]p1: ... The identifiers module and import shall not
Expand Down
10 changes: 10 additions & 0 deletions clang/test/CXX/module/cpp.pre/p1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: split-file %s %t

// RUN: %clang_cc1 -std=c++20 %t/import_is_first_decl.cpp -fsyntax-only -verify

//--- import_is_first_decl.cpp
import std; // expected-error {{module import declaration can only appears in global module fragment, module interface or module implementation}}
// expected-note@-1 {{add 'module;' to the start of the file to introduce a global module fragment}}
// expected-error@-2 {{module 'std' not found}}
Loading