Skip to content

Commit d7e5043

Browse files
committed
Remove error recovery
Signed-off-by: yronglin <[email protected]>
1 parent c07beac commit d7e5043

File tree

5 files changed

+38
-80
lines changed

5 files changed

+38
-80
lines changed

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,16 +1780,6 @@ def ext_bit_int : Extension<
17801780
} // end of Parse Issue category.
17811781

17821782
let CategoryName = "Modules Issue" in {
1783-
def err_invalid_module_or_import_directive : Error<
1784-
"the %select{module|import}0 directive is ill-formed, "
1785-
"%select{module keyword must be immediately "
1786-
"followed on the same line by an identifier, "
1787-
"or a ';' after being at the start of a line, or preceded by "
1788-
"an export keyword at the start of a line|"
1789-
"import keyword must be immediately followed "
1790-
"on the same line by an identifier, '<', '\"', or ':', but not '::', "
1791-
"after being at the start of a line or preceded by an export at "
1792-
"the start of the line}0">;
17931783
def err_unexpected_module_or_import_decl : Error<
17941784
"%select{module|import}0 declaration can only appear at the top level">;
17951785
def err_module_expected_ident : Error<

clang/lib/Lex/Preprocessor.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "llvm/Support/Capacity.h"
6262
#include "llvm/Support/ErrorHandling.h"
6363
#include "llvm/Support/MemoryBuffer.h"
64+
#include "llvm/Support/SaveAndRestore.h"
6465
#include "llvm/Support/raw_ostream.h"
6566
#include <algorithm>
6667
#include <cassert>
@@ -1195,21 +1196,20 @@ bool Preprocessor::HandleModuleContextualKeyword(
11951196
} else if (!TokAtPhysicalStartOfLine)
11961197
return false;
11971198

1198-
bool SavedParsingPreprocessorDirective =
1199-
CurPPLexer->ParsingPreprocessorDirective;
1200-
CurPPLexer->ParsingPreprocessorDirective = true;
1201-
auto _ = llvm::make_scope_exit([&]() {
1202-
CurPPLexer->ParsingPreprocessorDirective =
1203-
SavedParsingPreprocessorDirective;
1204-
});
1205-
1206-
if (Result.getIdentifierInfo()->isModulesImport() &&
1207-
isNextPPTokenOneOf(tok::raw_identifier, tok::less, tok::string_literal,
1208-
tok::colon)) {
1209-
Result.setKind(tok::kw_import);
1210-
ModuleImportLoc = Result.getLocation();
1211-
IsAtImport = false;
1212-
return true;
1199+
llvm::SaveAndRestore<bool> SavedParsingPreprocessorDirective(
1200+
CurPPLexer->ParsingPreprocessorDirective, true);
1201+
1202+
if (Result.getIdentifierInfo()->isModulesImport()) {
1203+
// The next token may be an angled string literal.
1204+
llvm::SaveAndRestore<bool> SavedParsingFilemame(CurPPLexer->ParsingFilename,
1205+
true);
1206+
if (isNextPPTokenOneOf(tok::raw_identifier, tok::less, tok::string_literal,
1207+
tok::colon, tok::header_name)) {
1208+
Result.setKind(tok::kw_import);
1209+
ModuleImportLoc = Result.getLocation();
1210+
IsAtImport = false;
1211+
return true;
1212+
}
12131213
}
12141214

12151215
if (Result.getIdentifierInfo()->isModulesDeclaration() &&

clang/lib/Parse/Parser.cpp

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -623,24 +623,6 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result,
623623
goto module_decl;
624624
case tok::kw_import:
625625
goto import_decl;
626-
// Error recovery and recognize context-sensitive C++20 'export module' and
627-
// 'export import' declarations. If the module/import directive is
628-
// well-formed, it should be converted to a keyword in preprocessor, but not
629-
// an identifier we saw here.
630-
//
631-
// FIXME: We should generate better diagnostic information here to explain
632-
// why the module/import directive is ill-formed.
633-
case tok::identifier: {
634-
if (NextToken().isModuleContextualKeyword(getLangOpts()) &&
635-
GetLookAheadToken(2).isNot(tok::coloncolon)) {
636-
if (NextToken().getIdentifierInfo()->isStr(
637-
tok::getKeywordSpelling(tok::kw_module)))
638-
goto module_decl;
639-
else
640-
goto import_decl;
641-
}
642-
break;
643-
}
644626
default:
645627
break;
646628
}
@@ -708,25 +690,6 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result,
708690
Actions.ActOnEndOfTranslationUnit();
709691
//else don't tell Sema that we ended parsing: more input might come.
710692
return true;
711-
case tok::identifier:
712-
// C++20 [basic.link]p3:
713-
// A token sequence beginning with 'export[opt] module' or
714-
// 'export[opt] import' and not immediately followed by '::'
715-
// is never interpreted as the declaration of a top-level-declaration.
716-
//
717-
// Error recovery and recognize context-sensitive C++20 'export module' and
718-
// 'export import' declarations. If the module/import directive is
719-
// well-formed, it should be converted to a keyword in preprocessor, but not
720-
// an identifier we saw here.
721-
if (Tok.isModuleContextualKeyword(getLangOpts()) &&
722-
NextToken().isNot(tok::coloncolon)) {
723-
if (Tok.getIdentifierInfo()->isStr(
724-
tok::getKeywordSpelling(tok::kw_module)))
725-
goto module_decl;
726-
else
727-
goto import_decl;
728-
}
729-
break;
730693
default:
731694
break;
732695
}
@@ -2362,10 +2325,6 @@ Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) {
23622325
tok::getKeywordSpelling(tok::kw_module)))) &&
23632326
"not a module declaration");
23642327

