https://github.com/jansvoboda11 created https://github.com/llvm/llvm-project/pull/216704
Using `ModuleToImport` when deciding whether to turn a repeated include into an import, or whether to skip it, isn't right. We have `ModuleToImport` even without `-fmodules`. This PR starts checking `UsableClangHeaderModule`, matching what we do for the first inclusion of that header. rdar://184549117 >From 3c723fa31c4e3934188daf39116fa6c60d4d2a36 Mon Sep 17 00:00:00 2001 From: Jan Svoboda <[email protected]> Date: Mon, 17 Aug 2026 13:45:08 +0200 Subject: [PATCH] [clang][lex] Do not translate repeated include into import --- clang/lib/Lex/PPDirectives.cpp | 3 +- .../Modules/non-modular-with-module-file.c | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 clang/test/Modules/non-modular-with-module-file.c diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index ec387a4d582fc..24e63b8bae711 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -2590,7 +2590,8 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport( if (UsableHeaderUnit && !getLangOpts().CompilingPCH) Action = TrackGMFState.inGMF() ? Import : Skip; else - Action = (ModuleToImport && !getLangOpts().CompilingPCH) ? Import : Skip; + Action = (UsableClangHeaderModule && !getLangOpts().CompilingPCH) ? Import + : Skip; } // Check for circular inclusion of the main file. diff --git a/clang/test/Modules/non-modular-with-module-file.c b/clang/test/Modules/non-modular-with-module-file.c new file mode 100644 index 0000000000000..f486d9564c2d0 --- /dev/null +++ b/clang/test/Modules/non-modular-with-module-file.c @@ -0,0 +1,31 @@ +// Check that repeated inclusion of a modular header doesn't get translated +// into an import in textual compilation. + +// RUN: rm -rf %t +// RUN: split-file %s %t + +// RUN: %clang_cc1 -E %t/tu.c -o %t/tu.ii -I %t -fmodule-map-file=%t/module.modulemap + +// RUN: FileCheck --input-file=%t/tu.ii %s -DPREFIX=%/t + +// CHECK: # 1 "[[PREFIX]]/tu.c" +// CHECK-NEXT: # 1 "<built-in>" 1 +// CHECK-NEXT: # 1 "<built-in>" 3 +// CHECK-NEXT: # {{[0-9]+}} "<built-in>" 3 +// CHECK-NEXT: # 1 "<command line>" 1 +// CHECK-NEXT: # 1 "<built-in>" 2 +// CHECK-NEXT: # 1 "[[PREFIX]]/tu.c" 2 +// CHECK-NEXT: # 1 "[[PREFIX]]/Mod.h" 1 +// CHECK-NEXT: #pragma clang module begin Mod +// CHECK-NEXT: # 2 "[[PREFIX]]/tu.c" 2 +// CHECK-NEXT: # 1 "[[PREFIX]]/tu.c" +// CHECK-NEXT: #pragma clang module end /*Mod*/ +// CHECK-NOT: #pragma clang module import + +//--- module.modulemap +module Mod { header "Mod.h" } +//--- Mod.h +#pragma once +//--- tu.c +#include "Mod.h" +#include "Mod.h" _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
