Skip to content

Commit 09c37ac

Browse files
committed
[Preprocessor] Do not expand macros if the input is already preprocessed
1 parent d6ca7b9 commit 09c37ac

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

clang/include/clang/Lex/Preprocessor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,11 @@ class Preprocessor {
18311831
MacroExpansionInDirectivesOverride = true;
18321832
}
18331833

1834+
void SetDisableMacroExpansion() {
1835+
DisableMacroExpansion = true;
1836+
MacroExpansionInDirectivesOverride = false;
1837+
}
1838+
18341839
/// Peeks ahead N tokens and returns that token without consuming any
18351840
/// tokens.
18361841
///

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,13 @@ void clang::InitializePreprocessor(Preprocessor &PP,
15581558
const PCHContainerReader &PCHContainerRdr,
15591559
const FrontendOptions &FEOpts,
15601560
const CodeGenOptions &CodeGenOpts) {
1561+
1562+
if (all_of(FEOpts.Inputs,
1563+
[](const FrontendInputFile &FI) { return FI.isPreprocessed(); })) {
1564+
PP.SetDisableMacroExpansion();
1565+
return;
1566+
}
1567+
15611568
const LangOptions &LangOpts = PP.getLangOpts();
15621569
std::string PredefineBuffer;
15631570
PredefineBuffer.reserve(4080);

clang/test/Preprocessor/preprocess-cpp-output.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// RUN: %clang_cc1 -E -x c %s | FileCheck %s --check-prefixes=EXPANDED
2-
// RUN: %clang_cc1 -E -x cpp-output %s | FileCheck %s --check-prefixes=EXPANDED
2+
// RUN: %clang_cc1 -E -x cpp-output %s | FileCheck %s --check-prefixes=NOT-EXPANDED
33

44
// EXPANDED: void __attribute__((__attribute__((always_inline)))) foo()
5+
// NOT-EXPANDED: void __attribute__((always_inline)) foo()
56

67
#define always_inline __attribute__((always_inline))
78
void __attribute__((always_inline)) foo() {

0 commit comments

Comments
 (0)