Author: Kadir Cetinkaya Date: 2023-06-16T09:26:45+02:00 New Revision: 4d0cfa6d09e2a84cceb669949d72df58c031a8c5
URL: https://github.com/llvm/llvm-project/commit/4d0cfa6d09e2a84cceb669949d72df58c031a8c5 DIFF: https://github.com/llvm/llvm-project/commit/4d0cfa6d09e2a84cceb669949d72df58c031a8c5.diff LOG: [clang] Don't create import decls without -fmodules 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). Differential Revision: https://reviews.llvm.org/D152274 Added: clang/test/Modules/Inputs/modulemaps-nomodules/header.h clang/test/Modules/Inputs/modulemaps-nomodules/module.modulemap 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 Modified: clang/lib/Sema/SemaModule.cpp clang/test/Modules/getSourceDescriptor-crash.cpp Removed: ################################################################################ diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp index 67c6556e00ddf..5ce5330b09466 100644 --- a/clang/lib/Sema/SemaModule.cpp +++ b/clang/lib/Sema/SemaModule.cpp @@ -636,11 +636,9 @@ void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { TUKind == TU_Module && getSourceManager().isWrittenInMainFile(DirectiveLoc); - bool ShouldAddImport = !IsInModuleIncludes; - - // If this module import was due to an inclusion directive, create an - // implicit import declaration to capture it in the AST. - if (ShouldAddImport) { + // If we are really importing a module (not just checking layering) due to an + // #include in the main file, synthesize an ImportDecl. + if (getLangOpts().Modules && !IsInModuleIncludes) { TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, DirectiveLoc, Mod, diff --git a/clang/test/Modules/Inputs/modulemaps-nomodules/header.h b/clang/test/Modules/Inputs/modulemaps-nomodules/header.h new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/clang/test/Modules/Inputs/modulemaps-nomodules/module.modulemap b/clang/test/Modules/Inputs/modulemaps-nomodules/module.modulemap new file mode 100644 index 0000000000000..dcae7e5a37c3a --- /dev/null +++ b/clang/test/Modules/Inputs/modulemaps-nomodules/module.modulemap @@ -0,0 +1,3 @@ +module M { + private header "header.h" +} diff --git a/clang/test/Modules/getSourceDescriptor-crash.cpp b/clang/test/Modules/getSourceDescriptor-crash.cpp index 84e527aef91b5..53111786472f7 100644 --- a/clang/test/Modules/getSourceDescriptor-crash.cpp +++ b/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" diff --git a/clang/test/Modules/modulemaps-nomodules.cpp b/clang/test/Modules/modulemaps-nomodules.cpp new file mode 100644 index 0000000000000..eec024fa24b47 --- /dev/null +++ b/clang/test/Modules/modulemaps-nomodules.cpp @@ -0,0 +1,8 @@ +// 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-error{{use of private header from outside its module: 'header.h'}} +// CHECK-NOT: ImportDecl diff --git a/clang/test/PCH/Inputs/modulemaps-nomodules/header.h b/clang/test/PCH/Inputs/modulemaps-nomodules/header.h new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/clang/test/PCH/Inputs/modulemaps-nomodules/module.modulemap b/clang/test/PCH/Inputs/modulemaps-nomodules/module.modulemap new file mode 100644 index 0000000000000..15b101f4be60e --- /dev/null +++ b/clang/test/PCH/Inputs/modulemaps-nomodules/module.modulemap @@ -0,0 +1,3 @@ +module M { + header "header.h" +} diff --git a/clang/test/PCH/modulemaps-nomodules.cpp b/clang/test/PCH/modulemaps-nomodules.cpp new file mode 100644 index 0000000000000..c18fabe4ac856 --- /dev/null +++ b/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" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits