[PATCH] D152274: [clang] Don't create import decls without -fmodules

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 6 07:38:45 PDT 2023


kadircet created this revision.
kadircet added reviewers: sammccall, dblaikie.
Herald added a subscriber: ChuanqiXu.
Herald added a project: All.
kadircet requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When modules are disabled, there's no loaded module for these import
decls to point at. This results in crashes when there are modulemap
files but no -fmodules flag (this configuration is used for layering
check violations).

This patch makes sure import declarations are introduced only when
modules are enabled, which makes this case similar to textual headers
(no import decls are created for #include of textual headers from a
modulemap).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152274

Files:
  clang/lib/Sema/SemaModule.cpp
  clang/test/Modules/Inputs/modulemaps-nomodules/header.h
  clang/test/Modules/Inputs/modulemaps-nomodules/module.modulemap
  clang/test/Modules/getSourceDescriptor-crash.cpp
  clang/test/Modules/modulemaps-nomodules.cpp
  clang/test/PCH/Inputs/modulemaps-nomodules/header.h
  clang/test/PCH/Inputs/modulemaps-nomodules/module.modulemap
  clang/test/PCH/modulemaps-nomodules.cpp


Index: clang/test/PCH/modulemaps-nomodules.cpp
===================================================================
--- /dev/null
+++ clang/test/PCH/modulemaps-nomodules.cpp
@@ -0,0 +1,6 @@
+// Make sure we don't crash when serializing a PCH with an include from a
+// modulemap file in nomodules mode.
+// No need to pass -fno-modules explicitly, absence implies negation for cc1.
+// RUN: %clang_cc1 -I %S/Inputs/modulemaps-nomodules -fmodule-map-file=%S/Inputs/modulemaps-nomodules/module.modulemap %s -emit-pch -o /dev/null
+
+#include "header.h"
Index: clang/test/PCH/Inputs/modulemaps-nomodules/module.modulemap
===================================================================
--- /dev/null
+++ clang/test/PCH/Inputs/modulemaps-nomodules/module.modulemap
@@ -0,0 +1,3 @@
+module M {
+  header "header.h"
+}
Index: clang/test/Modules/modulemaps-nomodules.cpp
===================================================================
--- /dev/null
+++ clang/test/Modules/modulemaps-nomodules.cpp
@@ -0,0 +1,9 @@
+// Make sure we treat includes that are part of modulemaps the same as textual
+// headers when modules are not enabled (e.g do't generate import decls, but
+// still perform layering checks).
+// No need to pass -fno-modules explicitly, absence implies negation for cc1.
+// RUN: %clang_cc1 -I %S/Inputs/modulemaps-nomodules -fmodule-map-file=%S/Inputs/modulemaps-nomodules/module.modulemap %s -verify -ast-dump | FileCheck %s
+
+#include "header.h"
+// expected-no-diagnostics
+// CHECK-NOT: ImportDecl
Index: clang/test/Modules/getSourceDescriptor-crash.cpp
===================================================================
--- clang/test/Modules/getSourceDescriptor-crash.cpp
+++ clang/test/Modules/getSourceDescriptor-crash.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -I %S/Inputs/getSourceDescriptor-crash -S -emit-llvm -debug-info-kind=limited -debugger-tuning=lldb -fimplicit-module-maps %s -o - | FileCheck %s
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -I %S/Inputs/getSourceDescriptor-crash -S -emit-llvm -debug-info-kind=limited -debugger-tuning=lldb -fmodules -fmodules-cache-path=%t -fimplicit-module-maps %s -o - | FileCheck %s
 
 #include "h1.h"
 #include "h1.h"
Index: clang/test/Modules/Inputs/modulemaps-nomodules/module.modulemap
===================================================================
--- /dev/null
+++ clang/test/Modules/Inputs/modulemaps-nomodules/module.modulemap
@@ -0,0 +1,3 @@
+module M {
+  header "header.h"
+}
Index: clang/lib/Sema/SemaModule.cpp
===================================================================
--- clang/lib/Sema/SemaModule.cpp
+++ clang/lib/Sema/SemaModule.cpp
@@ -636,7 +636,7 @@
       TUKind == TU_Module &&
       getSourceManager().isWrittenInMainFile(DirectiveLoc);
 
-  bool ShouldAddImport = !IsInModuleIncludes;
+  bool ShouldAddImport = !IsInModuleIncludes && getLangOpts().Modules;
 
   // If this module import was due to an inclusion directive, create an
   // implicit import declaration to capture it in the AST.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152274.528862.patch
Type: text/x-patch
Size: 2998 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230606/951ef5b6/attachment.bin>


More information about the cfe-commits mailing list