Skip to content

Commit 2298cdc

Browse files
committed
[Clang][Preprocessor] Enable macro expansion **only** in directives when parsing preprocessed sources
The preprocessor does not expand macro-identifiers in #pragma directives. When we preprocess & parse the code, clang expands the macros in the directives. When we parse already preprocessed code, clang still has to expand the macros in the directives. This also means that we're not always able to parse the preprocessor's output without preserving the definitions (-dD).
1 parent 279487b commit 2298cdc

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

clang/include/clang/Frontend/FrontendAction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class FrontendAction {
8585
/// EndSourceFileAction() will not be called.
8686
virtual bool BeginSourceFileAction(CompilerInstance &CI) {
8787
if (CurrentInput.isPreprocessed())
88-
CI.getPreprocessor().SetEnableMacroExpansion(false);
88+
CI.getPreprocessor().SetMacroExpansionOnlyInDirectives();
8989
return true;
9090
}
9191

@@ -103,7 +103,7 @@ class FrontendAction {
103103
virtual void EndSourceFileAction() {
104104
if (CurrentInput.isPreprocessed())
105105
// reset the preprocessor macro expansion to the default
106-
getCompilerInstance().getPreprocessor().SetEnableMacroExpansion(true);
106+
getCompilerInstance().getPreprocessor().SetEnableMacroExpansion();
107107
}
108108

109109
/// Callback at the end of processing a single input, to determine

clang/include/clang/Lex/Preprocessor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,9 @@ class Preprocessor {
18371837
MacroExpansionInDirectivesOverride = true;
18381838
}
18391839

1840-
void SetEnableMacroExpansion(bool Enable) { DisableMacroExpansion = !Enable; }
1840+
void SetEnableMacroExpansion() {
1841+
DisableMacroExpansion = MacroExpansionInDirectivesOverride = false;
1842+
}
18411843

18421844
/// Peeks ahead N tokens and returns that token without consuming any
18431845
/// tokens.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %clang_cc1 -E -x c %s | FileCheck %s
22
// RUN: %clang_cc1 -x c -fsyntax-only %s -verify
3+
// RUN: %clang_cc1 -x cpp-output -fsyntax-only -verify %s
34
// expected-no-diagnostics
4-
// RUN: %clang_cc1 -x cpp-output -fsyntax-only -verify=fail %s
55

66
// The preprocessor does not expand macro-identifiers in #pragma directives.
77
// When we preprocess & parse the code, clang expands the macros in directives.
@@ -14,7 +14,7 @@
1414

1515
void foo() {
1616
// CHECK: #pragma unroll FACTOR
17-
#pragma unroll FACTOR // fail-error{{use of undeclared identifier}}
17+
#pragma unroll FACTOR
1818
for(;;) {
1919
}
2020
return;

0 commit comments

Comments
 (0)