From bad8977786382739256f4bd966fb4cdcfd50be2a Mon Sep 17 00:00:00 2001 From: Kirill Bobyrev Date: Fri, 26 Nov 2021 10:10:49 +0100 Subject: [PATCH] [clang] Change ordering of PreableCallbacks to make sure PP can be referenced in them Currently, BeforeExecute is called before BeginSourceFile which does not allow using PP in the callbacks. Change the ordering to ensure it is possible. This is a prerequisite for D114370. Originated from a discussion with @kadircet. Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D114525 --- clang/include/clang/Frontend/PrecompiledPreamble.h | 4 ++-- clang/lib/Frontend/PrecompiledPreamble.cpp | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/Frontend/PrecompiledPreamble.h b/clang/include/clang/Frontend/PrecompiledPreamble.h index bb7fd97..dacbffe 100644 --- a/clang/include/clang/Frontend/PrecompiledPreamble.h +++ b/clang/include/clang/Frontend/PrecompiledPreamble.h @@ -274,7 +274,7 @@ class PreambleCallbacks { public: virtual ~PreambleCallbacks() = default; - /// Called before FrontendAction::BeginSourceFile. + /// Called before FrontendAction::Execute. /// Can be used to store references to various CompilerInstance fields /// (e.g. SourceManager) that may be interesting to the consumers of other /// callbacks. @@ -291,7 +291,7 @@ public: /// used instead, but having only this method allows a simpler API. virtual void HandleTopLevelDecl(DeclGroupRef DG); /// Creates wrapper class for PPCallbacks so we can also process information - /// about includes that are inside of a preamble + /// about includes that are inside of a preamble. Called after BeforeExecute. virtual std::unique_ptr createPPCallbacks(); /// The returned CommentHandler will be added to the preprocessor if not null. virtual CommentHandler *getCommentHandler(); diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp index af82ab3..8aa80a4 100644 --- a/clang/lib/Frontend/PrecompiledPreamble.cpp +++ b/clang/lib/Frontend/PrecompiledPreamble.cpp @@ -412,10 +412,13 @@ llvm::ErrorOr PrecompiledPreamble::Build( std::unique_ptr Act; Act.reset(new PrecompilePreambleAction( StoreInMemory ? &Storage.asMemory().Data : nullptr, Callbacks)); - Callbacks.BeforeExecute(*Clang); if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) return BuildPreambleError::BeginSourceFileFailed; + // Performed after BeginSourceFile to ensure Clang->Preprocessor can be + // referenced in the callback. + Callbacks.BeforeExecute(*Clang); + std::unique_ptr DelegatedPPCallbacks = Callbacks.createPPCallbacks(); if (DelegatedPPCallbacks) -- 2.7.4