Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Preprocessor] Do not expand macros if the input is already preprocessed #137665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jmmartinez
Copy link
Contributor

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).

InputKind::ModuleMap, /*Preprocessed*/true));

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 #137623

@jmmartinez jmmartinez requested a review from lamb-j April 28, 2025 16:33
@jmmartinez jmmartinez self-assigned this Apr 28, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 28, 2025

@llvm/pr-subscribers-clang

Author: Juan Manuel Martinez Caamaño (jmmartinez)

Changes

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).

InputKind::ModuleMap, /*Preprocessed*/true));

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 #137623


Full diff: https://github.com/llvm/llvm-project/pull/137665.diff

3 Files Affected:

  • (modified) clang/include/clang/Lex/Preprocessor.h (+5)
  • (modified) clang/lib/Frontend/InitPreprocessor.cpp (+7)
  • (added) clang/test/Preprocessor/preprocess-cpp-output.c (+10)
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
new file mode 100644
index 0000000000000..2c180601e30ac
--- /dev/null
+++ b/clang/test/Preprocessor/preprocess-cpp-output.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -E -x c %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() {
+    return 4;
+}

@jmmartinez jmmartinez requested a review from yxsamliu April 29, 2025 15:16
@shafik shafik requested review from cor3ntin and tahonermann April 29, 2025 17:19
@Bigcheese
Copy link
Contributor

module maps don't get preprocessed, so I don't think it matters what that is set to. For normal module.modulemap files Preprocessed is false. I think it's fine to remove the true and see if anything breaks, the preprocessor just skips over that section anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants