[clang] [Clang] Require a module name before tracking a partition (PR #245488)

Chen Miao via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 21:05:13 PST 2026


https://github.com/ChenMiaoi created https://github.com/llvm/llvm-project/pull/245488

A malformed module declaration can cause the preprocessor to record `:` as the module name. A subsequent angled include then triggers a spurious `-Winclude-angled-in-module-purview` warning. Only append the partition separator after a module name has been seen.

For example, with an empty `probe.h`:

```cc
export module :oops;
#include <probe.h>
```

Before:

```text
<stdin>:1:20: warning: extra tokens after semicolon in 'module' directive
    [-Wextra-tokens]
<stdin>:2:10: warning: '#include <filename>' attaches the declarations to
    the named module ':', which is not usually intended; consider moving
    that directive before the module declaration
    [-Winclude-angled-in-module-purview]
```

After:

```text
<stdin>:1:20: warning: extra tokens after semicolon in 'module' directive
    [-Wextra-tokens]
```

>From a2057cf15e75a2c37363875520b00d2747f10ec7 Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Tue, 24 Nov 2026 13:03:18 +0800
Subject: [PATCH] [Clang] Require a module name before tracking a partition

A malformed module declaration can cause the preprocessor to record `:`
as the module name. A subsequent angled include then triggers a spurious
`-Winclude-angled-in-module-purview` warning. Only append the partition
separator after a module name has been seen.

For example, with an empty `probe.h`:

```cc
export module :oops;
#include <probe.h>
```

Before:

```text
<stdin>:1:20: warning: extra tokens after semicolon in 'module' directive
    [-Wextra-tokens]
<stdin>:2:10: warning: '#include <filename>' attaches the declarations to
    the named module ':', which is not usually intended; consider moving
    that directive before the module declaration
    [-Winclude-angled-in-module-purview]
```

After:

```text
<stdin>:1:20: warning: extra tokens after semicolon in 'module' directive
    [-Wextra-tokens]
```
---
 clang/include/clang/Lex/Preprocessor.h                 | 2 +-
 clang/test/Preprocessor/include-in-module-purview.cppm | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index 8d15ed7e8afd85..70af444794de55 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -607,7 +607,7 @@ class Preprocessor {
     }
 
     void handleColon() {
-      if (isModuleCandidate())
+      if (!Name.empty() && isModuleCandidate())
         Name += ":";
       else if (!isNamedModule())
         reset();
diff --git a/clang/test/Preprocessor/include-in-module-purview.cppm b/clang/test/Preprocessor/include-in-module-purview.cppm
index 4a7883653ac45d..9fdaef67b56bbd 100644
--- a/clang/test/Preprocessor/include-in-module-purview.cppm
+++ b/clang/test/Preprocessor/include-in-module-purview.cppm
@@ -6,6 +6,8 @@
 // RUN: %clang_cc1 -std=c++20 %t/a.cppm -E -P -I%t -o - 2>&1 \
 // RUN:     -Wno-include-angled-in-module-purview | FileCheck %t/a.cppm --check-prefix=CHECK-NO-WARN
 // RUN: %clang_cc1 -std=c++20 %t/b.cpp -E -P -I%t -o - 2>&1 | FileCheck %t/a.cppm --check-prefix=CHECK-NO-WARN
+// RUN: %clang_cc1 -std=c++20 -E -verify -Wno-extra-tokens \
+// RUN:   -Winclude-angled-in-module-purview -I%t -o %t/tmp %t/invalid.cppm
 
 //--- a.h
 // left empty
@@ -66,3 +68,8 @@ module :private;
 struct module {};
 void foo(module m);
 #include <stddef.h>
+
+//--- invalid.cppm
+// expected-no-diagnostics
+export module :oops;
+#include <a.h>



More information about the cfe-commits mailing list