[clang] [Modules] [HeaderSearch] Don't reenter headers if it is pragma once o… (PR #76119)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 20 19:28:02 PST 2023


https://github.com/ChuanqiXu9 created https://github.com/llvm/llvm-project/pull/76119

…f #import

Close https://github.com/llvm/llvm-project/issues/73023

The direct issue of https://github.com/llvm/llvm-project/issues/73023 is that we entered a header which is marked as pragma once since the compiler think it is OK if there is controlling macro.

It doesn't make sense. I feel like it should be sufficient to skip it after we see the '#pragma once'.

>From the context, it looks like the workaround is primarily for ObjectiveC. So we might need reviewers from OC.

>From 6610a2599318f1e7ae6be6295f2d4e8371ac44f1 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Thu, 21 Dec 2023 11:24:14 +0800
Subject: [PATCH] [Modules] [HeaderSearch] Don't reenter headers if it is
 pragma once of #import

Close https://github.com/llvm/llvm-project/issues/73023

The direct issue of https://github.com/llvm/llvm-project/issues/73023 is
that we entered a header which is marked as pragma once since the
compiler think it is OK if there is controlling macro.

It doesn't make sense. I feel like it should be sufficient to skip it
after we see the '#pragma once'.

>From the context, it looks like the workaround is primarily for
ObjectiveC. So we might need reviewers from OC.
---
 clang/lib/Lex/HeaderSearch.cpp |  2 +-
 clang/test/Modules/pr73023.cpp | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Modules/pr73023.cpp

diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp
index 0f1090187734ff..9020a661321faf 100644
--- a/clang/lib/Lex/HeaderSearch.cpp
+++ b/clang/lib/Lex/HeaderSearch.cpp
@@ -1458,7 +1458,7 @@ bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
   } else {
     // Otherwise, if this is a #include of a file that was previously #import'd
     // or if this is the second #include of a #pragma once file, ignore it.
-    if ((FileInfo.isPragmaOnce || FileInfo.isImport) && !TryEnterImported())
+    if (FileInfo.isPragmaOnce || FileInfo.isImport)
       return false;
   }
 
diff --git a/clang/test/Modules/pr73023.cpp b/clang/test/Modules/pr73023.cpp
new file mode 100644
index 00000000000000..d80306d39ef48c
--- /dev/null
+++ b/clang/test/Modules/pr73023.cpp
@@ -0,0 +1,20 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 %t/foo.cpp -I%t -fsyntax-only -verify
+
+//--- i.h
+#ifndef FOO_H
+#pragma once
+struct S{};
+#endif
+
+//--- foo.cpp
+// expected-no-diagnostics
+#include "i.h"
+#include "i.h"
+
+int foo() {
+    return sizeof(S);
+}



More information about the cfe-commits mailing list