Skip to content

Commit

Permalink
Provide a backup plan if we fail to delay the handling of declarations.
Browse files Browse the repository at this point in the history
This patch enhances the work done in vgvassilev#766 where we assumed the clients are going
to be well-behaved such as clang or clang-repl. However, we can have clients
which connect the clang components in a non-standard way and we should provide a
reasonable fallback.

This patch detects if the setup is non-standard and teaches clad to handle
requests as they come, pretty much the same way as before vgvassilev#766 was implemented.
Of course this comes with a cost when we try to differentiate declarations that
are defined later in the translation unit. We expect such setups to be rare and
mostly in the cases of incremental processing and old repls such as Cling.
  • Loading branch information
vgvassilev committed May 8, 2024
1 parent 999a9eb commit 69a2d0c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tools/ClangPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ namespace clad {
SetRequestOptions(opts);
DiffCollector collector(DGR, CladEnabledRange, m_DiffRequestGraph, S,
opts);
// We could not delay the processing of derivatives, inform act as if each
// call is final. That would still have vgvassilev/clad#248 unresolved.
if (!m_Multiplexer)
FinalizeTranslationUnit();
}

FunctionDecl* CladPlugin::ProcessDiffRequest(DiffRequest& request) {
Expand Down Expand Up @@ -394,7 +398,7 @@ namespace clad {
SetTBRAnalysisOptions(m_DO, opts);
}

void CladPlugin::HandleTranslationUnit(ASTContext& C) {
void CladPlugin::FinalizeTranslationUnit() {
Sema& S = m_CI.getSema();
// Restore the TUScope that became a 0 in Sema::ActOnEndOfTranslationUnit.
if (!m_CI.getPreprocessor().isIncrementalProcessingEnabled())
Expand All @@ -418,7 +422,10 @@ namespace clad {
// Force emission of the produced pending template instantiations.
LocalInstantiations.perform();
GlobalInstantiations.perform();
}

void CladPlugin::HandleTranslationUnit(ASTContext& C) {
FinalizeTranslationUnit();
SendToMultiplexer();
m_Multiplexer->HandleTranslationUnit(C);
}
Expand Down
14 changes: 14 additions & 0 deletions tools/ClangPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Timer.h"

namespace clang {
Expand Down Expand Up @@ -167,6 +168,14 @@ class CladTimerGroup {
AppendDelayed({CallKind::HandleCXXStaticMemberVarInstantiation, D});
}
bool HandleTopLevelDecl(clang::DeclGroupRef D) override {
if (D.isSingleDecl())
if (auto* FD = llvm::dyn_cast<clang::FunctionDecl>(D.getSingleDecl()))
if (m_DFC.IsDerivative(FD)) {
assert(!m_Multiplexer &&
"Must happen only if we failed to rearrange the consumers");
return true;
}

HandleTopLevelDeclForClad(D);
AppendDelayed({CallKind::HandleTopLevelDecl, D});
return true; // happyness, continue parsing
Expand Down Expand Up @@ -250,6 +259,7 @@ class CladTimerGroup {
"Must start from index 0!");
m_DelayedCalls.push_back(DCI);
}
void FinalizeTranslationUnit();
void SendToMultiplexer();
bool CheckBuiltins();
void SetRequestOptions(RequestOptions& opts) const;
Expand All @@ -258,6 +268,10 @@ class CladTimerGroup {
DelayedCallInfo DCI{CallKind::HandleTopLevelDecl, D};
assert(!llvm::is_contained(m_DelayedCalls, DCI) && "Already exists!");
AppendDelayed(DCI);
// We could not delay the process due to some strange way of
// initialization, inform the consumers now.
if (!m_Multiplexer)
m_CI.getASTConsumer().HandleTopLevelDecl(DCI.m_DGR);
}
void HandleTopLevelDeclForClad(clang::DeclGroupRef DGR);
};
Expand Down

0 comments on commit 69a2d0c

Please sign in to comment.