[clang] [Preprocessor] Do not expand macros if the input is already preprocessed (PR #137665)
Juan Manuel Martinez CaamaƱo via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 28 09:33:24 PDT 2025
https://github.com/jmmartinez created https://github.com/llvm/llvm-project/pull/137665
This is a draft while I'm trying to figure out what's left to do.
This has issues with the test `Modules/initializers.cpp`.
The function `CompilerInstance::createModuleFromSource` creates a `FrontendInput` that "is preprocessed" from the preprocessed source string that is passed as argument (the module contents). However, this does not seem preprocessed (at least for this test).
https://github.com/llvm/llvm-project/blob/ca21508080031c3eda1c6085f1de9cc26be4c336/clang/lib/Frontend/CompilerInstance.cpp#L2243
I'm not familiar with modules and I'm not sure if it's this patch, or the test that has issues.
Other failures are fixed with https://github.com/llvm/llvm-project/pull/137623
>From 2a227b877994f41c9cbdc8eaf444571dfb5f333b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?= <juamarti at amd.com>
Date: Mon, 28 Apr 2025 17:05:46 +0200
Subject: [PATCH 1/2] Pre-commit test: [Preprocessor] Do not expand macros if
the input is already preprocessed
---
clang/test/Preprocessor/preprocess-cpp-output.c | 9 +++++++++
1 file changed, 9 insertions(+)
create mode 100644 clang/test/Preprocessor/preprocess-cpp-output.c
diff --git a/clang/test/Preprocessor/preprocess-cpp-output.c b/clang/test/Preprocessor/preprocess-cpp-output.c
new file mode 100644
index 0000000000000..59ff057e9b871
--- /dev/null
+++ b/clang/test/Preprocessor/preprocess-cpp-output.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -E -x c %s | FileCheck %s --check-prefixes=EXPANDED
+// RUN: %clang_cc1 -E -x cpp-output %s | FileCheck %s --check-prefixes=EXPANDED
+
+// EXPANDED: void __attribute__((__attribute__((always_inline)))) foo()
+
+#define always_inline __attribute__((always_inline))
+void __attribute__((always_inline)) foo() {
+ return 4;
+}
>From 4ed49c6c842221be8b3ce21e522934329cb4ef0d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?= <juamarti at amd.com>
Date: Tue, 22 Apr 2025 18:40:37 +0200
Subject: [PATCH 2/2] [Preprocessor] Do not expand macros if the input is
already preprocessed
---
clang/include/clang/Lex/Preprocessor.h | 5 +++++
clang/lib/Frontend/InitPreprocessor.cpp | 7 +++++++
clang/test/Preprocessor/preprocess-cpp-output.c | 3 ++-
3 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index f2dfd3a349b8b..63774e48a468b 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -1831,6 +1831,11 @@ class Preprocessor {
MacroExpansionInDirectivesOverride = true;
}
+ void SetDisableMacroExpansion() {
+ DisableMacroExpansion = true;
+ MacroExpansionInDirectivesOverride = false;
+ }
+
/// Peeks ahead N tokens and returns that token without consuming any
/// tokens.
///
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 1f297f228fc1b..6693cfb469f82 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1556,6 +1556,13 @@ void clang::InitializePreprocessor(Preprocessor &PP,
const PCHContainerReader &PCHContainerRdr,
const FrontendOptions &FEOpts,
const CodeGenOptions &CodeGenOpts) {
+
+ if (all_of(FEOpts.Inputs,
+ [](const FrontendInputFile &FI) { return FI.isPreprocessed(); })) {
+ PP.SetDisableMacroExpansion();
+ return;
+ }
+
const LangOptions &LangOpts = PP.getLangOpts();
std::string PredefineBuffer;
PredefineBuffer.reserve(4080);
diff --git a/clang/test/Preprocessor/preprocess-cpp-output.c b/clang/test/Preprocessor/preprocess-cpp-output.c
index 59ff057e9b871..2c180601e30ac 100644
--- a/clang/test/Preprocessor/preprocess-cpp-output.c
+++ b/clang/test/Preprocessor/preprocess-cpp-output.c
@@ -1,7 +1,8 @@
// RUN: %clang_cc1 -E -x c %s | FileCheck %s --check-prefixes=EXPANDED
-// RUN: %clang_cc1 -E -x cpp-output %s | FileCheck %s --check-prefixes=EXPANDED
+// RUN: %clang_cc1 -E -x cpp-output %s | FileCheck %s --check-prefixes=NOT-EXPANDED
// EXPANDED: void __attribute__((__attribute__((always_inline)))) foo()
+// NOT-EXPANDED: void __attribute__((always_inline)) foo()
#define always_inline __attribute__((always_inline))
void __attribute__((always_inline)) foo() {
More information about the cfe-commits
mailing list