[clang] f665164 - [clang] Don't crash in invalid #module directive (#208695)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 10 06:57:28 PDT 2026
Author: Yihan Wang
Date: 2026-07-10T21:57:23+08:00
New Revision: f66516456d3cf4cff2c1ab6d83afac6e2f9e4c26
URL: https://github.com/llvm/llvm-project/commit/f66516456d3cf4cff2c1ab6d83afac6e2f9e4c26
DIFF: https://github.com/llvm/llvm-project/commit/f66516456d3cf4cff2c1ab6d83afac6e2f9e4c26.diff
LOG: [clang] Don't crash in invalid #module directive (#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.
Signed-off-by: yronglin <yronglin777 at gmail.com>
Added:
clang/test/Lexer/invalid-directive.cpp
Modified:
clang/docs/ReleaseNotes.md
clang/lib/Lex/PPDirectives.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index bec72fc78394b..96317a3eece97 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -889,6 +889,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