[clang] [clang][Lex] Add null check for `IncludeTok` in `PreprocessingRecord::InclusionDirective` (PR #192051)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 15 05:06:02 PDT 2026
https://github.com/StepfenShawn updated https://github.com/llvm/llvm-project/pull/192051
>From 080b4059b99669509ee1fb122ab43bfcb75af98c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Tue, 14 Apr 2026 20:53:06 +0800
Subject: [PATCH 1/3] Add null check for IncludeTok in PreprocessingRecord.cpp
---
clang/lib/Lex/PreprocessingRecord.cpp | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Lex/PreprocessingRecord.cpp b/clang/lib/Lex/PreprocessingRecord.cpp
index f76155b1c45d6..d0f09552a003b 100644
--- a/clang/lib/Lex/PreprocessingRecord.cpp
+++ b/clang/lib/Lex/PreprocessingRecord.cpp
@@ -473,7 +473,11 @@ void PreprocessingRecord::InclusionDirective(
bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
- switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
+ IdentifierInfo *II = IncludeTok.getIdentifierInfo();
+ if (!II)
+ llvm_unreachable("Invalid include directive token");
+
+ switch (II->getPPKeywordID()) {
case tok::pp_include:
Kind = InclusionDirective::Include;
break;
>From 36c41a4d97898994ea22145d93037d25317e91ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Tue, 14 Apr 2026 23:09:55 +0800
Subject: [PATCH 2/3] Replace llvm_unreachable with assert for include token
---
clang/lib/Lex/PreprocessingRecord.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/Lex/PreprocessingRecord.cpp b/clang/lib/Lex/PreprocessingRecord.cpp
index d0f09552a003b..99914c1353e1c 100644
--- a/clang/lib/Lex/PreprocessingRecord.cpp
+++ b/clang/lib/Lex/PreprocessingRecord.cpp
@@ -474,8 +474,7 @@ void PreprocessingRecord::InclusionDirective(
InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
IdentifierInfo *II = IncludeTok.getIdentifierInfo();
- if (!II)
- llvm_unreachable("Invalid include directive token");
+ assert(II && "Invalid include directive token");
switch (II->getPPKeywordID()) {
case tok::pp_include:
>From f27d89ef473b525a16c42382745af13821f86e22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Wed, 15 Apr 2026 20:05:51 +0800
Subject: [PATCH 3/3] Add test for inclusion directive
---
clang/test/Lexer/pp-inclusion-directive.c | 8 ++++++++
clang/test/Lexer/pp-inclusion-directive.h | 1 +
2 files changed, 9 insertions(+)
create mode 100644 clang/test/Lexer/pp-inclusion-directive.c
create mode 100644 clang/test/Lexer/pp-inclusion-directive.h
diff --git a/clang/test/Lexer/pp-inclusion-directive.c b/clang/test/Lexer/pp-inclusion-directive.c
new file mode 100644
index 0000000000000..0cce9ace9aa83
--- /dev/null
+++ b/clang/test/Lexer/pp-inclusion-directive.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -detailed-preprocessing-record %s
+// Test that PreprocessingRecord::InclusionDirective correctly handles various
+// inclusion directive kinds (include, import, include_next).
+
+#include "pp-inclusion-directive.h"
+#include <pp-inclusion-directive.h>
+ at import pp-inclusion-directive;
+#include_next pp-inclusion-directive.h
diff --git a/clang/test/Lexer/pp-inclusion-directive.h b/clang/test/Lexer/pp-inclusion-directive.h
new file mode 100644
index 0000000000000..dbc3c8440097a
--- /dev/null
+++ b/clang/test/Lexer/pp-inclusion-directive.h
@@ -0,0 +1 @@
+/* Header for pp-inclusion-directive.c test */
More information about the cfe-commits
mailing list