Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clang/include/clang/Frontend/FrontendAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class FrontendAction {
/// \return True on success; on failure ExecutionAction() and
/// EndSourceFileAction() will not be called.
virtual bool BeginSourceFileAction(CompilerInstance &CI) {
if (CurrentInput.isPreprocessed())
CI.getPreprocessor().SetMacroExpansionOnlyInDirectives();
return true;
}

Expand All @@ -98,7 +100,11 @@ class FrontendAction {
///
/// This is guaranteed to only be called following a successful call to
/// BeginSourceFileAction (and BeginSourceFile).
virtual void EndSourceFileAction() {}
virtual void EndSourceFileAction() {
if (CurrentInput.isPreprocessed())
// Reset the preprocessor macro expansion to the default.
getCompilerInstance().getPreprocessor().SetEnableMacroExpansion();
}

/// Callback at the end of processing a single input, to determine
/// if the output files should be erased or not.
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,10 @@ class Preprocessor {
MacroExpansionInDirectivesOverride = true;
}

void SetEnableMacroExpansion() {
DisableMacroExpansion = MacroExpansionInDirectivesOverride = false;
}

/// Peeks ahead N tokens and returns that token without consuming any
/// tokens.
///
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ bool CodeGenAction::loadLinkModules(CompilerInstance &CI) {
bool CodeGenAction::hasIRSupport() const { return true; }

void CodeGenAction::EndSourceFileAction() {
ASTFrontendAction::EndSourceFileAction();

// If the consumer creation failed, do nothing.
if (!getCompilerInstance().hasASTConsumer())
return;
Expand All @@ -932,7 +934,7 @@ CodeGenerator *CodeGenAction::getCodeGenerator() const {
bool CodeGenAction::BeginSourceFileAction(CompilerInstance &CI) {
if (CI.getFrontendOpts().GenReducedBMI)
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
return true;
return ASTFrontendAction::BeginSourceFileAction(CI);
}

static std::unique_ptr<raw_pwrite_stream>
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ bool GeneratePCHAction::shouldEraseOutputFiles() {

bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
CI.getLangOpts().CompilingPCH = true;
return true;
return ASTFrontendAction::BeginSourceFileAction(CI);
}

std::vector<std::unique_ptr<ASTConsumer>>
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Frontend/Rewrite/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ bool FixItAction::BeginSourceFileAction(CompilerInstance &CI) {
}
Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
CI.getLangOpts(), FixItOpts.get()));
return true;
return ASTFrontendAction::BeginSourceFileAction(CI);
}

void FixItAction::EndSourceFileAction() {
// Otherwise rewrite all files.
Rewriter->WriteFixedFiles();
ASTFrontendAction::EndSourceFileAction();
}

bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
Expand Down Expand Up @@ -298,7 +299,7 @@ bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) {
std::make_unique<RewriteImportsListener>(CI, OutputStream));
}

return true;
return PreprocessorFrontendAction::BeginSourceFileAction(CI);
}

void RewriteIncludesAction::ExecuteAction() {
Expand Down
10 changes: 10 additions & 0 deletions clang/test/Preprocessor/preprocess-cpp-output.c
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions clang/test/Preprocessor/preprocess-pragma-cpp-output.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %clang_cc1 -E -x c %s | FileCheck %s
// RUN: %clang_cc1 -x c -fsyntax-only %s -verify
// RUN: %clang_cc1 -x cpp-output -fsyntax-only -verify %s
// expected-no-diagnostics

// The preprocessor does not expand macro-identifiers in #pragma directives.
// When we preprocess & parse the code, clang expands the macros in directives.
// When we parse already preprocessed code, clang still has to expand the
// macros in the directives.
// This means that we're not always able to parse the preprocessor's output
// without preserving the definitions (-dD).

#define FACTOR 4

void foo() {
// CHECK: #pragma unroll FACTOR
#pragma unroll FACTOR
for(;;) {
}
return;
}