[clang] [clang] Don't crash in invalid #module directive (PR #208695)
Yihan Wang via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 10 04:01:22 PDT 2026
https://github.com/yronglin created https://github.com/llvm/llvm-project/pull/208695
The following preprocessing directive is not a C++ module directive, don't handle it in C++ module subroutine.
```cpp
#module
```
Fixes https://github.com/llvm/llvm-project/issues/179220.
>From 924c8c8eab54a19115b7747d0174373f89087d0e Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Fri, 10 Jul 2026 03:58:44 -0700
Subject: [PATCH] [clang] Don't crash in invalid #module directive
Signed-off-by: yronglin <yronglin777 at gmail.com>
---
clang/docs/ReleaseNotes.md | 1 +
clang/lib/Lex/PPDirectives.cpp | 7 +++++--
clang/test/Lexer/invalid-directive.cpp | 3 +++
3 files changed, 9 insertions(+), 2 deletions(-)
create mode 100644 clang/test/Lexer/invalid-directive.cpp
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 449a3792eb4ba..3dff6efac576d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -883,6 +883,7 @@ latest release, please see the [Clang Web Site](https://clang.llvm.org) or the
- Correctly diagnose invalid non-dependent calls in dependent contexts. (#GH135694)
- Fix initialization of GRO when GRO-return type mismatches, as part of CWG2563. (#GH98744)
- Fix an error using an initializer list with array new for a type that is not default-constructible. (#GH81157)
+- Fixed a crash in invalid ``#module`` preprocessing directive. (#GH179220)
- We no longer consider conversion operators when copy-initializing from the same type. This was non
conforming and could lead to recursive constraint satisfaction checking. (#GH149443)
- Fixed a crash in Itanium C++ name mangling for a lambda in a local class field initializer inside a constructor/destructor. (#GH176395)
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 20f2575254e7b..76ea6b6a6e19b 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -1455,10 +1455,11 @@ void Preprocessor::HandleDirective(Token &Result) {
return HandlePragmaDirective({PIK_HashPragma, Introducer.getLocation()});
case tok::pp_module:
case tok::pp___preprocessed_module:
- return HandleCXXModuleDirective(Result);
+ if (Introducer.isModuleContextualKeyword())
+ return HandleCXXModuleDirective(Result);
+ break;
case tok::pp___preprocessed_import:
return HandleCXXImportDirective(Result);
- // GNU Extensions.
case tok::pp_import:
switch (Introducer.getKind()) {
case tok::hash:
@@ -1470,6 +1471,8 @@ void Preprocessor::HandleDirective(Token &Result) {
default:
llvm_unreachable("not a valid import directive");
}
+
+ // GNU Extensions.
case tok::pp_include_next:
return HandleIncludeNextDirective(Introducer.getLocation(), Result);
diff --git a/clang/test/Lexer/invalid-directive.cpp b/clang/test/Lexer/invalid-directive.cpp
new file mode 100644
index 0000000000000..c20cc885e7a7b
--- /dev/null
+++ b/clang/test/Lexer/invalid-directive.cpp
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
+
+#module // expected-error {{invalid preprocessing directive}}
More information about the cfe-commits
mailing list