2365-
if (getLangOpts().CPlusPlusModules && Tok.is(tok::identifier))
2366-
Diag(StartLoc, diag::err_invalid_module_or_import_directive)
2367-
<< /*IsImport=*/false;
2368-
23692328
SourceLocation ModuleLoc = ConsumeToken();
23702329

23712330
// Attributes appear after the module name, not before.
@@ -2449,12 +2408,6 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
24492408
: Tok.isObjCAtKeyword(tok::objc_import)) &&
24502409
"Improper start to module import");
24512410
bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import);
2452-
if (getLangOpts().CPlusPlusModules && !IsObjCAtImport &&
2453-
Tok.is(tok::identifier)) {
2454-
Diag(StartLoc, diag::err_invalid_module_or_import_directive)
2455-
<< /*IsImport=*/true;
2456-
}
2457-
24582411
SourceLocation ImportLoc = ConsumeToken();
24592412

24602413
// For C++20 modules, we can have "name" or ":Partition name" as valid input.

clang/test/CXX/module/cpp.pre/p1.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
// RUN: %clang_cc1 -std=c++20 %t/foo.cppm -emit-module-interface -o %t/foo.pcm
2121
// RUN: %clang_cc1 -std=c++20 %t/import_decl_not_in_same_line.cpp -fmodule-file=foo=%t/foo.pcm -fsyntax-only -verify
2222
// RUN: %clang_cc1 -std=c++20 %t/not_import.cpp -fsyntax-only -verify
23+
// RUN: %clang_cc1 -std=c++20 %t/import_spaceship.cpp -fsyntax-only -verify
24+
// RUN: %clang_cc1 -std=c++20 %t/leading_empty_macro.cpp -fsyntax-only -verify
25+
2326

2427
//--- hash.cpp
2528
// expected-no-diagnostics
@@ -54,22 +57,34 @@ import rightpad; // preprocessing directive
5457
import :part; // preprocessing directive
5558

5659
//--- module_decl_not_in_same_line.cpp
57-
module // expected-error {{the module directive is ill-formed, module keyword must be immediately followed on the same line by an identifier, or a ';' after being at the start of a line, or preceded by an export keyword at the start of a line}}
58-
;export module M; // expected-error {{the module directive is ill-formed, module keyword must be immediately followed on the same line by an identifier, or a ';' after being at the start of a line, or preceded by an export keyword at the start of a line}}
60+
module // expected-error {{a type specifier is required for all declarations}}
61+
;export module M; // expected-error {{export declaration can only be used within a module interface}} \
62+
// expected-error {{unknown type name 'module'}}
5963

6064
//--- foo.cppm
6165
export module foo;
6266

6367
//--- import_decl_not_in_same_line.cpp
6468
export module M;
65-
export // expected-error {{the import directive is ill-formed, import keyword must be immediately followed on the same line by an identifier, '<', '"', or ':', but not '::', after being at the start of a line or preceded by an export at the start of the line}}
66-
import
69+
export
70+
import // expected-error {{unknown type name 'import'}}
6771
foo;
6872

69-
export // expected-error {{the import directive is ill-formed, import keyword must be immediately followed on the same line by an identifier, '<', '"', or ':', but not '::', after being at the start of a line or preceded by an export at the start of the line}}
70-
import foo;
73+
export
74+
import foo; // expected-error {{unknown type name 'import'}}
7175

7276
//--- not_import.cpp
7377
export module M;
7478
import :: // expected-error {{use of undeclared identifier 'import'}}
7579
import -> // expected-error {{cannot use arrow operator on a type}}
80+
81+
//--- import_spaceship.cpp
82+
export module M;
83+
import <=>; // expected-error {{'=' file not found}}
84+
85+
//--- leading_empty_macro.cpp
86+
// expected-no-diagnostics
87+
export module M;
88+
typedef int import;
89+
#define EMP
90+
EMP import m; // The phase 7 grammar should see import as a typedef-name.

clang/test/CXX/module/dcl.dcl/dcl.module/dcl.module.import/p1.cppm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import x [[blarg::noreturn]]; // expected-warning-re {{unknown attribute 'blarg:
4545

4646
import x.y;
4747
import x.; // expected-error {{expected identifier after '.' in module name}}
48-
import .x; // expected-error-re {{the import directive is ill-formed{{.*}}}} expected-error {{expected module name}}
48+
import .x; // expected-error {{unknown type name 'import'}} expected-error {{cannot use dot operator on a type}}
4949

5050
import blarg; // expected-error {{module 'blarg' not found}}
5151

@@ -63,7 +63,7 @@ import x [[blarg::noreturn]]; // expected-warning-re {{unknown attribute 'blarg:
6363

6464
import x.y;
6565
import x.; // expected-error {{expected identifier after '.' in module name}}
66-
import .x; // expected-error-re {{the import directive is ill-formed{{.*}}}} expected-error {{expected module name}}
66+
import .x; // expected-error {{unknown type name 'import'}} expected-error {{cannot use dot operator on a type}}
6767

6868
import blarg; // expected-error {{module 'blarg' not found}}
6969

0 commit comments

Comments
 (0)